From 250c1ce4ae134411a3b09776e610781d91170842 Mon Sep 17 00:00:00 2001 From: Dmitrii Ostasevich Date: Thu, 27 Mar 2025 14:10:46 +0100 Subject: [PATCH] fix persistent history when used with teaser message --- src/webchat/store/reducer.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/webchat/store/reducer.ts b/src/webchat/store/reducer.ts index d6fc9c4d..97d7588a 100644 --- a/src/webchat/store/reducer.ts +++ b/src/webchat/store/reducer.ts @@ -52,15 +52,17 @@ export type SetPrevStateAction = ReturnType; export const reducer = (state = rootReducer(undefined, { type: "" }), action) => { switch (action.type) { + // This is actually "Restore persisted history" case "RESET_STATE": { + // To avoid duplicate messages in chat history during re-connection, we only restore messages and prepend them if the current message history is empty + const isEmptyHistory = + state.messages.length === 0 || + (state.messages.length === 1 && state.messages[0].source === "engagement"); + const messages = isEmptyHistory ? action.state.messages : []; return rootReducer( { ...state, - messages: [ - // To avoid duplicate messages in chat history during re-connection, we only restore messages and prepend them if the current message history is empty - ...(state.messages.length === 0 ? action.state.messages : []), - ...state.messages, - ], + messages: [...messages, ...state.messages], rating: { ...state.rating, hasGivenRating: action.state.rating.hasGivenRating,