@defra/flood-webchat 0.0.1-beta.4 → 0.0.1-beta.40
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/README.md +83 -1
- package/dist/client.js +1939 -416
- package/dist/client.js.map +1 -1
- package/dist/server.js +181 -241
- package/dist/server.js.map +1 -1
- package/main.scss +10 -6
- package/package.json +5 -2
- package/src/client/components/availability/availability.jsx +64 -55
- package/src/client/components/availability/availability.scss +15 -33
- package/src/client/components/chat/chat.jsx +199 -0
- package/src/client/components/chat/chat.scss +233 -0
- package/src/client/components/errorSummary/error-summary.jsx +34 -0
- package/src/client/components/message/message.jsx +20 -0
- package/src/client/components/panel/panel-footer.jsx +9 -0
- package/src/client/components/panel/panel-header.jsx +54 -14
- package/src/client/components/panel/panel.jsx +113 -72
- package/src/client/components/panel/panel.scss +63 -22
- package/src/client/components/screens/end-chat.jsx +77 -0
- package/src/client/components/screens/feedback.jsx +65 -0
- package/src/client/components/screens/pre-chat.jsx +50 -30
- package/src/client/components/screens/request-chat.jsx +171 -4
- package/src/client/components/screens/settings.jsx +97 -0
- package/src/client/components/screens/unavailable.jsx +23 -0
- package/src/client/components/skip-link.jsx +42 -0
- package/src/client/hooks/useFocusedElements.js +80 -0
- package/src/client/hooks/useTextareaAutosize.js +13 -0
- package/src/client/index.jsx +15 -1
- package/src/client/lib/agent-status-headline.js +17 -0
- package/src/client/lib/check-availability.js +1 -0
- package/src/client/lib/history.js +13 -0
- package/src/client/lib/message-notification.js +36 -0
- package/src/client/lib/transform-messages.js +34 -0
- package/src/client/scss/_objects.scss +33 -0
- package/src/client/scss/_settings.scss +80 -0
- package/src/client/scss/_tools.scss +33 -0
- package/src/client/scss/_utilities.scss +25 -0
- package/src/client/store/AppProvider.jsx +179 -0
- package/src/client/store/actions-map.js +143 -0
- package/src/client/store/constants.js +3 -0
- package/src/client/store/reducer.js +31 -0
- package/src/client/store/useApp.js +5 -0
- package/src/client/store/useChatSdk.js +37 -0
- package/src/server/index.js +15 -6
- package/src/server/lib/client.js +31 -35
- package/src/server/lib/utils.js +16 -8
- package/src/client/lib/external-stores.js +0 -4
- package/src/client/lib/external-sync-store.js +0 -42
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { createPortal } from 'react-dom'
|
|
3
|
+
import { useApp } from '../store/useApp'
|
|
4
|
+
|
|
5
|
+
export const SkipLink = () => {
|
|
6
|
+
const { threadId, setInstigatorId } = useApp()
|
|
7
|
+
|
|
8
|
+
if (!threadId) {
|
|
9
|
+
return null
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const targetContainer = document.getElementById('skip-links')
|
|
13
|
+
|
|
14
|
+
if (!targetContainer) {
|
|
15
|
+
return null
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const onClick = e => {
|
|
19
|
+
e.preventDefault()
|
|
20
|
+
setInstigatorId(e.target.id)
|
|
21
|
+
window.location.hash = '#webchat'
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<>
|
|
26
|
+
{createPortal(
|
|
27
|
+
<a
|
|
28
|
+
id='webchat-skip-link'
|
|
29
|
+
href='#webchat'
|
|
30
|
+
className='govuk-skip-link'
|
|
31
|
+
data-module='govuk-skip-link'
|
|
32
|
+
onClick={onClick}
|
|
33
|
+
data-wc-skiplink
|
|
34
|
+
data-wc-open-btn
|
|
35
|
+
>
|
|
36
|
+
Skip to webchat
|
|
37
|
+
</a>,
|
|
38
|
+
targetContainer
|
|
39
|
+
)}
|
|
40
|
+
</>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
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 }
|
|
@@ -0,0 +1,13 @@
|
|
|
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
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
import React from 'react'
|
|
2
2
|
import { createRoot } from 'react-dom/client'
|
|
3
|
+
import { ChatSdk } from '@nice-devone/nice-cxone-chat-web-sdk'
|
|
3
4
|
import { Availability } from './components/availability/availability.jsx'
|
|
4
5
|
import { checkAvailability } from './lib/check-availability'
|
|
6
|
+
import { AppProvider } from './store/AppProvider.jsx'
|
|
7
|
+
import { CUSTOMER_ID_STORAGE_KEY } from './store/constants.js'
|
|
5
8
|
|
|
6
9
|
export async function init (container, options) {
|
|
10
|
+
const sdk = new ChatSdk({
|
|
11
|
+
brandId: options.brandId,
|
|
12
|
+
channelId: options.channelId,
|
|
13
|
+
customerId: window.localStorage.getItem(CUSTOMER_ID_STORAGE_KEY) || '',
|
|
14
|
+
environment: options.environment
|
|
15
|
+
})
|
|
16
|
+
|
|
7
17
|
const root = createRoot(container)
|
|
8
18
|
let availability
|
|
9
19
|
try {
|
|
@@ -12,5 +22,9 @@ export async function init (container, options) {
|
|
|
12
22
|
} catch (e) {
|
|
13
23
|
availability = 'UNAVAILABLE'
|
|
14
24
|
}
|
|
15
|
-
root.render(
|
|
25
|
+
root.render(
|
|
26
|
+
<AppProvider sdk={sdk} availability={availability} options={options}>
|
|
27
|
+
<Availability />
|
|
28
|
+
</AppProvider>
|
|
29
|
+
)
|
|
16
30
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
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}` : 'No advisers currently available'
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export const messageNotification = audioUrl => {
|
|
2
|
+
let buffer
|
|
3
|
+
|
|
4
|
+
const context = new (window.AudioContext || window.webkitAudioContext)()
|
|
5
|
+
|
|
6
|
+
if (context.state === 'suspended') {
|
|
7
|
+
const events = ['touchstart', 'touchend', 'mousedown', 'wheel', 'keydown', 'click']
|
|
8
|
+
|
|
9
|
+
const unlock = _e => {
|
|
10
|
+
events.forEach(event => {
|
|
11
|
+
document.body.removeEventListener(event, unlock)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
context.resume()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
events.forEach(event => {
|
|
18
|
+
document.body.addEventListener(event, unlock, false)
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
fetch(audioUrl)
|
|
23
|
+
.then(response => response.arrayBuffer())
|
|
24
|
+
.then(data => context.decodeAudioData(data))
|
|
25
|
+
.then(decodedData => {
|
|
26
|
+
buffer = decodedData
|
|
27
|
+
})
|
|
28
|
+
.catch(console.error)
|
|
29
|
+
|
|
30
|
+
return () => {
|
|
31
|
+
const source = context.createBufferSource()
|
|
32
|
+
source.buffer = buffer
|
|
33
|
+
source.connect(context.destination)
|
|
34
|
+
source.start()
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { DateTime } from 'luxon'
|
|
2
|
+
|
|
3
|
+
export const transformMessage = message => {
|
|
4
|
+
return {
|
|
5
|
+
id: message.id,
|
|
6
|
+
text: message.messageContent?.text,
|
|
7
|
+
createdAt: new Date(message.createdAt),
|
|
8
|
+
user: message.authorEndUserIdentity?.fullName?.trim() || null,
|
|
9
|
+
assignee: message.authorUser?.firstName || null,
|
|
10
|
+
direction: message.direction
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const transformMessages = messages => messages.map(message => transformMessage(message))
|
|
15
|
+
|
|
16
|
+
export const formatTranscript = messages => {
|
|
17
|
+
const now = DateTime.local()
|
|
18
|
+
now.setZone('Europe/London')
|
|
19
|
+
|
|
20
|
+
let string = `Floodline webchat transcript, ${now.toFormat('HH:mm:ss a, dd LLLL yyyy')}\n\n`
|
|
21
|
+
|
|
22
|
+
for (const message of messages) {
|
|
23
|
+
const { text, direction, user, assignee, createdAt } = message
|
|
24
|
+
|
|
25
|
+
const author = direction === 'inbound' ? user : `${assignee} (Floodline adviser)`
|
|
26
|
+
const date = DateTime.fromJSDate(new Date(createdAt)).setZone('Europe/London').toFormat('HH:mm:ss a, dd LLLL yyyy')
|
|
27
|
+
|
|
28
|
+
string += `[${date}] ${author}: \n${text}\n\n`
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
string = string.replace(/<a\b[^>]*>/i, '').replace(/<\/a>/i, '')
|
|
32
|
+
|
|
33
|
+
return encodeURIComponent(string)
|
|
34
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import React, { createContext, useEffect, useReducer, useMemo } from 'react'
|
|
2
|
+
import { ChatEvent } from '@nice-devone/nice-cxone-chat-web-sdk'
|
|
3
|
+
|
|
4
|
+
import { messageNotification } from '../lib/message-notification.js'
|
|
5
|
+
|
|
6
|
+
import { initialState, reducer } from './reducer.js'
|
|
7
|
+
import { CUSTOMER_ID_STORAGE_KEY, THREAD_ID_STORAGE_KEY, SETTINGS_STORAGE_KEY } from './constants.js'
|
|
8
|
+
|
|
9
|
+
export const AppContext = createContext(initialState)
|
|
10
|
+
|
|
11
|
+
export const AppProvider = ({ sdk, availability, options, children }) => {
|
|
12
|
+
const [state, dispatch] = useReducer(reducer, initialState)
|
|
13
|
+
|
|
14
|
+
const playSound = messageNotification(options.audioUrl)
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* SDK event handlers
|
|
18
|
+
*/
|
|
19
|
+
const onLiveChatRecovered = e => {
|
|
20
|
+
dispatch({ type: 'SET_AGENT', payload: e.detail.data.inboxAssignee })
|
|
21
|
+
dispatch({ type: 'SET_AGENT_STATUS', payload: e.detail.data.contact.status })
|
|
22
|
+
dispatch({ type: 'SET_UNSEEN_COUNT', payload: e.detail.data.contact.customerStatistics.unseenMessagesCount })
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const onAssignedAgentChanged = e => {
|
|
26
|
+
dispatch({ type: 'SET_AGENT', payload: e.detail.data.inboxAssignee })
|
|
27
|
+
dispatch({ type: 'SET_AGENT_STATUS', payload: e.detail.data.case.status })
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const onAgentTypingStarted = () => {
|
|
31
|
+
dispatch({ type: 'SET_AGENT_TYPING', payload: true })
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const onAgentTypingEnded = () => {
|
|
35
|
+
dispatch({ type: 'SET_AGENT_TYPING', payload: false })
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const onMessageCreated = e => {
|
|
39
|
+
dispatch({ type: 'SET_MESSAGE', payload: e.detail.data.message })
|
|
40
|
+
dispatch({ type: 'SET_AGENT_STATUS', payload: e.detail.data.case.status })
|
|
41
|
+
dispatch({ type: 'SET_UNSEEN_COUNT', payload: e.detail.data.case.customerStatistics.unseenMessagesCount })
|
|
42
|
+
|
|
43
|
+
const isAudioOn = JSON.parse(window.localStorage.getItem(SETTINGS_STORAGE_KEY)).audio
|
|
44
|
+
|
|
45
|
+
if (isAudioOn && e.detail.data.message.direction === 'outbound') {
|
|
46
|
+
playSound()
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const onContactStatusChanged = e => {
|
|
51
|
+
dispatch({ type: 'SET_AGENT_STATUS', payload: e.detail.data.case.status })
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const onMatchMedia = e => {
|
|
55
|
+
dispatch({ type: 'TOGGLE_IS_MOBILE', payload: e.matches })
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const onKeydown = () => {
|
|
59
|
+
dispatch({ type: 'TOGGLE_IS_KEYBOARD', payload: true })
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const onPointerdown = () => {
|
|
63
|
+
dispatch({ type: 'TOGGLE_IS_KEYBOARD', payload: false })
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
sdk.onChatEvent(ChatEvent.LIVECHAT_RECOVERED, onLiveChatRecovered)
|
|
68
|
+
sdk.onChatEvent(ChatEvent.MESSAGE_CREATED, onMessageCreated)
|
|
69
|
+
sdk.onChatEvent(ChatEvent.AGENT_TYPING_STARTED, onAgentTypingStarted)
|
|
70
|
+
sdk.onChatEvent(ChatEvent.AGENT_TYPING_ENDED, onAgentTypingEnded)
|
|
71
|
+
sdk.onChatEvent(ChatEvent.ASSIGNED_AGENT_CHANGED, onAssignedAgentChanged)
|
|
72
|
+
sdk.onChatEvent(ChatEvent.CONTACT_STATUS_CHANGED, onContactStatusChanged)
|
|
73
|
+
// We need to know if it is a mobile and if it is a keyboard interaction
|
|
74
|
+
window.matchMedia('(max-width: 640px)').addEventListener('change', onMatchMedia)
|
|
75
|
+
window.addEventListener('keydown', onKeydown)
|
|
76
|
+
window.addEventListener('pointerdown', onPointerdown)
|
|
77
|
+
// Tidying up
|
|
78
|
+
return () => {
|
|
79
|
+
window.removeEventListener('change', onMatchMedia)
|
|
80
|
+
window.removeEventListener('keydown', onKeydown)
|
|
81
|
+
window.removeEventListener('pointerdown', onPointerdown)
|
|
82
|
+
}
|
|
83
|
+
}, [sdk])
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Initialize customerId, threadId and whether the webchat should be open
|
|
87
|
+
*/
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
dispatch({ type: 'SET_AVAILABILITY', payload: availability })
|
|
90
|
+
|
|
91
|
+
setCustomerId(window.localStorage.getItem(CUSTOMER_ID_STORAGE_KEY))
|
|
92
|
+
setThreadId(window.localStorage.getItem(THREAD_ID_STORAGE_KEY))
|
|
93
|
+
setSettings(JSON.parse(window.localStorage.getItem(SETTINGS_STORAGE_KEY)) || state.settings)
|
|
94
|
+
}, [])
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Set browser history on start chat click
|
|
98
|
+
*/
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
if (window.location.hash === '#webchat') {
|
|
101
|
+
setChatVisibility(true)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const onBrowserNavigation = window.addEventListener('popstate', () => {
|
|
105
|
+
if (window.location.hash === '#webchat') {
|
|
106
|
+
setChatVisibility(true)
|
|
107
|
+
} else {
|
|
108
|
+
setChatVisibility(false)
|
|
109
|
+
}
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
return () => {
|
|
113
|
+
window.removeEventListener('popstate', onBrowserNavigation)
|
|
114
|
+
}
|
|
115
|
+
}, [])
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* State update functions
|
|
119
|
+
*/
|
|
120
|
+
const setChatVisibility = payload => {
|
|
121
|
+
dispatch({ type: 'SET_CHAT_VISIBILITY', payload })
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const setCustomerId = customerId => {
|
|
125
|
+
dispatch({ type: 'SET_CUSTOMER_ID', payload: customerId })
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const setThreadId = threadId => {
|
|
129
|
+
dispatch({ type: 'SET_THREAD_ID', payload: threadId })
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const setThread = thread => {
|
|
133
|
+
dispatch({ type: 'SET_THREAD', payload: thread })
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const setMessages = messages => {
|
|
137
|
+
dispatch({ type: 'SET_MESSAGES', payload: messages })
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const setSettings = data => {
|
|
141
|
+
dispatch({ type: 'SET_SETTINGS', payload: data })
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const setUnseenCount = unseenCount => {
|
|
145
|
+
dispatch({ type: 'SET_UNSEEN_COUNT', payload: unseenCount })
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const setInstigatorId = id => {
|
|
149
|
+
dispatch({ type: 'SET_INSTIGATOR_ID', payload: id })
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Application-wide state and state functions
|
|
154
|
+
*/
|
|
155
|
+
const store = useMemo(() => ({
|
|
156
|
+
...state,
|
|
157
|
+
sdk,
|
|
158
|
+
setSettings,
|
|
159
|
+
setCustomerId,
|
|
160
|
+
setThreadId,
|
|
161
|
+
setThread,
|
|
162
|
+
setMessages,
|
|
163
|
+
setUnseenCount,
|
|
164
|
+
setChatVisibility,
|
|
165
|
+
setInstigatorId,
|
|
166
|
+
onLiveChatRecovered,
|
|
167
|
+
onAssignedAgentChanged,
|
|
168
|
+
onAgentTypingStarted,
|
|
169
|
+
onAgentTypingEnded,
|
|
170
|
+
onMessageCreated,
|
|
171
|
+
onContactStatusChanged
|
|
172
|
+
}))
|
|
173
|
+
|
|
174
|
+
return (
|
|
175
|
+
<AppContext.Provider value={store}>
|
|
176
|
+
{children}
|
|
177
|
+
</AppContext.Provider>
|
|
178
|
+
)
|
|
179
|
+
}
|