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