blob: 03f0717d84bb22e720e56923a8c9bb19e881a2af [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';
idillon6847e252022-11-04 11:50:08 -040019import { Account, Conversation, ConversationMember } from 'jami-web-common';
20import { 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';
simon35378692022-10-02 23:25:57 -040025import { SocketContext } from '../contexts/Socket';
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();
simond47ef9e2022-09-28 22:24:28 -040037 const socket = useContext(SocketContext);
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(() => {
62 if (!conversation) return;
simon80b7b3b2022-09-28 17:50:10 -040063 console.log(`io set conversation ${conversationId} ` + socket);
simon35378692022-10-02 23:25:57 -040064 if (socket) {
65 socket.emit('conversation', {
66 accountId,
67 conversationId,
68 });
simon35378692022-10-02 23:25:57 -040069 }
simon80b7b3b2022-09-28 17:50:10 -040070 }, [accountId, conversation, conversationId, socket]);
Adrien Béraudabba2e52021-04-24 21:39:56 -040071
idillonea465602022-09-13 19:58:51 -040072 if (isLoading) {
simond47ef9e2022-09-28 22:24:28 -040073 return <LoadingPage />;
idillonae655dd2022-10-14 18:11:02 -040074 } else if (error || !account || !conversation) {
simon80b7b3b2022-09-28 17:50:10 -040075 return <div>Error loading {conversationId}</div>;
Adrien Béraud5e9e19b2021-04-22 01:38:53 -040076 }
idillonbef18a52022-09-01 01:51:40 -040077
78 return (
idillonae655dd2022-10-14 18:11:02 -040079 <Stack height="100%">
idillon02f579d2022-11-06 21:26:55 -050080 <ConversationHeader
81 account={account}
82 members={conversation.getMembers()}
83 adminTitle={conversation.infos.title as string}
84 conversationId={conversationId}
85 />
idillonae655dd2022-10-14 18:11:02 -040086 <Divider
87 sx={{
88 borderTop: '1px solid #E5E5E5',
89 }}
90 />
simon5da8ca62022-11-09 15:21:25 -050091 <ChatInterface conversationId={conversationId} members={conversation.getMembers()} />
idillonbef18a52022-09-01 01:51:40 -040092 </Stack>
simond47ef9e2022-09-28 22:24:28 -040093 );
94};
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040095
idillonae655dd2022-10-14 18:11:02 -040096type ConversationHeaderProps = {
97 account: Account;
simon1170c322022-10-31 14:51:31 -040098 conversationId: string;
idillonae655dd2022-10-14 18:11:02 -040099 members: ConversationMember[];
100 adminTitle: string | undefined;
101};
102
simon1170c322022-10-31 14:51:31 -0400103const ConversationHeader = ({ account, members, adminTitle, conversationId }: ConversationHeaderProps) => {
idillonae655dd2022-10-14 18:11:02 -0400104 const { t } = useTranslation();
simon1170c322022-10-31 14:51:31 -0400105 const navigate = useNavigate();
idillonae655dd2022-10-14 18:11:02 -0400106
107 const title = useMemo(() => {
108 if (adminTitle) {
109 return adminTitle;
110 }
111
112 const options: TranslateEnumerationOptions<ConversationMember> = {
113 elementPartialKey: 'member',
114 getElementValue: (member) => getMemberName(member),
115 translaters: [
116 () =>
117 // The user is chatting with themself
118 t('conversation_title_one', { member0: account?.getDisplayName() }),
119 (interpolations) => t('conversation_title_one', interpolations),
120 (interpolations) => t('conversation_title_two', interpolations),
121 (interpolations) => t('conversation_title_three', interpolations),
122 (interpolations) => t('conversation_title_four', interpolations),
123 (interpolations) => t('conversation_title_more', interpolations),
124 ],
125 };
126
127 return translateEnumeration<ConversationMember>(members, options);
128 }, [account, members, adminTitle, t]);
129
simon1170c322022-10-31 14:51:31 -0400130 const startCall = (withVideo = false) => {
simoncd698c52022-11-08 19:21:38 -0500131 let url = `/account/call/${conversationId}`;
simon1170c322022-10-31 14:51:31 -0400132 if (withVideo) {
133 url += '?video=true';
134 }
135 navigate(url);
136 };
137
idillonae655dd2022-10-14 18:11:02 -0400138 return (
idillon6847e252022-11-04 11:50:08 -0400139 <Stack direction="row" padding="16px" overflow="hidden">
idillonae655dd2022-10-14 18:11:02 -0400140 <Stack flex={1} justifyContent="center" whiteSpace="nowrap" overflow="hidden">
141 <Typography variant="h3" textOverflow="ellipsis">
142 {title}
143 </Typography>
144 </Stack>
145 <Stack direction="row" spacing="20px">
simon1170c322022-10-31 14:51:31 -0400146 <StartAudioCallButton onClick={() => startCall(false)} />
147 <StartVideoCallButton onClick={() => startCall(true)} />
idillonae655dd2022-10-14 18:11:02 -0400148 <AddParticipantButton />
149 <ShowOptionsMenuButton />
150 </Stack>
151 </Stack>
152 );
153};
154
155const getMemberName = (member: ConversationMember) => {
156 const contact = member.contact;
157 return contact.getDisplayName();
158};
159
simond47ef9e2022-09-28 22:24:28 -0400160export default ConversationView;