simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 18 | import { Divider, Stack, Typography } from '@mui/material'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 19 | import { Account, ConversationMember } from 'jami-web-common'; |
| 20 | import { useContext, useMemo } from 'react'; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 21 | import { useTranslation } from 'react-i18next'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 22 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 23 | import { useAuthContext } from '../contexts/AuthProvider'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 24 | import { ConversationContext } from '../contexts/ConversationProvider'; |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 25 | import ChatInterface from '../pages/ChatInterface'; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 26 | import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations'; |
| 27 | import { AddParticipantButton, ShowOptionsMenuButton, StartAudioCallButton, StartVideoCallButton } from './Button'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 28 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 29 | const ConversationView = () => { |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 30 | const { account } = useAuthContext(); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 31 | const { conversationId, conversation } = useContext(ConversationContext); |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 32 | |
| 33 | return ( |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 34 | <Stack height="100%"> |
idillon | 02f579d | 2022-11-06 21:26:55 -0500 | [diff] [blame] | 35 | <ConversationHeader |
| 36 | account={account} |
| 37 | members={conversation.getMembers()} |
| 38 | adminTitle={conversation.infos.title as string} |
| 39 | conversationId={conversationId} |
| 40 | /> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 41 | <Divider |
| 42 | sx={{ |
| 43 | borderTop: '1px solid #E5E5E5', |
| 44 | }} |
| 45 | /> |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 46 | <ChatInterface conversationId={conversationId} members={conversation.getMembers()} /> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 47 | </Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 48 | ); |
| 49 | }; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 50 | |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 51 | type ConversationHeaderProps = { |
| 52 | account: Account; |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 53 | conversationId: string; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 54 | members: ConversationMember[]; |
| 55 | adminTitle: string | undefined; |
| 56 | }; |
| 57 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 58 | const ConversationHeader = ({ account, members, adminTitle }: ConversationHeaderProps) => { |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 59 | const { t } = useTranslation(); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 60 | const { beginCall } = useContext(ConversationContext); |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 61 | |
| 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 ( |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 86 | <Stack direction="row" padding="16px" overflow="hidden"> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 87 | <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"> |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 93 | <StartAudioCallButton onClick={() => beginCall()} /> |
| 94 | <StartVideoCallButton onClick={() => beginCall()} /> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 95 | <AddParticipantButton /> |
| 96 | <ShowOptionsMenuButton /> |
| 97 | </Stack> |
| 98 | </Stack> |
| 99 | ); |
| 100 | }; |
| 101 | |
| 102 | const getMemberName = (member: ConversationMember) => { |
| 103 | const contact = member.contact; |
| 104 | return contact.getDisplayName(); |
| 105 | }; |
| 106 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 107 | export default ConversationView; |