2022-06-17 10:48:15 +10:00
|
|
|
import { ChangeEvent, KeyboardEvent, useState } from "react";
|
|
|
|
|
import { useWaku } from "./WakuContext";
|
|
|
|
|
import {
|
|
|
|
|
TextInput,
|
|
|
|
|
TextComposer,
|
|
|
|
|
Row,
|
|
|
|
|
Fill,
|
|
|
|
|
Fit,
|
|
|
|
|
SendButton,
|
|
|
|
|
} from "@livechat/ui-kit";
|
|
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
sendMessage: ((msg: string) => Promise<void>) | undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function MessageInput(props: Props) {
|
|
|
|
|
const [inputText, setInputText] = useState<string>("");
|
|
|
|
|
const { waku } = useWaku();
|
|
|
|
|
|
|
|
|
|
const sendMessage = async () => {
|
|
|
|
|
if (props.sendMessage) {
|
|
|
|
|
await props.sendMessage(inputText);
|
|
|
|
|
setInputText("");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const messageHandler = (event: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
setInputText(event.target.value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const keyPressHandler = async (event: KeyboardEvent<HTMLInputElement>) => {
|
|
|
|
|
if (
|
|
|
|
|
event.key === "Enter" &&
|
|
|
|
|
!event.altKey &&
|
|
|
|
|
!event.ctrlKey &&
|
|
|
|
|
!event.shiftKey
|
|
|
|
|
) {
|
|
|
|
|
await sendMessage();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Enable the button if there are relay peers available or the user is sending a command
|
|
|
|
|
const activeButton =
|
2022-09-01 14:12:25 +10:00
|
|
|
(waku && waku.relay.getMeshPeers().length !== 0) ||
|
|
|
|
|
inputText.startsWith("/");
|
2022-06-17 10:48:15 +10:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<TextComposer
|
|
|
|
|
onKeyDown={keyPressHandler}
|
|
|
|
|
onChange={messageHandler}
|
|
|
|
|
active={activeButton}
|
|
|
|
|
onButtonClick={sendMessage}
|
|
|
|
|
>
|
|
|
|
|
<Row align="center">
|
|
|
|
|
<Fill>
|
|
|
|
|
<TextInput value={inputText} />
|
|
|
|
|
</Fill>
|
|
|
|
|
<Fit>
|
|
|
|
|
<SendButton />
|
|
|
|
|
</Fit>
|
|
|
|
|
</Row>
|
|
|
|
|
</TextComposer>
|
|
|
|
|
);
|
|
|
|
|
}
|