-
Notifications
You must be signed in to change notification settings - Fork 0
/
NostoProvider.tsx
220 lines (202 loc) · 7.06 KB
/
NostoProvider.tsx
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
import React, { useEffect, isValidElement, useState, useRef } from "react"
import { NostoContext } from "./context"
import { createRoot, Root } from "react-dom/client"
import { NostoClient, Recommendation } from "../types"
/**
* @group Components
*/
export interface NostoProviderProps {
/**
* Indicates merchant id
*/
account: string
/**
* Indicates currency
*/
currentVariation?: string
/**
* Indicates an url of a server
*/
host?: string
children: React.ReactElement
/**
* Indicates if merchant uses multiple currencies
*/
multiCurrency?: boolean
/**
* Recommendation component which holds nostoRecommendation object
*/
recommendationComponent?: React.ReactElement<{
nostoRecommendation: Recommendation
}>
/**
* Enables Shopify markets with language and market id
*/
shopifyMarkets?: {
language?: string
marketId?: string | number
}
}
/**
* This widget is what we call the Nosto root widget, which is responsible for adding the actual Nosto script and the JS API stub.
* This widget wraps all other React Nosto widgets.
*
* ```
* <NostoProvider account="your-nosto-account-id" recommendationComponent={<NostoSlot />}>
* <App />
* </NostoProvider>
* ```
*
* **Note:** the component also accepts a prop to configure the host `host="connect.nosto.com"`.
* In advanced use-cases, the need to configure the host may surface.
*
* In order to implement client-side rendering, the requires a designated component to render the recommendations provided by Nosto.
* This component should be capable of processing the JSON response received from our backend.
* Notice the `recommendationComponent` prop passed to `<NostoProvider>` above.
*
* Learn more [here](https://github.com/Nosto/shopify-hydrogen/blob/main/README.md#client-side-rendering-for-recommendations) and see a [live example](https://github.com/Nosto/shopify-hydrogen-demo) on our demo store.
*
* @group Components
*/
export default function NostoProvider(props: NostoProviderProps) {
const {
account,
multiCurrency = false,
host,
children,
recommendationComponent,
shopifyMarkets,
} = props
const [clientScriptLoadedState, setClientScriptLoadedState] = React.useState(false)
const clientScriptLoaded = React.useMemo(() => clientScriptLoadedState, [clientScriptLoadedState])
// Pass currentVariation as empty string if multiCurrency is disabled
const currentVariation = multiCurrency ? props.currentVariation : ""
// Set responseMode for loading campaigns:
const responseMode = isValidElement(recommendationComponent) ? "JSON_ORIGINAL" : "HTML"
// RecommendationComponent for client-side rendering:
function RecommendationComponentWrapper(props: { nostoRecommendation: Recommendation }) {
return React.cloneElement(recommendationComponent!, {
// eslint-disable-next-line react/prop-types
nostoRecommendation: props.nostoRecommendation,
})
}
// custom hook for rendering campaigns (CSR/SSR):
const [pageType, setPageType] = useState("")
function useRenderCampaigns(type: string = "") {
const placementRefs = useRef<Record<string, Root>>({})
useEffect(() => {
if (pageType !== type) {
setPageType(type)
}
}, [])
const pageTypeUpdated = type === pageType
function renderCampaigns(
data: {
recommendations: Record<string, Recommendation>
campaigns: {
recommendations: Record<string, Recommendation>
}
},
api: NostoClient
) {
if (responseMode == "HTML") {
// inject content campaigns as usual:
api.placements.injectCampaigns(data.recommendations)
} else {
// render recommendation component into placements:
const recommendations = data.campaigns.recommendations
for (const key in recommendations) {
const recommendation = recommendations[key]
const placementSelector = "#" + key
const placement = () => document.querySelector(placementSelector)
if (placement()) {
if (!placementRefs.current[key]) placementRefs.current[key] = createRoot(placement()!)
const root = placementRefs.current[key]!
root.render(
<RecommendationComponentWrapper
nostoRecommendation={recommendation}
></RecommendationComponentWrapper>
)
}
}
}
}
return { renderCampaigns, pageTypeUpdated }
}
useEffect(() => {
if (!window.nostojs) {
window.nostojs = (cb: (api: NostoClient) => void) => {
(window.nostojs.q = window.nostojs.q || []).push(cb)
}
window.nostojs(api => api.setAutoLoad(false))
}
if (!document.querySelectorAll("[nosto-client-script]").length && !shopifyMarkets) {
const script = document.createElement("script")
script.type = "text/javascript"
script.src = "//" + (host || "connect.nosto.com") + "/include/" + account
script.async = true
script.setAttribute("nosto-client-script", "")
script.onload = () => {
if (typeof jest !== "undefined") {
window.nosto?.reload({
site: "localhost",
})
}
setClientScriptLoadedState(true)
}
document.body.appendChild(script)
}
// Enable Shopify markets functionality:
if (shopifyMarkets) {
const existingScript = document.querySelector("[nosto-client-script]")
const nostoSandbox = document.querySelector("#nosto-sandbox")
if (
!existingScript ||
existingScript?.getAttribute("nosto-language") !== shopifyMarkets?.language ||
existingScript?.getAttribute("nosto-market-id") !== shopifyMarkets?.marketId
) {
if (clientScriptLoadedState) {
setClientScriptLoadedState(false)
}
existingScript?.parentNode?.removeChild(existingScript)
nostoSandbox?.parentNode?.removeChild(nostoSandbox)
const script = document.createElement("script")
script.type = "text/javascript"
script.src =
"//" +
(host || "connect.nosto.com") +
`/script/shopify/market/nosto.js?merchant=${account}&market=${
shopifyMarkets.marketId || ""
}&locale=${shopifyMarkets?.language?.toLowerCase() || ""}`
script.async = true
script.setAttribute("nosto-client-script", "")
script.setAttribute("nosto-language", shopifyMarkets?.language || "")
script.setAttribute("nosto-market-id", String(shopifyMarkets?.marketId))
script.onload = () => {
if (typeof jest !== "undefined") {
window.nosto?.reload({
site: "localhost",
})
}
setClientScriptLoadedState(true)
}
document.body.appendChild(script)
}
}
}, [clientScriptLoadedState, shopifyMarkets])
return (
<NostoContext.Provider
value={{
account,
clientScriptLoaded,
currentVariation,
responseMode,
recommendationComponent,
useRenderCampaigns,
pageType,
}}
>
{children}
</NostoContext.Provider>
)
}