Browse Source

Limit the length of messages to 200 bytes

pull/405/head
Hunter275 1 year ago
parent
commit
430e0cbd46
  1. 2
      src/components/PageComponents/Messages/ChannelChat.tsx
  2. 14
      src/components/PageComponents/Messages/MessageInput.tsx

2
src/components/PageComponents/Messages/ChannelChat.tsx

@ -69,7 +69,7 @@ export const ChannelChat = ({
</div>
</div>
<div className="pl-3 pr-3 pt-3 pb-1">
<MessageInput to={to} channel={channel} />
<MessageInput to={to} channel={channel} maxBytes={200} />
</div>
</div>
);

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

@ -9,11 +9,13 @@ import { useCallback, useMemo, useState } from "react";
export interface MessageInputProps {
to: Types.Destination;
channel: Types.ChannelNumber;
maxBytes: number;
}
export const MessageInput = ({
to,
channel,
maxBytes,
}: MessageInputProps): JSX.Element => {
const {
connection,
@ -24,6 +26,7 @@ export const MessageInput = ({
} = useDevice();
const myNodeNum = hardware.myNodeNum;
const [localDraft, setLocalDraft] = useState(messageDraft);
const [messageBytes, setMessageBytes] = useState(maxBytes);
const debouncedSetMessageDraft = useMemo(
() => debounce(setMessageDraft, 300),
@ -60,8 +63,12 @@ export const MessageInput = ({
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newValue = e.target.value;
setLocalDraft(newValue);
debouncedSetMessageDraft(newValue);
const byteLength = new Blob([newValue]).size;
if (byteLength <= maxBytes) {
setLocalDraft(newValue);
debouncedSetMessageDraft(newValue);
setMessageBytes(maxBytes - byteLength);
}
};
return (
@ -85,6 +92,9 @@ export const MessageInput = ({
onChange={handleInputChange}
/>
</span>
<div className="flex items-center">
{messageBytes}/{maxBytes}
</div>
<Button type="submit">
<SendIcon size={16} />
</Button>

Loading…
Cancel
Save