@defra/flood-webchat 0.0.1-beta.50 → 0.0.1-beta.52
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.
- package/package.json +1 -1
- package/docs/assets/create-a-new-release.png +0 -0
- package/docs/assets/example-pre-release.png +0 -0
- package/docs/assets/example-release.png +0 -0
- package/docs/development-guide.md +0 -5
- package/docs/how-to-publish.md +0 -41
- package/jest.config.mjs +0 -29
- package/src/client/components/availability/availability.jsx +0 -130
- package/src/client/components/availability/availability.scss +0 -77
- package/src/client/components/chat/chat.jsx +0 -233
- package/src/client/components/chat/chat.scss +0 -260
- package/src/client/components/errorSummary/error-summary.jsx +0 -34
- package/src/client/components/live-region.jsx +0 -55
- package/src/client/components/message/message.jsx +0 -21
- package/src/client/components/panel/panel-footer.jsx +0 -9
- package/src/client/components/panel/panel-header.jsx +0 -75
- package/src/client/components/panel/panel.jsx +0 -163
- package/src/client/components/panel/panel.scss +0 -112
- package/src/client/components/screens/end-chat.jsx +0 -77
- package/src/client/components/screens/feedback.jsx +0 -65
- package/src/client/components/screens/pre-chat.jsx +0 -62
- package/src/client/components/screens/request-chat.jsx +0 -183
- package/src/client/components/screens/settings.jsx +0 -97
- package/src/client/components/screens/unavailable.jsx +0 -23
- package/src/client/components/skip-link.jsx +0 -42
- package/src/client/hooks/useFocusedElements.js +0 -80
- package/src/client/hooks/useTextareaAutosize.js +0 -13
- package/src/client/index.jsx +0 -33
- package/src/client/lib/agent-status-headline.js +0 -17
- package/src/client/lib/check-availability.js +0 -8
- package/src/client/lib/classnames.js +0 -1
- package/src/client/lib/history.js +0 -13
- package/src/client/lib/message-notification.js +0 -30
- package/src/client/lib/transform-messages.js +0 -47
- package/src/client/scss/_objects.scss +0 -33
- package/src/client/scss/_settings.scss +0 -101
- package/src/client/scss/_tools.scss +0 -33
- package/src/client/scss/_utilities.scss +0 -25
- package/src/client/store/AppProvider.jsx +0 -183
- package/src/client/store/actions-map.js +0 -152
- package/src/client/store/constants.js +0 -3
- package/src/client/store/reducer.js +0 -31
- package/src/client/store/useApp.js +0 -5
- package/src/client/store/useChatSdk.js +0 -37
- package/src/server/index.js +0 -60
- package/src/server/lib/client.js +0 -88
- package/src/server/lib/utils.js +0 -23
- package/webpack.config.mjs +0 -30
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { useState, useEffect, useCallback } from 'react'
|
|
2
|
-
|
|
3
|
-
const ariaHidden = 'aria-hidden'
|
|
4
|
-
const dataWcInert = 'data-wc-inert'
|
|
5
|
-
|
|
6
|
-
const setAriaHidden = isInert => {
|
|
7
|
-
for (const node of document.body.children) {
|
|
8
|
-
if (node.id !== 'wc-panel') {
|
|
9
|
-
// We only want to toggle elements that aren't already inert
|
|
10
|
-
if (isInert && !node.getAttribute(ariaHidden)) {
|
|
11
|
-
node.setAttribute(ariaHidden, 'true')
|
|
12
|
-
node.setAttribute(dataWcInert, '')
|
|
13
|
-
} else if (node.hasAttribute(dataWcInert)) {
|
|
14
|
-
node.removeAttribute(ariaHidden)
|
|
15
|
-
node.removeAttribute(dataWcInert)
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export const getFocusableElements = () => {
|
|
22
|
-
const selectors = [
|
|
23
|
-
'#wc-panel a:not([disabled])',
|
|
24
|
-
'#wc-panel button:not([disabled])',
|
|
25
|
-
'#wc-panel select:not([disabled])',
|
|
26
|
-
'#wc-panel input:not([disabled])',
|
|
27
|
-
'#wc-panel textarea:not([disabled])',
|
|
28
|
-
'#wc-panel *[tabindex="0"]:not([disabled])'
|
|
29
|
-
]
|
|
30
|
-
|
|
31
|
-
const elements = document.body.querySelectorAll(selectors.join(','))
|
|
32
|
-
return Array.from(elements).filter(e => !e.closest('[hidden]') && !e.closest('[aria-hidden="true"]'))
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const useFocusedElements = screen => {
|
|
36
|
-
const [panelElements, setPanelElements] = useState([])
|
|
37
|
-
|
|
38
|
-
const onKeyDown = useCallback(e => {
|
|
39
|
-
const webchatPanelElement = document.querySelector('#wc-panel')
|
|
40
|
-
|
|
41
|
-
if (e.key === 'Tab') {
|
|
42
|
-
if (webchatPanelElement && !document.activeElement.closest('#wc-panel')) {
|
|
43
|
-
webchatPanelElement.focus()
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (e.shiftKey) {
|
|
47
|
-
if (document.activeElement === panelElements[0]) {
|
|
48
|
-
panelElements[panelElements.length - 1].focus()
|
|
49
|
-
e.preventDefault()
|
|
50
|
-
} else if (document.activeElement === document.querySelector('#wc-panel')) {
|
|
51
|
-
panelElements[panelElements.length - 1]?.focus()
|
|
52
|
-
e.preventDefault()
|
|
53
|
-
}
|
|
54
|
-
} else if (document.activeElement === panelElements[panelElements.length - 1]) {
|
|
55
|
-
panelElements[0].focus()
|
|
56
|
-
e.preventDefault()
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}, [panelElements])
|
|
60
|
-
|
|
61
|
-
useEffect(() => {
|
|
62
|
-
setPanelElements(getFocusableElements())
|
|
63
|
-
}, [screen])
|
|
64
|
-
|
|
65
|
-
useEffect(() => {
|
|
66
|
-
setAriaHidden(true)
|
|
67
|
-
|
|
68
|
-
const panelElement = document.querySelector('#wc-panel')
|
|
69
|
-
panelElement.focus()
|
|
70
|
-
|
|
71
|
-
document.addEventListener('keydown', onKeyDown)
|
|
72
|
-
|
|
73
|
-
return () => {
|
|
74
|
-
setAriaHidden()
|
|
75
|
-
document.removeEventListener('keydown', onKeyDown)
|
|
76
|
-
}
|
|
77
|
-
}, [panelElements])
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export { useFocusedElements }
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { useEffect } from 'react'
|
|
2
|
-
|
|
3
|
-
export const useTextareaAutosize = (textAreaRef, value) => {
|
|
4
|
-
useEffect(() => {
|
|
5
|
-
if (textAreaRef) {
|
|
6
|
-
textAreaRef.style.height = '0px'
|
|
7
|
-
|
|
8
|
-
const scrollHeight = textAreaRef.scrollHeight
|
|
9
|
-
|
|
10
|
-
textAreaRef.style.height = `${scrollHeight}px`
|
|
11
|
-
}
|
|
12
|
-
}, [textAreaRef, value])
|
|
13
|
-
}
|
package/src/client/index.jsx
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import React from 'react'
|
|
2
|
-
import { createRoot } from 'react-dom/client'
|
|
3
|
-
import { ChatSdk } from '@nice-devone/nice-cxone-chat-web-sdk'
|
|
4
|
-
import { Availability } from './components/availability/availability.jsx'
|
|
5
|
-
import { checkAvailability } from './lib/check-availability'
|
|
6
|
-
import { AppProvider } from './store/AppProvider.jsx'
|
|
7
|
-
import { CUSTOMER_ID_STORAGE_KEY } from './store/constants.js'
|
|
8
|
-
import { messageNotification } from './lib/message-notification.js'
|
|
9
|
-
|
|
10
|
-
export async function init (container, options) {
|
|
11
|
-
const sdk = new ChatSdk({
|
|
12
|
-
brandId: options.brandId,
|
|
13
|
-
channelId: options.channelId,
|
|
14
|
-
customerId: window.localStorage.getItem(CUSTOMER_ID_STORAGE_KEY) || '',
|
|
15
|
-
environment: options.environment
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
const root = createRoot(container)
|
|
19
|
-
let availability
|
|
20
|
-
let playSound
|
|
21
|
-
try {
|
|
22
|
-
const result = await checkAvailability(options.availabilityEndpoint)
|
|
23
|
-
playSound = await messageNotification(options.audioUrl)
|
|
24
|
-
availability = result.availability
|
|
25
|
-
} catch (e) {
|
|
26
|
-
availability = 'UNAVAILABLE'
|
|
27
|
-
}
|
|
28
|
-
root.render(
|
|
29
|
-
<AppProvider sdk={sdk} availability={availability} playSound={playSound}>
|
|
30
|
-
<Availability />
|
|
31
|
-
</AppProvider>
|
|
32
|
-
)
|
|
33
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export const agentStatusHeadline = (availability, agentStatus, agentName) => {
|
|
2
|
-
if (availability === 'UNAVAILABLE') {
|
|
3
|
-
return 'Webchat is not currently available'
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
if (!agentStatus) {
|
|
7
|
-
return 'Connecting to Floodline'
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
switch (agentStatus) {
|
|
11
|
-
case 'closed':
|
|
12
|
-
case 'resolved':
|
|
13
|
-
return agentName ? `${agentName} ended the session` : 'Session ended by advisor'
|
|
14
|
-
default:
|
|
15
|
-
return agentName ? `You are speaking with ${agentName}` : 'Waiting for an adviser'
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const classnames = (...classes) => classes.filter(classname => classname && typeof classname === 'string').join(' ')
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export const historyPushState = () => {
|
|
2
|
-
const url = window.location.href.split('#')[0]
|
|
3
|
-
window.history.pushState({ history: true }, null, `${url}#webchat`)
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export const historyReplaceState = () => {
|
|
7
|
-
if (window.history.state?.history) {
|
|
8
|
-
return window.history.back()
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const url = window.location.href.split('#')[0]
|
|
12
|
-
return window.history.replaceState(null, null, url)
|
|
13
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
export const messageNotification = async audioUrl => {
|
|
2
|
-
const context = new (window.AudioContext || window.webkitAudioContext)()
|
|
3
|
-
|
|
4
|
-
if (context.state === 'suspended') {
|
|
5
|
-
const events = ['touchstart', 'touchend', 'mousedown', 'wheel', 'keydown', 'click']
|
|
6
|
-
|
|
7
|
-
const unlock = _e => {
|
|
8
|
-
events.forEach(event => {
|
|
9
|
-
document.body.removeEventListener(event, unlock)
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
context.resume()
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
events.forEach(event => {
|
|
16
|
-
document.body.addEventListener(event, unlock, false)
|
|
17
|
-
})
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const response = await fetch(audioUrl)
|
|
21
|
-
const arrayBuffer = await response.arrayBuffer()
|
|
22
|
-
const buffer = await context.decodeAudioData(arrayBuffer)
|
|
23
|
-
|
|
24
|
-
return () => {
|
|
25
|
-
const source = context.createBufferSource()
|
|
26
|
-
source.buffer = buffer
|
|
27
|
-
source.connect(context.destination)
|
|
28
|
-
source.start()
|
|
29
|
-
}
|
|
30
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { DateTime } from 'luxon'
|
|
2
|
-
import parse from 'html-react-parser'
|
|
3
|
-
|
|
4
|
-
const websiteRegex = /(?:www|https?)[^\s]+/gi
|
|
5
|
-
|
|
6
|
-
export const formatMessage = message => {
|
|
7
|
-
if (message.match(websiteRegex)) {
|
|
8
|
-
message = message.replace(websiteRegex, link => {
|
|
9
|
-
return `<a class="govuk-link" href="${link}" target="_blank" rel="noreferrer">${link.replace(/https?:\/\//gi, '')}</a>`
|
|
10
|
-
})
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
return parse(message)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export const transformMessage = message => {
|
|
17
|
-
return {
|
|
18
|
-
id: message.id,
|
|
19
|
-
text: message.messageContent?.text,
|
|
20
|
-
createdAt: new Date(message.createdAt),
|
|
21
|
-
user: message.authorEndUserIdentity?.fullName?.trim() || null,
|
|
22
|
-
assignee: message.authorUser?.firstName || null,
|
|
23
|
-
direction: message.direction
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const transformMessages = messages => messages.map(message => transformMessage(message))
|
|
28
|
-
|
|
29
|
-
export const formatTranscript = messages => {
|
|
30
|
-
const now = DateTime.local()
|
|
31
|
-
now.setZone('Europe/London')
|
|
32
|
-
|
|
33
|
-
let string = `Floodline webchat transcript, ${now.toFormat('HH:mm:ss a, dd LLLL yyyy')}\n\n`
|
|
34
|
-
|
|
35
|
-
for (const message of messages) {
|
|
36
|
-
const { text, direction, user, assignee, createdAt } = message
|
|
37
|
-
|
|
38
|
-
const author = direction === 'inbound' ? user : `${assignee} (Floodline adviser)`
|
|
39
|
-
const date = DateTime.fromJSDate(new Date(createdAt)).setZone('Europe/London').toFormat('HH:mm:ss a, dd LLLL yyyy')
|
|
40
|
-
|
|
41
|
-
string += `[${date}] ${author}: \n${text}\n\n`
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
string = string.replace(/<a\b[^>]*>/i, '').replace(/<\/a>/i, '')
|
|
45
|
-
|
|
46
|
-
return encodeURIComponent(string)
|
|
47
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
%wc-link-button {
|
|
2
|
-
@extend %govuk-link;
|
|
3
|
-
position: relative;
|
|
4
|
-
display: inline-block;
|
|
5
|
-
font-size: 16px;
|
|
6
|
-
color: govuk-colour('black');
|
|
7
|
-
|
|
8
|
-
&:hover {
|
|
9
|
-
color: govuk-colour('black');
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
&:visited:not(:hover):not(:focus) {
|
|
13
|
-
color: govuk-colour('black');
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
&:active {
|
|
17
|
-
color: govuk-colour('black');
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
&:focus {
|
|
21
|
-
box-shadow: none;
|
|
22
|
-
background-color: transparent;
|
|
23
|
-
text-decoration: underline;
|
|
24
|
-
color: govuk-colour('black');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
&:focus-visible {
|
|
28
|
-
background-color: $govuk-focus-colour;
|
|
29
|
-
box-shadow: 0 -2px $govuk-focus-colour, 0 4px govuk-colour('black');
|
|
30
|
-
text-decoration: none;
|
|
31
|
-
color: govuk-colour('black');
|
|
32
|
-
}
|
|
33
|
-
}
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
.wc-heading {
|
|
2
|
-
@extend %govuk-heading-m;
|
|
3
|
-
font-size: 19px;
|
|
4
|
-
margin-bottom: 15px;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
.wc-list {
|
|
8
|
-
@extend %govuk-list;
|
|
9
|
-
@extend %govuk-list--bullet;
|
|
10
|
-
font-size: 16px;
|
|
11
|
-
line-height: 1.25;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
.wc-body p {
|
|
15
|
-
font-size: 16px;
|
|
16
|
-
line-height: 1.25;
|
|
17
|
-
margin-bottom: 15px;
|
|
18
|
-
@extend %govuk-body-s;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
.wc-body .govuk-error-message {
|
|
22
|
-
color: govuk-colour('red');
|
|
23
|
-
font-weight: 700;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
.wc-link,
|
|
27
|
-
.wc-button,
|
|
28
|
-
.govuk-button-group .wc-button,
|
|
29
|
-
.govuk-button-group .wc-link {
|
|
30
|
-
font-size: 16px;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
.wc-button[aria-disabled="true"] {
|
|
34
|
-
cursor: not-allowed;
|
|
35
|
-
opacity: 0.5;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
.wc-label {
|
|
39
|
-
font-size: 16px;
|
|
40
|
-
line-height: 1.25;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
.wc-form-group {
|
|
44
|
-
margin-bottom: 15px;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
.wc-hint {
|
|
48
|
-
font-size: 16px;
|
|
49
|
-
line-height: 1.25;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
.wc-inset-text {
|
|
53
|
-
margin: 0 0 20px;
|
|
54
|
-
padding: 5px 5px 5px 15px;
|
|
55
|
-
font-size: 16px;
|
|
56
|
-
border-left-width: 5px;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
.wc-error-summary__title {
|
|
60
|
-
font-size: 19px;
|
|
61
|
-
line-height: 1.25;
|
|
62
|
-
margin-bottom: 15px;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
.wc-error-summary__list,
|
|
66
|
-
.wc-error-message {
|
|
67
|
-
font-size: 16px;
|
|
68
|
-
line-height: 1.25;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
.wc-input, .wc-textarea {
|
|
72
|
-
font-size: 16px;
|
|
73
|
-
line-height: 1.25;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
.wc-back-link {
|
|
77
|
-
font-size: 16px;
|
|
78
|
-
line-height: 1.25;
|
|
79
|
-
margin: 0 0 25px;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// stylelint-disable declaration-no-important
|
|
83
|
-
.wc-live {
|
|
84
|
-
position: absolute !important;
|
|
85
|
-
width: 1px !important;
|
|
86
|
-
height: 1px !important;
|
|
87
|
-
margin: 0 !important;
|
|
88
|
-
padding: 0 !important;
|
|
89
|
-
overflow: hidden !important;
|
|
90
|
-
clip: rect(0 0 0 0) !important;
|
|
91
|
-
-webkit-clip-path: inset(50%) !important;
|
|
92
|
-
clip-path: inset(50%) !important;
|
|
93
|
-
border: 0 !important;
|
|
94
|
-
white-space: nowrap !important;
|
|
95
|
-
|
|
96
|
-
&::before,
|
|
97
|
-
&::after {
|
|
98
|
-
content: ' ';
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
// stylelint-enable declaration-no-important
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
// Check there is ot already similar functinality in GOV.UK Frontend
|
|
2
|
-
|
|
3
|
-
@mixin defra-visually-hidden() {
|
|
4
|
-
position: absolute;
|
|
5
|
-
width: 1px;
|
|
6
|
-
height: 1px;
|
|
7
|
-
margin: 0;
|
|
8
|
-
padding: 0;
|
|
9
|
-
overflow: hidden;
|
|
10
|
-
clip: rect(0 0 0 0);
|
|
11
|
-
-webkit-clip-path: inset(50%);
|
|
12
|
-
clip-path: inset(50%);
|
|
13
|
-
border: 0;
|
|
14
|
-
white-space: nowrap;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Map specific mixins
|
|
18
|
-
@mixin focus($glow: 8px, $strong: 5px, $background: 2px, $inset: 0) {
|
|
19
|
-
position:absolute;
|
|
20
|
-
content:'';
|
|
21
|
-
left: 5px;
|
|
22
|
-
right: 5px;
|
|
23
|
-
top: 5px;
|
|
24
|
-
bottom: 5px;
|
|
25
|
-
box-shadow:
|
|
26
|
-
0 0 0 $background #ffffff,
|
|
27
|
-
inset 0 0 0 $inset govuk-colour('black'),
|
|
28
|
-
0 0 0 $strong govuk-colour('black'),
|
|
29
|
-
0 0 0 $glow $govuk-focus-colour;
|
|
30
|
-
outline: 3px solid transparent;
|
|
31
|
-
pointer-events: none;
|
|
32
|
-
z-index: 99;
|
|
33
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
.wc-u-html {
|
|
2
|
-
@include mq ($until: tablet) {
|
|
3
|
-
height: 100vh;
|
|
4
|
-
overflow: hidden;
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
.wc-u-body {
|
|
9
|
-
@include mq ($until: tablet) {
|
|
10
|
-
position: fixed;
|
|
11
|
-
overflow: hidden;
|
|
12
|
-
top:0;
|
|
13
|
-
right:0;
|
|
14
|
-
bottom:0;
|
|
15
|
-
left:0;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
.wc-u-hidden {
|
|
20
|
-
visibility: hidden;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
.wc-u-scroll-padding {
|
|
24
|
-
scroll-padding-bottom: calc(2rem + 20px);
|
|
25
|
-
}
|
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import React, { createContext, useEffect, useReducer, useMemo } from 'react'
|
|
2
|
-
import { ChatEvent } from '@nice-devone/nice-cxone-chat-web-sdk'
|
|
3
|
-
|
|
4
|
-
import { initialState, reducer } from './reducer.js'
|
|
5
|
-
import { CUSTOMER_ID_STORAGE_KEY, THREAD_ID_STORAGE_KEY, SETTINGS_STORAGE_KEY } from './constants.js'
|
|
6
|
-
|
|
7
|
-
export const AppContext = createContext(initialState)
|
|
8
|
-
|
|
9
|
-
export const AppProvider = ({ sdk, availability, playSound, children }) => {
|
|
10
|
-
const [state, dispatch] = useReducer(reducer, initialState)
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* SDK event handlers
|
|
14
|
-
*/
|
|
15
|
-
const onLiveChatRecovered = e => {
|
|
16
|
-
dispatch({ type: 'SET_AGENT', payload: e.detail.data.inboxAssignee })
|
|
17
|
-
dispatch({ type: 'SET_AGENT_STATUS', payload: e.detail.data.contact.status })
|
|
18
|
-
dispatch({ type: 'SET_UNSEEN_COUNT', payload: e.detail.data.contact.customerStatistics.unseenMessagesCount })
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const onAssignedAgentChanged = e => {
|
|
22
|
-
dispatch({ type: 'SET_AGENT', payload: e.detail.data.inboxAssignee })
|
|
23
|
-
dispatch({ type: 'SET_AGENT_STATUS', payload: e.detail.data.case.status })
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const onAgentTypingStarted = () => {
|
|
27
|
-
dispatch({ type: 'SET_AGENT_TYPING', payload: true })
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const onAgentTypingEnded = () => {
|
|
31
|
-
dispatch({ type: 'SET_AGENT_TYPING', payload: false })
|
|
32
|
-
dispatch({ type: 'SET_LIVE_REGION_TEXT' })
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const onMessageCreated = e => {
|
|
36
|
-
dispatch({ type: 'SET_MESSAGE', payload: e.detail.data.message })
|
|
37
|
-
dispatch({ type: 'SET_AGENT_STATUS', payload: e.detail.data.case.status })
|
|
38
|
-
dispatch({ type: 'SET_UNSEEN_COUNT', payload: e.detail.data.case.customerStatistics.unseenMessagesCount })
|
|
39
|
-
|
|
40
|
-
const isAudioOn = JSON.parse(window.localStorage.getItem(SETTINGS_STORAGE_KEY)).audio
|
|
41
|
-
|
|
42
|
-
if (isAudioOn && e.detail.data.message.direction === 'outbound' && playSound) {
|
|
43
|
-
playSound()
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const onContactStatusChanged = e => {
|
|
48
|
-
dispatch({ type: 'SET_AGENT_STATUS', payload: e.detail.data.case.status })
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const onMatchMedia = e => {
|
|
52
|
-
dispatch({ type: 'TOGGLE_IS_MOBILE', payload: e.matches })
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const onKeydown = () => {
|
|
56
|
-
dispatch({ type: 'TOGGLE_IS_KEYBOARD', payload: true })
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const onPointerdown = () => {
|
|
60
|
-
dispatch({ type: 'TOGGLE_IS_KEYBOARD', payload: false })
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
useEffect(() => {
|
|
64
|
-
sdk.onChatEvent(ChatEvent.LIVECHAT_RECOVERED, onLiveChatRecovered)
|
|
65
|
-
sdk.onChatEvent(ChatEvent.MESSAGE_CREATED, onMessageCreated)
|
|
66
|
-
sdk.onChatEvent(ChatEvent.AGENT_TYPING_STARTED, onAgentTypingStarted)
|
|
67
|
-
sdk.onChatEvent(ChatEvent.AGENT_TYPING_ENDED, onAgentTypingEnded)
|
|
68
|
-
sdk.onChatEvent(ChatEvent.ASSIGNED_AGENT_CHANGED, onAssignedAgentChanged)
|
|
69
|
-
sdk.onChatEvent(ChatEvent.CONTACT_STATUS_CHANGED, onContactStatusChanged)
|
|
70
|
-
// We need to know if it is a mobile and if it is a keyboard interaction
|
|
71
|
-
window.matchMedia('(max-width: 640px)').addEventListener('change', onMatchMedia)
|
|
72
|
-
window.addEventListener('keydown', onKeydown)
|
|
73
|
-
window.addEventListener('pointerdown', onPointerdown)
|
|
74
|
-
// Tidying up
|
|
75
|
-
return () => {
|
|
76
|
-
window.removeEventListener('change', onMatchMedia)
|
|
77
|
-
window.removeEventListener('keydown', onKeydown)
|
|
78
|
-
window.removeEventListener('pointerdown', onPointerdown)
|
|
79
|
-
}
|
|
80
|
-
}, [sdk])
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Initialize customerId, threadId and whether the webchat should be open
|
|
84
|
-
*/
|
|
85
|
-
useEffect(() => {
|
|
86
|
-
dispatch({ type: 'SET_AVAILABILITY', payload: availability })
|
|
87
|
-
|
|
88
|
-
setCustomerId(window.localStorage.getItem(CUSTOMER_ID_STORAGE_KEY))
|
|
89
|
-
setThreadId(window.localStorage.getItem(THREAD_ID_STORAGE_KEY))
|
|
90
|
-
setSettings(JSON.parse(window.localStorage.getItem(SETTINGS_STORAGE_KEY)) || state.settings)
|
|
91
|
-
}, [])
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Set browser history on start chat click
|
|
95
|
-
*/
|
|
96
|
-
useEffect(() => {
|
|
97
|
-
if (window.location.hash === '#webchat') {
|
|
98
|
-
setChatVisibility(true)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const onBrowserNavigation = () => {
|
|
102
|
-
if (window.location.hash === '#webchat') {
|
|
103
|
-
setChatVisibility(true)
|
|
104
|
-
} else {
|
|
105
|
-
setChatVisibility(false)
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
window.addEventListener('popstate', onBrowserNavigation)
|
|
110
|
-
|
|
111
|
-
return () => {
|
|
112
|
-
window.removeEventListener('popstate', onBrowserNavigation)
|
|
113
|
-
}
|
|
114
|
-
}, [])
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* State update functions
|
|
118
|
-
*/
|
|
119
|
-
const setChatVisibility = payload => {
|
|
120
|
-
dispatch({ type: 'SET_CHAT_VISIBILITY', payload })
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const setCustomerId = customerId => {
|
|
124
|
-
dispatch({ type: 'SET_CUSTOMER_ID', payload: customerId })
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const setThreadId = threadId => {
|
|
128
|
-
dispatch({ type: 'SET_THREAD_ID', payload: threadId })
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const setThread = thread => {
|
|
132
|
-
dispatch({ type: 'SET_THREAD', payload: thread })
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const setMessages = messages => {
|
|
136
|
-
dispatch({ type: 'SET_MESSAGES', payload: messages })
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
const setSettings = data => {
|
|
140
|
-
dispatch({ type: 'SET_SETTINGS', payload: data })
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const setUnseenCount = unseenCount => {
|
|
144
|
-
dispatch({ type: 'SET_UNSEEN_COUNT', payload: unseenCount })
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const setInstigatorId = id => {
|
|
148
|
-
dispatch({ type: 'SET_INSTIGATOR_ID', payload: id })
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const setLiveRegionText = text => {
|
|
152
|
-
dispatch({ type: 'SET_LIVE_REGION_TEXT', payload: text })
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Application-wide state and state functions
|
|
157
|
-
*/
|
|
158
|
-
const store = useMemo(() => ({
|
|
159
|
-
...state,
|
|
160
|
-
sdk,
|
|
161
|
-
setSettings,
|
|
162
|
-
setCustomerId,
|
|
163
|
-
setThreadId,
|
|
164
|
-
setThread,
|
|
165
|
-
setMessages,
|
|
166
|
-
setUnseenCount,
|
|
167
|
-
setChatVisibility,
|
|
168
|
-
setInstigatorId,
|
|
169
|
-
setLiveRegionText,
|
|
170
|
-
onLiveChatRecovered,
|
|
171
|
-
onAssignedAgentChanged,
|
|
172
|
-
onAgentTypingStarted,
|
|
173
|
-
onAgentTypingEnded,
|
|
174
|
-
onMessageCreated,
|
|
175
|
-
onContactStatusChanged
|
|
176
|
-
}))
|
|
177
|
-
|
|
178
|
-
return (
|
|
179
|
-
<AppContext.Provider value={store}>
|
|
180
|
-
{children}
|
|
181
|
-
</AppContext.Provider>
|
|
182
|
-
)
|
|
183
|
-
}
|