diff --git a/src/MessageQueueContext.tsx b/src/MessageQueueContext.tsx index 3727b03..7104520 100644 --- a/src/MessageQueueContext.tsx +++ b/src/MessageQueueContext.tsx @@ -92,21 +92,6 @@ export const MessageQueueProvider = ({ children }) => { // Remove the message from the queue after successful sending messageQueueRef.current.shift(); - // Remove the message from queueChats - setQueueChats((prev) => { - const updatedChats = { ...prev }; - if (updatedChats[groupDirectId]) { - updatedChats[groupDirectId] = updatedChats[groupDirectId].filter( - (item) => item.identifier !== identifier - ); - - // If no more chats for this group, delete the groupDirectId entry - if (updatedChats[groupDirectId].length === 0) { - delete updatedChats[groupDirectId]; - } - } - return updatedChats; - }); } catch (error) { console.error('Message sending failed', error); @@ -142,15 +127,25 @@ export const MessageQueueProvider = ({ children }) => { // Method to process with new messages and groupDirectId const processWithNewMessages = (newMessages, groupDirectId) => { + let updatedNewMessages = newMessages if (newMessages.length > 0) { - messageQueueRef.current = messageQueueRef.current.filter((msg) => { - return !newMessages.some(newMsg => newMsg?.specialId === msg?.specialId); - }); - // Remove corresponding entries in queueChats for the provided groupDirectId setQueueChats((prev) => { const updatedChats = { ...prev }; if (updatedChats[groupDirectId]) { + + updatedNewMessages = newMessages?.map((msg)=> { + const findTempMsg = updatedChats[groupDirectId]?.find((msg2)=> msg2?.message?.specialId === msg?.specialId) + if(findTempMsg){ + return { + ...msg, + tempSignature: findTempMsg?.signature + } + } + return msg + }) + + updatedChats[groupDirectId] = updatedChats[groupDirectId].filter((chat) => { return !newMessages.some(newMsg => newMsg?.specialId === chat?.message?.specialId); }); @@ -167,8 +162,23 @@ export const MessageQueueProvider = ({ children }) => { } return updatedChats; }); + } - + setTimeout(() => { + if(!messageQueueRef.current.find((msg) => msg?.groupDirectId === groupDirectId)){ + setQueueChats((prev) => { + const updatedChats = { ...prev }; + if (updatedChats[groupDirectId]) { + delete updatedChats[groupDirectId] + } + + return updatedChats + } + ) + } + }, 300); + + return updatedNewMessages }; return ( diff --git a/src/components/Chat/ChatDirect.tsx b/src/components/Chat/ChatDirect.tsx index 9b3dcbb..5e2014e 100644 --- a/src/components/Chat/ChatDirect.tsx +++ b/src/components/Chat/ChatDirect.tsx @@ -118,9 +118,9 @@ export const ChatDirect = ({ myAddress, isNewChat, selectedDirect, setSelectedDi data: encryptedMessages, involvingAddress: selectedDirect?.address, }) - .then((response) => { - if (!response?.error) { - processWithNewMessages(response, selectedDirect?.address); + .then((decryptResponse) => { + if (!decryptResponse?.error) { + const response = processWithNewMessages(decryptResponse, selectedDirect?.address); res(response); if (isInitiated) { diff --git a/src/components/Chat/ChatGroup.tsx b/src/components/Chat/ChatGroup.tsx index 4aed230..02523a9 100644 --- a/src/components/Chat/ChatGroup.tsx +++ b/src/components/Chat/ChatGroup.tsx @@ -193,9 +193,9 @@ const [messageSize, setMessageSize] = useState(0) const filterUIMessages = encryptedMessages.filter((item) => !isExtMsg(item.data)); const decodedUIMessages = decodeBase64ForUIChatMessages(filterUIMessages); - const combineUIAndExtensionMsgs = [...decodedUIMessages, ...response]; - processWithNewMessages( - combineUIAndExtensionMsgs.map((item) => ({ + const combineUIAndExtensionMsgsBefore = [...decodedUIMessages, ...response]; + const combineUIAndExtensionMsgs = processWithNewMessages( + combineUIAndExtensionMsgsBefore.map((item) => ({ ...item, ...(item?.decryptedData || {}), })), diff --git a/src/components/Chat/ChatList.tsx b/src/components/Chat/ChatList.tsx index 3cae591..67a05f7 100644 --- a/src/components/Chat/ChatList.tsx +++ b/src/components/Chat/ChatList.tsx @@ -39,11 +39,10 @@ export const ChatList = ({ const scrollingIntervalRef = useRef(null); const lastSeenUnreadMessageTimestamp = useRef(null); - // Initialize the virtualizer const rowVirtualizer = useVirtualizer({ count: messages.length, - getItemKey: (index) => messages[index].signature, + getItemKey: (index) => messages[index]?.tempSignature || messages[index].signature, getScrollElement: () => parentRef?.current, estimateSize: useCallback(() => 80, []), // Provide an estimated height of items, adjust this as needed overscan: 10, // Number of items to render outside the visible area to improve smoothness