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 | */ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 18 | import { Divider, InputBase } from '@mui/material'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 19 | import { Stack } from '@mui/system'; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 20 | import { ChangeEvent, FormEvent, useCallback, useState } from 'react'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 21 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 22 | import { |
| 23 | RecordVideoMessageButton, |
| 24 | RecordVoiceMessageButton, |
| 25 | SelectEmojiButton, |
| 26 | SendMessageButton, |
| 27 | UploadFileButton, |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 28 | } from './Button'; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 29 | |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 30 | type SendMessageFormProps = { |
| 31 | onSend: (message: string) => void; |
| 32 | }; |
| 33 | |
| 34 | export default function SendMessageForm(props: SendMessageFormProps) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 35 | const [currentMessage, setCurrentMessage] = useState(''); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 36 | |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 37 | const handleSubmit = (e: FormEvent<HTMLFormElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 38 | e.preventDefault(); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 39 | if (currentMessage) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 40 | props.onSend(currentMessage); |
| 41 | setCurrentMessage(''); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 42 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 43 | }; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 44 | const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => setCurrentMessage(event.target.value); |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 45 | |
Adrien Béraud | 023f7cf | 2022-09-18 14:57:53 -0400 | [diff] [blame] | 46 | const onEmojiSelected = useCallback( |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 47 | (emoji: string) => setCurrentMessage((currentMessage) => currentMessage + emoji), |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 48 | [setCurrentMessage] |
| 49 | ); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 50 | |
Adrien Béraud | 8a438f8 | 2021-04-14 16:18:57 -0400 | [diff] [blame] | 51 | return ( |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 52 | <Stack padding="30px 16px 0px 16px"> |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 53 | <Divider |
| 54 | sx={{ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 55 | bordeTop: '1px solid #E5E5E5', |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 56 | }} |
| 57 | /> |
| 58 | <Stack |
| 59 | component="form" |
Adrien Béraud | 8a438f8 | 2021-04-14 16:18:57 -0400 | [diff] [blame] | 60 | onSubmit={handleSubmit} |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 61 | direction="row" |
| 62 | alignItems="center" |
| 63 | flexGrow={1} |
| 64 | spacing="20px" |
| 65 | padding="16px 0px" |
| 66 | > |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 67 | <UploadFileButton /> |
| 68 | <RecordVoiceMessageButton /> |
| 69 | <RecordVideoMessageButton /> |
Adrien Béraud | 8a438f8 | 2021-04-14 16:18:57 -0400 | [diff] [blame] | 70 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 71 | <Stack flexGrow={1}> |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 72 | <InputBase |
| 73 | placeholder="Write something nice" |
| 74 | value={currentMessage} |
| 75 | onChange={handleInputChange} |
| 76 | sx={{ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 77 | fontSize: '15px', |
| 78 | color: 'black', |
| 79 | '& ::placeholder': { |
| 80 | color: '#7E7E7E', |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 81 | opacity: 1, |
| 82 | }, |
| 83 | }} |
| 84 | /> |
| 85 | </Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 86 | <SelectEmojiButton onEmojiSelected={onEmojiSelected} /> |
| 87 | {currentMessage && <SendMessageButton type="submit" />} |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 88 | </Stack> |
| 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 | } |