blob: a94ab565189947ef6d90ba79495ff78f670b2e75 [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 { ChangeEvent, FormEvent, useCallback, useMemo, useState } from 'react';
21import { useTranslation } from 'react-i18next';
simon07b4eb02022-09-29 17:50:26 -040022
simon5da8ca62022-11-09 15:21:25 -050023import { useAuthContext } from '../contexts/AuthProvider';
idillon07d31cc2022-12-06 22:40:14 -050024import { useConversationContext } from '../contexts/ConversationProvider';
25import { ConversationMember } from '../models/conversation-member';
idillonae655dd2022-10-14 18:11:02 -040026import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations';
simond47ef9e2022-09-28 22:24:28 -040027import {
28 RecordVideoMessageButton,
29 RecordVoiceMessageButton,
30 SelectEmojiButton,
31 SendMessageButton,
32 UploadFileButton,
simon6b9ddfb2022-10-03 00:04:50 -040033} from './Button';
Larbi Gharibe9af9732021-03-31 15:08:01 +010034
simon6b9ddfb2022-10-03 00:04:50 -040035type SendMessageFormProps = {
36 onSend: (message: string) => void;
idilloncab81d72022-11-08 12:20:00 -050037 openFilePicker: () => void;
simon6b9ddfb2022-10-03 00:04:50 -040038};
39
idillon07d31cc2022-12-06 22:40:14 -050040export default function SendMessageForm({ onSend, openFilePicker }: SendMessageFormProps) {
41 const { members } = useConversationContext();
simond47ef9e2022-09-28 22:24:28 -040042 const [currentMessage, setCurrentMessage] = useState('');
simon5da8ca62022-11-09 15:21:25 -050043 const placeholder = usePlaceholder(members);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040044
simon6b9ddfb2022-10-03 00:04:50 -040045 const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
simond47ef9e2022-09-28 22:24:28 -040046 e.preventDefault();
Adrien Béraud88a52442021-04-26 12:11:41 -040047 if (currentMessage) {
idilloncab81d72022-11-08 12:20:00 -050048 onSend(currentMessage);
simond47ef9e2022-09-28 22:24:28 -040049 setCurrentMessage('');
Adrien Béraud88a52442021-04-26 12:11:41 -040050 }
simond47ef9e2022-09-28 22:24:28 -040051 };
simon6b9ddfb2022-10-03 00:04:50 -040052 const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => setCurrentMessage(event.target.value);
idillonaedab942022-09-01 14:29:43 -040053
Adrien Béraud023f7cf2022-09-18 14:57:53 -040054 const onEmojiSelected = useCallback(
simon6b9ddfb2022-10-03 00:04:50 -040055 (emoji: string) => setCurrentMessage((currentMessage) => currentMessage + emoji),
simond47ef9e2022-09-28 22:24:28 -040056 [setCurrentMessage]
57 );
Larbi Gharibe9af9732021-03-31 15:08:01 +010058
Adrien Béraud8a438f82021-04-14 16:18:57 -040059 return (
idilloncab81d72022-11-08 12:20:00 -050060 <Stack
61 component="form"
62 onSubmit={handleSubmit}
63 direction="row"
64 alignItems="center"
65 spacing="20px"
66 paddingX="16px"
67 paddingTop="16px"
68 >
69 <UploadFileButton onClick={openFilePicker} />
idillonae655dd2022-10-14 18:11:02 -040070 <RecordVoiceMessageButton />
71 <RecordVideoMessageButton />
72 <Stack flexGrow={1}>
73 <InputBase
74 placeholder={placeholder}
75 value={currentMessage}
76 onChange={handleInputChange}
77 sx={{
78 fontSize: '15px',
79 color: 'black',
80 '& ::placeholder': {
81 color: '#7E7E7E',
82 opacity: 1,
83 textOverflow: 'ellipsis',
84 },
85 }}
86 />
idillonaedab942022-09-01 14:29:43 -040087 </Stack>
idillonae655dd2022-10-14 18:11:02 -040088 <SelectEmojiButton onEmojiSelected={onEmojiSelected} />
89 {currentMessage && <SendMessageButton type="submit" />}
idillonaedab942022-09-01 14:29:43 -040090 </Stack>
Adrien Béraudab519ff2022-05-03 15:34:48 -040091 );
Larbi Gharibe9af9732021-03-31 15:08:01 +010092}
idillonae655dd2022-10-14 18:11:02 -040093
simon5da8ca62022-11-09 15:21:25 -050094const usePlaceholder = (members: ConversationMember[]) => {
95 const { account } = useAuthContext();
idillonae655dd2022-10-14 18:11:02 -040096 const { t } = useTranslation();
97
98 return useMemo(() => {
99 const options: TranslateEnumerationOptions<ConversationMember> = {
100 elementPartialKey: 'member',
idillon07d31cc2022-12-06 22:40:14 -0500101 getElementValue: (member) => member.getDisplayName(),
idillonae655dd2022-10-14 18:11:02 -0400102 translaters: [
103 () =>
104 // The user is chatting with themself
105 t('message_input_placeholder_one', { member0: account?.getDisplayName() }),
106 (interpolations) => t('message_input_placeholder_one', interpolations),
107 (interpolations) => t('message_input_placeholder_two', interpolations),
108 (interpolations) => t('message_input_placeholder_three', interpolations),
109 (interpolations) => t('message_input_placeholder_four', interpolations),
110 (interpolations) => t('message_input_placeholder_more', interpolations),
111 ],
112 };
113
114 return translateEnumeration<ConversationMember>(members, options);
115 }, [account, members, t]);
116};