create-brainerce-store 1.68.0 → 1.72.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 (50) hide show
  1. package/README.md +31 -10
  2. package/dist/index.js +197 -105
  3. package/messages/en.json +63 -3
  4. package/messages/he.json +63 -3
  5. package/package.json +1 -1
  6. package/templates/nextjs/base/TRANSLATIONS.md +14 -7
  7. package/templates/nextjs/base/src/app/checkout/page.tsx +1074 -1017
  8. package/templates/nextjs/base/src/app/order-confirmation/page.tsx +21 -2
  9. package/templates/nextjs/base/src/app/products/[slug]/page.tsx +1 -1
  10. package/templates/nextjs/base/src/app/register/page.tsx +67 -64
  11. package/templates/nextjs/base/src/components/account/order-history.tsx +25 -39
  12. package/templates/nextjs/base/src/components/account/order-status-timeline.tsx +30 -11
  13. package/templates/nextjs/base/src/components/account/profile-section.tsx +303 -226
  14. package/templates/nextjs/base/src/components/auth/register-form.tsx +326 -245
  15. package/templates/nextjs/base/src/components/checkout/custom-fields-step.tsx +306 -294
  16. package/templates/nextjs/base/src/components/checkout/date-picker.tsx +13 -1
  17. package/templates/nextjs/base/src/components/checkout/datetime-picker.tsx +61 -21
  18. package/templates/nextjs/base/src/components/shared/birthday-picker.tsx +258 -0
  19. package/templates/nextjs/base/src/core/hooks/use-cart-page.ts +71 -2
  20. package/templates/nextjs/base/src/core/lib/auth.ts +155 -154
  21. package/templates/nextjs/base/src/core/lib/birthday.ts +74 -0
  22. package/templates/nextjs/base/src/core/lib/brainerce.ts.ejs +5 -16
  23. package/templates/nextjs/base/src/core/lib/store-info.ts +10 -0
  24. package/templates/nextjs/base/src/core/providers/store-provider.tsx.ejs +3 -6
  25. package/templates/nextjs/base/src/ui/cart/cart-item.tsx +19 -1
  26. package/templates/nextjs/base/src/ui/cart/cart-view.tsx +42 -6
  27. package/templates/nextjs/base/src/ui/cart/reservation-countdown.tsx +52 -10
  28. package/templates/nextjs/base/src/ui/layout/newsletter-signup.tsx +143 -0
  29. package/templates/nextjs/base/src/ui/layout/site-footer.tsx.ejs +18 -2
  30. package/templates/nextjs/base/src/ui/product/back-in-stock-form.tsx +173 -0
  31. package/templates/nextjs/base/src/ui/product/product-client-section.tsx +484 -455
  32. package/templates/nextjs/base/src/ui/product/review-form.tsx +136 -12
  33. package/templates/nextjs/base/src/ui/product/reviews-section.tsx.ejs +139 -108
  34. package/templates/nextjs/designs/atelier/ui/cart/cart-drawer.tsx +21 -3
  35. package/templates/nextjs/designs/atelier/ui/cart/cart-item.tsx +19 -1
  36. package/templates/nextjs/designs/atelier/ui/cart/cart-view.tsx +44 -7
  37. package/templates/nextjs/designs/atelier/ui/cart/reservation-countdown.tsx +52 -10
  38. package/templates/nextjs/designs/atelier/ui/layout/site-footer.tsx.ejs +155 -142
  39. package/templates/nextjs/designs/atelier/ui/product/product-client-section.tsx +500 -477
  40. package/templates/nextjs/designs/atelier/ui/product/review-form.tsx +135 -11
  41. package/templates/nextjs/designs/atelier/ui/product/reviews-section.tsx.ejs +179 -148
  42. package/templates/nextjs/ui-canvas/cart/cart-item.tsx +15 -1
  43. package/templates/nextjs/ui-canvas/cart/cart-view.tsx +38 -4
  44. package/templates/nextjs/ui-canvas/cart/reservation-countdown.tsx +54 -11
  45. package/templates/nextjs/ui-canvas/layout/newsletter-signup.tsx +122 -0
  46. package/templates/nextjs/ui-canvas/layout/site-footer.tsx.ejs +87 -83
  47. package/templates/nextjs/ui-canvas/product/back-in-stock-form.tsx +151 -0
  48. package/templates/nextjs/ui-canvas/product/product-client-section.tsx +373 -352
  49. package/templates/nextjs/ui-canvas/product/review-form.tsx +129 -11
  50. package/templates/nextjs/ui-canvas/product/reviews-section.tsx.ejs +127 -96
