blob: 80e05c77b24e45857397256fe0887c6c50ef66c5 [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
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 */
idillonae655dd2022-10-14 18:11:02 -040018import { Divider, Stack, Typography } from '@mui/material';
Issam E. Maghni0432cb72022-11-12 06:09:26 +000019import { Account, Conversation, ConversationMember, WebSocketMessageType } from 'jami-web-common';
idillon6847e252022-11-04 11:50:08 -040020import { useContext, useEffect, useMemo, useState } from 'react';
idillonae655dd2022-10-14 18:11:02 -040021import { useTranslation } from 'react-i18next';
simon1170c322022-10-31 14:51:31 -040022import { useNavigate } from 'react-router';
simon07b4eb02022-09-29 17:50:26 -040023
simon5da8ca62022-11-09 15:21:25 -050024import { useAuthContext } from '../contexts/AuthProvider';
Issam E. Maghni0432cb72022-11-12 06:09:26 +000025import { WebSocketContext } from '../contexts/WebSocketProvider';
idillon6847e252022-11-04 11:50:08 -040026import ChatInterface from '../pages/ChatInterface';
idillon6847e252022-11-04 11:50:08 -040027import { useConversationQuery } from '../services/Conversation';
idillonae655dd2022-10-14 18:11:02 -040028import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations';
29import { AddParticipantButton, ShowOptionsMenuButton, StartAudioCallButton, StartVideoCallButton } from './Button';
simon6b9ddfb2022-10-03 00:04:50 -040030import LoadingPage from './Loading';
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040031
simon35378692022-10-02 23:25:57 -040032type ConversationViewProps = {
simon35378692022-10-02 23:25:57 -040033 conversationId: string;
34};
simon5da8ca62022-11-09 15:21:25 -050035const ConversationView = ({ conversationId }: ConversationViewProps) => {
36 const { account } = useAuthContext();
Issam E. Maghni0432cb72022-11-12 06:09:26 +000037 const webSocket = useContext(WebSocketContext);
simon35378692022-10-02 23:25:57 -040038 const [conversation, setConversation] = useState<Conversation | undefined>();
simond47ef9e2022-09-28 22:24:28 -040039 const [isLoading, setIsLoading] = useState(true);
40 const [error, setError] = useState(false);
idillon08f77172022-09-13 19:14:17 -040041
simon5da8ca62022-11-09 15:21:25 -050042 const accountId = account.getId();
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040043
simon5da8ca62022-11-09 15:21:25 -050044 const conversationQuery = useConversationQuery(conversationId);
idillonae655dd2022-10-14 18:11:02 -040045
46 useEffect(() => {
idillonea465602022-09-13 19:58:51 -040047 if (conversationQuery.isSuccess) {
simon80b7b3b2022-09-28 17:50:10 -040048 const conversation = Conversation.from(accountId, conversationQuery.data);
simond47ef9e2022-09-28 22:24:28 -040049 setConversation(conversation);
idillon08f77172022-09-13 19:14:17 -040050 }
simon80b7b3b2022-09-28 17:50:10 -040051 }, [accountId, conversationQuery.isSuccess, conversationQuery.data]);
idillon08f77172022-09-13 19:14:17 -040052
53 useEffect(() => {
simon5da8ca62022-11-09 15:21:25 -050054 setIsLoading(conversationQuery.isLoading);
55 }, [conversationQuery.isLoading]);
idillon08f77172022-09-13 19:14:17 -040056
57 useEffect(() => {
simon5da8ca62022-11-09 15:21:25 -050058 setError(conversationQuery.isError);
59 }, [conversationQuery.isError]);
simond47ef9e2022-09-28 22:24:28 -040060
61 useEffect(() => {
Issam E. Maghni0432cb72022-11-12 06:09:26 +000062 if (!conversation || !webSocket) {
63 return;
simon35378692022-10-02 23:25:57 -040064 }
Issam E. Maghni0432cb72022-11-12 06:09:26 +000065 console.log(`set conversation ${conversationId} ` + webSocket);
66 webSocket.send(WebSocketMessageType.ConversationView, { accountId, conversationId });
67 }, [accountId, conversation, conversationId, webSocket]);
Adrien Béraudabba2e52021-04-24 21:39:56 -040068
idillonea465602022-09-13 19:58:51 -040069 if (isLoading) {
simond47ef9e2022-09-28 22:24:28 -040070 return <LoadingPage />;
idillonae655dd2022-10-14 18:11:02 -040071 } else if (error || !account || !conversation) {
simon80b7b3b2022-09-28 17:50:10 -040072 return <div>Error loading {conversationId}</div>;
Adrien Béraud5e9e19b2021-04-22 01:38:53 -040073 }
idillonbef18a52022-09-01 01:51:40 -040074
75 return (
idillonae655dd2022-10-14 18:11:02 -040076 <Stack height="100%">
idillon02f579d2022-11-06 21:26:55 -050077 <ConversationHeader
78 account={account}
79 members={conversation.getMembers()}
80 adminTitle={conversation.infos.title as string}
81 conversationId={conversationId}
82 />
idillonae655dd2022-10-14 18:11:02 -040083 <Divider
84 sx={{
85 borderTop: '1px solid #E5E5E5',
86 }}
87 />
simon5da8ca62022-11-09 15:21:25 -050088 <ChatInterface conversationId={conversationId} members={conversation.getMembers()} />
idillonbef18a52022-09-01 01:51:40 -040089 </Stack>
simond47ef9e2022-09-28 22:24:28 -040090 );
91};
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040092
idillonae655dd2022-10-14 18:11:02 -040093type ConversationHeaderProps = {
94 account: Account;
simon1170c322022-10-31 14:51:31 -040095 conversationId: string;
idillonae655dd2022-10-14 18:11:02 -040096 members: ConversationMember[];
97 adminTitle: string | undefined;
98};
99
simon1170c322022-10-31 14:51:31 -0400100const ConversationHeader = ({ account, members, adminTitle, conversationId }: ConversationHeaderProps) => {
idillonae655dd2022-10-14 18:11:02 -0400101 const { t } = useTranslation();
simon1170c322022-10-31 14:51:31 -0400102 const navigate = useNavigate();
idillonae655dd2022-10-14 18:11:02 -0400103
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
simon1170c322022-10-31 14:51:31 -0400127 const startCall = (withVideo = false) => {
simon3f5f3e72022-11-08 21:01:57 -0500128 let url = `/call/${conversationId}`;
simon1170c322022-10-31 14:51:31 -0400129 if (withVideo) {
130 url += '?video=true';
131 }
132 navigate(url);
133 };
134
idillonae655dd2022-10-14 18:11:02 -0400135 return (
idillon6847e252022-11-04 11:50:08 -0400136 <Stack direction="row" padding="16px" overflow="hidden">
idillonae655dd2022-10-14 18:11:02 -0400137 <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">
simon1170c322022-10-31 14:51:31 -0400143 <StartAudioCallButton onClick={() => startCall(false)} />
144 <StartVideoCallButton onClick={() => startCall(true)} />
idillonae655dd2022-10-14 18:11:02 -0400145 <AddParticipantButton />
146 <ShowOptionsMenuButton />
147 </Stack>
148 </Stack>
149 );
150};
151
152const getMemberName = (member: ConversationMember) => {
153 const contact = member.contact;
154 return contact.getDisplayName();
155};
156
simond47ef9e2022-09-28 22:24:28 -0400157export default ConversationView;