-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.jsx
More file actions
197 lines (172 loc) · 7.39 KB
/
index.jsx
File metadata and controls
197 lines (172 loc) · 7.39 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
/*
* 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
*/
/*
* Developer note! When updating this file, make sure to also update the
* _app-config template files in pwa-kit-create-app.
*
* In the pwa-kit-create-app, the templates are found under:
* - assets/bootstrap/js/overrides/app/components/_app-config
* - assets/templates/@salesforce/retail-react-app/app/components/_app-config
*/
import React from 'react'
import PropTypes from 'prop-types'
import {ChakraProvider} from '@salesforce/retail-react-app/app/components/shared/ui'
// Removes focus for non-keyboard interactions for the whole application
import 'focus-visible/dist/focus-visible'
import theme from '@salesforce/retail-react-app/app/theme'
import {MultiSiteProvider, StoreLocatorProvider} from '@salesforce/retail-react-app/app/contexts'
import {useAppOrigin} from '@salesforce/retail-react-app/app/hooks/use-app-origin'
import {
resolveSiteFromUrl,
resolveLocaleFromUrl
} from '@salesforce/retail-react-app/app/utils/site-utils'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
import {
getEnvBasePath,
slasPrivateProxyPath
} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
import {buildAbsoluteUrl, createUrlTemplate} from '@salesforce/retail-react-app/app/utils/url'
import createLogger from '@salesforce/pwa-kit-runtime/utils/logger-factory'
import {CommerceApiProvider} from '@salesforce/commerce-sdk-react'
import {withReactQuery} from '@salesforce/pwa-kit-react-sdk/ssr/universal/components/with-react-query'
import {useCorrelationId} from '@salesforce/pwa-kit-react-sdk/ssr/universal/hooks'
import {ReactQueryDevtools} from '@tanstack/react-query-devtools'
import {generateSfdcUserAgent} from '@salesforce/retail-react-app/app/utils/sfdc-user-agent-utils'
import {
DEFAULT_DNT_STATE,
STORE_LOCATOR_RADIUS,
STORE_LOCATOR_RADIUS_UNIT,
STORE_LOCATOR_DEFAULT_COUNTRY,
STORE_LOCATOR_DEFAULT_COUNTRY_CODE,
STORE_LOCATOR_DEFAULT_POSTAL_CODE,
STORE_LOCATOR_DEFAULT_PAGE_SIZE,
STORE_LOCATOR_SUPPORTED_COUNTRIES
} from '@salesforce/retail-react-app/app/constants'
const sfdcUserAgent = generateSfdcUserAgent()
/**
* Use the AppConfig component to inject extra arguments into the getProps
* methods for all Route Components in the app – typically you'd want to do this
* to inject a connector instance that can be used in all Pages.
*
* You can also use the AppConfig to configure a state-management library such
* as Redux, or Mobx, if you like.
*/
const AppConfig = ({children, locals = {}}) => {
const {correlationId} = useCorrelationId()
const headers = {
'correlation-id': correlationId,
sfdc_user_agent: sfdcUserAgent
}
const commerceApiConfig = locals.appConfig.commerceAPI
const appOrigin = useAppOrigin()
const passwordlessCallback = locals.appConfig.login?.passwordless?.callbackURI
const storeLocatorConfig = {
radius: STORE_LOCATOR_RADIUS,
radiusUnit: STORE_LOCATOR_RADIUS_UNIT,
defaultCountry: STORE_LOCATOR_DEFAULT_COUNTRY,
defaultCountryCode: STORE_LOCATOR_DEFAULT_COUNTRY_CODE,
defaultPostalCode: STORE_LOCATOR_DEFAULT_POSTAL_CODE,
defaultPageSize: STORE_LOCATOR_DEFAULT_PAGE_SIZE,
supportedCountries: STORE_LOCATOR_SUPPORTED_COUNTRIES
}
// Set absolute uris for CommerceApiProvider proxies and callbacks
const redirectURI = `${appOrigin}${getEnvBasePath()}/callback`
const proxy = `${appOrigin}${getEnvBasePath()}${commerceApiConfig.proxyPath}`
const slasPrivateClientProxyEndpoint = `${appOrigin}${getEnvBasePath()}${slasPrivateProxyPath}`
const passwordlessLoginCallbackURI = buildAbsoluteUrl(appOrigin, passwordlessCallback)
return (
<CommerceApiProvider
shortCode={commerceApiConfig.parameters.shortCode}
clientId={commerceApiConfig.parameters.clientId}
organizationId={commerceApiConfig.parameters.organizationId}
siteId={locals.site?.id}
locale={locals.locale?.id}
currency={locals.locale?.preferredCurrency}
redirectURI={redirectURI}
passwordlessLoginCallbackURI={passwordlessLoginCallbackURI}
proxy={proxy}
headers={headers}
defaultDnt={DEFAULT_DNT_STATE}
// Set 'enablePWAKitPrivateClient' to true to use SLAS private client login flows.
// Make sure to also enable useSLASPrivateClient in ssr.js when enabling this setting.
enablePWAKitPrivateClient={false}
privateClientProxyEndpoint={slasPrivateClientProxyEndpoint}
// Uncomment 'hybridAuthEnabled' if the current site has Hybrid Auth enabled. Do NOT set this flag for hybrid storefronts using Plugin SLAS.
// hybridAuthEnabled={true}
logger={createLogger({packageName: 'commerce-sdk-react'})}
>
<MultiSiteProvider site={locals.site} locale={locals.locale} buildUrl={locals.buildUrl}>
<StoreLocatorProvider config={storeLocatorConfig}>
<ChakraProvider theme={theme}>{children}</ChakraProvider>
</StoreLocatorProvider>
</MultiSiteProvider>
<ReactQueryDevtools />
</CommerceApiProvider>
)
}
AppConfig.restore = (locals = {}) => {
const path =
typeof window === 'undefined'
? locals.originalUrl
: `${window.location.pathname}${window.location.search}`
const site = resolveSiteFromUrl(path)
const locale = resolveLocaleFromUrl(path)
const {app: appConfig} = getConfig()
const apiConfig = {
...appConfig.commerceAPI,
einsteinConfig: appConfig.einsteinAPI
}
apiConfig.parameters.siteId = site.id
locals.buildUrl = createUrlTemplate(appConfig, site.alias || site.id, locale.id)
locals.site = site
locals.locale = locale
locals.appConfig = appConfig
}
AppConfig.freeze = () => undefined
AppConfig.extraGetPropsArgs = (locals = {}) => {
return {
buildUrl: locals.buildUrl,
site: locals.site,
locale: locals.locale
}
}
AppConfig.propTypes = {
children: PropTypes.node,
locals: PropTypes.object
}
const isServerSide = typeof window === 'undefined'
// Recommended settings for PWA-Kit usages.
// NOTE: they will be applied on both server and client side.
// retry is always disabled on server side regardless of the value from the options
const options = {
queryClientConfig: {
defaultOptions: {
queries: {
retry: false,
refetchOnWindowFocus: false,
staleTime: 10 * 1000,
...(isServerSide ? {retryOnMount: false} : {})
},
mutations: {
retry: false
}
}
},
beforeHydrate: (data) => {
const now = Date.now()
// Helper to reset the data timestamp to time of app load.
const updateQueryTimeStamp = ({state}) => {
state.dataUpdatedAt = now
}
// Update serialized mutations and queries to ensure that the cached data is
// considered fresh on first load.
data?.mutations?.forEach(updateQueryTimeStamp)
data?.queries?.forEach(updateQueryTimeStamp)
return data
}
}
export default withReactQuery(AppConfig, options)