create-brainerce-store 1.4.1 → 1.5.1

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 (37) hide show
  1. package/dist/index.js +1 -1
  2. package/messages/en.json +9 -1
  3. package/messages/he.json +9 -1
  4. package/package.json +1 -1
  5. package/templates/nextjs/base/src/app/account/page.tsx +8 -4
  6. package/templates/nextjs/base/src/app/auth/callback/page.tsx +90 -90
  7. package/templates/nextjs/base/src/app/cart/page.tsx +110 -110
  8. package/templates/nextjs/base/src/app/checkout/page.tsx +614 -614
  9. package/templates/nextjs/base/src/app/login/page.tsx +58 -58
  10. package/templates/nextjs/base/src/app/order-confirmation/page.tsx +193 -193
  11. package/templates/nextjs/base/src/app/page.tsx +98 -98
  12. package/templates/nextjs/base/src/app/products/[slug]/page.tsx +435 -435
  13. package/templates/nextjs/base/src/app/products/page.tsx +246 -246
  14. package/templates/nextjs/base/src/app/register/page.tsx +68 -68
  15. package/templates/nextjs/base/src/app/verify-email/page.tsx +293 -293
  16. package/templates/nextjs/base/src/components/account/order-history.tsx +198 -198
  17. package/templates/nextjs/base/src/components/account/profile-section.tsx +189 -40
  18. package/templates/nextjs/base/src/components/auth/login-form.tsx +94 -94
  19. package/templates/nextjs/base/src/components/auth/oauth-buttons.tsx +137 -137
  20. package/templates/nextjs/base/src/components/auth/register-form.tsx +184 -184
  21. package/templates/nextjs/base/src/components/cart/cart-item.tsx +153 -153
  22. package/templates/nextjs/base/src/components/cart/cart-summary.tsx +70 -70
  23. package/templates/nextjs/base/src/components/cart/coupon-input.tsx +134 -134
  24. package/templates/nextjs/base/src/components/cart/reservation-countdown.tsx +103 -103
  25. package/templates/nextjs/base/src/components/checkout/checkout-form.tsx +305 -305
  26. package/templates/nextjs/base/src/components/checkout/delivery-method-step.tsx +64 -64
  27. package/templates/nextjs/base/src/components/checkout/payment-step.tsx +350 -344
  28. package/templates/nextjs/base/src/components/checkout/pickup-step.tsx +199 -199
  29. package/templates/nextjs/base/src/components/checkout/shipping-step.tsx +110 -110
  30. package/templates/nextjs/base/src/components/checkout/tax-display.tsx +65 -65
  31. package/templates/nextjs/base/src/components/layout/footer.tsx +38 -38
  32. package/templates/nextjs/base/src/components/layout/header.tsx +332 -332
  33. package/templates/nextjs/base/src/components/products/product-card.tsx +96 -96
  34. package/templates/nextjs/base/src/components/products/product-grid.tsx +35 -35
  35. package/templates/nextjs/base/src/components/shared/loading-spinner.tsx +32 -32
  36. package/templates/nextjs/base/src/lib/translations.ts +11 -11
  37. package/templates/nextjs/base/src/providers/store-provider.tsx.ejs +5 -1
