create-brainerce-store 1.72.0 → 1.74.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 (88) hide show
  1. package/dist/index.js +37 -4
  2. package/messages/en.json +30 -1
  3. package/messages/he.json +30 -1
  4. package/package.json +1 -1
  5. package/templates/nextjs/base/AGENTS.md.ejs +63 -16
  6. package/templates/nextjs/base/AI-GUIDE.md +17 -4
  7. package/templates/nextjs/base/CLAUDE.md.ejs +67 -20
  8. package/templates/nextjs/base/TRANSLATIONS.md +207 -207
  9. package/templates/nextjs/base/src/app/account/page.tsx +168 -122
  10. package/templates/nextjs/base/src/app/category/[slug]/page.tsx +10 -1
  11. package/templates/nextjs/base/src/app/checkout/page.tsx +1179 -1074
  12. package/templates/nextjs/base/src/app/layout.tsx.ejs +48 -4
  13. package/templates/nextjs/base/src/app/order-confirmation/page.tsx +98 -3
  14. package/templates/nextjs/base/src/app/products/[slug]/page.tsx +14 -2
  15. package/templates/nextjs/base/src/components/account/loyalty-panel.tsx +226 -0
  16. package/templates/nextjs/base/src/components/account/order-history.tsx +422 -371
  17. package/templates/nextjs/base/src/components/account/order-status-timeline.tsx +85 -85
  18. package/templates/nextjs/base/src/components/account/saved-payment-methods.tsx +104 -0
  19. package/templates/nextjs/base/src/components/checkout/checkout-form.tsx +618 -612
  20. package/templates/nextjs/base/src/components/checkout/custom-fields-step.tsx +354 -306
  21. package/templates/nextjs/base/src/components/checkout/tax-display.tsx +141 -74
  22. package/templates/nextjs/base/src/core/hooks/use-cart-page.ts +218 -127
  23. package/templates/nextjs/base/src/core/hooks/use-home-data.ts +63 -51
  24. package/templates/nextjs/base/src/core/hooks/use-product-listing.ts +379 -295
  25. package/templates/nextjs/base/src/core/hooks/use-product-page.ts +33 -1
  26. package/templates/nextjs/base/src/core/lib/capabilities.ts +65 -0
  27. package/templates/nextjs/base/src/core/lib/display-price.ts +139 -0
  28. package/templates/nextjs/base/src/core/lib/region.server.ts +112 -0
  29. package/templates/nextjs/base/src/core/lib/region.ts +112 -0
  30. package/templates/nextjs/base/src/core/lib/store-info.ts +19 -0
  31. package/templates/nextjs/base/src/core/providers/store-provider.tsx.ejs +423 -243
  32. package/templates/nextjs/base/src/ui/cart/cart-item.tsx +174 -164
  33. package/templates/nextjs/base/src/ui/cart/cart-summary.tsx +109 -110
  34. package/templates/nextjs/base/src/ui/cart/cart-view.tsx +219 -176
  35. package/templates/nextjs/base/src/ui/cart/gift-card-input.tsx +191 -0
  36. package/templates/nextjs/base/src/ui/cart/reservation-countdown.tsx +137 -137
  37. package/templates/nextjs/base/src/ui/cart/tax-estimate-line.tsx +108 -0
  38. package/templates/nextjs/base/src/ui/home/home-client.tsx +27 -29
  39. package/templates/nextjs/base/src/ui/layout/header-search.tsx +249 -0
  40. package/templates/nextjs/base/src/ui/layout/region-switcher.tsx +59 -0
  41. package/templates/nextjs/base/src/ui/layout/site-header.tsx.ejs +171 -152
  42. package/templates/nextjs/base/src/ui/product/frequently-bought-together.tsx +197 -191
  43. package/templates/nextjs/base/src/ui/product/product-card.tsx +25 -52
  44. package/templates/nextjs/base/src/ui/product/product-client-section.tsx +12 -3
  45. package/templates/nextjs/base/src/ui/product/product-listing.tsx +460 -446
  46. package/templates/nextjs/base/src/ui/product/recommendation-section.tsx +108 -102
  47. package/templates/nextjs/base/src/ui/product/review-form.tsx +33 -11
  48. package/templates/nextjs/base/src/ui/product/stock-badge.tsx +15 -3
  49. package/templates/nextjs/designs/atelier/app-overlay/layout.tsx.ejs +48 -4
  50. package/templates/nextjs/designs/atelier/messages-patch/en.json +21 -25
  51. package/templates/nextjs/designs/atelier/messages-patch/he.json +21 -25
  52. package/templates/nextjs/designs/atelier/ui/cart/cart-drawer.tsx +177 -181
  53. package/templates/nextjs/designs/atelier/ui/cart/cart-item.tsx +168 -158
  54. package/templates/nextjs/designs/atelier/ui/cart/cart-summary.tsx +106 -107
  55. package/templates/nextjs/designs/atelier/ui/cart/cart-view.tsx +229 -184
  56. package/templates/nextjs/designs/atelier/ui/cart/reservation-countdown.tsx +131 -131
  57. package/templates/nextjs/designs/atelier/ui/cart/tax-estimate-line.tsx +108 -0
  58. package/templates/nextjs/designs/atelier/ui/home/benefits-band.tsx +10 -1
  59. package/templates/nextjs/designs/atelier/ui/home/hero-section.tsx +178 -174
  60. package/templates/nextjs/designs/atelier/ui/home/home-client.tsx +4 -6
  61. package/templates/nextjs/designs/atelier/ui/home/newsletter-section.tsx +150 -78
  62. package/templates/nextjs/designs/atelier/ui/home/testimonials-band.tsx +79 -46
  63. package/templates/nextjs/designs/atelier/ui/layout/header-search.tsx +264 -0
  64. package/templates/nextjs/designs/atelier/ui/layout/region-switcher.tsx +44 -0
  65. package/templates/nextjs/designs/atelier/ui/layout/site-header.tsx.ejs +5 -0
  66. package/templates/nextjs/designs/atelier/ui/product/frequently-bought-together.tsx +202 -196
  67. package/templates/nextjs/designs/atelier/ui/product/product-card.tsx +27 -52
  68. package/templates/nextjs/designs/atelier/ui/product/product-client-section.tsx +12 -3
  69. package/templates/nextjs/designs/atelier/ui/product/product-listing.tsx +332 -260
  70. package/templates/nextjs/designs/atelier/ui/product/recommendation-section.tsx +101 -95
  71. package/templates/nextjs/designs/atelier/ui/product/review-form.tsx +30 -10
  72. package/templates/nextjs/designs/atelier/ui/product/stock-badge.tsx +17 -2
  73. package/templates/nextjs/designs/atelier/ui/shared/icons.tsx +9 -0
  74. package/templates/nextjs/ui-canvas/cart/cart-item.tsx +137 -137
  75. package/templates/nextjs/ui-canvas/cart/cart-summary.tsx +98 -99
  76. package/templates/nextjs/ui-canvas/cart/cart-view.tsx +140 -140
  77. package/templates/nextjs/ui-canvas/cart/gift-card-input.tsx +142 -0
  78. package/templates/nextjs/ui-canvas/cart/reservation-countdown.tsx +124 -124
  79. package/templates/nextjs/ui-canvas/cart/tax-estimate-line.tsx +104 -0
  80. package/templates/nextjs/ui-canvas/home/home-client.tsx +24 -26
  81. package/templates/nextjs/ui-canvas/layout/region-switcher.tsx +51 -0
  82. package/templates/nextjs/ui-canvas/layout/site-header.tsx.ejs +3 -0
  83. package/templates/nextjs/ui-canvas/product/frequently-bought-together.tsx +174 -168
  84. package/templates/nextjs/ui-canvas/product/product-card.tsx +9 -51
  85. package/templates/nextjs/ui-canvas/product/product-client-section.tsx +5 -3
  86. package/templates/nextjs/ui-canvas/product/recommendation-section.tsx +105 -99
  87. package/templates/nextjs/ui-canvas/product/review-form.tsx +9 -1
  88. package/templates/nextjs/ui-canvas/product/stock-badge.tsx +17 -2
