simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 18 | import { AddRounded } from '@mui/icons-material'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 19 | import { Avatar, Card, CardHeader, Container, List } from '@mui/material'; |
Adrien Béraud | 6c93496 | 2021-06-07 10:13:26 -0400 | [diff] [blame] | 20 | import { motion } from 'framer-motion'; |
simon | 2007698 | 2022-10-11 15:04:13 -0400 | [diff] [blame] | 21 | import { Account } from 'jami-web-common'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 22 | import { Fragment, useEffect, useState } from 'react'; |
| 23 | import { useNavigate } from 'react-router'; |
| 24 | |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 25 | import authManager from '../AuthManager'; |
| 26 | import ConversationAvatar from '../components/ConversationAvatar'; |
| 27 | import Header from '../components/Header'; |
| 28 | import ListItemLink from '../components/ListItemLink'; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 29 | import LoadingPage from '../components/Loading'; |
Adrien Béraud | 6c93496 | 2021-06-07 10:13:26 -0400 | [diff] [blame] | 30 | |
| 31 | const variants = { |
| 32 | enter: { opacity: 1, y: 0 }, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 33 | exit: { opacity: 0, y: '-50px' }, |
| 34 | }; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 35 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 36 | const AccountSelection = () => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 37 | const navigate = useNavigate(); |
| 38 | const [loaded, setLoaded] = useState(false); |
| 39 | const [error, setError] = useState(false); |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 40 | const [accounts, setAccounts] = useState<Account[]>([]); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 41 | |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 42 | useEffect(() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 43 | const controller = new AbortController(); |
| 44 | authManager |
| 45 | .fetch(`/api/accounts`, { signal: controller.signal }) |
| 46 | .then((res) => res.json()) |
| 47 | .then( |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 48 | (result: Account[]) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 49 | console.log(result); |
| 50 | if (result.length === 0) { |
| 51 | navigate('/newAccount'); |
| 52 | } else { |
| 53 | setLoaded(true); |
| 54 | setAccounts(result.map((account) => Account.from(account))); |
| 55 | } |
| 56 | }, |
| 57 | (error) => { |
| 58 | console.log(`get error ${error}`); |
| 59 | setLoaded(true); |
| 60 | setError(true); |
Adrien Béraud | e5cad98 | 2021-06-07 10:05:50 -0400 | [diff] [blame] | 61 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 62 | ) |
| 63 | .catch((e) => console.log(e)); |
| 64 | // return () => controller.abort() // crash on React18 |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 65 | }, [navigate]); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 66 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 67 | if (!loaded) return <LoadingPage />; |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 68 | return ( |
Adrien Béraud | 023f7cf | 2022-09-18 14:57:53 -0400 | [diff] [blame] | 69 | <Fragment> |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 70 | <Header /> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 71 | <Container maxWidth="sm" style={{ paddingBottom: 32 }}> |
Adrien Béraud | 6c93496 | 2021-06-07 10:13:26 -0400 | [diff] [blame] | 72 | <motion.div drag="x" initial="exit" animate="enter" exit="exit" variants={variants}> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 73 | <Card style={{ marginTop: 32, marginBottom: 32 }}> |
| 74 | <CardHeader title="Choose an account" /> |
| 75 | <List> |
| 76 | {accounts.map((account) => ( |
| 77 | <ListItemLink |
| 78 | key={account.getId()} |
| 79 | icon={<ConversationAvatar displayName={account.getDisplayNameNoFallback()} />} |
| 80 | to={`/account/${account.getId()}/settings`} |
| 81 | primary={account.getDisplayName()} |
| 82 | secondary={account.getDisplayUri()} |
| 83 | /> |
| 84 | ))} |
| 85 | <ListItemLink |
| 86 | icon={ |
| 87 | <Avatar> |
| 88 | <AddRounded /> |
| 89 | </Avatar> |
| 90 | } |
| 91 | to="/newAccount" |
| 92 | primary="Create new account" |
| 93 | /> |
| 94 | </List> |
| 95 | </Card> |
Adrien Béraud | 6c93496 | 2021-06-07 10:13:26 -0400 | [diff] [blame] | 96 | </motion.div> |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 97 | </Container> |
Adrien Béraud | 023f7cf | 2022-09-18 14:57:53 -0400 | [diff] [blame] | 98 | </Fragment> |
| 99 | ); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 100 | }; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 101 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 102 | export default AccountSelection; |