This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathindex.js
80 lines (65 loc) · 1.71 KB
/
index.js
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
import { h, Component, createContext } from 'preact';
import Store from './Store';
const createToken = () => Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
const initialState = {
token: createToken(),
typing: [],
config: {
messages: {},
settings: {},
theme: {},
triggers: [],
departments: [],
resources: {},
},
messages: [],
user: null,
sound: {
src: '',
enabled: true,
play: false,
},
iframe: {
guest: {},
theme: {},
visible: true,
},
gdpr: {
accepted: false,
},
alerts: [],
visible: true,
minimized: true,
unread: null,
incomingCallAlert: null,
ongoingCall: null, // TODO: store call info like url, startTime, timeout, etc here
};
const dontPersist = ['messages', 'typing', 'loading', 'alerts', 'unread', 'noMoreMessages', 'modal', 'incomingCallAlert', 'ongoingCall'];
export const store = new Store(initialState, { dontPersist });
if (process.env.NODE_ENV === 'development') {
store.on('change', ([, , partialState]) => {
// eslint-disable-next-line no-console
console.log('%cstore.setState %c%o', 'color: blue', 'color: initial', partialState);
});
}
const StoreContext = createContext();
export class Provider extends Component {
static displayName = 'StoreProvider'
state = { ...store.state, dispatch: store.setState.bind(store) }
handleStoreChange = () => {
this.setState({ ...store.state });
}
componentDidMount() {
store.on('change', this.handleStoreChange);
}
componentWillUnmount() {
store.off('change', this.handleStoreChange);
}
render = ({ children }) => (
<StoreContext.Provider value={this.state}>
{children}
</StoreContext.Provider>
)
}
export const { Consumer } = StoreContext;
export default store;