@@ -1,158 +1,168 @@
1
- 'use client';
2
-
3
- import { useState } from 'react';
4
- import { CdnImage as Image } from '@/ui/shared/cdn-image';
5
- import type { CartItem as CartItemType } from 'brainerce';
6
- import { getCartItemImage, formatPrice } from 'brainerce';
7
- import { getClient } from '@/core/lib/brainerce';
8
- import { useTranslations } from '@/core/lib/translations';
9
- import { useCurrency } from '@/core/lib/use-currency';
10
- import { LoadingSpinner } from '@/ui/shared/loading-spinner';
11
- import { cn } from '@/core/lib/utils';
12
- import { IconMinus, IconPlus, IconTrash } from '@/ui/shared/icons';
13
-
14
- interface CartItemProps {
15
- item: CartItemType;
16
- onUpdate: () => void;
17
- className?: string;
18
- }
19
-
20
- /**
21
- * Single cart line: image, name, variant, unit price, quantity controls,
22
- * remove, line total; the `item` prop comes from useCartPage().cart.
23
- */
24
- export function CartItem({ item, onUpdate, className }: CartItemProps) {
25
- const t = useTranslations('common');
26
- const td = useTranslations('productDetail');
27
- const currency = useCurrency();
28
- const [updating, setUpdating] = useState(false);
29
- const [removing, setRemoving] = useState(false);
30
-
31
- const productName = item.product.name;
32
- const imageUrl = getCartItemImage(item);
33
- const variantName = item.variant?.name;
34
- const unitPrice = parseFloat(item.unitPrice);
35
- const lineTotal = unitPrice * item.quantity;
36
-
37
- // The server decides purchasability, per line. `isAvailable === false` means
38
- // this line blocks checkout until it is removed, whether the stock ran out
39
- // (a released reservation is the common cause) or the product/variant was
40
- // withdrawn from sale. `unavailableReason` separates the two for the label.
41
- const isUnavailable = item.isAvailable === false;
42
- const unavailableLabel = isUnavailable
43
- ? item.unavailableReason === 'OUT_OF_STOCK'
44
- ? td('outOfStock')
45
- : td('unavailable')
46
- : null;
47
-
48
- async function handleQuantityChange(newQuantity: number) {
49
- if (newQuantity < 1 || updating) return;
50
-
51
- try {
52
- setUpdating(true);
53
- const client = getClient();
54
- await client.smartUpdateCartItem(item.productId, newQuantity, item.variantId || undefined);
55
- onUpdate();
56
- } catch (err) {
57
- console.error('Failed to update quantity:', err);
58
- } finally {
59
- setUpdating(false);
60
- }
61
- }
62
-
63
- async function handleRemove() {
64
- if (removing) return;
65
-
66
- try {
67
- setRemoving(true);
68
- const client = getClient();
69
- await client.smartRemoveFromCart(item.productId, item.variantId || undefined);
70
- onUpdate();
71
- } catch (err) {
72
- console.error('Failed to remove item:', err);
73
- } finally {
74
- setRemoving(false);
75
- }
76
- }
77
-
78
- return (
79
- <div className={cn('flex items-start gap-4', className)}>
80
- {/* Image — `relative` + fixed box are layout-critical for next/image fill */}
81
- <div className="relative h-20 w-20 shrink-0 overflow-hidden rounded-lg border bg-secondary sm:h-24 sm:w-24">
82
- {imageUrl ? (
83
- <Image src={imageUrl} alt={productName} fill sizes="96px" className="object-cover" />
84
- ) : (
85
- <span className="sr-only">{productName}</span>
86
- )}
87
- </div>
88
-
89
- {/* Details */}
90
- <div className="min-w-0 flex-1">
91
- <h3 className="font-sans text-[15px] font-semibold leading-snug text-foreground">
92
- {productName}
93
- </h3>
94
-
95
- {/* Availability badge. This line blocks checkout while it shows. */}
96
- {unavailableLabel && (
97
- <span className="mt-1 inline-flex items-center rounded-full bg-destructive/10 px-2 py-0.5 text-xs font-medium text-destructive">
98
- {unavailableLabel}
99
- </span>
100
- )}
101
-
102
- {/* Variant name */}
103
- {variantName && <p className="mt-0.5 text-xs text-muted-foreground">{variantName}</p>}
104
-
105
- {/* Unit price */}
106
- <p className="mt-1 text-sm text-muted-foreground">
107
- {formatPrice(unitPrice, { currency }) as string}
108
- </p>
109
-
110
- {/* Quantity controls + remove */}
111
- <div className="mt-3 flex flex-wrap items-center gap-3">
112
- <div
113
- className="flex h-9 items-center rounded-full border"
114
- role="group"
115
- aria-label={td('quantityLabel')}
116
- >
117
- <button
118
- type="button"
119
- onClick={() => handleQuantityChange(item.quantity - 1)}
120
- disabled={updating || item.quantity <= 1}
121
- aria-label={td('decreaseQuantity')}
122
- className="flex h-full w-9 items-center justify-center rounded-s-full transition-colors hover:bg-secondary disabled:opacity-40"
123
- >
124
- <IconMinus size={16} />
125
- </button>
126
- <span aria-live="polite" className="min-w-7 text-center text-sm font-semibold">
127
- {updating ? <LoadingSpinner size="sm" /> : item.quantity}
128
- </span>
129
- <button
130
- type="button"
131
- onClick={() => handleQuantityChange(item.quantity + 1)}
132
- disabled={updating || isUnavailable}
133
- aria-label={td('increaseQuantity')}
134
- className="flex h-full w-9 items-center justify-center rounded-e-full transition-colors hover:bg-secondary disabled:opacity-40"
135
- >
136
- <IconPlus size={16} />
137
- </button>
138
- </div>
139
-
140
- <button
141
- type="button"
142
- onClick={handleRemove}
143
- disabled={removing}
144
- className="inline-flex items-center gap-1.5 text-xs font-medium text-muted-foreground transition-colors hover:text-destructive disabled:opacity-50"
145
- >
146
- <IconTrash size={16} />
147
- {removing ? t('removing') : t('remove')}
148
- </button>
149
- </div>
150
- </div>
151
-
152
- {/* Line total */}
153
- <span className="text-base font-semibold text-foreground">
154
- {formatPrice(lineTotal, { currency }) as string}
155
- </span>
156
- </div>
157
- );
158
- }
1
+ 'use client';
2
+
3
+ import { useState } from 'react';
4
+ import { CdnImage as Image } from '@/ui/shared/cdn-image';
5
+ import type { CartItem as CartItemType } from 'brainerce';
6
+ import { getCartItemImage, formatPrice } from 'brainerce';
7
+ import { getClient } from '@/core/lib/brainerce';
8
+ import { useTranslations } from '@/core/lib/translations';
9
+ import { useCurrency } from '@/core/lib/use-currency';
10
+ import { LoadingSpinner } from '@/ui/shared/loading-spinner';
11
+ import { cn } from '@/core/lib/utils';
12
+ import { OrderCustomizations } from '@/components/account/order-customizations';
13
+ import { IconMinus, IconPlus, IconTrash } from '@/ui/shared/icons';
14
+
15
+ interface CartItemProps {
16
+ item: CartItemType;
17
+ onUpdate: () => void;
18
+ className?: string;
19
+ }
20
+
21
+ /**
22
+ * Single cart line: image, name, variant, unit price, quantity controls,
23
+ * remove, line total; the `item` prop comes from useCartPage().cart.
24
+ */
25
+ export function CartItem({ item, onUpdate, className }: CartItemProps) {
26
+ const t = useTranslations('common');
27
+ const td = useTranslations('productDetail');
28
+ const currency = useCurrency();
29
+ const [updating, setUpdating] = useState(false);
30
+ const [removing, setRemoving] = useState(false);
31
+
32
+ const productName = item.product.name;
33
+ const imageUrl = getCartItemImage(item);
34
+ const variantName = item.variant?.name;
35
+ const unitPrice = parseFloat(item.unitPrice);
36
+ const lineTotal = unitPrice * item.quantity;
37
+
38
+ // The server decides purchasability, per line. `isAvailable === false` means
39
+ // this line blocks checkout until it is removed, whether the stock ran out
40
+ // (a released reservation is the common cause) or the product/variant was
41
+ // withdrawn from sale. `unavailableReason` separates the two for the label.
42
+ const isUnavailable = item.isAvailable === false;
43
+ const unavailableLabel = isUnavailable
44
+ ? item.unavailableReason === 'OUT_OF_STOCK'
45
+ ? td('outOfStock')
46
+ : td('unavailable')
47
+ : null;
48
+
49
+ async function handleQuantityChange(newQuantity: number) {
50
+ if (newQuantity < 1 || updating) return;
51
+
52
+ try {
53
+ setUpdating(true);
54
+ const client = getClient();
55
+ await client.smartUpdateCartItem(item.productId, newQuantity, item.variantId || undefined);
56
+ onUpdate();
57
+ } catch (err) {
58
+ console.error('Failed to update quantity:', err);
59
+ } finally {
60
+ setUpdating(false);
61
+ }
62
+ }
63
+
64
+ async function handleRemove() {
65
+ if (removing) return;
66
+
67
+ try {
68
+ setRemoving(true);
69
+ const client = getClient();
70
+ await client.smartRemoveFromCart(item.productId, item.variantId || undefined);
71
+ onUpdate();
72
+ } catch (err) {
73
+ console.error('Failed to remove item:', err);
74
+ } finally {
75
+ setRemoving(false);
76
+ }
77
+ }
78
+
79
+ return (
80
+ <div className={cn('flex items-start gap-4', className)}>
81
+ {/* Image — `relative` + fixed box are layout-critical for next/image fill */}
82
+ <div className="bg-secondary relative h-20 w-20 shrink-0 overflow-hidden rounded-lg border sm:h-24 sm:w-24">
83
+ {imageUrl ? (
84
+ <Image src={imageUrl} alt={productName} fill sizes="96px" className="object-cover" />
85
+ ) : (
86
+ <span className="sr-only">{productName}</span>
87
+ )}
88
+ </div>
89
+
90
+ {/* Details */}
91
+ <div className="min-w-0 flex-1">
92
+ <h3 className="text-foreground font-sans text-[15px] font-semibold leading-snug">
93
+ {productName}
94
+ </h3>
95
+
96
+ {/* Availability badge. This line blocks checkout while it shows. */}
97
+ {unavailableLabel && (
98
+ <span className="bg-destructive/10 text-destructive mt-1 inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium">
99
+ {unavailableLabel}
100
+ </span>
101
+ )}
102
+
103
+ {/* Variant name */}
104
+ {variantName && <p className="text-muted-foreground mt-0.5 text-xs">{variantName}</p>}
105
+
106
+ {/* What the buyer typed / uploaded / picked, keyed by the field's own
107
+ `key` and carrying the merchant's label. The SDK resolves this on
108
+ the cart line itself (`CartItem.customizations`, same shape as
109
+ `OrderItem.customizations`) so no product fetch is needed for the
110
+ labels. Present only when the line has at least one value set, and
111
+ the component returns null on an empty map, so a plain product
112
+ renders exactly as before. */}
113
+ {item.customizations && <OrderCustomizations customizations={item.customizations} />}
114
+
115
+ {/* Unit price */}
116
+ <p className="text-muted-foreground mt-1 text-sm">
117
+ {formatPrice(unitPrice, { currency }) as string}
118
+ </p>
119
+
120
+ {/* Quantity controls + remove */}
121
+ <div className="mt-3 flex flex-wrap items-center gap-3">
122
+ <div
123
+ className="flex h-9 items-center rounded-full border"
124
+ role="group"
125
+ aria-label={td('quantityLabel')}
126
+ >
127
+ <button
128
+ type="button"
129
+ onClick={() => handleQuantityChange(item.quantity - 1)}
130
+ disabled={updating || item.quantity <= 1}
131
+ aria-label={td('decreaseQuantity')}
132
+ className="hover:bg-secondary flex h-full w-9 items-center justify-center rounded-s-full transition-colors disabled:opacity-40"
133
+ >
134
+ <IconMinus size={16} />
135
+ </button>
136
+ <span aria-live="polite" className="min-w-7 text-center text-sm font-semibold">
137
+ {updating ? <LoadingSpinner size="sm" /> : item.quantity}
138
+ </span>
139
+ <button
140
+ type="button"
141
+ onClick={() => handleQuantityChange(item.quantity + 1)}
142
+ disabled={updating || isUnavailable}
143
+ aria-label={td('increaseQuantity')}
144
+ className="hover:bg-secondary flex h-full w-9 items-center justify-center rounded-e-full transition-colors disabled:opacity-40"
145
+ >
146
+ <IconPlus size={16} />
147
+ </button>
148
+ </div>
149
+
150
+ <button
151
+ type="button"
152
+ onClick={handleRemove}
153
+ disabled={removing}
154
+ className="text-muted-foreground hover:text-destructive inline-flex items-center gap-1.5 text-xs font-medium transition-colors disabled:opacity-50"
155
+ >
156
+ <IconTrash size={16} />
157
+ {removing ? t('removing') : t('remove')}
158
+ </button>
159
+ </div>
160
+ </div>
161
+
162
+ {/* Line total */}
163
+ <span className="text-foreground text-base font-semibold">
164
+ {formatPrice(lineTotal, { currency }) as string}
165
+ </span>
166
+ </div>
167
+ );
168
+ }
@@ -1,107 +1,106 @@
1
- 'use client';
2
-
3
- import { formatPrice } from 'brainerce';
4
- import { useTranslations } from '@/core/lib/translations';
5
- import { useCart } from '@/core/providers/store-provider';
6
- import { useCurrency } from '@/core/lib/use-currency';
7
- import { cn } from '@/core/lib/utils';
8
-
9
- interface CartSummaryProps {
10
- className?: string;
11
- }
12
-
13
- /**
14
- * Order summary (subtotal, rule/coupon discounts, shipping, tax note, total);
15
- * all figures come from useCart().totals and useCart().cart. Every discount
16
- * branch is kept merchants rely on the per-rule breakdown.
17
- */
18
- export function CartSummary({ className }: CartSummaryProps) {
19
- const t = useTranslations('cart');
20
- const tc = useTranslations('common');
21
- const { totals, cart } = useCart();
22
- const currency = useCurrency();
23
-
24
- const rules = cart?.appliedDiscounts;
25
- const ruleAmt = cart?.ruleDiscountAmount ? parseFloat(cart.ruleDiscountAmount) : 0;
26
- const couponAmt = totals.discount - ruleAmt;
27
-
28
- return (
29
- <section className={cn(className)}>
30
- <h3 className="mb-4 font-sans text-base font-semibold">{t('orderSummary')}</h3>
31
-
32
- <dl className="space-y-2.5 text-sm">
33
- {/* Subtotal */}
34
- <div className="flex items-center justify-between gap-4">
35
- <dt className="text-muted-foreground">{tc('subtotal')}</dt>
36
- <dd className="font-medium">{formatPrice(totals.subtotal, { currency }) as string}</dd>
37
- </div>
38
-
39
- {/* Rule discounts - show each rule by name */}
40
- {rules && rules.length > 0
41
- ? rules.map((rule) => (
42
- <div key={rule.ruleId} className="flex items-center justify-between gap-4">
43
- <dt className="text-primary">{rule.ruleName}</dt>
44
- <dd className="font-medium text-primary">
45
- -{formatPrice(parseFloat(rule.discountAmount), { currency }) as string}
46
- </dd>
47
- </div>
48
- ))
49
- : ruleAmt > 0 && (
50
- <div className="flex items-center justify-between gap-4">
51
- <dt className="text-primary">{tc('generalDiscount')}</dt>
52
- <dd className="font-medium text-primary">
53
- -{formatPrice(ruleAmt, { currency }) as string}
54
- </dd>
55
- </div>
56
- )}
57
-
58
- {/* Coupon discount */}
59
- {cart?.couponCode && couponAmt > 0 && (
60
- <div className="flex items-center justify-between gap-4">
61
- <dt className="text-primary">
62
- {tc('couponDiscount')} ({cart.couponCode})
63
- </dt>
64
- <dd className="font-medium text-primary">
65
- -{formatPrice(couponAmt, { currency }) as string}
66
- </dd>
67
- </div>
68
- )}
69
-
70
- {/* Fallback: generic discount when no breakdown available */}
71
- {totals.discount > 0 &&
72
- ruleAmt <= 0 &&
73
- !cart?.couponCode &&
74
- (!rules || rules.length === 0) && (
75
- <div className="flex items-center justify-between gap-4">
76
- <dt className="text-primary">{tc('discount')}</dt>
77
- <dd className="font-medium text-primary">
78
- -{formatPrice(totals.discount, { currency }) as string}
79
- </dd>
80
- </div>
81
- )}
82
-
83
- {/* Shipping */}
84
- {totals.shipping > 0 && (
85
- <div className="flex items-center justify-between gap-4">
86
- <dt className="text-muted-foreground">{tc('shipping')}</dt>
87
- <dd className="font-medium">{formatPrice(totals.shipping, { currency }) as string}</dd>
88
- </div>
89
- )}
90
-
91
- {/* Tax */}
92
- <div className="flex items-center justify-between gap-4">
93
- <dt className="text-muted-foreground">{tc('tax')}</dt>
94
- <dd className="text-muted-foreground">{t('taxAtCheckout')}</dd>
95
- </div>
96
-
97
- {/* Total */}
98
- <div className="flex items-center justify-between gap-4 border-t pt-3">
99
- <dt className="text-base font-semibold text-foreground">{tc('total')}</dt>
100
- <dd className="text-lg font-bold text-foreground">
101
- {formatPrice(totals.total, { currency }) as string}
102
- </dd>
103
- </div>
104
- </dl>
105
- </section>
106
- );
107
- }
1
+ 'use client';
2
+
3
+ import { formatPrice } from 'brainerce';
4
+ import { useTranslations } from '@/core/lib/translations';
5
+ import { useCart } from '@/core/providers/store-provider';
6
+ import { TaxEstimateLine } from '@/ui/cart/tax-estimate-line';
7
+ import { useCurrency } from '@/core/lib/use-currency';
8
+ import { cn } from '@/core/lib/utils';
9
+
10
+ interface CartSummaryProps {
11
+ className?: string;
12
+ }
13
+
14
+ /**
15
+ * Order summary (subtotal, rule/coupon discounts, shipping, tax note, total);
16
+ * all figures come from useCart().totals and useCart().cart. Every discount
17
+ * branch is kept — merchants rely on the per-rule breakdown.
18
+ */
19
+ export function CartSummary({ className }: CartSummaryProps) {
20
+ const t = useTranslations('cart');
21
+ const tc = useTranslations('common');
22
+ const { totals, cart } = useCart();
23
+ const currency = useCurrency();
24
+
25
+ const rules = cart?.appliedDiscounts;
26
+ const ruleAmt = cart?.ruleDiscountAmount ? parseFloat(cart.ruleDiscountAmount) : 0;
27
+ const couponAmt = totals.discount - ruleAmt;
28
+
29
+ return (
30
+ <section className={cn(className)}>
31
+ <h3 className="mb-4 font-sans text-base font-semibold">{t('orderSummary')}</h3>
32
+
33
+ <dl className="space-y-2.5 text-sm">
34
+ {/* Subtotal */}
35
+ <div className="flex items-center justify-between gap-4">
36
+ <dt className="text-muted-foreground">{tc('subtotal')}</dt>
37
+ <dd className="font-medium">{formatPrice(totals.subtotal, { currency }) as string}</dd>
38
+ </div>
39
+
40
+ {/* Rule discounts - show each rule by name */}
41
+ {rules && rules.length > 0
42
+ ? rules.map((rule) => (
43
+ <div key={rule.ruleId} className="flex items-center justify-between gap-4">
44
+ <dt className="text-primary">{rule.ruleName}</dt>
45
+ <dd className="font-medium text-primary">
46
+ -{formatPrice(parseFloat(rule.discountAmount), { currency }) as string}
47
+ </dd>
48
+ </div>
49
+ ))
50
+ : ruleAmt > 0 && (
51
+ <div className="flex items-center justify-between gap-4">
52
+ <dt className="text-primary">{tc('generalDiscount')}</dt>
53
+ <dd className="font-medium text-primary">
54
+ -{formatPrice(ruleAmt, { currency }) as string}
55
+ </dd>
56
+ </div>
57
+ )}
58
+
59
+ {/* Coupon discount */}
60
+ {cart?.couponCode && couponAmt > 0 && (
61
+ <div className="flex items-center justify-between gap-4">
62
+ <dt className="text-primary">
63
+ {tc('couponDiscount')} ({cart.couponCode})
64
+ </dt>
65
+ <dd className="font-medium text-primary">
66
+ -{formatPrice(couponAmt, { currency }) as string}
67
+ </dd>
68
+ </div>
69
+ )}
70
+
71
+ {/* Fallback: generic discount when no breakdown available */}
72
+ {totals.discount > 0 &&
73
+ ruleAmt <= 0 &&
74
+ !cart?.couponCode &&
75
+ (!rules || rules.length === 0) && (
76
+ <div className="flex items-center justify-between gap-4">
77
+ <dt className="text-primary">{tc('discount')}</dt>
78
+ <dd className="font-medium text-primary">
79
+ -{formatPrice(totals.discount, { currency }) as string}
80
+ </dd>
81
+ </div>
82
+ )}
83
+
84
+ {/* Shipping */}
85
+ {totals.shipping > 0 && (
86
+ <div className="flex items-center justify-between gap-4">
87
+ <dt className="text-muted-foreground">{tc('shipping')}</dt>
88
+ <dd className="font-medium">{formatPrice(totals.shipping, { currency }) as string}</dd>
89
+ </div>
90
+ )}
91
+
92
+ {/* Tax: a real estimate when the store has a rate for the buyer's
93
+ country, and the original "calculated at checkout" row otherwise. */}
94
+ <TaxEstimateLine />
95
+
96
+ {/* Total */}
97
+ <div className="flex items-center justify-between gap-4 border-t pt-3">
98
+ <dt className="text-base font-semibold text-foreground">{tc('total')}</dt>
99
+ <dd className="text-lg font-bold text-foreground">
100
+ {formatPrice(totals.total, { currency }) as string}
101
+ </dd>
102
+ </div>
103
+ </dl>
104
+ </section>
105
+ );
106
+ }