@@ -1,455 +1,484 @@
1
- 'use client';
2
-
3
- import { Download, FileText, Image as ImageIcon } from 'lucide-react';
4
- import { CdnImage as Image } from '@/ui/shared/cdn-image';
5
- import type { Product, ProductMetafield, DownloadFile } from 'brainerce';
6
- import { useProductPage } from '@/core/hooks/use-product-page';
7
- import { PriceDisplay } from '@/ui/product/price-display';
8
- import { LoadingSpinner } from '@/ui/shared/loading-spinner';
9
- import { VariantSelector } from '@/ui/product/variant-selector';
10
- import { StockBadge } from '@/ui/product/stock-badge';
11
- import { RecommendationSection } from '@/ui/product/recommendation-section';
12
- import { FrequentlyBoughtTogether } from '@/ui/product/frequently-bought-together';
13
- import { CustomizationFields } from '@/ui/product/customization-fields';
14
- import { ModifierGroupSelector } from '@/ui/product/modifier-group-selector';
15
- import { Badge } from '@/components/ui/badge';
16
- import { Button } from '@/components/ui/button';
17
- import { Separator } from '@/components/ui/separator';
18
- import { useTranslations } from '@/core/lib/translations';
19
- import { cn } from '@/core/lib/utils';
20
- import { sanitizeProductHtml } from '@/core/lib/sanitize-html';
21
-
22
- /** Render a metafield value based on its type */
23
- function MetafieldValue({ field }: { field: ProductMetafield }) {
24
- const tc = useTranslations('common');
25
- switch (field.type) {
26
- case 'IMAGE': {
27
- if (!field.value) return <span className="text-muted-foreground">-</span>;
28
- return (
29
- <img
30
- src={field.value}
31
- alt={field.definitionName}
32
- className="h-16 w-16 rounded object-cover"
33
- />
34
- );
35
- }
36
- case 'GALLERY': {
37
- let urls: string[] = [];
38
- try {
39
- const parsed = JSON.parse(field.value);
40
- urls = Array.isArray(parsed)
41
- ? parsed.filter((u: unknown) => typeof u === 'string' && u)
42
- : [];
43
- } catch {
44
- urls = field.value ? [field.value] : [];
45
- }
46
- if (urls.length === 0) return <span className="text-muted-foreground">-</span>;
47
- return (
48
- <div className="flex flex-wrap gap-2">
49
- {urls.map((url, i) => (
50
- <img
51
- key={i}
52
- src={url}
53
- alt={`${field.definitionName} ${i + 1}`}
54
- className="h-16 w-16 rounded object-cover"
55
- />
56
- ))}
57
- </div>
58
- );
59
- }
60
- case 'URL':
61
- return field.value ? (
62
- <a
63
- href={field.value}
64
- target="_blank"
65
- rel="noopener noreferrer"
66
- className="text-primary break-all hover:underline"
67
- >
68
- {field.value}
69
- </a>
70
- ) : (
71
- <span className="text-muted-foreground">-</span>
72
- );
73
- case 'COLOR':
74
- return field.value ? (
75
- <span className="inline-flex items-center gap-2">
76
- <span
77
- className="border-border inline-block h-4 w-4 rounded-full border"
78
- style={{ backgroundColor: field.value }}
79
- />
80
- {field.value}
81
- </span>
82
- ) : (
83
- <span className="text-muted-foreground">-</span>
84
- );
85
- case 'BOOLEAN':
86
- return <span>{field.value === 'true' ? tc('yes') : tc('no')}</span>;
87
- case 'DATE':
88
- case 'DATETIME': {
89
- if (!field.value) return <span className="text-muted-foreground">-</span>;
90
- try {
91
- const date = new Date(field.value);
92
- return (
93
- <span>
94
- {field.type === 'DATETIME' ? date.toLocaleString() : date.toLocaleDateString()}
95
- </span>
96
- );
97
- } catch {
98
- return <span>{field.value}</span>;
99
- }
100
- }
101
- default:
102
- return <span>{field.value || '-'}</span>;
103
- }
104
- }
105
-
106
- interface ProductClientSectionProps {
107
- product: Product;
108
- }
109
-
110
- export function ProductClientSection({ product: initialProduct }: ProductClientSectionProps) {
111
- const t = useTranslations('productDetail');
112
-
113
- const {
114
- product,
115
- recommendations,
116
- images,
117
- selectedImageIndex,
118
- setSelectedImageIndex,
119
- mainImageUrl,
120
- selectedVariant,
121
- setSelectedVariant,
122
- priceInfo,
123
- inventory,
124
- canPurchase,
125
- description,
126
- quantity,
127
- setQuantity,
128
- addingToCart,
129
- addedMessage,
130
- handleAddToCart,
131
- customizationFields,
132
- customizationValues,
133
- setCustomizationValues,
134
- customizationErrors,
135
- modifierGroups,
136
- modifierSelections,
137
- setModifierSelection,
138
- modifierError,
139
- } = useProductPage(initialProduct);
140
-
141
- return (
142
- <div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
143
- <div className="grid grid-cols-1 gap-8 lg:grid-cols-2 lg:gap-12">
144
- {/* Image Gallery */}
145
- <div className="space-y-4">
146
- {/* Main Image */}
147
- <div className="bg-muted relative aspect-square overflow-hidden rounded-lg">
148
- {mainImageUrl ? (
149
- <Image
150
- src={mainImageUrl}
151
- alt={product.name}
152
- fill
153
- sizes="(max-width: 1024px) 100vw, 50vw"
154
- className="object-contain"
155
- priority
156
- />
157
- ) : (
158
- <div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
159
- <ImageIcon className="h-24 w-24" strokeWidth={1} aria-hidden="true" />
160
- </div>
161
- )}
162
- </div>
163
-
164
- {/* Thumbnails */}
165
- {images.length > 1 && (
166
- <div className="flex gap-2 overflow-x-auto pb-2">
167
- {images.map((img, idx) => (
168
- <button
169
- key={idx}
170
- type="button"
171
- onClick={() => setSelectedImageIndex(idx)}
172
- className={cn(
173
- 'relative h-16 w-16 flex-shrink-0 overflow-hidden rounded border-2 transition-colors',
174
- selectedImageIndex === idx
175
- ? 'border-primary'
176
- : 'border-border hover:border-muted-foreground'
177
- )}
178
- >
179
- <Image
180
- src={img.url}
181
- alt={img.alt || `${product.name} ${idx + 1}`}
182
- fill
183
- sizes="64px"
184
- className="object-cover"
185
- />
186
- </button>
187
- ))}
188
- </div>
189
- )}
190
- </div>
191
-
192
- {/* Product Info */}
193
- <div className="space-y-6">
194
- {/* Categories */}
195
- {product.categories && product.categories.length > 0 && (
196
- <div className="flex flex-wrap gap-2">
197
- {product.categories.map((cat) => (
198
- <Badge
199
- key={cat.id}
200
- variant="secondary"
201
- className="text-muted-foreground bg-muted rounded px-2 py-1 font-normal"
202
- >
203
- {cat.name}
204
- </Badge>
205
- ))}
206
- </div>
207
- )}
208
-
209
- {/* Brand */}
210
- {(product as { brands?: Array<{ id: string; name: string }> }).brands &&
211
- (product as { brands: Array<{ id: string; name: string }> }).brands.length > 0 && (
212
- <div className="text-muted-foreground text-sm">
213
- {t('by')}{' '}
214
- <span className="text-foreground font-medium">
215
- {(product as { brands: Array<{ id: string; name: string }> }).brands
216
- .map((b) => b.name)
217
- .join(', ')}
218
- </span>
219
- </div>
220
- )}
221
-
222
- {/* Title */}
223
- <h1 className="text-foreground text-2xl font-bold sm:text-3xl">{product.name}</h1>
224
-
225
- {/* Tags */}
226
- {(product as unknown as { tags?: Array<{ id: string; name: string }> }).tags &&
227
- (product as unknown as { tags: Array<{ id: string; name: string }> }).tags.length > 0 && (
228
- <div className="flex flex-wrap gap-1.5">
229
- {(product as unknown as { tags: Array<{ id: string; name: string }> }).tags.map((tag) => (
230
- <Badge
231
- key={tag.id}
232
- variant="outline"
233
- className="border-border text-muted-foreground px-2.5 py-0.5 font-normal"
234
- >
235
- #{tag.name}
236
- </Badge>
237
- ))}
238
- </div>
239
- )}
240
-
241
- {/* Price */}
242
- <PriceDisplay
243
- price={priceInfo.originalPrice}
244
- salePrice={priceInfo.isOnSale ? priceInfo.price : undefined}
245
- size="lg"
246
- />
247
-
248
- {/* Stock / Digital badge */}
249
- {product.isDownloadable ? (
250
- <Badge
251
- variant="outline"
252
- className="gap-1.5 border-transparent bg-green-100 px-3 py-1 font-medium text-green-800 dark:bg-green-950/30 dark:text-green-400"
253
- >
254
- <Download className="h-3.5 w-3.5" aria-hidden="true" />
255
- {t('instantDownload')}
256
- </Badge>
257
- ) : (
258
- <StockBadge inventory={inventory} lowStockThreshold={5} />
259
- )}
260
-
261
- {/* Downloadable files info */}
262
- {product.isDownloadable && product.downloads && product.downloads.length > 0 && (
263
- <div className="bg-muted/50 rounded-lg border p-4">
264
- <p className="text-foreground mb-2 text-sm font-medium">
265
- {t('filesIncluded')} ({product.downloads.length})
266
- </p>
267
- <ul className="space-y-1.5">
268
- {product.downloads.map((file: DownloadFile) => (
269
- <li
270
- key={file.id}
271
- className="text-muted-foreground flex items-center gap-2 text-sm"
272
- >
273
- <FileText className="h-4 w-4 flex-shrink-0" strokeWidth={1.5} aria-hidden="true" />
274
- <span className="truncate">{file.name}</span>
275
- {file.size && (
276
- <span className="flex-shrink-0 text-xs">
277
- (
278
- {file.size < 1024 * 1024
279
- ? `${(file.size / 1024).toFixed(0)} KB`
280
- : `${(file.size / (1024 * 1024)).toFixed(1)} MB`}
281
- )
282
- </span>
283
- )}
284
- </li>
285
- ))}
286
- </ul>
287
- </div>
288
- )}
289
-
290
- {/* Variant Selector */}
291
- {product.type === 'VARIABLE' && product.variants && product.variants.length > 0 && (
292
- <VariantSelector
293
- product={product}
294
- selectedVariant={selectedVariant}
295
- onVariantChange={setSelectedVariant}
296
- />
297
- )}
298
-
299
- {/* Customization Fields (buyer input) */}
300
- {customizationFields.length > 0 && (
301
- <CustomizationFields
302
- fields={customizationFields}
303
- values={customizationValues}
304
- onChange={setCustomizationValues}
305
- errors={customizationErrors}
306
- />
307
- )}
308
-
309
- {/* Modifier groups (toppings, sauce, bread type, …) */}
310
- {modifierGroups.length > 0 && (
311
- <div className="space-y-4">
312
- {modifierGroups.map((group) => (
313
- <ModifierGroupSelector
314
- key={group.attachmentId ?? group.id}
315
- group={group}
316
- value={modifierSelections[group.id] ?? []}
317
- onChange={(next) => setModifierSelection(group.id, next)}
318
- disabled={addingToCart}
319
- />
320
- ))}
321
- {modifierError && (
322
- <p className="text-destructive text-sm" role="alert">
323
- {modifierError}
324
- </p>
325
- )}
326
- </div>
327
- )}
328
-
329
- {/* Quantity + Add to Cart */}
330
- <div className="flex items-center gap-4">
331
- <div className="border-border flex items-center rounded border">
332
- <button
333
- type="button"
334
- onClick={() => setQuantity((q) => Math.max(1, q - 1))}
335
- className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
336
- aria-label={t('decreaseQuantity')}
337
- >
338
- -
339
- </button>
340
- <span className="text-foreground min-w-[3rem] px-4 py-2 text-center text-sm font-medium">
341
- {quantity}
342
- </span>
343
- <button
344
- type="button"
345
- onClick={() => setQuantity((q) => q + 1)}
346
- className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
347
- aria-label={t('increaseQuantity')}
348
- >
349
- +
350
- </button>
351
- </div>
352
-
353
- <Button
354
- type="button"
355
- size="lg"
356
- onClick={handleAddToCart}
357
- disabled={!canPurchase || addingToCart}
358
- className={cn(
359
- 'flex-1 rounded px-6 text-sm',
360
- !canPurchase &&
361
- 'bg-muted text-muted-foreground hover:bg-muted disabled:opacity-100'
362
- )}
363
- >
364
- {addingToCart ? (
365
- <span className="inline-flex items-center gap-2">
366
- <LoadingSpinner
367
- size="sm"
368
- className="border-primary-foreground/30 border-t-primary-foreground"
369
- />
370
- {t('addingToCart')}
371
- </span>
372
- ) : addedMessage ? (
373
- t('addedToCart')
374
- ) : !canPurchase ? (
375
- t('outOfStock')
376
- ) : (
377
- t('addToCart')
378
- )}
379
- </Button>
380
- </div>
381
-
382
- {/* Download after purchase note */}
383
- {product.isDownloadable && (
384
- <p className="text-muted-foreground text-sm">{t('downloadAfterPurchase')}</p>
385
- )}
386
-
387
- {/* Description */}
388
- {description && (
389
- <div>
390
- <Separator className="mb-4" />
391
- <h2 className="text-foreground mb-3 text-lg font-semibold">{t('description')}</h2>
392
- {'html' in description ? (
393
- <div
394
- className="prose prose-sm text-muted-foreground max-w-none"
395
- dangerouslySetInnerHTML={{ __html: sanitizeProductHtml(description.html) }}
396
- />
397
- ) : (
398
- <p className="text-muted-foreground whitespace-pre-wrap">{description.text}</p>
399
- )}
400
- </div>
401
- )}
402
-
403
- {/* Metafields / Specifications */}
404
- {product.metafields && product.metafields.length > 0 && (
405
- <div>
406
- <Separator className="mb-4" />
407
- <h2 className="text-foreground mb-3 text-lg font-semibold">{t('specifications')}</h2>
408
- <table className="w-full text-sm">
409
- <tbody>
410
- {product.metafields.map((field) => (
411
- <tr key={field.id} className="border-border border-b last:border-0">
412
- <td className="text-foreground whitespace-nowrap py-2 pe-4 font-medium">
413
- {field.definitionName}
414
- </td>
415
- <td className="text-muted-foreground py-2">
416
- <MetafieldValue field={field} />
417
- </td>
418
- </tr>
419
- ))}
420
- </tbody>
421
- </table>
422
- </div>
423
- )}
424
- </div>
425
- </div>
426
-
427
- {/* Frequently Bought Together (cross-sells) */}
428
- {recommendations?.crossSells && recommendations.crossSells.length > 0 && (
429
- <FrequentlyBoughtTogether
430
- items={recommendations.crossSells}
431
- currentProduct={product}
432
- className="mt-12"
433
- />
434
- )}
435
-
436
- {/* Upsells */}
437
- {recommendations?.upsells && recommendations.upsells.length > 0 && (
438
- <RecommendationSection
439
- title={t('upgradeYourChoice')}
440
- items={recommendations.upsells}
441
- className="mt-12"
442
- />
443
- )}
444
-
445
- {/* Related products */}
446
- {recommendations?.related && recommendations.related.length > 0 && (
447
- <RecommendationSection
448
- title={t('similarProducts')}
449
- items={recommendations.related}
450
- className="mt-12"
451
- />
452
- )}
453
- </div>
454
- );
455
- }
1
+ 'use client';
2
+
3
+ import { Download, FileText, Image as ImageIcon } from 'lucide-react';
4
+ import { CdnImage as Image } from '@/ui/shared/cdn-image';
5
+ import type { Product, ProductMetafield, DownloadFile } from 'brainerce';
6
+ import { useProductPage } from '@/core/hooks/use-product-page';
7
+ import { PriceDisplay } from '@/ui/product/price-display';
8
+ import { LoadingSpinner } from '@/ui/shared/loading-spinner';
9
+ import { VariantSelector } from '@/ui/product/variant-selector';
10
+ import { StockBadge } from '@/ui/product/stock-badge';
11
+ import { BackInStockForm, canOfferStockAlert } from '@/ui/product/back-in-stock-form';
12
+ import { RecommendationSection } from '@/ui/product/recommendation-section';
13
+ import { FrequentlyBoughtTogether } from '@/ui/product/frequently-bought-together';
14
+ import { CustomizationFields } from '@/ui/product/customization-fields';
15
+ import { ModifierGroupSelector } from '@/ui/product/modifier-group-selector';
16
+ import { Badge } from '@/components/ui/badge';
17
+ import { Button } from '@/components/ui/button';
18
+ import { Separator } from '@/components/ui/separator';
19
+ import { useTranslations } from '@/core/lib/translations';
20
+ import { cn } from '@/core/lib/utils';
21
+ import { sanitizeProductHtml } from '@/core/lib/sanitize-html';
22
+
23
+ /** Render a metafield value based on its type */
24
+ function MetafieldValue({ field }: { field: ProductMetafield }) {
25
+ const tc = useTranslations('common');
26
+ switch (field.type) {
27
+ case 'IMAGE': {
28
+ if (!field.value) return <span className="text-muted-foreground">-</span>;
29
+ return (
30
+ <img
31
+ src={field.value}
32
+ alt={field.definitionName}
33
+ className="h-16 w-16 rounded object-cover"
34
+ />
35
+ );
36
+ }
37
+ case 'GALLERY': {
38
+ let urls: string[] = [];
39
+ try {
40
+ const parsed = JSON.parse(field.value);
41
+ urls = Array.isArray(parsed)
42
+ ? parsed.filter((u: unknown) => typeof u === 'string' && u)
43
+ : [];
44
+ } catch {
45
+ urls = field.value ? [field.value] : [];
46
+ }
47
+ if (urls.length === 0) return <span className="text-muted-foreground">-</span>;
48
+ return (
49
+ <div className="flex flex-wrap gap-2">
50
+ {urls.map((url, i) => (
51
+ <img
52
+ key={i}
53
+ src={url}
54
+ alt={`${field.definitionName} ${i + 1}`}
55
+ className="h-16 w-16 rounded object-cover"
56
+ />
57
+ ))}
58
+ </div>
59
+ );
60
+ }
61
+ case 'URL':
62
+ return field.value ? (
63
+ <a
64
+ href={field.value}
65
+ target="_blank"
66
+ rel="noopener noreferrer"
67
+ className="text-primary break-all hover:underline"
68
+ >
69
+ {field.value}
70
+ </a>
71
+ ) : (
72
+ <span className="text-muted-foreground">-</span>
73
+ );
74
+ case 'COLOR':
75
+ return field.value ? (
76
+ <span className="inline-flex items-center gap-2">
77
+ <span
78
+ className="border-border inline-block h-4 w-4 rounded-full border"
79
+ style={{ backgroundColor: field.value }}
80
+ />
81
+ {field.value}
82
+ </span>
83
+ ) : (
84
+ <span className="text-muted-foreground">-</span>
85
+ );
86
+ case 'BOOLEAN':
87
+ return <span>{field.value === 'true' ? tc('yes') : tc('no')}</span>;
88
+ case 'DATE':
89
+ case 'DATETIME': {
90
+ if (!field.value) return <span className="text-muted-foreground">-</span>;
91
+ try {
92
+ const date = new Date(field.value);
93
+ return (
94
+ <span>
95
+ {field.type === 'DATETIME' ? date.toLocaleString() : date.toLocaleDateString()}
96
+ </span>
97
+ );
98
+ } catch {
99
+ return <span>{field.value}</span>;
100
+ }
101
+ }
102
+ default:
103
+ return <span>{field.value || '-'}</span>;
104
+ }
105
+ }
106
+
107
+ interface ProductClientSectionProps {
108
+ product: Product;
109
+ /**
110
+ * From `getStoreInfo().stockAlertsEnabled` the merchant's switch for
111
+ * back-in-stock alerts on this storefront. Passed down rather than fetched
112
+ * here because the page already holds storeInfo and a second call would be a
113
+ * waterfall. Undefined on a public-storefront (storeId) client, where the
114
+ * setting does not apply and the feature is on.
115
+ */
116
+ stockAlertsEnabled?: boolean;
117
+ }
118
+
119
+ export function ProductClientSection({
120
+ product: initialProduct,
121
+ stockAlertsEnabled,
122
+ }: ProductClientSectionProps) {
123
+ const t = useTranslations('productDetail');
124
+
125
+ const {
126
+ product,
127
+ recommendations,
128
+ images,
129
+ selectedImageIndex,
130
+ setSelectedImageIndex,
131
+ mainImageUrl,
132
+ selectedVariant,
133
+ setSelectedVariant,
134
+ priceInfo,
135
+ inventory,
136
+ canPurchase,
137
+ description,
138
+ quantity,
139
+ setQuantity,
140
+ addingToCart,
141
+ addedMessage,
142
+ handleAddToCart,
143
+ customizationFields,
144
+ customizationValues,
145
+ setCustomizationValues,
146
+ customizationErrors,
147
+ modifierGroups,
148
+ modifierSelections,
149
+ setModifierSelection,
150
+ modifierError,
151
+ } = useProductPage(initialProduct);
152
+
153
+ return (
154
+ <div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
155
+ <div className="grid grid-cols-1 gap-8 lg:grid-cols-2 lg:gap-12">
156
+ {/* Image Gallery */}
157
+ <div className="space-y-4">
158
+ {/* Main Image */}
159
+ <div className="bg-muted relative aspect-square overflow-hidden rounded-lg">
160
+ {mainImageUrl ? (
161
+ <Image
162
+ src={mainImageUrl}
163
+ alt={product.name}
164
+ fill
165
+ sizes="(max-width: 1024px) 100vw, 50vw"
166
+ className="object-contain"
167
+ priority
168
+ />
169
+ ) : (
170
+ <div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
171
+ <ImageIcon className="h-24 w-24" strokeWidth={1} aria-hidden="true" />
172
+ </div>
173
+ )}
174
+ </div>
175
+
176
+ {/* Thumbnails */}
177
+ {images.length > 1 && (
178
+ <div className="flex gap-2 overflow-x-auto pb-2">
179
+ {images.map((img, idx) => (
180
+ <button
181
+ key={idx}
182
+ type="button"
183
+ onClick={() => setSelectedImageIndex(idx)}
184
+ className={cn(
185
+ 'relative h-16 w-16 flex-shrink-0 overflow-hidden rounded border-2 transition-colors',
186
+ selectedImageIndex === idx
187
+ ? 'border-primary'
188
+ : 'border-border hover:border-muted-foreground'
189
+ )}
190
+ >
191
+ <Image
192
+ src={img.url}
193
+ alt={img.alt || `${product.name} ${idx + 1}`}
194
+ fill
195
+ sizes="64px"
196
+ className="object-cover"
197
+ />
198
+ </button>
199
+ ))}
200
+ </div>
201
+ )}
202
+ </div>
203
+
204
+ {/* Product Info */}
205
+ <div className="space-y-6">
206
+ {/* Categories */}
207
+ {product.categories && product.categories.length > 0 && (
208
+ <div className="flex flex-wrap gap-2">
209
+ {product.categories.map((cat) => (
210
+ <Badge
211
+ key={cat.id}
212
+ variant="secondary"
213
+ className="text-muted-foreground bg-muted rounded px-2 py-1 font-normal"
214
+ >
215
+ {cat.name}
216
+ </Badge>
217
+ ))}
218
+ </div>
219
+ )}
220
+
221
+ {/* Brand */}
222
+ {(product as { brands?: Array<{ id: string; name: string }> }).brands &&
223
+ (product as { brands: Array<{ id: string; name: string }> }).brands.length > 0 && (
224
+ <div className="text-muted-foreground text-sm">
225
+ {t('by')}{' '}
226
+ <span className="text-foreground font-medium">
227
+ {(product as { brands: Array<{ id: string; name: string }> }).brands
228
+ .map((b) => b.name)
229
+ .join(', ')}
230
+ </span>
231
+ </div>
232
+ )}
233
+
234
+ {/* Title */}
235
+ <h1 className="text-foreground text-2xl font-bold sm:text-3xl">{product.name}</h1>
236
+
237
+ {/* Tags */}
238
+ {(product as unknown as { tags?: Array<{ id: string; name: string }> }).tags &&
239
+ (product as unknown as { tags: Array<{ id: string; name: string }> }).tags.length >
240
+ 0 && (
241
+ <div className="flex flex-wrap gap-1.5">
242
+ {(product as unknown as { tags: Array<{ id: string; name: string }> }).tags.map(
243
+ (tag) => (
244
+ <Badge
245
+ key={tag.id}
246
+ variant="outline"
247
+ className="border-border text-muted-foreground px-2.5 py-0.5 font-normal"
248
+ >
249
+ #{tag.name}
250
+ </Badge>
251
+ )
252
+ )}
253
+ </div>
254
+ )}
255
+
256
+ {/* Price */}
257
+ <PriceDisplay
258
+ price={priceInfo.originalPrice}
259
+ salePrice={priceInfo.isOnSale ? priceInfo.price : undefined}
260
+ size="lg"
261
+ />
262
+
263
+ {/* Stock / Digital badge */}
264
+ {product.isDownloadable ? (
265
+ <Badge
266
+ variant="outline"
267
+ className="gap-1.5 border-transparent bg-green-100 px-3 py-1 font-medium text-green-800 dark:bg-green-950/30 dark:text-green-400"
268
+ >
269
+ <Download className="h-3.5 w-3.5" aria-hidden="true" />
270
+ {t('instantDownload')}
271
+ </Badge>
272
+ ) : (
273
+ <StockBadge inventory={inventory} lowStockThreshold={5} />
274
+ )}
275
+
276
+ {/* Downloadable files info */}
277
+ {product.isDownloadable && product.downloads && product.downloads.length > 0 && (
278
+ <div className="bg-muted/50 rounded-lg border p-4">
279
+ <p className="text-foreground mb-2 text-sm font-medium">
280
+ {t('filesIncluded')} ({product.downloads.length})
281
+ </p>
282
+ <ul className="space-y-1.5">
283
+ {product.downloads.map((file: DownloadFile) => (
284
+ <li
285
+ key={file.id}
286
+ className="text-muted-foreground flex items-center gap-2 text-sm"
287
+ >
288
+ <FileText
289
+ className="h-4 w-4 flex-shrink-0"
290
+ strokeWidth={1.5}
291
+ aria-hidden="true"
292
+ />
293
+ <span className="truncate">{file.name}</span>
294
+ {file.size && (
295
+ <span className="flex-shrink-0 text-xs">
296
+ (
297
+ {file.size < 1024 * 1024
298
+ ? `${(file.size / 1024).toFixed(0)} KB`
299
+ : `${(file.size / (1024 * 1024)).toFixed(1)} MB`}
300
+ )
301
+ </span>
302
+ )}
303
+ </li>
304
+ ))}
305
+ </ul>
306
+ </div>
307
+ )}
308
+
309
+ {/* Variant Selector */}
310
+ {product.type === 'VARIABLE' && product.variants && product.variants.length > 0 && (
311
+ <VariantSelector
312
+ product={product}
313
+ selectedVariant={selectedVariant}
314
+ onVariantChange={setSelectedVariant}
315
+ />
316
+ )}
317
+
318
+ {/* Customization Fields (buyer input) */}
319
+ {customizationFields.length > 0 && (
320
+ <CustomizationFields
321
+ fields={customizationFields}
322
+ values={customizationValues}
323
+ onChange={setCustomizationValues}
324
+ errors={customizationErrors}
325
+ />
326
+ )}
327
+
328
+ {/* Modifier groups (toppings, sauce, bread type, …) */}
329
+ {modifierGroups.length > 0 && (
330
+ <div className="space-y-4">
331
+ {modifierGroups.map((group) => (
332
+ <ModifierGroupSelector
333
+ key={group.attachmentId ?? group.id}
334
+ group={group}
335
+ value={modifierSelections[group.id] ?? []}
336
+ onChange={(next) => setModifierSelection(group.id, next)}
337
+ disabled={addingToCart}
338
+ />
339
+ ))}
340
+ {modifierError && (
341
+ <p className="text-destructive text-sm" role="alert">
342
+ {modifierError}
343
+ </p>
344
+ )}
345
+ </div>
346
+ )}
347
+
348
+ {/* Quantity + Add to Cart */}
349
+ <div className="flex items-center gap-4">
350
+ <div className="border-border flex items-center rounded border">
351
+ <button
352
+ type="button"
353
+ onClick={() => setQuantity((q) => Math.max(1, q - 1))}
354
+ className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
355
+ aria-label={t('decreaseQuantity')}
356
+ >
357
+ -
358
+ </button>
359
+ <span className="text-foreground min-w-[3rem] px-4 py-2 text-center text-sm font-medium">
360
+ {quantity}
361
+ </span>
362
+ <button
363
+ type="button"
364
+ onClick={() => setQuantity((q) => q + 1)}
365
+ className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
366
+ aria-label={t('increaseQuantity')}
367
+ >
368
+ +
369
+ </button>
370
+ </div>
371
+
372
+ <Button
373
+ type="button"
374
+ size="lg"
375
+ onClick={handleAddToCart}
376
+ disabled={!canPurchase || addingToCart}
377
+ className={cn(
378
+ 'flex-1 rounded px-6 text-sm',
379
+ !canPurchase && 'bg-muted text-muted-foreground hover:bg-muted disabled:opacity-100'
380
+ )}
381
+ >
382
+ {addingToCart ? (
383
+ <span className="inline-flex items-center gap-2">
384
+ <LoadingSpinner
385
+ size="sm"
386
+ className="border-primary-foreground/30 border-t-primary-foreground"
387
+ />
388
+ {t('addingToCart')}
389
+ </span>
390
+ ) : addedMessage ? (
391
+ t('addedToCart')
392
+ ) : !canPurchase ? (
393
+ t('outOfStock')
394
+ ) : (
395
+ t('addToCart')
396
+ )}
397
+ </Button>
398
+ </div>
399
+
400
+ {/*
401
+ Sold out is not the end of the page. `canOfferStockAlert` is the whole
402
+ gate — it also covers the merchant's switch and backorderable items,
403
+ where an alert would tell someone to come back and do what they can
404
+ already do. The selected variant is passed on purpose: without it a
405
+ shopper who wanted the medium gets mailed when the small returns.
406
+ */}
407
+ {canOfferStockAlert(inventory, stockAlertsEnabled) && (
408
+ <BackInStockForm productId={product.id} variantId={selectedVariant?.id} />
409
+ )}
410
+
411
+ {/* Download after purchase note */}
412
+ {product.isDownloadable && (
413
+ <p className="text-muted-foreground text-sm">{t('downloadAfterPurchase')}</p>
414
+ )}
415
+
416
+ {/* Description */}
417
+ {description && (
418
+ <div>
419
+ <Separator className="mb-4" />
420
+ <h2 className="text-foreground mb-3 text-lg font-semibold">{t('description')}</h2>
421
+ {'html' in description ? (
422
+ <div
423
+ className="prose prose-sm text-muted-foreground max-w-none"
424
+ dangerouslySetInnerHTML={{ __html: sanitizeProductHtml(description.html) }}
425
+ />
426
+ ) : (
427
+ <p className="text-muted-foreground whitespace-pre-wrap">{description.text}</p>
428
+ )}
429
+ </div>
430
+ )}
431
+
432
+ {/* Metafields / Specifications */}
433
+ {product.metafields && product.metafields.length > 0 && (
434
+ <div>
435
+ <Separator className="mb-4" />
436
+ <h2 className="text-foreground mb-3 text-lg font-semibold">{t('specifications')}</h2>
437
+ <table className="w-full text-sm">
438
+ <tbody>
439
+ {product.metafields.map((field) => (
440
+ <tr key={field.id} className="border-border border-b last:border-0">
441
+ <td className="text-foreground whitespace-nowrap py-2 pe-4 font-medium">
442
+ {field.definitionName}
443
+ </td>
444
+ <td className="text-muted-foreground py-2">
445
+ <MetafieldValue field={field} />
446
+ </td>
447
+ </tr>
448
+ ))}
449
+ </tbody>
450
+ </table>
451
+ </div>
452
+ )}
453
+ </div>
454
+ </div>
455
+
456
+ {/* Frequently Bought Together (cross-sells) */}
457
+ {recommendations?.crossSells && recommendations.crossSells.length > 0 && (
458
+ <FrequentlyBoughtTogether
459
+ items={recommendations.crossSells}
460
+ currentProduct={product}
461
+ className="mt-12"
462
+ />
463
+ )}
464
+
465
+ {/* Upsells */}
466
+ {recommendations?.upsells && recommendations.upsells.length > 0 && (
467
+ <RecommendationSection
468
+ title={t('upgradeYourChoice')}
469
+ items={recommendations.upsells}
470
+ className="mt-12"
471
+ />
472
+ )}
473
+
474
+ {/* Related products */}
475
+ {recommendations?.related && recommendations.related.length > 0 && (
476
+ <RecommendationSection
477
+ title={t('similarProducts')}
478
+ items={recommendations.related}
479
+ className="mt-12"
480
+ />
481
+ )}
482
+ </div>
483
+ );
484
+ }