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