@konfiqo/storefront-react 0.0.0-stage → 0.4.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.
- package/LICENSE +21 -0
- package/README.md +346 -2
- package/dist/index.cjs +1153 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +355 -0
- package/dist/index.d.ts +355 -0
- package/dist/index.js +1120 -0
- package/dist/index.js.map +1 -0
- package/package.json +72 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,1120 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/provider.tsx
|
|
4
|
+
import {
|
|
5
|
+
createContext,
|
|
6
|
+
useContext,
|
|
7
|
+
useEffect,
|
|
8
|
+
useMemo,
|
|
9
|
+
useRef
|
|
10
|
+
} from "react";
|
|
11
|
+
import {
|
|
12
|
+
createStorefrontClient
|
|
13
|
+
} from "@konfiqo/storefront-sdk";
|
|
14
|
+
|
|
15
|
+
// src/cart-store.ts
|
|
16
|
+
import {
|
|
17
|
+
StorefrontError,
|
|
18
|
+
cartIdKey,
|
|
19
|
+
clearCartId,
|
|
20
|
+
cookieCartStorage,
|
|
21
|
+
isStorefrontError,
|
|
22
|
+
loadCartId,
|
|
23
|
+
saveCartId
|
|
24
|
+
} from "@konfiqo/storefront-sdk";
|
|
25
|
+
function isOrderedCartError(e) {
|
|
26
|
+
return isStorefrontError(e) && e.code === "cart_read_only" && e.details.status === "ordered";
|
|
27
|
+
}
|
|
28
|
+
function isDeadCartError(e) {
|
|
29
|
+
if (!isStorefrontError(e)) return false;
|
|
30
|
+
if (e.code === "cart_not_found") return true;
|
|
31
|
+
return e.code === "cart_read_only" && e.details.status === "expired";
|
|
32
|
+
}
|
|
33
|
+
var toError = (e) => e instanceof Error ? e : new Error(String(e));
|
|
34
|
+
var CartStore = class {
|
|
35
|
+
/**
|
|
36
|
+
* @param getClient - the provider's current client (read per call, so
|
|
37
|
+
* a new client — e.g. another `fetch` — keeps this cart state)
|
|
38
|
+
*/
|
|
39
|
+
constructor(getClient, storeId, storage) {
|
|
40
|
+
this.getClient = getClient;
|
|
41
|
+
this.storeId = storeId;
|
|
42
|
+
this.storage = storage;
|
|
43
|
+
}
|
|
44
|
+
getClient;
|
|
45
|
+
storeId;
|
|
46
|
+
storage;
|
|
47
|
+
cartState = {
|
|
48
|
+
cart: null,
|
|
49
|
+
status: "idle",
|
|
50
|
+
isUpdating: false,
|
|
51
|
+
error: null,
|
|
52
|
+
previousCartId: null
|
|
53
|
+
};
|
|
54
|
+
checkoutState = {
|
|
55
|
+
isPending: false,
|
|
56
|
+
result: null,
|
|
57
|
+
error: null,
|
|
58
|
+
attempts: 0
|
|
59
|
+
};
|
|
60
|
+
listeners = /* @__PURE__ */ new Set();
|
|
61
|
+
queue = Promise.resolve();
|
|
62
|
+
cartId = null;
|
|
63
|
+
loadPromise = null;
|
|
64
|
+
reads = new AbortController();
|
|
65
|
+
pendingCheckout = null;
|
|
66
|
+
pendingWrites = 0;
|
|
67
|
+
/**
|
|
68
|
+
* Bumped by `reset()` (and a cart id changed in another tab): a queued
|
|
69
|
+
* operation that started under an older epoch drops its outcome instead
|
|
70
|
+
* of resurrecting (or persisting) the cart it was working on.
|
|
71
|
+
*/
|
|
72
|
+
epoch = 0;
|
|
73
|
+
/**
|
|
74
|
+
* Bumped by `reset()` only. A checkout in flight keeps its outcome (the
|
|
75
|
+
* once-only order token) when another tab merely changes the cart id.
|
|
76
|
+
*/
|
|
77
|
+
resetEpoch = 0;
|
|
78
|
+
/** Whether a checkout was started under this provider. */
|
|
79
|
+
checkoutStarted = false;
|
|
80
|
+
get client() {
|
|
81
|
+
return this.getClient();
|
|
82
|
+
}
|
|
83
|
+
// ------------------------------------------------------ subscription
|
|
84
|
+
subscribe = (listener) => {
|
|
85
|
+
this.listeners.add(listener);
|
|
86
|
+
return () => this.listeners.delete(listener);
|
|
87
|
+
};
|
|
88
|
+
getCart = () => this.cartState;
|
|
89
|
+
getCheckout = () => this.checkoutState;
|
|
90
|
+
setCart(patch) {
|
|
91
|
+
this.cartState = { ...this.cartState, ...patch };
|
|
92
|
+
this.emit();
|
|
93
|
+
}
|
|
94
|
+
setCheckout(patch) {
|
|
95
|
+
this.checkoutState = { ...this.checkoutState, ...patch };
|
|
96
|
+
this.emit();
|
|
97
|
+
}
|
|
98
|
+
emit() {
|
|
99
|
+
for (const l of this.listeners) l();
|
|
100
|
+
}
|
|
101
|
+
/** Runs `fn` after everything queued before it. */
|
|
102
|
+
enqueue(fn) {
|
|
103
|
+
const run = this.queue.then(fn);
|
|
104
|
+
this.queue = run.then(
|
|
105
|
+
() => void 0,
|
|
106
|
+
() => void 0
|
|
107
|
+
);
|
|
108
|
+
return run;
|
|
109
|
+
}
|
|
110
|
+
currentId() {
|
|
111
|
+
return this.cartId ?? loadCartId(this.storage(), this.storeId);
|
|
112
|
+
}
|
|
113
|
+
/** Forgets the cart: the next write creates a new one. */
|
|
114
|
+
forget() {
|
|
115
|
+
this.cartId = null;
|
|
116
|
+
clearCartId(this.storage(), this.storeId);
|
|
117
|
+
}
|
|
118
|
+
/** Puts a server cart view into the state (an expired cart is forgotten). */
|
|
119
|
+
apply(cart) {
|
|
120
|
+
if (cart.status === "expired") {
|
|
121
|
+
this.forget();
|
|
122
|
+
this.setCart({ cart: null, status: "ready", error: null });
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (this.cartId !== cart.id) {
|
|
126
|
+
this.cartId = cart.id;
|
|
127
|
+
saveCartId(this.storage(), this.storeId, cart.id);
|
|
128
|
+
}
|
|
129
|
+
this.setCart({ cart, status: "ready", error: null });
|
|
130
|
+
}
|
|
131
|
+
// ------------------------------------------------------ reads
|
|
132
|
+
/** Reads the saved cart once (later calls share the first read). */
|
|
133
|
+
load = () => {
|
|
134
|
+
this.loadPromise ??= this.read();
|
|
135
|
+
return this.loadPromise;
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Reads the cart again. Also how a cart whose payment failed shows up
|
|
139
|
+
* as `open` again: the read reopens it on the server.
|
|
140
|
+
*/
|
|
141
|
+
refresh = () => this.read();
|
|
142
|
+
/** Cancels pending cart reads (the provider unmounted). */
|
|
143
|
+
abortReads() {
|
|
144
|
+
this.reads.abort();
|
|
145
|
+
this.reads = new AbortController();
|
|
146
|
+
this.loadPromise = null;
|
|
147
|
+
}
|
|
148
|
+
read() {
|
|
149
|
+
const signal = this.reads.signal;
|
|
150
|
+
return this.enqueue(async () => {
|
|
151
|
+
if (signal.aborted) {
|
|
152
|
+
if (this.cartState.status === "loading")
|
|
153
|
+
this.setCart({ status: "idle" });
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const epoch = this.epoch;
|
|
157
|
+
const id = this.currentId();
|
|
158
|
+
if (!id) {
|
|
159
|
+
this.setCart({ cart: null, status: "ready" });
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
this.cartId = id;
|
|
163
|
+
if (this.cartState.cart === null) this.setCart({ status: "loading" });
|
|
164
|
+
try {
|
|
165
|
+
const cart = await this.client.carts.get(id, { signal });
|
|
166
|
+
if (epoch !== this.epoch) return;
|
|
167
|
+
this.apply(cart);
|
|
168
|
+
} catch (e) {
|
|
169
|
+
if (epoch !== this.epoch) return;
|
|
170
|
+
if (signal.aborted) {
|
|
171
|
+
if (this.cartState.status === "loading")
|
|
172
|
+
this.setCart({ status: "idle" });
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (isDeadCartError(e)) {
|
|
176
|
+
this.forget();
|
|
177
|
+
this.setCart({ cart: null, status: "ready", error: null });
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
this.setCart({ status: "ready", error: toError(e) });
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
// ------------------------------------------------------ writes
|
|
185
|
+
write(op) {
|
|
186
|
+
this.pendingWrites++;
|
|
187
|
+
this.setCart({ isUpdating: true });
|
|
188
|
+
return this.enqueue(async () => {
|
|
189
|
+
const epoch = this.epoch;
|
|
190
|
+
const current = () => epoch === this.epoch;
|
|
191
|
+
const attempt = async () => {
|
|
192
|
+
let id = this.currentId();
|
|
193
|
+
if (!id) {
|
|
194
|
+
const created = await this.client.carts.create();
|
|
195
|
+
if (!current()) {
|
|
196
|
+
throw new StorefrontError(
|
|
197
|
+
"aborted",
|
|
198
|
+
0,
|
|
199
|
+
"The cart was reset while the write was pending",
|
|
200
|
+
void 0
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
this.apply(created);
|
|
204
|
+
id = created.id;
|
|
205
|
+
}
|
|
206
|
+
return op(id);
|
|
207
|
+
};
|
|
208
|
+
try {
|
|
209
|
+
let cart;
|
|
210
|
+
try {
|
|
211
|
+
cart = await attempt();
|
|
212
|
+
} catch (e) {
|
|
213
|
+
if (!current() || !isOrderedCartError(e)) throw e;
|
|
214
|
+
const previousCartId = this.currentId();
|
|
215
|
+
this.forget();
|
|
216
|
+
this.setCart({ cart: null, previousCartId });
|
|
217
|
+
cart = await attempt();
|
|
218
|
+
}
|
|
219
|
+
if (current()) this.apply(cart);
|
|
220
|
+
return cart;
|
|
221
|
+
} catch (e) {
|
|
222
|
+
if (current()) {
|
|
223
|
+
if (isDeadCartError(e)) {
|
|
224
|
+
this.forget();
|
|
225
|
+
this.setCart({ cart: null, status: "ready" });
|
|
226
|
+
}
|
|
227
|
+
this.setCart({ error: toError(e) });
|
|
228
|
+
}
|
|
229
|
+
throw e;
|
|
230
|
+
} finally {
|
|
231
|
+
this.pendingWrites--;
|
|
232
|
+
if (this.pendingWrites === 0) this.setCart({ isUpdating: false });
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
addLine = (input) => this.write((id) => this.client.carts.addLine(id, input));
|
|
237
|
+
updateLine = (variantId, input) => this.write((id) => this.client.carts.updateLine(id, variantId, input));
|
|
238
|
+
removeLine = (variantId) => this.write((id) => this.client.carts.removeLine(id, variantId));
|
|
239
|
+
setCustomer = (input) => this.write((id) => this.client.carts.setCustomer(id, input));
|
|
240
|
+
setAddress = (input) => this.write((id) => this.client.carts.setAddress(id, input));
|
|
241
|
+
setShippingOption = (optionId) => this.write((id) => this.client.carts.setShippingOption(id, optionId));
|
|
242
|
+
/** The shipping options of the cart (empty when there is no cart yet). */
|
|
243
|
+
shippingOptions = async (options) => {
|
|
244
|
+
const id = this.currentId();
|
|
245
|
+
if (!id) return { items: [] };
|
|
246
|
+
return this.client.carts.shippingOptions(id, options);
|
|
247
|
+
};
|
|
248
|
+
/**
|
|
249
|
+
* Forgets the cart (e.g. once its order is paid): the next write
|
|
250
|
+
* creates a new one. The old cart is not deleted on the server.
|
|
251
|
+
*/
|
|
252
|
+
reset = () => {
|
|
253
|
+
this.epoch++;
|
|
254
|
+
this.resetEpoch++;
|
|
255
|
+
this.forget();
|
|
256
|
+
this.loadPromise = null;
|
|
257
|
+
this.setCart({
|
|
258
|
+
cart: null,
|
|
259
|
+
status: "ready",
|
|
260
|
+
error: null,
|
|
261
|
+
previousCartId: null
|
|
262
|
+
});
|
|
263
|
+
this.setCheckout({ result: null, error: null });
|
|
264
|
+
};
|
|
265
|
+
/**
|
|
266
|
+
* The saved cart id changed in another tab (a `storage` event for this
|
|
267
|
+
* store's key): follow it. Operations in flight on the old cart drop
|
|
268
|
+
* their outcome.
|
|
269
|
+
*/
|
|
270
|
+
syncFromStorage = () => {
|
|
271
|
+
const id = loadCartId(this.storage(), this.storeId);
|
|
272
|
+
if (id === this.cartId) return;
|
|
273
|
+
this.epoch++;
|
|
274
|
+
this.cartId = id;
|
|
275
|
+
this.loadPromise = null;
|
|
276
|
+
if (id === null) {
|
|
277
|
+
this.setCart({ cart: null, status: "ready", error: null });
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
void this.refresh();
|
|
281
|
+
};
|
|
282
|
+
/** The storage key this store persists its cart id under. */
|
|
283
|
+
get storageKey() {
|
|
284
|
+
return cartIdKey(this.storeId);
|
|
285
|
+
}
|
|
286
|
+
/** The storage the cart id is kept in (null: memory only). */
|
|
287
|
+
currentStorage() {
|
|
288
|
+
return this.storage();
|
|
289
|
+
}
|
|
290
|
+
// ------------------------------------------------------ checkout
|
|
291
|
+
/**
|
|
292
|
+
* Checks the cart out. Never aborted (the server may place the order
|
|
293
|
+
* anyway); a second call while one is in flight returns the same
|
|
294
|
+
* promise. The cart is read again afterwards.
|
|
295
|
+
*/
|
|
296
|
+
checkout = (options = {}) => {
|
|
297
|
+
if (this.pendingCheckout) return this.pendingCheckout;
|
|
298
|
+
this.checkoutStarted = true;
|
|
299
|
+
this.setCheckout({ isPending: true, error: null });
|
|
300
|
+
let epoch = this.resetEpoch;
|
|
301
|
+
const run = this.enqueue(async () => {
|
|
302
|
+
epoch = this.resetEpoch;
|
|
303
|
+
const id = this.currentId();
|
|
304
|
+
if (!id) {
|
|
305
|
+
throw new StorefrontError(
|
|
306
|
+
"checkout_incomplete",
|
|
307
|
+
0,
|
|
308
|
+
"There is no cart to check out",
|
|
309
|
+
{ missing: ["lines"] }
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
return this.client.carts.checkout(id, {
|
|
313
|
+
turnstileToken: options.turnstileToken || void 0
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
this.pendingCheckout = run;
|
|
317
|
+
run.then(
|
|
318
|
+
(result) => this.settleCheckout(epoch, { result, error: null }),
|
|
319
|
+
(e) => this.settleCheckout(epoch, { result: null, error: toError(e) })
|
|
320
|
+
);
|
|
321
|
+
return run;
|
|
322
|
+
};
|
|
323
|
+
settleCheckout(epoch, patch) {
|
|
324
|
+
this.pendingCheckout = null;
|
|
325
|
+
this.setCheckout({
|
|
326
|
+
// After `reset()` the outcome belongs to a cart the buyer left.
|
|
327
|
+
...epoch === this.resetEpoch ? patch : {},
|
|
328
|
+
isPending: false,
|
|
329
|
+
attempts: this.checkoutState.attempts + 1
|
|
330
|
+
});
|
|
331
|
+
void this.refresh();
|
|
332
|
+
}
|
|
333
|
+
/** Clears the last checkout result and error. */
|
|
334
|
+
clearCheckout = () => {
|
|
335
|
+
this.setCheckout({ result: null, error: null });
|
|
336
|
+
};
|
|
337
|
+
};
|
|
338
|
+
function defaultCartStorage() {
|
|
339
|
+
if (typeof window === "undefined") return null;
|
|
340
|
+
let local = null;
|
|
341
|
+
try {
|
|
342
|
+
local = window.localStorage;
|
|
343
|
+
} catch {
|
|
344
|
+
local = null;
|
|
345
|
+
}
|
|
346
|
+
const cookie = cookieCartStorage();
|
|
347
|
+
const attempt = (f) => {
|
|
348
|
+
try {
|
|
349
|
+
f();
|
|
350
|
+
return true;
|
|
351
|
+
} catch {
|
|
352
|
+
return false;
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
const tombstoneKey = (key) => `${key}:cleared`;
|
|
356
|
+
const legacyMarkerKey = (key) => `${key}:in-cookie`;
|
|
357
|
+
const cleared = (key) => {
|
|
358
|
+
try {
|
|
359
|
+
return local?.getItem(tombstoneKey(key)) === "1";
|
|
360
|
+
} catch {
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
const storage = {
|
|
365
|
+
// The area a `storage` event of another tab names.
|
|
366
|
+
storageArea: local,
|
|
367
|
+
getItem(key) {
|
|
368
|
+
let fromCookie = null;
|
|
369
|
+
try {
|
|
370
|
+
fromCookie = cookie.getItem(key);
|
|
371
|
+
} catch {
|
|
372
|
+
fromCookie = null;
|
|
373
|
+
}
|
|
374
|
+
if (fromCookie) {
|
|
375
|
+
attempt(() => cookie.setItem(key, fromCookie));
|
|
376
|
+
return fromCookie;
|
|
377
|
+
}
|
|
378
|
+
let fromLocal = null;
|
|
379
|
+
try {
|
|
380
|
+
fromLocal = local?.getItem(key) ?? null;
|
|
381
|
+
} catch {
|
|
382
|
+
fromLocal = null;
|
|
383
|
+
}
|
|
384
|
+
if (!fromLocal) return null;
|
|
385
|
+
if (cleared(key)) return null;
|
|
386
|
+
attempt(() => cookie.setItem(key, fromLocal));
|
|
387
|
+
return fromLocal;
|
|
388
|
+
},
|
|
389
|
+
setItem(key, value) {
|
|
390
|
+
const a = attempt(() => cookie.setItem(key, value));
|
|
391
|
+
const b = local ? attempt(() => local.setItem(key, value)) : false;
|
|
392
|
+
if (local) {
|
|
393
|
+
attempt(() => local.removeItem(tombstoneKey(key)));
|
|
394
|
+
attempt(() => local.removeItem(legacyMarkerKey(key)));
|
|
395
|
+
}
|
|
396
|
+
if (!a && !b) throw new Error("no cart storage available");
|
|
397
|
+
},
|
|
398
|
+
removeItem(key) {
|
|
399
|
+
attempt(() => cookie.removeItem(key));
|
|
400
|
+
if (local) {
|
|
401
|
+
attempt(() => local.removeItem(key));
|
|
402
|
+
attempt(() => local.setItem(tombstoneKey(key), "1"));
|
|
403
|
+
attempt(() => local.removeItem(legacyMarkerKey(key)));
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
return storage;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// src/provider.tsx
|
|
411
|
+
import { jsx } from "react/jsx-runtime";
|
|
412
|
+
var StorefrontContext = createContext(null);
|
|
413
|
+
function StorefrontProvider({
|
|
414
|
+
storeId,
|
|
415
|
+
client,
|
|
416
|
+
config,
|
|
417
|
+
cartStorage,
|
|
418
|
+
children
|
|
419
|
+
}) {
|
|
420
|
+
const forbidden = ["serverKey", "trustedStorefrontKey", "buyerIp"].filter(
|
|
421
|
+
(k) => config !== void 0 && k in config
|
|
422
|
+
);
|
|
423
|
+
if (forbidden.length > 0) {
|
|
424
|
+
throw new Error(
|
|
425
|
+
`StorefrontProvider: config.${forbidden.join(", config.")} must not be passed to a client component (it would reach the browser). Create a server-side client instead.`
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
const baseUrl = config?.baseUrl;
|
|
429
|
+
const publishableKey = config?.publishableKey;
|
|
430
|
+
const timeoutMs = config?.timeoutMs;
|
|
431
|
+
const customFetch = config?.fetch;
|
|
432
|
+
const resolved = useMemo(() => {
|
|
433
|
+
if (client) return client;
|
|
434
|
+
if (baseUrl === void 0 || publishableKey === void 0) {
|
|
435
|
+
throw new Error("StorefrontProvider: pass `client` or `config`");
|
|
436
|
+
}
|
|
437
|
+
return createStorefrontClient({
|
|
438
|
+
baseUrl,
|
|
439
|
+
publishableKey,
|
|
440
|
+
timeoutMs,
|
|
441
|
+
fetch: customFetch
|
|
442
|
+
});
|
|
443
|
+
}, [client, baseUrl, publishableKey, timeoutMs, customFetch]);
|
|
444
|
+
const clientRef = useRef(resolved);
|
|
445
|
+
clientRef.current = resolved;
|
|
446
|
+
const storageRef = useRef(cartStorage);
|
|
447
|
+
storageRef.current = cartStorage;
|
|
448
|
+
const cart = useMemo(
|
|
449
|
+
() => new CartStore(
|
|
450
|
+
() => clientRef.current,
|
|
451
|
+
storeId,
|
|
452
|
+
() => storageRef.current === void 0 ? defaultCartStorage() : storageRef.current
|
|
453
|
+
),
|
|
454
|
+
[storeId]
|
|
455
|
+
);
|
|
456
|
+
useEffect(() => () => cart.abortReads(), [cart]);
|
|
457
|
+
useEffect(() => {
|
|
458
|
+
if (typeof window === "undefined") return;
|
|
459
|
+
const onStorage = (e) => {
|
|
460
|
+
const storage = cart.currentStorage();
|
|
461
|
+
if (!storage) return;
|
|
462
|
+
const area = storage.storageArea;
|
|
463
|
+
if (e.storageArea !== (area ?? storage)) return;
|
|
464
|
+
if (e.key !== null && e.key !== cart.storageKey) return;
|
|
465
|
+
cart.syncFromStorage();
|
|
466
|
+
};
|
|
467
|
+
window.addEventListener("storage", onStorage);
|
|
468
|
+
return () => window.removeEventListener("storage", onStorage);
|
|
469
|
+
}, [cart]);
|
|
470
|
+
const value = useMemo(
|
|
471
|
+
() => ({ client: resolved, storeId, cart }),
|
|
472
|
+
[resolved, storeId, cart]
|
|
473
|
+
);
|
|
474
|
+
return /* @__PURE__ */ jsx(StorefrontContext.Provider, { value, children });
|
|
475
|
+
}
|
|
476
|
+
function useStorefrontContext() {
|
|
477
|
+
const ctx = useContext(StorefrontContext);
|
|
478
|
+
if (!ctx) {
|
|
479
|
+
throw new Error(
|
|
480
|
+
"Storefront hooks must be used inside <StorefrontProvider>"
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
return ctx;
|
|
484
|
+
}
|
|
485
|
+
function useOptionalStorefrontContext() {
|
|
486
|
+
return useContext(StorefrontContext);
|
|
487
|
+
}
|
|
488
|
+
function useStorefrontClient() {
|
|
489
|
+
return useStorefrontContext().client;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// src/use-resource.ts
|
|
493
|
+
import { useCallback, useEffect as useEffect2, useRef as useRef2, useState } from "react";
|
|
494
|
+
var toError2 = (e) => e instanceof Error ? e : new Error(String(e));
|
|
495
|
+
function useResource(key, deps, fetcher) {
|
|
496
|
+
const [state, setState] = useState({
|
|
497
|
+
key: null,
|
|
498
|
+
data: null,
|
|
499
|
+
error: null,
|
|
500
|
+
isLoading: key !== null
|
|
501
|
+
});
|
|
502
|
+
const [nonce, setNonce] = useState(0);
|
|
503
|
+
const fetcherRef = useRef2(fetcher);
|
|
504
|
+
useEffect2(() => {
|
|
505
|
+
fetcherRef.current = fetcher;
|
|
506
|
+
});
|
|
507
|
+
useEffect2(() => {
|
|
508
|
+
if (key === null) {
|
|
509
|
+
setState({ key, data: null, error: null, isLoading: false });
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
const controller = new AbortController();
|
|
513
|
+
setState(
|
|
514
|
+
(s) => s.key === key ? { ...s, isLoading: true } : { key, data: null, error: null, isLoading: true }
|
|
515
|
+
);
|
|
516
|
+
fetcherRef.current(controller.signal).then(
|
|
517
|
+
(data) => {
|
|
518
|
+
if (!controller.signal.aborted)
|
|
519
|
+
setState({ key, data, error: null, isLoading: false });
|
|
520
|
+
},
|
|
521
|
+
(e) => {
|
|
522
|
+
if (!controller.signal.aborted)
|
|
523
|
+
setState({ key, data: null, error: toError2(e), isLoading: false });
|
|
524
|
+
}
|
|
525
|
+
);
|
|
526
|
+
return () => controller.abort();
|
|
527
|
+
}, [key, deps, nonce]);
|
|
528
|
+
const refetch = useCallback(() => setNonce((n) => n + 1), []);
|
|
529
|
+
if (state.key !== key) {
|
|
530
|
+
return { data: null, error: null, isLoading: key !== null, refetch };
|
|
531
|
+
}
|
|
532
|
+
return {
|
|
533
|
+
data: state.data,
|
|
534
|
+
error: state.error,
|
|
535
|
+
isLoading: state.isLoading,
|
|
536
|
+
refetch
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// src/catalog.ts
|
|
541
|
+
function useStore() {
|
|
542
|
+
const { client } = useStorefrontContext();
|
|
543
|
+
return useResource("store", client, (signal) => client.store.get({ signal }));
|
|
544
|
+
}
|
|
545
|
+
function useCategories() {
|
|
546
|
+
const { client } = useStorefrontContext();
|
|
547
|
+
return useResource(
|
|
548
|
+
"categories",
|
|
549
|
+
client,
|
|
550
|
+
(signal) => client.categories.list({ signal })
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
function useProducts(params = {}) {
|
|
554
|
+
const { client } = useStorefrontContext();
|
|
555
|
+
const query = {
|
|
556
|
+
category: params.category,
|
|
557
|
+
q: params.q,
|
|
558
|
+
page: params.page,
|
|
559
|
+
pageSize: params.pageSize
|
|
560
|
+
};
|
|
561
|
+
return useResource(
|
|
562
|
+
JSON.stringify(query),
|
|
563
|
+
client,
|
|
564
|
+
(signal) => client.products.list(query, { signal })
|
|
565
|
+
);
|
|
566
|
+
}
|
|
567
|
+
function useProduct(slug) {
|
|
568
|
+
const { client } = useStorefrontContext();
|
|
569
|
+
return useResource(
|
|
570
|
+
slug ? `product:${slug}` : null,
|
|
571
|
+
client,
|
|
572
|
+
(signal) => client.products.get(slug, { signal })
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// src/use-cart.ts
|
|
577
|
+
import { useEffect as useEffect3, useMemo as useMemo2, useSyncExternalStore } from "react";
|
|
578
|
+
var SERVER_CART = {
|
|
579
|
+
cart: null,
|
|
580
|
+
status: "idle",
|
|
581
|
+
isUpdating: false,
|
|
582
|
+
error: null,
|
|
583
|
+
previousCartId: null
|
|
584
|
+
};
|
|
585
|
+
function useCart() {
|
|
586
|
+
const { cart: store } = useStorefrontContext();
|
|
587
|
+
const snapshot = useSyncExternalStore(
|
|
588
|
+
store.subscribe,
|
|
589
|
+
store.getCart,
|
|
590
|
+
() => SERVER_CART
|
|
591
|
+
);
|
|
592
|
+
useEffect3(() => {
|
|
593
|
+
void store.load();
|
|
594
|
+
}, [store]);
|
|
595
|
+
const itemCount = snapshot.cart?.lines.reduce((n, line) => n + line.quantity, 0) ?? 0;
|
|
596
|
+
return {
|
|
597
|
+
...snapshot,
|
|
598
|
+
itemCount,
|
|
599
|
+
addLine: store.addLine,
|
|
600
|
+
updateLine: store.updateLine,
|
|
601
|
+
removeLine: store.removeLine,
|
|
602
|
+
setCustomer: store.setCustomer,
|
|
603
|
+
setAddress: store.setAddress,
|
|
604
|
+
setShippingOption: store.setShippingOption,
|
|
605
|
+
shippingOptions: store.shippingOptions,
|
|
606
|
+
refresh: store.refresh,
|
|
607
|
+
reset: store.reset
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
function useCartStorage() {
|
|
611
|
+
const { cart } = useStorefrontContext();
|
|
612
|
+
return useMemo2(() => () => cart.currentStorage(), [cart]);
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
// src/use-account.ts
|
|
616
|
+
import { useCallback as useCallback2, useEffect as useEffect4, useRef as useRef3, useState as useState2 } from "react";
|
|
617
|
+
import { isStorefrontError as isStorefrontError2 } from "@konfiqo/storefront-sdk";
|
|
618
|
+
function useAccount() {
|
|
619
|
+
const { client } = useStorefrontContext();
|
|
620
|
+
const [state, setState] = useState2({
|
|
621
|
+
account: null,
|
|
622
|
+
status: "loading",
|
|
623
|
+
error: null
|
|
624
|
+
});
|
|
625
|
+
const seq = useRef3(0);
|
|
626
|
+
const alive = useRef3(true);
|
|
627
|
+
const refresh = useCallback2(async () => {
|
|
628
|
+
const mine = ++seq.current;
|
|
629
|
+
try {
|
|
630
|
+
const account = await client.account.get();
|
|
631
|
+
if (!alive.current || mine !== seq.current) return;
|
|
632
|
+
setState({ account, status: "signedIn", error: null });
|
|
633
|
+
} catch (e) {
|
|
634
|
+
if (!alive.current || mine !== seq.current) return;
|
|
635
|
+
if (isStorefrontError2(e) && e.status === 401) {
|
|
636
|
+
setState({ account: null, status: "signedOut", error: null });
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
const error = e instanceof Error ? e : new Error(String(e));
|
|
640
|
+
setState((s) => ({
|
|
641
|
+
account: s.account,
|
|
642
|
+
status: s.account ? "signedIn" : "signedOut",
|
|
643
|
+
error
|
|
644
|
+
}));
|
|
645
|
+
}
|
|
646
|
+
}, [client]);
|
|
647
|
+
useEffect4(() => {
|
|
648
|
+
alive.current = true;
|
|
649
|
+
void refresh();
|
|
650
|
+
return () => {
|
|
651
|
+
alive.current = false;
|
|
652
|
+
};
|
|
653
|
+
}, [refresh]);
|
|
654
|
+
return { ...state, refresh };
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
// src/use-checkout.ts
|
|
658
|
+
import { useCallback as useCallback3, useEffect as useEffect5, useSyncExternalStore as useSyncExternalStore2 } from "react";
|
|
659
|
+
var SERVER_CHECKOUT = {
|
|
660
|
+
isPending: false,
|
|
661
|
+
result: null,
|
|
662
|
+
error: null,
|
|
663
|
+
attempts: 0
|
|
664
|
+
};
|
|
665
|
+
function useCheckout() {
|
|
666
|
+
const { cart: store } = useStorefrontContext();
|
|
667
|
+
const snapshot = useSyncExternalStore2(
|
|
668
|
+
store.subscribe,
|
|
669
|
+
store.getCheckout,
|
|
670
|
+
() => SERVER_CHECKOUT
|
|
671
|
+
);
|
|
672
|
+
useEffect5(() => {
|
|
673
|
+
if (store.checkoutStarted) void store.refresh();
|
|
674
|
+
}, [store]);
|
|
675
|
+
const checkout = useCallback3(
|
|
676
|
+
(options = {}) => store.checkout({ turnstileToken: options.turnstileToken ?? void 0 }),
|
|
677
|
+
[store]
|
|
678
|
+
);
|
|
679
|
+
return { ...snapshot, checkout, reset: store.clearCheckout };
|
|
680
|
+
}
|
|
681
|
+
function useCheckoutAttempts() {
|
|
682
|
+
const ctx = useOptionalStorefrontContext();
|
|
683
|
+
const store = ctx?.cart;
|
|
684
|
+
return useSyncExternalStore2(
|
|
685
|
+
store?.subscribe ?? noopSubscribe,
|
|
686
|
+
() => store?.getCheckout().attempts ?? 0,
|
|
687
|
+
() => 0
|
|
688
|
+
);
|
|
689
|
+
}
|
|
690
|
+
var noopSubscribe = () => () => {
|
|
691
|
+
};
|
|
692
|
+
|
|
693
|
+
// src/use-order.ts
|
|
694
|
+
import { useCallback as useCallback4, useEffect as useEffect6, useState as useState3 } from "react";
|
|
695
|
+
import { isStorefrontError as isStorefrontError3 } from "@konfiqo/storefront-sdk";
|
|
696
|
+
var ORDER_POLL_MIN_MS = 2e3;
|
|
697
|
+
var ORDER_POLL_MAX_MS = 15e3;
|
|
698
|
+
function orderPollDelay(attempt) {
|
|
699
|
+
return Math.min(ORDER_POLL_MIN_MS * 2 ** attempt, ORDER_POLL_MAX_MS);
|
|
700
|
+
}
|
|
701
|
+
function isOrderSettled(order) {
|
|
702
|
+
if (order.status !== "pendingPayment") return true;
|
|
703
|
+
return order.paymentStatus === "failed" || order.paymentStatus === "expired";
|
|
704
|
+
}
|
|
705
|
+
function isTransient(e) {
|
|
706
|
+
if (!isStorefrontError3(e)) return false;
|
|
707
|
+
switch (e.code) {
|
|
708
|
+
case "network_error":
|
|
709
|
+
case "timeout":
|
|
710
|
+
case "rate_limited":
|
|
711
|
+
case "order_unavailable":
|
|
712
|
+
case "http_error":
|
|
713
|
+
return true;
|
|
714
|
+
default:
|
|
715
|
+
return false;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
function useOrder(orderId, token, options = {}) {
|
|
719
|
+
const { client } = useStorefrontContext();
|
|
720
|
+
const poll = options.pollUntilFinal ?? false;
|
|
721
|
+
const key = orderId && token ? `${orderId} ${token}` : null;
|
|
722
|
+
const [state, setState] = useState3({
|
|
723
|
+
key: null,
|
|
724
|
+
order: null,
|
|
725
|
+
error: null,
|
|
726
|
+
isPolling: false
|
|
727
|
+
});
|
|
728
|
+
const [nonce, setNonce] = useState3(0);
|
|
729
|
+
useEffect6(() => {
|
|
730
|
+
if (!orderId || !token) {
|
|
731
|
+
setState({ key: null, order: null, error: null, isPolling: false });
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
const k = `${orderId} ${token}`;
|
|
735
|
+
const controller = new AbortController();
|
|
736
|
+
let timer;
|
|
737
|
+
let attempt = 0;
|
|
738
|
+
const schedule = (atLeastMs) => {
|
|
739
|
+
const delay = Math.max(orderPollDelay(attempt++), atLeastMs);
|
|
740
|
+
setState((s) => ({ ...s, isPolling: true }));
|
|
741
|
+
timer = setTimeout(tick, delay);
|
|
742
|
+
};
|
|
743
|
+
const tick = async () => {
|
|
744
|
+
try {
|
|
745
|
+
const order = await client.orders.get(orderId, token, {
|
|
746
|
+
signal: controller.signal
|
|
747
|
+
});
|
|
748
|
+
if (controller.signal.aborted) return;
|
|
749
|
+
setState({ key: k, order, error: null, isPolling: false });
|
|
750
|
+
if (poll && !isOrderSettled(order)) schedule(0);
|
|
751
|
+
} catch (e) {
|
|
752
|
+
if (controller.signal.aborted) return;
|
|
753
|
+
const error = e instanceof Error ? e : new Error(String(e));
|
|
754
|
+
setState((s) => ({
|
|
755
|
+
key: k,
|
|
756
|
+
order: s.key === k ? s.order : null,
|
|
757
|
+
error,
|
|
758
|
+
isPolling: false
|
|
759
|
+
}));
|
|
760
|
+
if (poll && isTransient(e)) {
|
|
761
|
+
const retryAfter = isStorefrontError3(e) && e.code === "rate_limited" ? (e.details.retryAfter ?? 0) * 1e3 : 0;
|
|
762
|
+
schedule(retryAfter);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
};
|
|
766
|
+
void tick();
|
|
767
|
+
return () => {
|
|
768
|
+
controller.abort();
|
|
769
|
+
clearTimeout(timer);
|
|
770
|
+
};
|
|
771
|
+
}, [client, orderId, token, poll, nonce]);
|
|
772
|
+
const refetch = useCallback4(() => setNonce((n) => n + 1), []);
|
|
773
|
+
if (state.key !== key) {
|
|
774
|
+
return {
|
|
775
|
+
order: null,
|
|
776
|
+
error: null,
|
|
777
|
+
isLoading: key !== null,
|
|
778
|
+
isPolling: false,
|
|
779
|
+
refetch
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
return {
|
|
783
|
+
order: state.order,
|
|
784
|
+
error: state.error,
|
|
785
|
+
isLoading: false,
|
|
786
|
+
isPolling: state.isPolling,
|
|
787
|
+
refetch
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
// src/payment-form.tsx
|
|
792
|
+
import { useEffect as useEffect7, useRef as useRef4 } from "react";
|
|
793
|
+
import {
|
|
794
|
+
mountPayment
|
|
795
|
+
} from "@konfiqo/storefront-sdk";
|
|
796
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
797
|
+
var embeddedContent = (p) => p.presentation === "embedded" && typeof p.embed.content === "string" ? p.embed.content : null;
|
|
798
|
+
function PaymentForm({
|
|
799
|
+
presentation,
|
|
800
|
+
onClosed,
|
|
801
|
+
onUnsupported,
|
|
802
|
+
onError,
|
|
803
|
+
...divProps
|
|
804
|
+
}) {
|
|
805
|
+
const ref = useRef4(null);
|
|
806
|
+
const callbacks = useRef4({ onClosed, onUnsupported, onError });
|
|
807
|
+
useEffect7(() => {
|
|
808
|
+
callbacks.current = { onClosed, onUnsupported, onError };
|
|
809
|
+
});
|
|
810
|
+
const pending = useRef4(null);
|
|
811
|
+
const navigated = useRef4(null);
|
|
812
|
+
useEffect7(() => {
|
|
813
|
+
const element = ref.current;
|
|
814
|
+
if (!element) return;
|
|
815
|
+
const prev = pending.current;
|
|
816
|
+
pending.current = null;
|
|
817
|
+
if (prev) {
|
|
818
|
+
clearTimeout(prev.timer);
|
|
819
|
+
if (prev.content === null || prev.content !== embeddedContent(presentation))
|
|
820
|
+
prev.unmount();
|
|
821
|
+
}
|
|
822
|
+
if (presentation.presentation !== "embedded") {
|
|
823
|
+
if (navigated.current === presentation) return;
|
|
824
|
+
navigated.current = presentation;
|
|
825
|
+
}
|
|
826
|
+
let unmount = null;
|
|
827
|
+
try {
|
|
828
|
+
const mount = mountPayment(element, presentation, {
|
|
829
|
+
onClosed: (page) => callbacks.current.onClosed?.(page)
|
|
830
|
+
});
|
|
831
|
+
if (mount.kind === "embedded") unmount = mount.unmount;
|
|
832
|
+
else if (mount.kind === "unsupported")
|
|
833
|
+
callbacks.current.onUnsupported?.(mount.embedKind);
|
|
834
|
+
} catch (e) {
|
|
835
|
+
callbacks.current.onError?.(
|
|
836
|
+
e instanceof Error ? e : new Error(String(e))
|
|
837
|
+
);
|
|
838
|
+
}
|
|
839
|
+
if (!unmount) return;
|
|
840
|
+
const remove = unmount;
|
|
841
|
+
return () => {
|
|
842
|
+
const entry = {
|
|
843
|
+
content: embeddedContent(presentation),
|
|
844
|
+
unmount: remove,
|
|
845
|
+
timer: setTimeout(() => {
|
|
846
|
+
if (pending.current === entry) pending.current = null;
|
|
847
|
+
remove();
|
|
848
|
+
}, 0)
|
|
849
|
+
};
|
|
850
|
+
pending.current = entry;
|
|
851
|
+
};
|
|
852
|
+
}, [presentation]);
|
|
853
|
+
return /* @__PURE__ */ jsx2("div", { ...divProps, ref });
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
// src/checkout-challenge.tsx
|
|
857
|
+
import {
|
|
858
|
+
forwardRef,
|
|
859
|
+
useEffect as useEffect8,
|
|
860
|
+
useImperativeHandle,
|
|
861
|
+
useRef as useRef5
|
|
862
|
+
} from "react";
|
|
863
|
+
import { renderTurnstile } from "@konfiqo/storefront-sdk";
|
|
864
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
865
|
+
var CheckoutChallenge = forwardRef(function CheckoutChallenge2({ siteKey, onToken, onExpired, onError, ...divProps }, handle) {
|
|
866
|
+
const ref = useRef5(null);
|
|
867
|
+
const widget = useRef5(null);
|
|
868
|
+
const callbacks = useRef5({ onToken, onExpired, onError });
|
|
869
|
+
useEffect8(() => {
|
|
870
|
+
callbacks.current = { onToken, onExpired, onError };
|
|
871
|
+
});
|
|
872
|
+
useEffect8(() => {
|
|
873
|
+
const element = ref.current;
|
|
874
|
+
if (!element || !siteKey) return;
|
|
875
|
+
let active = true;
|
|
876
|
+
let rendered = null;
|
|
877
|
+
renderTurnstile(element, siteKey, {
|
|
878
|
+
onToken: (t) => callbacks.current.onToken(t),
|
|
879
|
+
onExpired: () => callbacks.current.onExpired?.(),
|
|
880
|
+
onError: (code) => callbacks.current.onError?.(code)
|
|
881
|
+
}).then(
|
|
882
|
+
(w) => {
|
|
883
|
+
if (!active) {
|
|
884
|
+
w.remove();
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
rendered = w;
|
|
888
|
+
widget.current = w;
|
|
889
|
+
},
|
|
890
|
+
(e) => {
|
|
891
|
+
if (active)
|
|
892
|
+
callbacks.current.onError?.(
|
|
893
|
+
e instanceof Error ? e : new Error(String(e))
|
|
894
|
+
);
|
|
895
|
+
}
|
|
896
|
+
);
|
|
897
|
+
return () => {
|
|
898
|
+
active = false;
|
|
899
|
+
if (rendered) {
|
|
900
|
+
rendered.remove();
|
|
901
|
+
if (widget.current === rendered) widget.current = null;
|
|
902
|
+
}
|
|
903
|
+
};
|
|
904
|
+
}, [siteKey]);
|
|
905
|
+
const reset = () => {
|
|
906
|
+
if (!widget.current) return;
|
|
907
|
+
callbacks.current.onExpired?.();
|
|
908
|
+
widget.current.reset();
|
|
909
|
+
};
|
|
910
|
+
useImperativeHandle(handle, () => ({ reset }), []);
|
|
911
|
+
const attempts = useCheckoutAttempts();
|
|
912
|
+
const seen = useRef5(attempts);
|
|
913
|
+
useEffect8(() => {
|
|
914
|
+
if (attempts === seen.current) return;
|
|
915
|
+
seen.current = attempts;
|
|
916
|
+
reset();
|
|
917
|
+
});
|
|
918
|
+
return /* @__PURE__ */ jsx3("div", { ...divProps, ref });
|
|
919
|
+
});
|
|
920
|
+
|
|
921
|
+
// src/content.tsx
|
|
922
|
+
import Markdoc from "@markdoc/markdoc";
|
|
923
|
+
import * as React from "react";
|
|
924
|
+
import { useEffect as useEffect9 } from "react";
|
|
925
|
+
import { formatMoney } from "@konfiqo/storefront-sdk";
|
|
926
|
+
import { Fragment, jsx as jsx4, jsxs } from "react/jsx-runtime";
|
|
927
|
+
var HTML_TAGS = new Set(
|
|
928
|
+
"article section div span p h1 h2 h3 h4 h5 h6 ul ol li strong em s del code pre blockquote hr br a table thead tbody tr th td figure figcaption small sub sup mark".split(" ")
|
|
929
|
+
);
|
|
930
|
+
var HTML_ATTRS = /* @__PURE__ */ new Set([
|
|
931
|
+
"class",
|
|
932
|
+
"title",
|
|
933
|
+
"align",
|
|
934
|
+
"colspan",
|
|
935
|
+
"rowspan",
|
|
936
|
+
"start"
|
|
937
|
+
]);
|
|
938
|
+
var MAX_DEPTH = 40;
|
|
939
|
+
var MAX_NODES = 5e3;
|
|
940
|
+
var hasControl = (s) => Array.from(s).some((c) => {
|
|
941
|
+
const n = c.charCodeAt(0);
|
|
942
|
+
return n < 32 || n === 127;
|
|
943
|
+
});
|
|
944
|
+
function safeHref(v) {
|
|
945
|
+
if (typeof v !== "string") return null;
|
|
946
|
+
const href = v.trim();
|
|
947
|
+
if (href === "" || /\s/.test(href) || hasControl(href)) return null;
|
|
948
|
+
if (href.includes("\\") || /%5c/i.test(href)) return null;
|
|
949
|
+
if (href.startsWith("#")) return href;
|
|
950
|
+
if (href.startsWith("/")) return href[1] === "/" ? null : href;
|
|
951
|
+
return /^(https?:|mailto:|tel:)/i.test(href) ? href : null;
|
|
952
|
+
}
|
|
953
|
+
var isProp = (v) => typeof v === "string" || typeof v === "number" || typeof v === "boolean";
|
|
954
|
+
function clean(node, w, depth) {
|
|
955
|
+
if (typeof node === "string") return node;
|
|
956
|
+
if (node === null || typeof node !== "object") return null;
|
|
957
|
+
const n = node;
|
|
958
|
+
if (n.$$mdtype !== "Tag" || typeof n.name !== "string" || depth > MAX_DEPTH || ++w.nodes > MAX_NODES) {
|
|
959
|
+
return null;
|
|
960
|
+
}
|
|
961
|
+
const name = n.name;
|
|
962
|
+
const raw = n.attributes && typeof n.attributes === "object" ? n.attributes : {};
|
|
963
|
+
const attributes = {};
|
|
964
|
+
if (name === "") return null;
|
|
965
|
+
const isHtml = name[0] === name[0].toLowerCase();
|
|
966
|
+
if (isHtml) {
|
|
967
|
+
if (!HTML_TAGS.has(name)) return null;
|
|
968
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
969
|
+
if (name === "a" && k === "href") {
|
|
970
|
+
const href = safeHref(v);
|
|
971
|
+
if (href !== null) attributes.href = href;
|
|
972
|
+
} else if (HTML_ATTRS.has(k) && isProp(v)) {
|
|
973
|
+
attributes[k] = v;
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
} else {
|
|
977
|
+
if (!Object.hasOwn(w.components, name)) return null;
|
|
978
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
979
|
+
if (!isProp(v)) continue;
|
|
980
|
+
if (k === "ref" || k === "children" || /^on/i.test(k)) continue;
|
|
981
|
+
if (k === "dangerouslySetInnerHTML") continue;
|
|
982
|
+
if (k === "key") attributes.contentKey = v;
|
|
983
|
+
else attributes[k] = v;
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
const children = [];
|
|
987
|
+
for (const c of Array.isArray(n.children) ? n.children : []) {
|
|
988
|
+
const cleaned = clean(c, w, depth + 1);
|
|
989
|
+
if (cleaned !== null) children.push(cleaned);
|
|
990
|
+
}
|
|
991
|
+
return { $$mdtype: "Tag", name, attributes, children };
|
|
992
|
+
}
|
|
993
|
+
var text = (v) => typeof v === "string" && v !== "" ? v : void 0;
|
|
994
|
+
var slugOf = (v) => {
|
|
995
|
+
const s = text(v);
|
|
996
|
+
return s && !/[/\\?#]/.test(s) && s !== "." && s !== ".." ? s : null;
|
|
997
|
+
};
|
|
998
|
+
function ProductCard({
|
|
999
|
+
product
|
|
1000
|
+
}) {
|
|
1001
|
+
const { client } = useStorefrontContext();
|
|
1002
|
+
const variant = product.variants[0];
|
|
1003
|
+
let price = null;
|
|
1004
|
+
try {
|
|
1005
|
+
price = variant ? formatMoney(variant.priceMinor, variant.currency) : null;
|
|
1006
|
+
} catch {
|
|
1007
|
+
price = null;
|
|
1008
|
+
}
|
|
1009
|
+
let image = null;
|
|
1010
|
+
try {
|
|
1011
|
+
image = product.image ? client.media.url(product.image) : null;
|
|
1012
|
+
} catch {
|
|
1013
|
+
image = null;
|
|
1014
|
+
}
|
|
1015
|
+
return /* @__PURE__ */ jsxs("a", { href: `/p/${encodeURIComponent(product.slug)}`, children: [
|
|
1016
|
+
image ? /* @__PURE__ */ jsx4("img", { src: image, alt: "" }) : null,
|
|
1017
|
+
/* @__PURE__ */ jsx4("span", { children: product.name }),
|
|
1018
|
+
price ? /* @__PURE__ */ jsxs("span", { children: [
|
|
1019
|
+
" ",
|
|
1020
|
+
price
|
|
1021
|
+
] }) : null
|
|
1022
|
+
] });
|
|
1023
|
+
}
|
|
1024
|
+
function ProductGrid(props) {
|
|
1025
|
+
const category = text(props.category);
|
|
1026
|
+
const limit = Number(props.limit);
|
|
1027
|
+
const cap = Number.isInteger(limit) && limit > 0 ? Math.min(limit, 48) : 12;
|
|
1028
|
+
const { data } = useProducts({ category, pageSize: cap });
|
|
1029
|
+
if (!data) return null;
|
|
1030
|
+
return /* @__PURE__ */ jsx4("ul", { className: "content-product-grid", children: data.items.slice(0, cap).map((p) => /* @__PURE__ */ jsx4("li", { children: /* @__PURE__ */ jsx4(ProductCard, { product: p }) }, p.id)) });
|
|
1031
|
+
}
|
|
1032
|
+
function ProductTag(props) {
|
|
1033
|
+
const { data } = useProduct(slugOf(props.slug));
|
|
1034
|
+
return data ? /* @__PURE__ */ jsx4(ProductCard, { product: data }) : null;
|
|
1035
|
+
}
|
|
1036
|
+
function CategoryLink(props) {
|
|
1037
|
+
const slug = slugOf(props.slug);
|
|
1038
|
+
if (!slug) return null;
|
|
1039
|
+
return /* @__PURE__ */ jsx4("a", { href: `/c/${encodeURIComponent(slug)}`, children: props.children ?? slug });
|
|
1040
|
+
}
|
|
1041
|
+
function ImageTag(props) {
|
|
1042
|
+
const { client } = useStorefrontContext();
|
|
1043
|
+
const key = text(props.contentKey);
|
|
1044
|
+
if (!key) return null;
|
|
1045
|
+
let src;
|
|
1046
|
+
try {
|
|
1047
|
+
src = client.media.url(key);
|
|
1048
|
+
} catch {
|
|
1049
|
+
return null;
|
|
1050
|
+
}
|
|
1051
|
+
return /* @__PURE__ */ jsx4("img", { src, alt: text(props.alt) ?? "" });
|
|
1052
|
+
}
|
|
1053
|
+
function ButtonTag(props) {
|
|
1054
|
+
const href = safeHref(props.href);
|
|
1055
|
+
const label = text(props.label);
|
|
1056
|
+
if (!href || !label) return null;
|
|
1057
|
+
return /* @__PURE__ */ jsx4("a", { className: "button", href, children: label });
|
|
1058
|
+
}
|
|
1059
|
+
var DEFAULT_COMPONENTS = {
|
|
1060
|
+
ProductGrid,
|
|
1061
|
+
Product: ProductTag,
|
|
1062
|
+
CategoryLink,
|
|
1063
|
+
Image: ImageTag,
|
|
1064
|
+
Button: ButtonTag
|
|
1065
|
+
};
|
|
1066
|
+
function renderContent(tree, components = {}) {
|
|
1067
|
+
try {
|
|
1068
|
+
const merged = { ...DEFAULT_COMPONENTS, ...components };
|
|
1069
|
+
const cleaned = clean(tree, { nodes: 0, components: merged }, 0);
|
|
1070
|
+
if (cleaned === null) return null;
|
|
1071
|
+
return Markdoc.renderers.react(
|
|
1072
|
+
cleaned,
|
|
1073
|
+
React,
|
|
1074
|
+
{ components: merged }
|
|
1075
|
+
);
|
|
1076
|
+
} catch {
|
|
1077
|
+
return null;
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
function Content({ contentKey, components }) {
|
|
1081
|
+
const { client } = useStorefrontContext();
|
|
1082
|
+
const { data, error } = useResource(
|
|
1083
|
+
`content:${contentKey}`,
|
|
1084
|
+
client,
|
|
1085
|
+
(signal) => client.getContent(contentKey, { signal })
|
|
1086
|
+
);
|
|
1087
|
+
useEffect9(() => {
|
|
1088
|
+
if (error && typeof process !== "undefined" && process.env?.NODE_ENV !== "production") {
|
|
1089
|
+
console.warn(
|
|
1090
|
+
`<Content contentKey="${contentKey}">: ${error.message || "could not be read"}`
|
|
1091
|
+
);
|
|
1092
|
+
}
|
|
1093
|
+
}, [error, contentKey]);
|
|
1094
|
+
if (!data) return null;
|
|
1095
|
+
return /* @__PURE__ */ jsx4(Fragment, { children: renderContent(data.tree, components) });
|
|
1096
|
+
}
|
|
1097
|
+
export {
|
|
1098
|
+
CheckoutChallenge,
|
|
1099
|
+
Content,
|
|
1100
|
+
ORDER_POLL_MAX_MS,
|
|
1101
|
+
ORDER_POLL_MIN_MS,
|
|
1102
|
+
PaymentForm,
|
|
1103
|
+
StorefrontProvider,
|
|
1104
|
+
defaultCartStorage,
|
|
1105
|
+
isDeadCartError,
|
|
1106
|
+
isOrderSettled,
|
|
1107
|
+
orderPollDelay,
|
|
1108
|
+
renderContent,
|
|
1109
|
+
useAccount,
|
|
1110
|
+
useCart,
|
|
1111
|
+
useCartStorage,
|
|
1112
|
+
useCategories,
|
|
1113
|
+
useCheckout,
|
|
1114
|
+
useOrder,
|
|
1115
|
+
useProduct,
|
|
1116
|
+
useProducts,
|
|
1117
|
+
useStore,
|
|
1118
|
+
useStorefrontClient
|
|
1119
|
+
};
|
|
1120
|
+
//# sourceMappingURL=index.js.map
|