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,371 +1,422 @@
1
- 'use client';
2
-
3
- import { useState, useEffect } from 'react';
4
- import { CdnImage as Image } from '@/ui/shared/cdn-image';
5
- import type { Order, OrderStatus, OrderDownloadLink } from 'brainerce';
6
- import { formatPrice } from 'brainerce';
7
- import { getClient } from '@/core/lib/brainerce';
8
- import { useCurrency } from '@/core/lib/use-currency';
9
- import { useTranslations } from '@/core/lib/translations';
10
- import { cn } from '@/core/lib/utils';
11
- import { OrderCustomizations } from './order-customizations';
12
- import { OrderStatusTimeline, ORDER_STATUS_LABEL_KEYS } from './order-status-timeline';
13
- import { OrderShippingBlock } from './order-shipping-block';
14
- import { OrderPaymentBlock } from './order-payment-block';
15
-
16
- /**
17
- * Badge colour per order status. The API sends the status UPPERCASE and
18
- * verbatim, so these keys are uppercase too. Do NOT lowercase `order.status`
19
- * before the lookup: every row would miss and render as "Pending".
20
- * Labels come from ORDER_STATUS_LABEL_KEYS so the two stay in step.
21
- */
22
- const STATUS_STYLES: Record<OrderStatus, string> = {
23
- DRAFT: 'bg-muted text-muted-foreground',
24
- PENDING: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-950/30 dark:text-yellow-400',
25
- PROCESSING: 'bg-blue-100 text-blue-800 dark:bg-blue-950/30 dark:text-blue-400',
26
- ON_HOLD: 'bg-amber-100 text-amber-800 dark:bg-amber-950/30 dark:text-amber-400',
27
- PAID: 'bg-emerald-100 text-emerald-800 dark:bg-emerald-950/30 dark:text-emerald-400',
28
- SHIPPED: 'bg-purple-100 text-purple-800 dark:bg-purple-950/30 dark:text-purple-400',
29
- DELIVERED: 'bg-green-100 text-green-800 dark:bg-green-950/30 dark:text-green-400',
30
- COMPLETED: 'bg-green-100 text-green-800 dark:bg-green-950/30 dark:text-green-400',
31
- FULFILLED: 'bg-teal-100 text-teal-800 dark:bg-teal-950/30 dark:text-teal-400',
32
- CANCELLED: 'bg-red-100 text-red-800 dark:bg-red-950/30 dark:text-red-400',
33
- REFUNDED: 'bg-orange-100 text-orange-800 dark:bg-orange-950/30 dark:text-orange-400',
34
- PARTIALLY_REFUNDED: 'bg-orange-100 text-orange-800 dark:bg-orange-950/30 dark:text-orange-400',
35
- };
36
-
37
- interface OrderHistoryProps {
38
- orders: Order[];
39
- className?: string;
40
- }
41
-
42
- export function OrderHistory({ orders, className }: OrderHistoryProps) {
43
- const t = useTranslations('account');
44
- if (orders.length === 0) {
45
- return (
46
- <div className={cn('py-12 text-center', className)}>
47
- <svg
48
- className="text-muted-foreground mx-auto mb-3 h-12 w-12"
49
- fill="none"
50
- viewBox="0 0 24 24"
51
- stroke="currentColor"
52
- >
53
- <path
54
- strokeLinecap="round"
55
- strokeLinejoin="round"
56
- strokeWidth={1.5}
57
- d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"
58
- />
59
- </svg>
60
- <h3 className="text-foreground text-lg font-semibold">{t('noOrders')}</h3>
61
- <p className="text-muted-foreground mt-1 text-sm">{t('noOrdersDesc')}</p>
62
- </div>
63
- );
64
- }
65
-
66
- return (
67
- <div className={cn('space-y-4', className)}>
68
- {orders.map((order) => (
69
- <OrderCard key={order.id} order={order} />
70
- ))}
71
- </div>
72
- );
73
- }
74
-
75
- function OrderCard({ order }: { order: Order }) {
76
- const t = useTranslations('account');
77
- const tc = useTranslations('common');
78
- const [expanded, setExpanded] = useState(false);
79
- const statusLabelKey = ORDER_STATUS_LABEL_KEYS[order.status] || 'statusPending';
80
- const statusClassName = STATUS_STYLES[order.status] || STATUS_STYLES.PENDING;
81
- const currency = useCurrency(order.currency);
82
- const totalAmount = order.totalAmount || order.total || '0';
83
-
84
- return (
85
- <div className="border-border overflow-hidden rounded-lg border">
86
- {/* Order header */}
87
- <button
88
- type="button"
89
- onClick={() => setExpanded(!expanded)}
90
- className="hover:bg-muted/50 flex w-full items-center justify-between p-4 text-start transition-colors"
91
- >
92
- <div className="min-w-0 flex-1">
93
- <div className="flex flex-wrap items-center gap-3">
94
- <span className="text-foreground text-sm font-semibold">
95
- {order.orderNumber || `${t('orderPrefix')} ${order.id.slice(0, 8)}`}
96
- </span>
97
- <span
98
- className={cn(
99
- 'inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium',
100
- statusClassName
101
- )}
102
- >
103
- {t(statusLabelKey)}
104
- </span>
105
- </div>
106
- <div className="text-muted-foreground mt-1 flex items-center gap-4 text-xs">
107
- <span>
108
- {order.createdAt && !isNaN(new Date(order.createdAt).getTime())
109
- ? new Date(order.createdAt).toLocaleDateString(undefined, {
110
- year: 'numeric',
111
- month: 'short',
112
- day: 'numeric',
113
- })
114
- : ''}
115
- </span>
116
- <span>
117
- {order.items.length} {order.items.length === 1 ? tc('item') : tc('items')}
118
- </span>
119
- </div>
120
- </div>
121
-
122
- <div className="flex flex-shrink-0 items-center gap-3">
123
- <span className="text-foreground text-sm font-semibold">
124
- {formatPrice(parseFloat(totalAmount), { currency }) as string}
125
- </span>
126
- <svg
127
- className={cn(
128
- 'text-muted-foreground h-4 w-4 transition-transform',
129
- expanded && 'rotate-180'
130
- )}
131
- fill="none"
132
- viewBox="0 0 24 24"
133
- stroke="currentColor"
134
- >
135
- <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" />
136
- </svg>
137
- </div>
138
- </button>
139
-
140
- {/* Expanded order items */}
141
- {expanded && (
142
- <div className="border-border bg-muted/30 space-y-3 border-t px-4 py-3">
143
- {order.items.map((item, index) => (
144
- <div key={`${item.productId}-${index}`} className="space-y-1">
145
- <div className="flex items-center gap-3">
146
- <div className="bg-muted relative h-10 w-10 flex-shrink-0 overflow-hidden rounded">
147
- {item.image ? (
148
- <Image
149
- src={item.image}
150
- alt={item.name || t('productFallback')}
151
- fill
152
- sizes="40px"
153
- className="object-cover"
154
- />
155
- ) : (
156
- <div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
157
- <svg
158
- className="h-4 w-4"
159
- fill="none"
160
- viewBox="0 0 24 24"
161
- stroke="currentColor"
162
- >
163
- <path
164
- strokeLinecap="round"
165
- strokeLinejoin="round"
166
- strokeWidth={1.5}
167
- d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"
168
- />
169
- </svg>
170
- </div>
171
- )}
172
- </div>
173
-
174
- <div className="min-w-0 flex-1">
175
- <p className="text-foreground truncate text-sm">
176
- {item.name || t('productFallback')}
177
- </p>
178
- <p className="text-muted-foreground text-xs">
179
- {tc('qty')} {item.quantity}
180
- </p>
181
- </div>
182
-
183
- <span className="text-foreground flex-shrink-0 text-sm">
184
- {formatPrice(parseFloat(item.price), { currency }) as string}
185
- </span>
186
- </div>
187
-
188
- {item.customizations && <OrderCustomizations customizations={item.customizations} />}
189
- </div>
190
- ))}
191
-
192
- <OrderStatusTimeline history={order.statusHistory} />
193
- <OrderShippingBlock order={order} />
194
- <OrderPaymentBlock order={order} />
195
-
196
- {/* The shopper's own checkout note, read-only */}
197
- {order.notes && (
198
- <div className="border-border border-t pt-2">
199
- <p className="text-foreground text-sm font-medium">{t('orderNotes')}</p>
200
- <p className="text-muted-foreground text-sm whitespace-pre-wrap">{order.notes}</p>
201
- </div>
202
- )}
203
-
204
- {/* Downloads section */}
205
- {order.hasDownloads && <OrderDownloads orderId={order.id} />}
206
-
207
- <OrderFinancialSummary order={order} currency={currency} />
208
- </div>
209
- )}
210
- </div>
211
- );
212
- }
213
-
214
- function OrderDownloads({ orderId }: { orderId: string }) {
215
- const t = useTranslations('account');
216
- const [downloads, setDownloads] = useState<OrderDownloadLink[] | null>(null);
217
- const [loading, setLoading] = useState(true);
218
-
219
- useEffect(() => {
220
- let cancelled = false;
221
- async function fetch() {
222
- try {
223
- const client = getClient();
224
- const links = await client.getOrderDownloads(orderId);
225
- if (!cancelled) setDownloads(links);
226
- } catch {
227
- if (!cancelled) setDownloads([]);
228
- } finally {
229
- if (!cancelled) setLoading(false);
230
- }
231
- }
232
- fetch();
233
- return () => {
234
- cancelled = true;
235
- };
236
- }, [orderId]);
237
-
238
- if (loading) {
239
- return (
240
- <div className="border-border border-t pt-2">
241
- <p className="text-muted-foreground animate-pulse text-xs">{t('downloads')}...</p>
242
- </div>
243
- );
244
- }
245
-
246
- if (!downloads || downloads.length === 0) return null;
247
-
248
- return (
249
- <div className="border-border space-y-2 border-t pt-2">
250
- <p className="text-foreground text-sm font-medium">{t('downloads')}</p>
251
- {downloads.map((link, idx) => (
252
- <div key={idx} className="flex items-center gap-3">
253
- <div className="min-w-0 flex-1">
254
- <p className="text-foreground truncate text-sm">{link.fileName}</p>
255
- <p className="text-muted-foreground text-xs">
256
- {link.productName}
257
- {' · '}
258
- {link.downloadLimit != null
259
- ? `${link.downloadsUsed}/${link.downloadLimit} ${t('downloadsRemaining')}`
260
- : t('unlimitedDownloads')}
261
- {' · '}
262
- {link.expiresAt
263
- ? `${t('expiresAt')} ${new Date(link.expiresAt).toLocaleDateString()}`
264
- : t('noExpiry')}
265
- </p>
266
- </div>
267
- <a
268
- href={link.downloadUrl}
269
- target="_blank"
270
- rel="noopener noreferrer"
271
- className="bg-primary text-primary-foreground flex-shrink-0 rounded px-3 py-1 text-xs font-medium hover:opacity-90"
272
- >
273
- {t('downloadFile')}
274
- </a>
275
- </div>
276
- ))}
277
- </div>
278
- );
279
- }
280
-
281
- function OrderFinancialSummary({ order, currency }: { order: Order; currency: string }) {
282
- const tc = useTranslations('common');
283
- const totalAmount = order.totalAmount || order.total || '0';
284
- const subtotal = order.subtotal ? parseFloat(order.subtotal) : null;
285
- const ruleAmt = order.ruleDiscountAmount ? parseFloat(order.ruleDiscountAmount) : 0;
286
- const couponAmt = order.couponDiscount ? parseFloat(order.couponDiscount) : 0;
287
- const shipping = order.shippingAmount ? parseFloat(order.shippingAmount) : 0;
288
- // Inclusive (VAT) orders persist taxAmount=0; the real VAT lives on the
289
- // breakdown. Surface whichever is present (mirror of TaxDisplay).
290
- const explicitTax = order.taxAmount ? parseFloat(order.taxAmount) : 0;
291
- const taxIncluded = !!order.taxBreakdown?.pricesIncludeTax;
292
- const tax =
293
- explicitTax > 0
294
- ? explicitTax
295
- : typeof order.taxBreakdown?.totalTax === 'number'
296
- ? order.taxBreakdown.totalTax
297
- : 0;
298
- const rules = order.appliedDiscounts;
299
-
300
- const hasBreakdown = subtotal !== null && subtotal > 0;
301
-
302
- if (!hasBreakdown) {
303
- return (
304
- <div className="border-border flex items-center justify-between border-t pt-2">
305
- <span className="text-muted-foreground text-sm font-medium">{tc('total')}</span>
306
- <span className="text-foreground text-sm font-semibold">
307
- {formatPrice(parseFloat(totalAmount), { currency }) as string}
308
- </span>
309
- </div>
310
- );
311
- }
312
-
313
- return (
314
- <div className="border-border space-y-1 border-t pt-2 text-sm">
315
- <div className="flex items-center justify-between">
316
- <span className="text-muted-foreground">{tc('subtotal')}</span>
317
- <span className="text-foreground">{formatPrice(subtotal, { currency }) as string}</span>
318
- </div>
319
-
320
- {rules && rules.length > 0
321
- ? rules.map((rule) => (
322
- <div key={rule.ruleId} className="flex items-center justify-between">
323
- <span className="text-muted-foreground">{rule.ruleName}</span>
324
- <span className="text-destructive">
325
- -{formatPrice(parseFloat(rule.discountAmount || '0'), { currency }) as string}
326
- </span>
327
- </div>
328
- ))
329
- : ruleAmt > 0 && (
330
- <div className="flex items-center justify-between">
331
- <span className="text-muted-foreground">{tc('generalDiscount')}</span>
332
- <span className="text-destructive">
333
- -{formatPrice(ruleAmt, { currency }) as string}
334
- </span>
335
- </div>
336
- )}
337
-
338
- {order.couponCode && couponAmt > 0 && (
339
- <div className="flex items-center justify-between">
340
- <span className="text-muted-foreground">
341
- {tc('couponDiscount')} ({order.couponCode})
342
- </span>
343
- <span className="text-destructive">
344
- -{formatPrice(couponAmt, { currency }) as string}
345
- </span>
346
- </div>
347
- )}
348
-
349
- {shipping > 0 && (
350
- <div className="flex items-center justify-between">
351
- <span className="text-muted-foreground">{tc('shipping')}</span>
352
- <span className="text-foreground">{formatPrice(shipping, { currency }) as string}</span>
353
- </div>
354
- )}
355
-
356
- {tax > 0 && (
357
- <div className="flex items-center justify-between">
358
- <span className="text-muted-foreground">{taxIncluded ? tc('taxIncl') : tc('tax')}</span>
359
- <span className="text-foreground">{formatPrice(tax, { currency }) as string}</span>
360
- </div>
361
- )}
362
-
363
- <div className="border-border flex items-center justify-between border-t pt-1">
364
- <span className="text-foreground font-medium">{tc('total')}</span>
365
- <span className="text-foreground font-semibold">
366
- {formatPrice(parseFloat(totalAmount), { currency }) as string}
367
- </span>
368
- </div>
369
- </div>
370
- );
371
- }
1
+ 'use client';
2
+
3
+ import { useState, useEffect } from 'react';
4
+ import { CdnImage as Image } from '@/ui/shared/cdn-image';
5
+ import type { Order, OrderStatus, OrderDownloadLink } from 'brainerce';
6
+ import { formatPrice } from 'brainerce';
7
+ import { getClient } from '@/core/lib/brainerce';
8
+ import { useCurrency } from '@/core/lib/use-currency';
9
+ import { useTranslations } from '@/core/lib/translations';
10
+ import { cn } from '@/core/lib/utils';
11
+ import { OrderCustomizations } from './order-customizations';
12
+ import { OrderStatusTimeline, ORDER_STATUS_LABEL_KEYS } from './order-status-timeline';
13
+ import { OrderShippingBlock } from './order-shipping-block';
14
+ import { OrderPaymentBlock } from './order-payment-block';
15
+
16
+ /**
17
+ * Badge colour per order status. The API sends the status UPPERCASE and
18
+ * verbatim, so these keys are uppercase too. Do NOT lowercase `order.status`
19
+ * before the lookup: every row would miss and render as "Pending".
20
+ * Labels come from ORDER_STATUS_LABEL_KEYS so the two stay in step.
21
+ */
22
+ const STATUS_STYLES: Record<OrderStatus, string> = {
23
+ DRAFT: 'bg-muted text-muted-foreground',
24
+ PENDING: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-950/30 dark:text-yellow-400',
25
+ PROCESSING: 'bg-blue-100 text-blue-800 dark:bg-blue-950/30 dark:text-blue-400',
26
+ ON_HOLD: 'bg-amber-100 text-amber-800 dark:bg-amber-950/30 dark:text-amber-400',
27
+ PAID: 'bg-emerald-100 text-emerald-800 dark:bg-emerald-950/30 dark:text-emerald-400',
28
+ SHIPPED: 'bg-purple-100 text-purple-800 dark:bg-purple-950/30 dark:text-purple-400',
29
+ DELIVERED: 'bg-green-100 text-green-800 dark:bg-green-950/30 dark:text-green-400',
30
+ COMPLETED: 'bg-green-100 text-green-800 dark:bg-green-950/30 dark:text-green-400',
31
+ FULFILLED: 'bg-teal-100 text-teal-800 dark:bg-teal-950/30 dark:text-teal-400',
32
+ CANCELLED: 'bg-red-100 text-red-800 dark:bg-red-950/30 dark:text-red-400',
33
+ REFUNDED: 'bg-orange-100 text-orange-800 dark:bg-orange-950/30 dark:text-orange-400',
34
+ PARTIALLY_REFUNDED: 'bg-orange-100 text-orange-800 dark:bg-orange-950/30 dark:text-orange-400',
35
+ };
36
+
37
+ interface OrderHistoryProps {
38
+ orders: Order[];
39
+ /** True while another page exists (`meta.page < meta.totalPages`). */
40
+ hasMore?: boolean;
41
+ /** True while the next page is in flight. */
42
+ loadingMore?: boolean;
43
+ /** Fetch and append the next page. Omit to render without pagination. */
44
+ onLoadMore?: () => void;
45
+ className?: string;
46
+ }
47
+
48
+ export function OrderHistory({
49
+ orders,
50
+ hasMore = false,
51
+ loadingMore = false,
52
+ onLoadMore,
53
+ className,
54
+ }: OrderHistoryProps) {
55
+ const t = useTranslations('account');
56
+ const tp = useTranslations('products');
57
+ const tc = useTranslations('common');
58
+ // Only the genuinely-empty history hits this branch. Appending a page can
59
+ // never shrink the list, so the empty state cannot flash mid-pagination.
60
+ if (orders.length === 0) {
61
+ return (
62
+ <div className={cn('py-12 text-center', className)}>
63
+ <svg
64
+ className="text-muted-foreground mx-auto mb-3 h-12 w-12"
65
+ fill="none"
66
+ viewBox="0 0 24 24"
67
+ stroke="currentColor"
68
+ >
69
+ <path
70
+ strokeLinecap="round"
71
+ strokeLinejoin="round"
72
+ strokeWidth={1.5}
73
+ d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"
74
+ />
75
+ </svg>
76
+ <h3 className="text-foreground text-lg font-semibold">{t('noOrders')}</h3>
77
+ <p className="text-muted-foreground mt-1 text-sm">{t('noOrdersDesc')}</p>
78
+ </div>
79
+ );
80
+ }
81
+
82
+ return (
83
+ <div className={cn('space-y-4', className)}>
84
+ {orders.map((order) => (
85
+ <OrderCard key={order.id} order={order} />
86
+ ))}
87
+
88
+ {/* Auto-hides on the last page: `hasMore` is false when
89
+ meta.page === meta.totalPages, which is also the single-page case. */}
90
+ {hasMore && onLoadMore && (
91
+ <button
92
+ type="button"
93
+ onClick={onLoadMore}
94
+ disabled={loadingMore}
95
+ className="border-border text-foreground hover:bg-muted w-full rounded-lg border px-6 py-3 text-sm font-medium transition-colors disabled:cursor-not-allowed disabled:opacity-50"
96
+ >
97
+ {loadingMore ? tc('loading') : tp('loadMore')}
98
+ </button>
99
+ )}
100
+ </div>
101
+ );
102
+ }
103
+
104
+ function OrderCard({ order }: { order: Order }) {
105
+ const t = useTranslations('account');
106
+ const tc = useTranslations('common');
107
+ const [expanded, setExpanded] = useState(false);
108
+ const statusLabelKey = ORDER_STATUS_LABEL_KEYS[order.status] || 'statusPending';
109
+ const statusClassName = STATUS_STYLES[order.status] || STATUS_STYLES.PENDING;
110
+ const currency = useCurrency(order.currency);
111
+ const totalAmount = order.totalAmount || order.total || '0';
112
+
113
+ return (
114
+ <div className="border-border overflow-hidden rounded-lg border">
115
+ {/* Order header */}
116
+ <button
117
+ type="button"
118
+ onClick={() => setExpanded(!expanded)}
119
+ className="hover:bg-muted/50 flex w-full items-center justify-between p-4 text-start transition-colors"
120
+ >
121
+ <div className="min-w-0 flex-1">
122
+ <div className="flex flex-wrap items-center gap-3">
123
+ <span className="text-foreground text-sm font-semibold">
124
+ {order.orderNumber || `${t('orderPrefix')} ${order.id.slice(0, 8)}`}
125
+ </span>
126
+ <span
127
+ className={cn(
128
+ 'inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium',
129
+ statusClassName
130
+ )}
131
+ >
132
+ {t(statusLabelKey)}
133
+ </span>
134
+ </div>
135
+ <div className="text-muted-foreground mt-1 flex items-center gap-4 text-xs">
136
+ <span>
137
+ {order.createdAt && !isNaN(new Date(order.createdAt).getTime())
138
+ ? new Date(order.createdAt).toLocaleDateString(undefined, {
139
+ year: 'numeric',
140
+ month: 'short',
141
+ day: 'numeric',
142
+ })
143
+ : ''}
144
+ </span>
145
+ <span>
146
+ {order.items.length} {order.items.length === 1 ? tc('item') : tc('items')}
147
+ </span>
148
+ </div>
149
+ </div>
150
+
151
+ <div className="flex flex-shrink-0 items-center gap-3">
152
+ <span className="text-foreground text-sm font-semibold">
153
+ {formatPrice(parseFloat(totalAmount), { currency }) as string}
154
+ </span>
155
+ <svg
156
+ className={cn(
157
+ 'text-muted-foreground h-4 w-4 transition-transform',
158
+ expanded && 'rotate-180'
159
+ )}
160
+ fill="none"
161
+ viewBox="0 0 24 24"
162
+ stroke="currentColor"
163
+ >
164
+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" />
165
+ </svg>
166
+ </div>
167
+ </button>
168
+
169
+ {/* Expanded order items */}
170
+ {expanded && (
171
+ <div className="border-border bg-muted/30 space-y-3 border-t px-4 py-3">
172
+ {order.items.map((item, index) => (
173
+ <div key={`${item.productId}-${index}`} className="space-y-1">
174
+ <div className="flex items-center gap-3">
175
+ <div className="bg-muted relative h-10 w-10 flex-shrink-0 overflow-hidden rounded">
176
+ {item.image ? (
177
+ <Image
178
+ src={item.image}
179
+ alt={item.name || t('productFallback')}
180
+ fill
181
+ sizes="40px"
182
+ className="object-cover"
183
+ />
184
+ ) : (
185
+ <div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
186
+ <svg
187
+ className="h-4 w-4"
188
+ fill="none"
189
+ viewBox="0 0 24 24"
190
+ stroke="currentColor"
191
+ >
192
+ <path
193
+ strokeLinecap="round"
194
+ strokeLinejoin="round"
195
+ strokeWidth={1.5}
196
+ d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"
197
+ />
198
+ </svg>
199
+ </div>
200
+ )}
201
+ </div>
202
+
203
+ <div className="min-w-0 flex-1">
204
+ <p className="text-foreground truncate text-sm">
205
+ {item.name || t('productFallback')}
206
+ </p>
207
+ <p className="text-muted-foreground text-xs">
208
+ {tc('qty')} {item.quantity}
209
+ </p>
210
+ </div>
211
+
212
+ <span className="text-foreground flex-shrink-0 text-sm">
213
+ {formatPrice(parseFloat(item.price), { currency }) as string}
214
+ </span>
215
+ </div>
216
+
217
+ {item.customizations && <OrderCustomizations customizations={item.customizations} />}
218
+ </div>
219
+ ))}
220
+
221
+ <OrderStatusTimeline history={order.statusHistory} />
222
+ <OrderShippingBlock order={order} />
223
+ <OrderPaymentBlock order={order} />
224
+
225
+ {/* The shopper's own checkout note, read-only */}
226
+ {order.notes && (
227
+ <div className="border-border border-t pt-2">
228
+ <p className="text-foreground text-sm font-medium">{t('orderNotes')}</p>
229
+ <p className="text-muted-foreground whitespace-pre-wrap text-sm">{order.notes}</p>
230
+ </div>
231
+ )}
232
+
233
+ {/* Downloads section */}
234
+ {order.hasDownloads && <OrderDownloads orderId={order.id} />}
235
+
236
+ <OrderFinancialSummary order={order} currency={currency} />
237
+ </div>
238
+ )}
239
+ </div>
240
+ );
241
+ }
242
+
243
+ function OrderDownloads({ orderId }: { orderId: string }) {
244
+ const t = useTranslations('account');
245
+ const [downloads, setDownloads] = useState<OrderDownloadLink[] | null>(null);
246
+ const [loading, setLoading] = useState(true);
247
+
248
+ useEffect(() => {
249
+ let cancelled = false;
250
+ async function fetch() {
251
+ try {
252
+ const client = getClient();
253
+ const links = await client.getOrderDownloads(orderId);
254
+ if (!cancelled) setDownloads(links);
255
+ } catch {
256
+ if (!cancelled) setDownloads([]);
257
+ } finally {
258
+ if (!cancelled) setLoading(false);
259
+ }
260
+ }
261
+ fetch();
262
+ return () => {
263
+ cancelled = true;
264
+ };
265
+ }, [orderId]);
266
+
267
+ if (loading) {
268
+ return (
269
+ <div className="border-border border-t pt-2">
270
+ <p className="text-muted-foreground animate-pulse text-xs">{t('downloads')}...</p>
271
+ </div>
272
+ );
273
+ }
274
+
275
+ if (!downloads || downloads.length === 0) return null;
276
+
277
+ return (
278
+ <div className="border-border space-y-2 border-t pt-2">
279
+ <p className="text-foreground text-sm font-medium">{t('downloads')}</p>
280
+ {downloads.map((link, idx) => (
281
+ <div key={idx} className="flex items-center gap-3">
282
+ <div className="min-w-0 flex-1">
283
+ <p className="text-foreground truncate text-sm">{link.fileName}</p>
284
+ <p className="text-muted-foreground text-xs">
285
+ {link.productName}
286
+ {' · '}
287
+ {link.downloadLimit != null
288
+ ? `${link.downloadsUsed}/${link.downloadLimit} ${t('downloadsRemaining')}`
289
+ : t('unlimitedDownloads')}
290
+ {' · '}
291
+ {link.expiresAt
292
+ ? `${t('expiresAt')} ${new Date(link.expiresAt).toLocaleDateString()}`
293
+ : t('noExpiry')}
294
+ </p>
295
+ </div>
296
+ <a
297
+ href={link.downloadUrl}
298
+ target="_blank"
299
+ rel="noopener noreferrer"
300
+ className="bg-primary text-primary-foreground flex-shrink-0 rounded px-3 py-1 text-xs font-medium hover:opacity-90"
301
+ >
302
+ {t('downloadFile')}
303
+ </a>
304
+ </div>
305
+ ))}
306
+ </div>
307
+ );
308
+ }
309
+
310
+ function OrderFinancialSummary({ order, currency }: { order: Order; currency: string }) {
311
+ const tc = useTranslations('common');
312
+ const totalAmount = order.totalAmount || order.total || '0';
313
+ const subtotal = order.subtotal ? parseFloat(order.subtotal) : null;
314
+ const ruleAmt = order.ruleDiscountAmount ? parseFloat(order.ruleDiscountAmount) : 0;
315
+ const couponAmt = order.couponDiscount ? parseFloat(order.couponDiscount) : 0;
316
+ const shipping = order.shippingAmount ? parseFloat(order.shippingAmount) : 0;
317
+ // Inclusive (VAT) orders persist taxAmount=0; the real VAT lives on the
318
+ // breakdown. Surface whichever is present (mirror of TaxDisplay).
319
+ const explicitTax = order.taxAmount ? parseFloat(order.taxAmount) : 0;
320
+ const taxIncluded = !!order.taxBreakdown?.pricesIncludeTax;
321
+ const tax =
322
+ explicitTax > 0
323
+ ? explicitTax
324
+ : typeof order.taxBreakdown?.totalTax === 'number'
325
+ ? order.taxBreakdown.totalTax
326
+ : 0;
327
+ // One row per applied rate. Guard every field: this is a frozen snapshot and
328
+ // an older order can carry rows without a name or an amount.
329
+ const taxRows = (order.taxBreakdown?.breakdown ?? []).filter(
330
+ (row): row is { name: string; rate: number; amount: number } =>
331
+ !!row && typeof row.name === 'string' && typeof row.amount === 'number'
332
+ );
333
+ const rules = order.appliedDiscounts;
334
+
335
+ const hasBreakdown = subtotal !== null && subtotal > 0;
336
+
337
+ if (!hasBreakdown) {
338
+ return (
339
+ <div className="border-border flex items-center justify-between border-t pt-2">
340
+ <span className="text-muted-foreground text-sm font-medium">{tc('total')}</span>
341
+ <span className="text-foreground text-sm font-semibold">
342
+ {formatPrice(parseFloat(totalAmount), { currency }) as string}
343
+ </span>
344
+ </div>
345
+ );
346
+ }
347
+
348
+ return (
349
+ <div className="border-border space-y-1 border-t pt-2 text-sm">
350
+ <div className="flex items-center justify-between">
351
+ <span className="text-muted-foreground">{tc('subtotal')}</span>
352
+ <span className="text-foreground">{formatPrice(subtotal, { currency }) as string}</span>
353
+ </div>
354
+
355
+ {rules && rules.length > 0
356
+ ? rules.map((rule) => (
357
+ <div key={rule.ruleId} className="flex items-center justify-between">
358
+ <span className="text-muted-foreground">{rule.ruleName}</span>
359
+ <span className="text-destructive">
360
+ -{formatPrice(parseFloat(rule.discountAmount || '0'), { currency }) as string}
361
+ </span>
362
+ </div>
363
+ ))
364
+ : ruleAmt > 0 && (
365
+ <div className="flex items-center justify-between">
366
+ <span className="text-muted-foreground">{tc('generalDiscount')}</span>
367
+ <span className="text-destructive">
368
+ -{formatPrice(ruleAmt, { currency }) as string}
369
+ </span>
370
+ </div>
371
+ )}
372
+
373
+ {order.couponCode && couponAmt > 0 && (
374
+ <div className="flex items-center justify-between">
375
+ <span className="text-muted-foreground">
376
+ {tc('couponDiscount')} ({order.couponCode})
377
+ </span>
378
+ <span className="text-destructive">
379
+ -{formatPrice(couponAmt, { currency }) as string}
380
+ </span>
381
+ </div>
382
+ )}
383
+
384
+ {shipping > 0 && (
385
+ <div className="flex items-center justify-between">
386
+ <span className="text-muted-foreground">{tc('shipping')}</span>
387
+ <span className="text-foreground">{formatPrice(shipping, { currency }) as string}</span>
388
+ </div>
389
+ )}
390
+
391
+ {/* `breakdown` holds ONE ROW PER TAX. A Quebec order carries GST and QST
392
+ and both have to appear; an order frozen before breakdowns existed
393
+ carries none, so fall back to the single combined line. */}
394
+ {tax > 0 &&
395
+ (taxRows.length > 1 ? (
396
+ taxRows.map((row, i) => (
397
+ <div key={`${row.name}-${i}`} className="flex items-center justify-between">
398
+ <span className="text-muted-foreground">
399
+ {row.name}
400
+ {taxIncluded ? ` (${tc('inclSuffix')})` : ''}
401
+ </span>
402
+ <span className="text-foreground">
403
+ {formatPrice(row.amount, { currency }) as string}
404
+ </span>
405
+ </div>
406
+ ))
407
+ ) : (
408
+ <div className="flex items-center justify-between">
409
+ <span className="text-muted-foreground">{taxIncluded ? tc('taxIncl') : tc('tax')}</span>
410
+ <span className="text-foreground">{formatPrice(tax, { currency }) as string}</span>
411
+ </div>
412
+ ))}
413
+
414
+ <div className="border-border flex items-center justify-between border-t pt-1">
415
+ <span className="text-foreground font-medium">{tc('total')}</span>
416
+ <span className="text-foreground font-semibold">
417
+ {formatPrice(parseFloat(totalAmount), { currency }) as string}
418
+ </span>
419
+ </div>
420
+ </div>
421
+ );
422
+ }