blob: 51c0899e8019f8e05ea0fc7aca03e42730df1ada [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';
simone35acc22022-12-02 16:51:12 -050019import { useContext, useMemo } from 'react';
idillonae655dd2022-10-14 18:11:02 -040020import { useTranslation } from 'react-i18next';
simon07b4eb02022-09-29 17:50:26 -040021
simon5da8ca62022-11-09 15:21:25 -050022import { useAuthContext } from '../contexts/AuthProvider';
simone35acc22022-12-02 16:51:12 -050023import { CallManagerContext } from '../contexts/CallManagerProvider';
simon5c677962022-12-02 16:51:54 -050024import { useCallContext } from '../contexts/CallProvider';
simon09fe4822022-11-30 23:36:25 -050025import { useConversationContext } from '../contexts/ConversationProvider';
simon5c677962022-12-02 16:51:54 -050026import { useWebRtcContext } from '../contexts/WebRtcProvider';
Misha Krieger-Raynauldcfa44302022-11-30 18:36:36 -050027import { ConversationMember } from '../models/conversation';
simone35acc22022-12-02 16:51:12 -050028import CallInterface from '../pages/CallInterface';
idillon6847e252022-11-04 11:50:08 -040029import ChatInterface from '../pages/ChatInterface';
idillonae655dd2022-10-14 18:11:02 -040030import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations';
31import { AddParticipantButton, ShowOptionsMenuButton, StartAudioCallButton, StartVideoCallButton } from './Button';
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040032
simonf929a362022-11-18 16:53:45 -050033const ConversationView = () => {
simone35acc22022-12-02 16:51:12 -050034 const { conversationId } = useConversationContext();
simon5c677962022-12-02 16:51:54 -050035 const webRtcContext = useWebRtcContext(true);
36 const callContext = useCallContext(true);
simone35acc22022-12-02 16:51:12 -050037 const { callData } = useContext(CallManagerContext);
38
simon5c677962022-12-02 16:51:54 -050039 if (webRtcContext && callContext && callData?.conversationId === conversationId) {
simone35acc22022-12-02 16:51:12 -050040 return <CallInterface />;
41 }
42
idillonbef18a52022-09-01 01:51:40 -040043 return (
simon9f814a32022-11-22 21:40:53 -050044 <Stack flexGrow={1} height="100%">
simonf9d78f22022-11-25 15:47:15 -050045 <ConversationHeader />
idillonae655dd2022-10-14 18:11:02 -040046 <Divider
47 sx={{
48 borderTop: '1px solid #E5E5E5',
49 }}
50 />
simonf9d78f22022-11-25 15:47:15 -050051 <ChatInterface />
idillonbef18a52022-09-01 01:51:40 -040052 </Stack>
simond47ef9e2022-09-28 22:24:28 -040053 );
54};
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040055
simonf9d78f22022-11-25 15:47:15 -050056const ConversationHeader = () => {
57 const { account } = useAuthContext();
simon09fe4822022-11-30 23:36:25 -050058 const { conversation, conversationId } = useConversationContext();
simone35acc22022-12-02 16:51:12 -050059 const { startCall } = useContext(CallManagerContext);
idillonae655dd2022-10-14 18:11:02 -040060 const { t } = useTranslation();
61
Misha Krieger-Raynauldcfa44302022-11-30 18:36:36 -050062 const members = conversation.members;
simonf9d78f22022-11-25 15:47:15 -050063 const adminTitle = conversation.infos.title as string;
64
idillonae655dd2022-10-14 18:11:02 -040065 const title = useMemo(() => {
66 if (adminTitle) {
67 return adminTitle;
68 }
69
70 const options: TranslateEnumerationOptions<ConversationMember> = {
71 elementPartialKey: 'member',
72 getElementValue: (member) => getMemberName(member),
73 translaters: [
74 () =>
75 // The user is chatting with themself
76 t('conversation_title_one', { member0: account?.getDisplayName() }),
77 (interpolations) => t('conversation_title_one', interpolations),
78 (interpolations) => t('conversation_title_two', interpolations),
79 (interpolations) => t('conversation_title_three', interpolations),
80 (interpolations) => t('conversation_title_four', interpolations),
81 (interpolations) => t('conversation_title_more', interpolations),
82 ],
83 };
84
85 return translateEnumeration<ConversationMember>(members, options);
86 }, [account, members, adminTitle, t]);
87
88 return (
idillon6847e252022-11-04 11:50:08 -040089 <Stack direction="row" padding="16px" overflow="hidden">
idillonae655dd2022-10-14 18:11:02 -040090 <Stack flex={1} justifyContent="center" whiteSpace="nowrap" overflow="hidden">
91 <Typography variant="h3" textOverflow="ellipsis">
92 {title}
93 </Typography>
94 </Stack>
95 <Stack direction="row" spacing="20px">
simone35acc22022-12-02 16:51:12 -050096 <StartAudioCallButton onClick={() => startCall({ conversationId, role: 'caller' })} />
97 <StartVideoCallButton onClick={() => startCall({ conversationId, role: 'caller', withVideoOn: true })} />
idillonae655dd2022-10-14 18:11:02 -040098 <AddParticipantButton />
99 <ShowOptionsMenuButton />
100 </Stack>
101 </Stack>
102 );
103};
104
105const getMemberName = (member: ConversationMember) => {
106 const contact = member.contact;
107 return contact.getDisplayName();
108};
109
simond47ef9e2022-09-28 22:24:28 -0400110export default ConversationView;