@nosto/nosto-react 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/README.md +5 -2
  2. package/dist/index.d.ts +519 -0
  3. package/dist/index.es.js +1117 -0
  4. package/dist/index.umd.js +25 -0
  5. package/package.json +20 -13
  6. package/src/components/Nosto404.tsx +47 -0
  7. package/src/components/{Category/index.client.tsx → NostoCategory.tsx} +18 -39
  8. package/src/components/NostoCheckout.tsx +47 -0
  9. package/src/components/{Home/index.client.tsx → NostoHome.tsx} +17 -36
  10. package/src/components/NostoOrder.tsx +55 -0
  11. package/src/components/NostoOther.tsx +46 -0
  12. package/src/components/{Placement/index.client.tsx → NostoPlacement.tsx} +5 -8
  13. package/src/components/{Product/index.client.tsx → NostoProduct.tsx} +37 -81
  14. package/src/components/NostoProvider.tsx +220 -0
  15. package/src/components/{Search/index.client.tsx → NostoSearch.tsx} +18 -39
  16. package/src/components/{Session/index.client.tsx → NostoSession.tsx} +14 -17
  17. package/src/components/context.ts +55 -0
  18. package/src/components/index.ts +14 -0
  19. package/src/index.ts +3 -0
  20. package/src/types.ts +112 -97
  21. package/src/utils/compare.ts +9 -9
  22. package/src/utils/hooks.ts +28 -8
  23. package/src/utils/object.ts +10 -11
  24. package/src/utils/snakeize.ts +11 -11
  25. package/dist/index.es.client.js +0 -660
  26. package/dist/index.umd.client.js +0 -9
  27. package/src/components/Checkout/index.client.tsx +0 -66
  28. package/src/components/Fohofo/index.client.tsx +0 -66
  29. package/src/components/Order/index.client.tsx +0 -72
  30. package/src/components/Other/index.client.tsx +0 -64
  31. package/src/components/Provider/context.client.ts +0 -45
  32. package/src/components/Provider/index.client.tsx +0 -222
  33. package/src/index.client.ts +0 -33
