-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathshopper-agent-utils.js
More file actions
57 lines (52 loc) · 1.67 KB
/
shopper-agent-utils.js
File metadata and controls
57 lines (52 loc) · 1.67 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
/*
* Copyright (c) 2025, Salesforce, 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
*/
const onClient = typeof window !== 'undefined'
/**
* Launch the chat using the embedded service bootstrap API
*
* When the floating chat button is hidden (hideChatButtonOnLoad=true), this function
* first shows the chat button via utilAPI.showChatButton() before launching the chat,
* ensuring the chat window opens correctly.
*
* @function launchChat
* @returns {void}
*/
export function launchChat() {
if (!onClient) return
try {
const utilAPI = window.embeddedservice_bootstrap?.utilAPI
if (!utilAPI) return
const hideChatButtonOnLoad =
window.embeddedservice_bootstrap?.settings?.hideChatButtonOnLoad === true
if (hideChatButtonOnLoad && typeof utilAPI.showChatButton === 'function') {
utilAPI.showChatButton()
}
if (typeof utilAPI.launchChat === 'function') {
utilAPI.launchChat()
}
} catch (error) {
console.error('Shopper Agent: Error launching chat', error)
}
}
/**
* Open the shopper agent chat window
*
* Programmatically opens the embedded messaging widget by finding and clicking
* the embedded service chat button. This function can be called from custom
* UI elements like header buttons.
*
* @function openShopperAgent
* @returns {void}
*/
export function openShopperAgent() {
if (!onClient) return
try {
launchChat()
} catch (error) {
console.error('Shopper Agent: Error opening agent', error)
}
}