forked from SalesforceCommerceCloud/pwa-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
141 lines (121 loc) · 4.3 KB
/
index.jsx
File metadata and controls
141 lines (121 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {useEffect, useState} from 'react'
import useScript from '@salesforce/retail-react-app/app/hooks/use-script'
import {useUsid} from '@salesforce/commerce-sdk-react'
const onClient = typeof window !== 'undefined'
// Function to initialize embedded messaging
const initEmbeddedMessaging = (
orgId,
embeddedServiceDeploymentName,
embeddedServiceDeploymentUrl,
scrt2Url
) => {
try {
if (
onClient &&
window.embeddedservice_bootstrap &&
window.embeddedservice_bootstrap.settings
) {
window.embeddedservice_bootstrap.settings.language = 'en_US'
window.embeddedservice_bootstrap.init(
orgId,
embeddedServiceDeploymentName,
embeddedServiceDeploymentUrl,
{
scrt2URL: scrt2Url
}
)
}
} catch (err) {
console.error('Error initializing Embedded Messaging: ', err)
}
}
function useMiaw(
scriptLoadStatus,
orgId,
embeddedServiceDeploymentName,
embeddedServiceDeploymentUrl,
scrt2Url
) {
const [isMiawInitialized, setIsMiawInitialized] = useState(false)
useEffect(() => {
if (scriptLoadStatus.loaded && !scriptLoadStatus.error) {
initEmbeddedMessaging(
orgId,
embeddedServiceDeploymentName,
embeddedServiceDeploymentUrl,
scrt2Url
)
setIsMiawInitialized(true)
}
}, [scriptLoadStatus])
return isMiawInitialized
}
function isEnabled(enabled) {
return enabled === 'true'
}
/**
* ShopperAgent component that initializes and manages the embedded messaging service
* @param {Object} props - Component props
* @param {string} props.commerceAgent - JSON stringified commerce agent settings
* @param {string} props.domainUrl - The domain URL for the embedded messaging script
* @param {string} props.basketId - The basket ID for the embedded messaging script
* @param {string} props.locale - The locale for the embedded messaging script
* @param {function} props.onAgentConversationOpened - The callback function for when the conversation is opened
* @param {function} props.onAgentConversationClosed - The callback function for when the conversation is closed
* @returns {JSX.Element} The ShopperAgent component
*/
const ShopperAgent = ({
commerceAgent,
domainUrl,
basketId,
locale,
onAgentConversationOpened,
onAgentConversationClosed
}) => {
const { enabled, embeddedServiceName, embeddedServiceEndpoint, scriptSourceUrl, scrt2Url, salesforceOrgId, siteId } = JSON.parse(commerceAgent)
if (!onClient || !isEnabled(enabled)) {
return null
}
const {usid} = useUsid();
useEffect(() => {
window.addEventListener('onEmbeddedMessagingReady', (e) => {
window.embeddedservice_bootstrap.prechatAPI.setHiddenPrechatFields({
DomainURL: domainUrl,
SiteId: siteId,
BasketId: basketId,
Locale: locale,
OrganizationId: salesforceOrgId,
UsId: usid
})
})
window.addEventListener('onEmbeddedMessagingConversationClosed', (e) => {
console.error('Error initializing Embedded Messaging: ', e)
})
window.addEventListener('onEmbeddedMessagingConversationOpened', (e) => {
console.log('Conversation opened', e)
})
window.addEventListener('onEmbeddedMessagingConversationEnded', (e) => {
console.log('Conversation ended', e)
})
}, [commerceAgent])
// Load the embedded messaging script
const scriptLoadStatus = useScript(scriptSourceUrl)
// Initialize the embedded messaging service
useMiaw(
scriptLoadStatus,
salesforceOrgId,
embeddedServiceName,
embeddedServiceEndpoint,
scrt2Url
)
// The component doesn't render anything visible
// It's just a wrapper for the embedded messaging service
return null
}
export default ShopperAgent