@simpleapps-com/augur-hooks 2.4.0 → 2026.11.1
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/EXPORTS.md +25 -65
- package/README.md +21 -246
- package/dist/checkout/create-checkout-provider.d.ts +25 -0
- package/dist/checkout/create-checkout-provider.d.ts.map +1 -0
- package/dist/checkout/index.cjs +123 -106
- package/dist/checkout/index.cjs.map +1 -1
- package/dist/checkout/index.d.ts +5 -293
- package/dist/checkout/index.d.ts.map +1 -0
- package/dist/checkout/index.js +99 -82
- package/dist/checkout/index.js.map +1 -1
- package/dist/checkout/{index.d.cts → types.d.ts} +18 -40
- package/dist/checkout/types.d.ts.map +1 -0
- package/dist/chunk-6Z6JGK2C.cjs.map +1 -1
- package/dist/chunk-QB7VB5FZ.cjs +523 -0
- package/dist/chunk-QB7VB5FZ.cjs.map +1 -0
- package/dist/{chunk-JNOHB4OP.js → chunk-UTDAFIQP.js} +234 -94
- package/dist/chunk-UTDAFIQP.js.map +1 -0
- package/dist/hooks/use-cart-actions.d.ts +68 -0
- package/dist/hooks/use-cart-actions.d.ts.map +1 -0
- package/dist/hooks/use-cart-initialization.d.ts +60 -0
- package/dist/hooks/use-cart-initialization.d.ts.map +1 -0
- package/dist/hooks/use-cart-pricing.d.ts +41 -0
- package/dist/hooks/use-cart-pricing.d.ts.map +1 -0
- package/dist/hooks/use-checkout-persistence.d.ts +40 -0
- package/dist/hooks/use-checkout-persistence.d.ts.map +1 -0
- package/dist/hooks/use-debounce.d.ts +6 -0
- package/dist/hooks/use-debounce.d.ts.map +1 -0
- package/dist/hooks/use-format-price.d.ts +29 -0
- package/dist/hooks/use-format-price.d.ts.map +1 -0
- package/dist/hooks/use-latest.d.ts +8 -0
- package/dist/hooks/use-latest.d.ts.map +1 -0
- package/dist/hooks/use-modal-lifecycle.d.ts +11 -0
- package/dist/hooks/use-modal-lifecycle.d.ts.map +1 -0
- package/dist/hooks/use-order-readiness.d.ts +47 -0
- package/dist/hooks/use-order-readiness.d.ts.map +1 -0
- package/dist/hooks/use-pagination-prefetch.d.ts +26 -0
- package/dist/hooks/use-pagination-prefetch.d.ts.map +1 -0
- package/dist/{use-order-readiness-DZTIfRzw.d.ts → hooks/use-payment-flow.d.ts} +23 -67
- package/dist/hooks/use-payment-flow.d.ts.map +1 -0
- package/dist/hooks/use-stock.d.ts +32 -0
- package/dist/hooks/use-stock.d.ts.map +1 -0
- package/dist/index.cjs +215 -178
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +32 -456
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +193 -156
- package/dist/index.js.map +1 -1
- package/dist/persistence/apply-limits.d.ts +8 -0
- package/dist/persistence/apply-limits.d.ts.map +1 -0
- package/dist/persistence/create-persister.d.ts +10 -0
- package/dist/persistence/create-persister.d.ts.map +1 -0
- package/dist/persistence/serializer.d.ts +11 -0
- package/dist/persistence/serializer.d.ts.map +1 -0
- package/dist/persistence/use-persistence.d.ts +59 -0
- package/dist/persistence/use-persistence.d.ts.map +1 -0
- package/dist/persistence/use-storage-monitor.d.ts +11 -0
- package/dist/persistence/use-storage-monitor.d.ts.map +1 -0
- package/dist/provider.d.ts +89 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/queries/batch-query.d.ts +23 -0
- package/dist/queries/batch-query.d.ts.map +1 -0
- package/dist/queries/cache-helper.d.ts +16 -0
- package/dist/queries/cache-helper.d.ts.map +1 -0
- package/dist/queries/index.d.ts +3 -0
- package/dist/queries/index.d.ts.map +1 -0
- package/dist/query-action-registry.d.ts +19 -0
- package/dist/query-action-registry.d.ts.map +1 -0
- package/dist/rules.d.ts +130 -0
- package/dist/rules.d.ts.map +1 -0
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +10 -12
- package/dist/server.d.ts.map +1 -0
- package/dist/stores/cart-store.d.ts +51 -0
- package/dist/stores/cart-store.d.ts.map +1 -0
- package/dist/stores/items-filters-store.d.ts +30 -0
- package/dist/stores/items-filters-store.d.ts.map +1 -0
- package/dist/types.d.ts +48 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/web/use-image-with-fallback.d.ts +16 -0
- package/dist/web/use-image-with-fallback.d.ts.map +1 -0
- package/dist/web/use-mobile-detection.d.ts +28 -0
- package/dist/web/use-mobile-detection.d.ts.map +1 -0
- package/dist/web/use-mobile-optimized-intersection.d.ts +17 -0
- package/dist/web/use-mobile-optimized-intersection.d.ts.map +1 -0
- package/dist/web.cjs.map +1 -1
- package/dist/web.d.ts +4 -60
- package/dist/web.d.ts.map +1 -0
- package/dist/web.js.map +1 -1
- package/llms.txt +76 -54
- package/package.json +25 -14
- package/dist/batch-query-CTlvxSmL.d.cts +0 -165
- package/dist/batch-query-CTlvxSmL.d.ts +0 -165
- package/dist/chunk-F5DUFW57.cjs +0 -383
- package/dist/chunk-F5DUFW57.cjs.map +0 -1
- package/dist/chunk-JNOHB4OP.js.map +0 -1
- package/dist/index.d.cts +0 -456
- package/dist/server.d.cts +0 -44
- package/dist/use-order-readiness-DZTIfRzw.d.cts +0 -137
- package/dist/web.d.cts +0 -60
package/dist/index.js
CHANGED
|
@@ -7,8 +7,15 @@ import {
|
|
|
7
7
|
clearCheckoutPersistence,
|
|
8
8
|
clearFromSession,
|
|
9
9
|
determineOrderReadiness,
|
|
10
|
+
indexOfUidFrom,
|
|
11
|
+
keepsAfterReverse,
|
|
10
12
|
loadFromSession,
|
|
13
|
+
qtyOrZero,
|
|
14
|
+
requiredCartUid,
|
|
11
15
|
saveToSession,
|
|
16
|
+
shouldPersistOf,
|
|
17
|
+
stockEnabled,
|
|
18
|
+
stockRetryDelay,
|
|
12
19
|
useCartHdrUid,
|
|
13
20
|
useCartLineCount,
|
|
14
21
|
useCartLines,
|
|
@@ -16,11 +23,12 @@ import {
|
|
|
16
23
|
useCartStore,
|
|
17
24
|
useCheckoutPersistence,
|
|
18
25
|
useClearCart,
|
|
26
|
+
useLatest,
|
|
19
27
|
useOrderReadiness,
|
|
20
28
|
usePaymentFlow,
|
|
21
29
|
useSetCartHdrUid,
|
|
22
30
|
useSetCartLines
|
|
23
|
-
} from "./chunk-
|
|
31
|
+
} from "./chunk-UTDAFIQP.js";
|
|
24
32
|
|
|
25
33
|
// src/provider.tsx
|
|
26
34
|
import { createContext, useContext, useMemo } from "react";
|
|
@@ -37,12 +45,14 @@ var AugurOptionsContext = createContext(null);
|
|
|
37
45
|
var AugurSiteContext = createContext(void 0);
|
|
38
46
|
function AugurHooksProvider({ api, auth, cache, site, children }) {
|
|
39
47
|
const proxyConfig = cache ? { cache: { static: cache.static, semiStatic: cache.semiStatic } } : void 0;
|
|
48
|
+
const proxyConfigRef = useLatest(proxyConfig);
|
|
40
49
|
const options = useMemo(() => {
|
|
41
|
-
|
|
50
|
+
const config = proxyConfigRef.current;
|
|
51
|
+
if (api) return createOptionsProxy(api, config);
|
|
42
52
|
const registeredAction = getRegisteredQueryAction();
|
|
43
|
-
if (registeredAction) return createQueryActionProxy(registeredAction,
|
|
44
|
-
return createKeysOnlyProxy(
|
|
45
|
-
}, [api]);
|
|
53
|
+
if (registeredAction) return createQueryActionProxy(registeredAction, config);
|
|
54
|
+
return createKeysOnlyProxy(config);
|
|
55
|
+
}, [api, proxyConfigRef]);
|
|
46
56
|
return /* @__PURE__ */ jsx(AugurApiContext.Provider, { value: api ?? null, children: /* @__PURE__ */ jsx(AugurAuthReactContext.Provider, { value: auth, children: /* @__PURE__ */ jsx(AugurCacheContext.Provider, { value: cache, children: /* @__PURE__ */ jsx(AugurSiteContext.Provider, { value: site, children: /* @__PURE__ */ jsx(AugurOptionsContext.Provider, { value: options, children }) }) }) }) });
|
|
47
57
|
}
|
|
48
58
|
function useAugurOptions() {
|
|
@@ -117,6 +127,7 @@ function useDebounce(value, delay = 500) {
|
|
|
117
127
|
}
|
|
118
128
|
|
|
119
129
|
// src/hooks/use-format-price.ts
|
|
130
|
+
import { cleanMoney } from "@simpleapps-com/augur-utils";
|
|
120
131
|
function normaliseOptions(optionsOrLocale, legacyCurrency) {
|
|
121
132
|
if (typeof optionsOrLocale === "string") {
|
|
122
133
|
return { locale: optionsOrLocale, currency: legacyCurrency };
|
|
@@ -124,11 +135,12 @@ function normaliseOptions(optionsOrLocale, legacyCurrency) {
|
|
|
124
135
|
return optionsOrLocale ?? {};
|
|
125
136
|
}
|
|
126
137
|
function resolveFormatting(opts, siteFormatting) {
|
|
138
|
+
const site = siteFormatting ?? {};
|
|
127
139
|
return {
|
|
128
|
-
locale: opts.locale ??
|
|
129
|
-
currency: opts.currency ??
|
|
130
|
-
precision: opts.precision ??
|
|
131
|
-
zeroLabel: opts.zeroLabel ??
|
|
140
|
+
locale: opts.locale ?? site.locale ?? "en-US",
|
|
141
|
+
currency: opts.currency ?? site.currency ?? "USD",
|
|
142
|
+
precision: opts.precision ?? site.precision ?? 2,
|
|
143
|
+
zeroLabel: opts.zeroLabel ?? site.zeroLabel
|
|
132
144
|
};
|
|
133
145
|
}
|
|
134
146
|
function useFormatPrice(optionsOrLocale, legacyCurrency) {
|
|
@@ -141,7 +153,7 @@ function useFormatPrice(optionsOrLocale, legacyCurrency) {
|
|
|
141
153
|
zeroLabel
|
|
142
154
|
} = resolveFormatting(opts, site?.formatting);
|
|
143
155
|
const formatPrice = (price, callOptions) => {
|
|
144
|
-
const effectiveValue = price * (callOptions?.quantity ?? 1);
|
|
156
|
+
const effectiveValue = cleanMoney(price * (callOptions?.quantity ?? 1));
|
|
145
157
|
if (effectiveValue === 0 && zeroLabel !== void 0) {
|
|
146
158
|
return zeroLabel;
|
|
147
159
|
}
|
|
@@ -167,22 +179,11 @@ function useStock(invMastUid, options = {}) {
|
|
|
167
179
|
const query = useQuery({
|
|
168
180
|
...proxyOpts,
|
|
169
181
|
...resolvedQueryFn ? { queryFn: resolvedQueryFn } : {},
|
|
170
|
-
enabled: (options.enabled
|
|
182
|
+
enabled: stockEnabled(options.enabled, !!invMastUid),
|
|
171
183
|
retry: 3,
|
|
172
|
-
|
|
173
|
-
retryDelay: (attemptIndex) => Math.min(1e3 * 2 ** attemptIndex, 1e4)
|
|
174
|
-
/* v8 ignore stop */
|
|
184
|
+
retryDelay: stockRetryDelay
|
|
175
185
|
});
|
|
176
|
-
const stockData =
|
|
177
|
-
const rawData = query.data;
|
|
178
|
-
if (!rawData) return null;
|
|
179
|
-
const inner = rawData.data ?? rawData;
|
|
180
|
-
const stock = inner;
|
|
181
|
-
return {
|
|
182
|
-
stockData: stock.stockData ?? [],
|
|
183
|
-
companySummary: stock.companySummary ?? {}
|
|
184
|
-
};
|
|
185
|
-
}, [query.data]);
|
|
186
|
+
const stockData = query.data ?? null;
|
|
186
187
|
const resolved = useMemo2(
|
|
187
188
|
() => resolveStock({
|
|
188
189
|
stock: stockData,
|
|
@@ -204,16 +205,30 @@ function useStock(invMastUid, options = {}) {
|
|
|
204
205
|
// src/hooks/use-cart-actions.ts
|
|
205
206
|
import { useCallback } from "react";
|
|
206
207
|
import { useQueryClient } from "@tanstack/react-query";
|
|
208
|
+
|
|
209
|
+
// ../utils/src/lists.ts
|
|
210
|
+
function hasNumberFrom(xs, x, i) {
|
|
211
|
+
if (i === xs.length) return false;
|
|
212
|
+
if (xs[i] === x) return true;
|
|
213
|
+
return hasNumberFrom(xs, x, i + 1);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// src/hooks/use-cart-actions.ts
|
|
207
217
|
function requireCartUid(cartHdrUid) {
|
|
208
218
|
const num = typeof cartHdrUid === "string" ? Number(cartHdrUid) : cartHdrUid;
|
|
209
|
-
|
|
210
|
-
|
|
219
|
+
const uid = requiredCartUid(num === void 0 || Number.isNaN(num) ? void 0 : num);
|
|
220
|
+
if (uid === null) throw new Error("Cart UID is not set");
|
|
221
|
+
return uid;
|
|
222
|
+
}
|
|
223
|
+
function liveCart() {
|
|
224
|
+
const { cartHdrUid, cartLines } = useCartStore.getState();
|
|
225
|
+
return { uid: requireCartUid(cartHdrUid), lines: cartLines };
|
|
211
226
|
}
|
|
212
227
|
function buildOptimisticAdd(cartLines, item, cartHdrUid, itemId) {
|
|
213
|
-
const existingIndex =
|
|
228
|
+
const existingIndex = indexOfUidFrom(uidsOf(cartLines), item.invMastUid, 0);
|
|
214
229
|
if (existingIndex >= 0) {
|
|
215
230
|
return cartLines.map(
|
|
216
|
-
(line, index) => index === existingIndex ? { ...line, quantity: line.quantity + item.quantity } : line
|
|
231
|
+
(line, index) => index === existingIndex ? { ...line, quantity: qtyOrZero(line.quantity) + item.quantity } : line
|
|
217
232
|
);
|
|
218
233
|
}
|
|
219
234
|
return [
|
|
@@ -227,14 +242,17 @@ function buildOptimisticAdd(cartLines, item, cartHdrUid, itemId) {
|
|
|
227
242
|
lineNo: cartLines.length + 1,
|
|
228
243
|
lineNote: null,
|
|
229
244
|
quantity: item.quantity,
|
|
230
|
-
unitOfMeasure: item.unitOfMeasure
|
|
245
|
+
unitOfMeasure: item.unitOfMeasure,
|
|
246
|
+
key: null,
|
|
247
|
+
unitPrice: null
|
|
231
248
|
}
|
|
232
249
|
];
|
|
233
250
|
}
|
|
234
251
|
function reverseAdd(cartLines, invMastUid, quantity) {
|
|
235
|
-
return cartLines.
|
|
236
|
-
(line
|
|
237
|
-
|
|
252
|
+
return cartLines.flatMap((line) => {
|
|
253
|
+
if (line.invMastUid !== invMastUid) return [line];
|
|
254
|
+
return keepsAfterReverse(line.quantity, quantity) ? [{ ...line, quantity: qtyOrZero(line.quantity) - quantity }] : [];
|
|
255
|
+
});
|
|
238
256
|
}
|
|
239
257
|
function reverseQuantity(cartLines, invMastUid, previousQuantity) {
|
|
240
258
|
if (previousQuantity === void 0) return cartLines;
|
|
@@ -244,16 +262,37 @@ function reverseQuantity(cartLines, invMastUid, previousQuantity) {
|
|
|
244
262
|
}
|
|
245
263
|
function restoreRemoved(cartLines, invMastUid, removedLine) {
|
|
246
264
|
if (!removedLine) return cartLines;
|
|
247
|
-
if (
|
|
265
|
+
if (hasNumberFrom(uidsOf(cartLines), invMastUid, 0)) return cartLines;
|
|
248
266
|
return [...cartLines, removedLine];
|
|
249
267
|
}
|
|
250
268
|
function mergeRestore(cartLines, snapshot) {
|
|
251
269
|
const merged = [...cartLines];
|
|
252
270
|
for (const line of snapshot) {
|
|
253
|
-
if (!
|
|
271
|
+
if (!hasNumberFrom(uidsOf(merged), line.invMastUid, 0)) merged.push(line);
|
|
254
272
|
}
|
|
255
273
|
return merged;
|
|
256
274
|
}
|
|
275
|
+
function uidsOf(cartLines) {
|
|
276
|
+
return cartLines.map((line) => line.invMastUid);
|
|
277
|
+
}
|
|
278
|
+
function sendToast(notify, kind, showToast, message) {
|
|
279
|
+
if (showToast && message !== void 0) notify?.[kind]?.(message);
|
|
280
|
+
}
|
|
281
|
+
async function settleCartOp(op, options, notify, invalidate) {
|
|
282
|
+
try {
|
|
283
|
+
const success = await op.call();
|
|
284
|
+
if (!success) throw new Error(op.failure);
|
|
285
|
+
invalidate();
|
|
286
|
+
sendToast(notify, "info", op.showToast, op.infoMessage);
|
|
287
|
+
options.onSuccess?.();
|
|
288
|
+
return true;
|
|
289
|
+
} catch (error) {
|
|
290
|
+
op.rollback();
|
|
291
|
+
sendToast(notify, "error", op.showToast, op.errorMessage);
|
|
292
|
+
options.onError?.(error);
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
257
296
|
function useCartActions(callbacks) {
|
|
258
297
|
const queryClient = useQueryClient();
|
|
259
298
|
const cartHdrUid = useCartHdrUid();
|
|
@@ -268,119 +307,105 @@ function useCartActions(callbacks) {
|
|
|
268
307
|
}, [queryClient, cartHdrUid]);
|
|
269
308
|
const addToCart = useCallback(
|
|
270
309
|
async (item, options = {}) => {
|
|
271
|
-
const { showToast = true, itemId = ""
|
|
272
|
-
const {
|
|
273
|
-
const uid = requireCartUid(liveUid);
|
|
310
|
+
const { showToast = true, itemId = "" } = options;
|
|
311
|
+
const { uid, lines: liveLines } = liveCart();
|
|
274
312
|
setCartLines(buildOptimisticAdd(liveLines, item, uid, itemId));
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
return false;
|
|
298
|
-
}
|
|
313
|
+
return settleCartOp(
|
|
314
|
+
{
|
|
315
|
+
call: () => callbacks.addToCart(uid, [
|
|
316
|
+
{
|
|
317
|
+
cartHdrUid: uid,
|
|
318
|
+
invMastUid: item.invMastUid,
|
|
319
|
+
quantity: item.quantity,
|
|
320
|
+
unitOfMeasure: item.unitOfMeasure
|
|
321
|
+
}
|
|
322
|
+
]),
|
|
323
|
+
failure: "Failed to add item to cart",
|
|
324
|
+
infoMessage: `${item.quantity} x ${itemId || "Item"} added to cart`,
|
|
325
|
+
errorMessage: "Failed to add item to cart. Please try again.",
|
|
326
|
+
showToast,
|
|
327
|
+
rollback: () => setCartLines(
|
|
328
|
+
reverseAdd(useCartStore.getState().cartLines, item.invMastUid, item.quantity)
|
|
329
|
+
)
|
|
330
|
+
},
|
|
331
|
+
options,
|
|
332
|
+
notify,
|
|
333
|
+
invalidateCartCache
|
|
334
|
+
);
|
|
299
335
|
},
|
|
300
336
|
[setCartLines, invalidateCartCache, callbacks, notify]
|
|
301
337
|
);
|
|
302
338
|
const updateQuantity = useCallback(
|
|
303
339
|
async (invMastUid, newQuantity, options = {}) => {
|
|
304
|
-
const { showToast = false
|
|
305
|
-
const {
|
|
306
|
-
const uid = requireCartUid(liveUid);
|
|
340
|
+
const { showToast = false } = options;
|
|
341
|
+
const { uid, lines: liveLines } = liveCart();
|
|
307
342
|
const previousQuantity = liveLines.find((line) => line.invMastUid === invMastUid)?.quantity;
|
|
308
343
|
const updatedLines = liveLines.map(
|
|
309
344
|
(line) => line.invMastUid === invMastUid ? { ...line, quantity: newQuantity } : line
|
|
310
345
|
);
|
|
311
346
|
setCartLines(updatedLines);
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
return false;
|
|
327
|
-
}
|
|
347
|
+
return settleCartOp(
|
|
348
|
+
{
|
|
349
|
+
call: () => callbacks.updateCartLines(uid, updatedLines),
|
|
350
|
+
failure: "Failed to update quantity",
|
|
351
|
+
errorMessage: "Failed to update quantity. Please try again.",
|
|
352
|
+
showToast,
|
|
353
|
+
rollback: () => setCartLines(
|
|
354
|
+
reverseQuantity(useCartStore.getState().cartLines, invMastUid, previousQuantity)
|
|
355
|
+
)
|
|
356
|
+
},
|
|
357
|
+
options,
|
|
358
|
+
notify,
|
|
359
|
+
invalidateCartCache
|
|
360
|
+
);
|
|
328
361
|
},
|
|
329
362
|
[setCartLines, invalidateCartCache, callbacks, notify]
|
|
330
363
|
);
|
|
331
364
|
const removeFromCart = useCallback(
|
|
332
365
|
async (invMastUid, options = {}) => {
|
|
333
|
-
const { showToast = true, itemId = ""
|
|
334
|
-
const {
|
|
335
|
-
const uid = requireCartUid(liveUid);
|
|
366
|
+
const { showToast = true, itemId = "" } = options;
|
|
367
|
+
const { uid, lines: liveLines } = liveCart();
|
|
336
368
|
const removedLine = liveLines.find((line) => line.invMastUid === invMastUid);
|
|
337
369
|
setCartLines(liveLines.filter((line) => line.invMastUid !== invMastUid));
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
return false;
|
|
357
|
-
}
|
|
370
|
+
const zeroedLines = liveLines.map(
|
|
371
|
+
(line) => line.invMastUid === invMastUid ? { ...line, quantity: 0 } : line
|
|
372
|
+
);
|
|
373
|
+
return settleCartOp(
|
|
374
|
+
{
|
|
375
|
+
call: () => callbacks.updateCartLines(uid, zeroedLines),
|
|
376
|
+
failure: "Failed to remove item",
|
|
377
|
+
infoMessage: `${itemId || "Item"} removed from cart`,
|
|
378
|
+
errorMessage: "Failed to remove item. Please try again.",
|
|
379
|
+
showToast,
|
|
380
|
+
rollback: () => setCartLines(
|
|
381
|
+
restoreRemoved(useCartStore.getState().cartLines, invMastUid, removedLine)
|
|
382
|
+
)
|
|
383
|
+
},
|
|
384
|
+
options,
|
|
385
|
+
notify,
|
|
386
|
+
invalidateCartCache
|
|
387
|
+
);
|
|
358
388
|
},
|
|
359
389
|
[setCartLines, invalidateCartCache, callbacks, notify]
|
|
360
390
|
);
|
|
361
391
|
const clearCart = useCallback(
|
|
362
392
|
async (options = {}) => {
|
|
363
|
-
const { showToast = true
|
|
364
|
-
const {
|
|
365
|
-
const uid = requireCartUid(liveUid);
|
|
393
|
+
const { showToast = true } = options;
|
|
394
|
+
const { uid, lines: snapshot } = liveCart();
|
|
366
395
|
setCartLines([]);
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
}
|
|
381
|
-
onError?.(error);
|
|
382
|
-
return false;
|
|
383
|
-
}
|
|
396
|
+
return settleCartOp(
|
|
397
|
+
{
|
|
398
|
+
call: () => callbacks.deleteItemsFromCart(uid),
|
|
399
|
+
failure: "Failed to clear cart",
|
|
400
|
+
infoMessage: "Cart cleared",
|
|
401
|
+
errorMessage: "Failed to clear cart. Please try again.",
|
|
402
|
+
showToast,
|
|
403
|
+
rollback: () => setCartLines(mergeRestore(useCartStore.getState().cartLines, snapshot))
|
|
404
|
+
},
|
|
405
|
+
options,
|
|
406
|
+
notify,
|
|
407
|
+
invalidateCartCache
|
|
408
|
+
);
|
|
384
409
|
},
|
|
385
410
|
[setCartLines, invalidateCartCache, callbacks, notify]
|
|
386
411
|
);
|
|
@@ -437,6 +462,11 @@ import { useEffect as useEffect3, useMemo as useMemo3, useRef } from "react";
|
|
|
437
462
|
import { useQuery as useQuery2, useQueryClient as useQueryClient2 } from "@tanstack/react-query";
|
|
438
463
|
var MAX_RETRY_ATTEMPTS = 7;
|
|
439
464
|
var RETRY_DELAY_MS = 1e3;
|
|
465
|
+
async function waitBeforeRetry(attempt) {
|
|
466
|
+
if (attempt >= MAX_RETRY_ATTEMPTS) return;
|
|
467
|
+
const delay = Math.min(RETRY_DELAY_MS * Math.pow(1.5, attempt), 1e4);
|
|
468
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
469
|
+
}
|
|
440
470
|
var defaultGetCartToken = () => localStorage.getItem("cartToken");
|
|
441
471
|
var defaultSaveCartToken = (token) => localStorage.setItem("cartToken", token);
|
|
442
472
|
var defaultGenerateCartToken = () => crypto.randomUUID();
|
|
@@ -492,17 +522,11 @@ function useCartInitialization(session, callbacks) {
|
|
|
492
522
|
return;
|
|
493
523
|
}
|
|
494
524
|
retryCountRef.current++;
|
|
495
|
-
|
|
496
|
-
const delay = Math.min(RETRY_DELAY_MS * Math.pow(1.5, retryCountRef.current), 1e4);
|
|
497
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
498
|
-
}
|
|
525
|
+
await waitBeforeRetry(retryCountRef.current);
|
|
499
526
|
} catch (error) {
|
|
500
527
|
lastError = error;
|
|
501
528
|
retryCountRef.current++;
|
|
502
|
-
|
|
503
|
-
const delay = Math.min(RETRY_DELAY_MS * Math.pow(1.5, retryCountRef.current), 1e4);
|
|
504
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
505
|
-
}
|
|
529
|
+
await waitBeforeRetry(retryCountRef.current);
|
|
506
530
|
}
|
|
507
531
|
}
|
|
508
532
|
console.error(`Cart initialization failed after ${MAX_RETRY_ATTEMPTS} attempts`, lastError);
|
|
@@ -539,7 +563,20 @@ function useCartInitialization(session, callbacks) {
|
|
|
539
563
|
// src/hooks/use-cart-pricing.ts
|
|
540
564
|
import { useMemo as useMemo4 } from "react";
|
|
541
565
|
import { useQueries } from "@tanstack/react-query";
|
|
542
|
-
import { derivePrice } from "@simpleapps-com/augur-utils";
|
|
566
|
+
import { cleanMoney as cleanMoney2, derivePrice } from "@simpleapps-com/augur-utils";
|
|
567
|
+
function deriveLineUnitPrice(priceData, quantity) {
|
|
568
|
+
if (typeof quantity !== "number" || !Number.isFinite(quantity)) return 0;
|
|
569
|
+
try {
|
|
570
|
+
const derived = derivePrice({ priceData, quantity });
|
|
571
|
+
return derived.state === "priced" ? derived.unitPrice : 0;
|
|
572
|
+
} catch (error) {
|
|
573
|
+
if (error instanceof RangeError) return 0;
|
|
574
|
+
throw error;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
function lineTotal(unitPrice, quantity) {
|
|
578
|
+
return Number.isFinite(quantity) ? cleanMoney2(unitPrice * quantity) : 0;
|
|
579
|
+
}
|
|
543
580
|
function useCartPricing(options = {}) {
|
|
544
581
|
const proxy = useAugurOptions();
|
|
545
582
|
const auth = useAugurAuth();
|
|
@@ -549,10 +586,11 @@ function useCartPricing(options = {}) {
|
|
|
549
586
|
const priceFn = options.queryFn;
|
|
550
587
|
const priceQueries = useQueries({
|
|
551
588
|
queries: cartLines.map((line) => {
|
|
589
|
+
const quantity = line.quantity ?? 1;
|
|
552
590
|
if (priceFn) {
|
|
553
591
|
return {
|
|
554
|
-
queryKey: ["price", line.itemId?.toUpperCase() || "", customerId,
|
|
555
|
-
queryFn: () => priceFn(line.itemId, customerId,
|
|
592
|
+
queryKey: ["price", line.itemId?.toUpperCase() || "", customerId, quantity],
|
|
593
|
+
queryFn: () => priceFn(line.itemId, customerId, quantity),
|
|
556
594
|
refetchOnReconnect: true,
|
|
557
595
|
refetchOnWindowFocus: false,
|
|
558
596
|
meta: { persist: true },
|
|
@@ -562,7 +600,7 @@ function useCartPricing(options = {}) {
|
|
|
562
600
|
const proxyOpts = proxy.pricing.priceEngine.list({
|
|
563
601
|
itemId: line.itemId?.toUpperCase() || "",
|
|
564
602
|
customerId: Number(customerId),
|
|
565
|
-
quantity
|
|
603
|
+
quantity
|
|
566
604
|
});
|
|
567
605
|
return {
|
|
568
606
|
...proxyOpts,
|
|
@@ -577,14 +615,9 @@ function useCartPricing(options = {}) {
|
|
|
577
615
|
const map = {};
|
|
578
616
|
cartLines.forEach((line, index) => {
|
|
579
617
|
const queryResult = priceQueries[index];
|
|
580
|
-
const
|
|
581
|
-
if (
|
|
582
|
-
const
|
|
583
|
-
const derived = derivePrice({
|
|
584
|
-
priceData,
|
|
585
|
-
quantity: line.quantity
|
|
586
|
-
});
|
|
587
|
-
const unitPrice = derived.state === "priced" ? derived.unitPrice : 0;
|
|
618
|
+
const priceData = queryResult?.data;
|
|
619
|
+
if (priceData) {
|
|
620
|
+
const unitPrice = deriveLineUnitPrice(priceData, line.quantity);
|
|
588
621
|
map[line.itemId.toUpperCase()] = {
|
|
589
622
|
unitPrice,
|
|
590
623
|
priceData
|
|
@@ -600,13 +633,13 @@ function useCartPricing(options = {}) {
|
|
|
600
633
|
}, [prices]);
|
|
601
634
|
const getItemTotal = useMemo4(() => {
|
|
602
635
|
return (itemId, quantity) => {
|
|
603
|
-
return getUnitPrice(itemId)
|
|
636
|
+
return lineTotal(getUnitPrice(itemId), quantity);
|
|
604
637
|
};
|
|
605
638
|
}, [getUnitPrice]);
|
|
606
639
|
const subtotal = useMemo4(() => {
|
|
607
640
|
return cartLines.reduce((sum, line) => {
|
|
608
641
|
const unitPrice = prices[line.itemId.toUpperCase()]?.unitPrice ?? 0;
|
|
609
|
-
return sum + unitPrice
|
|
642
|
+
return cleanMoney2(sum + lineTotal(unitPrice, line.quantity));
|
|
610
643
|
}, 0);
|
|
611
644
|
}, [cartLines, prices]);
|
|
612
645
|
const isLoading = priceQueries.some((q) => q.isLoading);
|
|
@@ -724,14 +757,12 @@ function createPersister(config) {
|
|
|
724
757
|
const limits = config.limits ?? {};
|
|
725
758
|
let timer;
|
|
726
759
|
let pending;
|
|
727
|
-
function
|
|
728
|
-
if (!pending) return;
|
|
760
|
+
function write(client) {
|
|
729
761
|
try {
|
|
730
|
-
const limited = applyLimits(
|
|
762
|
+
const limited = applyLimits(client, limits);
|
|
731
763
|
window.localStorage.setItem(config.key, serialize(limited));
|
|
732
764
|
} catch {
|
|
733
765
|
}
|
|
734
|
-
pending = void 0;
|
|
735
766
|
}
|
|
736
767
|
return {
|
|
737
768
|
persistClient(client) {
|
|
@@ -739,7 +770,7 @@ function createPersister(config) {
|
|
|
739
770
|
if (!timer) {
|
|
740
771
|
timer = setTimeout(() => {
|
|
741
772
|
timer = void 0;
|
|
742
|
-
|
|
773
|
+
write(pending);
|
|
743
774
|
}, throttleMs);
|
|
744
775
|
}
|
|
745
776
|
},
|
|
@@ -763,8 +794,10 @@ import { useEffect as useEffect4 } from "react";
|
|
|
763
794
|
var CHECK_INTERVAL_MS = 5 * 60 * 1e3;
|
|
764
795
|
function useStorageMonitor(config) {
|
|
765
796
|
const { key, limits = {}, maxStorageMB = 3 } = config;
|
|
766
|
-
const
|
|
797
|
+
const limitKeysRef = useLatest(Object.keys(limits));
|
|
798
|
+
const limitCount = limitKeysRef.current.length;
|
|
767
799
|
useEffect4(() => {
|
|
800
|
+
const limitKeys = limitKeysRef.current;
|
|
768
801
|
if (typeof window === "undefined" || limitKeys.length === 0) return;
|
|
769
802
|
function evict() {
|
|
770
803
|
const storage = window.localStorage;
|
|
@@ -795,10 +828,11 @@ function useStorageMonitor(config) {
|
|
|
795
828
|
evict();
|
|
796
829
|
const id = setInterval(evict, CHECK_INTERVAL_MS);
|
|
797
830
|
return () => clearInterval(id);
|
|
798
|
-
}, [key, maxStorageMB,
|
|
831
|
+
}, [key, maxStorageMB, limitCount, limitKeysRef]);
|
|
799
832
|
}
|
|
800
833
|
|
|
801
834
|
// src/persistence/use-persistence.ts
|
|
835
|
+
var PERSISTED_DATA_FORMAT = "augur-2";
|
|
802
836
|
function useAugurPersistence(config) {
|
|
803
837
|
const [persister] = useState3(() => createPersister(config));
|
|
804
838
|
const [isMounted, setIsMounted] = useState3(false);
|
|
@@ -812,9 +846,12 @@ function useAugurPersistence(config) {
|
|
|
812
846
|
() => ({
|
|
813
847
|
shouldDehydrateQuery: (query) => {
|
|
814
848
|
const key = query.queryKey;
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
849
|
+
const hasHead = Array.isArray(key) && key.length > 0;
|
|
850
|
+
return shouldPersistOf(
|
|
851
|
+
hasHead,
|
|
852
|
+
query.state.status === "success",
|
|
853
|
+
hasHead && persistableKeys.includes(key[0])
|
|
854
|
+
);
|
|
818
855
|
}
|
|
819
856
|
}),
|
|
820
857
|
[persistableKeys]
|
|
@@ -822,7 +859,7 @@ function useAugurPersistence(config) {
|
|
|
822
859
|
return {
|
|
823
860
|
persister,
|
|
824
861
|
maxAge: isMounted ? config.maxAge ?? 24 * 60 * 60 * 1e3 : 0,
|
|
825
|
-
buster: isDev ? String(Date.now()) : config.buster ?? ""
|
|
862
|
+
buster: isDev ? String(Date.now()) : `${PERSISTED_DATA_FORMAT}:${config.buster ?? ""}`,
|
|
826
863
|
dehydrateOptions
|
|
827
864
|
};
|
|
828
865
|
}
|