blob: 797d810c3e01445adc9cffc1002b55706b37b51a [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 { InputBase } from '@mui/material';
simon07b4eb02022-09-29 17:50:26 -040019import { Stack } from '@mui/system';
idillonae655dd2022-10-14 18:11:02 -040020import { Account, ConversationMember } from 'jami-web-common';
21import { ChangeEvent, FormEvent, useCallback, useMemo, useState } from 'react';
22import { useTranslation } from 'react-i18next';
simon07b4eb02022-09-29 17:50:26 -040023
idillonae655dd2022-10-14 18:11:02 -040024import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations';
simond47ef9e2022-09-28 22:24:28 -040025import {
26 RecordVideoMessageButton,
27 RecordVoiceMessageButton,
28 SelectEmojiButton,
29 SendMessageButton,
30 UploadFileButton,
simon6b9ddfb2022-10-03 00:04:50 -040031} from './Button';
Larbi Gharibe9af9732021-03-31 15:08:01 +010032
simon6b9ddfb2022-10-03 00:04:50 -040033type SendMessageFormProps = {
idillonae655dd2022-10-14 18:11:02 -040034 account: Account;
35 members: ConversationMember[];
simon6b9ddfb2022-10-03 00:04:50 -040036 onSend: (message: string) => void;
idilloncab81d72022-11-08 12:20:00 -050037 openFilePicker: () => void;
simon6b9ddfb2022-10-03 00:04:50 -040038};
39
idilloncab81d72022-11-08 12:20:00 -050040export default function SendMessageForm({ account, members, onSend, openFilePicker }: SendMessageFormProps) {
simond47ef9e2022-09-28 22:24:28 -040041 const [currentMessage, setCurrentMessage] = useState('');
idilloncab81d72022-11-08 12:20:00 -050042 const placeholder = usePlaceholder(account, members);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040043
simon6b9ddfb2022-10-03 00:04:50 -040044 const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
simond47ef9e2022-09-28 22:24:28 -040045 e.preventDefault();
Adrien Béraud88a52442021-04-26 12:11:41 -040046 if (currentMessage) {
idilloncab81d72022-11-08 12:20:00 -050047 onSend(currentMessage);
simond47ef9e2022-09-28 22:24:28 -040048 setCurrentMessage('');
Adrien Béraud88a52442021-04-26 12:11:41 -040049 }
simond47ef9e2022-09-28 22:24:28 -040050 };
simon6b9ddfb2022-10-03 00:04:50 -040051 const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => setCurrentMessage(event.target.value);
idillonaedab942022-09-01 14:29:43 -040052
Adrien Béraud023f7cf2022-09-18 14:57:53 -040053 const onEmojiSelected = useCallback(
simon6b9ddfb2022-10-03 00:04:50 -040054 (emoji: string) => setCurrentMessage((currentMessage) => currentMessage + emoji),
simond47ef9e2022-09-28 22:24:28 -040055 [setCurrentMessage]
56 );
Larbi Gharibe9af9732021-03-31 15:08:01 +010057
Adrien Béraud8a438f82021-04-14 16:18:57 -040058 return (
idilloncab81d72022-11-08 12:20:00 -050059 <Stack
60 component="form"
61 onSubmit={handleSubmit}
62 direction="row"
63 alignItems="center"
64 spacing="20px"
65 paddingX="16px"
66 paddingTop="16px"
67 >
68 <UploadFileButton onClick={openFilePicker} />
idillonae655dd2022-10-14 18:11:02 -040069 <RecordVoiceMessageButton />
70 <RecordVideoMessageButton />
71 <Stack flexGrow={1}>
72 <InputBase
73 placeholder={placeholder}
74 value={currentMessage}
75 onChange={handleInputChange}
76 sx={{
77 fontSize: '15px',
78 color: 'black',
79 '& ::placeholder': {
80 color: '#7E7E7E',
81 opacity: 1,
82 textOverflow: 'ellipsis',
83 },
84 }}
85 />
idillonaedab942022-09-01 14:29:43 -040086 </Stack>
idillonae655dd2022-10-14 18:11:02 -040087 <SelectEmojiButton onEmojiSelected={onEmojiSelected} />
88 {currentMessage && <SendMessageButton type="submit" />}
idillonaedab942022-09-01 14:29:43 -040089 </Stack>
Adrien Béraudab519ff2022-05-03 15:34:48 -040090 );
Larbi Gharibe9af9732021-03-31 15:08:01 +010091}
idillonae655dd2022-10-14 18:11:02 -040092
93const usePlaceholder = (account: Account, members: ConversationMember[]) => {
94 const { t } = useTranslation();
95
96 return useMemo(() => {
97 const options: TranslateEnumerationOptions<ConversationMember> = {
98 elementPartialKey: 'member',
99 getElementValue: (member) => getMemberName(member),
100 translaters: [
101 () =>
102 // The user is chatting with themself
103 t('message_input_placeholder_one', { member0: account?.getDisplayName() }),
104 (interpolations) => t('message_input_placeholder_one', interpolations),
105 (interpolations) => t('message_input_placeholder_two', interpolations),
106 (interpolations) => t('message_input_placeholder_three', interpolations),
107 (interpolations) => t('message_input_placeholder_four', interpolations),
108 (interpolations) => t('message_input_placeholder_more', interpolations),
109 ],
110 };
111
112 return translateEnumeration<ConversationMember>(members, options);
113 }, [account, members, t]);
114};
115
116const getMemberName = (member: ConversationMember) => {
117 const contact = member.contact;
118 return contact.getDisplayName();
119};