@@ -1,435 +1,435 @@
1
- 'use client';
2
-
3
- import { useEffect, useState, useMemo } from 'react';
4
- import { useParams } from 'next/navigation';
5
- import Image from 'next/image';
6
- import Link from 'next/link';
7
- import type { Product, ProductVariant, ProductImage, ProductMetafield } from 'brainerce';
8
- import { getProductPriceInfo, getDescriptionContent } from 'brainerce';
9
- import { getClient } from '@/lib/brainerce';
10
- import { useCart } from '@/providers/store-provider';
11
- import { PriceDisplay } from '@/components/shared/price-display';
12
- import { LoadingSpinner } from '@/components/shared/loading-spinner';
13
- import { VariantSelector } from '@/components/products/variant-selector';
14
- import { StockBadge } from '@/components/products/stock-badge';
15
- import { useTranslations } from '@/lib/translations';
16
- import { cn } from '@/lib/utils';
17
-
18
- /** Render a metafield value based on its type */
19
- function MetafieldValue({ field }: { field: ProductMetafield }) {
20
- const tc = useTranslations('common');
21
- switch (field.type) {
22
- case 'IMAGE': {
23
- if (!field.value) return <span className="text-muted-foreground">-</span>;
24
- return (
25
- <img
26
- src={field.value}
27
- alt={field.definitionName}
28
- className="h-16 w-16 rounded object-cover"
29
- />
30
- );
31
- }
32
- case 'GALLERY': {
33
- let urls: string[] = [];
34
- try {
35
- const parsed = JSON.parse(field.value);
36
- urls = Array.isArray(parsed)
37
- ? parsed.filter((u: unknown) => typeof u === 'string' && u)
38
- : [];
39
- } catch {
40
- urls = field.value ? [field.value] : [];
41
- }
42
- if (urls.length === 0) return <span className="text-muted-foreground">-</span>;
43
- return (
44
- <div className="flex flex-wrap gap-2">
45
- {urls.map((url, i) => (
46
- <img
47
- key={i}
48
- src={url}
49
- alt={`${field.definitionName} ${i + 1}`}
50
- className="h-16 w-16 rounded object-cover"
51
- />
52
- ))}
53
- </div>
54
- );
55
- }
56
- case 'URL':
57
- return field.value ? (
58
- <a
59
- href={field.value}
60
- target="_blank"
61
- rel="noopener noreferrer"
62
- className="text-primary break-all hover:underline"
63
- >
64
- {field.value}
65
- </a>
66
- ) : (
67
- <span className="text-muted-foreground">-</span>
68
- );
69
- case 'COLOR':
70
- return field.value ? (
71
- <span className="inline-flex items-center gap-2">
72
- <span
73
- className="border-border inline-block h-4 w-4 rounded-full border"
74
- style={{ backgroundColor: field.value }}
75
- />
76
- {field.value}
77
- </span>
78
- ) : (
79
- <span className="text-muted-foreground">-</span>
80
- );
81
- case 'BOOLEAN':
82
- return <span>{field.value === 'true' ? tc('yes') : tc('no')}</span>;
83
- case 'DATE':
84
- case 'DATETIME': {
85
- if (!field.value) return <span className="text-muted-foreground">-</span>;
86
- try {
87
- const date = new Date(field.value);
88
- return (
89
- <span>
90
- {field.type === 'DATETIME' ? date.toLocaleString() : date.toLocaleDateString()}
91
- </span>
92
- );
93
- } catch {
94
- return <span>{field.value}</span>;
95
- }
96
- }
97
- default:
98
- return <span>{field.value || '-'}</span>;
99
- }
100
- }
101
-
102
- export default function ProductDetailPage() {
103
- const params = useParams();
104
- const slug = params.slug as string;
105
- const { refreshCart } = useCart();
106
- const t = useTranslations('productDetail');
107
- const tc = useTranslations('common');
108
- const [product, setProduct] = useState<Product | null>(null);
109
- const [loading, setLoading] = useState(true);
110
- const [error, setError] = useState<string | null>(null);
111
- const [selectedVariant, setSelectedVariant] = useState<ProductVariant | null>(null);
112
- const [selectedImageIndex, setSelectedImageIndex] = useState(0);
113
- const [quantity, setQuantity] = useState(1);
114
- const [addingToCart, setAddingToCart] = useState(false);
115
- const [addedMessage, setAddedMessage] = useState(false);
116
-
117
- // Load product
118
- useEffect(() => {
119
- async function load() {
120
- try {
121
- setLoading(true);
122
- setError(null);
123
- const client = getClient();
124
- const p = await client.getProductBySlug(slug);
125
- setProduct(p);
126
-
127
- // Auto-select first variant
128
- if (p.variants && p.variants.length > 0) {
129
- setSelectedVariant(p.variants[0]);
130
- }
131
- } catch {
132
- setError(t('notFound'));
133
- } finally {
134
- setLoading(false);
135
- }
136
- }
137
- load();
138
- }, [slug]);
139
-
140
- // Images list - switch main image when variant changes
141
- const images: ProductImage[] = useMemo(() => {
142
- return product?.images || [];
143
- }, [product]);
144
-
145
- // When variant changes, update selected image to variant image if available
146
- useEffect(() => {
147
- if (!selectedVariant?.image || !product) return;
148
-
149
- const variantImgUrl =
150
- typeof selectedVariant.image === 'string' ? selectedVariant.image : selectedVariant.image.url;
151
-
152
- // Find if variant image exists in product images
153
- const idx = images.findIndex((img) => img.url === variantImgUrl);
154
- if (idx >= 0) {
155
- setSelectedImageIndex(idx);
156
- } else {
157
- // Variant image not in product images - select index 0 as fallback
158
- // (The variant image will be shown as the main image via override)
159
- setSelectedImageIndex(-1);
160
- }
161
- }, [selectedVariant, images, product]);
162
-
163
- // Determine which image to show
164
- const mainImageUrl = useMemo(() => {
165
- if (selectedImageIndex === -1 && selectedVariant?.image) {
166
- const img = selectedVariant.image;
167
- return typeof img === 'string' ? img : img.url;
168
- }
169
- return images[selectedImageIndex]?.url || null;
170
- }, [selectedImageIndex, selectedVariant, images]);
171
-
172
- // Price info - use variant price if selected, else product price
173
- const priceInfo = useMemo(() => {
174
- if (selectedVariant?.price) {
175
- return {
176
- price: parseFloat(selectedVariant.salePrice || selectedVariant.price),
177
- originalPrice: parseFloat(selectedVariant.price),
178
- isOnSale:
179
- selectedVariant.salePrice != null &&
180
- parseFloat(selectedVariant.salePrice) < parseFloat(selectedVariant.price),
181
- discountPercent:
182
- selectedVariant.salePrice != null &&
183
- parseFloat(selectedVariant.salePrice) < parseFloat(selectedVariant.price)
184
- ? Math.round(
185
- ((parseFloat(selectedVariant.price) - parseFloat(selectedVariant.salePrice)) /
186
- parseFloat(selectedVariant.price)) *
187
- 100
188
- )
189
- : 0,
190
- };
191
- }
192
- return getProductPriceInfo(product);
193
- }, [product, selectedVariant]);
194
-
195
- // Inventory: use variant inventory if selected, else product inventory
196
- const inventory = selectedVariant?.inventory ?? product?.inventory ?? null;
197
- const canPurchase = inventory?.canPurchase !== false;
198
-
199
- // Description
200
- const description = useMemo(() => {
201
- return product ? getDescriptionContent(product) : null;
202
- }, [product]);
203
-
204
- async function handleAddToCart() {
205
- if (!product || addingToCart) return;
206
-
207
- try {
208
- setAddingToCart(true);
209
- const client = getClient();
210
- await client.smartAddToCart({
211
- productId: product.id,
212
- variantId: selectedVariant?.id,
213
- quantity,
214
- name: product.name,
215
- price: String(priceInfo.price),
216
- image: mainImageUrl || undefined,
217
- });
218
- await refreshCart();
219
- setAddedMessage(true);
220
- setTimeout(() => setAddedMessage(false), 2000);
221
- } catch (err) {
222
- console.error('Failed to add to cart:', err);
223
- } finally {
224
- setAddingToCart(false);
225
- }
226
- }
227
-
228
- if (loading) {
229
- return (
230
- <div className="flex min-h-[60vh] items-center justify-center">
231
- <LoadingSpinner size="lg" />
232
- </div>
233
- );
234
- }
235
-
236
- if (error || !product) {
237
- return (
238
- <div className="mx-auto max-w-7xl px-4 py-16 text-center sm:px-6 lg:px-8">
239
- <h1 className="text-foreground text-2xl font-bold">{error || t('notFound')}</h1>
240
- <Link href="/products" className="text-primary mt-4 inline-block hover:underline">
241
- {t('backToProducts')}
242
- </Link>
243
- </div>
244
- );
245
- }
246
-
247
- return (
248
- <div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
249
- <div className="grid grid-cols-1 gap-8 lg:grid-cols-2 lg:gap-12">
250
- {/* Image Gallery */}
251
- <div className="space-y-4">
252
- {/* Main Image */}
253
- <div className="bg-muted relative aspect-square overflow-hidden rounded-lg">
254
- {mainImageUrl ? (
255
- <Image
256
- src={mainImageUrl}
257
- alt={product.name}
258
- fill
259
- sizes="(max-width: 1024px) 100vw, 50vw"
260
- className="object-contain"
261
- priority
262
- />
263
- ) : (
264
- <div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
265
- <svg className="h-24 w-24" fill="none" viewBox="0 0 24 24" stroke="currentColor">
266
- <path
267
- strokeLinecap="round"
268
- strokeLinejoin="round"
269
- strokeWidth={1}
270
- 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"
271
- />
272
- </svg>
273
- </div>
274
- )}
275
- </div>
276
-
277
- {/* Thumbnails */}
278
- {images.length > 1 && (
279
- <div className="flex gap-2 overflow-x-auto pb-2">
280
- {images.map((img, idx) => (
281
- <button
282
- key={idx}
283
- type="button"
284
- onClick={() => setSelectedImageIndex(idx)}
285
- className={cn(
286
- 'relative h-16 w-16 flex-shrink-0 overflow-hidden rounded border-2 transition-colors',
287
- selectedImageIndex === idx
288
- ? 'border-primary'
289
- : 'border-border hover:border-muted-foreground'
290
- )}
291
- >
292
- <Image
293
- src={img.url}
294
- alt={img.alt || `${product.name} ${idx + 1}`}
295
- fill
296
- sizes="64px"
297
- className="object-cover"
298
- />
299
- </button>
300
- ))}
301
- </div>
302
- )}
303
- </div>
304
-
305
- {/* Product Info */}
306
- <div className="space-y-6">
307
- {/* Categories */}
308
- {product.categories && product.categories.length > 0 && (
309
- <div className="flex flex-wrap gap-2">
310
- {product.categories.map((cat) => (
311
- <span
312
- key={cat.id}
313
- className="text-muted-foreground bg-muted rounded px-2 py-1 text-xs"
314
- >
315
- {cat.name}
316
- </span>
317
- ))}
318
- </div>
319
- )}
320
-
321
- {/* Title */}
322
- <h1 className="text-foreground text-2xl font-bold sm:text-3xl">{product.name}</h1>
323
-
324
- {/* Price */}
325
- <PriceDisplay
326
- price={priceInfo.originalPrice}
327
- salePrice={priceInfo.isOnSale ? priceInfo.price : undefined}
328
- size="lg"
329
- />
330
-
331
- {/* Stock */}
332
- <StockBadge inventory={inventory} lowStockThreshold={5} />
333
-
334
- {/* Variant Selector */}
335
- {product.type === 'VARIABLE' && product.variants && product.variants.length > 0 && (
336
- <VariantSelector
337
- product={product}
338
- selectedVariant={selectedVariant}
339
- onVariantChange={setSelectedVariant}
340
- />
341
- )}
342
-
343
- {/* Quantity + Add to Cart */}
344
- <div className="flex items-center gap-4">
345
- <div className="border-border flex items-center rounded border">
346
- <button
347
- type="button"
348
- onClick={() => setQuantity((q) => Math.max(1, q - 1))}
349
- className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
350
- aria-label={t('decreaseQuantity')}
351
- >
352
- -
353
- </button>
354
- <span className="text-foreground min-w-[3rem] px-4 py-2 text-center text-sm font-medium">
355
- {quantity}
356
- </span>
357
- <button
358
- type="button"
359
- onClick={() => setQuantity((q) => q + 1)}
360
- className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
361
- aria-label={t('increaseQuantity')}
362
- >
363
- +
364
- </button>
365
- </div>
366
-
367
- <button
368
- type="button"
369
- onClick={handleAddToCart}
370
- disabled={!canPurchase || addingToCart}
371
- className={cn(
372
- 'flex-1 rounded px-6 py-3 text-sm font-medium transition-all',
373
- canPurchase
374
- ? 'bg-primary text-primary-foreground hover:opacity-90'
375
- : 'bg-muted text-muted-foreground cursor-not-allowed'
376
- )}
377
- >
378
- {addingToCart ? (
379
- <span className="inline-flex items-center gap-2">
380
- <LoadingSpinner
381
- size="sm"
382
- className="border-primary-foreground/30 border-t-primary-foreground"
383
- />
384
- {t('addingToCart')}
385
- </span>
386
- ) : addedMessage ? (
387
- t('addedToCart')
388
- ) : !canPurchase ? (
389
- t('outOfStock')
390
- ) : (
391
- t('addToCart')
392
- )}
393
- </button>
394
- </div>
395
-
396
- {/* Description */}
397
- {description && (
398
- <div className="border-border border-t pt-4">
399
- <h2 className="text-foreground mb-3 text-lg font-semibold">{t('description')}</h2>
400
- {'html' in description ? (
401
- <div
402
- className="prose prose-sm text-muted-foreground max-w-none"
403
- dangerouslySetInnerHTML={{ __html: description.html }}
404
- />
405
- ) : (
406
- <p className="text-muted-foreground whitespace-pre-wrap">{description.text}</p>
407
- )}
408
- </div>
409
- )}
410
-
411
- {/* Metafields / Specifications */}
412
- {product.metafields && product.metafields.length > 0 && (
413
- <div className="border-border border-t pt-4">
414
- <h2 className="text-foreground mb-3 text-lg font-semibold">{t('specifications')}</h2>
415
- <table className="w-full text-sm">
416
- <tbody>
417
- {product.metafields.map((field) => (
418
- <tr key={field.id} className="border-border border-b last:border-0">
419
- <td className="text-foreground whitespace-nowrap py-2 pe-4 font-medium">
420
- {field.definitionName}
421
- </td>
422
- <td className="text-muted-foreground py-2">
423
- <MetafieldValue field={field} />
424
- </td>
425
- </tr>
426
- ))}
427
- </tbody>
428
- </table>
429
- </div>
430
- )}
431
- </div>
432
- </div>
433
- </div>
434
- );
435
- }
1
+ 'use client';
2
+
3
+ import { useEffect, useState, useMemo } from 'react';
4
+ import { useParams } from 'next/navigation';
5
+ import Image from 'next/image';
6
+ import Link from 'next/link';
7
+ import type { Product, ProductVariant, ProductImage, ProductMetafield } from 'brainerce';
8
+ import { getProductPriceInfo, getDescriptionContent } from 'brainerce';
9
+ import { getClient } from '@/lib/brainerce';
10
+ import { useCart } from '@/providers/store-provider';
11
+ import { PriceDisplay } from '@/components/shared/price-display';
12
+ import { LoadingSpinner } from '@/components/shared/loading-spinner';
13
+ import { VariantSelector } from '@/components/products/variant-selector';
14
+ import { StockBadge } from '@/components/products/stock-badge';
15
+ import { useTranslations } from '@/lib/translations';
16
+ import { cn } from '@/lib/utils';
17
+
18
+ /** Render a metafield value based on its type */
19
+ function MetafieldValue({ field }: { field: ProductMetafield }) {
20
+ const tc = useTranslations('common');
21
+ switch (field.type) {
22
+ case 'IMAGE': {
23
+ if (!field.value) return <span className="text-muted-foreground">-</span>;
24
+ return (
25
+ <img
26
+ src={field.value}
27
+ alt={field.definitionName}
28
+ className="h-16 w-16 rounded object-cover"
29
+ />
30
+ );
31
+ }
32
+ case 'GALLERY': {
33
+ let urls: string[] = [];
34
+ try {
35
+ const parsed = JSON.parse(field.value);
36
+ urls = Array.isArray(parsed)
37
+ ? parsed.filter((u: unknown) => typeof u === 'string' && u)
38
+ : [];
39
+ } catch {
40
+ urls = field.value ? [field.value] : [];
41
+ }
42
+ if (urls.length === 0) return <span className="text-muted-foreground">-</span>;
43
+ return (
44
+ <div className="flex flex-wrap gap-2">
45
+ {urls.map((url, i) => (
46
+ <img
47
+ key={i}
48
+ src={url}
49
+ alt={`${field.definitionName} ${i + 1}`}
50
+ className="h-16 w-16 rounded object-cover"
51
+ />
52
+ ))}
53
+ </div>
54
+ );
55
+ }
56
+ case 'URL':
57
+ return field.value ? (
58
+ <a
59
+ href={field.value}
60
+ target="_blank"
61
+ rel="noopener noreferrer"
62
+ className="text-primary break-all hover:underline"
63
+ >
64
+ {field.value}
65
+ </a>
66
+ ) : (
67
+ <span className="text-muted-foreground">-</span>
68
+ );
69
+ case 'COLOR':
70
+ return field.value ? (
71
+ <span className="inline-flex items-center gap-2">
72
+ <span
73
+ className="border-border inline-block h-4 w-4 rounded-full border"
74
+ style={{ backgroundColor: field.value }}
75
+ />
76
+ {field.value}
77
+ </span>
78
+ ) : (
79
+ <span className="text-muted-foreground">-</span>
80
+ );
81
+ case 'BOOLEAN':
82
+ return <span>{field.value === 'true' ? tc('yes') : tc('no')}</span>;
83
+ case 'DATE':
84
+ case 'DATETIME': {
85
+ if (!field.value) return <span className="text-muted-foreground">-</span>;
86
+ try {
87
+ const date = new Date(field.value);
88
+ return (
89
+ <span>
90
+ {field.type === 'DATETIME' ? date.toLocaleString() : date.toLocaleDateString()}
91
+ </span>
92
+ );
93
+ } catch {
94
+ return <span>{field.value}</span>;
95
+ }
96
+ }
97
+ default:
98
+ return <span>{field.value || '-'}</span>;
99
+ }
100
+ }
101
+
102
+ export default function ProductDetailPage() {
103
+ const params = useParams();
104
+ const slug = params.slug as string;
105
+ const { refreshCart } = useCart();
106
+ const t = useTranslations('productDetail');
107
+ const tc = useTranslations('common');
108
+ const [product, setProduct] = useState<Product | null>(null);
109
+ const [loading, setLoading] = useState(true);
110
+ const [error, setError] = useState<string | null>(null);
111
+ const [selectedVariant, setSelectedVariant] = useState<ProductVariant | null>(null);
112
+ const [selectedImageIndex, setSelectedImageIndex] = useState(0);
113
+ const [quantity, setQuantity] = useState(1);
114
+ const [addingToCart, setAddingToCart] = useState(false);
115
+ const [addedMessage, setAddedMessage] = useState(false);
116
+
117
+ // Load product
118
+ useEffect(() => {
119
+ async function load() {
120
+ try {
121
+ setLoading(true);
122
+ setError(null);
123
+ const client = getClient();
124
+ const p = await client.getProductBySlug(slug);
125
+ setProduct(p);
126
+
127
+ // Auto-select first variant
128
+ if (p.variants && p.variants.length > 0) {
129
+ setSelectedVariant(p.variants[0]);
130
+ }
131
+ } catch {
132
+ setError(t('notFound'));
133
+ } finally {
134
+ setLoading(false);
135
+ }
136
+ }
137
+ load();
138
+ }, [slug]);
139
+
140
+ // Images list - switch main image when variant changes
141
+ const images: ProductImage[] = useMemo(() => {
142
+ return product?.images || [];
143
+ }, [product]);
144
+
145
+ // When variant changes, update selected image to variant image if available
146
+ useEffect(() => {
147
+ if (!selectedVariant?.image || !product) return;
148
+
149
+ const variantImgUrl =
150
+ typeof selectedVariant.image === 'string' ? selectedVariant.image : selectedVariant.image.url;
151
+
152
+ // Find if variant image exists in product images
153
+ const idx = images.findIndex((img) => img.url === variantImgUrl);
154
+ if (idx >= 0) {
155
+ setSelectedImageIndex(idx);
156
+ } else {
157
+ // Variant image not in product images - select index 0 as fallback
158
+ // (The variant image will be shown as the main image via override)
159
+ setSelectedImageIndex(-1);
160
+ }
161
+ }, [selectedVariant, images, product]);
162
+
163
+ // Determine which image to show
164
+ const mainImageUrl = useMemo(() => {
165
+ if (selectedImageIndex === -1 && selectedVariant?.image) {
166
+ const img = selectedVariant.image;
167
+ return typeof img === 'string' ? img : img.url;
168
+ }
169
+ return images[selectedImageIndex]?.url || null;
170
+ }, [selectedImageIndex, selectedVariant, images]);
171
+
172
+ // Price info - use variant price if selected, else product price
173
+ const priceInfo = useMemo(() => {
174
+ if (selectedVariant?.price) {
175
+ return {
176
+ price: parseFloat(selectedVariant.salePrice || selectedVariant.price),
177
+ originalPrice: parseFloat(selectedVariant.price),
178
+ isOnSale:
179
+ selectedVariant.salePrice != null &&
180
+ parseFloat(selectedVariant.salePrice) < parseFloat(selectedVariant.price),
181
+ discountPercent:
182
+ selectedVariant.salePrice != null &&
183
+ parseFloat(selectedVariant.salePrice) < parseFloat(selectedVariant.price)
184
+ ? Math.round(
185
+ ((parseFloat(selectedVariant.price) - parseFloat(selectedVariant.salePrice)) /
186
+ parseFloat(selectedVariant.price)) *
187
+ 100
188
+ )
189
+ : 0,
190
+ };
191
+ }
192
+ return getProductPriceInfo(product);
193
+ }, [product, selectedVariant]);
194
+
195
+ // Inventory: use variant inventory if selected, else product inventory
196
+ const inventory = selectedVariant?.inventory ?? product?.inventory ?? null;
197
+ const canPurchase = inventory?.canPurchase !== false;
198
+
199
+ // Description
200
+ const description = useMemo(() => {
201
+ return product ? getDescriptionContent(product) : null;
202
+ }, [product]);
203
+
204
+ async function handleAddToCart() {
205
+ if (!product || addingToCart) return;
206
+
207
+ try {
208
+ setAddingToCart(true);
209
+ const client = getClient();
210
+ await client.smartAddToCart({
211
+ productId: product.id,
212
+ variantId: selectedVariant?.id,
213
+ quantity,
214
+ name: product.name,
215
+ price: String(priceInfo.price),
216
+ image: mainImageUrl || undefined,
217
+ });
218
+ await refreshCart();
219
+ setAddedMessage(true);
220
+ setTimeout(() => setAddedMessage(false), 2000);
221
+ } catch (err) {
222
+ console.error('Failed to add to cart:', err);
223
+ } finally {
224
+ setAddingToCart(false);
225
+ }
226
+ }
227
+
228
+ if (loading) {
229
+ return (
230
+ <div className="flex min-h-[60vh] items-center justify-center">
231
+ <LoadingSpinner size="lg" />
232
+ </div>
233
+ );
234
+ }
235
+
236
+ if (error || !product) {
237
+ return (
238
+ <div className="mx-auto max-w-7xl px-4 py-16 text-center sm:px-6 lg:px-8">
239
+ <h1 className="text-foreground text-2xl font-bold">{error || t('notFound')}</h1>
240
+ <Link href="/products" className="text-primary mt-4 inline-block hover:underline">
241
+ {t('backToProducts')}
242
+ </Link>
243
+ </div>
244
+ );
245
+ }
246
+
247
+ return (
248
+ <div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
249
+ <div className="grid grid-cols-1 gap-8 lg:grid-cols-2 lg:gap-12">
250
+ {/* Image Gallery */}
251
+ <div className="space-y-4">
252
+ {/* Main Image */}
253
+ <div className="bg-muted relative aspect-square overflow-hidden rounded-lg">
254
+ {mainImageUrl ? (
255
+ <Image
256
+ src={mainImageUrl}
257
+ alt={product.name}
258
+ fill
259
+ sizes="(max-width: 1024px) 100vw, 50vw"
260
+ className="object-contain"
261
+ priority
262
+ />
263
+ ) : (
264
+ <div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
265
+ <svg className="h-24 w-24" fill="none" viewBox="0 0 24 24" stroke="currentColor">
266
+ <path
267
+ strokeLinecap="round"
268
+ strokeLinejoin="round"
269
+ strokeWidth={1}
270
+ 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"
271
+ />
272
+ </svg>
273
+ </div>
274
+ )}
275
+ </div>
276
+
277
+ {/* Thumbnails */}
278
+ {images.length > 1 && (
279
+ <div className="flex gap-2 overflow-x-auto pb-2">
280
+ {images.map((img, idx) => (
281
+ <button
282
+ key={idx}
283
+ type="button"
284
+ onClick={() => setSelectedImageIndex(idx)}
285
+ className={cn(
286
+ 'relative h-16 w-16 flex-shrink-0 overflow-hidden rounded border-2 transition-colors',
287
+ selectedImageIndex === idx
288
+ ? 'border-primary'
289
+ : 'border-border hover:border-muted-foreground'
290
+ )}
291
+ >
292
+ <Image
293
+ src={img.url}
294
+ alt={img.alt || `${product.name} ${idx + 1}`}
295
+ fill
296
+ sizes="64px"
297
+ className="object-cover"
298
+ />
299
+ </button>
300
+ ))}
301
+ </div>
302
+ )}
303
+ </div>
304
+
305
+ {/* Product Info */}
306
+ <div className="space-y-6">
307
+ {/* Categories */}
308
+ {product.categories && product.categories.length > 0 && (
309
+ <div className="flex flex-wrap gap-2">
310
+ {product.categories.map((cat) => (
311
+ <span
312
+ key={cat.id}
313
+ className="text-muted-foreground bg-muted rounded px-2 py-1 text-xs"
314
+ >
315
+ {cat.name}
316
+ </span>
317
+ ))}
318
+ </div>
319
+ )}
320
+
321
+ {/* Title */}
322
+ <h1 className="text-foreground text-2xl font-bold sm:text-3xl">{product.name}</h1>
323
+
324
+ {/* Price */}
325
+ <PriceDisplay
326
+ price={priceInfo.originalPrice}
327
+ salePrice={priceInfo.isOnSale ? priceInfo.price : undefined}
328
+ size="lg"
329
+ />
330
+
331
+ {/* Stock */}
332
+ <StockBadge inventory={inventory} lowStockThreshold={5} />
333
+
334
+ {/* Variant Selector */}
335
+ {product.type === 'VARIABLE' && product.variants && product.variants.length > 0 && (
336
+ <VariantSelector
337
+ product={product}
338
+ selectedVariant={selectedVariant}
339
+ onVariantChange={setSelectedVariant}
340
+ />
341
+ )}
342
+
343
+ {/* Quantity + Add to Cart */}
344
+ <div className="flex items-center gap-4">
345
+ <div className="border-border flex items-center rounded border">
346
+ <button
347
+ type="button"
348
+ onClick={() => setQuantity((q) => Math.max(1, q - 1))}
349
+ className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
350
+ aria-label={t('decreaseQuantity')}
351
+ >
352
+ -
353
+ </button>
354
+ <span className="text-foreground min-w-[3rem] px-4 py-2 text-center text-sm font-medium">
355
+ {quantity}
356
+ </span>
357
+ <button
358
+ type="button"
359
+ onClick={() => setQuantity((q) => q + 1)}
360
+ className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
361
+ aria-label={t('increaseQuantity')}
362
+ >
363
+ +
364
+ </button>
365
+ </div>
366
+
367
+ <button
368
+ type="button"
369
+ onClick={handleAddToCart}
370
+ disabled={!canPurchase || addingToCart}
371
+ className={cn(
372
+ 'flex-1 rounded px-6 py-3 text-sm font-medium transition-all',
373
+ canPurchase
374
+ ? 'bg-primary text-primary-foreground hover:opacity-90'
375
+ : 'bg-muted text-muted-foreground cursor-not-allowed'
376
+ )}
377
+ >
378
+ {addingToCart ? (
379
+ <span className="inline-flex items-center gap-2">
380
+ <LoadingSpinner
381
+ size="sm"
382
+ className="border-primary-foreground/30 border-t-primary-foreground"
383
+ />
384
+ {t('addingToCart')}
385
+ </span>
386
+ ) : addedMessage ? (
387
+ t('addedToCart')
388
+ ) : !canPurchase ? (
389
+ t('outOfStock')
390
+ ) : (
391
+ t('addToCart')
392
+ )}
393
+ </button>
394
+ </div>
395
+
396
+ {/* Description */}
397
+ {description && (
398
+ <div className="border-border border-t pt-4">
399
+ <h2 className="text-foreground mb-3 text-lg font-semibold">{t('description')}</h2>
400
+ {'html' in description ? (
401
+ <div
402
+ className="prose prose-sm text-muted-foreground max-w-none"
403
+ dangerouslySetInnerHTML={{ __html: description.html }}
404
+ />
405
+ ) : (
406
+ <p className="text-muted-foreground whitespace-pre-wrap">{description.text}</p>
407
+ )}
408
+ </div>
409
+ )}
410
+
411
+ {/* Metafields / Specifications */}
412
+ {product.metafields && product.metafields.length > 0 && (
413
+ <div className="border-border border-t pt-4">
414
+ <h2 className="text-foreground mb-3 text-lg font-semibold">{t('specifications')}</h2>
415
+ <table className="w-full text-sm">
416
+ <tbody>
417
+ {product.metafields.map((field) => (
418
+ <tr key={field.id} className="border-border border-b last:border-0">
419
+ <td className="text-foreground whitespace-nowrap py-2 pe-4 font-medium">
420
+ {field.definitionName}
421
+ </td>
422
+ <td className="text-muted-foreground py-2">
423
+ <MetafieldValue field={field} />
424
+ </td>
425
+ </tr>
426
+ ))}
427
+ </tbody>
428
+ </table>
429
+ </div>
430
+ )}
431
+ </div>
432
+ </div>
433
+ </div>
434
+ );
435
+ }