simon | 26e79f7 | 2022-10-05 22:16:08 -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 | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 18 | import { Divider, Stack, Typography } from '@mui/material'; |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 19 | import { Account, Conversation, ConversationMember, WebSocketMessageType } from 'jami-web-common'; |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 20 | import { useContext, useEffect, useMemo, useState } from 'react'; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 21 | import { useTranslation } from 'react-i18next'; |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 22 | import { useNavigate } from 'react-router'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 23 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 24 | import { useAuthContext } from '../contexts/AuthProvider'; |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 25 | import { WebSocketContext } from '../contexts/WebSocketProvider'; |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 26 | import ChatInterface from '../pages/ChatInterface'; |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 27 | import { useConversationQuery } from '../services/Conversation'; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 28 | import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations'; |
| 29 | import { AddParticipantButton, ShowOptionsMenuButton, StartAudioCallButton, StartVideoCallButton } from './Button'; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 30 | import LoadingPage from './Loading'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 31 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 32 | type ConversationViewProps = { |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 33 | conversationId: string; |
| 34 | }; |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 35 | const ConversationView = ({ conversationId }: ConversationViewProps) => { |
| 36 | const { account } = useAuthContext(); |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 37 | const webSocket = useContext(WebSocketContext); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 38 | const [conversation, setConversation] = useState<Conversation | undefined>(); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 39 | const [isLoading, setIsLoading] = useState(true); |
| 40 | const [error, setError] = useState(false); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 41 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 42 | const accountId = account.getId(); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 43 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 44 | const conversationQuery = useConversationQuery(conversationId); |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 45 | |
| 46 | useEffect(() => { |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 47 | if (conversationQuery.isSuccess) { |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 48 | const conversation = Conversation.from(accountId, conversationQuery.data); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 49 | setConversation(conversation); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 50 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 51 | }, [accountId, conversationQuery.isSuccess, conversationQuery.data]); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 52 | |
| 53 | useEffect(() => { |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 54 | setIsLoading(conversationQuery.isLoading); |
| 55 | }, [conversationQuery.isLoading]); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 56 | |
| 57 | useEffect(() => { |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 58 | setError(conversationQuery.isError); |
| 59 | }, [conversationQuery.isError]); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 60 | |
| 61 | useEffect(() => { |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 62 | if (!conversation || !webSocket) { |
| 63 | return; |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 64 | } |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 65 | console.log(`set conversation ${conversationId} ` + webSocket); |
| 66 | webSocket.send(WebSocketMessageType.ConversationView, { accountId, conversationId }); |
| 67 | }, [accountId, conversation, conversationId, webSocket]); |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame] | 68 | |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 69 | if (isLoading) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 70 | return <LoadingPage />; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 71 | } else if (error || !account || !conversation) { |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 72 | return <div>Error loading {conversationId}</div>; |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 73 | } |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 74 | |
| 75 | return ( |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 76 | <Stack height="100%"> |
idillon | 02f579d | 2022-11-06 21:26:55 -0500 | [diff] [blame] | 77 | <ConversationHeader |
| 78 | account={account} |
| 79 | members={conversation.getMembers()} |
| 80 | adminTitle={conversation.infos.title as string} |
| 81 | conversationId={conversationId} |
| 82 | /> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 83 | <Divider |
| 84 | sx={{ |
| 85 | borderTop: '1px solid #E5E5E5', |
| 86 | }} |
| 87 | /> |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 88 | <ChatInterface conversationId={conversationId} members={conversation.getMembers()} /> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 89 | </Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 90 | ); |
| 91 | }; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 92 | |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 93 | type ConversationHeaderProps = { |
| 94 | account: Account; |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 95 | conversationId: string; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 96 | members: ConversationMember[]; |
| 97 | adminTitle: string | undefined; |
| 98 | }; |
| 99 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 100 | const ConversationHeader = ({ account, members, adminTitle, conversationId }: ConversationHeaderProps) => { |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 101 | const { t } = useTranslation(); |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 102 | const navigate = useNavigate(); |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 103 | |
| 104 | const title = useMemo(() => { |
| 105 | if (adminTitle) { |
| 106 | return adminTitle; |
| 107 | } |
| 108 | |
| 109 | const options: TranslateEnumerationOptions<ConversationMember> = { |
| 110 | elementPartialKey: 'member', |
| 111 | getElementValue: (member) => getMemberName(member), |
| 112 | translaters: [ |
| 113 | () => |
| 114 | // The user is chatting with themself |
| 115 | t('conversation_title_one', { member0: account?.getDisplayName() }), |
| 116 | (interpolations) => t('conversation_title_one', interpolations), |
| 117 | (interpolations) => t('conversation_title_two', interpolations), |
| 118 | (interpolations) => t('conversation_title_three', interpolations), |
| 119 | (interpolations) => t('conversation_title_four', interpolations), |
| 120 | (interpolations) => t('conversation_title_more', interpolations), |
| 121 | ], |
| 122 | }; |
| 123 | |
| 124 | return translateEnumeration<ConversationMember>(members, options); |
| 125 | }, [account, members, adminTitle, t]); |
| 126 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 127 | const startCall = (withVideo = false) => { |
simon | 3f5f3e7 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 128 | let url = `/call/${conversationId}`; |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 129 | if (withVideo) { |
| 130 | url += '?video=true'; |
| 131 | } |
| 132 | navigate(url); |
| 133 | }; |
| 134 | |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 135 | return ( |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 136 | <Stack direction="row" padding="16px" overflow="hidden"> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 137 | <Stack flex={1} justifyContent="center" whiteSpace="nowrap" overflow="hidden"> |
| 138 | <Typography variant="h3" textOverflow="ellipsis"> |
| 139 | {title} |
| 140 | </Typography> |
| 141 | </Stack> |
| 142 | <Stack direction="row" spacing="20px"> |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 143 | <StartAudioCallButton onClick={() => startCall(false)} /> |
| 144 | <StartVideoCallButton onClick={() => startCall(true)} /> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 145 | <AddParticipantButton /> |
| 146 | <ShowOptionsMenuButton /> |
| 147 | </Stack> |
| 148 | </Stack> |
| 149 | ); |
| 150 | }; |
| 151 | |
| 152 | const getMemberName = (member: ConversationMember) => { |
| 153 | const contact = member.contact; |
| 154 | return contact.getDisplayName(); |
| 155 | }; |
| 156 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 157 | export default ConversationView; |