Skip to content

Commit 250c1ce

Browse files
committed
fix persistent history when used with teaser message
1 parent 7c5511c commit 250c1ce

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/webchat/store/reducer.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,17 @@ export type SetPrevStateAction = ReturnType<typeof setPrevState>;
5252

5353
export const reducer = (state = rootReducer(undefined, { type: "" }), action) => {
5454
switch (action.type) {
55+
// This is actually "Restore persisted history"
5556
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.length === 0 ||
60+
(state.messages.length === 1 && state.messages[0].source === "engagement");
61+
const messages = isEmptyHistory ? action.state.messages : [];
5662
return rootReducer(
5763
{
5864
...state,
59-
messages: [
60-
// To avoid duplicate messages in chat history during re-connection, we only restore messages and prepend them if the current message history is empty
61-
...(state.messages.length === 0 ? action.state.messages : []),
62-
...state.messages,
63-
],
65+
messages: [...messages, ...state.messages],
6466
rating: {
6567
...state.rating,
6668
hasGivenRating: action.state.rating.hasGivenRating,

0 commit comments

Comments
 (0)