-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreducer.ts
106 lines (100 loc) · 3.22 KB
/
reducer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { combineReducers } from "redux";
import { options } from "./options/options-reducer";
import { createMessageReducer } from "./messages/message-reducer";
import { ui } from "./ui/ui-reducer";
import { config } from "./config/config-reducer";
import { connection } from "./connection/connection-reducer";
import { unseenMessages } from "./unseen-messages/unseen-message-reducer";
import { autoInject } from "./autoinject/autoinject-reducer";
import { rating } from "./rating/rating-reducer";
import { inputCollation } from "./input-collation/input-collation-reducer";
import { input } from "./input/input-reducer";
import {
PrevConversationsState,
prevConversations,
} from "./previous-conversations/previous-conversations-reducer";
import { StoreState } from "./store";
import xAppOverlay from "./xapp-overlay/slice";
import queueUpdates from "./queue-updates/slice";
const rootReducer = (state, action) => {
const combinedReducer = combineReducers({
messages: createMessageReducer(() => state),
unseenMessages,
options,
config,
autoInject,
rating,
ui,
connection,
inputCollation,
input,
prevConversations,
xAppOverlay,
queueUpdates,
});
return combinedReducer(state, action);
};
const RESET_STATE = "RESET_STATE";
export const resetState = (state?: StoreState) => ({
type: RESET_STATE as "RESET_STATE",
state,
});
export type ResetStateAction = ReturnType<typeof resetState>;
const SET_PREV_STATE = "SET_PREV_STATE";
export const setPrevState = (state: PrevConversationsState[string]) => ({
type: SET_PREV_STATE as "SET_PREV_STATE",
state,
});
export type SetPrevStateAction = ReturnType<typeof setPrevState>;
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.messageHistory.length === 0 ||
(state.messages.messageHistory.length === 1 &&
state.messages.messageHistory[0].source === "engagement");
const messages = isEmptyHistory ? action.state.messages : [];
return rootReducer(
{
...state,
messages: {
messageHistory: [...messages, ...state.messages.messageHistory],
visibleOutputMessages: state.messages.visibleOutputMessages,
},
rating: {
...state.rating,
hasGivenRating: action.state.rating.hasGivenRating,
},
},
{ type: "" },
);
}
case "SET_PREV_STATE": {
const { showRatingScreen, ...rating } = action.state.rating;
const visibleOutputMessages = [];
const messages = action.state.messages.map(message => {
if (message.animationState) {
message.animationState = "done";
}
if ((message.source === "bot" || message.source === "engagement") && message.id) {
visibleOutputMessages.push(message.id as string);
}
return message;
});
return rootReducer(
{
...state,
messages: {
messageHistory: [...messages],
visibleOutputMessages,
},
rating: { showRatingScreen: false, ...rating },
},
{ type: "" },
);
}
}
return rootReducer(state, action);
};