simon | 575c940 | 2022-10-25 16:21:40 -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 | */ |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 18 | import { |
| 19 | Box, |
| 20 | ListItem, |
| 21 | ListItemAvatar, |
| 22 | ListItemIcon, |
| 23 | ListItemText, |
| 24 | Menu, |
| 25 | MenuItem, |
| 26 | Stack, |
| 27 | Typography, |
| 28 | } from '@mui/material'; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 29 | import { Conversation } from 'jami-web-common'; |
| 30 | import { QRCodeCanvas } from 'qrcode.react'; |
| 31 | import { MouseEvent, useState } from 'react'; |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 32 | import { Trans } from 'react-i18next'; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 33 | import Modal from 'react-modal'; |
| 34 | import { useNavigate, useParams } from 'react-router-dom'; |
| 35 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 36 | import { useAuthContext } from '../contexts/AuthProvider'; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 37 | import { setRefreshFromSlice } from '../redux/appSlice'; |
| 38 | import { useAppDispatch } from '../redux/hooks'; |
| 39 | import ConversationAvatar from './ConversationAvatar'; |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 40 | import { |
| 41 | AudioCallIcon, |
| 42 | BlockContactIcon, |
| 43 | CancelIcon, |
| 44 | ContactDetailsIcon, |
| 45 | MessageIcon, |
| 46 | RemoveContactIcon, |
| 47 | VideoCallIcon, |
| 48 | } from './SvgIcon'; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 49 | |
| 50 | const cancelStyles: Modal.Styles = { |
| 51 | content: { |
| 52 | left: '94px', |
| 53 | width: '300px', |
| 54 | height: '220px', |
| 55 | background: '#FFFFFF 0% 0% no-repeat padding-box', |
| 56 | boxShadow: '3px 3px 7px #00000029', |
| 57 | borderRadius: '20px', |
| 58 | opacity: '1', |
| 59 | |
| 60 | textAlign: 'left', |
| 61 | font: 'normal normal normal 12px/26px Ubuntu', |
| 62 | letterSpacing: '0px', |
| 63 | color: '#000000', |
| 64 | }, |
| 65 | }; |
| 66 | |
| 67 | const contactDetailsStyles: Modal.Styles = { |
| 68 | content: { |
| 69 | left: '94px', |
| 70 | width: '450px', |
| 71 | height: '450px', |
| 72 | background: '#FFFFFF 0% 0% no-repeat padding-box', |
| 73 | boxShadow: '3px 3px 7px #00000029', |
| 74 | borderRadius: '20px', |
| 75 | opacity: '1', |
| 76 | |
| 77 | textAlign: 'left', |
| 78 | font: 'normal normal normal 12px/26px Ubuntu', |
| 79 | letterSpacing: '0px', |
| 80 | color: '#000000', |
| 81 | }, |
| 82 | }; |
| 83 | |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 84 | const iconColor = '#005699'; |
| 85 | |
| 86 | type ConversationListItemProps = { |
| 87 | conversation: Conversation; |
| 88 | }; |
| 89 | |
| 90 | export default function ConversationListItem({ conversation }: ConversationListItemProps) { |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 91 | const { axiosInstance } = useAuthContext(); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 92 | const { conversationId, contactId } = useParams(); |
| 93 | const dispatch = useAppDispatch(); |
| 94 | |
| 95 | const pathId = conversationId || contactId; |
| 96 | const isSelected = conversation.getDisplayUri() === pathId; |
| 97 | const navigate = useNavigate(); |
| 98 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 99 | const [menuAnchorEl, setMenuAnchorEl] = useState<HTMLElement | null>(null); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 100 | const [modalDetailsIsOpen, setModalDetailsIsOpen] = useState(false); |
| 101 | const [modalDeleteIsOpen, setModalDeleteIsOpen] = useState(false); |
| 102 | const [blockOrRemove, setBlockOrRemove] = useState(true); |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 103 | const [userId] = useState(conversation?.getFirstMember()?.contact.getUri()); |
| 104 | const [isSwarm] = useState(true); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 105 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 106 | const openMenu = (e: MouseEvent<HTMLDivElement>) => { |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 107 | e.preventDefault(); |
| 108 | console.log(e); |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 109 | setMenuAnchorEl(e.currentTarget); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 110 | }; |
| 111 | const openModalDetails = () => setModalDetailsIsOpen(true); |
| 112 | const openModalDelete = () => setModalDeleteIsOpen(true); |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 113 | const closeModal = () => setMenuAnchorEl(null); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 114 | const closeModalDetails = () => setModalDetailsIsOpen(false); |
| 115 | const closeModalDelete = () => setModalDeleteIsOpen(false); |
| 116 | |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 117 | const getContactDetails = async () => { |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 118 | const controller = new AbortController(); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 119 | try { |
Misha Krieger-Raynauld | 46e9d24 | 2022-11-12 18:02:43 -0500 | [diff] [blame] | 120 | const { data } = await axiosInstance.get(`/contacts/${userId}`, { |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 121 | signal: controller.signal, |
| 122 | }); |
| 123 | console.log('CONTACT LIST - DETAILS: ', data); |
| 124 | } catch (e) { |
| 125 | console.log('ERROR GET CONTACT DETAILS: ', e); |
| 126 | } |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 127 | }; |
| 128 | |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 129 | const removeOrBlock = async (block = false) => { |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 130 | setBlockOrRemove(false); |
| 131 | |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 132 | const controller = new AbortController(); |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 133 | let url = `/contacts/${userId}`; |
| 134 | if (block) { |
| 135 | url += '/block'; |
| 136 | } |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 137 | try { |
| 138 | await axiosInstance(url, { |
| 139 | signal: controller.signal, |
| 140 | method: block ? 'POST' : 'DELETE', |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 141 | }); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 142 | dispatch(setRefreshFromSlice()); |
| 143 | } catch (e) { |
| 144 | console.error(`Error ${block ? 'blocking' : 'removing'} contact : `, e); |
| 145 | dispatch(setRefreshFromSlice()); |
| 146 | } |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 147 | closeModalDelete(); |
| 148 | }; |
| 149 | |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 150 | const uri = conversation.getId() ? `/conversation/${conversation.getId()}` : `/add-contact/${userId}`; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 151 | return ( |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 152 | <div onContextMenu={openMenu}> |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 153 | <div> |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 154 | <Menu open={!!menuAnchorEl} onClose={closeModal} anchorEl={menuAnchorEl}> |
| 155 | <MenuItem |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 156 | onClick={() => { |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 157 | navigate(uri); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 158 | closeModal(); |
| 159 | }} |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 160 | > |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 161 | <ListItemIcon> |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 162 | <MessageIcon style={{ color: iconColor }} /> |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 163 | </ListItemIcon> |
| 164 | <ListItemText> |
| 165 | <Trans i18nKey="conversation_message" /> |
| 166 | </ListItemText> |
| 167 | </MenuItem> |
| 168 | <MenuItem |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 169 | onClick={() => { |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 170 | navigate(`/account/call/${conversation.getId()}`); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 171 | }} |
| 172 | > |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 173 | <ListItemIcon> |
| 174 | <AudioCallIcon style={{ color: iconColor }} /> |
| 175 | </ListItemIcon> |
| 176 | <ListItemText> |
| 177 | <Trans i18nKey="conversation_start_audiocall" /> |
| 178 | </ListItemText> |
| 179 | </MenuItem> |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 180 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 181 | <MenuItem |
| 182 | onClick={() => { |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 183 | navigate(`call/${conversation.getId()}?video=true`); |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 184 | }} |
| 185 | > |
| 186 | <ListItemIcon> |
| 187 | <VideoCallIcon style={{ color: iconColor }} /> |
| 188 | </ListItemIcon> |
| 189 | <ListItemText> |
| 190 | <Trans i18nKey="conversation_start_videocall" /> |
| 191 | </ListItemText> |
| 192 | </MenuItem> |
| 193 | |
| 194 | {isSelected && ( |
| 195 | <MenuItem |
| 196 | onClick={() => { |
simon | 3f5f3e7 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 197 | navigate(`/`); |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 198 | closeModal(); |
| 199 | }} |
| 200 | > |
| 201 | <ListItemIcon> |
| 202 | <CancelIcon style={{ color: iconColor }} /> |
| 203 | </ListItemIcon> |
| 204 | <ListItemText> |
| 205 | <Trans i18nKey="conversation_close" /> |
| 206 | </ListItemText> |
| 207 | </MenuItem> |
| 208 | )} |
| 209 | |
| 210 | <MenuItem |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 211 | onClick={() => { |
| 212 | console.log('open details contact for: '); |
| 213 | closeModal(); |
| 214 | openModalDetails(); |
| 215 | getContactDetails(); |
| 216 | }} |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 217 | > |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 218 | <ListItemIcon> |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 219 | <ContactDetailsIcon style={{ color: iconColor }} /> |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 220 | </ListItemIcon> |
| 221 | <ListItemText> |
| 222 | <Trans i18nKey="conversation_details" /> |
| 223 | </ListItemText> |
| 224 | </MenuItem> |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 225 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 226 | <MenuItem |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 227 | onClick={() => { |
| 228 | setBlockOrRemove(true); |
| 229 | closeModal(); |
| 230 | openModalDelete(); |
| 231 | }} |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 232 | > |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 233 | <ListItemIcon> |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 234 | <BlockContactIcon style={{ color: iconColor }} /> |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 235 | </ListItemIcon> |
| 236 | <ListItemText> |
| 237 | <Trans i18nKey="conversation_block_contact" /> |
| 238 | </ListItemText> |
| 239 | </MenuItem> |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 240 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 241 | <MenuItem |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 242 | onClick={() => { |
| 243 | setBlockOrRemove(false); |
| 244 | closeModal(); |
| 245 | openModalDelete(); |
| 246 | }} |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 247 | > |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 248 | <ListItemIcon> |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 249 | <RemoveContactIcon style={{ color: iconColor }} /> |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 250 | </ListItemIcon> |
| 251 | <ListItemText> |
| 252 | <Trans i18nKey="conversation_delete_contact" /> |
| 253 | </ListItemText> |
| 254 | </MenuItem> |
| 255 | </Menu> |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 256 | </div> |
| 257 | |
| 258 | <div> |
| 259 | <Modal |
| 260 | isOpen={modalDetailsIsOpen} |
| 261 | onRequestClose={closeModalDetails} |
| 262 | style={contactDetailsStyles} |
| 263 | contentLabel="Détails contact" |
| 264 | > |
| 265 | <Stack direction={'row'} alignContent="flex-end"> |
| 266 | <Stack direction={'column'}> |
| 267 | <div style={{ height: '100px' }}> |
| 268 | <ConversationAvatar displayName={conversation.getDisplayNameNoFallback()} /> |
| 269 | </div> |
| 270 | |
| 271 | <div |
| 272 | style={{ |
| 273 | fontSize: '20px', |
| 274 | marginBottom: '20px', |
| 275 | height: '20px', |
| 276 | }} |
| 277 | > |
| 278 | Informations |
| 279 | </div> |
| 280 | |
| 281 | <Typography variant="caption">Nom d'utilisateur</Typography> |
| 282 | <div style={{ height: '20px' }} /> |
| 283 | <Typography variant="caption">Identifiant </Typography> |
| 284 | <div style={{ height: '20px' }} /> |
| 285 | |
| 286 | <div |
| 287 | style={{ |
| 288 | flex: 1, |
| 289 | height: '150px', |
| 290 | flexDirection: 'column', |
| 291 | // alignSelf: "flex-end", |
| 292 | }} |
| 293 | > |
| 294 | <Typography variant="caption">Code QR</Typography> |
| 295 | </div> |
| 296 | |
| 297 | <Typography variant="caption">est un swarm </Typography> |
| 298 | </Stack> |
| 299 | |
| 300 | <Stack direction={'column'}> |
| 301 | <div |
| 302 | style={{ |
| 303 | fontWeight: 'bold', |
| 304 | fontSize: '20px', |
| 305 | height: '100px', |
| 306 | }} |
| 307 | > |
| 308 | {conversation.getDisplayNameNoFallback() + '(resolved name)'} |
| 309 | </div> |
| 310 | |
| 311 | <div |
| 312 | style={{ |
| 313 | height: '40px', |
| 314 | }} |
| 315 | /> |
| 316 | <Typography variant="caption"> |
| 317 | <div style={{ fontWeight: 'bold' }}>{conversation.getDisplayNameNoFallback()}</div> |
| 318 | </Typography> |
| 319 | |
| 320 | <div style={{ height: '20px' }} /> |
| 321 | |
| 322 | <Typography variant="caption"> |
| 323 | <div style={{ fontWeight: 'bold' }}> {userId}</div> |
| 324 | </Typography> |
| 325 | |
| 326 | <div style={{ height: '20px' }} /> |
| 327 | |
| 328 | <div> |
| 329 | <QRCodeCanvas size={40} value={`${userId}`} /> |
| 330 | </div> |
| 331 | |
| 332 | <Typography variant="caption"> |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 333 | <div style={{ fontWeight: 'bold' }}> {String(isSwarm)}</div> |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 334 | </Typography> |
| 335 | </Stack> |
| 336 | </Stack> |
| 337 | <div |
| 338 | onClick={closeModalDetails} |
| 339 | style={{ |
| 340 | width: '100px', |
| 341 | borderStyle: 'solid', |
| 342 | textAlign: 'center', |
| 343 | borderRadius: '5px', |
| 344 | marginLeft: '150px', |
| 345 | marginTop: '10px', |
| 346 | }} |
| 347 | > |
| 348 | <Typography variant="caption">Fermer</Typography> |
| 349 | </div> |
| 350 | </Modal> |
| 351 | </div> |
| 352 | |
| 353 | <div> |
| 354 | <Modal |
| 355 | isOpen={modalDeleteIsOpen} |
| 356 | onRequestClose={closeModalDelete} |
| 357 | style={cancelStyles} |
| 358 | contentLabel="Merci de confirmer" |
| 359 | > |
| 360 | <Typography variant="h4">Merci de confirmer</Typography> |
| 361 | <Stack direction={'column'} justifyContent="space-around" spacing={'75px'}> |
| 362 | <div style={{ textAlign: 'center', marginTop: '10%' }}> |
| 363 | <Typography variant="body2"> |
| 364 | Voulez vous vraiment {blockOrRemove ? 'bloquer' : 'supprimer'} ce contact? |
| 365 | </Typography> |
| 366 | </div> |
| 367 | |
| 368 | <Stack direction={'row'} top={'25px'} alignSelf="center" spacing={1}> |
| 369 | <Box |
| 370 | onClick={() => { |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 371 | if (blockOrRemove) removeOrBlock(true); |
| 372 | else removeOrBlock(false); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 373 | }} |
| 374 | style={{ |
| 375 | width: '100px', |
| 376 | textAlign: 'center', |
| 377 | borderStyle: 'solid', |
| 378 | borderColor: 'red', |
| 379 | borderRadius: '10px', |
| 380 | color: 'red', |
| 381 | }} |
| 382 | > |
| 383 | {blockOrRemove ? 'Bloquer' : 'Supprimer'} |
| 384 | </Box> |
| 385 | <Box |
| 386 | onClick={closeModalDelete} |
| 387 | style={{ |
| 388 | width: '100px', |
| 389 | textAlign: 'center', |
| 390 | paddingLeft: '12px', |
| 391 | paddingRight: '12px', |
| 392 | borderStyle: 'solid', |
| 393 | borderRadius: '10px', |
| 394 | }} |
| 395 | > |
| 396 | Annuler |
| 397 | </Box> |
| 398 | </Stack> |
| 399 | </Stack> |
| 400 | </Modal> |
| 401 | </div> |
| 402 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 403 | <ListItem button alignItems="flex-start" selected={isSelected} onClick={() => navigate(uri)}> |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 404 | <ListItemAvatar> |
| 405 | <ConversationAvatar displayName={conversation.getDisplayNameNoFallback()} /> |
| 406 | </ListItemAvatar> |
| 407 | <ListItemText primary={conversation.getDisplayName()} secondary={conversation.getDisplayUri()} /> |
| 408 | </ListItem> |
| 409 | </div> |
| 410 | ); |
| 411 | } |