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