blob: 35aed97fcac3d11aff5ff675d6c28e90a815425e [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
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éraudab519ff2022-05-03 15:34:48 -040018import { AddRounded } from '@mui/icons-material';
simon07b4eb02022-09-29 17:50:26 -040019import { Avatar, Card, CardHeader, Container, List } from '@mui/material';
Adrien Béraud6c934962021-06-07 10:13:26 -040020import { motion } from 'framer-motion';
simon20076982022-10-11 15:04:13 -040021import { Account } from 'jami-web-common';
simon07b4eb02022-09-29 17:50:26 -040022import { Fragment, useEffect, useState } from 'react';
23import { useNavigate } from 'react-router';
24
simon07b4eb02022-09-29 17:50:26 -040025import authManager from '../AuthManager';
26import ConversationAvatar from '../components/ConversationAvatar';
27import Header from '../components/Header';
28import ListItemLink from '../components/ListItemLink';
simon6b9ddfb2022-10-03 00:04:50 -040029import LoadingPage from '../components/Loading';
Adrien Béraud6c934962021-06-07 10:13:26 -040030
31const variants = {
32 enter: { opacity: 1, y: 0 },
simond47ef9e2022-09-28 22:24:28 -040033 exit: { opacity: 0, y: '-50px' },
34};
Adrien Béraud6ecaa402021-04-06 17:37:25 -040035
simonfe1de722022-10-02 00:21:43 -040036const AccountSelection = () => {
simond47ef9e2022-09-28 22:24:28 -040037 const navigate = useNavigate();
38 const [loaded, setLoaded] = useState(false);
39 const [error, setError] = useState(false);
simonfe1de722022-10-02 00:21:43 -040040 const [accounts, setAccounts] = useState<Account[]>([]);
Adrien Béraud6ecaa402021-04-06 17:37:25 -040041
Adrien Béraud150b4782021-04-21 19:40:59 -040042 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040043 const controller = new AbortController();
44 authManager
45 .fetch(`/api/accounts`, { signal: controller.signal })
46 .then((res) => res.json())
47 .then(
simonfe1de722022-10-02 00:21:43 -040048 (result: Account[]) => {
simond47ef9e2022-09-28 22:24:28 -040049 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éraude5cad982021-06-07 10:05:50 -040061 }
simond47ef9e2022-09-28 22:24:28 -040062 )
63 .catch((e) => console.log(e));
64 // return () => controller.abort() // crash on React18
simon80b7b3b2022-09-28 17:50:10 -040065 }, [navigate]);
Adrien Béraud6ecaa402021-04-06 17:37:25 -040066
simond47ef9e2022-09-28 22:24:28 -040067 if (!loaded) return <LoadingPage />;
Adrien Béraud150b4782021-04-21 19:40:59 -040068 return (
Adrien Béraud023f7cf2022-09-18 14:57:53 -040069 <Fragment>
Adrien Béraud150b4782021-04-21 19:40:59 -040070 <Header />
simond47ef9e2022-09-28 22:24:28 -040071 <Container maxWidth="sm" style={{ paddingBottom: 32 }}>
Adrien Béraud6c934962021-06-07 10:13:26 -040072 <motion.div drag="x" initial="exit" animate="enter" exit="exit" variants={variants}>
simond47ef9e2022-09-28 22:24:28 -040073 <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éraud6c934962021-06-07 10:13:26 -040096 </motion.div>
Adrien Béraud150b4782021-04-21 19:40:59 -040097 </Container>
Adrien Béraud023f7cf2022-09-18 14:57:53 -040098 </Fragment>
99 );
simond47ef9e2022-09-28 22:24:28 -0400100};
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400101
simond47ef9e2022-09-28 22:24:28 -0400102export default AccountSelection;