Browse Source

remove duplicate msgs

master
PhilReact 1 week ago
parent
commit
9cb1a977e0
  1. 4
      src/MessageQueueContext.tsx
  2. 12
      src/components/Chat/ChatList.tsx

4
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

12
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

Loading…
Cancel
Save