@defra/flood-webchat 0.0.1-alpha.9 → 0.0.1-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +3 -3
  2. package/dist/client.js +1547 -0
  3. package/dist/client.js.map +1 -0
  4. package/dist/server.js +303 -0
  5. package/dist/server.js.map +1 -0
  6. package/docs/development-guide.md +1 -2
  7. package/jest.config.mjs +29 -0
  8. package/main.scss +7 -742
  9. package/package.json +23 -13
  10. package/src/client/components/availability/availability.jsx +98 -0
  11. package/src/client/components/availability/availability.scss +89 -0
  12. package/src/client/components/chat/chat.jsx +143 -0
  13. package/src/client/components/chat/chat.scss +134 -0
  14. package/src/client/components/message/message.jsx +20 -0
  15. package/src/client/components/panel/panel-footer.jsx +9 -0
  16. package/src/client/components/panel/panel-header.jsx +39 -0
  17. package/src/client/components/panel/panel.jsx +109 -0
  18. package/src/client/components/panel/panel.scss +72 -0
  19. package/src/client/components/screens/end-chat.jsx +17 -0
  20. package/src/client/components/screens/pre-chat.jsx +55 -0
  21. package/src/client/components/screens/request-chat.jsx +170 -0
  22. package/src/client/components/screens/unavailable.jsx +23 -0
  23. package/src/client/hooks/useFocusedElements.js +71 -0
  24. package/src/client/hooks/useTextareaAutosize.js +13 -0
  25. package/src/client/index.jsx +30 -0
  26. package/src/client/lib/check-availability.js +8 -0
  27. package/src/client/lib/classnames.js +1 -0
  28. package/src/client/lib/transform-messages.js +14 -0
  29. package/src/client/store/AppProvider.jsx +114 -0
  30. package/src/client/store/actions-map.js +97 -0
  31. package/src/client/store/reducer.js +29 -0
  32. package/src/client/store/useApp.js +5 -0
  33. package/src/client/store/useChatSdk.js +37 -0
  34. package/src/server/index.js +25 -11
  35. package/src/server/lib/client.js +88 -0
  36. package/src/server/lib/utils.js +23 -0
  37. package/webpack.config.mjs +32 -0
  38. package/assets/audio/notification.mp3 +0 -0
  39. package/babel.config.cjs +0 -3
  40. package/dist/templates.js +0 -554
  41. package/src/client/index.js +0 -14
  42. package/src/client/lib/availability.js +0 -75
  43. package/src/client/lib/config.js +0 -17
  44. package/src/client/lib/keyboard.js +0 -149
  45. package/src/client/lib/notification.js +0 -59
  46. package/src/client/lib/nunjucks.js +0 -3
  47. package/src/client/lib/panel.js +0 -293
  48. package/src/client/lib/provider.js +0 -11
  49. package/src/client/lib/skiplink.js +0 -27
  50. package/src/client/lib/state.js +0 -82
  51. package/src/client/lib/transcript.js +0 -34
  52. package/src/client/lib/utils.js +0 -190
  53. package/src/client/lib/webchat.js +0 -1075
  54. package/src/server/client.js +0 -72
  55. package/src/server/utils.js +0 -19
  56. package/src/style/_mixins.scss +0 -13
