From c6f6ab58f1890ece72c4de372fe54639b1c345f3 Mon Sep 17 00:00:00 2001 From: PhilReact Date: Thu, 21 Nov 2024 00:44:48 +0200 Subject: [PATCH] added byte limit to chat --- src/components/Chat/ChatDirect.tsx | 37 ++++++++++++++++++++++ src/components/Chat/ChatGroup.tsx | 51 +++++++++++++++++++++++++++--- 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/src/components/Chat/ChatDirect.tsx b/src/components/Chat/ChatDirect.tsx index a053b67..2dcd6a2 100644 --- a/src/components/Chat/ChatDirect.tsx +++ b/src/components/Chat/ChatDirect.tsx @@ -46,6 +46,8 @@ export const ChatDirect = ({ myAddress, isNewChat, selectedDirect, setSelectedDi const setEditorRef = (editorInstance) => { editorRef.current = editorInstance; }; + const [messageSize, setMessageSize] = useState(0) + const [, forceUpdate] = useReducer((x) => x + 1, 0); const triggerRerender = () => { @@ -299,6 +301,7 @@ const sendChatDirect = async ({ chatReference = undefined, messageText, otherDat const clearEditorContent = () => { if (editorRef.current) { editorRef.current.chain().focus().clearContent().run(); + setMessageSize(0) if(isMobile){ setTimeout(() => { editorRef.current?.chain().blur().run(); @@ -312,6 +315,24 @@ const clearEditorContent = () => { } }; +useEffect(() => { + if (!editorRef?.current) return; + const handleUpdate = () => { + const htmlContent = editorRef?.current.getHTML(); + const stringified = JSON.stringify(htmlContent); + const size = new Blob([stringified]).size; + setMessageSize(size + 100); + }; + + // Add a listener for the editorRef?.current's content updates + editorRef?.current.on('update', handleUpdate); + + // Cleanup the listener on unmount + return () => { + editorRef?.current.off('update', handleUpdate); + }; +}, [editorRef?.current]); + const sendMessage = async ()=> { try { @@ -574,6 +595,7 @@ const clearEditorContent = () => { )} { + if(messageSize > 4000) return if(isSending) return sendMessage() }} @@ -603,6 +625,21 @@ const clearEditorContent = () => { + {isFocusedParent && messageSize > 750 && ( + + 4000 ? 'var(--unread)' : 'unset' + }}>{`size ${messageSize} of 4000`} + + + )} { + if (!editorRef?.current) return; + const handleUpdate = () => { + const htmlContent = editorRef?.current.getHTML(); + const stringified = JSON.stringify(htmlContent); + const size = new Blob([stringified]).size; + setMessageSize(size + 100); + }; + + // Add a listener for the editorRef?.current's content updates + editorRef?.current.on('update', handleUpdate); + + // Cleanup the listener on unmount + return () => { + editorRef?.current.off('update', handleUpdate); + }; + }, [editorRef?.current]); + useEffect(()=> { const notifications = messages.filter((message)=> message?.decryptedData @@ -516,6 +536,7 @@ const sendChatGroup = async ({groupId, typeMessage = undefined, chatReference = const clearEditorContent = () => { if (editorRef.current) { editorRef.current.chain().focus().clearContent().run(); + setMessageSize(0) if(isMobile){ setTimeout(() => { editorRef.current?.chain().blur().run(); @@ -743,19 +764,22 @@ const clearEditorContent = () => { width: '100%' }}> - + -
- {}} members={members} myName={myName} selectedGroup={selectedGroup}/>
+ { if(isSending) return setIsFocusedParent(false) clearEditorContent() + // Unfocus the editor }} style={{ @@ -786,6 +811,7 @@ const clearEditorContent = () => { {isFocusedParent && ( { + if(messageSize > 4000) return if(isSending) return sendMessage() }} @@ -818,6 +844,21 @@ const clearEditorContent = () => { + {isFocusedParent && messageSize > 750 && ( + + 4000 ? 'var(--unread)' : 'unset' + }}>{`size ${messageSize} of 4000`} + + + )} {/* */} {/* */}