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 { InputBase } from '@mui/material'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 19 | import { Stack } from '@mui/system'; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 20 | import { ChangeEvent, FormEvent, useCallback, useMemo, useState } from 'react'; |
| 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'; |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 24 | import { ConversationMember } from '../models/conversation'; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 25 | import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 26 | import { |
| 27 | RecordVideoMessageButton, |
| 28 | RecordVoiceMessageButton, |
| 29 | SelectEmojiButton, |
| 30 | SendMessageButton, |
| 31 | UploadFileButton, |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 32 | } from './Button'; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 33 | |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 34 | type SendMessageFormProps = { |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 35 | members: ConversationMember[]; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 36 | onSend: (message: string) => void; |
idillon | cab81d7 | 2022-11-08 12:20:00 -0500 | [diff] [blame] | 37 | openFilePicker: () => void; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 38 | }; |
| 39 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 40 | export default function SendMessageForm({ members, onSend, openFilePicker }: SendMessageFormProps) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 41 | const [currentMessage, setCurrentMessage] = useState(''); |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 42 | const placeholder = usePlaceholder(members); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 43 | |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 44 | const handleSubmit = (e: FormEvent<HTMLFormElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 45 | e.preventDefault(); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 46 | if (currentMessage) { |
idillon | cab81d7 | 2022-11-08 12:20:00 -0500 | [diff] [blame] | 47 | onSend(currentMessage); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 48 | setCurrentMessage(''); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 49 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 50 | }; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 51 | const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => setCurrentMessage(event.target.value); |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 52 | |
Adrien Béraud | 023f7cf | 2022-09-18 14:57:53 -0400 | [diff] [blame] | 53 | const onEmojiSelected = useCallback( |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 54 | (emoji: string) => setCurrentMessage((currentMessage) => currentMessage + emoji), |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 55 | [setCurrentMessage] |
| 56 | ); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 57 | |
Adrien Béraud | 8a438f8 | 2021-04-14 16:18:57 -0400 | [diff] [blame] | 58 | return ( |
idillon | cab81d7 | 2022-11-08 12:20:00 -0500 | [diff] [blame] | 59 | <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} /> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 69 | <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 | /> |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 86 | </Stack> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 87 | <SelectEmojiButton onEmojiSelected={onEmojiSelected} /> |
| 88 | {currentMessage && <SendMessageButton type="submit" />} |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 89 | </Stack> |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 90 | ); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 91 | } |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 92 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 93 | const usePlaceholder = (members: ConversationMember[]) => { |
| 94 | const { account } = useAuthContext(); |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 95 | const { t } = useTranslation(); |
| 96 | |
| 97 | return useMemo(() => { |
| 98 | const options: TranslateEnumerationOptions<ConversationMember> = { |
| 99 | elementPartialKey: 'member', |
| 100 | getElementValue: (member) => getMemberName(member), |
| 101 | translaters: [ |
| 102 | () => |
| 103 | // The user is chatting with themself |
| 104 | t('message_input_placeholder_one', { member0: account?.getDisplayName() }), |
| 105 | (interpolations) => t('message_input_placeholder_one', interpolations), |
| 106 | (interpolations) => t('message_input_placeholder_two', interpolations), |
| 107 | (interpolations) => t('message_input_placeholder_three', interpolations), |
| 108 | (interpolations) => t('message_input_placeholder_four', interpolations), |
| 109 | (interpolations) => t('message_input_placeholder_more', interpolations), |
| 110 | ], |
| 111 | }; |
| 112 | |
| 113 | return translateEnumeration<ConversationMember>(members, options); |
| 114 | }, [account, members, t]); |
| 115 | }; |
| 116 | |
| 117 | const getMemberName = (member: ConversationMember) => { |
| 118 | const contact = member.contact; |
| 119 | return contact.getDisplayName(); |
| 120 | }; |