2021-04-29 08:13:09 +02:00
|
|
|
import { ChangeEvent, KeyboardEvent, useState } from 'react';
|
2021-04-22 17:05:19 +10:00
|
|
|
import { useWaku } from './WakuContext';
|
2021-04-29 08:13:09 +02:00
|
|
|
import {
|
|
|
|
|
TextInput,
|
|
|
|
|
TextComposer,
|
|
|
|
|
Row,
|
|
|
|
|
Fill,
|
|
|
|
|
Fit,
|
|
|
|
|
SendButton,
|
|
|
|
|
} from '@livechat/ui-kit';
|
2021-04-19 13:33:23 +10:00
|
|
|
|
|
|
|
|
interface Props {
|
2021-05-05 14:29:00 +10:00
|
|
|
sendMessage: ((msg: string) => Promise<void>) | undefined;
|
2021-04-19 13:33:23 +10:00
|
|
|
}
|
|
|
|
|
|
2021-04-22 17:01:46 +10:00
|
|
|
export default function MessageInput(props: Props) {
|
2021-04-22 17:02:10 +10:00
|
|
|
const [inputText, setInputText] = useState<string>('');
|
2021-04-22 17:05:19 +10:00
|
|
|
const { waku } = useWaku();
|
2021-04-19 13:33:23 +10:00
|
|
|
|
2021-04-27 13:36:25 +10:00
|
|
|
const sendMessage = async () => {
|
|
|
|
|
if (props.sendMessage) {
|
2021-05-05 14:29:00 +10:00
|
|
|
await props.sendMessage(inputText);
|
2021-04-27 13:36:25 +10:00
|
|
|
setInputText('');
|
|
|
|
|
}
|
2021-04-22 17:02:10 +10:00
|
|
|
};
|
2021-04-19 13:33:23 +10:00
|
|
|
|
2021-04-22 17:01:46 +10:00
|
|
|
const messageHandler = (event: ChangeEvent<HTMLInputElement>) => {
|
2021-04-22 17:02:10 +10:00
|
|
|
setInputText(event.target.value);
|
2021-04-22 17:01:46 +10:00
|
|
|
};
|
2021-04-19 13:33:23 +10:00
|
|
|
|
2021-04-27 13:36:25 +10:00
|
|
|
const keyPressHandler = async (event: KeyboardEvent<HTMLInputElement>) => {
|
2021-04-22 15:11:37 +10:00
|
|
|
if (event.key === 'Enter') {
|
2021-04-27 13:36:25 +10:00
|
|
|
await sendMessage();
|
2021-04-22 15:11:37 +10:00
|
|
|
}
|
2021-04-22 17:02:10 +10:00
|
|
|
};
|
2021-04-22 15:11:37 +10:00
|
|
|
|
2021-04-22 17:02:10 +10:00
|
|
|
return (
|
2021-04-29 08:13:09 +02:00
|
|
|
<TextComposer
|
|
|
|
|
onKeyDown={keyPressHandler}
|
|
|
|
|
onChange={messageHandler}
|
2021-04-30 14:06:58 +10:00
|
|
|
active={!!waku}
|
2021-04-29 08:13:09 +02:00
|
|
|
onButtonClick={sendMessage}
|
|
|
|
|
>
|
|
|
|
|
<Row align="center">
|
|
|
|
|
<Fill>
|
|
|
|
|
<TextInput value={inputText} />
|
|
|
|
|
</Fill>
|
|
|
|
|
<Fit>
|
|
|
|
|
<SendButton />
|
|
|
|
|
</Fit>
|
|
|
|
|
</Row>
|
|
|
|
|
</TextComposer>
|
2021-04-22 17:02:10 +10:00
|
|
|
);
|
2021-04-19 13:33:23 +10:00
|
|
|
}
|