blob: 73c65d7f09809f1283143c0ae4e3306dd325428e [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';
idillon6847e252022-11-04 11:50:08 -040025import ChatInterface from '../pages/ChatInterface';
idillonae655dd2022-10-14 18:11:02 -040026import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations';
27import { AddParticipantButton, ShowOptionsMenuButton, StartAudioCallButton, StartVideoCallButton } from './Button';
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040028
simonf929a362022-11-18 16:53:45 -050029const ConversationView = () => {
simon5da8ca62022-11-09 15:21:25 -050030 const { account } = useAuthContext();
simonf929a362022-11-18 16:53:45 -050031 const { conversationId, conversation } = useContext(ConversationContext);
idillonbef18a52022-09-01 01:51:40 -040032
33 return (
idillonae655dd2022-10-14 18:11:02 -040034 <Stack height="100%">
idillon02f579d2022-11-06 21:26:55 -050035 <ConversationHeader
36 account={account}
37 members={conversation.getMembers()}
38 adminTitle={conversation.infos.title as string}
39 conversationId={conversationId}
40 />
idillonae655dd2022-10-14 18:11:02 -040041 <Divider
42 sx={{
43 borderTop: '1px solid #E5E5E5',
44 }}
45 />
simon5da8ca62022-11-09 15:21:25 -050046 <ChatInterface conversationId={conversationId} members={conversation.getMembers()} />
idillonbef18a52022-09-01 01:51:40 -040047 </Stack>
simond47ef9e2022-09-28 22:24:28 -040048 );
49};
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040050
idillonae655dd2022-10-14 18:11:02 -040051type ConversationHeaderProps = {
52 account: Account;
simon1170c322022-10-31 14:51:31 -040053 conversationId: string;
idillonae655dd2022-10-14 18:11:02 -040054 members: ConversationMember[];
55 adminTitle: string | undefined;
56};
57
simonf929a362022-11-18 16:53:45 -050058const ConversationHeader = ({ account, members, adminTitle }: ConversationHeaderProps) => {
idillonae655dd2022-10-14 18:11:02 -040059 const { t } = useTranslation();
simonf929a362022-11-18 16:53:45 -050060 const { beginCall } = useContext(ConversationContext);
idillonae655dd2022-10-14 18:11:02 -040061
62 const title = useMemo(() => {
63 if (adminTitle) {
64 return adminTitle;
65 }
66
67 const options: TranslateEnumerationOptions<ConversationMember> = {
68 elementPartialKey: 'member',
69 getElementValue: (member) => getMemberName(member),
70 translaters: [
71 () =>
72 // The user is chatting with themself
73 t('conversation_title_one', { member0: account?.getDisplayName() }),
74 (interpolations) => t('conversation_title_one', interpolations),
75 (interpolations) => t('conversation_title_two', interpolations),
76 (interpolations) => t('conversation_title_three', interpolations),
77 (interpolations) => t('conversation_title_four', interpolations),
78 (interpolations) => t('conversation_title_more', interpolations),
79 ],
80 };
81
82 return translateEnumeration<ConversationMember>(members, options);
83 }, [account, members, adminTitle, t]);
84
85 return (
idillon6847e252022-11-04 11:50:08 -040086 <Stack direction="row" padding="16px" overflow="hidden">
idillonae655dd2022-10-14 18:11:02 -040087 <Stack flex={1} justifyContent="center" whiteSpace="nowrap" overflow="hidden">
88 <Typography variant="h3" textOverflow="ellipsis">
89 {title}
90 </Typography>
91 </Stack>
92 <Stack direction="row" spacing="20px">
simonf929a362022-11-18 16:53:45 -050093 <StartAudioCallButton onClick={() => beginCall()} />
94 <StartVideoCallButton onClick={() => beginCall()} />
idillonae655dd2022-10-14 18:11:02 -040095 <AddParticipantButton />
96 <ShowOptionsMenuButton />
97 </Stack>
98 </Stack>
99 );
100};
101
102const getMemberName = (member: ConversationMember) => {
103 const contact = member.contact;
104 return contact.getDisplayName();
105};
106
simond47ef9e2022-09-28 22:24:28 -0400107export default ConversationView;