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