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 ( |
idillon | 22ed819 | 2022-11-29 12:17:04 -0500 | [diff] [blame] | 83 | <Box> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 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 | /> |
idillon | 22ed819 | 2022-11-29 12:17:04 -0500 | [diff] [blame] | 91 | <ListItem |
| 92 | button |
| 93 | alignItems="flex-start" |
| 94 | selected={isSelected} |
| 95 | onClick={onClick} |
| 96 | onContextMenu={contextMenuHandler.handleAnchorPosition} |
| 97 | > |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 98 | <ListItemAvatar> |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 99 | <ConversationAvatar displayName={conversationName} /> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 100 | </ListItemAvatar> |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 101 | <ListItemText primary={conversationName} secondary={secondaryText} /> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 102 | </ListItem> |
| 103 | </Box> |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | interface ConversationMenuProps { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 108 | conversationId: string; |
| 109 | conversationName: string; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 110 | onMessageClick: () => void; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 111 | isSelected: boolean; |
| 112 | contextMenuProps: ContextMenuHandler['props']; |
| 113 | } |
| 114 | |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 115 | const ConversationMenu = ({ |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 116 | conversationId, |
| 117 | conversationName, |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 118 | onMessageClick, |
| 119 | isSelected, |
| 120 | contextMenuProps, |
| 121 | }: ConversationMenuProps) => { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 122 | const { t } = useTranslation(); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 123 | const { startCall } = useContext(CallManagerContext); |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 124 | const [isSwarm] = useState(true); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 125 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 126 | const detailsDialogHandler = useDialogHandler(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 127 | const RemoveConversationDialogHandler = useDialogHandler(); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 128 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 129 | const navigate = useNavigate(); |
| 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) { |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 143 | startCall({ |
| 144 | conversationId, |
| 145 | role: 'caller', |
| 146 | }); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 147 | } |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 148 | }, |
| 149 | }, |
| 150 | { |
| 151 | label: t('conversation_start_videocall'), |
| 152 | Icon: VideoCallIcon, |
| 153 | onClick: () => { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 154 | if (conversationId) { |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 155 | startCall({ |
| 156 | conversationId, |
| 157 | role: 'caller', |
| 158 | withVideoOn: true, |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 159 | }); |
| 160 | } |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 161 | }, |
| 162 | }, |
| 163 | ...(isSelected |
| 164 | ? [ |
| 165 | { |
| 166 | label: t('conversation_close'), |
| 167 | Icon: CancelIcon, |
| 168 | onClick: () => { |
| 169 | navigate(`/`); |
| 170 | }, |
| 171 | }, |
| 172 | ] |
| 173 | : []), |
| 174 | { |
| 175 | label: t('conversation_details'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 176 | Icon: PersonIcon, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 177 | onClick: () => { |
| 178 | detailsDialogHandler.openDialog(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 179 | }, |
| 180 | }, |
| 181 | { |
| 182 | label: t('conversation_delete'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 183 | Icon: CancelIcon, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 184 | onClick: () => { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 185 | RemoveConversationDialogHandler.openDialog(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 186 | }, |
| 187 | }, |
| 188 | ], |
| 189 | [ |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 190 | navigate, |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 191 | onMessageClick, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 192 | isSelected, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 193 | detailsDialogHandler, |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 194 | RemoveConversationDialogHandler, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 195 | t, |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 196 | startCall, |
| 197 | conversationId, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 198 | ] |
| 199 | ); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 200 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 201 | return ( |
| 202 | <> |
| 203 | <ContextMenu {...contextMenuProps} items={menuOptions} /> |
| 204 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 205 | <DetailsDialog |
| 206 | {...detailsDialogHandler.props} |
| 207 | conversationId={conversationId} |
| 208 | conversationName={conversationName} |
| 209 | isSwarm={isSwarm} |
| 210 | /> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 211 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 212 | <RemoveConversationDialog {...RemoveConversationDialogHandler.props} conversationId={conversationId} /> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 213 | </> |
| 214 | ); |
| 215 | }; |
| 216 | |
| 217 | interface DetailsDialogProps { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 218 | conversationId: string; |
| 219 | conversationName: string; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 220 | open: boolean; |
| 221 | onClose: () => void; |
| 222 | isSwarm: boolean; |
| 223 | } |
| 224 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 225 | const DetailsDialog = ({ conversationId, conversationName, open, onClose, isSwarm }: DetailsDialogProps) => { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 226 | const { t } = useTranslation(); |
| 227 | const items = useMemo( |
| 228 | () => [ |
| 229 | { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 230 | label: t('conversation_details_name'), |
| 231 | value: conversationName, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 232 | }, |
| 233 | { |
| 234 | label: t('conversation_details_identifier'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 235 | value: conversationId, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 236 | }, |
| 237 | { |
| 238 | label: t('conversation_details_qr_code'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 239 | value: <QRCodeCanvas size={80} value={`${conversationId}`} />, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 240 | }, |
| 241 | { |
| 242 | label: t('conversation_details_is_swarm'), |
| 243 | value: isSwarm ? t('conversation_details_is_swarm_true') : t('conversation_details_is_swarm_false'), |
| 244 | }, |
| 245 | ], |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 246 | [conversationId, conversationName, isSwarm, t] |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 247 | ); |
| 248 | return ( |
| 249 | <InfosDialog |
| 250 | open={open} |
| 251 | onClose={onClose} |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 252 | icon={<ConversationAvatar sx={{ width: 'inherit', height: 'inherit' }} displayName={conversationName} />} |
| 253 | title={conversationName} |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 254 | content={<DialogContentList title={t('conversation_details_informations')} items={items} />} |
| 255 | /> |
| 256 | ); |
| 257 | }; |
| 258 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 259 | interface RemoveConversationDialogProps { |
| 260 | conversationId: string; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 261 | open: boolean; |
| 262 | onClose: () => void; |
| 263 | } |
| 264 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 265 | const RemoveConversationDialog = ({ conversationId, open, onClose }: RemoveConversationDialogProps) => { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 266 | const { axiosInstance } = useAuthContext(); |
| 267 | const { t } = useTranslation(); |
| 268 | const dispatch = useAppDispatch(); |
| 269 | |
| 270 | const remove = async () => { |
| 271 | const controller = new AbortController(); |
| 272 | try { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 273 | await axiosInstance.delete(`/conversations/${conversationId}`, { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 274 | signal: controller.signal, |
| 275 | }); |
| 276 | dispatch(setRefreshFromSlice()); |
| 277 | } catch (e) { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 278 | console.error(`Error removing conversation : `, e); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 279 | dispatch(setRefreshFromSlice()); |
| 280 | } |
| 281 | onClose(); |
| 282 | }; |
| 283 | |
| 284 | return ( |
| 285 | <ConfirmationDialog |
| 286 | open={open} |
| 287 | onClose={onClose} |
| 288 | title={t('dialog_confirm_title_default')} |
| 289 | content={t('conversation_ask_confirm_remove')} |
| 290 | onConfirm={remove} |
| 291 | confirmButtonText={t('conversation_confirm_remove')} |
| 292 | /> |
| 293 | ); |
| 294 | }; |