blob: 2086f14e1914cbbd427551cb4687c2b91c8a7350 [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 */
simon07b4eb02022-09-29 17:50:26 -040018import { AddCircle, DeleteRounded, GroupRounded, PhoneCallbackRounded } from '@mui/icons-material';
simond47ef9e2022-09-28 22:24:28 -040019import {
simon07b4eb02022-09-29 17:50:26 -040020 Card,
21 CardContent,
22 Grid,
23 IconButton,
simond47ef9e2022-09-28 22:24:28 -040024 List,
25 ListItem,
simon07b4eb02022-09-29 17:50:26 -040026 ListItemAvatar,
simond47ef9e2022-09-28 22:24:28 -040027 ListItemIcon,
28 ListItemSecondaryAction,
29 ListItemText,
30 ListSubheader,
simond47ef9e2022-09-28 22:24:28 -040031 Paper,
simon07b4eb02022-09-29 17:50:26 -040032 Switch,
simond47ef9e2022-09-28 22:24:28 -040033 TextField,
simon07b4eb02022-09-29 17:50:26 -040034 Toolbar,
35 Typography,
simond47ef9e2022-09-28 22:24:28 -040036} from '@mui/material';
simon07b4eb02022-09-29 17:50:26 -040037import { motion } from 'framer-motion';
simon416d0792022-11-03 02:46:18 -040038import { Account, AccountDetails } from 'jami-web-common';
simon07b4eb02022-09-29 17:50:26 -040039import { useState } from 'react';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040040
simon416d0792022-11-03 02:46:18 -040041import { useAuthContext } from '../contexts/AuthProvider';
simond47ef9e2022-09-28 22:24:28 -040042import ConversationAvatar from './ConversationAvatar';
43import ConversationsOverviewCard from './ConversationsOverviewCard';
simon07b4eb02022-09-29 17:50:26 -040044import JamiIdCard from './JamiIdCard';
Adrien Béraud4e287b92021-04-24 16:15:56 -040045
simond47ef9e2022-09-28 22:24:28 -040046const transition = { duration: 0.3, ease: [0.43, 0.13, 0.23, 0.96] };
Adrien Béraud6c934962021-06-07 10:13:26 -040047
48const thumbnailVariants = {
49 initial: { scale: 0.9, opacity: 0 },
50 enter: { scale: 1, opacity: 1, transition },
51 exit: {
52 scale: 0.5,
53 opacity: 0,
simonfe1de722022-10-02 00:21:43 -040054 transition: { ...transition, duration: 1.5 },
simond47ef9e2022-09-28 22:24:28 -040055 },
56};
Adrien Béraud6c934962021-06-07 10:13:26 -040057
simon94fe53e2022-11-10 12:51:58 -050058export default function AccountPreferences() {
59 const { account, axiosInstance } = useAuthContext();
simon416d0792022-11-03 02:46:18 -040060
simonfe1de722022-10-02 00:21:43 -040061 const devices: string[][] = [];
62 const accountDevices = account.getDevices();
63 for (const i in accountDevices) devices.push([i, accountDevices[i]]);
Adrien Béraud4e287b92021-04-24 16:15:56 -040064
simond47ef9e2022-09-28 22:24:28 -040065 console.log(devices);
ervinanohf1758a42022-09-14 14:52:51 -040066
67 const isJamiAccount = account.getType() === Account.TYPE_JAMI;
simond47ef9e2022-09-28 22:24:28 -040068 const alias = isJamiAccount ? 'Jami account' : 'SIP account';
ervinanohf1758a42022-09-14 14:52:51 -040069 const moderators = account.getDefaultModerators();
simond47ef9e2022-09-28 22:24:28 -040070 const [defaultModeratorUri, setDefaultModeratorUri] = useState('');
ervinanohf1758a42022-09-14 14:52:51 -040071
72 const [details, setDetails] = useState(account.getDetails());
Adrien Béraud86986032021-04-25 12:04:53 -040073
simon94fe53e2022-11-10 12:51:58 -050074 const addModerator = async () => {
Adrien Béraud4e287b92021-04-24 16:15:56 -040075 if (defaultModeratorUri) {
simon94fe53e2022-11-10 12:51:58 -050076 await axiosInstance.put(`/default-moderators/${defaultModeratorUri}`);
simond47ef9e2022-09-28 22:24:28 -040077 setDefaultModeratorUri('');
Adrien Béraud4e287b92021-04-24 16:15:56 -040078 }
ervinanohf1758a42022-09-14 14:52:51 -040079 };
Adrien Béraud4e287b92021-04-24 16:15:56 -040080
simon94fe53e2022-11-10 12:51:58 -050081 const removeModerator = async (uri: string) => await axiosInstance.delete(`/default-moderators/${uri}`);
Adrien Béraud4e287b92021-04-24 16:15:56 -040082
simon94fe53e2022-11-10 12:51:58 -050083 const handleToggle = async (key: keyof AccountDetails, value: boolean) => {
ervinanohf1758a42022-09-14 14:52:51 -040084 console.log(`handleToggle ${key} ${value}`);
simonfe1de722022-10-02 00:21:43 -040085 const newDetails: Partial<AccountDetails> = {};
simond47ef9e2022-09-28 22:24:28 -040086 newDetails[key] = value ? 'true' : 'false';
ervinanohf1758a42022-09-14 14:52:51 -040087 console.log(newDetails);
simon94fe53e2022-11-10 12:51:58 -050088 await axiosInstance.patch('/account', newDetails);
ervinanohf1758a42022-09-14 14:52:51 -040089 setDetails({ ...account.updateDetails(newDetails) });
90 };
Adrien Béraud86986032021-04-25 12:04:53 -040091
Adrien Béraud150b4782021-04-21 19:40:59 -040092 return (
Adrien Béraud6c934962021-06-07 10:13:26 -040093 <motion.div
ervinanohf1758a42022-09-14 14:52:51 -040094 initial="initial"
95 animate="enter"
96 exit="exit"
97 variants={{
98 enter: { transition: { staggerChildren: 0.05 } },
99 exit: { transition: { staggerChildren: 0.02 } },
100 }}
101 >
102 <motion.div variants={thumbnailVariants}>
103 <Typography variant="h2" component="h2" gutterBottom>
104 {alias}
105 </Typography>
106 </motion.div>
Adrien Béraud21c53cf2021-04-22 00:04:32 -0400107 <Grid container spacing={3} style={{ marginBottom: 16 }}>
ervinanohf1758a42022-09-14 14:52:51 -0400108 {isJamiAccount && (
109 <Grid item xs={12}>
110 <motion.div variants={thumbnailVariants}>
111 <JamiIdCard account={account} />
112 </motion.div>
113 </Grid>
114 )}
Adrien Béraud150b4782021-04-21 19:40:59 -0400115
116 <Grid item xs={12} sm={6}>
ervinanohf1758a42022-09-14 14:52:51 -0400117 <motion.div variants={thumbnailVariants}>
simon5da8ca62022-11-09 15:21:25 -0500118 <ConversationsOverviewCard />
Adrien Béraud6c934962021-06-07 10:13:26 -0400119 </motion.div>
Adrien Béraud150b4782021-04-21 19:40:59 -0400120 </Grid>
121
122 <Grid item xs={12} sm={6}>
ervinanohf1758a42022-09-14 14:52:51 -0400123 <motion.div variants={thumbnailVariants}>
124 <Card>
125 <CardContent>
126 <Typography color="textSecondary" gutterBottom>
127 Current calls
128 </Typography>
129 <Typography gutterBottom variant="h5" component="h2">
130 0
131 </Typography>
132 </CardContent>
133 </Card>
Adrien Béraud6c934962021-06-07 10:13:26 -0400134 </motion.div>
Adrien Béraud150b4782021-04-21 19:40:59 -0400135 </Grid>
Adrien Béraud6c934962021-06-07 10:13:26 -0400136
ervinanohf1758a42022-09-14 14:52:51 -0400137 <Grid item xs={12} sm={6}>
138 <motion.div variants={thumbnailVariants}>
139 <Card>
140 <CardContent>
141 <Typography color="textSecondary" gutterBottom>
142 Appareils associés
143 </Typography>
144 <Typography gutterBottom variant="h5" component="h2">
simon80b7b3b2022-09-28 17:50:10 -0400145 {devices.map((device, i) => (
146 <ListItem key={i}>
ervinanohf1758a42022-09-14 14:52:51 -0400147 <GroupRounded />
simond47ef9e2022-09-28 22:24:28 -0400148 <ListItemText id="switch-list-label-rendezvous" primary={device[1]} secondary={device[0]} />
ervinanohf1758a42022-09-14 14:52:51 -0400149 </ListItem>
150 ))}
151 {/* <ListItemTextsion> */}
152 </Typography>
153 </CardContent>
154 </Card>
155 </motion.div>
156 </Grid>
Adrien Béraud150b4782021-04-21 19:40:59 -0400157 </Grid>
158
ervinanohf1758a42022-09-14 14:52:51 -0400159 <List
160 subheader={
161 <motion.div variants={thumbnailVariants}>
162 <ListSubheader>Settings</ListSubheader>
163 </motion.div>
164 }
165 >
166 <motion.div variants={thumbnailVariants}>
167 <ListItem>
168 <ListItemIcon>
169 <GroupRounded />
170 </ListItemIcon>
simond47ef9e2022-09-28 22:24:28 -0400171 <ListItemText id="switch-list-label-rendezvous" primary="Rendez-Vous point" />
ervinanohf1758a42022-09-14 14:52:51 -0400172 <ListItemSecondaryAction>
173 <Switch
174 edge="end"
simond47ef9e2022-09-28 22:24:28 -0400175 onChange={(e) => handleToggle('Account.rendezVous', e.target.checked)}
176 checked={details['Account.rendezVous'] === 'true'}
ervinanohf1758a42022-09-14 14:52:51 -0400177 inputProps={{
simond47ef9e2022-09-28 22:24:28 -0400178 'aria-labelledby': 'switch-list-label-rendezvous',
ervinanohf1758a42022-09-14 14:52:51 -0400179 }}
180 />
181 </ListItemSecondaryAction>
182 </ListItem>
Adrien Béraud6c934962021-06-07 10:13:26 -0400183 </motion.div>
184 <motion.div variants={thumbnailVariants}>
ervinanohf1758a42022-09-14 14:52:51 -0400185 <ListItem>
186 <ListItemIcon>
187 <PhoneCallbackRounded />
188 </ListItemIcon>
simond47ef9e2022-09-28 22:24:28 -0400189 <ListItemText id="switch-list-label-publicin" primary="Allow connection from unkown peers" />
ervinanohf1758a42022-09-14 14:52:51 -0400190 <ListItemSecondaryAction>
191 <Switch
192 edge="end"
simond47ef9e2022-09-28 22:24:28 -0400193 onChange={(e) => handleToggle('DHT.PublicInCalls', e.target.checked)}
194 checked={details['DHT.PublicInCalls'] === 'true'}
195 inputProps={{ 'aria-labelledby': 'switch-list-label-publicin' }}
ervinanohf1758a42022-09-14 14:52:51 -0400196 />
197 </ListItemSecondaryAction>
198 </ListItem>
Adrien Béraud6c934962021-06-07 10:13:26 -0400199 </motion.div>
200 <motion.div variants={thumbnailVariants}>
ervinanohf1758a42022-09-14 14:52:51 -0400201 <Paper>
202 <Toolbar>
203 <Typography variant="h6">Default moderators</Typography>
204 </Toolbar>
205 <List>
206 <ListItem key="add">
207 <TextField
208 variant="outlined"
209 value={defaultModeratorUri}
210 onChange={(e) => setDefaultModeratorUri(e.target.value)}
211 label="Add new default moderator"
212 placeholder="Enter new moderator name or URI"
213 fullWidth
214 />
215 <ListItemSecondaryAction>
216 <IconButton onClick={addModerator} size="large">
217 <AddCircle />
218 </IconButton>
219 </ListItemSecondaryAction>
220 </ListItem>
221 {!moderators || moderators.length === 0 ? (
222 <ListItem key="placeholder">
223 <ListItemText primary="No default moderator" />
Adrien Béraud21c53cf2021-04-22 00:04:32 -0400224 </ListItem>
ervinanohf1758a42022-09-14 14:52:51 -0400225 ) : (
226 moderators.map((moderator) => (
simonfe1de722022-10-02 00:21:43 -0400227 <ListItem key={moderator.getUri()}>
ervinanohf1758a42022-09-14 14:52:51 -0400228 <ListItemAvatar>
simonfe1de722022-10-02 00:21:43 -0400229 <ConversationAvatar displayName={moderator.getDisplayName()} />
ervinanohf1758a42022-09-14 14:52:51 -0400230 </ListItemAvatar>
231 <ListItemText primary={moderator.getDisplayName()} />
232 <ListItemSecondaryAction>
simon416d0792022-11-03 02:46:18 -0400233 <IconButton onClick={() => removeModerator(moderator.getUri())} size="large">
ervinanohf1758a42022-09-14 14:52:51 -0400234 <DeleteRounded />
235 </IconButton>
236 </ListItemSecondaryAction>
237 </ListItem>
238 ))
239 )}
240 </List>
241 </Paper>
Adrien Béraud6c934962021-06-07 10:13:26 -0400242 </motion.div>
Adrien Béraud150b4782021-04-21 19:40:59 -0400243 </List>
Adrien Béraudab519ff2022-05-03 15:34:48 -0400244 </motion.div>
245 );
Adrien Béraud150b4782021-04-21 19:40:59 -0400246}