From 983b0cdd629d6ce11b9847a49c06c8b48875efe7 Mon Sep 17 00:00:00 2001 From: Lawrence Baker Date: Wed, 7 Jun 2023 11:26:12 -0400 Subject: [PATCH] needed deduplication on incoming messages. this fixes receiving published messages while fetching messages is in flight --- packages/common/src/chat/chat.tsx | 6 ++++-- packages/common/src/hooks/use-messages.ts | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/common/src/chat/chat.tsx b/packages/common/src/chat/chat.tsx index 10c5ff09a..a5180eadd 100644 --- a/packages/common/src/chat/chat.tsx +++ b/packages/common/src/chat/chat.tsx @@ -195,7 +195,8 @@ export const ChatInternal: FC = (props: ChatProps) => { setMessages((messages) => { const messagesClone = cloneDeep(messages) || {}; messagesClone[message.channel] = messagesClone[message.channel] || []; - messagesClone[message.channel].push(message); + const hasMessage = messagesClone[message.channel].findIndex(m => m.timetoken === message.timetoken) > -1; + if (!hasMessage) messagesClone[message.channel].push(message); return messagesClone; }); @@ -298,7 +299,8 @@ export const ChatInternal: FC = (props: ChatProps) => { const newMessage = { ...payload, message: { file, message }, messageType: 4 }; const messagesClone = cloneDeep(messages) || {}; messagesClone[newMessage.channel] = messagesClone[newMessage.channel] || []; - messagesClone[newMessage.channel].push(newMessage); + const hasMessage = messagesClone[message.channel].findIndex(m => m.timetoken === message.timetoken) > -1; + if (!hasMessage) messagesClone[message.channel].push(message); return messagesClone; }); diff --git a/packages/common/src/hooks/use-messages.ts b/packages/common/src/hooks/use-messages.ts index 4a48b6bd5..fa57dcb33 100644 --- a/packages/common/src/hooks/use-messages.ts +++ b/packages/common/src/hooks/use-messages.ts @@ -49,7 +49,8 @@ export const useMessages = (options: FetchMessagesParameters): HookReturnValue = setMessages((messages) => { const messagesClone = cloneDeep(messages); if (!messagesClone[message.channel]) messagesClone[message.channel] = []; - messagesClone[message.channel].push(message); + const hasMessage = messagesClone[message.channel].findIndex(m => m.timetoken === message.timetoken) > -1; + if (!hasMessage) messagesClone[message.channel].push(message); return messagesClone; }); } catch (e) {