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 | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 18 | import { Box, List, Stack, Typography } from '@mui/material'; |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 19 | import dayjs from 'dayjs'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 20 | import { IConversationSummary } from 'jami-web-common'; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 21 | import { QRCodeCanvas } from 'qrcode.react'; |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 22 | import { useCallback, useMemo, useState } from 'react'; |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 23 | import { useTranslation } from 'react-i18next'; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 24 | import { useNavigate } from 'react-router-dom'; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 25 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 26 | import { useAuthContext } from '../contexts/AuthProvider'; |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 27 | import { useCallManagerContext } from '../contexts/CallManagerProvider'; |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 28 | import { useConversationDisplayNameShort } from '../hooks/useConversationDisplayName'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 29 | import { useUrlParams } from '../hooks/useUrlParams'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 30 | import { ConversationRouteParams } from '../router'; |
idillon | c45a43d | 2023-02-10 18:12:10 -0500 | [diff] [blame] | 31 | import { CallStatus } from '../services/CallManager'; |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 32 | import { useRemoveConversationMutation } from '../services/conversationQueries'; |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 33 | import { getMessageCallText, getMessageMemberText } from '../utils/chatmessages'; |
| 34 | import { formatRelativeDate, formatTime } from '../utils/dates×'; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 35 | import ContextMenu, { ContextMenuHandler, useContextMenuHandler } from './ContextMenu'; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 36 | import ConversationAvatar from './ConversationAvatar'; |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 37 | import { CustomListItemButton } from './CustomListItemButton'; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 38 | import { ConfirmationDialog, DialogContentList, InfosDialog, useDialogHandler } from './Dialog'; |
| 39 | import { PopoverListItemData } from './PopoverList'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 40 | import { AudioCallIcon, CancelIcon, MessageIcon, PersonIcon, VideoCallIcon } from './SvgIcon'; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 41 | |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 42 | type ConversationSummaryListProps = { |
| 43 | conversationsSummaries: IConversationSummary[]; |
| 44 | }; |
| 45 | |
| 46 | export const ConversationSummaryList = ({ conversationsSummaries }: ConversationSummaryListProps) => { |
| 47 | return ( |
| 48 | <List> |
| 49 | {conversationsSummaries?.map((conversationSummary) => ( |
| 50 | <ConversationSummaryListItem key={conversationSummary.id} conversationSummary={conversationSummary} /> |
| 51 | ))} |
| 52 | </List> |
| 53 | ); |
| 54 | }; |
| 55 | |
| 56 | type ConversationSummaryListItemProps = { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 57 | conversationSummary: IConversationSummary; |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 58 | }; |
| 59 | |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 60 | const ConversationSummaryListItem = ({ conversationSummary }: ConversationSummaryListItemProps) => { |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 61 | const { account } = useAuthContext(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 62 | const { |
| 63 | urlParams: { conversationId: selectedConversationId }, |
| 64 | } = useUrlParams<ConversationRouteParams>(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 65 | const contextMenuHandler = useContextMenuHandler(); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 66 | const navigate = useNavigate(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 67 | |
| 68 | const conversationId = conversationSummary.id; |
| 69 | const isSelected = conversationId === selectedConversationId; |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 70 | |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 71 | const onClick = useCallback(() => { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 72 | if (conversationId) { |
| 73 | navigate(`/conversation/${conversationId}`); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 74 | } |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 75 | }, [navigate, conversationId]); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 76 | |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 77 | const conversationName = useConversationDisplayNameShort( |
| 78 | account, |
| 79 | conversationSummary.title, |
| 80 | conversationSummary.membersNames |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 81 | ); |
| 82 | |
| 83 | return ( |
| 84 | <Box> |
| 85 | <ConversationMenu |
| 86 | conversationId={conversationId} |
| 87 | conversationName={conversationName} |
| 88 | onMessageClick={onClick} |
| 89 | isSelected={isSelected} |
| 90 | contextMenuProps={contextMenuHandler.props} |
| 91 | /> |
| 92 | <CustomListItemButton |
| 93 | selected={isSelected} |
| 94 | onClick={onClick} |
| 95 | onContextMenu={contextMenuHandler.handleAnchorPosition} |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 96 | icon={<ConversationAvatar displayName={conversationName} src={conversationSummary.avatar} />} |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 97 | primaryText={<Typography variant="body1">{conversationName}</Typography>} |
| 98 | secondaryText={<SecondaryText conversationSummary={conversationSummary} isSelected={isSelected} />} |
| 99 | /> |
| 100 | </Box> |
| 101 | ); |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 102 | }; |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 103 | |
| 104 | type SecondaryTextProps = { |
| 105 | conversationSummary: IConversationSummary; |
| 106 | isSelected: boolean; |
| 107 | }; |
| 108 | |
| 109 | const SecondaryText = ({ conversationSummary, isSelected }: SecondaryTextProps) => { |
| 110 | const { account } = useAuthContext(); |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 111 | const { callData, callStatus, isAudioOn } = useCallManagerContext(); |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 112 | const { t, i18n } = useTranslation(); |
| 113 | |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 114 | const timeIndicator = useMemo(() => { |
| 115 | const message = conversationSummary.lastMessage; |
| 116 | const time = dayjs.unix(Number(message.timestamp)); |
| 117 | if (time.isToday()) { |
| 118 | return formatTime(time, i18n); |
| 119 | } else { |
| 120 | return formatRelativeDate(time, i18n); |
| 121 | } |
| 122 | }, [conversationSummary, i18n]); |
| 123 | |
| 124 | const lastMessageText = useMemo(() => { |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 125 | if (!callData || callData.conversationId !== conversationSummary.id) { |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 126 | const message = conversationSummary.lastMessage; |
| 127 | switch (message.type) { |
| 128 | case 'initial': { |
| 129 | return t('message_swarm_created'); |
| 130 | } |
| 131 | case 'application/data-transfer+json': { |
| 132 | return message.fileId; |
| 133 | } |
| 134 | case 'application/call-history+json': { |
| 135 | const isAccountMessage = message.author === account.getUri(); |
| 136 | return getMessageCallText(isAccountMessage, message, i18n); |
| 137 | } |
| 138 | case 'member': { |
| 139 | return getMessageMemberText(message, i18n); |
| 140 | } |
| 141 | case 'text/plain': { |
| 142 | return message.body; |
| 143 | } |
| 144 | default: { |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 145 | console.error(`${ConversationSummaryListItem.name} received an unexpected lastMessage type: ${message.type}`); |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 146 | return ''; |
| 147 | } |
| 148 | } |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 149 | } |
| 150 | |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 151 | if (callStatus === CallStatus.InCall) { |
| 152 | return isAudioOn ? t('ongoing_call_unmuted') : t('ongoing_call_muted'); |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 153 | } |
| 154 | |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 155 | if (callStatus === CallStatus.Connecting) { |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 156 | return t('connecting_call'); |
| 157 | } |
| 158 | |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 159 | return callData.role === 'caller' ? t('outgoing_call') : t('incoming_call'); |
| 160 | }, [account, conversationSummary, callData, callStatus, isAudioOn, t, i18n]); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 161 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 162 | return ( |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 163 | <Stack direction="row" spacing="5px"> |
| 164 | <Typography variant="body2" fontWeight={isSelected ? 'bold' : 'normal'}> |
| 165 | {timeIndicator} |
| 166 | </Typography> |
| 167 | <Typography variant="body2">{lastMessageText}</Typography> |
| 168 | </Stack> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 169 | ); |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 170 | }; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 171 | |
| 172 | interface ConversationMenuProps { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 173 | conversationId: string; |
| 174 | conversationName: string; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 175 | onMessageClick: () => void; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 176 | isSelected: boolean; |
| 177 | contextMenuProps: ContextMenuHandler['props']; |
| 178 | } |
| 179 | |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 180 | const ConversationMenu = ({ |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 181 | conversationId, |
| 182 | conversationName, |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 183 | onMessageClick, |
| 184 | isSelected, |
| 185 | contextMenuProps, |
| 186 | }: ConversationMenuProps) => { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 187 | const { t } = useTranslation(); |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 188 | const { startCall } = useCallManagerContext(); |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 189 | const [isSwarm] = useState(true); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 190 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 191 | const detailsDialogHandler = useDialogHandler(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 192 | const RemoveConversationDialogHandler = useDialogHandler(); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 193 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 194 | const navigate = useNavigate(); |
| 195 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 196 | const menuOptions: PopoverListItemData[] = useMemo( |
| 197 | () => [ |
| 198 | { |
| 199 | label: t('conversation_message'), |
| 200 | Icon: MessageIcon, |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 201 | onClick: onMessageClick, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 202 | }, |
| 203 | { |
| 204 | label: t('conversation_start_audiocall'), |
| 205 | Icon: AudioCallIcon, |
| 206 | onClick: () => { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 207 | if (conversationId) { |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 208 | startCall(conversationId); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 209 | } |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 210 | }, |
| 211 | }, |
| 212 | { |
| 213 | label: t('conversation_start_videocall'), |
| 214 | Icon: VideoCallIcon, |
| 215 | onClick: () => { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 216 | if (conversationId) { |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 217 | startCall(conversationId, true); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 218 | } |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 219 | }, |
| 220 | }, |
| 221 | ...(isSelected |
| 222 | ? [ |
| 223 | { |
| 224 | label: t('conversation_close'), |
| 225 | Icon: CancelIcon, |
| 226 | onClick: () => { |
| 227 | navigate(`/`); |
| 228 | }, |
| 229 | }, |
| 230 | ] |
| 231 | : []), |
| 232 | { |
| 233 | label: t('conversation_details'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 234 | Icon: PersonIcon, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 235 | onClick: () => { |
| 236 | detailsDialogHandler.openDialog(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 237 | }, |
| 238 | }, |
| 239 | { |
| 240 | label: t('conversation_delete'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 241 | Icon: CancelIcon, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 242 | onClick: () => { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 243 | RemoveConversationDialogHandler.openDialog(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 244 | }, |
| 245 | }, |
| 246 | ], |
| 247 | [ |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 248 | navigate, |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 249 | onMessageClick, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 250 | isSelected, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 251 | detailsDialogHandler, |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 252 | RemoveConversationDialogHandler, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 253 | t, |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 254 | startCall, |
| 255 | conversationId, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 256 | ] |
| 257 | ); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 258 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 259 | return ( |
| 260 | <> |
| 261 | <ContextMenu {...contextMenuProps} items={menuOptions} /> |
| 262 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 263 | <DetailsDialog |
| 264 | {...detailsDialogHandler.props} |
| 265 | conversationId={conversationId} |
| 266 | conversationName={conversationName} |
| 267 | isSwarm={isSwarm} |
| 268 | /> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 269 | |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 270 | <RemoveConversationDialog |
| 271 | {...RemoveConversationDialogHandler.props} |
| 272 | conversationId={conversationId} |
| 273 | isSelected={isSelected} |
| 274 | /> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 275 | </> |
| 276 | ); |
| 277 | }; |
| 278 | |
| 279 | interface DetailsDialogProps { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 280 | conversationId: string; |
| 281 | conversationName: string; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 282 | open: boolean; |
| 283 | onClose: () => void; |
| 284 | isSwarm: boolean; |
| 285 | } |
| 286 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 287 | const DetailsDialog = ({ conversationId, conversationName, open, onClose, isSwarm }: DetailsDialogProps) => { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 288 | const { t } = useTranslation(); |
| 289 | const items = useMemo( |
| 290 | () => [ |
| 291 | { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 292 | label: t('conversation_details_name'), |
| 293 | value: conversationName, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 294 | }, |
| 295 | { |
| 296 | label: t('conversation_details_identifier'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 297 | value: conversationId, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 298 | }, |
| 299 | { |
| 300 | label: t('conversation_details_qr_code'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 301 | value: <QRCodeCanvas size={80} value={`${conversationId}`} />, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 302 | }, |
| 303 | { |
| 304 | label: t('conversation_details_is_swarm'), |
| 305 | value: isSwarm ? t('conversation_details_is_swarm_true') : t('conversation_details_is_swarm_false'), |
| 306 | }, |
| 307 | ], |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 308 | [conversationId, conversationName, isSwarm, t] |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 309 | ); |
| 310 | return ( |
| 311 | <InfosDialog |
| 312 | open={open} |
| 313 | onClose={onClose} |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 314 | icon={<ConversationAvatar sx={{ width: 'inherit', height: 'inherit' }} displayName={conversationName} />} |
| 315 | title={conversationName} |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 316 | content={<DialogContentList title={t('conversation_details_informations')} items={items} />} |
| 317 | /> |
| 318 | ); |
| 319 | }; |
| 320 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 321 | interface RemoveConversationDialogProps { |
| 322 | conversationId: string; |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 323 | isSelected: boolean; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 324 | open: boolean; |
| 325 | onClose: () => void; |
| 326 | } |
| 327 | |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 328 | const RemoveConversationDialog = ({ conversationId, isSelected, open, onClose }: RemoveConversationDialogProps) => { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 329 | const { t } = useTranslation(); |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 330 | const navigate = useNavigate(); |
| 331 | const removeConversationMutation = useRemoveConversationMutation(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 332 | |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 333 | const remove = useCallback(async () => { |
| 334 | removeConversationMutation.mutate( |
| 335 | { conversationId }, |
| 336 | { |
| 337 | onSuccess: () => { |
| 338 | if (isSelected) { |
| 339 | navigate('/conversation/'); |
| 340 | } |
| 341 | }, |
| 342 | onError: (e) => { |
| 343 | console.error(`Error removing conversation : `, e); |
| 344 | }, |
| 345 | onSettled: () => { |
| 346 | onClose(); |
| 347 | }, |
| 348 | } |
| 349 | ); |
| 350 | }, [conversationId, isSelected, navigate, onClose, removeConversationMutation]); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 351 | |
| 352 | return ( |
| 353 | <ConfirmationDialog |
| 354 | open={open} |
| 355 | onClose={onClose} |
| 356 | title={t('dialog_confirm_title_default')} |
| 357 | content={t('conversation_ask_confirm_remove')} |
| 358 | onConfirm={remove} |
| 359 | confirmButtonText={t('conversation_confirm_remove')} |
| 360 | /> |
| 361 | ); |
| 362 | }; |