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'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 19 | import { IConversationSummary } 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'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -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'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -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'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 36 | import { AudioCallIcon, CancelIcon, MessageIcon, PersonIcon, VideoCallIcon } from './SvgIcon'; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 37 | |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 38 | type ConversationListItemProps = { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 39 | conversationSummary: IConversationSummary; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 40 | }; |
| 41 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 42 | export default function ConversationListItem({ conversationSummary }: ConversationListItemProps) { |
| 43 | const { |
| 44 | urlParams: { conversationId: selectedConversationId }, |
| 45 | } = useUrlParams<ConversationRouteParams>(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 46 | const contextMenuHandler = useContextMenuHandler(); |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 47 | const callContext = useCallContext(true); |
| 48 | const { callData } = useContext(CallManagerContext); |
| 49 | const { t } = useTranslation(); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 50 | const navigate = useNavigate(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 51 | |
| 52 | const conversationId = conversationSummary.id; |
| 53 | const isSelected = conversationId === selectedConversationId; |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 54 | |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 55 | const onClick = useCallback(() => { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 56 | if (conversationId) { |
| 57 | navigate(`/conversation/${conversationId}`); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 58 | } |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 59 | }, [navigate, conversationId]); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 60 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 61 | const secondaryText = useMemo(() => { |
| 62 | if (!callContext || !callData || callData.conversationId !== conversationSummary.id) { |
| 63 | return conversationSummary.lastMessage.body; |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | if (callContext.callStatus === CallStatus.InCall) { |
| 67 | return callContext.isAudioOn ? t('ongoing_call_unmuted') : t('ongoing_call_muted'); |
| 68 | } |
| 69 | |
| 70 | if (callContext.callStatus === CallStatus.Connecting) { |
| 71 | return t('connecting_call'); |
| 72 | } |
| 73 | |
| 74 | return callContext.callRole === 'caller' ? t('outgoing_call') : t('incoming_call'); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 75 | }, [conversationSummary, callContext, callData, t]); |
| 76 | |
| 77 | const conversationName = useMemo( |
| 78 | () => conversationSummary.title ?? conversationSummary.membersNames.join(', '), |
| 79 | [conversationSummary] |
| 80 | ); |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 81 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 82 | return ( |
| 83 | <Box onContextMenu={contextMenuHandler.handleAnchorPosition}> |
| 84 | <ConversationMenu |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 85 | conversationId={conversationId} |
| 86 | conversationName={conversationName} |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 87 | onMessageClick={onClick} |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 88 | isSelected={isSelected} |
| 89 | contextMenuProps={contextMenuHandler.props} |
| 90 | /> |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 91 | <ListItem button alignItems="flex-start" selected={isSelected} onClick={onClick}> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 92 | <ListItemAvatar> |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 93 | <ConversationAvatar displayName={conversationName} /> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 94 | </ListItemAvatar> |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 95 | <ListItemText primary={conversationName} secondary={secondaryText} /> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 96 | </ListItem> |
| 97 | </Box> |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | interface ConversationMenuProps { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 102 | conversationId: string; |
| 103 | conversationName: string; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 104 | onMessageClick: () => void; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 105 | isSelected: boolean; |
| 106 | contextMenuProps: ContextMenuHandler['props']; |
| 107 | } |
| 108 | |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 109 | const ConversationMenu = ({ |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 110 | conversationId, |
| 111 | conversationName, |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 112 | onMessageClick, |
| 113 | isSelected, |
| 114 | contextMenuProps, |
| 115 | }: ConversationMenuProps) => { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 116 | const { t } = useTranslation(); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 117 | const { startCall } = useContext(CallManagerContext); |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 118 | const [isSwarm] = useState(true); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 119 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 120 | const detailsDialogHandler = useDialogHandler(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 121 | const RemoveConversationDialogHandler = useDialogHandler(); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 122 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 123 | const navigate = useNavigate(); |
| 124 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 125 | const menuOptions: PopoverListItemData[] = useMemo( |
| 126 | () => [ |
| 127 | { |
| 128 | label: t('conversation_message'), |
| 129 | Icon: MessageIcon, |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 130 | onClick: onMessageClick, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 131 | }, |
| 132 | { |
| 133 | label: t('conversation_start_audiocall'), |
| 134 | Icon: AudioCallIcon, |
| 135 | onClick: () => { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 136 | if (conversationId) { |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 137 | startCall({ |
| 138 | conversationId, |
| 139 | role: 'caller', |
| 140 | }); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 141 | } |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 142 | }, |
| 143 | }, |
| 144 | { |
| 145 | label: t('conversation_start_videocall'), |
| 146 | Icon: VideoCallIcon, |
| 147 | onClick: () => { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 148 | if (conversationId) { |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 149 | startCall({ |
| 150 | conversationId, |
| 151 | role: 'caller', |
| 152 | withVideoOn: true, |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 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'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 170 | Icon: PersonIcon, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 171 | onClick: () => { |
| 172 | detailsDialogHandler.openDialog(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 173 | }, |
| 174 | }, |
| 175 | { |
| 176 | label: t('conversation_delete'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 177 | Icon: CancelIcon, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 178 | onClick: () => { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 179 | RemoveConversationDialogHandler.openDialog(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 180 | }, |
| 181 | }, |
| 182 | ], |
| 183 | [ |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 184 | navigate, |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 185 | onMessageClick, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 186 | isSelected, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 187 | detailsDialogHandler, |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 188 | RemoveConversationDialogHandler, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 189 | t, |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 190 | startCall, |
| 191 | conversationId, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 192 | ] |
| 193 | ); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 194 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 195 | return ( |
| 196 | <> |
| 197 | <ContextMenu {...contextMenuProps} items={menuOptions} /> |
| 198 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 199 | <DetailsDialog |
| 200 | {...detailsDialogHandler.props} |
| 201 | conversationId={conversationId} |
| 202 | conversationName={conversationName} |
| 203 | isSwarm={isSwarm} |
| 204 | /> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 205 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 206 | <RemoveConversationDialog {...RemoveConversationDialogHandler.props} conversationId={conversationId} /> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 207 | </> |
| 208 | ); |
| 209 | }; |
| 210 | |
| 211 | interface DetailsDialogProps { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 212 | conversationId: string; |
| 213 | conversationName: string; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 214 | open: boolean; |
| 215 | onClose: () => void; |
| 216 | isSwarm: boolean; |
| 217 | } |
| 218 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 219 | const DetailsDialog = ({ conversationId, conversationName, open, onClose, isSwarm }: DetailsDialogProps) => { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 220 | const { t } = useTranslation(); |
| 221 | const items = useMemo( |
| 222 | () => [ |
| 223 | { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 224 | label: t('conversation_details_name'), |
| 225 | value: conversationName, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 226 | }, |
| 227 | { |
| 228 | label: t('conversation_details_identifier'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 229 | value: conversationId, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 230 | }, |
| 231 | { |
| 232 | label: t('conversation_details_qr_code'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 233 | value: <QRCodeCanvas size={80} value={`${conversationId}`} />, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 234 | }, |
| 235 | { |
| 236 | label: t('conversation_details_is_swarm'), |
| 237 | value: isSwarm ? t('conversation_details_is_swarm_true') : t('conversation_details_is_swarm_false'), |
| 238 | }, |
| 239 | ], |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 240 | [conversationId, conversationName, isSwarm, t] |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 241 | ); |
| 242 | return ( |
| 243 | <InfosDialog |
| 244 | open={open} |
| 245 | onClose={onClose} |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 246 | icon={<ConversationAvatar sx={{ width: 'inherit', height: 'inherit' }} displayName={conversationName} />} |
| 247 | title={conversationName} |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 248 | content={<DialogContentList title={t('conversation_details_informations')} items={items} />} |
| 249 | /> |
| 250 | ); |
| 251 | }; |
| 252 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 253 | interface RemoveConversationDialogProps { |
| 254 | conversationId: string; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 255 | open: boolean; |
| 256 | onClose: () => void; |
| 257 | } |
| 258 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 259 | const RemoveConversationDialog = ({ conversationId, open, onClose }: RemoveConversationDialogProps) => { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 260 | const { axiosInstance } = useAuthContext(); |
| 261 | const { t } = useTranslation(); |
| 262 | const dispatch = useAppDispatch(); |
| 263 | |
| 264 | const remove = async () => { |
| 265 | const controller = new AbortController(); |
| 266 | try { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 267 | await axiosInstance.delete(`/conversations/${conversationId}`, { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 268 | signal: controller.signal, |
| 269 | }); |
| 270 | dispatch(setRefreshFromSlice()); |
| 271 | } catch (e) { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 272 | console.error(`Error removing conversation : `, e); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 273 | dispatch(setRefreshFromSlice()); |
| 274 | } |
| 275 | onClose(); |
| 276 | }; |
| 277 | |
| 278 | return ( |
| 279 | <ConfirmationDialog |
| 280 | open={open} |
| 281 | onClose={onClose} |
| 282 | title={t('dialog_confirm_title_default')} |
| 283 | content={t('conversation_ask_confirm_remove')} |
| 284 | onConfirm={remove} |
| 285 | confirmButtonText={t('conversation_confirm_remove')} |
| 286 | /> |
| 287 | ); |
| 288 | }; |