@@ -1,660 +0,0 @@
1
- import React, { createContext, useContext, useEffect, useRef, useMemo, isValidElement, useState } from "react";
2
- import { createRoot } from "react-dom/client";
3
- const NostoContext = createContext({
4
- account: "",
5
- currentVariation: "",
6
- pageType: "",
7
- responseMode: "HTML",
8
- clientScriptLoaded: false,
9
- useRenderCampaigns: () => void 0
10
- });
11
- function useNostoContext() {
12
- const context = useContext(NostoContext);
13
- if (!context) {
14
- throw new Error("No nosto context found");
15
- }
16
- return context;
17
- }
18
- var jsxRuntime = { exports: {} };
19
- var reactJsxRuntime_production_min = {};
20
- /**
21
- * @license React
22
- * react-jsx-runtime.production.min.js
23
- *
24
- * Copyright (c) Facebook, Inc. and its affiliates.
25
- *
26
- * This source code is licensed under the MIT license found in the
27
- * LICENSE file in the root directory of this source tree.
28
- */
29
- var f = React, k = Symbol.for("react.element"), l = Symbol.for("react.fragment"), m = Object.prototype.hasOwnProperty, n = f.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, p = { key: true, ref: true, __self: true, __source: true };
30
- function q(c, a, g) {
31
- var b, d = {}, e = null, h = null;
32
- void 0 !== g && (e = "" + g);
33
- void 0 !== a.key && (e = "" + a.key);
34
- void 0 !== a.ref && (h = a.ref);
35
- for (b in a)
36
- m.call(a, b) && !p.hasOwnProperty(b) && (d[b] = a[b]);
37
- if (c && c.defaultProps)
38
- for (b in a = c.defaultProps, a)
39
- void 0 === d[b] && (d[b] = a[b]);
40
- return { $$typeof: k, type: c, key: e, ref: h, props: d, _owner: n.current };
41
- }
42
- reactJsxRuntime_production_min.Fragment = l;
43
- reactJsxRuntime_production_min.jsx = q;
44
- reactJsxRuntime_production_min.jsxs = q;
45
- {
46
- jsxRuntime.exports = reactJsxRuntime_production_min;
47
- }
48
- const jsx = jsxRuntime.exports.jsx;
49
- const jsxs = jsxRuntime.exports.jsxs;
50
- const Fragment = jsxRuntime.exports.Fragment;
51
- function Nosto404(props) {
52
- const {
53
- clientScriptLoaded,
54
- currentVariation,
55
- responseMode,
56
- recommendationComponent,
57
- useRenderCampaigns
58
- } = useNostoContext();
59
- const {
60
- renderCampaigns,
61
- pageTypeUpdated
62
- } = useRenderCampaigns("404");
63
- useEffect(() => {
64
- if (clientScriptLoaded && pageTypeUpdated) {
65
- window.nostojs((api) => {
66
- api.defaultSession().setVariation(currentVariation).setResponseMode(responseMode).viewNotFound().setPlacements(props.placements || api.placements.getPlacements()).load().then((data) => {
67
- renderCampaigns(data, api);
68
- });
69
- });
70
- }
71
- }, [clientScriptLoaded, currentVariation, recommendationComponent, pageTypeUpdated]);
72
- return /* @__PURE__ */ jsx(Fragment, {
73
- children: /* @__PURE__ */ jsx("div", {
74
- className: "nosto_page_type",
75
- style: {
76
- display: "none"
77
- },
78
- children: "notfound"
79
- })
80
- });
81
- }
82
- function NostoOther(props) {
83
- const {
84
- clientScriptLoaded,
85
- currentVariation,
86
- responseMode,
87
- recommendationComponent,
88
- useRenderCampaigns
89
- } = useNostoContext();
90
- const {
91
- renderCampaigns,
92
- pageTypeUpdated
93
- } = useRenderCampaigns("other");
94
- useEffect(() => {
95
- if (clientScriptLoaded && pageTypeUpdated) {
96
- window.nostojs((api) => {
97
- api.defaultSession().setVariation(currentVariation).setResponseMode(responseMode).viewOther().setPlacements(props.placements || api.placements.getPlacements()).load().then((data) => {
98
- renderCampaigns(data, api);
99
- });
100
- });
101
- }
102
- }, [clientScriptLoaded, currentVariation, recommendationComponent, pageTypeUpdated]);
103
- return /* @__PURE__ */ jsx(Fragment, {
104
- children: /* @__PURE__ */ jsx("div", {
105
- className: "nosto_page_type",
106
- style: {
107
- display: "none"
108
- },
109
- children: "other"
110
- })
111
- });
112
- }
113
- function NostoCheckout(props) {
114
- const {
115
- clientScriptLoaded,
116
- currentVariation,
117
- responseMode,
118
- recommendationComponent,
119
- useRenderCampaigns
120
- } = useNostoContext();
121
- const {
122
- renderCampaigns,
123
- pageTypeUpdated
124
- } = useRenderCampaigns("checkout");
125
- useEffect(() => {
126
- if (clientScriptLoaded && pageTypeUpdated) {
127
- window.nostojs((api) => {
128
- api.defaultSession().setVariation(currentVariation).setResponseMode(responseMode).viewCart().setPlacements(props.placements || api.placements.getPlacements()).load().then((data) => {
129
- renderCampaigns(data, api);
130
- });
131
- });
132
- }
133
- }, [clientScriptLoaded, currentVariation, recommendationComponent, pageTypeUpdated]);
134
- return /* @__PURE__ */ jsx(Fragment, {
135
- children: /* @__PURE__ */ jsx("div", {
136
- className: "nosto_page_type",
137
- style: {
138
- display: "none"
139
- },
140
- children: "cart"
141
- })
142
- });
143
- }
144
- function isPlainObject(value) {
145
- const isObject = (v) => String(v) === "[object Object]";
146
- if (!isObject(value))
147
- return false;
148
- const constructor = value.constructor;
149
- if (constructor === void 0)
150
- return true;
151
- const prototype = constructor.prototype;
152
- if (!isObject(prototype))
153
- return false;
154
- if (!prototype.hasOwnProperty("isPrototypeOf")) {
155
- return false;
156
- }
157
- return true;
158
- }
159
- function deepCompare(a, b) {
160
- if (a === b) {
161
- return true;
162
- }
163
- if (a instanceof Date && b instanceof Date) {
164
- return a.getTime() === b.getTime();
165
- }
166
- if (a instanceof Array && b instanceof Array) {
167
- if (a.length !== b.length) {
168
- return false;
169
- }
170
- return a.every((v, i) => deepCompare(v, b[i]));
171
- }
172
- if (isPlainObject(a) && isPlainObject(b)) {
173
- const entriesA = Object.entries(a);
174
- if (entriesA.length !== Object.keys(b).length) {
175
- return false;
176
- }
177
- return entriesA.every(([k2, v]) => deepCompare(v, b[k2]));
178
- }
179
- return false;
180
- }
181
- function useDeepCompareEffect(callback, dependencies) {
182
- return useEffect(callback, useDeepCompareMemoize(dependencies));
183
- }
184
- function useDeepCompareMemoize(value) {
185
- const ref = useRef(value);
186
- const signalRef = useRef(0);
187
- if (!deepCompare(value, ref.current)) {
188
- ref.current = value;
189
- signalRef.current += 1;
190
- }
191
- return useMemo(() => ref.current, [signalRef.current]);
192
- }
193
- function NostoProduct(props) {
194
- const {
195
- product,
196
- tagging
197
- } = props;
198
- const {
199
- clientScriptLoaded,
200
- currentVariation,
201
- responseMode,
202
- recommendationComponent,
203
- useRenderCampaigns
204
- } = useNostoContext();
205
- const {
206
- renderCampaigns,
207
- pageTypeUpdated
208
- } = useRenderCampaigns("product");
209
- useDeepCompareEffect(() => {
210
- if (clientScriptLoaded && pageTypeUpdated) {
211
- window.nostojs((api) => {
212
- api.defaultSession().setResponseMode(responseMode).viewProduct(product).setPlacements(props.placements || api.placements.getPlacements()).load().then((data) => {
213
- renderCampaigns(data, api);
214
- });
215
- });
216
- }
217
- }, [clientScriptLoaded, currentVariation, product, recommendationComponent, pageTypeUpdated]);
218
- return /* @__PURE__ */ jsxs(Fragment, {
219
- children: [/* @__PURE__ */ jsx("div", {
220
- className: "nosto_page_type",
221
- style: {
222
- display: "none"
223
- },
224
- children: "product"
225
- }), /* @__PURE__ */ jsxs("div", {
226
- className: "nosto_product",
227
- style: {
228
- display: "none"
229
- },
230
- children: [(tagging == null ? void 0 : tagging.variationId) && /* @__PURE__ */ jsx("span", {
231
- className: "variation_id",
232
- children: tagging.variationId
233
- }), product && /* @__PURE__ */ jsx("span", {
234
- className: "product_id",
235
- children: product
236
- }), (tagging == null ? void 0 : tagging.name) && /* @__PURE__ */ jsx("span", {
237
- className: "name",
238
- children: tagging.name
239
- }), (tagging == null ? void 0 : tagging.url) && /* @__PURE__ */ jsx("span", {
240
- className: "url",
241
- children: tagging.url.toString()
242
- }), (tagging == null ? void 0 : tagging.imageUrl) && /* @__PURE__ */ jsx("span", {
243
- className: "image_url",
244
- children: tagging.imageUrl.toString()
245
- }), (tagging == null ? void 0 : tagging.availability) && /* @__PURE__ */ jsx("span", {
246
- className: "availability",
247
- children: tagging.availability
248
- }), (tagging == null ? void 0 : tagging.price) && /* @__PURE__ */ jsx("span", {
249
- className: "price",
250
- children: tagging.price
251
- }), (tagging == null ? void 0 : tagging.listPrice) && /* @__PURE__ */ jsx("span", {
252
- className: "list_price",
253
- children: tagging.listPrice
254
- }), (tagging == null ? void 0 : tagging.priceCurrencyCode) && /* @__PURE__ */ jsx("span", {
255
- className: "price_currency_code",
256
- children: tagging.priceCurrencyCode
257
- }), (tagging == null ? void 0 : tagging.brand) && /* @__PURE__ */ jsx("span", {
258
- className: "brand",
259
- children: tagging.brand
260
- }), (tagging == null ? void 0 : tagging.description) && /* @__PURE__ */ jsx("span", {
261
- className: "description",
262
- children: tagging.description
263
- }), (tagging == null ? void 0 : tagging.googleCategory) && /* @__PURE__ */ jsx("span", {
264
- className: "description",
265
- children: tagging.googleCategory
266
- }), (tagging == null ? void 0 : tagging.condition) && /* @__PURE__ */ jsx("span", {
267
- className: "condition",
268
- children: tagging.condition
269
- }), (tagging == null ? void 0 : tagging.gender) && /* @__PURE__ */ jsx("span", {
270
- className: "gender",
271
- children: tagging.gender
272
- }), (tagging == null ? void 0 : tagging.ageGroup) && /* @__PURE__ */ jsx("span", {
273
- className: "age_group",
274
- children: tagging.ageGroup
275
- }), (tagging == null ? void 0 : tagging.gtin) && /* @__PURE__ */ jsx("span", {
276
- className: "gtin",
277
- children: tagging.gtin
278
- }), (tagging == null ? void 0 : tagging.category) && (tagging == null ? void 0 : tagging.category.map((category, index) => /* @__PURE__ */ jsx("span", {
279
- className: "category",
280
- children: category
281
- }, index))), (tagging == null ? void 0 : tagging.tags1) && tagging.tags1.map((tag, index) => /* @__PURE__ */ jsx("span", {
282
- className: "tag1",
283
- children: tag
284
- }, index)), (tagging == null ? void 0 : tagging.tags2) && tagging.tags2.map((tag, index) => /* @__PURE__ */ jsx("span", {
285
- className: "tag2",
286
- children: tag
287
- }, index)), (tagging == null ? void 0 : tagging.tags3) && tagging.tags3.map((tag, index) => /* @__PURE__ */ jsx("span", {
288
- className: "tag3",
289
- children: tag
290
- }, index)), (tagging == null ? void 0 : tagging.ratingValue) && /* @__PURE__ */ jsx("span", {
291
- className: "rating_value",
292
- children: tagging.ratingValue
293
- }), (tagging == null ? void 0 : tagging.reviewCount) && /* @__PURE__ */ jsx("span", {
294
- className: "review_count",
295
- children: tagging.reviewCount
296
- }), (tagging == null ? void 0 : tagging.alternateImageUrls) && tagging.alternateImageUrls.map((url, index) => /* @__PURE__ */ jsx("span", {
297
- className: "alternate_image_url",
298
- children: url.toString()
299
- }, index)), (tagging == null ? void 0 : tagging.customFields) && Object.keys(tagging.customFields).map((field, index) => tagging.customFields && tagging.customFields[field] && /* @__PURE__ */ jsx("span", {
300
- className: field,
301
- children: tagging.customFields[field]
302
- }, index)), (tagging == null ? void 0 : tagging.skus) && tagging.skus.map((sku, index) => /* @__PURE__ */ jsxs("span", {
303
- className: "nosto_sku",
304
- children: [(sku == null ? void 0 : sku.id) && /* @__PURE__ */ jsx("span", {
305
- className: "product_id",
306
- children: sku.id
307
- }), (sku == null ? void 0 : sku.name) && /* @__PURE__ */ jsx("span", {
308
- className: "name",
309
- children: sku.name
310
- }), (sku == null ? void 0 : sku.price) && /* @__PURE__ */ jsx("span", {
311
- className: "price",
312
- children: sku.price
313
- }), (sku == null ? void 0 : sku.listPrice) && /* @__PURE__ */ jsx("span", {
314
- className: "list_price",
315
- children: sku.listPrice
316
- }), (sku == null ? void 0 : sku.url) && /* @__PURE__ */ jsx("span", {
317
- className: "url",
318
- children: sku.url.toString()
319
- }), (sku == null ? void 0 : sku.imageUrl) && /* @__PURE__ */ jsx("span", {
320
- className: "image_url",
321
- children: sku.imageUrl.toString()
322
- }), (sku == null ? void 0 : sku.gtin) && /* @__PURE__ */ jsx("span", {
323
- className: "gtin",
324
- children: sku.gtin
325
- }), (sku == null ? void 0 : sku.availability) && /* @__PURE__ */ jsx("span", {
326
- className: "availability",
327
- children: sku.availability
328
- }), (sku == null ? void 0 : sku.customFields) && Object.keys(sku.customFields).map((field, index2) => sku.customFields && sku.customFields[field] && /* @__PURE__ */ jsx("span", {
329
- className: field,
330
- children: sku.customFields[field]
331
- }, index2))]
332
- }, index))]
333
- })]
334
- });
335
- }
336
- function NostoCategory(props) {
337
- const {
338
- category
339
- } = props;
340
- const {
341
- clientScriptLoaded,
342
- currentVariation,
343
- responseMode,
344
- recommendationComponent,
345
- useRenderCampaigns
346
- } = useNostoContext();
347
- const {
348
- renderCampaigns,
349
- pageTypeUpdated
350
- } = useRenderCampaigns("home");
351
- useEffect(() => {
352
- if (clientScriptLoaded && pageTypeUpdated) {
353
- window.nostojs((api) => {
354
- api.defaultSession().setVariation(currentVariation).setResponseMode(responseMode).viewCategory(category).setPlacements(props.placements || api.placements.getPlacements()).load().then((data) => {
355
- renderCampaigns(data, api);
356
- });
357
- });
358
- }
359
- }, [clientScriptLoaded, category, currentVariation, recommendationComponent, pageTypeUpdated]);
360
- return /* @__PURE__ */ jsxs(Fragment, {
361
- children: [/* @__PURE__ */ jsx("div", {
362
- className: "nosto_page_type",
363
- style: {
364
- display: "none"
365
- },
366
- children: "category"
367
- }), /* @__PURE__ */ jsx("div", {
368
- className: "nosto_category",
369
- style: {
370
- display: "none"
371
- },
372
- children: category
373
- })]
374
- });
375
- }
376
- function NostoSearch(props) {
377
- const {
378
- query
379
- } = props;
380
- const {
381
- clientScriptLoaded,
382
- currentVariation,
383
- responseMode,
384
- recommendationComponent,
385
- useRenderCampaigns
386
- } = useNostoContext();
387
- const {
388
- renderCampaigns,
389
- pageTypeUpdated
390
- } = useRenderCampaigns("search");
391
- useEffect(() => {
392
- if (clientScriptLoaded && pageTypeUpdated) {
393
- window.nostojs((api) => {
394
- api.defaultSession().setVariation(currentVariation).setResponseMode(responseMode).viewSearch(query).setPlacements(props.placements || api.placements.getPlacements()).load().then((data) => {
395
- renderCampaigns(data, api);
396
- });
397
- });
398
- }
399
- }, [clientScriptLoaded, currentVariation, query, recommendationComponent, pageTypeUpdated]);
400
- return /* @__PURE__ */ jsxs(Fragment, {
401
- children: [/* @__PURE__ */ jsx("div", {
402
- className: "nosto_page_type",
403
- style: {
404
- display: "none"
405
- },
406
- children: "search"
407
- }), /* @__PURE__ */ jsx("div", {
408
- className: "nosto_search",
409
- style: {
410
- display: "none"
411
- },
412
- children: query
413
- })]
414
- });
415
- }
416
- function snakeize(obj) {
417
- if (!obj || typeof obj !== "object") {
418
- return obj;
419
- }
420
- if (isDate(obj) || isRegex(obj)) {
421
- return obj;
422
- }
423
- if (Array.isArray(obj)) {
424
- return obj.map(snakeize);
425
- }
426
- return Object.keys(obj).reduce((acc, key) => {
427
- var camel = key[0].toLowerCase() + key.slice(1).replace(/([A-Z]+)/g, (_, x) => {
428
- return "_" + x.toLowerCase();
429
- });
430
- acc[camel] = snakeize(obj[key]);
431
- return acc;
432
- }, {});
433
- }
434
- function isDate(obj) {
435
- return Object.prototype.toString.call(obj) === "[object Date]";
436
- }
437
- function isRegex(obj) {
438
- return Object.prototype.toString.call(obj) === "[object RegExp]";
439
- }
440
- function NostoOrder(props) {
441
- const {
442
- order
443
- } = props;
444
- const {
445
- clientScriptLoaded,
446
- currentVariation,
447
- responseMode,
448
- recommendationComponent,
449
- useRenderCampaigns
450
- } = useNostoContext();
451
- const {
452
- renderCampaigns,
453
- pageTypeUpdated
454
- } = useRenderCampaigns("order");
455
- useEffect(() => {
456
- if (clientScriptLoaded && pageTypeUpdated) {
457
- window.nostojs((api) => {
458
- api.defaultSession().setVariation(currentVariation).setResponseMode(responseMode).addOrder(snakeize(order)).setPlacements(props.placements || api.placements.getPlacements()).load().then((data) => {
459
- renderCampaigns(data, api);
460
- });
461
- });
462
- }
463
- }, [clientScriptLoaded, currentVariation, recommendationComponent, pageTypeUpdated]);
464
- return /* @__PURE__ */ jsxs(Fragment, {
465
- children: [/* @__PURE__ */ jsx("div", {
466
- className: "nosto_page_type",
467
- style: {
468
- display: "none"
469
- },
470
- children: "order"
471
- }), /* @__PURE__ */ jsx("div", {
472
- className: "nosto_order",
473
- style: {
474
- display: "none"
475
- },
476
- children: order.purchase.number
477
- })]
478
- });
479
- }
480
- function NostoHome(props) {
481
- const {
482
- clientScriptLoaded,
483
- currentVariation,
484
- responseMode,
485
- recommendationComponent,
486
- useRenderCampaigns
487
- } = useNostoContext();
488
- const {
489
- renderCampaigns,
490
- pageTypeUpdated
491
- } = useRenderCampaigns("home");
492
- useEffect(() => {
493
- if (clientScriptLoaded && pageTypeUpdated) {
494
- window.nostojs((api) => {
495
- api.defaultSession().setVariation(currentVariation).setResponseMode(responseMode).viewFrontPage().setPlacements(props.placements || api.placements.getPlacements()).load().then((data) => {
496
- renderCampaigns(data, api);
497
- });
498
- });
499
- }
500
- }, [clientScriptLoaded, currentVariation, recommendationComponent, pageTypeUpdated]);
501
- return /* @__PURE__ */ jsx(Fragment, {
502
- children: /* @__PURE__ */ jsx("div", {
503
- className: "nosto_page_type",
504
- style: {
505
- display: "none"
506
- },
507
- children: "front"
508
- })
509
- });
510
- }
511
- function NostoPlacement(props) {
512
- const {
513
- id,
514
- pageType
515
- } = props;
516
- return /* @__PURE__ */ jsx("div", {
517
- className: "nosto_element",
518
- id
519
- }, id + (pageType || ""));
520
- }
521
- function NostoProvider(props) {
522
- let {
523
- account,
524
- currentVariation = "",
525
- multiCurrency = false,
526
- host,
527
- children,
528
- recommendationComponent,
529
- shopifyMarkets
530
- } = props;
531
- const [clientScriptLoadedState, setClientScriptLoadedState] = React.useState(false);
532
- const clientScriptLoaded = React.useMemo(() => clientScriptLoadedState, [clientScriptLoadedState]);
533
- currentVariation = multiCurrency ? currentVariation : "";
534
- const responseMode = isValidElement(recommendationComponent) ? "JSON_ORIGINAL" : "HTML";
535
- function RecommendationComponentWrapper(props2) {
536
- return React.cloneElement(recommendationComponent, {
537
- nostoRecommendation: props2.nostoRecommendation
538
- });
539
- }
540
- const [pageType, setPageType] = useState("");
541
- const useRenderCampaigns = function(type = "") {
542
- const placementRefs = useRef({});
543
- useEffect(() => {
544
- if (pageType != type) {
545
- setPageType(type);
546
- }
547
- }, []);
548
- const pageTypeUpdated = type == pageType;
549
- function renderCampaigns(data, api) {
550
- if (responseMode == "HTML") {
551
- api.placements.injectCampaigns(data.recommendations);
552
- } else {
553
- const recommendations = data.campaigns.recommendations;
554
- for (const key in recommendations) {
555
- let recommendation = recommendations[key];
556
- let placementSelector = "#" + key;
557
- let placement = () => document.querySelector(placementSelector);
558
- if (!!placement()) {
559
- if (!placementRefs.current[key])
560
- placementRefs.current[key] = createRoot(placement());
561
- const root = placementRefs.current[key];
562
- root.render(/* @__PURE__ */ jsx(RecommendationComponentWrapper, {
563
- nostoRecommendation: recommendation
564
- }));
565
- }
566
- }
567
- }
568
- }
569
- return {
570
- renderCampaigns,
571
- pageTypeUpdated
572
- };
573
- };
574
- useEffect(() => {
575
- var _a, _b, _c;
576
- if (!window.nostojs) {
577
- window.nostojs = (cb) => {
578
- (window.nostojs.q = window.nostojs.q || []).push(cb);
579
- };
580
- window.nostojs((api) => api.setAutoLoad(false));
581
- }
582
- if (!document.querySelectorAll("[nosto-client-script]").length && !shopifyMarkets) {
583
- const script = document.createElement("script");
584
- script.type = "text/javascript";
585
- script.src = "//" + (host || "connect.nosto.com") + "/include/" + account;
586
- script.async = true;
587
- script.setAttribute("nosto-client-script", "");
588
- script.onload = () => {
589
- var _a2;
590
- if (typeof jest !== "undefined") {
591
- (_a2 = window.nosto) == null ? void 0 : _a2.reload({
592
- site: "localhost"
593
- });
594
- }
595
- setClientScriptLoadedState(true);
596
- };
597
- document.body.appendChild(script);
598
- }
599
- if (!!shopifyMarkets) {
600
- const existingScript = document.querySelector("[nosto-client-script]");
601
- const nostoSandbox = document.querySelector("#nosto-sandbox");
602
- if (!existingScript || (existingScript == null ? void 0 : existingScript.getAttribute("nosto-language")) != (shopifyMarkets == null ? void 0 : shopifyMarkets.language) || (existingScript == null ? void 0 : existingScript.getAttribute("nosto-market-id")) != (shopifyMarkets == null ? void 0 : shopifyMarkets.marketId)) {
603
- if (clientScriptLoadedState) {
604
- setClientScriptLoadedState(false);
605
- }
606
- (_a = existingScript == null ? void 0 : existingScript.parentNode) == null ? void 0 : _a.removeChild(existingScript);
607
- (_b = nostoSandbox == null ? void 0 : nostoSandbox.parentNode) == null ? void 0 : _b.removeChild(nostoSandbox);
608
- const script = document.createElement("script");
609
- script.type = "text/javascript";
610
- script.src = "//" + (host || "connect.nosto.com") + `/script/shopify/market/nosto.js?merchant=${account}&market=${shopifyMarkets.marketId || ""}&locale=${((_c = shopifyMarkets == null ? void 0 : shopifyMarkets.language) == null ? void 0 : _c.toLowerCase()) || ""}`;
611
- script.async = true;
612
- script.setAttribute("nosto-client-script", "");
613
- script.setAttribute("nosto-language", (shopifyMarkets == null ? void 0 : shopifyMarkets.language) || "");
614
- script.setAttribute("nosto-market-id", String(shopifyMarkets == null ? void 0 : shopifyMarkets.marketId));
615
- script.onload = () => {
616
- var _a2;
617
- if (typeof jest !== "undefined") {
618
- (_a2 = window.nosto) == null ? void 0 : _a2.reload({
619
- site: "localhost"
620
- });
621
- }
622
- setClientScriptLoadedState(true);
623
- };
624
- document.body.appendChild(script);
625
- }
626
- }
627
- }, [clientScriptLoadedState, shopifyMarkets]);
628
- return /* @__PURE__ */ jsx(NostoContext.Provider, {
629
- value: {
630
- account,
631
- clientScriptLoaded,
632
- currentVariation,
633
- responseMode,
634
- recommendationComponent,
635
- useRenderCampaigns,
636
- pageType
637
- },
638
- children
639
- });
640
- }
641
- function NostoSession(props) {
642
- const {
643
- cart,
644
- customer
645
- } = props;
646
- const {
647
- clientScriptLoaded
648
- } = useNostoContext();
649
- useDeepCompareEffect(() => {
650
- const currentCart = cart ? snakeize(cart) : void 0;
651
- const currentCustomer = customer ? snakeize(customer) : void 0;
652
- if (clientScriptLoaded) {
653
- window.nostojs((api) => {
654
- api.defaultSession().setResponseMode("HTML").setCart(currentCart).setCustomer(currentCustomer).viewOther().load();
655
- });
656
- }
657
- }, [clientScriptLoaded, cart, customer]);
658
- return /* @__PURE__ */ jsx(Fragment, {});
659
- }
660
- export { Nosto404, NostoCategory, NostoCheckout, NostoContext, NostoHome, NostoOrder, NostoOther, NostoPlacement, NostoProduct, NostoProvider, NostoSearch, NostoSession, useNostoContext };