From 9cb1a977e021e4deb07a3a287c974cbe1b313b80 Mon Sep 17 00:00:00 2001 From: PhilReact Date: Wed, 11 Sep 2024 22:29:17 +0300 Subject: [PATCH] remove duplicate msgs --- src/MessageQueueContext.tsx | 4 ++-- src/components/Chat/ChatList.tsx | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/MessageQueueContext.tsx b/src/MessageQueueContext.tsx index 2c6493d..2284da0 100644 --- a/src/MessageQueueContext.tsx +++ b/src/MessageQueueContext.tsx @@ -11,7 +11,7 @@ let messageQueue = []; // Global message queue export const MessageQueueProvider = ({ children }) => { const [queueChats, setQueueChats] = useState({}); // Stores chats and status for display const isProcessingRef = useRef(false); // To track if the queue is being processed - const maxRetries = 3; + const maxRetries = 4; const clearStatesMessageQueueProvider = useCallback(() => { setQueueChats({}) @@ -122,7 +122,7 @@ const processQueue = useCallback(async () => { } // Delay between processing each message to avoid overlap - await new Promise((res) => setTimeout(res, 3000)); + await new Promise((res) => setTimeout(res, 5000)); } // Reset the processing lock once all messages are processed diff --git a/src/components/Chat/ChatList.tsx b/src/components/Chat/ChatList.tsx index 80a61b6..b4cae82 100644 --- a/src/components/Chat/ChatList.tsx +++ b/src/components/Chat/ChatList.tsx @@ -135,8 +135,16 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages }) => { }; useEffect(() => { - - const totalMessages = [...initialMessages, ...(tempMessages || [])] + let uniqueInitialMessagesMap = new Map(); + +// Iterate over initialMessages and add only unique messages based on signature +initialMessages.forEach((message) => { + uniqueInitialMessagesMap.set(message.signature, message); +}); + +// Convert the map back to an array and sort by timestamp (old to new) +let uniqueInitialMessages = Array.from(uniqueInitialMessagesMap.values()).sort((a, b) => a.timestamp - b.timestamp); + const totalMessages = [...uniqueInitialMessages, ...(tempMessages || [])] if(totalMessages.length === 0) return setMessages(totalMessages); // cache.clearAll(); // Clear cache so the list can properly re-render with new messages