-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathindex.jsx
More file actions
157 lines (135 loc) · 5.55 KB
/
index.jsx
File metadata and controls
157 lines (135 loc) · 5.55 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
/*
* 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} 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 {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'
/**
* 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
}
const commerceApiConfig = locals.appConfig.commerceAPI
const appOrigin = useAppOrigin()
const passwordlessCallback = locals.appConfig.login?.passwordless?.callbackURI
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={`${appOrigin}/callback`}
passwordlessLoginCallbackURI={passwordlessCallback}
proxy={`${appOrigin}${commerceApiConfig.proxyPath}`}
headers={headers}
// Uncomment 'enablePWAKitPrivateClient' to use SLAS private client login flows.
// Make sure to also enable useSLASPrivateClient in ssr.js when enabling this setting.
// enablePWAKitPrivateClient={true}
logger={createLogger({packageName: 'commerce-sdk-react'})}
>
<MultiSiteProvider site={locals.site} locale={locals.locale} buildUrl={locals.buildUrl}>
<ChakraProvider theme={theme}>{children}</ChakraProvider>
</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)