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'; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 22 | import { useCallback, useContext, 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'; |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 27 | import { CallManagerContext } from '../contexts/CallManagerProvider'; |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 28 | import { CallStatus, useCallContext } from '../contexts/CallProvider'; |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 29 | import { useConversationDisplayNameShort } from '../hooks/useConversationDisplayName'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 30 | import { useUrlParams } from '../hooks/useUrlParams'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 31 | import { ConversationRouteParams } from '../router'; |
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(); |
| 111 | const callContext = useCallContext(true); |
| 112 | const { callData } = useContext(CallManagerContext); |
| 113 | const { t, i18n } = useTranslation(); |
| 114 | |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 115 | const timeIndicator = useMemo(() => { |
| 116 | const message = conversationSummary.lastMessage; |
| 117 | const time = dayjs.unix(Number(message.timestamp)); |
| 118 | if (time.isToday()) { |
| 119 | return formatTime(time, i18n); |
| 120 | } else { |
| 121 | return formatRelativeDate(time, i18n); |
| 122 | } |
| 123 | }, [conversationSummary, i18n]); |
| 124 | |
| 125 | const lastMessageText = useMemo(() => { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 126 | if (!callContext || !callData || callData.conversationId !== conversationSummary.id) { |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 127 | const message = conversationSummary.lastMessage; |
| 128 | switch (message.type) { |
| 129 | case 'initial': { |
| 130 | return t('message_swarm_created'); |
| 131 | } |
| 132 | case 'application/data-transfer+json': { |
| 133 | return message.fileId; |
| 134 | } |
| 135 | case 'application/call-history+json': { |
| 136 | const isAccountMessage = message.author === account.getUri(); |
| 137 | return getMessageCallText(isAccountMessage, message, i18n); |
| 138 | } |
| 139 | case 'member': { |
| 140 | return getMessageMemberText(message, i18n); |
| 141 | } |
| 142 | case 'text/plain': { |
| 143 | return message.body; |
| 144 | } |
| 145 | default: { |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 146 | console.error(`${ConversationSummaryListItem.name} received an unexpected lastMessage type: ${message.type}`); |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 147 | return ''; |
| 148 | } |
| 149 | } |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (callContext.callStatus === CallStatus.InCall) { |
| 153 | return callContext.isAudioOn ? t('ongoing_call_unmuted') : t('ongoing_call_muted'); |
| 154 | } |
| 155 | |
| 156 | if (callContext.callStatus === CallStatus.Connecting) { |
| 157 | return t('connecting_call'); |
| 158 | } |
| 159 | |
| 160 | return callContext.callRole === 'caller' ? t('outgoing_call') : t('incoming_call'); |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 161 | }, [account, conversationSummary, callContext, callData, t, i18n]); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 162 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 163 | return ( |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 164 | <Stack direction="row" spacing="5px"> |
| 165 | <Typography variant="body2" fontWeight={isSelected ? 'bold' : 'normal'}> |
| 166 | {timeIndicator} |
| 167 | </Typography> |
| 168 | <Typography variant="body2">{lastMessageText}</Typography> |
| 169 | </Stack> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 170 | ); |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 171 | }; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 172 | |
| 173 | interface ConversationMenuProps { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 174 | conversationId: string; |
| 175 | conversationName: string; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 176 | onMessageClick: () => void; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 177 | isSelected: boolean; |
| 178 | contextMenuProps: ContextMenuHandler['props']; |
| 179 | } |
| 180 | |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 181 | const ConversationMenu = ({ |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 182 | conversationId, |
| 183 | conversationName, |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 184 | onMessageClick, |
| 185 | isSelected, |
| 186 | contextMenuProps, |
| 187 | }: ConversationMenuProps) => { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 188 | const { t } = useTranslation(); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 189 | const { startCall } = useContext(CallManagerContext); |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 190 | const [isSwarm] = useState(true); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 191 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 192 | const detailsDialogHandler = useDialogHandler(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 193 | const RemoveConversationDialogHandler = useDialogHandler(); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 194 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 195 | const navigate = useNavigate(); |
| 196 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 197 | const menuOptions: PopoverListItemData[] = useMemo( |
| 198 | () => [ |
| 199 | { |
| 200 | label: t('conversation_message'), |
| 201 | Icon: MessageIcon, |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 202 | onClick: onMessageClick, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 203 | }, |
| 204 | { |
| 205 | label: t('conversation_start_audiocall'), |
| 206 | Icon: AudioCallIcon, |
| 207 | onClick: () => { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 208 | if (conversationId) { |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 209 | startCall({ |
| 210 | conversationId, |
| 211 | role: 'caller', |
| 212 | }); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 213 | } |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 214 | }, |
| 215 | }, |
| 216 | { |
| 217 | label: t('conversation_start_videocall'), |
| 218 | Icon: VideoCallIcon, |
| 219 | onClick: () => { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 220 | if (conversationId) { |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 221 | startCall({ |
| 222 | conversationId, |
| 223 | role: 'caller', |
| 224 | withVideoOn: true, |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 225 | }); |
| 226 | } |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 227 | }, |
| 228 | }, |
| 229 | ...(isSelected |
| 230 | ? [ |
| 231 | { |
| 232 | label: t('conversation_close'), |
| 233 | Icon: CancelIcon, |
| 234 | onClick: () => { |
| 235 | navigate(`/`); |
| 236 | }, |
| 237 | }, |
| 238 | ] |
| 239 | : []), |
| 240 | { |
| 241 | label: t('conversation_details'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 242 | Icon: PersonIcon, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 243 | onClick: () => { |
| 244 | detailsDialogHandler.openDialog(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 245 | }, |
| 246 | }, |
| 247 | { |
| 248 | label: t('conversation_delete'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 249 | Icon: CancelIcon, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 250 | onClick: () => { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 251 | RemoveConversationDialogHandler.openDialog(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 252 | }, |
| 253 | }, |
| 254 | ], |
| 255 | [ |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 256 | navigate, |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 257 | onMessageClick, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 258 | isSelected, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 259 | detailsDialogHandler, |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 260 | RemoveConversationDialogHandler, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 261 | t, |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 262 | startCall, |
| 263 | conversationId, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 264 | ] |
| 265 | ); |
simon | 575c940 | 2022-10-25 16:21:40 -0400 | [diff] [blame] | 266 | |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 267 | return ( |
| 268 | <> |
| 269 | <ContextMenu {...contextMenuProps} items={menuOptions} /> |
| 270 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 271 | <DetailsDialog |
| 272 | {...detailsDialogHandler.props} |
| 273 | conversationId={conversationId} |
| 274 | conversationName={conversationName} |
| 275 | isSwarm={isSwarm} |
| 276 | /> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 277 | |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 278 | <RemoveConversationDialog |
| 279 | {...RemoveConversationDialogHandler.props} |
| 280 | conversationId={conversationId} |
| 281 | isSelected={isSelected} |
| 282 | /> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 283 | </> |
| 284 | ); |
| 285 | }; |
| 286 | |
| 287 | interface DetailsDialogProps { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 288 | conversationId: string; |
| 289 | conversationName: string; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 290 | open: boolean; |
| 291 | onClose: () => void; |
| 292 | isSwarm: boolean; |
| 293 | } |
| 294 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 295 | const DetailsDialog = ({ conversationId, conversationName, open, onClose, isSwarm }: DetailsDialogProps) => { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 296 | const { t } = useTranslation(); |
| 297 | const items = useMemo( |
| 298 | () => [ |
| 299 | { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 300 | label: t('conversation_details_name'), |
| 301 | value: conversationName, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 302 | }, |
| 303 | { |
| 304 | label: t('conversation_details_identifier'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 305 | value: conversationId, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 306 | }, |
| 307 | { |
| 308 | label: t('conversation_details_qr_code'), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 309 | value: <QRCodeCanvas size={80} value={`${conversationId}`} />, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 310 | }, |
| 311 | { |
| 312 | label: t('conversation_details_is_swarm'), |
| 313 | value: isSwarm ? t('conversation_details_is_swarm_true') : t('conversation_details_is_swarm_false'), |
| 314 | }, |
| 315 | ], |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 316 | [conversationId, conversationName, isSwarm, t] |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 317 | ); |
| 318 | return ( |
| 319 | <InfosDialog |
| 320 | open={open} |
| 321 | onClose={onClose} |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 322 | icon={<ConversationAvatar sx={{ width: 'inherit', height: 'inherit' }} displayName={conversationName} />} |
| 323 | title={conversationName} |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 324 | content={<DialogContentList title={t('conversation_details_informations')} items={items} />} |
| 325 | /> |
| 326 | ); |
| 327 | }; |
| 328 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 329 | interface RemoveConversationDialogProps { |
| 330 | conversationId: string; |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 331 | isSelected: boolean; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 332 | open: boolean; |
| 333 | onClose: () => void; |
| 334 | } |
| 335 | |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 336 | const RemoveConversationDialog = ({ conversationId, isSelected, open, onClose }: RemoveConversationDialogProps) => { |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 337 | const { t } = useTranslation(); |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 338 | const navigate = useNavigate(); |
| 339 | const removeConversationMutation = useRemoveConversationMutation(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 340 | |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 341 | const remove = useCallback(async () => { |
| 342 | removeConversationMutation.mutate( |
| 343 | { conversationId }, |
| 344 | { |
| 345 | onSuccess: () => { |
| 346 | if (isSelected) { |
| 347 | navigate('/conversation/'); |
| 348 | } |
| 349 | }, |
| 350 | onError: (e) => { |
| 351 | console.error(`Error removing conversation : `, e); |
| 352 | }, |
| 353 | onSettled: () => { |
| 354 | onClose(); |
| 355 | }, |
| 356 | } |
| 357 | ); |
| 358 | }, [conversationId, isSelected, navigate, onClose, removeConversationMutation]); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 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 | }; |