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 { Account, ConversationMember } from 'jami-web-common'; |
| 21 | import { ChangeEvent, FormEvent, useCallback, useMemo, useState } from 'react'; |
| 22 | import { useTranslation } from 'react-i18next'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 23 | |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 24 | import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 25 | import { |
| 26 | RecordVideoMessageButton, |
| 27 | RecordVoiceMessageButton, |
| 28 | SelectEmojiButton, |
| 29 | SendMessageButton, |
| 30 | UploadFileButton, |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 31 | } from './Button'; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 32 | |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 33 | type SendMessageFormProps = { |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 34 | account: Account; |
| 35 | members: ConversationMember[]; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 36 | onSend: (message: string) => void; |
| 37 | }; |
| 38 | |
| 39 | export default function SendMessageForm(props: SendMessageFormProps) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 40 | const [currentMessage, setCurrentMessage] = useState(''); |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 41 | const placeholder = usePlaceholder(props.account, props.members); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 42 | |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 43 | const handleSubmit = (e: FormEvent<HTMLFormElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 44 | e.preventDefault(); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 45 | if (currentMessage) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 46 | props.onSend(currentMessage); |
| 47 | setCurrentMessage(''); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 48 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 49 | }; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 50 | const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => setCurrentMessage(event.target.value); |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 51 | |
Adrien Béraud | 023f7cf | 2022-09-18 14:57:53 -0400 | [diff] [blame] | 52 | const onEmojiSelected = useCallback( |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 53 | (emoji: string) => setCurrentMessage((currentMessage) => currentMessage + emoji), |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 54 | [setCurrentMessage] |
| 55 | ); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 56 | |
Adrien Béraud | 8a438f8 | 2021-04-14 16:18:57 -0400 | [diff] [blame] | 57 | return ( |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 58 | <Stack component="form" onSubmit={handleSubmit} direction="row" alignItems="center" spacing="20px"> |
| 59 | <UploadFileButton /> |
| 60 | <RecordVoiceMessageButton /> |
| 61 | <RecordVideoMessageButton /> |
| 62 | <Stack flexGrow={1}> |
| 63 | <InputBase |
| 64 | placeholder={placeholder} |
| 65 | value={currentMessage} |
| 66 | onChange={handleInputChange} |
| 67 | sx={{ |
| 68 | fontSize: '15px', |
| 69 | color: 'black', |
| 70 | '& ::placeholder': { |
| 71 | color: '#7E7E7E', |
| 72 | opacity: 1, |
| 73 | textOverflow: 'ellipsis', |
| 74 | }, |
| 75 | }} |
| 76 | /> |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 77 | </Stack> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 78 | <SelectEmojiButton onEmojiSelected={onEmojiSelected} /> |
| 79 | {currentMessage && <SendMessageButton type="submit" />} |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 80 | </Stack> |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 81 | ); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 82 | } |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 83 | |
| 84 | const usePlaceholder = (account: Account, members: ConversationMember[]) => { |
| 85 | const { t } = useTranslation(); |
| 86 | |
| 87 | return useMemo(() => { |
| 88 | const options: TranslateEnumerationOptions<ConversationMember> = { |
| 89 | elementPartialKey: 'member', |
| 90 | getElementValue: (member) => getMemberName(member), |
| 91 | translaters: [ |
| 92 | () => |
| 93 | // The user is chatting with themself |
| 94 | t('message_input_placeholder_one', { member0: account?.getDisplayName() }), |
| 95 | (interpolations) => t('message_input_placeholder_one', interpolations), |
| 96 | (interpolations) => t('message_input_placeholder_two', interpolations), |
| 97 | (interpolations) => t('message_input_placeholder_three', interpolations), |
| 98 | (interpolations) => t('message_input_placeholder_four', interpolations), |
| 99 | (interpolations) => t('message_input_placeholder_more', interpolations), |
| 100 | ], |
| 101 | }; |
| 102 | |
| 103 | return translateEnumeration<ConversationMember>(members, options); |
| 104 | }, [account, members, t]); |
| 105 | }; |
| 106 | |
| 107 | const getMemberName = (member: ConversationMember) => { |
| 108 | const contact = member.contact; |
| 109 | return contact.getDisplayName(); |
| 110 | }; |