Skip to content

Commit b86804e

Browse files
authored
Merge pull request #85 from Cognigy/bug/87720-teaser-message-with-persisted-history
Fix persistent history when used with teaser message
2 parents 45c6f7f + bdc8bb9 commit b86804e

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/webchat/store/reducer.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
import { StoreState } from "./store";
1717
import xAppOverlay from "./xapp-overlay/slice";
1818
import queueUpdates from "./queue-updates/slice";
19-
import { IStreamingMessage } from "../../common/interfaces/message";
2019

2120
const rootReducer = (state, action) => {
2221
const combinedReducer = combineReducers({
@@ -53,18 +52,19 @@ export type SetPrevStateAction = ReturnType<typeof setPrevState>;
5352

5453
export const reducer = (state = rootReducer(undefined, { type: "" }), action) => {
5554
switch (action.type) {
55+
// This is actually "Restore persisted history"
5656
case "RESET_STATE": {
57+
// To avoid duplicate messages in chat history during re-connection, we only restore messages and prepend them if the current message history is empty
58+
const isEmptyHistory =
59+
state.messages.messageHistory.length === 0 ||
60+
(state.messages.messageHistory.length === 1 &&
61+
state.messages.messageHistory[0].source === "engagement");
62+
const messages = isEmptyHistory ? action.state.messages : [];
5763
return rootReducer(
5864
{
5965
...state,
6066
messages: {
61-
messageHistory: [
62-
// To avoid duplicate messages in chat history during re-connection, we only restore messages and prepend them if the current message history is empty
63-
...(state.messages.messageHistory.length === 0
64-
? action.state.messages
65-
: []),
66-
...state.messages.messageHistory,
67-
],
67+
messageHistory: [...messages, ...state.messages.messageHistory],
6868
visibleOutputMessages: state.messages.visibleOutputMessages,
6969
},
7070
rating: {

0 commit comments

Comments
 (0)