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 | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 20 | import { WebSocketMessageType } from 'jami-web-common'; |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 21 | import { ChangeEvent, FormEvent, useCallback, useMemo, useRef, useState } from 'react'; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 22 | import { useTranslation } from 'react-i18next'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 23 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 24 | import { useAuthContext } from '../contexts/AuthProvider'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 25 | import { useConversationContext } from '../contexts/ConversationProvider'; |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 26 | import { useWebSocketContext } from '../contexts/WebSocketProvider'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 27 | import { ConversationMember } from '../models/conversation-member'; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 28 | import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 29 | import { |
| 30 | RecordVideoMessageButton, |
| 31 | RecordVoiceMessageButton, |
| 32 | SelectEmojiButton, |
| 33 | SendMessageButton, |
| 34 | UploadFileButton, |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 35 | } from './Button'; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 36 | |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 37 | type SendMessageFormProps = { |
| 38 | onSend: (message: string) => void; |
idillon | cab81d7 | 2022-11-08 12:20:00 -0500 | [diff] [blame] | 39 | openFilePicker: () => void; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 40 | }; |
| 41 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 42 | export default function SendMessageForm({ onSend, openFilePicker }: SendMessageFormProps) { |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 43 | const webSocket = useWebSocketContext(); |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 44 | const { members, conversationId } = useConversationContext(); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 45 | const [currentMessage, setCurrentMessage] = useState(''); |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 46 | const composingNotificationTimeRef = useRef(0); |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 47 | const placeholder = usePlaceholder(members); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 48 | |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 49 | const notifyComposing = useCallback(() => { |
| 50 | const currentTime = new Date().getTime(); |
| 51 | // The daemon automatically turns off "isComposing" after 12 seconds |
| 52 | // We ensure it will stay on at least 4 seconds after the last typed character |
| 53 | if (currentTime - composingNotificationTimeRef.current > 8000) { |
| 54 | composingNotificationTimeRef.current = currentTime; |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 55 | webSocket.send(WebSocketMessageType.ComposingStatus, { conversationId, isWriting: true }); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 56 | } |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 57 | }, [webSocket, conversationId]); |
| 58 | |
| 59 | const notifyStopcomposing = useCallback(() => { |
| 60 | composingNotificationTimeRef.current = 0; |
idillon | bad5197 | 2023-01-18 17:00:38 -0500 | [diff] [blame] | 61 | webSocket?.send(WebSocketMessageType.ComposingStatus, { conversationId, isWriting: false }); |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 62 | }, [webSocket, conversationId]); |
| 63 | |
| 64 | const handleSubmit = useCallback( |
| 65 | (e: FormEvent<HTMLFormElement>) => { |
| 66 | e.preventDefault(); |
| 67 | if (currentMessage) { |
| 68 | onSend(currentMessage); |
| 69 | setCurrentMessage(''); |
| 70 | notifyStopcomposing(); |
| 71 | } |
| 72 | }, |
| 73 | [currentMessage, onSend, notifyStopcomposing] |
| 74 | ); |
| 75 | |
| 76 | const handleInputChange = useCallback( |
| 77 | (event: ChangeEvent<HTMLInputElement>) => { |
| 78 | setCurrentMessage(event.target.value); |
| 79 | notifyComposing(); |
| 80 | }, |
| 81 | [notifyComposing] |
| 82 | ); |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 83 | |
Adrien Béraud | 023f7cf | 2022-09-18 14:57:53 -0400 | [diff] [blame] | 84 | const onEmojiSelected = useCallback( |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 85 | (emoji: string) => setCurrentMessage((currentMessage) => currentMessage + emoji), |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 86 | [setCurrentMessage] |
| 87 | ); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 88 | |
Adrien Béraud | 8a438f8 | 2021-04-14 16:18:57 -0400 | [diff] [blame] | 89 | return ( |
idillon | cab81d7 | 2022-11-08 12:20:00 -0500 | [diff] [blame] | 90 | <Stack |
| 91 | component="form" |
| 92 | onSubmit={handleSubmit} |
| 93 | direction="row" |
| 94 | alignItems="center" |
| 95 | spacing="20px" |
| 96 | paddingX="16px" |
| 97 | paddingTop="16px" |
| 98 | > |
| 99 | <UploadFileButton onClick={openFilePicker} /> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 100 | <RecordVoiceMessageButton /> |
| 101 | <RecordVideoMessageButton /> |
| 102 | <Stack flexGrow={1}> |
| 103 | <InputBase |
| 104 | placeholder={placeholder} |
| 105 | value={currentMessage} |
| 106 | onChange={handleInputChange} |
| 107 | sx={{ |
| 108 | fontSize: '15px', |
| 109 | color: 'black', |
| 110 | '& ::placeholder': { |
| 111 | color: '#7E7E7E', |
| 112 | opacity: 1, |
| 113 | textOverflow: 'ellipsis', |
| 114 | }, |
| 115 | }} |
| 116 | /> |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 117 | </Stack> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 118 | <SelectEmojiButton onEmojiSelected={onEmojiSelected} /> |
| 119 | {currentMessage && <SendMessageButton type="submit" />} |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 120 | </Stack> |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 121 | ); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 122 | } |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 123 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 124 | const usePlaceholder = (members: ConversationMember[]) => { |
| 125 | const { account } = useAuthContext(); |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 126 | const { t } = useTranslation(); |
| 127 | |
| 128 | return useMemo(() => { |
| 129 | const options: TranslateEnumerationOptions<ConversationMember> = { |
| 130 | elementPartialKey: 'member', |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 131 | getElementValue: (member) => member.getDisplayName(), |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 132 | translaters: [ |
| 133 | () => |
| 134 | // The user is chatting with themself |
idillon | 6e8ca41 | 2022-12-16 11:22:42 -0500 | [diff] [blame] | 135 | t('message_input_placeholder_1', { member0: account?.getDisplayName() }), |
| 136 | (interpolations) => t('message_input_placeholder_1', interpolations), |
| 137 | (interpolations) => t('message_input_placeholder_2', interpolations), |
| 138 | (interpolations) => t('message_input_placeholder_3', interpolations), |
| 139 | (interpolations) => t('message_input_placeholder_4', interpolations), |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 140 | (interpolations) => t('message_input_placeholder_more', interpolations), |
| 141 | ], |
| 142 | }; |
| 143 | |
| 144 | return translateEnumeration<ConversationMember>(members, options); |
| 145 | }, [account, members, t]); |
| 146 | }; |