blob: 075ca6e54d63a90a1de8dade27910765ba5bb5ef [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';
simon09fe4822022-11-30 23:36:25 -050019import { 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';
simon09fe4822022-11-30 23:36:25 -050023import { useConversationContext } from '../contexts/ConversationProvider';
simonff1cb352022-11-24 15:15:26 -050024import { useStartCall } from '../hooks/useStartCall';
Misha Krieger-Raynauld6bbdacf2022-11-29 21:45:40 -050025import { ConversationMember } from '../models/Conversation';
idillon6847e252022-11-04 11:50:08 -040026import ChatInterface from '../pages/ChatInterface';
idillonae655dd2022-10-14 18:11:02 -040027import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations';
28import { AddParticipantButton, ShowOptionsMenuButton, StartAudioCallButton, StartVideoCallButton } from './Button';
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040029
simonf929a362022-11-18 16:53:45 -050030const ConversationView = () => {
idillonbef18a52022-09-01 01:51:40 -040031 return (
simon9f814a32022-11-22 21:40:53 -050032 <Stack flexGrow={1} height="100%">
simonf9d78f22022-11-25 15:47:15 -050033 <ConversationHeader />
idillonae655dd2022-10-14 18:11:02 -040034 <Divider
35 sx={{
36 borderTop: '1px solid #E5E5E5',
37 }}
38 />
simonf9d78f22022-11-25 15:47:15 -050039 <ChatInterface />
idillonbef18a52022-09-01 01:51:40 -040040 </Stack>
simond47ef9e2022-09-28 22:24:28 -040041 );
42};
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040043
simonf9d78f22022-11-25 15:47:15 -050044const ConversationHeader = () => {
45 const { account } = useAuthContext();
simon09fe4822022-11-30 23:36:25 -050046 const { conversation, conversationId } = useConversationContext();
idillonae655dd2022-10-14 18:11:02 -040047 const { t } = useTranslation();
48
simonf9d78f22022-11-25 15:47:15 -050049 const members = conversation.getMembers();
50 const adminTitle = conversation.infos.title as string;
51
idillonae655dd2022-10-14 18:11:02 -040052 const title = useMemo(() => {
53 if (adminTitle) {
54 return adminTitle;
55 }
56
57 const options: TranslateEnumerationOptions<ConversationMember> = {
58 elementPartialKey: 'member',
59 getElementValue: (member) => getMemberName(member),
60 translaters: [
61 () =>
62 // The user is chatting with themself
63 t('conversation_title_one', { member0: account?.getDisplayName() }),
64 (interpolations) => t('conversation_title_one', interpolations),
65 (interpolations) => t('conversation_title_two', interpolations),
66 (interpolations) => t('conversation_title_three', interpolations),
67 (interpolations) => t('conversation_title_four', interpolations),
68 (interpolations) => t('conversation_title_more', interpolations),
69 ],
70 };
71
72 return translateEnumeration<ConversationMember>(members, options);
73 }, [account, members, adminTitle, t]);
74
simonff1cb352022-11-24 15:15:26 -050075 const startCall = useStartCall();
76
idillonae655dd2022-10-14 18:11:02 -040077 return (
idillon6847e252022-11-04 11:50:08 -040078 <Stack direction="row" padding="16px" overflow="hidden">
idillonae655dd2022-10-14 18:11:02 -040079 <Stack flex={1} justifyContent="center" whiteSpace="nowrap" overflow="hidden">
80 <Typography variant="h3" textOverflow="ellipsis">
81 {title}
82 </Typography>
83 </Stack>
84 <Stack direction="row" spacing="20px">
Charlie380dc5e2022-11-29 16:51:42 -050085 <StartAudioCallButton onClick={() => startCall(conversationId)} />
simonff1cb352022-11-24 15:15:26 -050086 <StartVideoCallButton
87 onClick={() =>
Charlie380dc5e2022-11-29 16:51:42 -050088 startCall(conversationId, {
simonff1cb352022-11-24 15:15:26 -050089 isVideoOn: true,
90 })
91 }
92 />
idillonae655dd2022-10-14 18:11:02 -040093 <AddParticipantButton />
94 <ShowOptionsMenuButton />
95 </Stack>
96 </Stack>
97 );
98};
99
100const getMemberName = (member: ConversationMember) => {
101 const contact = member.contact;
102 return contact.getDisplayName();
103};
104
simond47ef9e2022-09-28 22:24:28 -0400105export default ConversationView;