blob: 2eedc97e1cb41663fee8eaaa29827e21b6e54901 [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';
simon20076982022-10-11 15:04:13 -040038import { Account } from 'jami-web-common';
39import { AccountDetails } from 'jami-web-common';
simon07b4eb02022-09-29 17:50:26 -040040import { useState } from 'react';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040041
simon07b4eb02022-09-29 17:50:26 -040042import authManager from '../AuthManager';
simond47ef9e2022-09-28 22:24:28 -040043import ConversationAvatar from './ConversationAvatar';
44import ConversationsOverviewCard from './ConversationsOverviewCard';
simon07b4eb02022-09-29 17:50:26 -040045import JamiIdCard from './JamiIdCard';
Adrien Béraud4e287b92021-04-24 16:15:56 -040046
simond47ef9e2022-09-28 22:24:28 -040047const transition = { duration: 0.3, ease: [0.43, 0.13, 0.23, 0.96] };
Adrien Béraud6c934962021-06-07 10:13:26 -040048
49const thumbnailVariants = {
50 initial: { scale: 0.9, opacity: 0 },
51 enter: { scale: 1, opacity: 1, transition },
52 exit: {
53 scale: 0.5,
54 opacity: 0,
simonfe1de722022-10-02 00:21:43 -040055 transition: { ...transition, duration: 1.5 },
simond47ef9e2022-09-28 22:24:28 -040056 },
57};
Adrien Béraud6c934962021-06-07 10:13:26 -040058
simonfe1de722022-10-02 00:21:43 -040059type AccountPreferencesProps = {
60 account: Account;
simonfe1de722022-10-02 00:21:43 -040061};
62
63export default function AccountPreferences({ account }: AccountPreferencesProps) {
64 const devices: string[][] = [];
65 const accountDevices = account.getDevices();
66 for (const i in accountDevices) devices.push([i, accountDevices[i]]);
Adrien Béraud4e287b92021-04-24 16:15:56 -040067
simond47ef9e2022-09-28 22:24:28 -040068 console.log(devices);
ervinanohf1758a42022-09-14 14:52:51 -040069
70 const isJamiAccount = account.getType() === Account.TYPE_JAMI;
simond47ef9e2022-09-28 22:24:28 -040071 const alias = isJamiAccount ? 'Jami account' : 'SIP account';
ervinanohf1758a42022-09-14 14:52:51 -040072 const moderators = account.getDefaultModerators();
simond47ef9e2022-09-28 22:24:28 -040073 const [defaultModeratorUri, setDefaultModeratorUri] = useState('');
ervinanohf1758a42022-09-14 14:52:51 -040074
75 const [details, setDetails] = useState(account.getDetails());
Adrien Béraud86986032021-04-25 12:04:53 -040076
Adrien Béraud4e287b92021-04-24 16:15:56 -040077 const addModerator = () => {
78 if (defaultModeratorUri) {
simond47ef9e2022-09-28 22:24:28 -040079 authManager.fetch(`/api/accounts/${account.getId()}/defaultModerators/${defaultModeratorUri}`, { method: 'PUT' });
80 setDefaultModeratorUri('');
Adrien Béraud4e287b92021-04-24 16:15:56 -040081 }
ervinanohf1758a42022-09-14 14:52:51 -040082 };
Adrien Béraud4e287b92021-04-24 16:15:56 -040083
simonfe1de722022-10-02 00:21:43 -040084 const removeModerator = (uri: string) =>
simond47ef9e2022-09-28 22:24:28 -040085 authManager.fetch(`/api/accounts/${account.getId()}/defaultModerators/${uri}`, { method: 'DELETE' });
Adrien Béraud4e287b92021-04-24 16:15:56 -040086
simonfe1de722022-10-02 00:21:43 -040087 const handleToggle = (key: keyof AccountDetails, value: boolean) => {
ervinanohf1758a42022-09-14 14:52:51 -040088 console.log(`handleToggle ${key} ${value}`);
simonfe1de722022-10-02 00:21:43 -040089 const newDetails: Partial<AccountDetails> = {};
simond47ef9e2022-09-28 22:24:28 -040090 newDetails[key] = value ? 'true' : 'false';
ervinanohf1758a42022-09-14 14:52:51 -040091 console.log(newDetails);
Adrien Béraud86986032021-04-25 12:04:53 -040092 authManager.fetch(`/api/accounts/${account.getId()}`, {
simond47ef9e2022-09-28 22:24:28 -040093 method: 'POST',
Adrien Béraud86986032021-04-25 12:04:53 -040094 headers: {
simond47ef9e2022-09-28 22:24:28 -040095 Accept: 'application/json',
96 'Content-Type': 'application/json',
Adrien Béraud86986032021-04-25 12:04:53 -040097 },
ervinanohf1758a42022-09-14 14:52:51 -040098 body: JSON.stringify(newDetails),
99 });
100 setDetails({ ...account.updateDetails(newDetails) });
101 };
Adrien Béraud86986032021-04-25 12:04:53 -0400102
Adrien Béraud150b4782021-04-21 19:40:59 -0400103 return (
Adrien Béraud6c934962021-06-07 10:13:26 -0400104 <motion.div
ervinanohf1758a42022-09-14 14:52:51 -0400105 initial="initial"
106 animate="enter"
107 exit="exit"
108 variants={{
109 enter: { transition: { staggerChildren: 0.05 } },
110 exit: { transition: { staggerChildren: 0.02 } },
111 }}
112 >
113 <motion.div variants={thumbnailVariants}>
114 <Typography variant="h2" component="h2" gutterBottom>
115 {alias}
116 </Typography>
117 </motion.div>
Adrien Béraud21c53cf2021-04-22 00:04:32 -0400118 <Grid container spacing={3} style={{ marginBottom: 16 }}>
ervinanohf1758a42022-09-14 14:52:51 -0400119 {isJamiAccount && (
120 <Grid item xs={12}>
121 <motion.div variants={thumbnailVariants}>
122 <JamiIdCard account={account} />
123 </motion.div>
124 </Grid>
125 )}
Adrien Béraud150b4782021-04-21 19:40:59 -0400126
127 <Grid item xs={12} sm={6}>
ervinanohf1758a42022-09-14 14:52:51 -0400128 <motion.div variants={thumbnailVariants}>
129 <ConversationsOverviewCard accountId={account.getId()} />
Adrien Béraud6c934962021-06-07 10:13:26 -0400130 </motion.div>
Adrien Béraud150b4782021-04-21 19:40:59 -0400131 </Grid>
132
133 <Grid item xs={12} sm={6}>
ervinanohf1758a42022-09-14 14:52:51 -0400134 <motion.div variants={thumbnailVariants}>
135 <Card>
136 <CardContent>
137 <Typography color="textSecondary" gutterBottom>
138 Current calls
139 </Typography>
140 <Typography gutterBottom variant="h5" component="h2">
141 0
142 </Typography>
143 </CardContent>
144 </Card>
Adrien Béraud6c934962021-06-07 10:13:26 -0400145 </motion.div>
Adrien Béraud150b4782021-04-21 19:40:59 -0400146 </Grid>
Adrien Béraud6c934962021-06-07 10:13:26 -0400147
ervinanohf1758a42022-09-14 14:52:51 -0400148 <Grid item xs={12} sm={6}>
149 <motion.div variants={thumbnailVariants}>
150 <Card>
151 <CardContent>
152 <Typography color="textSecondary" gutterBottom>
153 Appareils associés
154 </Typography>
155 <Typography gutterBottom variant="h5" component="h2">
simon80b7b3b2022-09-28 17:50:10 -0400156 {devices.map((device, i) => (
157 <ListItem key={i}>
ervinanohf1758a42022-09-14 14:52:51 -0400158 <GroupRounded />
simond47ef9e2022-09-28 22:24:28 -0400159 <ListItemText id="switch-list-label-rendezvous" primary={device[1]} secondary={device[0]} />
ervinanohf1758a42022-09-14 14:52:51 -0400160 </ListItem>
161 ))}
162 {/* <ListItemTextsion> */}
163 </Typography>
164 </CardContent>
165 </Card>
166 </motion.div>
167 </Grid>
Adrien Béraud150b4782021-04-21 19:40:59 -0400168 </Grid>
169
ervinanohf1758a42022-09-14 14:52:51 -0400170 <List
171 subheader={
172 <motion.div variants={thumbnailVariants}>
173 <ListSubheader>Settings</ListSubheader>
174 </motion.div>
175 }
176 >
177 <motion.div variants={thumbnailVariants}>
178 <ListItem>
179 <ListItemIcon>
180 <GroupRounded />
181 </ListItemIcon>
simond47ef9e2022-09-28 22:24:28 -0400182 <ListItemText id="switch-list-label-rendezvous" primary="Rendez-Vous point" />
ervinanohf1758a42022-09-14 14:52:51 -0400183 <ListItemSecondaryAction>
184 <Switch
185 edge="end"
simond47ef9e2022-09-28 22:24:28 -0400186 onChange={(e) => handleToggle('Account.rendezVous', e.target.checked)}
187 checked={details['Account.rendezVous'] === 'true'}
ervinanohf1758a42022-09-14 14:52:51 -0400188 inputProps={{
simond47ef9e2022-09-28 22:24:28 -0400189 'aria-labelledby': 'switch-list-label-rendezvous',
ervinanohf1758a42022-09-14 14:52:51 -0400190 }}
191 />
192 </ListItemSecondaryAction>
193 </ListItem>
Adrien Béraud6c934962021-06-07 10:13:26 -0400194 </motion.div>
195 <motion.div variants={thumbnailVariants}>
ervinanohf1758a42022-09-14 14:52:51 -0400196 <ListItem>
197 <ListItemIcon>
198 <PhoneCallbackRounded />
199 </ListItemIcon>
simond47ef9e2022-09-28 22:24:28 -0400200 <ListItemText id="switch-list-label-publicin" primary="Allow connection from unkown peers" />
ervinanohf1758a42022-09-14 14:52:51 -0400201 <ListItemSecondaryAction>
202 <Switch
203 edge="end"
simond47ef9e2022-09-28 22:24:28 -0400204 onChange={(e) => handleToggle('DHT.PublicInCalls', e.target.checked)}
205 checked={details['DHT.PublicInCalls'] === 'true'}
206 inputProps={{ 'aria-labelledby': 'switch-list-label-publicin' }}
ervinanohf1758a42022-09-14 14:52:51 -0400207 />
208 </ListItemSecondaryAction>
209 </ListItem>
Adrien Béraud6c934962021-06-07 10:13:26 -0400210 </motion.div>
211 <motion.div variants={thumbnailVariants}>
ervinanohf1758a42022-09-14 14:52:51 -0400212 <Paper>
213 <Toolbar>
214 <Typography variant="h6">Default moderators</Typography>
215 </Toolbar>
216 <List>
217 <ListItem key="add">
218 <TextField
219 variant="outlined"
220 value={defaultModeratorUri}
221 onChange={(e) => setDefaultModeratorUri(e.target.value)}
222 label="Add new default moderator"
223 placeholder="Enter new moderator name or URI"
224 fullWidth
225 />
226 <ListItemSecondaryAction>
227 <IconButton onClick={addModerator} size="large">
228 <AddCircle />
229 </IconButton>
230 </ListItemSecondaryAction>
231 </ListItem>
232 {!moderators || moderators.length === 0 ? (
233 <ListItem key="placeholder">
234 <ListItemText primary="No default moderator" />
Adrien Béraud21c53cf2021-04-22 00:04:32 -0400235 </ListItem>
ervinanohf1758a42022-09-14 14:52:51 -0400236 ) : (
237 moderators.map((moderator) => (
simonfe1de722022-10-02 00:21:43 -0400238 <ListItem key={moderator.getUri()}>
ervinanohf1758a42022-09-14 14:52:51 -0400239 <ListItemAvatar>
simonfe1de722022-10-02 00:21:43 -0400240 <ConversationAvatar displayName={moderator.getDisplayName()} />
ervinanohf1758a42022-09-14 14:52:51 -0400241 </ListItemAvatar>
242 <ListItemText primary={moderator.getDisplayName()} />
243 <ListItemSecondaryAction>
simonfe1de722022-10-02 00:21:43 -0400244 <IconButton onClick={(e) => removeModerator(moderator.getUri())} size="large">
ervinanohf1758a42022-09-14 14:52:51 -0400245 <DeleteRounded />
246 </IconButton>
247 </ListItemSecondaryAction>
248 </ListItem>
249 ))
250 )}
251 </List>
252 </Paper>
Adrien Béraud6c934962021-06-07 10:13:26 -0400253 </motion.div>
Adrien Béraud150b4782021-04-21 19:40:59 -0400254 </List>
Adrien Béraudab519ff2022-05-03 15:34:48 -0400255 </motion.div>
256 );
Adrien Béraud150b4782021-04-21 19:40:59 -0400257}