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 | */ |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 18 | import { Box, ListItem, ListItemAvatar, ListItemText } from '@mui/material'; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 19 | import { QRCodeCanvas } from 'qrcode.react'; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 20 | import { useCallback, useContext, useMemo, useState } from 'react'; |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 21 | import { useTranslation } from 'react-i18next'; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 22 | import { useNavigate } from 'react-router-dom'; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 23 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 24 | import { useAuthContext } from '../contexts/AuthProvider'; |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame^] | 25 | import { useConversationContext } from '../contexts/ConversationProvider'; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 26 | import { MessengerContext } from '../contexts/MessengerProvider'; |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 27 | import { useStartCall } from '../hooks/useStartCall'; |
Misha Krieger-Raynauld | 6bbdacf | 2022-11-29 21:45:40 -0500 | [diff] [blame] | 28 | import { Conversation } from '../models/Conversation'; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 29 | import { setRefreshFromSlice } from '../redux/appSlice'; |
| 30 | import { useAppDispatch } from '../redux/hooks'; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 31 | import ContextMenu, { ContextMenuHandler, useContextMenuHandler } from './ContextMenu'; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 32 | import ConversationAvatar from './ConversationAvatar'; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 33 | import { ConfirmationDialog, DialogContentList, InfosDialog, useDialogHandler } from './Dialog'; |
| 34 | import { PopoverListItemData } from './PopoverList'; |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 35 | import { |
| 36 | AudioCallIcon, |
| 37 | BlockContactIcon, |
| 38 | CancelIcon, |
| 39 | ContactDetailsIcon, |
| 40 | MessageIcon, |
| 41 | RemoveContactIcon, |
| 42 | VideoCallIcon, |
| 43 | } from './SvgIcon'; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 44 | |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 45 | type ConversationListItemProps = { |
| 46 | conversation: Conversation; |
| 47 | }; |
| 48 | |
| 49 | export default function ConversationListItem({ conversation }: ConversationListItemProps) { |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame^] | 50 | const conversationContext = useConversationContext(true); |
| 51 | const conversationId = conversationContext?.conversationId; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 52 | const contextMenuHandler = useContextMenuHandler(); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 53 | const { newContactId, setNewContactId } = useContext(MessengerContext); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 54 | |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame^] | 55 | const pathId = conversationId ?? newContactId; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 56 | const isSelected = conversation.getDisplayUri() === pathId; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 57 | |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 58 | const navigate = useNavigate(); |
Michelle Sepkap Sime | dd82cbf | 2022-11-17 23:31:49 -0500 | [diff] [blame] | 59 | const userId = conversation?.getFirstMember()?.contact.getUri(); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 60 | |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 61 | const onClick = useCallback(() => { |
| 62 | const newConversationId = conversation.getId(); |
| 63 | if (newConversationId) { |
| 64 | navigate(`/conversation/${newConversationId}`); |
| 65 | } else { |
| 66 | setNewContactId(userId); |
| 67 | } |
| 68 | }, [navigate, conversation, userId, setNewContactId]); |
| 69 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 70 | return ( |
| 71 | <Box onContextMenu={contextMenuHandler.handleAnchorPosition}> |
| 72 | <ConversationMenu |
| 73 | userId={userId} |
| 74 | conversation={conversation} |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 75 | onMessageClick={onClick} |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 76 | isSelected={isSelected} |
| 77 | contextMenuProps={contextMenuHandler.props} |
| 78 | /> |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 79 | <ListItem button alignItems="flex-start" selected={isSelected} onClick={onClick}> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 80 | <ListItemAvatar> |
| 81 | <ConversationAvatar displayName={conversation.getDisplayNameNoFallback()} /> |
| 82 | </ListItemAvatar> |
| 83 | <ListItemText primary={conversation.getDisplayName()} secondary={conversation.getDisplayUri()} /> |
| 84 | </ListItem> |
| 85 | </Box> |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | interface ConversationMenuProps { |
| 90 | userId: string; |
| 91 | conversation: Conversation; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 92 | onMessageClick: () => void; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 93 | isSelected: boolean; |
| 94 | contextMenuProps: ContextMenuHandler['props']; |
| 95 | } |
| 96 | |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 97 | const ConversationMenu = ({ |
| 98 | userId, |
| 99 | conversation, |
| 100 | onMessageClick, |
| 101 | isSelected, |
| 102 | contextMenuProps, |
| 103 | }: ConversationMenuProps) => { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 104 | const { t } = useTranslation(); |
| 105 | const { axiosInstance } = useAuthContext(); |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 106 | const [isSwarm] = useState(true); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 107 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 108 | const detailsDialogHandler = useDialogHandler(); |
| 109 | const blockContactDialogHandler = useDialogHandler(); |
| 110 | const removeContactDialogHandler = useDialogHandler(); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 111 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 112 | const navigate = useNavigate(); |
| 113 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 114 | const startCall = useStartCall(); |
| 115 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 116 | const getContactDetails = useCallback(async () => { |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 117 | const controller = new AbortController(); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 118 | try { |
Misha Krieger-Raynauld | 46e9d24 | 2022-11-12 18:02:43 -0500 | [diff] [blame] | 119 | const { data } = await axiosInstance.get(`/contacts/${userId}`, { |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 120 | signal: controller.signal, |
| 121 | }); |
| 122 | console.log('CONTACT LIST - DETAILS: ', data); |
| 123 | } catch (e) { |
| 124 | console.log('ERROR GET CONTACT DETAILS: ', e); |
| 125 | } |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 126 | }, [axiosInstance, userId]); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 127 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 128 | const conversationId = conversation.getId(); |
| 129 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 130 | const menuOptions: PopoverListItemData[] = useMemo( |
| 131 | () => [ |
| 132 | { |
| 133 | label: t('conversation_message'), |
| 134 | Icon: MessageIcon, |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 135 | onClick: onMessageClick, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 136 | }, |
| 137 | { |
| 138 | label: t('conversation_start_audiocall'), |
| 139 | Icon: AudioCallIcon, |
| 140 | onClick: () => { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 141 | if (conversationId) { |
Charlie | 380dc5e | 2022-11-29 16:51:42 -0500 | [diff] [blame] | 142 | startCall(conversationId); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 143 | } |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 144 | }, |
| 145 | }, |
| 146 | { |
| 147 | label: t('conversation_start_videocall'), |
| 148 | Icon: VideoCallIcon, |
| 149 | onClick: () => { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 150 | if (conversationId) { |
Charlie | 380dc5e | 2022-11-29 16:51:42 -0500 | [diff] [blame] | 151 | startCall(conversationId, { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 152 | isVideoOn: true, |
| 153 | }); |
| 154 | } |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 155 | }, |
| 156 | }, |
| 157 | ...(isSelected |
| 158 | ? [ |
| 159 | { |
| 160 | label: t('conversation_close'), |
| 161 | Icon: CancelIcon, |
| 162 | onClick: () => { |
| 163 | navigate(`/`); |
| 164 | }, |
| 165 | }, |
| 166 | ] |
| 167 | : []), |
| 168 | { |
| 169 | label: t('conversation_details'), |
| 170 | Icon: ContactDetailsIcon, |
| 171 | onClick: () => { |
| 172 | detailsDialogHandler.openDialog(); |
| 173 | getContactDetails(); |
| 174 | }, |
| 175 | }, |
| 176 | { |
| 177 | label: t('conversation_block'), |
| 178 | Icon: BlockContactIcon, |
| 179 | onClick: () => { |
| 180 | blockContactDialogHandler.openDialog(); |
| 181 | }, |
| 182 | }, |
| 183 | { |
| 184 | label: t('conversation_delete'), |
| 185 | Icon: RemoveContactIcon, |
| 186 | onClick: () => { |
| 187 | removeContactDialogHandler.openDialog(); |
| 188 | }, |
| 189 | }, |
| 190 | ], |
| 191 | [ |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 192 | navigate, |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 193 | onMessageClick, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 194 | isSelected, |
| 195 | getContactDetails, |
| 196 | detailsDialogHandler, |
| 197 | blockContactDialogHandler, |
| 198 | removeContactDialogHandler, |
| 199 | t, |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 200 | startCall, |
| 201 | conversationId, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 202 | ] |
| 203 | ); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 204 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 205 | return ( |
| 206 | <> |
| 207 | <ContextMenu {...contextMenuProps} items={menuOptions} /> |
| 208 | |
| 209 | <DetailsDialog {...detailsDialogHandler.props} userId={userId} conversation={conversation} isSwarm={isSwarm} /> |
| 210 | |
| 211 | <RemoveContactDialog {...removeContactDialogHandler.props} userId={userId} conversation={conversation} /> |
| 212 | |
| 213 | <BlockContactDialog {...blockContactDialogHandler.props} userId={userId} conversation={conversation} /> |
| 214 | </> |
| 215 | ); |
| 216 | }; |
| 217 | |
| 218 | interface DetailsDialogProps { |
| 219 | userId: string; |
| 220 | conversation: Conversation; |
| 221 | open: boolean; |
| 222 | onClose: () => void; |
| 223 | isSwarm: boolean; |
| 224 | } |
| 225 | |
| 226 | const DetailsDialog = ({ userId, conversation, open, onClose, isSwarm }: DetailsDialogProps) => { |
| 227 | const { t } = useTranslation(); |
| 228 | const items = useMemo( |
| 229 | () => [ |
| 230 | { |
| 231 | label: t('conversation_details_username'), |
| 232 | value: conversation.getDisplayNameNoFallback(), |
| 233 | }, |
| 234 | { |
| 235 | label: t('conversation_details_identifier'), |
| 236 | value: userId, |
| 237 | }, |
| 238 | { |
| 239 | label: t('conversation_details_qr_code'), |
| 240 | value: <QRCodeCanvas size={80} value={`${userId}`} />, |
| 241 | }, |
| 242 | { |
| 243 | label: t('conversation_details_is_swarm'), |
| 244 | value: isSwarm ? t('conversation_details_is_swarm_true') : t('conversation_details_is_swarm_false'), |
| 245 | }, |
| 246 | ], |
| 247 | [userId, conversation, isSwarm, t] |
| 248 | ); |
| 249 | return ( |
| 250 | <InfosDialog |
| 251 | open={open} |
| 252 | onClose={onClose} |
| 253 | icon={ |
| 254 | <ConversationAvatar |
| 255 | sx={{ width: 'inherit', height: 'inherit' }} |
| 256 | displayName={conversation.getDisplayNameNoFallback()} |
| 257 | /> |
| 258 | } |
| 259 | title={conversation.getDisplayNameNoFallback() || ''} |
| 260 | content={<DialogContentList title={t('conversation_details_informations')} items={items} />} |
| 261 | /> |
| 262 | ); |
| 263 | }; |
| 264 | |
| 265 | interface BlockContactDialogProps { |
| 266 | userId: string; |
| 267 | conversation: Conversation; |
| 268 | open: boolean; |
| 269 | onClose: () => void; |
| 270 | } |
| 271 | |
| 272 | const BlockContactDialog = ({ userId, open, onClose }: BlockContactDialogProps) => { |
| 273 | const { axiosInstance } = useAuthContext(); |
| 274 | const { t } = useTranslation(); |
| 275 | const dispatch = useAppDispatch(); |
| 276 | |
| 277 | const block = async () => { |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 278 | const controller = new AbortController(); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 279 | try { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 280 | await axiosInstance.post(`/contacts/${userId}/block`, { |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 281 | signal: controller.signal, |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 282 | }); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 283 | dispatch(setRefreshFromSlice()); |
| 284 | } catch (e) { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 285 | console.error(`Error $block contact : `, e); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 286 | dispatch(setRefreshFromSlice()); |
| 287 | } |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 288 | onClose(); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 289 | }; |
| 290 | |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 291 | return ( |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 292 | <ConfirmationDialog |
| 293 | open={open} |
| 294 | onClose={onClose} |
| 295 | title={t('dialog_confirm_title_default')} |
| 296 | content={t('conversation_ask_confirm_block')} |
| 297 | onConfirm={block} |
| 298 | confirmButtonText={t('conversation_confirm_block')} |
| 299 | /> |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 300 | ); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | interface RemoveContactDialogProps { |
| 304 | userId: string; |
| 305 | conversation: Conversation; |
| 306 | open: boolean; |
| 307 | onClose: () => void; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 308 | } |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 309 | |
| 310 | const RemoveContactDialog = ({ userId, open, onClose }: RemoveContactDialogProps) => { |
| 311 | const { axiosInstance } = useAuthContext(); |
| 312 | const { t } = useTranslation(); |
| 313 | const dispatch = useAppDispatch(); |
| 314 | |
| 315 | const remove = async () => { |
| 316 | const controller = new AbortController(); |
| 317 | try { |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 318 | await axiosInstance.delete(`/contacts/${userId}`, { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 319 | signal: controller.signal, |
| 320 | }); |
| 321 | dispatch(setRefreshFromSlice()); |
| 322 | } catch (e) { |
| 323 | console.error(`Error removing contact : `, e); |
| 324 | dispatch(setRefreshFromSlice()); |
| 325 | } |
| 326 | onClose(); |
| 327 | }; |
| 328 | |
| 329 | return ( |
| 330 | <ConfirmationDialog |
| 331 | open={open} |
| 332 | onClose={onClose} |
| 333 | title={t('dialog_confirm_title_default')} |
| 334 | content={t('conversation_ask_confirm_remove')} |
| 335 | onConfirm={remove} |
| 336 | confirmButtonText={t('conversation_confirm_remove')} |
| 337 | /> |
| 338 | ); |
| 339 | }; |