Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit the length of messages to 200 bytes #405

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/PageComponents/Messages/ChannelChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
);
Expand Down
14 changes: 12 additions & 2 deletions src/components/PageComponents/Messages/MessageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -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 (
Expand All @@ -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>
Expand Down