package/dist/client.js ADDED
@@ -0,0 +1,1547 @@
1
+ /******/ (() => { // webpackBootstrap
2
+ /******/ "use strict";
3
+ /******/ var __webpack_modules__ = ({
4
+
5
+ /***/ "./src/client/components/availability/availability.jsx":
6
+ /*!*************************************************************!*\
7
+ !*** ./src/client/components/availability/availability.jsx ***!
8
+ \*************************************************************/
9
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
10
+
11
+ __webpack_require__.r(__webpack_exports__);
12
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
13
+ /* harmony export */ Availability: () => (/* binding */ Availability)
14
+ /* harmony export */ });
15
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
16
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
17
+ /* harmony import */ var _lib_classnames__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../lib/classnames */ "./src/client/lib/classnames.js");
18
+ /* harmony import */ var _panel_panel_jsx__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../panel/panel.jsx */ "./src/client/components/panel/panel.jsx");
19
+ /* harmony import */ var _store_useApp__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../../store/useApp */ "./src/client/store/useApp.js");
20
+
21
+
22
+
23
+
24
+ function Availability() {
25
+ const {
26
+ availability,
27
+ messages,
28
+ isChatOpen,
29
+ setChatVisibility
30
+ } = (0,_store_useApp__WEBPACK_IMPORTED_MODULE_3__.useApp)();
31
+ const [isFixed, setIsFixed] = (0,react__WEBPACK_IMPORTED_MODULE_0__.useState)(false);
32
+ const buttonRef = (0,react__WEBPACK_IMPORTED_MODULE_0__.useRef)();
33
+ const onClick = () => {
34
+ setChatVisibility(!isChatOpen);
35
+ };
36
+ const onKeyDown = event => {
37
+ if (event.key === ' ') {
38
+ event.preventDefault();
39
+ }
40
+ };
41
+ const onKeyUp = event => {
42
+ if (event.key === ' ') {
43
+ setChatVisibility(!isChatOpen);
44
+ }
45
+ };
46
+ const intersectionCallback = entries => {
47
+ const [entry] = entries;
48
+ const isBelowFold = !entry.isIntersecting && entry.boundingClientRect.top > 0;
49
+ setIsFixed(!isChatOpen && isBelowFold);
50
+ };
51
+ (0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(() => {
52
+ const observer = new window.IntersectionObserver(intersectionCallback, {
53
+ rootMargin: '35px'
54
+ });
55
+ const parentElement = buttonRef.current?.parentElement;
56
+ if (parentElement) {
57
+ observer.observe(parentElement);
58
+ }
59
+ return () => {
60
+ if (parentElement) {
61
+ observer.unobserve(parentElement);
62
+ }
63
+ };
64
+ }, [buttonRef, isChatOpen]);
65
+ (0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(() => {
66
+ document.documentElement.classList.toggle('wc-scroll-padding', isFixed);
67
+ document.body.classList.toggle('wc-scroll-padding', isFixed);
68
+ }, [isFixed]);
69
+ let TextComponent = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement((react__WEBPACK_IMPORTED_MODULE_0___default().Fragment), null, "Show Chat");
70
+ if (!messages.length) {
71
+ TextComponent = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement((react__WEBPACK_IMPORTED_MODULE_0___default().Fragment), null, "Start Chat");
72
+ }
73
+ switch (availability) {
74
+ case 'AVAILABLE':
75
+ return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement((react__WEBPACK_IMPORTED_MODULE_0___default().Fragment), null, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
76
+ className: (0,_lib_classnames__WEBPACK_IMPORTED_MODULE_1__.classnames)('wc-availability', isFixed && 'wc-availability--fixed', isChatOpen && 'wc-open'),
77
+ ref: buttonRef
78
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
79
+ className: "wc-availability__inner"
80
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("a", {
81
+ className: "wc-availability__link",
82
+ href: "#webchat",
83
+ draggable: "false",
84
+ onClick: onClick,
85
+ onKeyUp: onKeyUp,
86
+ onKeyDown: onKeyDown
87
+ }, TextComponent))), isChatOpen && /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_panel_panel_jsx__WEBPACK_IMPORTED_MODULE_2__.Panel, null));
88
+ case 'EXISTING':
89
+ case 'UNAVAILABLE':
90
+ return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
91
+ className: "govuk-body"
92
+ }, "When it is available, a 'start chat' link will appear.");
93
+ default:
94
+ return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
95
+ className: "govuk-body"
96
+ }, "Checking availability");
97
+ }
98
+ }
99
+
100
+ /***/ }),
101
+
102
+ /***/ "./src/client/components/chat/chat.jsx":
103
+ /*!*********************************************!*\
104
+ !*** ./src/client/components/chat/chat.jsx ***!
105
+ \*********************************************/
106
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
107
+
108
+ __webpack_require__.r(__webpack_exports__);
109
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
110
+ /* harmony export */ Chat: () => (/* binding */ Chat)
111
+ /* harmony export */ });
112
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
113
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
114
+ /* harmony import */ var sanitize_html__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! sanitize-html */ "sanitize-html");
115
+ /* harmony import */ var sanitize_html__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(sanitize_html__WEBPACK_IMPORTED_MODULE_1__);
116
+ /* harmony import */ var _panel_panel_header_jsx__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../panel/panel-header.jsx */ "./src/client/components/panel/panel-header.jsx");
117
+ /* harmony import */ var _panel_panel_footer_jsx__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../panel/panel-footer.jsx */ "./src/client/components/panel/panel-footer.jsx");
118
+ /* harmony import */ var _message_message_jsx__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ../message/message.jsx */ "./src/client/components/message/message.jsx");
119
+ /* harmony import */ var _store_useApp_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ../../store/useApp.js */ "./src/client/store/useApp.js");
120
+ /* harmony import */ var _hooks_useTextareaAutosize_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ../../hooks/useTextareaAutosize.js */ "./src/client/hooks/useTextareaAutosize.js");
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+ function Chat({
129
+ onEndChat
130
+ }) {
131
+ const {
132
+ availability,
133
+ thread,
134
+ messages,
135
+ agent,
136
+ agentStatus,
137
+ isAgentTyping
138
+ } = (0,_store_useApp_js__WEBPACK_IMPORTED_MODULE_5__.useApp)();
139
+ const [userMessage, setUserMessage] = (0,react__WEBPACK_IMPORTED_MODULE_0__.useState)('');
140
+ const messageRef = (0,react__WEBPACK_IMPORTED_MODULE_0__.useRef)();
141
+ (0,_hooks_useTextareaAutosize_js__WEBPACK_IMPORTED_MODULE_6__.useTextareaAutosize)(messageRef.current, userMessage);
142
+ (0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(() => {
143
+ if (messages.length !== 0) {
144
+ const chatBody = document.querySelector('.wc-body');
145
+ chatBody.scrollTop = chatBody.scrollHeight;
146
+ }
147
+ }, [messages, isAgentTyping]);
148
+ (0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(() => {
149
+ const label = document.querySelector('.wc-form__label');
150
+ if (userMessage.length === 0) {
151
+ label.classList.remove('govuk-visually-hidden');
152
+ } else {
153
+ label.classList.add('govuk-visually-hidden');
154
+ }
155
+ }, [userMessage]);
156
+ const onChange = e => {
157
+ setUserMessage(e.target?.value);
158
+ };
159
+ const agentName = agent?.nickname || agent?.firstName;
160
+ let connectionHeadlineText = 'Connecting to Floodline';
161
+ if (agentStatus === 'closed') {
162
+ connectionHeadlineText = agentName ? `${agentName} ended the session` : 'Session ended by advisor';
163
+ } else if (agentStatus) {
164
+ if (agentName) {
165
+ connectionHeadlineText = `You are speaking with ${agentName}`;
166
+ } else {
167
+ connectionHeadlineText = 'No advisers currently available';
168
+ }
169
+ }
170
+ if (availability === 'UNAVAILABLE') {
171
+ connectionHeadlineText = 'Webchat is not currently available';
172
+ }
173
+ const sendMessage = e => {
174
+ e.preventDefault();
175
+ if (messageRef.current.value.length === 0) {
176
+ return;
177
+ }
178
+ try {
179
+ thread.sendTextMessage(sanitize_html__WEBPACK_IMPORTED_MODULE_1___default()(messageRef.current.value.trim()));
180
+ } catch (err) {
181
+ console.log('[Chat Error] sendMessage', err);
182
+ }
183
+ setUserMessage('');
184
+ };
185
+ return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement((react__WEBPACK_IMPORTED_MODULE_0___default().Fragment), null, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_panel_panel_header_jsx__WEBPACK_IMPORTED_MODULE_2__.PanelHeader, null), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
186
+ className: "wc-status"
187
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
188
+ className: "wc-status__availability govuk-body-s"
189
+ }, connectionHeadlineText), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("a", {
190
+ className: "wc-status__link govuk-!-font-size-16",
191
+ href: "#",
192
+ "data-module": "govuk-button",
193
+ onClick: onEndChat
194
+ }, "End chat")), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
195
+ className: "wc-body"
196
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
197
+ className: "wc-chat__body"
198
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("ul", {
199
+ className: "wc-chat__messages"
200
+ }, messages.length ? messages.map((msg, index) => /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_message_message_jsx__WEBPACK_IMPORTED_MODULE_4__.Message, {
201
+ key: msg.id,
202
+ message: msg,
203
+ previousMessage: messages[index - 1]
204
+ })) : null, isAgentTyping ? /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("li", {
205
+ className: "wc-chat__message govuk-body outbound"
206
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
207
+ className: "wc-chat__from govuk-!-font-size-14"
208
+ }, agentName, " is typing"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
209
+ className: "wc-chat__text outbound"
210
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("svg", {
211
+ width: "28",
212
+ height: "16",
213
+ x: "0px",
214
+ y: "0px",
215
+ viewBox: "0 0 28 16"
216
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("circle", {
217
+ stroke: "none",
218
+ cx: "3",
219
+ cy: "8",
220
+ r: "3",
221
+ fill: "currentColor"
222
+ }), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("circle", {
223
+ stroke: "none",
224
+ cx: "14",
225
+ cy: "8",
226
+ r: "3",
227
+ fill: "currentColor"
228
+ }), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("circle", {
229
+ stroke: "none",
230
+ cx: "25",
231
+ cy: "8",
232
+ r: "3",
233
+ fill: "currentColor"
234
+ })))) : null))), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_panel_panel_footer_jsx__WEBPACK_IMPORTED_MODULE_3__.PanelFooter, null, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
235
+ className: "wc-footer__input"
236
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("form", {
237
+ className: "wc-form",
238
+ noValidate: true
239
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("label", {
240
+ className: "govuk-label wc-form__label govuk-!-font-size-16",
241
+ htmlFor: "wc-form-textarea"
242
+ }, "Your message", /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("span", {
243
+ className: "govuk-visually-hidden"
244
+ }, " (enter key submits)")), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("textarea", {
245
+ ref: messageRef,
246
+ rows: "1",
247
+ "aria-required": "true",
248
+ className: "wc-form__textarea govuk-textarea govuk-!-font-size-16",
249
+ id: "wc-form-textarea",
250
+ name: "message",
251
+ onChange: onChange,
252
+ value: userMessage
253
+ }), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("input", {
254
+ type: "submit",
255
+ className: "wc-form__button govuk-button govuk-!-font-size-16",
256
+ value: "Send",
257
+ "data-prevent-double-click": "true",
258
+ onClick: sendMessage
259
+ }))), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
260
+ className: "wc-footer__settings"
261
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("a", {
262
+ href: "#",
263
+ className: "wc-footer__settings-link",
264
+ "data-module": "govuk-button"
265
+ }, "Settings"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("a", {
266
+ href: "#",
267
+ className: "wc-footer__settings-link",
268
+ "data-module": "govuk-button",
269
+ download: "transcript.txt"
270
+ }, "Save chat"))));
271
+ }
272
+
273
+ /***/ }),
274
+
275
+ /***/ "./src/client/components/message/message.jsx":
276
+ /*!***************************************************!*\
277
+ !*** ./src/client/components/message/message.jsx ***!
278
+ \***************************************************/
279
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
280
+
281
+ __webpack_require__.r(__webpack_exports__);
282
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
283
+ /* harmony export */ Message: () => (/* binding */ Message)
284
+ /* harmony export */ });
285
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
286
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
287
+ /* harmony import */ var _lib_classnames__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../lib/classnames */ "./src/client/lib/classnames.js");
288
+
289
+
290
+ function Message({
291
+ message,
292
+ previousMessage
293
+ }) {
294
+ const outbound = message.direction === 'outbound';
295
+ const messageOwnerSameAsPrevious = message?.direction === previousMessage?.direction;
296
+ return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("li", {
297
+ className: (0,_lib_classnames__WEBPACK_IMPORTED_MODULE_1__.classnames)('wc-chat__message govuk-body', outbound ? 'outbound' : 'inbound')
298
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
299
+ className: "wc-chat__from govuk-!-font-size-14"
300
+ }, messageOwnerSameAsPrevious ? null : /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("span", null, outbound ? message.assignee : 'You'), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("span", {
301
+ className: "govuk-visually-hidden"
302
+ }, ":")), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
303
+ className: (0,_lib_classnames__WEBPACK_IMPORTED_MODULE_1__.classnames)('wc-chat__text', 'govuk-body-s', outbound ? 'outbound' : 'inbound')
304
+ }, message.text));
305
+ }
306
+
307
+ /***/ }),
308
+
309
+ /***/ "./src/client/components/panel/panel-footer.jsx":
310
+ /*!******************************************************!*\
311
+ !*** ./src/client/components/panel/panel-footer.jsx ***!
312
+ \******************************************************/
313
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
314
+
315
+ __webpack_require__.r(__webpack_exports__);
316
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
317
+ /* harmony export */ PanelFooter: () => (/* binding */ PanelFooter)
318
+ /* harmony export */ });
319
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
320
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
321
+
322
+ function PanelFooter({
323
+ children
324
+ }) {
325
+ return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
326
+ className: "wc-footer"
327
+ }, children);
328
+ }
329
+
330
+ /***/ }),
331
+
332
+ /***/ "./src/client/components/panel/panel-header.jsx":
333
+ /*!******************************************************!*\
334
+ !*** ./src/client/components/panel/panel-header.jsx ***!
335
+ \******************************************************/
336
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
337
+
338
+ __webpack_require__.r(__webpack_exports__);
339
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
340
+ /* harmony export */ PanelHeader: () => (/* binding */ PanelHeader)
341
+ /* harmony export */ });
342
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
343
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
344
+ /* harmony import */ var _store_useApp_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../store/useApp.js */ "./src/client/store/useApp.js");
345
+
346
+
347
+ function PanelHeader() {
348
+ const {
349
+ thread,
350
+ setChatVisibility
351
+ } = (0,_store_useApp_js__WEBPACK_IMPORTED_MODULE_1__.useApp)();
352
+ const onClose = e => {
353
+ e.preventDefault();
354
+ setChatVisibility(false);
355
+ };
356
+ let ButtonComponent = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("button", {
357
+ className: "wc-header__close",
358
+ "aria-label": "Close the webchat",
359
+ onClick: onClose
360
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("svg", {
361
+ "aria-hidden": "true",
362
+ focusable: "false",
363
+ width: "20",
364
+ height: "20",
365
+ viewBox: "0 0 20 20"
366
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("path", {
367
+ d: "M10,8.6L15.6,3L17,4.4L11.4,10L17,15.6L15.6,17L10,11.4L4.4,17L3,15.6L8.6,10L3,4.4L4.4,3L10,8.6Z",
368
+ fill: "currentColor"
369
+ })));
370
+ if (thread) {
371
+ ButtonComponent = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("button", {
372
+ className: "wc-header__hide",
373
+ "aria-label": "Minimise the webchat",
374
+ onClick: onClose
375
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("svg", {
376
+ "aria-hidden": "true",
377
+ focusable: "false",
378
+ width: "20",
379
+ height: "20",
380
+ viewBox: "0 0 20 20"
381
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("path", {
382
+ d: "M10 14.4l-7-7L4.4 6l5.6 5.6L15.6 6 17 7.4l-7 7z",
383
+ fill: "currentColor"
384
+ })));
385
+ }
386
+ return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
387
+ className: "wc-header"
388
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("h2", {
389
+ id: "wc-header-title",
390
+ className: "wc-header__title govuk-heading-s"
391
+ }, "Floodline Webchat"), ButtonComponent);
392
+ }
393
+
394
+ /***/ }),
395
+
396
+ /***/ "./src/client/components/panel/panel.jsx":
397
+ /*!***********************************************!*\
398
+ !*** ./src/client/components/panel/panel.jsx ***!
399
+ \***********************************************/
400
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
401
+
402
+ __webpack_require__.r(__webpack_exports__);
403
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
404
+ /* harmony export */ Panel: () => (/* binding */ Panel)
405
+ /* harmony export */ });
406
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
407
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
408
+ /* harmony import */ var react_dom__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! react-dom */ "react-dom");
409
+ /* harmony import */ var react_dom__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react_dom__WEBPACK_IMPORTED_MODULE_1__);
410
+ /* harmony import */ var _screens_pre_chat_jsx__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../screens/pre-chat.jsx */ "./src/client/components/screens/pre-chat.jsx");
411
+ /* harmony import */ var _screens_request_chat_jsx__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../screens/request-chat.jsx */ "./src/client/components/screens/request-chat.jsx");
412
+ /* harmony import */ var _chat_chat_jsx__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ../chat/chat.jsx */ "./src/client/components/chat/chat.jsx");
413
+ /* harmony import */ var _screens_unavailable_jsx__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ../screens/unavailable.jsx */ "./src/client/components/screens/unavailable.jsx");
414
+ /* harmony import */ var _screens_end_chat_jsx__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ../screens/end-chat.jsx */ "./src/client/components/screens/end-chat.jsx");
415
+ /* harmony import */ var _store_useApp_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ../../store/useApp.js */ "./src/client/store/useApp.js");
416
+ /* harmony import */ var _store_useChatSdk_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ../../store/useChatSdk.js */ "./src/client/store/useChatSdk.js");
417
+ /* harmony import */ var _hooks_useFocusedElements_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ../../hooks/useFocusedElements.js */ "./src/client/hooks/useFocusedElements.js");
418
+
419
+
420
+
421
+
422
+
423
+
424
+
425
+
426
+
427
+
428
+ function Panel() {
429
+ const {
430
+ sdk,
431
+ availability,
432
+ thread,
433
+ threadId,
434
+ setThread,
435
+ setThreadId,
436
+ setChatVisibility,
437
+ setMessages
438
+ } = (0,_store_useApp_js__WEBPACK_IMPORTED_MODULE_7__.useApp)();
439
+ const {
440
+ fetchThread,
441
+ fetchMessages
442
+ } = (0,_store_useChatSdk_js__WEBPACK_IMPORTED_MODULE_8__.useChatSdk)(sdk);
443
+ const [screen, setScreen] = (0,react__WEBPACK_IMPORTED_MODULE_0__.useState)(threadId ? 2 : 0);
444
+ (0,_hooks_useFocusedElements_js__WEBPACK_IMPORTED_MODULE_9__.useFocusedElements)(screen);
445
+
446
+ /**
447
+ * Initializes the eventListener for pressing the escape key
448
+ */
449
+ (0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(() => {
450
+ const escapeKeyEvent = document.addEventListener('keydown', onEscapeKey);
451
+ return () => {
452
+ document.removeEventListener('keydown', escapeKeyEvent);
453
+ };
454
+ }, []);
455
+
456
+ /**
457
+ * Recovers the thread if there is a threadId but no thread loaded in to state
458
+ */
459
+ (0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(() => {
460
+ const recover = async () => {
461
+ try {
462
+ const fetchedThread = await fetchThread(threadId);
463
+ setThread(fetchedThread);
464
+ const fetchedMessages = await fetchMessages(fetchedThread, threadId);
465
+ setMessages(fetchedMessages);
466
+ } catch (err) {
467
+ console.log('[Chat Error] fetchThread', err);
468
+ setThreadId();
469
+ setThread();
470
+ setScreen(0);
471
+ }
472
+ };
473
+ if (threadId) {
474
+ thread ? setScreen(2) : recover();
475
+ }
476
+ }, [thread, threadId]);
477
+ const onEscapeKey = (0,react__WEBPACK_IMPORTED_MODULE_0__.useCallback)(e => {
478
+ if (e.key === 'Escape' || e.key === 'Esc') {
479
+ setChatVisibility(false);
480
+ }
481
+ }, []);
482
+ const handleScreenChange = newScreen => e => {
483
+ e.preventDefault();
484
+ setScreen(newScreen);
485
+ };
486
+ const onForward = handleScreenChange(screen + 1);
487
+ const onBack = handleScreenChange(screen - 1);
488
+ const onEndChat = handleScreenChange(3);
489
+ const onResume = handleScreenChange(2);
490
+ const onEndChatConfirm = e => {
491
+ e.preventDefault();
492
+ console.log('confirmed end chat');
493
+ };
494
+ let ScreenComponent;
495
+ switch (screen) {
496
+ case 0:
497
+ ScreenComponent = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_screens_pre_chat_jsx__WEBPACK_IMPORTED_MODULE_2__.PreChat, {
498
+ onForward: onForward
499
+ });
500
+ break;
501
+ case 1:
502
+ ScreenComponent = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_screens_request_chat_jsx__WEBPACK_IMPORTED_MODULE_3__.RequestChat, {
503
+ onBack: onBack
504
+ });
505
+ break;
506
+ case 2:
507
+ ScreenComponent = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_chat_chat_jsx__WEBPACK_IMPORTED_MODULE_4__.Chat, {
508
+ setScreen: setScreen,
509
+ onEndChat: onEndChat
510
+ });
511
+ break;
512
+ case 3:
513
+ ScreenComponent = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_screens_end_chat_jsx__WEBPACK_IMPORTED_MODULE_6__.EndChat, {
514
+ onResume: onResume,
515
+ onEndChatConfirm: onEndChatConfirm
516
+ });
517
+ break;
518
+ default:
519
+ ScreenComponent = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_screens_pre_chat_jsx__WEBPACK_IMPORTED_MODULE_2__.PreChat, {
520
+ onForward: onForward
521
+ });
522
+ }
523
+ if (availability === 'UNAVAILABLE') {
524
+ ScreenComponent = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_screens_unavailable_jsx__WEBPACK_IMPORTED_MODULE_5__.Unavailable, null);
525
+ }
526
+ const Component = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
527
+ id: "wc-panel",
528
+ className: "wc-panel",
529
+ role: "dialog",
530
+ tabIndex: "-1",
531
+ "aria-modal": "true",
532
+ "aria-labelledby": "wc-header-title"
533
+ }, ScreenComponent);
534
+ return /*#__PURE__*/(0,react_dom__WEBPACK_IMPORTED_MODULE_1__.createPortal)(Component, document.body);
535
+ }
536
+
537
+ /***/ }),
538
+
539
+ /***/ "./src/client/components/screens/end-chat.jsx":
540
+ /*!****************************************************!*\
541
+ !*** ./src/client/components/screens/end-chat.jsx ***!
542
+ \****************************************************/
543
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
544
+
545
+ __webpack_require__.r(__webpack_exports__);
546
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
547
+ /* harmony export */ EndChat: () => (/* binding */ EndChat)
548
+ /* harmony export */ });
549
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
550
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
551
+ /* harmony import */ var _panel_panel_header_jsx__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../panel/panel-header.jsx */ "./src/client/components/panel/panel-header.jsx");
552
+
553
+
554
+ function EndChat({
555
+ onResume,
556
+ onEndChatConfirm
557
+ }) {
558
+ return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement((react__WEBPACK_IMPORTED_MODULE_0___default().Fragment), null, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_panel_panel_header_jsx__WEBPACK_IMPORTED_MODULE_1__.PanelHeader, null), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
559
+ className: "wc-body"
560
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("h3", {
561
+ className: "govuk-heading-s",
562
+ "aria-live": "polite"
563
+ }, "Are you sure you want to end the chat?"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
564
+ className: "govuk-button-group"
565
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("a", {
566
+ href: "#",
567
+ className: "govuk-button govuk-!-font-size-16",
568
+ "data-module": "govuk-button",
569
+ onClick: onEndChatConfirm
570
+ }, "Yes, end chat"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("a", {
571
+ href: "#",
572
+ className: "govuk-link govuk-!-font-size-16",
573
+ onClick: onResume
574
+ }, "No, resume chat"))));
575
+ }
576
+
577
+ /***/ }),
578
+
579
+ /***/ "./src/client/components/screens/pre-chat.jsx":
580
+ /*!****************************************************!*\
581
+ !*** ./src/client/components/screens/pre-chat.jsx ***!
582
+ \****************************************************/
583
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
584
+
585
+ __webpack_require__.r(__webpack_exports__);
586
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
587
+ /* harmony export */ PreChat: () => (/* binding */ PreChat)
588
+ /* harmony export */ });
589
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
590
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
591
+ /* harmony import */ var _panel_panel_header_jsx__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../panel/panel-header.jsx */ "./src/client/components/panel/panel-header.jsx");
592
+
593
+
594
+ function PreChat({
595
+ onForward
596
+ }) {
597
+ return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement((react__WEBPACK_IMPORTED_MODULE_0___default().Fragment), null, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_panel_panel_header_jsx__WEBPACK_IMPORTED_MODULE_1__.PanelHeader, null), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
598
+ className: "wc-body"
599
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("h3", {
600
+ className: "govuk-heading-s",
601
+ "aria-live": "polite"
602
+ }, "What you can use webchat for"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
603
+ className: "govuk-body-s"
604
+ }, "Webchat lets you talk directly to a Floodline adviser."), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
605
+ className: "govuk-body-s"
606
+ }, "You can use webchat to get:"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("ul", {
607
+ className: "govuk-list govuk-list--bullet govuk-body-s"
608
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("li", null, "current flood warnings and alerts in your area"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("li", null, "information on the flood warning service"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("li", null, "advice on what to do before, during and after a flood")), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
609
+ className: "govuk-body-s"
610
+ }, 'There are other ways to ', /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("a", {
611
+ className: "govuk-link",
612
+ href: "https://www.gov.uk/sign-up-for-flood-warnings"
613
+ }, "sign up for flood warnings"), ' and ', /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("a", {
614
+ className: "govuk-link",
615
+ href: "https://www.fws.environment-agency.gov.uk/app/olr/login"
616
+ }, "manage your flood warnings account"), "."), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
617
+ className: "govuk-body-s"
618
+ }, "Do not use webchat to ", /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("a", {
619
+ className: "govuk-link",
620
+ href: "https://www.gov.uk/report-flood-cause"
621
+ }, "report a flood"), "."), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("button", {
622
+ className: "govuk-button govuk-!-font-size-16",
623
+ "data-module": "govuk-button",
624
+ onClick: onForward
625
+ }, "Continue"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("h3", {
626
+ className: "govuk-heading-s"
627
+ }, "Other flood information"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
628
+ className: "govuk-body-s"
629
+ }, "Call Floodline for all other flood and flood warning information."), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
630
+ className: "govuk-body-s"
631
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("strong", null, "Floodline helpline"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("br", null), "Telephone: 0345 988 1188", /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("br", null), "Textphone: 0345 602 6340", /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("br", null), "Open 24 hours a day, 7 days a week", /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("br", null), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("a", {
632
+ className: "govuk-link",
633
+ href: "https://gov.uk/call-charges"
634
+ }, "Find out more about call charges"))));
635
+ }
636
+
637
+ /***/ }),
638
+
639
+ /***/ "./src/client/components/screens/request-chat.jsx":
640
+ /*!********************************************************!*\
641
+ !*** ./src/client/components/screens/request-chat.jsx ***!
642
+ \********************************************************/
643
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
644
+
645
+ __webpack_require__.r(__webpack_exports__);
646
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
647
+ /* harmony export */ RequestChat: () => (/* binding */ RequestChat)
648
+ /* harmony export */ });
649
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
650
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
651
+ /* harmony import */ var uuid__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! uuid */ "uuid");
652
+ /* harmony import */ var uuid__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(uuid__WEBPACK_IMPORTED_MODULE_1__);
653
+ /* harmony import */ var _lib_classnames_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../lib/classnames.js */ "./src/client/lib/classnames.js");
654
+ /* harmony import */ var _panel_panel_header_jsx__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../panel/panel-header.jsx */ "./src/client/components/panel/panel-header.jsx");
655
+ /* harmony import */ var _store_useApp_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ../../store/useApp.js */ "./src/client/store/useApp.js");
656
+ /* harmony import */ var _store_useChatSdk_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ../../store/useChatSdk.js */ "./src/client/store/useChatSdk.js");
657
+
658
+
659
+
660
+
661
+
662
+
663
+ const QUESTION_MAX_LENGTH = 500;
664
+ function RequestChat({
665
+ onBack
666
+ }) {
667
+ const {
668
+ sdk,
669
+ setCustomerId,
670
+ setThreadId,
671
+ setThread
672
+ } = (0,_store_useApp_js__WEBPACK_IMPORTED_MODULE_4__.useApp)();
673
+ const {
674
+ fetchCustomerId,
675
+ fetchThread
676
+ } = (0,_store_useChatSdk_js__WEBPACK_IMPORTED_MODULE_5__.useChatSdk)(sdk);
677
+ const [errors, setErrors] = (0,react__WEBPACK_IMPORTED_MODULE_0__.useState)({});
678
+ const [questionLength, setQuestionLength] = (0,react__WEBPACK_IMPORTED_MODULE_0__.useState)(0);
679
+ const nameRef = (0,react__WEBPACK_IMPORTED_MODULE_0__.useRef)();
680
+ const questionRef = (0,react__WEBPACK_IMPORTED_MODULE_0__.useRef)();
681
+ (0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(() => {
682
+ if (Object.keys(errors).length) {
683
+ document.querySelector('.govuk-error-summary')?.focus();
684
+ }
685
+ }, [errors]);
686
+ const onQuestionChange = e => {
687
+ setQuestionLength(e.target.value.length);
688
+ };
689
+ const onRequestChat = async e => {
690
+ e.preventDefault();
691
+ const errs = {};
692
+ if (nameRef.current.value.length === 0) {
693
+ errs.name = 'Enter your name';
694
+ }
695
+ if (questionRef.current.value.length === 0) {
696
+ errs.question = 'Enter your question';
697
+ }
698
+ if (questionRef.current.value.length > QUESTION_MAX_LENGTH) {
699
+ errs.question = 'Your question must be 500 characters or less';
700
+ }
701
+ if (Object.keys(errs).length === 0) {
702
+ try {
703
+ const threadId = uuid__WEBPACK_IMPORTED_MODULE_1__.v4();
704
+ const customerId = await fetchCustomerId();
705
+ const thread = await fetchThread(threadId);
706
+ sdk.getCustomer().setName(nameRef.current.value);
707
+ await thread.startChat(questionRef.current.value || 'Begin conversation');
708
+ setCustomerId(customerId);
709
+ setThreadId(threadId);
710
+ setThread(thread);
711
+ } catch (err) {
712
+ console.log('[Request Chat Error]', err);
713
+ }
714
+ }
715
+ setErrors(errs);
716
+ };
717
+ const isQuestionLengthValid = QUESTION_MAX_LENGTH >= questionLength;
718
+ const questionLengthRemaining = QUESTION_MAX_LENGTH - questionLength;
719
+ const questionLengthExceeded = questionLength - QUESTION_MAX_LENGTH;
720
+ let questionHint = `You have ${questionLengthRemaining} characters remaining`;
721
+ if (!isQuestionLengthValid) {
722
+ questionHint = `You have ${questionLengthExceeded} characters too many`;
723
+ }
724
+ return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement((react__WEBPACK_IMPORTED_MODULE_0___default().Fragment), null, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_panel_panel_header_jsx__WEBPACK_IMPORTED_MODULE_3__.PanelHeader, null), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
725
+ className: "wc-body"
726
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("a", {
727
+ href: "#",
728
+ className: "wc-back-link govuk-back-link",
729
+ onClick: onBack
730
+ }, "What you can use webchat for"), Object.keys(errors).length > 0 ? /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
731
+ className: "govuk-error-summary govuk-!-static-margin-bottom-7",
732
+ "data-module": "govuk-error-summary",
733
+ tabIndex: "-1"
734
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
735
+ role: "alert"
736
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("h2", {
737
+ id: "wc-error",
738
+ className: "govuk-error-summary__title"
739
+ }, "There is a problem"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
740
+ className: "govuk-error-summary__body"
741
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("ul", {
742
+ className: "govuk-list govuk-error-summary__list"
743
+ }, Object.keys(errors).map(key => /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("li", {
744
+ key: key
745
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("a", {
746
+ href: `#${key}`
747
+ }, errors[key]))))))) : null, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("h3", {
748
+ className: "govuk-heading-s",
749
+ "aria-live": "polite"
750
+ }, "Your name and question"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("form", null, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
751
+ className: (0,_lib_classnames_js__WEBPACK_IMPORTED_MODULE_2__.classnames)('govuk-form-group', errors.name && 'govuk-form-group--error')
752
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("label", {
753
+ className: "govuk-label",
754
+ htmlFor: "wc-name"
755
+ }, "Your name"), errors.name && /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
756
+ className: "govuk-error-message"
757
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("span", {
758
+ className: "govuk-visually-hidden"
759
+ }, "Error:"), " ", errors.name), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("input", {
760
+ ref: nameRef,
761
+ className: (0,_lib_classnames_js__WEBPACK_IMPORTED_MODULE_2__.classnames)('govuk-input', errors.name && 'govuk-input--error'),
762
+ id: "wc-name",
763
+ name: "name",
764
+ type: "text",
765
+ "data-testid": "request-chat-user-name"
766
+ })), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
767
+ className: "govuk-character-count",
768
+ "data-module": "govuk-character-count",
769
+ "data-maxlength": "500"
770
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
771
+ className: (0,_lib_classnames_js__WEBPACK_IMPORTED_MODULE_2__.classnames)('govuk-form-group', errors.question && 'govuk-form-group--error')
772
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("label", {
773
+ className: "govuk-label",
774
+ htmlFor: "wc-question"
775
+ }, "Your question"), errors.question && /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
776
+ className: "govuk-error-message"
777
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("span", {
778
+ className: "govuk-visually-hidden"
779
+ }, "Error:"), errors.question), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("textarea", {
780
+ ref: questionRef,
781
+ id: "wc-question",
782
+ name: "question",
783
+ rows: "5",
784
+ "aria-describedby": "wc-question-info",
785
+ onChange: onQuestionChange,
786
+ className: (0,_lib_classnames_js__WEBPACK_IMPORTED_MODULE_2__.classnames)('govuk-textarea', 'govuk-js-character-count', !isQuestionLengthValid || errors.question ? 'govuk-textarea--error' : ''),
787
+ "data-testid": "request-chat-user-question"
788
+ }), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
789
+ id: "wc-question-info",
790
+ className: "govuk-hint govuk-character-count__message",
791
+ style: {
792
+ color: `${!isQuestionLengthValid ? '#d4351c' : ''}`
793
+ },
794
+ "aria-hidden": "true"
795
+ }, questionHint), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
796
+ className: "govuk-character-count__sr-status govuk-visually-hidden",
797
+ "aria-live": "polite"
798
+ }, questionHint))), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("button", {
799
+ className: "govuk-button govuk-!-margin-top-1 govuk-!-font-size-16",
800
+ "data-module": "govuk-button",
801
+ onClick: onRequestChat
802
+ }, "Request chat"))));
803
+ }
804
+
805
+ /***/ }),
806
+
807
+ /***/ "./src/client/components/screens/unavailable.jsx":
808
+ /*!*******************************************************!*\
809
+ !*** ./src/client/components/screens/unavailable.jsx ***!
810
+ \*******************************************************/
811
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
812
+
813
+ __webpack_require__.r(__webpack_exports__);
814
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
815
+ /* harmony export */ Unavailable: () => (/* binding */ Unavailable)
816
+ /* harmony export */ });
817
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
818
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
819
+ /* harmony import */ var _panel_panel_header_jsx__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../panel/panel-header.jsx */ "./src/client/components/panel/panel-header.jsx");
820
+
821
+
822
+ function Unavailable() {
823
+ return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement((react__WEBPACK_IMPORTED_MODULE_0___default().Fragment), null, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_panel_panel_header_jsx__WEBPACK_IMPORTED_MODULE_1__.PanelHeader, null), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
824
+ className: "wc-body"
825
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("h3", {
826
+ id: "wc-subtitle",
827
+ className: "govuk-heading-m"
828
+ }, "Webchat is currently not available"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
829
+ className: "govuk-body-s"
830
+ }, "Try again later, or call Floodline:"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
831
+ className: "govuk-body-s"
832
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("strong", null, "Floodline helpline"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("br", null), "Telephone: 0345 988 1188", /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("br", null), "Textphone: 0345 602 6340", /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("br", null), "Open 24 hours a day, 7 days a week", /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("br", null), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("a", {
833
+ className: "govuk-link",
834
+ href: "https://gov.uk/call-charges"
835
+ }, "Find out more about call charges")), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("p", {
836
+ className: "govuk-body-s"
837
+ }, "We're running webchat as a trial, it will not always be available.")));
838
+ }
839
+
840
+ /***/ }),
841
+
842
+ /***/ "./src/client/hooks/useFocusedElements.js":
843
+ /*!************************************************!*\
844
+ !*** ./src/client/hooks/useFocusedElements.js ***!
845
+ \************************************************/
846
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
847
+
848
+ __webpack_require__.r(__webpack_exports__);
849
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
850
+ /* harmony export */ getFocusableElements: () => (/* binding */ getFocusableElements),
851
+ /* harmony export */ useFocusedElements: () => (/* binding */ useFocusedElements)
852
+ /* harmony export */ });
853
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
854
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
855
+
856
+ const setAriaHidden = bool => {
857
+ for (const node of document.body.children) {
858
+ if (node.id !== 'wc-panel') {
859
+ bool ? node.setAttribute('aria-hidden', 'true') : node.removeAttribute('aria-hidden');
860
+ }
861
+ }
862
+ };
863
+ const getFocusableElements = () => {
864
+ const selectors = ['#wc-panel a:not([disabled])', '#wc-panel button:not([disabled])', '#wc-panel select:not([disabled])', '#wc-panel input:not([disabled])', '#wc-panel textarea:not([disabled])', '#wc-panel *[tabindex="0"]:not([disabled])'];
865
+ const elements = document.body.querySelectorAll(selectors.join(','));
866
+ return Array.from(elements).filter(e => !e.closest('[hidden]') && !e.closest('[aria-hidden="true"]'));
867
+ };
868
+ const useFocusedElements = screen => {
869
+ const [panelElements, setPanelElements] = (0,react__WEBPACK_IMPORTED_MODULE_0__.useState)([]);
870
+ const onKeyDown = (0,react__WEBPACK_IMPORTED_MODULE_0__.useCallback)(e => {
871
+ const webchatPanelElement = document.querySelector('#wc-panel');
872
+ if (e.key === 'Tab') {
873
+ if (webchatPanelElement && !document.activeElement.closest('#wc-panel')) {
874
+ webchatPanelElement.focus();
875
+ }
876
+ if (e.shiftKey) {
877
+ if (document.activeElement === panelElements[0]) {
878
+ panelElements[panelElements.length - 1].focus();
879
+ e.preventDefault();
880
+ } else if (document.activeElement === document.querySelector('#wc-panel')) {
881
+ panelElements[panelElements.length - 1]?.focus();
882
+ e.preventDefault();
883
+ }
884
+ } else if (document.activeElement === panelElements[panelElements.length - 1]) {
885
+ panelElements[0].focus();
886
+ e.preventDefault();
887
+ }
888
+ }
889
+ }, [panelElements]);
890
+ (0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(() => {
891
+ setPanelElements(getFocusableElements());
892
+ }, [screen]);
893
+ (0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(() => {
894
+ setAriaHidden(true);
895
+ const panelElement = document.querySelector('#wc-panel');
896
+ panelElement.focus();
897
+ document.addEventListener('keydown', onKeyDown);
898
+ return () => {
899
+ setAriaHidden();
900
+ document.body.querySelector('.wc-availability__link')?.focus();
901
+ document.removeEventListener('keydown', onKeyDown);
902
+ };
903
+ }, [panelElements]);
904
+ };
905
+
906
+
907
+ /***/ }),
908
+
909
+ /***/ "./src/client/hooks/useTextareaAutosize.js":
910
+ /*!*************************************************!*\
911
+ !*** ./src/client/hooks/useTextareaAutosize.js ***!
912
+ \*************************************************/
913
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
914
+
915
+ __webpack_require__.r(__webpack_exports__);
916
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
917
+ /* harmony export */ useTextareaAutosize: () => (/* binding */ useTextareaAutosize)
918
+ /* harmony export */ });
919
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
920
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
921
+
922
+ const useTextareaAutosize = (textAreaRef, value) => {
923
+ (0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(() => {
924
+ if (textAreaRef) {
925
+ textAreaRef.style.height = '0px';
926
+ const scrollHeight = textAreaRef.scrollHeight;
927
+ textAreaRef.style.height = `${scrollHeight}px`;
928
+ }
929
+ }, [textAreaRef, value]);
930
+ };
931
+
932
+ /***/ }),
933
+
934
+ /***/ "./src/client/lib/check-availability.js":
935
+ /*!**********************************************!*\
936
+ !*** ./src/client/lib/check-availability.js ***!
937
+ \**********************************************/
938
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
939
+
940
+ __webpack_require__.r(__webpack_exports__);
941
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
942
+ /* harmony export */ checkAvailability: () => (/* binding */ checkAvailability)
943
+ /* harmony export */ });
944
+ async function checkAvailability(endpoint) {
945
+ const response = await fetch(endpoint);
946
+ const data = await response.json();
947
+ return {
948
+ availability: data.availability || 'UNAVAILABLE'
949
+ };
950
+ }
951
+
952
+ /***/ }),
953
+
954
+ /***/ "./src/client/lib/classnames.js":
955
+ /*!**************************************!*\
956
+ !*** ./src/client/lib/classnames.js ***!
957
+ \**************************************/
958
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
959
+
960
+ __webpack_require__.r(__webpack_exports__);
961
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
962
+ /* harmony export */ classnames: () => (/* binding */ classnames)
963
+ /* harmony export */ });
964
+ const classnames = (...classes) => classes.filter(classname => classname && typeof classname === 'string').join(' ');
965
+
966
+ /***/ }),
967
+
968
+ /***/ "./src/client/lib/transform-messages.js":
969
+ /*!**********************************************!*\
970
+ !*** ./src/client/lib/transform-messages.js ***!
971
+ \**********************************************/
972
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
973
+
974
+ __webpack_require__.r(__webpack_exports__);
975
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
976
+ /* harmony export */ transformMessage: () => (/* binding */ transformMessage),
977
+ /* harmony export */ transformMessages: () => (/* binding */ transformMessages)
978
+ /* harmony export */ });
979
+ /* harmony import */ var sanitize_html__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! sanitize-html */ "sanitize-html");
980
+ /* harmony import */ var sanitize_html__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(sanitize_html__WEBPACK_IMPORTED_MODULE_0__);
981
+
982
+ const transformMessage = message => {
983
+ return {
984
+ id: message.id,
985
+ text: sanitize_html__WEBPACK_IMPORTED_MODULE_0___default()(message.messageContent?.text),
986
+ createdAt: new Date(message.createdAt),
987
+ user: message.authorEndUserIdentity?.fullName?.trim() || null,
988
+ assignee: message.authorUser?.firstName || null,
989
+ direction: message.direction
990
+ };
991
+ };
992
+ const transformMessages = messages => messages.map(message => transformMessage(message));
993
+
994
+ /***/ }),
995
+
996
+ /***/ "./src/client/store/AppProvider.jsx":
997
+ /*!******************************************!*\
998
+ !*** ./src/client/store/AppProvider.jsx ***!
999
+ \******************************************/
1000
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
1001
+
1002
+ __webpack_require__.r(__webpack_exports__);
1003
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
1004
+ /* harmony export */ AppContext: () => (/* binding */ AppContext),
1005
+ /* harmony export */ AppProvider: () => (/* binding */ AppProvider)
1006
+ /* harmony export */ });
1007
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
1008
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
1009
+ /* harmony import */ var _nice_devone_nice_cxone_chat_web_sdk__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @nice-devone/nice-cxone-chat-web-sdk */ "@nice-devone/nice-cxone-chat-web-sdk");
1010
+ /* harmony import */ var _nice_devone_nice_cxone_chat_web_sdk__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_nice_devone_nice_cxone_chat_web_sdk__WEBPACK_IMPORTED_MODULE_1__);
1011
+ /* harmony import */ var _reducer_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./reducer.js */ "./src/client/store/reducer.js");
1012
+
1013
+
1014
+
1015
+ const AppContext = /*#__PURE__*/(0,react__WEBPACK_IMPORTED_MODULE_0__.createContext)(_reducer_js__WEBPACK_IMPORTED_MODULE_2__.initialState);
1016
+ const AppProvider = ({
1017
+ sdk,
1018
+ availability,
1019
+ children
1020
+ }) => {
1021
+ const [state, dispatch] = (0,react__WEBPACK_IMPORTED_MODULE_0__.useReducer)(_reducer_js__WEBPACK_IMPORTED_MODULE_2__.reducer, _reducer_js__WEBPACK_IMPORTED_MODULE_2__.initialState);
1022
+
1023
+ /**
1024
+ * SDK event handlers
1025
+ */
1026
+ const onLiveChatRecovered = e => {
1027
+ dispatch({
1028
+ type: 'SET_AGENT',
1029
+ payload: e.detail.data.inboxAssignee
1030
+ });
1031
+ dispatch({
1032
+ type: 'SET_AGENT_STATUS',
1033
+ payload: e.detail.data.contact.status
1034
+ });
1035
+ };
1036
+ const onAssignedAgentChanged = e => {
1037
+ dispatch({
1038
+ type: 'SET_AGENT',
1039
+ payload: e.detail.data.inboxAssignee
1040
+ });
1041
+ dispatch({
1042
+ type: 'SET_AGENT_STATUS',
1043
+ payload: e.detail.data.case.status
1044
+ });
1045
+ };
1046
+ const onAgentTypingStarted = () => {
1047
+ dispatch({
1048
+ type: 'SET_AGENT_TYPING',
1049
+ payload: true
1050
+ });
1051
+ };
1052
+ const onAgentTypingEnded = () => {
1053
+ dispatch({
1054
+ type: 'SET_AGENT_TYPING',
1055
+ payload: false
1056
+ });
1057
+ };
1058
+ const onMessageCreated = e => {
1059
+ setMessage(e.detail.data.message);
1060
+ dispatch({
1061
+ type: 'SET_AGENT_STATUS',
1062
+ payload: e.detail.data.case.status
1063
+ });
1064
+ };
1065
+ const onContactStatusChanged = e => {
1066
+ dispatch({
1067
+ type: 'SET_AGENT_STATUS',
1068
+ payload: e.detail.data.case.status
1069
+ });
1070
+ };
1071
+ (0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(() => {
1072
+ sdk.onChatEvent(_nice_devone_nice_cxone_chat_web_sdk__WEBPACK_IMPORTED_MODULE_1__.ChatEvent.LIVECHAT_RECOVERED, onLiveChatRecovered);
1073
+ sdk.onChatEvent(_nice_devone_nice_cxone_chat_web_sdk__WEBPACK_IMPORTED_MODULE_1__.ChatEvent.MESSAGE_CREATED, onMessageCreated);
1074
+ sdk.onChatEvent(_nice_devone_nice_cxone_chat_web_sdk__WEBPACK_IMPORTED_MODULE_1__.ChatEvent.AGENT_TYPING_STARTED, onAgentTypingStarted);
1075
+ sdk.onChatEvent(_nice_devone_nice_cxone_chat_web_sdk__WEBPACK_IMPORTED_MODULE_1__.ChatEvent.AGENT_TYPING_ENDED, onAgentTypingEnded);
1076
+ sdk.onChatEvent(_nice_devone_nice_cxone_chat_web_sdk__WEBPACK_IMPORTED_MODULE_1__.ChatEvent.ASSIGNED_AGENT_CHANGED, onAssignedAgentChanged);
1077
+ sdk.onChatEvent(_nice_devone_nice_cxone_chat_web_sdk__WEBPACK_IMPORTED_MODULE_1__.ChatEvent.CONTACT_STATUS_CHANGED, onContactStatusChanged);
1078
+ }, [sdk]);
1079
+
1080
+ /**
1081
+ * Initialize customerId, threadId and whether the webchat should be open
1082
+ */
1083
+ (0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(() => {
1084
+ dispatch({
1085
+ type: 'SET_AVAILABILITY',
1086
+ payload: availability
1087
+ });
1088
+ setCustomerId(window.localStorage.getItem(_reducer_js__WEBPACK_IMPORTED_MODULE_2__.CUSTOMER_ID_STORAGE_KEY));
1089
+ setThreadId(window.localStorage.getItem(_reducer_js__WEBPACK_IMPORTED_MODULE_2__.THREAD_ID_STORAGE_KEY));
1090
+ if (window.location.hash === '#webchat') {
1091
+ setChatVisibility(true);
1092
+ }
1093
+ }, []);
1094
+
1095
+ /**
1096
+ * State update functions
1097
+ */
1098
+ const setChatVisibility = payload => {
1099
+ if (!payload) {
1100
+ window.location.hash = '';
1101
+ }
1102
+ dispatch({
1103
+ type: 'SET_CHAT_VISIBILITY',
1104
+ payload
1105
+ });
1106
+ };
1107
+ const setCustomerId = customerId => {
1108
+ dispatch({
1109
+ type: 'SET_CUSTOMER_ID',
1110
+ payload: customerId
1111
+ });
1112
+ };
1113
+ const setThreadId = threadId => {
1114
+ dispatch({
1115
+ type: 'SET_THREAD_ID',
1116
+ payload: threadId
1117
+ });
1118
+ };
1119
+ const setThread = thread => {
1120
+ dispatch({
1121
+ type: 'SET_THREAD',
1122
+ payload: thread
1123
+ });
1124
+ };
1125
+ const setMessage = message => {
1126
+ dispatch({
1127
+ type: 'SET_MESSAGE',
1128
+ payload: message
1129
+ });
1130
+ };
1131
+ const setMessages = messages => {
1132
+ dispatch({
1133
+ type: 'SET_MESSAGES',
1134
+ payload: messages
1135
+ });
1136
+ };
1137
+
1138
+ /**
1139
+ * Application-wide state and state functions
1140
+ */
1141
+ const store = (0,react__WEBPACK_IMPORTED_MODULE_0__.useMemo)(() => ({
1142
+ ...state,
1143
+ sdk,
1144
+ setCustomerId,
1145
+ setThreadId,
1146
+ setThread,
1147
+ setMessage,
1148
+ setMessages,
1149
+ setChatVisibility
1150
+ }));
1151
+ return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(AppContext.Provider, {
1152
+ value: store
1153
+ }, children);
1154
+ };
1155
+
1156
+ /***/ }),
1157
+
1158
+ /***/ "./src/client/store/actions-map.js":
1159
+ /*!*****************************************!*\
1160
+ !*** ./src/client/store/actions-map.js ***!
1161
+ \*****************************************/
1162
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
1163
+
1164
+ __webpack_require__.r(__webpack_exports__);
1165
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
1166
+ /* harmony export */ actionsMap: () => (/* binding */ actionsMap)
1167
+ /* harmony export */ });
1168
+ /* harmony import */ var _lib_transform_messages__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../lib/transform-messages */ "./src/client/lib/transform-messages.js");
1169
+ /* harmony import */ var _reducer__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./reducer */ "./src/client/store/reducer.js");
1170
+
1171
+
1172
+ const setCustomerId = (state, payload) => {
1173
+ if (payload) {
1174
+ window.localStorage.setItem(_reducer__WEBPACK_IMPORTED_MODULE_1__.CUSTOMER_ID_STORAGE_KEY, payload);
1175
+ } else {
1176
+ window.localStorage.removeItem(_reducer__WEBPACK_IMPORTED_MODULE_1__.CUSTOMER_ID_STORAGE_KEY);
1177
+ }
1178
+ return {
1179
+ ...state,
1180
+ customerId: payload
1181
+ };
1182
+ };
1183
+ const setThreadId = (state, payload) => {
1184
+ if (payload) {
1185
+ window.localStorage.setItem(_reducer__WEBPACK_IMPORTED_MODULE_1__.THREAD_ID_STORAGE_KEY, payload);
1186
+ } else {
1187
+ window.localStorage.removeItem(_reducer__WEBPACK_IMPORTED_MODULE_1__.THREAD_ID_STORAGE_KEY);
1188
+ }
1189
+ return {
1190
+ ...state,
1191
+ threadId: payload
1192
+ };
1193
+ };
1194
+ const setChatVisibility = (state, payload) => {
1195
+ return {
1196
+ ...state,
1197
+ isChatOpen: payload
1198
+ };
1199
+ };
1200
+ const setAvailability = (state, payload) => {
1201
+ return {
1202
+ ...state,
1203
+ availability: payload
1204
+ };
1205
+ };
1206
+ const setThread = (state, payload) => {
1207
+ return {
1208
+ ...state,
1209
+ thread: payload
1210
+ };
1211
+ };
1212
+ const setMessage = (state, payload) => {
1213
+ return {
1214
+ ...state,
1215
+ messages: [...state.messages, (0,_lib_transform_messages__WEBPACK_IMPORTED_MODULE_0__.transformMessage)(payload)]
1216
+ };
1217
+ };
1218
+ const setMessages = (state, payload) => {
1219
+ return {
1220
+ ...state,
1221
+ messages: (0,_lib_transform_messages__WEBPACK_IMPORTED_MODULE_0__.transformMessages)(payload).reverse()
1222
+ };
1223
+ };
1224
+ const setAgent = (state, payload) => {
1225
+ return {
1226
+ ...state,
1227
+ agent: payload
1228
+ };
1229
+ };
1230
+ const setAgentIsTyping = (state, payload) => {
1231
+ return {
1232
+ ...state,
1233
+ isAgentTyping: payload
1234
+ };
1235
+ };
1236
+ const setAgentStatus = (state, payload) => {
1237
+ return {
1238
+ ...state,
1239
+ agentStatus: payload
1240
+ };
1241
+ };
1242
+ const actionsMap = {
1243
+ SET_CHAT_VISIBILITY: setChatVisibility,
1244
+ SET_AVAILABILITY: setAvailability,
1245
+ SET_CUSTOMER_ID: setCustomerId,
1246
+ SET_THREAD_ID: setThreadId,
1247
+ SET_THREAD: setThread,
1248
+ SET_MESSAGE: setMessage,
1249
+ SET_MESSAGES: setMessages,
1250
+ SET_AGENT: setAgent,
1251
+ SET_AGENT_TYPING: setAgentIsTyping,
1252
+ SET_AGENT_STATUS: setAgentStatus
1253
+ };
1254
+
1255
+ /***/ }),
1256
+
1257
+ /***/ "./src/client/store/reducer.js":
1258
+ /*!*************************************!*\
1259
+ !*** ./src/client/store/reducer.js ***!
1260
+ \*************************************/
1261
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
1262
+
1263
+ __webpack_require__.r(__webpack_exports__);
1264
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
1265
+ /* harmony export */ CUSTOMER_ID_STORAGE_KEY: () => (/* binding */ CUSTOMER_ID_STORAGE_KEY),
1266
+ /* harmony export */ THREAD_ID_STORAGE_KEY: () => (/* binding */ THREAD_ID_STORAGE_KEY),
1267
+ /* harmony export */ initialState: () => (/* binding */ initialState),
1268
+ /* harmony export */ reducer: () => (/* binding */ reducer)
1269
+ /* harmony export */ });
1270
+ /* harmony import */ var _actions_map__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./actions-map */ "./src/client/store/actions-map.js");
1271
+
1272
+ const CUSTOMER_ID_STORAGE_KEY = 'webchat_customer_id';
1273
+ const THREAD_ID_STORAGE_KEY = 'webchat_thread_id';
1274
+ const initialState = {
1275
+ availability: null,
1276
+ customerId: null,
1277
+ threadId: null,
1278
+ thread: null,
1279
+ messages: [],
1280
+ agent: null,
1281
+ agentStatus: null,
1282
+ isAgentTyping: false,
1283
+ isChatOpen: false
1284
+ };
1285
+ const reducer = (state, action) => {
1286
+ const {
1287
+ type,
1288
+ payload
1289
+ } = action;
1290
+ const fn = _actions_map__WEBPACK_IMPORTED_MODULE_0__.actionsMap[type];
1291
+ if (fn) {
1292
+ const actionFunction = fn.bind(undefined, state, payload);
1293
+ return actionFunction();
1294
+ }
1295
+ return state;
1296
+ };
1297
+
1298
+ /***/ }),
1299
+
1300
+ /***/ "./src/client/store/useApp.js":
1301
+ /*!************************************!*\
1302
+ !*** ./src/client/store/useApp.js ***!
1303
+ \************************************/
1304
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
1305
+
1306
+ __webpack_require__.r(__webpack_exports__);
1307
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
1308
+ /* harmony export */ useApp: () => (/* binding */ useApp)
1309
+ /* harmony export */ });
1310
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
1311
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
1312
+ /* harmony import */ var _AppProvider_jsx__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./AppProvider.jsx */ "./src/client/store/AppProvider.jsx");
1313
+
1314
+
1315
+ const useApp = () => (0,react__WEBPACK_IMPORTED_MODULE_0__.useContext)(_AppProvider_jsx__WEBPACK_IMPORTED_MODULE_1__.AppContext);
1316
+
1317
+ /***/ }),
1318
+
1319
+ /***/ "./src/client/store/useChatSdk.js":
1320
+ /*!****************************************!*\
1321
+ !*** ./src/client/store/useChatSdk.js ***!
1322
+ \****************************************/
1323
+ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
1324
+
1325
+ __webpack_require__.r(__webpack_exports__);
1326
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
1327
+ /* harmony export */ useChatSdk: () => (/* binding */ useChatSdk)
1328
+ /* harmony export */ });
1329
+ const useChatSdk = sdk => {
1330
+ const connect = async () => sdk.authorize();
1331
+ const fetchCustomerId = async () => {
1332
+ const response = await connect();
1333
+ return response?.consumerIdentity.idOnExternalPlatform;
1334
+ };
1335
+ const fetchThread = async threadId => {
1336
+ await connect();
1337
+ return sdk.getThread(threadId);
1338
+ };
1339
+ const fetchMessages = async (thread, threadId) => {
1340
+ await connect();
1341
+ const recovered = await thread.recover(threadId);
1342
+ const allMessages = [];
1343
+ let fetchedMessages = recovered.messages;
1344
+ while (fetchedMessages.length) {
1345
+ fetchedMessages.map(msg => allMessages.push(msg));
1346
+ try {
1347
+ const response = await thread.loadMoreMessages();
1348
+ fetchedMessages = response.data.messages;
1349
+ } catch (err) {
1350
+ fetchedMessages = [];
1351
+ }
1352
+ }
1353
+ return allMessages;
1354
+ };
1355
+ return {
1356
+ connect,
1357
+ fetchCustomerId,
1358
+ fetchThread,
1359
+ fetchMessages
1360
+ };
1361
+ };
1362
+
1363
+ /***/ }),
1364
+
1365
+ /***/ "@nice-devone/nice-cxone-chat-web-sdk":
1366
+ /*!*******************************************************!*\
1367
+ !*** external "@nice-devone/nice-cxone-chat-web-sdk" ***!
1368
+ \*******************************************************/
1369
+ /***/ ((module) => {
1370
+
1371
+ module.exports = require("@nice-devone/nice-cxone-chat-web-sdk");
1372
+
1373
+ /***/ }),
1374
+
1375
+ /***/ "react":
1376
+ /*!************************!*\
1377
+ !*** external "react" ***!
1378
+ \************************/
1379
+ /***/ ((module) => {
1380
+
1381
+ module.exports = require("react");
1382
+
1383
+ /***/ }),
1384
+
1385
+ /***/ "react-dom":
1386
+ /*!****************************!*\
1387
+ !*** external "react-dom" ***!
1388
+ \****************************/
1389
+ /***/ ((module) => {
1390
+
1391
+ module.exports = require("react-dom");
1392
+
1393
+ /***/ }),
1394
+
1395
+ /***/ "react-dom/client":
1396
+ /*!***********************************!*\
1397
+ !*** external "react-dom/client" ***!
1398
+ \***********************************/
1399
+ /***/ ((module) => {
1400
+
1401
+ module.exports = require("react-dom/client");
1402
+
1403
+ /***/ }),
1404
+
1405
+ /***/ "sanitize-html":
1406
+ /*!********************************!*\
1407
+ !*** external "sanitize-html" ***!
1408
+ \********************************/
1409
+ /***/ ((module) => {
1410
+
1411
+ module.exports = require("sanitize-html");
1412
+
1413
+ /***/ }),
1414
+
1415
+ /***/ "uuid":
1416
+ /*!***********************!*\
1417
+ !*** external "uuid" ***!
1418
+ \***********************/
1419
+ /***/ ((module) => {
1420
+
1421
+ module.exports = require("uuid");
1422
+
1423
+ /***/ })
1424
+
1425
+ /******/ });
1426
+ /************************************************************************/
1427
+ /******/ // The module cache
1428
+ /******/ var __webpack_module_cache__ = {};
1429
+ /******/
1430
+ /******/ // The require function
1431
+ /******/ function __webpack_require__(moduleId) {
1432
+ /******/ // Check if module is in cache
1433
+ /******/ var cachedModule = __webpack_module_cache__[moduleId];
1434
+ /******/ if (cachedModule !== undefined) {
1435
+ /******/ return cachedModule.exports;
1436
+ /******/ }
1437
+ /******/ // Create a new module (and put it into the cache)
1438
+ /******/ var module = __webpack_module_cache__[moduleId] = {
1439
+ /******/ // no module.id needed
1440
+ /******/ // no module.loaded needed
1441
+ /******/ exports: {}
1442
+ /******/ };
1443
+ /******/
1444
+ /******/ // Execute the module function
1445
+ /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
1446
+ /******/
1447
+ /******/ // Return the exports of the module
1448
+ /******/ return module.exports;
1449
+ /******/ }
1450
+ /******/
1451
+ /************************************************************************/
1452
+ /******/ /* webpack/runtime/compat get default export */
1453
+ /******/ (() => {
1454
+ /******/ // getDefaultExport function for compatibility with non-harmony modules
1455
+ /******/ __webpack_require__.n = (module) => {
1456
+ /******/ var getter = module && module.__esModule ?
1457
+ /******/ () => (module['default']) :
1458
+ /******/ () => (module);
1459
+ /******/ __webpack_require__.d(getter, { a: getter });
1460
+ /******/ return getter;
1461
+ /******/ };
1462
+ /******/ })();
1463
+ /******/
1464
+ /******/ /* webpack/runtime/define property getters */
1465
+ /******/ (() => {
1466
+ /******/ // define getter functions for harmony exports
1467
+ /******/ __webpack_require__.d = (exports, definition) => {
1468
+ /******/ for(var key in definition) {
1469
+ /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
1470
+ /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
1471
+ /******/ }
1472
+ /******/ }
1473
+ /******/ };
1474
+ /******/ })();
1475
+ /******/
1476
+ /******/ /* webpack/runtime/hasOwnProperty shorthand */
1477
+ /******/ (() => {
1478
+ /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
1479
+ /******/ })();
1480
+ /******/
1481
+ /******/ /* webpack/runtime/make namespace object */
1482
+ /******/ (() => {
1483
+ /******/ // define __esModule on exports
1484
+ /******/ __webpack_require__.r = (exports) => {
1485
+ /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
1486
+ /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1487
+ /******/ }
1488
+ /******/ Object.defineProperty(exports, '__esModule', { value: true });
1489
+ /******/ };
1490
+ /******/ })();
1491
+ /******/
1492
+ /************************************************************************/
1493
+ var __webpack_exports__ = {};
1494
+ // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
1495
+ (() => {
1496
+ /*!******************************!*\
1497
+ !*** ./src/client/index.jsx ***!
1498
+ \******************************/
1499
+ __webpack_require__.r(__webpack_exports__);
1500
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
1501
+ /* harmony export */ init: () => (/* binding */ init)
1502
+ /* harmony export */ });
1503
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
1504
+ /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
1505
+ /* harmony import */ var react_dom_client__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! react-dom/client */ "react-dom/client");
1506
+ /* harmony import */ var react_dom_client__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react_dom_client__WEBPACK_IMPORTED_MODULE_1__);
1507
+ /* harmony import */ var _nice_devone_nice_cxone_chat_web_sdk__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @nice-devone/nice-cxone-chat-web-sdk */ "@nice-devone/nice-cxone-chat-web-sdk");
1508
+ /* harmony import */ var _nice_devone_nice_cxone_chat_web_sdk__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_nice_devone_nice_cxone_chat_web_sdk__WEBPACK_IMPORTED_MODULE_2__);
1509
+ /* harmony import */ var _components_availability_availability_jsx__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./components/availability/availability.jsx */ "./src/client/components/availability/availability.jsx");
1510
+ /* harmony import */ var _lib_check_availability__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./lib/check-availability */ "./src/client/lib/check-availability.js");
1511
+ /* harmony import */ var _store_AppProvider_jsx__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./store/AppProvider.jsx */ "./src/client/store/AppProvider.jsx");
1512
+ /* harmony import */ var _store_reducer_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./store/reducer.js */ "./src/client/store/reducer.js");
1513
+
1514
+
1515
+
1516
+
1517
+
1518
+
1519
+
1520
+ async function init(container, options) {
1521
+ const sdk = new _nice_devone_nice_cxone_chat_web_sdk__WEBPACK_IMPORTED_MODULE_2__.ChatSdk({
1522
+ brandId: options.brandId,
1523
+ channelId: options.channelId,
1524
+ customerId: window.localStorage.getItem(_store_reducer_js__WEBPACK_IMPORTED_MODULE_6__.CUSTOMER_ID_STORAGE_KEY) || '',
1525
+ environment: options.environment
1526
+ });
1527
+ const root = (0,react_dom_client__WEBPACK_IMPORTED_MODULE_1__.createRoot)(container);
1528
+ let availability;
1529
+ try {
1530
+ const result = await (0,_lib_check_availability__WEBPACK_IMPORTED_MODULE_4__.checkAvailability)(options.availabilityEndpoint);
1531
+ availability = result.availability;
1532
+ } catch (e) {
1533
+ availability = 'UNAVAILABLE';
1534
+ }
1535
+ root.render( /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_store_AppProvider_jsx__WEBPACK_IMPORTED_MODULE_5__.AppProvider, {
1536
+ sdk: sdk,
1537
+ availability: availability
1538
+ }, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_components_availability_availability_jsx__WEBPACK_IMPORTED_MODULE_3__.Availability, {
1539
+ availability: availability
1540
+ })));
1541
+ }
1542
+ })();
1543
+
1544
+ module.exports = __webpack_exports__;
1545
+ /******/ })()
1546
+ ;
1547
+ //# sourceMappingURL=client.js.map