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