-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.jsx
More file actions
461 lines (428 loc) · 17.3 KB
/
index.jsx
File metadata and controls
461 lines (428 loc) · 17.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
* Copyright (c) 2021, 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 React, {useEffect, useMemo, useRef, useState} from 'react'
import {useSearchSuggestions} from '@salesforce/commerce-sdk-react'
import {
Input,
InputGroup,
InputLeftElement,
Popover,
PopoverTrigger,
PopoverContent,
Button,
Box,
Flex,
HStack,
Spinner
} from '@salesforce/retail-react-app/app/components/shared/ui'
import SearchSuggestions from '@salesforce/retail-react-app/app/components/search/partials/search-suggestions'
import {SearchIcon} from '@salesforce/retail-react-app/app/components/icons'
import {
capitalize,
getSessionJSONItem,
setSessionJSONItem
} from '@salesforce/retail-react-app/app/utils/utils'
import useNavigation from '@salesforce/retail-react-app/app/hooks/use-navigation'
import {HideOnDesktop, HideOnMobile} from '@salesforce/retail-react-app/app/components/responsive'
import {FormattedMessage} from 'react-intl'
import debounce from 'lodash/debounce'
import {
RECENT_SEARCH_KEY,
RECENT_SEARCH_LIMIT,
RECENT_SEARCH_MIN_LENGTH
} from '@salesforce/retail-react-app/app/constants'
import {
productUrlBuilder,
searchUrlBuilder,
categoryUrlBuilder
} from '@salesforce/retail-react-app/app/utils/url'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
import {getCommerceAgentConfig} from '@salesforce/retail-react-app/app/utils/config-utils'
import {useUsid} from '@salesforce/commerce-sdk-react'
import {useLocation} from 'react-router-dom'
import useRefreshToken from '@salesforce/retail-react-app/app/hooks/use-refresh-token'
import useMultiSite from '@salesforce/retail-react-app/app/hooks/use-multi-site'
import {useAppOrigin} from '@salesforce/retail-react-app/app/hooks/use-app-origin'
import {normalizeLocaleToSalesforce} from '@salesforce/retail-react-app/app/hooks/use-miaw'
const onClient = typeof window !== 'undefined'
function isAskAgentOnSearchEnabled(enabled, askAgentOnSearch) {
return enabled === 'true' && askAgentOnSearch === 'true' && onClient
}
const formatSuggestions = (searchSuggestions) => {
return {
categorySuggestions: searchSuggestions?.categorySuggestions?.categories?.map(
(suggestion) => {
return {
type: 'category',
id: suggestion.id,
link: categoryUrlBuilder({id: suggestion.id}),
name: capitalize(suggestion.name),
image: suggestion.image?.disBaseLink,
parentCategoryName: suggestion.parentCategoryName
}
}
),
productSuggestions: searchSuggestions?.productSuggestions?.products?.map((product) => {
return {
type: 'product',
currency: product.currency,
price: product.price,
productId: product.productId,
name: capitalize(product.productName),
link: productUrlBuilder({id: product.productId}),
image: product.image?.disBaseLink
}
}),
brandSuggestions: searchSuggestions?.brandSuggestions?.suggestedPhrases?.map((brand) => {
// Init cap the brand name
return {
type: 'brand',
name: capitalize(brand.phrase),
link: searchUrlBuilder(brand.phrase)
}
}),
phraseSuggestions: searchSuggestions?.productSuggestions?.suggestedPhrases?.map(
(phrase) => {
return {
type: 'phrase',
name: phrase.phrase,
link: searchUrlBuilder(phrase.phrase),
exactMatch: phrase.exactMatch
}
}
),
// Einstein suggestions for popular and recent searches
popularSearchSuggestions:
searchSuggestions?.einsteinSuggestedPhrases?.popularSearchPhrases?.map((phrase) => {
return {
type: 'popular',
name: phrase.phrase,
link: searchUrlBuilder(phrase.phrase),
exactMatch: phrase.exactMatch
}
}),
recentSearchSuggestions:
searchSuggestions?.einsteinSuggestedPhrases?.recentSearchPhrases?.map((phrase) => {
return {
type: 'recent',
name: phrase.phrase,
link: searchUrlBuilder(phrase.phrase),
exactMatch: phrase.exactMatch
}
}),
searchPhrase: searchSuggestions?.searchPhrase
}
}
/**
* The SearchInput component is a stylized
* text input made specifically for use in
* the application header.
* @param {object} props
* @param {object} ref reference to the input element
* @return {React.ReactElement} - SearchInput component
*/
const Search = (props) => {
const config = getConfig()
// Add new hooks for chat functionality
const {locale, siteId, commerceOrgId, buildUrl} = useMultiSite()
const {usid} = useUsid()
const refreshToken = useRefreshToken()
const location = useLocation()
const appOrigin = useAppOrigin()
const sfLanguage = normalizeLocaleToSalesforce(locale.id)
const commerceAgentConfig = useMemo(
() => getCommerceAgentConfig(),
[config.app?.commerceAgent, config.app?.commerceAgentSettings]
)
const askAgentOnSearchEnabled = useMemo(
() =>
isAskAgentOnSearchEnabled(
commerceAgentConfig?.enabled,
commerceAgentConfig?.askAgentOnSearch
),
[commerceAgentConfig]
)
const enableAgentFromSearchSuggestions = commerceAgentConfig?.enableAgentFromSearchSuggestions
const [isOpen, setIsOpen] = useState(false)
const [searchQuery, setSearchQuery] = useState('')
const navigate = useNavigation()
const searchSuggestion = useSearchSuggestions(
{
parameters: {
q: searchQuery,
expand: 'images,prices',
includeEinsteinSuggestedPhrases: true
}
},
{
enabled: searchQuery?.length >= RECENT_SEARCH_MIN_LENGTH
}
)
const searchInputRef = useRef()
const miawChatRef = useRef({
newChatLaunched: false,
hasFired: false
})
const recentSearches = getSessionJSONItem(RECENT_SEARCH_KEY)
const searchSuggestions = useMemo(
() => formatSuggestions(searchSuggestion.data),
[searchSuggestion]
)
// check if popover should open if we have suggestions
useEffect(() => {
shouldOpenPopover()
}, [searchQuery, searchSuggestion.data])
const searchSuggestionsAvailable =
searchSuggestions &&
(searchSuggestions?.categorySuggestions?.length ||
searchSuggestions?.phraseSuggestions?.length ||
searchSuggestions?.popularSearchSuggestions?.length ||
searchSuggestions?.recentSearchSuggestions?.length)
const saveRecentSearch = (searchText) => {
// Get recent searches or an empty array if undefined.
let searches = getSessionJSONItem(RECENT_SEARCH_KEY) || []
// Check if term is already in the saved searches
searches = searches.filter((savedSearchTerm) => {
return searchText.toLowerCase() !== savedSearchTerm.toLowerCase()
})
// Create a new array consisting of the search text and up to 4 other resent searches.
// I'm assuming the order is newest to oldest.
searches = [searchText, ...searches].slice(0, RECENT_SEARCH_LIMIT)
// Replace the save resent search with the updated value.
setSessionJSONItem(RECENT_SEARCH_KEY, searches)
}
const debouncedSearch = debounce((input) => {
debouncedSearch.cancel()
setSearchQuery(input)
}, 300)
const onSearchChange = async (e) => {
const input = e.target.value
if (input.length >= RECENT_SEARCH_MIN_LENGTH) {
debouncedSearch(input)
} else {
setSearchQuery('')
}
}
const clearInput = () => {
searchInputRef.current.blur()
setIsOpen(false)
}
// Function to set pre-chat fields only when launching a new chat session
const setPrechatFieldsForNewSession = () => {
// Only set pre-chat fields if this is a new chat launch (not already launched)
if (!miawChatRef.current.newChatLaunched) {
if (window.embeddedservice_bootstrap?.prechatAPI) {
window.embeddedservice_bootstrap.prechatAPI.setHiddenPrechatFields({
SiteId: siteId,
Locale: locale.id,
OrganizationId: commerceOrgId,
UsId: usid,
IsCartMgmtSupported: 'true',
RefreshToken: refreshToken,
Currency: locale.preferredCurrency,
Language: sfLanguage,
DomainUrl: `${appOrigin}${buildUrl(location.pathname)}`
})
}
}
}
useEffect(() => {
const handleEmbeddedMessageSent = (e) => {
if (!miawChatRef.current.hasFired && miawChatRef.current.newChatLaunched) {
if (
e.detail.conversationEntry?.sender?.role === 'Chatbot' &&
searchInputRef?.current?.value &&
window.embeddedservice_bootstrap?.utilAPI
) {
miawChatRef.current.hasFired = true
setTimeout(() => {
window.embeddedservice_bootstrap.utilAPI.sendTextMessage(
searchInputRef.current.value.trim()
)
}, 500)
}
}
}
window.addEventListener('onEmbeddedMessageSent', handleEmbeddedMessageSent)
return () => {
// Clean up
window.removeEventListener('onEmbeddedMessageSent', handleEmbeddedMessageSent)
}
}, [])
const launchChat = () => {
// Set pre-chat fields only for new sessions
setPrechatFieldsForNewSession()
if (window.embeddedservice_bootstrap?.settings) {
window.embeddedservice_bootstrap.settings.disableStreamingResponses = true
window.embeddedservice_bootstrap.settings.enableUserInputForConversationWithBot = false
}
window.embeddedservice_bootstrap?.utilAPI
?.launchChat()
?.then((successMessage) => {
/* TODO: With the Salesforce Winter '26 release, we will be able to use the
* onEmbeddedMessagingFirstBotMessageSent event instead, and get rid of this logic. */
if (successMessage.includes('Successfully initialized the messaging client')) {
miawChatRef.current.hasFired = false //We want the logic in onEmbeddedMessageSent to happen once per new conversation
miawChatRef.current.newChatLaunched = true
}
})
?.catch((err) => {
console.error('launchChat error', err)
})
}
const onSubmitSearch = (e) => {
e.preventDefault()
// Avoid blank spaces to be searched
let searchText = searchInputRef.current.value.trim()
// Avoid empty string searches
if (searchText.length < 1) {
return
}
if (askAgentOnSearchEnabled && window.embeddedservice_bootstrap?.utilAPI) {
// Add a 500ms delay before sending the message to ensure the experience isn't jarring to the user
setTimeout(() => {
window.embeddedservice_bootstrap.utilAPI
.sendTextMessage(searchText)
.catch((err) => {
console.error(err)
if (
err.includes(
'invoke API before the onEmbeddedMessagingConversationOpened event is fired'
)
) {
launchChat()
}
})
}, 500)
}
saveRecentSearch(searchText)
clearInput()
navigate(searchUrlBuilder(searchText))
}
const closeAndNavigate = (link) => {
if (!link) {
clearInput()
setIsOpen(false)
} else {
clearInput()
setIsOpen(false)
navigate(link)
}
}
const onAskAssistantClick = () => {
launchChat()
clearInput()
}
const shouldOpenPopover = () => {
// As per design we only want to show the popover if the input is focused and we have recent searches saved
// or we have search suggestions available and have inputed some text (empty text in this scenario should show recent searches)
if (
(document.activeElement.id === 'search-input' && recentSearches?.length > 0) ||
(searchSuggestionsAvailable && searchInputRef.current?.value?.length > 0)
) {
setIsOpen(true)
} else {
setIsOpen(false)
}
}
const onSearchInputChange = (e) => {
onSearchChange(e)
shouldOpenPopover()
}
return (
<Box>
<Popover isOpen={isOpen} isLazy initialFocusRef={searchInputRef}>
<PopoverTrigger asChild>
<form onSubmit={onSubmitSearch}>
<HStack>
<InputGroup>
<InputLeftElement pointerEvents="none">
<SearchIcon />
</InputLeftElement>
<Input
autoComplete="off"
id="search-input"
onChange={(e) => onSearchInputChange(e)}
onFocus={() => shouldOpenPopover()}
onBlur={() => setIsOpen(false)}
type="search"
ref={searchInputRef}
{...props}
variant="filled"
/>
</InputGroup>
<HideOnDesktop>
<Button
display={isOpen ? 'block' : 'none'}
variant="link"
size="sm"
onMouseDown={() => closeAndNavigate(false)}
>
<FormattedMessage
defaultMessage="Cancel"
id="search.action.cancel"
/>
</Button>
</HideOnDesktop>
</HStack>
</form>
</PopoverTrigger>
<HideOnMobile>
<PopoverContent
data-testid="sf-suggestion-popover"
width="100vw"
maxWidth="100vw"
left={0}
right={0}
marginLeft={0}
marginRight={0}
>
<SearchSuggestions
closeAndNavigate={closeAndNavigate}
recentSearches={recentSearches}
searchSuggestions={searchSuggestions}
enableAgentFromSearchSuggestions={enableAgentFromSearchSuggestions}
onAskAssistantClick={onAskAssistantClick}
/>
</PopoverContent>
</HideOnMobile>
</Popover>
<HideOnDesktop>
<Flex
display={isOpen || searchInputRef?.value?.length > 0 ? 'block' : 'none'}
postion="absolute"
background="white"
left={0}
right={0}
height="100vh"
>
{searchSuggestion.isLoading ? (
<Spinner
position="absolute"
top="50%"
left="50%"
opacity={0.85}
color="blue.600"
zIndex="9999"
margin="-25px 0 0 -25px"
/>
) : (
<SearchSuggestions
closeAndNavigate={closeAndNavigate}
recentSearches={recentSearches}
searchSuggestions={searchSuggestions}
enableAgentFromSearchSuggestions={enableAgentFromSearchSuggestions}
onAskAssistantClick={onAskAssistantClick}
/>
)}
</Flex>
</HideOnDesktop>
</Box>
)
}
Search.displayName = 'SearchInput'
export default Search