Browse Source

fix: prevent empty/blank messages from being sent. fix: count chars during copy/paste action

pull/416/head
Dan Ditomaso 1 year ago
parent
commit
467effa62e
  1. 29
      src/components/PageComponents/Messages/MessageInput.tsx

29
src/components/PageComponents/Messages/MessageInput.tsx

@ -4,7 +4,13 @@ import { Input } from "@components/UI/Input.tsx";
import { useDevice } from "@core/stores/deviceStore.ts"; import { useDevice } from "@core/stores/deviceStore.ts";
import type { Types } from "@meshtastic/js"; import type { Types } from "@meshtastic/js";
import { SendIcon } from "lucide-react"; import { SendIcon } from "lucide-react";
import { type JSX, useCallback, useMemo, useState } from "react"; import {
type JSX,
startTransition,
useCallback,
useMemo,
useState,
} from "react";
export interface MessageInputProps { export interface MessageInputProps {
to: Types.Destination; to: Types.Destination;
@ -63,11 +69,11 @@ export const MessageInput = ({
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newValue = e.target.value; const newValue = e.target.value;
const byteLength = new Blob([newValue]).size; const messageLength = newValue.length;
if (byteLength <= maxBytes) { if (messageLength <= maxBytes) {
setLocalDraft(newValue); setLocalDraft(newValue);
debouncedSetMessageDraft(newValue); debouncedSetMessageDraft(newValue);
setMessageBytes(maxBytes - byteLength); setMessageBytes(maxBytes - messageLength);
} }
}; };
@ -75,11 +81,15 @@ export const MessageInput = ({
<div className="flex gap-2"> <div className="flex gap-2">
<form <form
className="w-full" className="w-full"
onSubmit={(e) => { action={async (formData: FormData) => {
e.preventDefault(); // prevent user from sending blank/empty message
sendText(localDraft); if (localDraft === "") return;
setLocalDraft(""); const message = formData.get("messageInput") as string;
setMessageDraft(""); startTransition(() => {
sendText(message);
setLocalDraft("");
setMessageDraft("");
});
}} }}
> >
<div className="flex flex-grow gap-2"> <div className="flex flex-grow gap-2">
@ -87,6 +97,7 @@ export const MessageInput = ({
<Input <Input
autoFocus={true} autoFocus={true}
minLength={1} minLength={1}
name="messageInput"
placeholder="Enter Message" placeholder="Enter Message"
value={localDraft} value={localDraft}
onChange={handleInputChange} onChange={handleInputChange}

Loading…
Cancel
Save