-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppStateContext.tsx
337 lines (302 loc) · 11.4 KB
/
AppStateContext.tsx
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import React, { useCallback, createContext, useReducer, useContext, useState, useEffect } from "react"
import { generateUUID } from 'pubnub';
import PubNub, { SubscribeParameters } from "pubnub";
import keyConfiguration from "../src/config/pubnub-keys.json";
import { debug } from "console";
import Blank from './';
import DOMPurify from 'dompurify';
//This is where you define the chat app properties.
export const appData: AppState = {
presence: true, // Enable or disable presence. REQUIRED FOR SUPPORT CHAT DEMO
presenceLastUpdated: 0, // Last time that a presence event was used to update the activeUsers list. Used to prevent duplicate events from triggering multiple calls to hereNow.
presenceOccupancy: 0, // How many active users there are.
history: true, // Enable or disable history.
historyMax: 10, // How many messages to load from history (max 100).
maxMessagesInList: 200, // Max number of messages at most in the message list.
selfAvatar: "https://ui-avatars.com/api/?name=Support+Agent?size=100&rounded=true&uppercase=true&bold=true&background=edab63&color=000", //The URL for the avatar graphic file
selfName: "Support Agent", // Set the display name to be the same as the UUID. You can make this whatever you want.
messages: [{
message: "← Select a conversation from the user list to start chatting.",
senderName: "Support Dashboard Alert"
}], // Array of UserMessages. - In support chat we preload with a message to prompt the agent to start a conversation.
activeUsers: [], // Array of active users.
channel: "support", // The root chat channel. In this demo this channel is used for presence and as a wildcard prefix for the active channel.
activeChannel: "", // // In the support demo this is used to set the active channel that should be used for messages. The generated name from the client is used to create a support channel for that user.
pubnub: new PubNub({
publishKey: keyConfiguration.publishKey, // See config/pubnub-keys.json.
subscribeKey: keyConfiguration.subscribeKey, // See config/pubnub-keys.json.
uuid: "Support Agent" // Use the UUID for identification on PubNub.
}),
message: "",
}
//This is the default settings for your chat app.
export interface AppState {
presence: boolean,
presenceLastUpdated: number,
history: boolean,
historyMax: number,
maxMessagesInList: number,
message: string;
selfAvatar: string,
selfName: string,
messages: Array<string>,
activeUsers: Array<string>,
pubnub: PubNub,
channel: string
}
type Action =
{
type: "ADD_MESSAGE",
payload: string
}
|{
type: "ADD_HISTORY",
payload: Array<string>
}
| {
type: "REFRESH_ACTIVEUSERS",
payload: Array<string>
}
| {
type: "UPDATE_OCCUPANCY",
payload: string
}
| {
type: "SEND_MESSAGE",
payload: {
messageContent: string
}
}
| {
type: "CHANGE_CHANNEL",
payload: string
}
interface AppStateContextProps {
state: AppState,
}
export const AppStateContext = createContext<AppStateContextProps>(
{} as AppStateContextProps
)
export const asyncDispatchMiddleware = store => next => action => {
let syncActivityFinished = false;
let actionQueue = [];
function flushQueue() {
actionQueue.forEach(a => store.dispatch(a)); // flush queue
actionQueue = [];
}
function asyncDispatch(asyncAction) {
actionQueue = actionQueue.concat([asyncAction]);
if (syncActivityFinished) {
flushQueue();
}
}
const actionWithAsyncDispatch =
Object.assign({}, action, { asyncDispatch });
next(actionWithAsyncDispatch);
syncActivityFinished = true;
flushQueue();
};
//The functions below are accessible through passing parameters to a dispatch function always accessible in our components.
export const appStateReducer = (state: AppState, action: Action): AppState => {
switch (action.type) {
//ADD_MESSAGE adds an incoming message to our internal MessageList buffer.
case "ADD_MESSAGE": {
//If the messagelist is over our cap we discard the oldest message in the list.
if (state.messages.length > state.maxMessagesInList ){
state.messages.shift();
}
const addMessage: AppState = {
...state,
messages: [
...state.messages as Array<string>,
{
...action.payload as Array<string>
}
]
};
return addMessage;
}
//ADD_HISTORY prepends array of messages to our internal MessageList buffer.
case "ADD_HISTORY": {
const historyMerged: AppState = {
...state,
messages: [
...action.payload as Array<string>,
...state.messages as Array<string>
]
};
//If the messagelist is over our cap we discard the oldest messages in the list.
if (state.messages.length > state.maxMessagesInList) {
state.messages.slice(state.messages.length-state.maxMessagesInList, state.messages.length);
}
return historyMerged;
}
//REFRESH_ACTIVEUSERS replaces array of users in our internal activeUsers buffer.
case "REFRESH_ACTIVEUSERS": {
const activeUsersList: AppState = {
...state,
activeUsers: [
...action.payload as Array<string>
]
};
return activeUsersList;
}
//UPDATE_OCCUPANCY updates the current count of users
case "UPDATE_OCCUPANCY": {
const occupantsUpdate: AppState = {
...state,
presenceOccupancy: action.payload as string
};
return occupantsUpdate;
}
// Publishes a message to chat channel.
case "SEND_MESSAGE": {
if (state.activeChannel == "") {
alert("Select a conversation from the user list first to send messages.");
} else {
state.pubnub.publish({
channel: state.activeChannel,
message: {
"message": DOMPurify.sanitize(action.payload as string) as string,
"senderName": state.selfName as string,
},
});
}
return { ...state }
}
// Unsubscribes from activeChannel and subscribes to new channel.
case "CHANGE_CHANNEL": {
if (state.activeChannel != "support."+action.payload) {
var historyMessages: Array<string> = [];
state.pubnub.history(
{
channel: state.channel+"."+action.payload,
count: state.historyMax // Limit of 100 messages.
},
(status, response) => {
if (typeof response.messages !== "undefined" && response.messages.length > 0) {
for (var i = 0; i <= response.messages.length; i++) {
if (typeof response.messages[i] !== "undefined") {
historyMessages.push(response.messages[i].entry);
}
}
} else {
historyMessages.push({
message: "Send a message to "+action.payload+" to start the conversation.",
senderName: "Support Dashboard Alert",
userAvatar: "https://ui-avatars.com/api/?name=Support+Dashboard?size=100&rounded=true&uppercase=true&bold=true&background=FB0106&color=FFF"
});
}
}
);
const changeChannelState: AppState = {
...state,
messages: historyMessages,
activeChannel: state.channel+"."+action.payload
};
return changeChannelState;
}
return { ...state }
}
default: {
return state
}
}
}
export const AppStateProvider = ({ children }: React.PropsWithChildren<{}>) => {
const [state, dispatch] = useReducer(appStateReducer, appData)
useEffect(() => {
var newActiveUsers = state.activeUsers;
try {
//This where PubNub receives messages subscribed by the channel.
state.pubnub.addListener({
message: (messageEvent) => {
//console.log(`RECEIVING MESSAGE ${messageEvent.message.key}`);
messageEvent.message.message = DOMPurify.sanitize(messageEvent.message.message as string) as string;
if (messageEvent.channel == state.activeChannel) { // Only add messages sent to activeChannel
dispatch({
type: "ADD_MESSAGE",
payload: messageEvent.message
});
}
},
presence: function(p) {
if (p.action == "join") {
if ((!state.activeUsers.includes(p.uuid)) ) { // Only add user if they are missing from the list.
if (p.uuid !== state.selfName) { // Don't include support agent on the list.
newActiveUsers.push(p.uuid);
}
newActiveUsers.sort();
dispatch({
type: "REFRESH_ACTIVEUSERS",
payload: newActiveUsers
});
dispatch({
type: "UPDATE_OCCUPANCY",
payload: newActiveUsers.length
});
// Add to current count
}
}
if ((p.action == "timeout") || (p.action == "leave")) {
var index = newActiveUsers.indexOf(p.uuid)
if (index !== -1) {
newActiveUsers.splice(index, 1);
dispatch({
type: "REFRESH_ACTIVEUSERS",
payload: newActiveUsers
});
dispatch({
type: "UPDATE_OCCUPANCY",
payload: newActiveUsers.length
});
}
}
}
});
if (state.presence) {
state.pubnub.hereNow(
{
channels: [state.channel],
includeUUIDs: true // In this demo we're using the uuid as the user's name. You could also use presence state to provide a username and more. In this app all we need is the UUID of online users.
},
(status, response) => {
if (response.channels[state.channel].occupancy > 0) {
for (var i = 0; i < response.channels[state.channel].occupancy; i++) {
if ((!state.activeUsers.includes(response.channels[state.channel].occupants[i].uuid)) && (response.channels[state.channel].occupants[i].uuid !== state.selfName)) {
newActiveUsers.push(response.channels[state.channel].occupants[i].uuid);
}
}
newActiveUsers.sort();
dispatch({
type: "REFRESH_ACTIVEUSERS",
payload: newActiveUsers
});
dispatch({
type: "UPDATE_OCCUPANCY",
payload: newActiveUsers.length
});
}
}
);
}
// Subscribe on the default channel.
state.pubnub.subscribe(
{
channels: [state.channel, state.channel+".*"], // Subscribe to global channel for presence events from chat clients and to the wildcard for all sub channels for chat messages.
withPresence: state.presence,
}
);
} catch (e) {
console.log(`Subscribe error ${e.message}`);
}
}, [state.activeChannel]);
return (
<AppStateContext.Provider value={{ state, dispatch }}>
{children}
</AppStateContext.Provider>
);
}
export const useAppState = () => {
return useContext(AppStateContext)
}