blob: 52215e89ece04b6d041c5eb26712db911c8c141b [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';
simonf929a362022-11-18 16:53:45 -050019import { Account, ConversationMember } from 'jami-web-common';
20import { useContext, useMemo } from 'react';
idillonae655dd2022-10-14 18:11:02 -040021import { useTranslation } from 'react-i18next';
simon07b4eb02022-09-29 17:50:26 -040022
simon5da8ca62022-11-09 15:21:25 -050023import { useAuthContext } from '../contexts/AuthProvider';
simonf929a362022-11-18 16:53:45 -050024import { ConversationContext } from '../contexts/ConversationProvider';
simonff1cb352022-11-24 15:15:26 -050025import { useStartCall } from '../hooks/useStartCall';
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 = () => {
simon5da8ca62022-11-09 15:21:25 -050031 const { account } = useAuthContext();
simonf929a362022-11-18 16:53:45 -050032 const { conversationId, conversation } = useContext(ConversationContext);
idillonbef18a52022-09-01 01:51:40 -040033
34 return (
simon9f814a32022-11-22 21:40:53 -050035 <Stack flexGrow={1} height="100%">
idillon02f579d2022-11-06 21:26:55 -050036 <ConversationHeader
37 account={account}
38 members={conversation.getMembers()}
39 adminTitle={conversation.infos.title as string}
40 conversationId={conversationId}
41 />
idillonae655dd2022-10-14 18:11:02 -040042 <Divider
43 sx={{
44 borderTop: '1px solid #E5E5E5',
45 }}
46 />
simon5da8ca62022-11-09 15:21:25 -050047 <ChatInterface conversationId={conversationId} members={conversation.getMembers()} />
idillonbef18a52022-09-01 01:51:40 -040048 </Stack>
simond47ef9e2022-09-28 22:24:28 -040049 );
50};
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040051
idillonae655dd2022-10-14 18:11:02 -040052type ConversationHeaderProps = {
53 account: Account;
simon1170c322022-10-31 14:51:31 -040054 conversationId: string;
idillonae655dd2022-10-14 18:11:02 -040055 members: ConversationMember[];
56 adminTitle: string | undefined;
57};
58
simonf929a362022-11-18 16:53:45 -050059const ConversationHeader = ({ account, members, adminTitle }: ConversationHeaderProps) => {
idillonae655dd2022-10-14 18:11:02 -040060 const { t } = useTranslation();
simonff1cb352022-11-24 15:15:26 -050061 const { conversationId } = useContext(ConversationContext);
idillonae655dd2022-10-14 18:11:02 -040062
63 const title = useMemo(() => {
64 if (adminTitle) {
65 return adminTitle;
66 }
67
68 const options: TranslateEnumerationOptions<ConversationMember> = {
69 elementPartialKey: 'member',
70 getElementValue: (member) => getMemberName(member),
71 translaters: [
72 () =>
73 // The user is chatting with themself
74 t('conversation_title_one', { member0: account?.getDisplayName() }),
75 (interpolations) => t('conversation_title_one', interpolations),
76 (interpolations) => t('conversation_title_two', interpolations),
77 (interpolations) => t('conversation_title_three', interpolations),
78 (interpolations) => t('conversation_title_four', interpolations),
79 (interpolations) => t('conversation_title_more', interpolations),
80 ],
81 };
82
83 return translateEnumeration<ConversationMember>(members, options);
84 }, [account, members, adminTitle, t]);
85
simonff1cb352022-11-24 15:15:26 -050086 const startCall = useStartCall();
87
idillonae655dd2022-10-14 18:11:02 -040088 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">
simonff1cb352022-11-24 15:15:26 -050096 <StartAudioCallButton onClick={() => startCall(conversationId)} />
97 <StartVideoCallButton
98 onClick={() =>
99 startCall(conversationId, {
100 isVideoOn: true,
101 })
102 }
103 />
idillonae655dd2022-10-14 18:11:02 -0400104 <AddParticipantButton />
105 <ShowOptionsMenuButton />
106 </Stack>
107 </Stack>
108 );
109};
110
111const getMemberName = (member: ConversationMember) => {
112 const contact = member.contact;
113 return contact.getDisplayName();
114};
115
simond47ef9e2022-09-28 22:24:28 -0400116export default ConversationView;