From 342b558a4f0651ba2d8748de52672045c3b7050c Mon Sep 17 00:00:00 2001 From: Sacha Weatherstone Date: Sat, 15 Oct 2022 22:48:44 +1100 Subject: [PATCH] Chat improvements --- .../PageComponents/Connect/Serial.tsx | 4 +- .../PageComponents/Messages/ChannelChat.tsx | 54 +-------------- .../PageComponents/Messages/MessageInput.tsx | 65 +++++++++++++++++++ 3 files changed, 71 insertions(+), 52 deletions(-) create mode 100644 src/components/PageComponents/Messages/MessageInput.tsx diff --git a/src/components/PageComponents/Connect/Serial.tsx b/src/components/PageComponents/Connect/Serial.tsx index 98c6616e..d8b13881 100644 --- a/src/components/PageComponents/Connect/Serial.tsx +++ b/src/components/PageComponents/Connect/Serial.tsx @@ -59,7 +59,9 @@ export const Serial = (): JSX.Element => { void onConnect(port); }} > - # {index} - {port.getInfo().usbVendorId} - {port.getInfo().usbProductId} + {`# ${index} - ${port.getInfo().usbVendorId} - ${ + port.getInfo().usbProductId + }`} ))} {serialPorts.length === 0 && ( diff --git a/src/components/PageComponents/Messages/ChannelChat.tsx b/src/components/PageComponents/Messages/ChannelChat.tsx index 4fdf5e86..e61c321a 100644 --- a/src/components/PageComponents/Messages/ChannelChat.tsx +++ b/src/components/PageComponents/Messages/ChannelChat.tsx @@ -1,13 +1,10 @@ import type React from "react"; -import { ChangeEvent, useState } from "react"; -import { Input } from "@app/components/form/Input.js"; -import { IconButton } from "@app/components/IconButton.js"; import { Message } from "@components/PageComponents/Messages/Message.js"; import { useDevice } from "@core/providers/useDevice.js"; import type { Channel } from "@core/stores/deviceStore.js"; -import { MapPinIcon, PaperAirplaneIcon } from "@heroicons/react/24/outline"; -import type { Types } from "@meshtastic/meshtasticjs"; + +import { MessageInput } from "./MessageInput.js"; export interface ChannelChatProps { channel: Channel; @@ -15,21 +12,6 @@ export interface ChannelChatProps { export const ChannelChat = ({ channel }: ChannelChatProps): JSX.Element => { const { nodes, connection, ackMessage } = useDevice(); - const [currentMessage, setCurrentMessage] = useState(""); - - const sendMessage = (): void => { - void connection?.sendText( - currentMessage, - undefined, - true, - channel.config.index as Types.ChannelNumber, - (id) => { - ackMessage(channel.config.index, id); - return Promise.resolve(); - } - ); - setCurrentMessage(""); - }; return (
@@ -50,37 +32,7 @@ export const ChannelChat = ({ channel }: ChannelChatProps): JSX.Element => { /> ))}
-
-
{ - e.preventDefault(); - sendMessage(); - }} - > -
- - ): void => { - setCurrentMessage(e.target.value); - }} - /> - - } - /> -
-
- } - /> -
+ ); }; diff --git a/src/components/PageComponents/Messages/MessageInput.tsx b/src/components/PageComponents/Messages/MessageInput.tsx new file mode 100644 index 00000000..d3fb8e38 --- /dev/null +++ b/src/components/PageComponents/Messages/MessageInput.tsx @@ -0,0 +1,65 @@ +import type React from "react"; + +import { useForm } from "react-hook-form"; + +import { Input } from "@app/components/form/Input.js"; +import { IconButton } from "@app/components/IconButton.js"; +import { useDevice } from "@core/providers/useDevice.js"; +import type { Channel } from "@core/stores/deviceStore.js"; +import { MapPinIcon, PaperAirplaneIcon } from "@heroicons/react/24/outline"; +import type { Types } from "@meshtastic/meshtasticjs"; + +export interface MessageInputProps { + channel: Channel; +} + +export const MessageInput = ({ channel }: MessageInputProps): JSX.Element => { + const { connection, ackMessage } = useDevice(); + + const { register, handleSubmit } = useForm<{ + message: string; + }>({ + defaultValues: { + message: "", + }, + }); + + const onSubmit = handleSubmit((data) => { + void connection?.sendText( + data.message, + undefined, + true, + channel.config.index as Types.ChannelNumber, + (id) => { + ackMessage(channel.config.index, id); + return Promise.resolve(); + } + ); + }); + + return ( +
+
+
+ + + + } + /> +
+
+ } + /> +
+ ); +};