From 1caec055172de0fc7ff782bea683fc0b2d2967d1 Mon Sep 17 00:00:00 2001 From: iamsivin Date: Thu, 25 Sep 2025 16:26:59 +0530 Subject: [PATCH] chore: Simplify reply message lookup with store-based fetching --- .../components-next/message/MessageList.vue | 100 ++++++------------ .../store/modules/conversations/index.js | 10 +- .../specs/conversations/mutations.spec.js | 23 ++++ 3 files changed, 66 insertions(+), 67 deletions(-) diff --git a/app/javascript/dashboard/components-next/message/MessageList.vue b/app/javascript/dashboard/components-next/message/MessageList.vue index b73b44d5a..e0c7abd34 100644 --- a/app/javascript/dashboard/components-next/message/MessageList.vue +++ b/app/javascript/dashboard/components-next/message/MessageList.vue @@ -1,10 +1,9 @@ diff --git a/app/javascript/dashboard/store/modules/conversations/index.js b/app/javascript/dashboard/store/modules/conversations/index.js index 2ee6fd061..19b52839d 100644 --- a/app/javascript/dashboard/store/modules/conversations/index.js +++ b/app/javascript/dashboard/store/modules/conversations/index.js @@ -74,9 +74,17 @@ export const mutations = { [types.SET_PREVIOUS_CONVERSATIONS](_state, { id, data }) { if (data.length) { const [chat] = _state.allConversations.filter(c => c.id === id); - chat.messages.unshift(...data); + if (chat) { + // Duplicate check: only add messages that don't already exist + const existingIds = new Set(chat.messages.map(m => m.id)); + const newMessages = data.filter(m => !existingIds.has(m.id)); + if (newMessages.length) { + chat.messages.unshift(...newMessages); + } + } } }, + [types.SET_ALL_ATTACHMENTS](_state, { id, data }) { _state.attachments[id] = [...data]; }, diff --git a/app/javascript/dashboard/store/modules/specs/conversations/mutations.spec.js b/app/javascript/dashboard/store/modules/specs/conversations/mutations.spec.js index b8660b20d..d8573dc84 100644 --- a/app/javascript/dashboard/store/modules/specs/conversations/mutations.spec.js +++ b/app/javascript/dashboard/store/modules/specs/conversations/mutations.spec.js @@ -615,6 +615,29 @@ describe('#mutations', () => { mutations[types.SET_PREVIOUS_CONVERSATIONS](state, payload); expect(state.allConversations[0].messages).toEqual([{ id: 'msg2' }]); }); + + it('should filter out duplicate messages', () => { + const state = { + allConversations: [{ id: 1, messages: [{ id: 'msg2' }] }], + }; + const payload = { id: 1, data: [{ id: 'msg1' }, { id: 'msg2' }] }; + + mutations[types.SET_PREVIOUS_CONVERSATIONS](state, payload); + expect(state.allConversations[0].messages).toEqual([ + { id: 'msg1' }, + { id: 'msg2' }, + ]); + }); + + it('should do nothing if conversation is not found', () => { + const state = { + allConversations: [{ id: 2, messages: [{ id: 'msg2' }] }], + }; + const payload = { id: 1, data: [{ id: 'msg1' }] }; + + mutations[types.SET_PREVIOUS_CONVERSATIONS](state, payload); + expect(state.allConversations[0].messages).toEqual([{ id: 'msg2' }]); + }); }); describe('#SET_MISSING_MESSAGES', () => {