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,164 +1,174 @@
1
- 'use client';
2
-
3
- import { useState } from 'react';
4
- import { Image as ImageIcon } from 'lucide-react';
5
- import { CdnImage as Image } from '@/ui/shared/cdn-image';
6
- import type { CartItem as CartItemType } from 'brainerce';
7
- import { getCartItemImage, formatPrice } from 'brainerce';
8
- import { getClient } from '@/core/lib/brainerce';
9
- import { useTranslations } from '@/core/lib/translations';
10
- import { useCurrency } from '@/core/lib/use-currency';
11
- import { LoadingSpinner } from '@/ui/shared/loading-spinner';
12
- import { cn } from '@/core/lib/utils';
13
-
14
- interface CartItemProps {
15
- item: CartItemType;
16
- onUpdate: () => void;
17
- className?: string;
18
- }
19
-
20
- export function CartItem({ item, onUpdate, className }: CartItemProps) {
21
- const t = useTranslations('common');
22
- const td = useTranslations('productDetail');
23
- const currency = useCurrency();
24
- const [updating, setUpdating] = useState(false);
25
- const [removing, setRemoving] = useState(false);
26
-
27
- const productName = item.product.name;
28
- const imageUrl = getCartItemImage(item);
29
- const variantName = item.variant?.name;
30
- const unitPrice = parseFloat(item.unitPrice);
31
- const lineTotal = unitPrice * item.quantity;
32
-
33
- // The server decides purchasability, per line. `isAvailable === false` means
34
- // this line blocks checkout until it is removed, whether the stock ran out
35
- // (a released reservation is the common cause) or the product/variant was
36
- // withdrawn from sale. `unavailableReason` separates the two for the label.
37
- const isUnavailable = item.isAvailable === false;
38
- const unavailableLabel = isUnavailable
39
- ? item.unavailableReason === 'OUT_OF_STOCK'
40
- ? td('outOfStock')
41
- : td('unavailable')
42
- : null;
43
-
44
- async function handleQuantityChange(newQuantity: number) {
45
- if (newQuantity < 1 || updating) return;
46
-
47
- try {
48
- setUpdating(true);
49
- const client = getClient();
50
- await client.smartUpdateCartItem(item.productId, newQuantity, item.variantId || undefined);
51
- onUpdate();
52
- } catch (err) {
53
- console.error('Failed to update quantity:', err);
54
- } finally {
55
- setUpdating(false);
56
- }
57
- }
58
-
59
- async function handleRemove() {
60
- if (removing) return;
61
-
62
- try {
63
- setRemoving(true);
64
- const client = getClient();
65
- await client.smartRemoveFromCart(item.productId, item.variantId || undefined);
66
- onUpdate();
67
- } catch (err) {
68
- console.error('Failed to remove item:', err);
69
- } finally {
70
- setRemoving(false);
71
- }
72
- }
73
-
74
- return (
75
- <div
76
- className={cn(
77
- 'border-border flex gap-4 border-b py-4 last:border-0',
78
- (updating || removing) && 'opacity-60',
79
- className
80
- )}
81
- >
82
- {/* Image */}
83
- <div className="bg-muted relative h-20 w-20 flex-shrink-0 overflow-hidden rounded">
84
- {imageUrl ? (
85
- <Image src={imageUrl} alt={productName} fill sizes="80px" className="object-cover" />
86
- ) : (
87
- <div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
88
- <ImageIcon className="h-8 w-8" strokeWidth={1.5} aria-hidden="true" />
89
- </div>
90
- )}
91
- </div>
92
-
93
- {/* Details */}
94
- <div className="min-w-0 flex-1">
95
- <h3 className="text-foreground truncate text-sm font-medium">{productName}</h3>
96
-
97
- {/* Availability badge. This line blocks checkout while it shows. */}
98
- {unavailableLabel && (
99
- <span className="bg-destructive/10 text-destructive mt-1 inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium">
100
- {unavailableLabel}
101
- </span>
102
- )}
103
-
104
- {/* Variant name */}
105
- {variantName && <p className="text-muted-foreground mt-1 text-xs">{variantName}</p>}
106
-
107
- {/* Unit price */}
108
- <p className="text-muted-foreground mt-1 text-sm">
109
- {formatPrice(unitPrice, { currency }) as string}
110
- </p>
111
-
112
- {/* Quantity controls */}
113
- <div className="mt-2 flex items-center gap-3">
114
- <div className="border-border flex items-center rounded border">
115
- <button
116
- type="button"
117
- onClick={() => handleQuantityChange(item.quantity - 1)}
118
- disabled={updating || item.quantity <= 1}
119
- className="text-foreground hover:bg-muted px-2 py-1 text-sm transition-colors disabled:cursor-not-allowed disabled:opacity-40"
120
- aria-label={td('decreaseQuantity')}
121
- >
122
- -
123
- </button>
124
- <span className="text-foreground min-w-[2.5rem] px-3 py-1 text-center text-sm font-medium">
125
- {updating ? (
126
- <LoadingSpinner
127
- size="sm"
128
- className="border-muted-foreground/30 border-t-foreground mx-auto"
129
- />
130
- ) : (
131
- item.quantity
132
- )}
133
- </span>
134
- <button
135
- type="button"
136
- onClick={() => handleQuantityChange(item.quantity + 1)}
137
- disabled={updating || isUnavailable}
138
- className="text-foreground hover:bg-muted px-2 py-1 text-sm transition-colors disabled:cursor-not-allowed disabled:opacity-40"
139
- aria-label={td('increaseQuantity')}
140
- >
141
- +
142
- </button>
143
- </div>
144
-
145
- <button
146
- type="button"
147
- onClick={handleRemove}
148
- disabled={removing}
149
- className="text-destructive hover:text-destructive/80 text-xs transition-colors disabled:opacity-40"
150
- >
151
- {removing ? t('removing') : t('remove')}
152
- </button>
153
- </div>
154
- </div>
155
-
156
- {/* Line total */}
157
- <div className="flex-shrink-0 text-end">
158
- <span className="text-foreground text-sm font-medium">
159
- {formatPrice(lineTotal, { currency }) as string}
160
- </span>
161
- </div>
162
- </div>
163
- );
164
- }
1
+ 'use client';
2
+
3
+ import { useState } from 'react';
4
+ import { Image as ImageIcon } from 'lucide-react';
5
+ import { CdnImage as Image } from '@/ui/shared/cdn-image';
6
+ import type { CartItem as CartItemType } from 'brainerce';
7
+ import { getCartItemImage, formatPrice } from 'brainerce';
8
+ import { getClient } from '@/core/lib/brainerce';
9
+ import { useTranslations } from '@/core/lib/translations';
10
+ import { useCurrency } from '@/core/lib/use-currency';
11
+ import { LoadingSpinner } from '@/ui/shared/loading-spinner';
12
+ import { cn } from '@/core/lib/utils';
13
+ import { OrderCustomizations } from '@/components/account/order-customizations';
14
+
15
+ interface CartItemProps {
16
+ item: CartItemType;
17
+ onUpdate: () => void;
18
+ className?: string;
19
+ }
20
+
21
+ export function CartItem({ item, onUpdate, className }: CartItemProps) {
22
+ const t = useTranslations('common');
23
+ const td = useTranslations('productDetail');
24
+ const currency = useCurrency();
25
+ const [updating, setUpdating] = useState(false);
26
+ const [removing, setRemoving] = useState(false);
27
+
28
+ const productName = item.product.name;
29
+ const imageUrl = getCartItemImage(item);
30
+ const variantName = item.variant?.name;
31
+ const unitPrice = parseFloat(item.unitPrice);
32
+ const lineTotal = unitPrice * item.quantity;
33
+
34
+ // The server decides purchasability, per line. `isAvailable === false` means
35
+ // this line blocks checkout until it is removed, whether the stock ran out
36
+ // (a released reservation is the common cause) or the product/variant was
37
+ // withdrawn from sale. `unavailableReason` separates the two for the label.
38
+ const isUnavailable = item.isAvailable === false;
39
+ const unavailableLabel = isUnavailable
40
+ ? item.unavailableReason === 'OUT_OF_STOCK'
41
+ ? td('outOfStock')
42
+ : td('unavailable')
43
+ : null;
44
+
45
+ async function handleQuantityChange(newQuantity: number) {
46
+ if (newQuantity < 1 || updating) return;
47
+
48
+ try {
49
+ setUpdating(true);
50
+ const client = getClient();
51
+ await client.smartUpdateCartItem(item.productId, newQuantity, item.variantId || undefined);
52
+ onUpdate();
53
+ } catch (err) {
54
+ console.error('Failed to update quantity:', err);
55
+ } finally {
56
+ setUpdating(false);
57
+ }
58
+ }
59
+
60
+ async function handleRemove() {
61
+ if (removing) return;
62
+
63
+ try {
64
+ setRemoving(true);
65
+ const client = getClient();
66
+ await client.smartRemoveFromCart(item.productId, item.variantId || undefined);
67
+ onUpdate();
68
+ } catch (err) {
69
+ console.error('Failed to remove item:', err);
70
+ } finally {
71
+ setRemoving(false);
72
+ }
73
+ }
74
+
75
+ return (
76
+ <div
77
+ className={cn(
78
+ 'border-border flex gap-4 border-b py-4 last:border-0',
79
+ (updating || removing) && 'opacity-60',
80
+ className
81
+ )}
82
+ >
83
+ {/* Image */}
84
+ <div className="bg-muted relative h-20 w-20 flex-shrink-0 overflow-hidden rounded">
85
+ {imageUrl ? (
86
+ <Image src={imageUrl} alt={productName} fill sizes="80px" className="object-cover" />
87
+ ) : (
88
+ <div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
89
+ <ImageIcon className="h-8 w-8" strokeWidth={1.5} aria-hidden="true" />
90
+ </div>
91
+ )}
92
+ </div>
93
+
94
+ {/* Details */}
95
+ <div className="min-w-0 flex-1">
96
+ <h3 className="text-foreground truncate text-sm font-medium">{productName}</h3>
97
+
98
+ {/* Availability badge. This line blocks checkout while it shows. */}
99
+ {unavailableLabel && (
100
+ <span className="bg-destructive/10 text-destructive mt-1 inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium">
101
+ {unavailableLabel}
102
+ </span>
103
+ )}
104
+
105
+ {/* Variant name */}
106
+ {variantName && <p className="text-muted-foreground mt-1 text-xs">{variantName}</p>}
107
+
108
+ {/* What the buyer typed / uploaded / picked, keyed by the field's own
109
+ `key` and carrying the merchant's label. The SDK resolves this on
110
+ the cart line itself (`CartItem.customizations`, same shape as
111
+ `OrderItem.customizations`) so no product fetch is needed for the
112
+ labels. Present only when the line has at least one value set, and
113
+ the component returns null on an empty map, so a plain product
114
+ renders exactly as before. */}
115
+ {item.customizations && <OrderCustomizations customizations={item.customizations} />}
116
+
117
+ {/* Unit price */}
118
+ <p className="text-muted-foreground mt-1 text-sm">
119
+ {formatPrice(unitPrice, { currency }) as string}
120
+ </p>
121
+
122
+ {/* Quantity controls */}
123
+ <div className="mt-2 flex items-center gap-3">
124
+ <div className="border-border flex items-center rounded border">
125
+ <button
126
+ type="button"
127
+ onClick={() => handleQuantityChange(item.quantity - 1)}
128
+ disabled={updating || item.quantity <= 1}
129
+ className="text-foreground hover:bg-muted px-2 py-1 text-sm transition-colors disabled:cursor-not-allowed disabled:opacity-40"
130
+ aria-label={td('decreaseQuantity')}
131
+ >
132
+ -
133
+ </button>
134
+ <span className="text-foreground min-w-[2.5rem] px-3 py-1 text-center text-sm font-medium">
135
+ {updating ? (
136
+ <LoadingSpinner
137
+ size="sm"
138
+ className="border-muted-foreground/30 border-t-foreground mx-auto"
139
+ />
140
+ ) : (
141
+ item.quantity
142
+ )}
143
+ </span>
144
+ <button
145
+ type="button"
146
+ onClick={() => handleQuantityChange(item.quantity + 1)}
147
+ disabled={updating || isUnavailable}
148
+ className="text-foreground hover:bg-muted px-2 py-1 text-sm transition-colors disabled:cursor-not-allowed disabled:opacity-40"
149
+ aria-label={td('increaseQuantity')}
150
+ >
151
+ +
152
+ </button>
153
+ </div>
154
+
155
+ <button
156
+ type="button"
157
+ onClick={handleRemove}
158
+ disabled={removing}
159
+ className="text-destructive hover:text-destructive/80 text-xs transition-colors disabled:opacity-40"
160
+ >
161
+ {removing ? t('removing') : t('remove')}
162
+ </button>
163
+ </div>
164
+ </div>
165
+
166
+ {/* Line total */}
167
+ <div className="flex-shrink-0 text-end">
168
+ <span className="text-foreground text-sm font-medium">
169
+ {formatPrice(lineTotal, { currency }) as string}
170
+ </span>
171
+ </div>
172
+ </div>
173
+ );
174
+ }
@@ -1,110 +1,109 @@
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 { Separator } from '@/components/ui/separator';
8
- import { cn } from '@/core/lib/utils';
9
-
10
- interface CartSummaryProps {
11
- className?: string;
12
- }
13
-
14
- export function CartSummary({ className }: CartSummaryProps) {
15
- const t = useTranslations('cart');
16
- const tc = useTranslations('common');
17
- const { totals, cart } = useCart();
18
- const currency = useCurrency();
19
-
20
- const rules = cart?.appliedDiscounts;
21
- const ruleAmt = cart?.ruleDiscountAmount ? parseFloat(cart.ruleDiscountAmount) : 0;
22
- const couponAmt = totals.discount - ruleAmt;
23
-
24
- return (
25
- <div className={cn('space-y-3', className)}>
26
- <h3 className="text-foreground text-lg font-semibold">{t('orderSummary')}</h3>
27
-
28
- <div className="space-y-2 text-sm">
29
- {/* Subtotal */}
30
- <div className="flex items-center justify-between">
31
- <span className="text-muted-foreground">{tc('subtotal')}</span>
32
- <span className="text-foreground font-medium">
33
- {formatPrice(totals.subtotal, { currency }) as string}
34
- </span>
35
- </div>
36
-
37
- {/* Rule discounts - show each rule by name */}
38
- {rules && rules.length > 0
39
- ? rules.map((rule) => (
40
- <div key={rule.ruleId} className="flex items-center justify-between">
41
- <span className="text-muted-foreground">{rule.ruleName}</span>
42
- <span className="text-destructive font-medium">
43
- -{formatPrice(parseFloat(rule.discountAmount), { currency }) as string}
44
- </span>
45
- </div>
46
- ))
47
- : ruleAmt > 0 && (
48
- <div className="flex items-center justify-between">
49
- <span className="text-muted-foreground">{tc('generalDiscount')}</span>
50
- <span className="text-destructive font-medium">
51
- -{formatPrice(ruleAmt, { currency }) as string}
52
- </span>
53
- </div>
54
- )}
55
-
56
- {/* Coupon discount */}
57
- {cart?.couponCode && couponAmt > 0 && (
58
- <div className="flex items-center justify-between">
59
- <span className="text-muted-foreground">
60
- {tc('couponDiscount')} ({cart.couponCode})
61
- </span>
62
- <span className="text-destructive font-medium">
63
- -{formatPrice(couponAmt, { currency }) as string}
64
- </span>
65
- </div>
66
- )}
67
-
68
- {/* Fallback: generic discount when no breakdown available */}
69
- {totals.discount > 0 &&
70
- ruleAmt <= 0 &&
71
- !cart?.couponCode &&
72
- (!rules || rules.length === 0) && (
73
- <div className="flex items-center justify-between">
74
- <span className="text-muted-foreground">{tc('discount')}</span>
75
- <span className="text-destructive font-medium">
76
- -{formatPrice(totals.discount, { currency }) as string}
77
- </span>
78
- </div>
79
- )}
80
-
81
- {/* Shipping */}
82
- {totals.shipping > 0 && (
83
- <div className="flex items-center justify-between">
84
- <span className="text-muted-foreground">{tc('shipping')}</span>
85
- <span className="text-foreground font-medium">
86
- {formatPrice(totals.shipping, { currency }) as string}
87
- </span>
88
- </div>
89
- )}
90
-
91
- {/* Tax */}
92
- <div className="flex items-center justify-between">
93
- <span className="text-muted-foreground">{tc('tax')}</span>
94
- <span className="text-muted-foreground text-xs">{t('taxAtCheckout')}</span>
95
- </div>
96
-
97
- {/* Divider */}
98
- <Separator className="mt-2" />
99
- <div className="pt-1">
100
- <div className="flex items-center justify-between">
101
- <span className="text-foreground font-semibold">{tc('total')}</span>
102
- <span className="text-foreground text-base font-semibold">
103
- {formatPrice(totals.total, { currency }) as string}
104
- </span>
105
- </div>
106
- </div>
107
- </div>
108
- </div>
109
- );
110
- }
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 { Separator } from '@/components/ui/separator';
9
+ import { cn } from '@/core/lib/utils';
10
+
11
+ interface CartSummaryProps {
12
+ className?: string;
13
+ }
14
+
15
+ export function CartSummary({ className }: CartSummaryProps) {
16
+ const t = useTranslations('cart');
17
+ const tc = useTranslations('common');
18
+ const { totals, cart } = useCart();
19
+ const currency = useCurrency();
20
+
21
+ const rules = cart?.appliedDiscounts;
22
+ const ruleAmt = cart?.ruleDiscountAmount ? parseFloat(cart.ruleDiscountAmount) : 0;
23
+ const couponAmt = totals.discount - ruleAmt;
24
+
25
+ return (
26
+ <div className={cn('space-y-3', className)}>
27
+ <h3 className="text-foreground text-lg font-semibold">{t('orderSummary')}</h3>
28
+
29
+ <div className="space-y-2 text-sm">
30
+ {/* Subtotal */}
31
+ <div className="flex items-center justify-between">
32
+ <span className="text-muted-foreground">{tc('subtotal')}</span>
33
+ <span className="text-foreground font-medium">
34
+ {formatPrice(totals.subtotal, { currency }) as string}
35
+ </span>
36
+ </div>
37
+
38
+ {/* Rule discounts - show each rule by name */}
39
+ {rules && rules.length > 0
40
+ ? rules.map((rule) => (
41
+ <div key={rule.ruleId} className="flex items-center justify-between">
42
+ <span className="text-muted-foreground">{rule.ruleName}</span>
43
+ <span className="text-destructive font-medium">
44
+ -{formatPrice(parseFloat(rule.discountAmount), { currency }) as string}
45
+ </span>
46
+ </div>
47
+ ))
48
+ : ruleAmt > 0 && (
49
+ <div className="flex items-center justify-between">
50
+ <span className="text-muted-foreground">{tc('generalDiscount')}</span>
51
+ <span className="text-destructive font-medium">
52
+ -{formatPrice(ruleAmt, { currency }) as string}
53
+ </span>
54
+ </div>
55
+ )}
56
+
57
+ {/* Coupon discount */}
58
+ {cart?.couponCode && couponAmt > 0 && (
59
+ <div className="flex items-center justify-between">
60
+ <span className="text-muted-foreground">
61
+ {tc('couponDiscount')} ({cart.couponCode})
62
+ </span>
63
+ <span className="text-destructive font-medium">
64
+ -{formatPrice(couponAmt, { currency }) as string}
65
+ </span>
66
+ </div>
67
+ )}
68
+
69
+ {/* Fallback: generic discount when no breakdown available */}
70
+ {totals.discount > 0 &&
71
+ ruleAmt <= 0 &&
72
+ !cart?.couponCode &&
73
+ (!rules || rules.length === 0) && (
74
+ <div className="flex items-center justify-between">
75
+ <span className="text-muted-foreground">{tc('discount')}</span>
76
+ <span className="text-destructive font-medium">
77
+ -{formatPrice(totals.discount, { currency }) as string}
78
+ </span>
79
+ </div>
80
+ )}
81
+
82
+ {/* Shipping */}
83
+ {totals.shipping > 0 && (
84
+ <div className="flex items-center justify-between">
85
+ <span className="text-muted-foreground">{tc('shipping')}</span>
86
+ <span className="text-foreground font-medium">
87
+ {formatPrice(totals.shipping, { currency }) as string}
88
+ </span>
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
+ {/* Divider */}
97
+ <Separator className="mt-2" />
98
+ <div className="pt-1">
99
+ <div className="flex items-center justify-between">
100
+ <span className="text-foreground font-semibold">{tc('total')}</span>
101
+ <span className="text-foreground text-base font-semibold">
102
+ {formatPrice(totals.total, { currency }) as string}
103
+ </span>
104
+ </div>
105
+ </div>
106
+ </div>
107
+ </div>
108
+ );
109
+ }