create-brainerce-store 1.72.0 → 1.73.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.
@@ -1,371 +1,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
- 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
+ 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 whitespace-pre-wrap text-sm">{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
+ }