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