2022-09-02 14:08:44 +10:00
|
|
|
import { ChangeEvent, KeyboardEvent, useEffect, useState } from "react";
|
2023-02-28 00:36:17 +01:00
|
|
|
import { useWaku } from "@waku/react";
|
|
|
|
|
import { LightNode } from "@waku/interfaces";
|
2022-06-17 10:48:15 +10:00
|
|
|
import {
|
|
|
|
|
TextInput,
|
|
|
|
|
TextComposer,
|
|
|
|
|
Row,
|
|
|
|
|
Fill,
|
|
|
|
|
Fit,
|
|
|
|
|
SendButton,
|
|
|
|
|
} from "@livechat/ui-kit";
|
|
|
|
|
|
|
|
|
|
interface Props {
|
2023-02-28 00:36:17 +01:00
|
|
|
hasLightPushPeers: boolean;
|
2022-06-17 10:48:15 +10:00
|
|
|
sendMessage: ((msg: string) => Promise<void>) | undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function MessageInput(props: Props) {
|
2023-02-28 00:36:17 +01:00
|
|
|
const { hasLightPushPeers } = props;
|
|
|
|
|
const { node } = useWaku<LightNode>();
|
|
|
|
|
|
2022-06-17 10:48:15 +10:00
|
|
|
const [inputText, setInputText] = useState<string>("");
|
2023-02-28 00:36:17 +01:00
|
|
|
const [isActive, setActiveButton] = useState<boolean>(false);
|
2022-06-17 10:48:15 +10:00
|
|
|
|
2023-02-28 00:36:17 +01:00
|
|
|
const onMessage = async () => {
|
2023-03-02 00:56:20 +01:00
|
|
|
if (props.sendMessage && inputText) {
|
2023-04-22 01:04:39 +02:00
|
|
|
try {
|
|
|
|
|
await props.sendMessage(inputText);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(`Failed to send message: ${e}`);
|
|
|
|
|
}
|
2022-06-17 10:48:15 +10:00
|
|
|
setInputText("");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-28 00:36:17 +01:00
|
|
|
const onChange = (event: ChangeEvent<HTMLInputElement>) => {
|
2023-03-02 00:56:20 +01:00
|
|
|
event.preventDefault();
|
2022-06-17 10:48:15 +10:00
|
|
|
setInputText(event.target.value);
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-28 00:36:17 +01:00
|
|
|
const onKeyDown = async (event: KeyboardEvent<HTMLInputElement>) => {
|
2022-06-17 10:48:15 +10:00
|
|
|
if (
|
2023-04-22 01:04:39 +02:00
|
|
|
isActive &&
|
2022-06-17 10:48:15 +10:00
|
|
|
event.key === "Enter" &&
|
|
|
|
|
!event.altKey &&
|
|
|
|
|
!event.ctrlKey &&
|
|
|
|
|
!event.shiftKey
|
|
|
|
|
) {
|
2023-02-28 00:36:17 +01:00
|
|
|
await onMessage();
|
2022-06-17 10:48:15 +10:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-02 14:08:44 +10:00
|
|
|
// Enable the button if there are peers available or the user is sending a command
|
|
|
|
|
useEffect(() => {
|
2023-02-28 00:36:17 +01:00
|
|
|
if (inputText.startsWith("/") || hasLightPushPeers) {
|
2022-09-02 14:08:44 +10:00
|
|
|
setActiveButton(true);
|
2023-02-28 00:36:17 +01:00
|
|
|
} else if (node) {
|
|
|
|
|
setActiveButton(false);
|
2022-09-02 14:08:44 +10:00
|
|
|
}
|
2023-02-28 00:36:17 +01:00
|
|
|
}, [node, inputText, hasLightPushPeers]);
|
2022-06-17 10:48:15 +10:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<TextComposer
|
2023-02-28 00:36:17 +01:00
|
|
|
onKeyDown={onKeyDown}
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
active={isActive}
|
|
|
|
|
onButtonClick={onMessage}
|
2022-06-17 10:48:15 +10:00
|
|
|
>
|
|
|
|
|
<Row align="center">
|
|
|
|
|
<Fill>
|
2023-04-22 01:04:39 +02:00
|
|
|
<TextInput />
|
2022-06-17 10:48:15 +10:00
|
|
|
</Fill>
|
|
|
|
|
<Fit>
|
|
|
|
|
<SendButton />
|
|
|
|
|
</Fit>
|
|
|
|
|
</Row>
|
|
|
|
|
</TextComposer>
|
|
|
|
|
);
|
|
|
|
|
}
|