@simpleapps-com/augur-hooks 2026.11.5 → 2026.11.7

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/dist/index.js CHANGED
@@ -5,9 +5,12 @@ import {
5
5
  } from "./chunk-7SUD5Z2V.js";
6
6
  import {
7
7
  acceptsLookup,
8
+ addAccepted,
9
+ bodyOverflow,
8
10
  cartIdle,
9
11
  clearCheckoutPersistence,
10
12
  clearFromSession,
13
+ closesOnKey,
11
14
  determineOrderReadiness,
12
15
  hasNumberFrom,
13
16
  indexOfUidFrom,
@@ -16,6 +19,7 @@ import {
16
19
  keepsAfterReverse,
17
20
  keyPathFrom,
18
21
  leadingTextsFrom,
22
+ lineUnitPrice,
19
23
  loadFromSession,
20
24
  qtyOrZero,
21
25
  requiredCartUid,
@@ -25,6 +29,7 @@ import {
25
29
  shouldPersistOf,
26
30
  stockEnabled,
27
31
  stockRetryDelay,
32
+ toastShown,
28
33
  useCartHdrUid,
29
34
  useCartLineCount,
30
35
  useCartLines,
@@ -36,8 +41,9 @@ import {
36
41
  useOrderReadiness,
37
42
  usePaymentFlow,
38
43
  useSetCartHdrUid,
39
- useSetCartLines
40
- } from "./chunk-LLTE6COX.js";
44
+ useSetCartLines,
45
+ zeroLabelShown
46
+ } from "./chunk-V3VBNWLM.js";
41
47
 
42
48
  // src/provider.tsx
43
49
  import { createContext, useContext, useMemo } from "react";
@@ -163,7 +169,7 @@ function useFormatPrice(optionsOrLocale, legacyCurrency) {
163
169
  } = resolveFormatting(opts, site?.formatting);
164
170
  const formatPrice = (price, callOptions) => {
165
171
  const effectiveValue = cleanMoney(price * (callOptions?.quantity ?? 1));
166
- if (effectiveValue === 0 && zeroLabel !== void 0) {
172
+ if (zeroLabelShown(effectiveValue === 0, zeroLabel !== void 0)) {
167
173
  return zeroLabel;
168
174
  }
169
175
  const precision = callOptions?.precision ?? basePrecision;
@@ -214,6 +220,7 @@ function useStock(invMastUid, options = {}) {
214
220
  // src/hooks/use-cart-actions.ts
215
221
  import { useCallback } from "react";
216
222
  import { useQueryClient } from "@tanstack/react-query";
223
+ import { lineHasQuantity } from "@simpleapps-com/augur-utils";
217
224
  function cartUidOf(cartHdrUid) {
218
225
  const num = typeof cartHdrUid === "string" ? Number(cartHdrUid) : cartHdrUid;
219
226
  return num === void 0 || Number.isNaN(num) ? void 0 : num;
@@ -316,7 +323,7 @@ function unitsOf(cartLines) {
316
323
  return cartLines.map(unitOf);
317
324
  }
318
325
  function sendToast(notify, kind, showToast, message) {
319
- if (showToast && message !== void 0) notify?.[kind]?.(message);
326
+ if (toastShown(showToast, message !== void 0)) notify?.[kind]?.(message);
320
327
  }
321
328
  async function settleCartOp(op, options, notify, invalidate) {
322
329
  const { endCartOp } = useCartStore.getState();
@@ -352,6 +359,12 @@ function useCartActions(callbacks) {
352
359
  const addToCart = useCallback(
353
360
  async (item, options = {}) => {
354
361
  const { showToast = true, itemId = "" } = options;
362
+ const quantity = Number.isFinite(item.quantity) ? item.quantity : null;
363
+ if (!addAccepted(item.unitOfMeasure, quantity)) {
364
+ sendToast(notify, "error", showToast, "Choose a quantity and a unit of measure.");
365
+ options.onError?.(new Error("Quantity and unit of measure are required"));
366
+ return false;
367
+ }
355
368
  const { uid, lines: liveLines } = liveCart();
356
369
  useCartStore.getState().beginCartOp();
357
370
  setCartLines(buildOptimisticAdd(liveLines, item, uid, itemId));
@@ -384,6 +397,11 @@ function useCartActions(callbacks) {
384
397
  const updateQuantity = useCallback(
385
398
  async (invMastUid, newQuantity, options = {}) => {
386
399
  const { showToast = false, unitOfMeasure } = options;
400
+ if (!lineHasQuantity(Number.isFinite(newQuantity) ? newQuantity : null)) {
401
+ sendToast(notify, "error", showToast, "Choose a quantity.");
402
+ options.onError?.(new Error("Quantity is required"));
403
+ return false;
404
+ }
387
405
  const { uid, lines: liveLines } = liveCart();
388
406
  const target = targetOf(liveLines, invMastUid, unitOfMeasure);
389
407
  if (target === null) return true;
@@ -493,14 +511,14 @@ function useModalLifecycle(isOpen, onClose) {
493
511
  setIsClient(true);
494
512
  }, []);
495
513
  useEffect2(() => {
496
- document.body.style.overflow = isOpen ? "hidden" : "auto";
514
+ document.body.style.overflow = bodyOverflow(isOpen);
497
515
  return () => {
498
516
  document.body.style.overflow = "auto";
499
517
  };
500
518
  }, [isOpen]);
501
519
  useEffect2(() => {
502
520
  const handleEscape = (e) => {
503
- if (e.key === "Escape" && isOpen) onClose();
521
+ if (closesOnKey(e.key, isOpen)) onClose();
504
522
  };
505
523
  document.addEventListener("keydown", handleEscape);
506
524
  return () => document.removeEventListener("keydown", handleEscape);
@@ -642,17 +660,29 @@ function useCartInitialization(session, callbacks) {
642
660
  // src/hooks/use-cart-pricing.ts
643
661
  import { useMemo as useMemo4 } from "react";
644
662
  import { useQueries } from "@tanstack/react-query";
645
- import { cleanMoney as cleanMoney2, derivePrice } from "@simpleapps-com/augur-utils";
646
- function deriveLineUnitPrice(priceData, quantity) {
647
- if (typeof quantity !== "number" || !Number.isFinite(quantity)) return 0;
663
+ import {
664
+ cleanMoney as cleanMoney2,
665
+ derivePrice,
666
+ linePriceAt,
667
+ priceQueryRuns,
668
+ priceQueryUnit,
669
+ pricingLoadingFrom,
670
+ pricingSettledFrom
671
+ } from "@simpleapps-com/augur-utils";
672
+ function derivedOrRefused(priceData, quantity) {
648
673
  try {
649
- const derived = derivePrice({ priceData, quantity });
650
- return derived.state === "priced" ? derived.unitPrice : 0;
674
+ return derivePrice({ priceData, quantity });
651
675
  } catch (error) {
652
- if (error instanceof RangeError) return 0;
676
+ if (error instanceof RangeError) return null;
653
677
  throw error;
654
678
  }
655
679
  }
680
+ function deriveLineUnitPrice(priceData, quantity) {
681
+ const hasQuantity = typeof quantity === "number" && Number.isFinite(quantity);
682
+ const derived = hasQuantity ? derivedOrRefused(priceData, quantity) : null;
683
+ const priced = derived?.state === "priced";
684
+ return lineUnitPrice(hasQuantity, priced, priced ? derived.unitPrice : 0);
685
+ }
656
686
  function lineTotal(unitPrice, quantity) {
657
687
  return Number.isFinite(quantity) ? cleanMoney2(unitPrice * quantity) : 0;
658
688
  }
@@ -663,48 +693,63 @@ function useCartPricing(options = {}) {
663
693
  const cartLines = useCartLines();
664
694
  const customerId = options.customerId ?? auth?.customerId ?? site?.defaults?.customerId;
665
695
  const priceFn = options.queryFn;
696
+ const runs = cartLines.map((line) => priceQueryRuns(!!customerId, !!line.itemId, line.quantity));
666
697
  const priceQueries = useQueries({
667
- queries: cartLines.map((line) => {
668
- const quantity = line.quantity ?? 1;
698
+ queries: cartLines.map((line, index) => {
699
+ const quantity = line.quantity ?? 0;
700
+ const enabled = runs[index] === true;
701
+ const unitOfMeasure = priceQueryUnit(line.unitOfMeasure) ?? void 0;
669
702
  if (priceFn) {
670
703
  return {
671
- queryKey: ["price", line.itemId?.toUpperCase() || "", customerId, quantity],
672
- queryFn: () => priceFn(line.itemId, customerId, quantity),
704
+ queryKey: [
705
+ "price",
706
+ line.itemId?.toUpperCase() || "",
707
+ customerId,
708
+ quantity,
709
+ unitOfMeasure ?? ""
710
+ ],
711
+ queryFn: () => priceFn(line.itemId, customerId, quantity, unitOfMeasure),
673
712
  refetchOnReconnect: true,
674
713
  refetchOnWindowFocus: false,
675
714
  meta: { persist: true },
676
- enabled: !!customerId && !!line.itemId
715
+ enabled
677
716
  };
678
717
  }
679
718
  const proxyOpts = proxy.pricing.priceEngine.list({
680
719
  itemId: line.itemId?.toUpperCase() || "",
681
720
  customerId: Number(customerId),
682
- quantity
721
+ quantity,
722
+ unitOfMeasure
683
723
  });
684
724
  return {
685
725
  ...proxyOpts,
686
- enabled: !!customerId && !!line.itemId,
726
+ enabled,
687
727
  refetchOnReconnect: true,
688
728
  refetchOnWindowFocus: false,
689
729
  meta: { persist: true }
690
730
  };
691
731
  })
692
732
  });
733
+ const linePrices = useMemo4(
734
+ () => cartLines.map((line, index) => {
735
+ const priceData = priceQueries[index]?.data;
736
+ if (!priceData) return null;
737
+ return {
738
+ unitPrice: deriveLineUnitPrice(priceData, line.quantity),
739
+ priceData
740
+ };
741
+ }),
742
+ [cartLines, priceQueries]
743
+ );
744
+ const lineUnitPrices = useMemo4(() => linePrices.map((p) => p?.unitPrice ?? 0), [linePrices]);
693
745
  const prices = useMemo4(() => {
694
746
  const map = {};
695
747
  cartLines.forEach((line, index) => {
696
- const queryResult = priceQueries[index];
697
- const priceData = queryResult?.data;
698
- if (priceData) {
699
- const unitPrice = deriveLineUnitPrice(priceData, line.quantity);
700
- map[line.itemId.toUpperCase()] = {
701
- unitPrice,
702
- priceData
703
- };
704
- }
748
+ const linePrice = linePrices[index];
749
+ if (linePrice) map[line.itemId.toUpperCase()] = linePrice;
705
750
  });
706
751
  return map;
707
- }, [cartLines, priceQueries]);
752
+ }, [cartLines, linePrices]);
708
753
  const getUnitPrice = useMemo4(() => {
709
754
  return (itemId) => {
710
755
  return prices[itemId?.toUpperCase()]?.unitPrice ?? 0;
@@ -715,19 +760,36 @@ function useCartPricing(options = {}) {
715
760
  return lineTotal(getUnitPrice(itemId), quantity);
716
761
  };
717
762
  }, [getUnitPrice]);
763
+ const getLineUnitPrice = useMemo4(() => {
764
+ return (index) => linePriceAt(lineUnitPrices, index);
765
+ }, [lineUnitPrices]);
766
+ const getLineTotal = useMemo4(() => {
767
+ return (index) => lineTotal(linePriceAt(lineUnitPrices, index), cartLines[index]?.quantity ?? null);
768
+ }, [cartLines, lineUnitPrices]);
718
769
  const subtotal = useMemo4(() => {
719
- return cartLines.reduce((sum, line) => {
720
- const unitPrice = prices[line.itemId.toUpperCase()]?.unitPrice ?? 0;
721
- return cleanMoney2(sum + lineTotal(unitPrice, line.quantity));
722
- }, 0);
723
- }, [cartLines, prices]);
724
- const isLoading = priceQueries.some((q) => q.isLoading);
725
- const isSuccess = priceQueries.every((q) => q.isSuccess);
770
+ return cartLines.reduce(
771
+ (sum, line, index) => cleanMoney2(sum + lineTotal(linePriceAt(lineUnitPrices, index), line.quantity)),
772
+ 0
773
+ );
774
+ }, [cartLines, lineUnitPrices]);
775
+ const isLoading = pricingLoadingFrom(
776
+ priceQueries.map((q) => q.isLoading),
777
+ runs,
778
+ 0
779
+ );
780
+ const isSuccess = pricingSettledFrom(
781
+ priceQueries.map((q) => q.isSuccess),
782
+ runs,
783
+ 0
784
+ );
726
785
  const isError = priceQueries.some((q) => q.isError);
727
786
  return {
728
787
  prices,
788
+ linePrices,
729
789
  getUnitPrice,
730
790
  getItemTotal,
791
+ getLineUnitPrice,
792
+ getLineTotal,
731
793
  subtotal,
732
794
  isLoading,
733
795
  isSuccess,