create-brainerce-store 1.13.0 → 1.14.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.
package/dist/index.js CHANGED
@@ -31,7 +31,7 @@ var require_package = __commonJS({
31
31
  "package.json"(exports2, module2) {
32
32
  module2.exports = {
33
33
  name: "create-brainerce-store",
34
- version: "1.13.0",
34
+ version: "1.14.0",
35
35
  description: "Scaffold a production-ready e-commerce storefront connected to Brainerce",
36
36
  bin: {
37
37
  "create-brainerce-store": "dist/index.js"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-brainerce-store",
3
- "version": "1.13.0",
3
+ "version": "1.14.0",
4
4
  "description": "Scaffold a production-ready e-commerce storefront connected to Brainerce",
5
5
  "bin": {
6
6
  "create-brainerce-store": "dist/index.js"
@@ -9,7 +9,7 @@
9
9
  "lint": "next lint"
10
10
  },
11
11
  "dependencies": {
12
- "brainerce": "^1.0.0",
12
+ "brainerce": "^1.11.0",
13
13
  "next": "^15.0.0",
14
14
  "react": "^19.0.0",
15
15
  "react-dom": "^19.0.0",
@@ -1,485 +1,484 @@
1
- 'use client';
2
-
3
- import { useEffect, useState, useMemo } from 'react';
4
- import Image from 'next/image';
5
- import type {
6
- Product,
7
- ProductVariant,
8
- ProductImage,
9
- ProductMetafield,
10
- ProductRecommendationsResponse,
11
- DownloadFile,
12
- } from 'brainerce';
13
-
14
- type ProductWithRecommendations = Product & {
15
- recommendations?: ProductRecommendationsResponse;
16
- };
17
- import { getProductPriceInfo, getDescriptionContent } from 'brainerce';
18
- import { useCart } from '@/providers/store-provider';
19
- import { PriceDisplay } from '@/components/shared/price-display';
20
- import { LoadingSpinner } from '@/components/shared/loading-spinner';
21
- import { VariantSelector } from '@/components/products/variant-selector';
22
- import { StockBadge } from '@/components/products/stock-badge';
23
- import { RecommendationSection } from '@/components/products/recommendation-section';
24
- import { useTranslations } from '@/lib/translations';
25
- import { cn } from '@/lib/utils';
26
-
27
- /** Render a metafield value based on its type */
28
- function MetafieldValue({ field }: { field: ProductMetafield }) {
29
- const tc = useTranslations('common');
30
- switch (field.type) {
31
- case 'IMAGE': {
32
- if (!field.value) return <span className="text-muted-foreground">-</span>;
33
- return (
34
- <img
35
- src={field.value}
36
- alt={field.definitionName}
37
- className="h-16 w-16 rounded object-cover"
38
- />
39
- );
40
- }
41
- case 'GALLERY': {
42
- let urls: string[] = [];
43
- try {
44
- const parsed = JSON.parse(field.value);
45
- urls = Array.isArray(parsed)
46
- ? parsed.filter((u: unknown) => typeof u === 'string' && u)
47
- : [];
48
- } catch {
49
- urls = field.value ? [field.value] : [];
50
- }
51
- if (urls.length === 0) return <span className="text-muted-foreground">-</span>;
52
- return (
53
- <div className="flex flex-wrap gap-2">
54
- {urls.map((url, i) => (
55
- <img
56
- key={i}
57
- src={url}
58
- alt={`${field.definitionName} ${i + 1}`}
59
- className="h-16 w-16 rounded object-cover"
60
- />
61
- ))}
62
- </div>
63
- );
64
- }
65
- case 'URL':
66
- return field.value ? (
67
- <a
68
- href={field.value}
69
- target="_blank"
70
- rel="noopener noreferrer"
71
- className="text-primary break-all hover:underline"
72
- >
73
- {field.value}
74
- </a>
75
- ) : (
76
- <span className="text-muted-foreground">-</span>
77
- );
78
- case 'COLOR':
79
- return field.value ? (
80
- <span className="inline-flex items-center gap-2">
81
- <span
82
- className="border-border inline-block h-4 w-4 rounded-full border"
83
- style={{ backgroundColor: field.value }}
84
- />
85
- {field.value}
86
- </span>
87
- ) : (
88
- <span className="text-muted-foreground">-</span>
89
- );
90
- case 'BOOLEAN':
91
- return <span>{field.value === 'true' ? tc('yes') : tc('no')}</span>;
92
- case 'DATE':
93
- case 'DATETIME': {
94
- if (!field.value) return <span className="text-muted-foreground">-</span>;
95
- try {
96
- const date = new Date(field.value);
97
- return (
98
- <span>
99
- {field.type === 'DATETIME' ? date.toLocaleString() : date.toLocaleDateString()}
100
- </span>
101
- );
102
- } catch {
103
- return <span>{field.value}</span>;
104
- }
105
- }
106
- default:
107
- return <span>{field.value || '-'}</span>;
108
- }
109
- }
110
-
111
- interface ProductClientSectionProps {
112
- product: Product;
113
- }
114
-
115
- export function ProductClientSection({ product: initialProduct }: ProductClientSectionProps) {
116
- const { refreshCart } = useCart();
117
- const t = useTranslations('productDetail');
118
- const tc = useTranslations('common');
119
-
120
- const product = initialProduct as ProductWithRecommendations;
121
- const recommendations = product?.recommendations ?? null;
122
-
123
- const [selectedVariant, setSelectedVariant] = useState<ProductVariant | null>(
124
- product.variants && product.variants.length > 0 ? product.variants[0] : null
125
- );
126
- const [selectedImageIndex, setSelectedImageIndex] = useState(0);
127
- const [quantity, setQuantity] = useState(1);
128
- const [addingToCart, setAddingToCart] = useState(false);
129
- const [addedMessage, setAddedMessage] = useState(false);
130
-
131
- // Images list - switch main image when variant changes
132
- const images: ProductImage[] = useMemo(() => {
133
- return product?.images || [];
134
- }, [product]);
135
-
136
- // When variant changes, update selected image to variant image if available
137
- useEffect(() => {
138
- if (!selectedVariant?.image || !product) return;
139
-
140
- const variantImgUrl =
141
- typeof selectedVariant.image === 'string' ? selectedVariant.image : selectedVariant.image.url;
142
-
143
- // Find if variant image exists in product images
144
- const idx = images.findIndex((img) => img.url === variantImgUrl);
145
- if (idx >= 0) {
146
- setSelectedImageIndex(idx);
147
- } else {
148
- // Variant image not in product images - select index 0 as fallback
149
- setSelectedImageIndex(-1);
150
- }
151
- }, [selectedVariant, images, product]);
152
-
153
- // Determine which image to show
154
- const mainImageUrl = useMemo(() => {
155
- if (selectedImageIndex === -1 && selectedVariant?.image) {
156
- const img = selectedVariant.image;
157
- return typeof img === 'string' ? img : img.url;
158
- }
159
- return images[selectedImageIndex]?.url || null;
160
- }, [selectedImageIndex, selectedVariant, images]);
161
-
162
- // Price info - use variant price if selected, else product price
163
- const priceInfo = useMemo(() => {
164
- if (selectedVariant?.price) {
165
- return {
166
- price: parseFloat(selectedVariant.salePrice || selectedVariant.price),
167
- originalPrice: parseFloat(selectedVariant.price),
168
- isOnSale:
169
- selectedVariant.salePrice != null &&
170
- parseFloat(selectedVariant.salePrice) < parseFloat(selectedVariant.price),
171
- discountPercent:
172
- selectedVariant.salePrice != null &&
173
- parseFloat(selectedVariant.salePrice) < parseFloat(selectedVariant.price)
174
- ? Math.round(
175
- ((parseFloat(selectedVariant.price) - parseFloat(selectedVariant.salePrice)) /
176
- parseFloat(selectedVariant.price)) *
177
- 100
178
- )
179
- : 0,
180
- };
181
- }
182
- return getProductPriceInfo(product);
183
- }, [product, selectedVariant]);
184
-
185
- // Inventory: use variant inventory if selected, else product inventory
186
- const inventory = selectedVariant?.inventory ?? product?.inventory ?? null;
187
- const canPurchase = inventory?.canPurchase !== false;
188
-
189
- // Description
190
- const description = useMemo(() => {
191
- return product ? getDescriptionContent(product) : null;
192
- }, [product]);
193
-
194
- async function handleAddToCart() {
195
- if (!product || addingToCart) return;
196
-
197
- try {
198
- setAddingToCart(true);
199
- const { getClient } = await import('@/lib/brainerce');
200
- const client = getClient();
201
- await client.smartAddToCart({
202
- productId: product.id,
203
- variantId: selectedVariant?.id,
204
- quantity,
205
- name: product.name,
206
- price: String(priceInfo.price),
207
- image: mainImageUrl || undefined,
208
- });
209
- await refreshCart();
210
- setAddedMessage(true);
211
- setTimeout(() => setAddedMessage(false), 2000);
212
- } catch (err) {
213
- console.error('Failed to add to cart:', err);
214
- } finally {
215
- setAddingToCart(false);
216
- }
217
- }
218
-
219
- return (
220
- <div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
221
- <div className="grid grid-cols-1 gap-8 lg:grid-cols-2 lg:gap-12">
222
- {/* Image Gallery */}
223
- <div className="space-y-4">
224
- {/* Main Image */}
225
- <div className="bg-muted relative aspect-square overflow-hidden rounded-lg">
226
- {mainImageUrl ? (
227
- <Image
228
- src={mainImageUrl}
229
- alt={product.name}
230
- fill
231
- sizes="(max-width: 1024px) 100vw, 50vw"
232
- className="object-contain"
233
- priority
234
- />
235
- ) : (
236
- <div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
237
- <svg className="h-24 w-24" fill="none" viewBox="0 0 24 24" stroke="currentColor">
238
- <path
239
- strokeLinecap="round"
240
- strokeLinejoin="round"
241
- strokeWidth={1}
242
- 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"
243
- />
244
- </svg>
245
- </div>
246
- )}
247
- </div>
248
-
249
- {/* Thumbnails */}
250
- {images.length > 1 && (
251
- <div className="flex gap-2 overflow-x-auto pb-2">
252
- {images.map((img, idx) => (
253
- <button
254
- key={idx}
255
- type="button"
256
- onClick={() => setSelectedImageIndex(idx)}
257
- className={cn(
258
- 'relative h-16 w-16 flex-shrink-0 overflow-hidden rounded border-2 transition-colors',
259
- selectedImageIndex === idx
260
- ? 'border-primary'
261
- : 'border-border hover:border-muted-foreground'
262
- )}
263
- >
264
- <Image
265
- src={img.url}
266
- alt={img.alt || `${product.name} ${idx + 1}`}
267
- fill
268
- sizes="64px"
269
- className="object-cover"
270
- />
271
- </button>
272
- ))}
273
- </div>
274
- )}
275
- </div>
276
-
277
- {/* Product Info */}
278
- <div className="space-y-6">
279
- {/* Categories */}
280
- {product.categories && product.categories.length > 0 && (
281
- <div className="flex flex-wrap gap-2">
282
- {product.categories.map((cat) => (
283
- <span
284
- key={cat.id}
285
- className="text-muted-foreground bg-muted rounded px-2 py-1 text-xs"
286
- >
287
- {cat.name}
288
- </span>
289
- ))}
290
- </div>
291
- )}
292
-
293
- {/* Title */}
294
- <h1 className="text-foreground text-2xl font-bold sm:text-3xl">{product.name}</h1>
295
-
296
- {/* Price */}
297
- <PriceDisplay
298
- price={priceInfo.originalPrice}
299
- salePrice={priceInfo.isOnSale ? priceInfo.price : undefined}
300
- size="lg"
301
- />
302
-
303
- {/* Stock / Digital badge */}
304
- {product.isDownloadable ? (
305
- <span className="inline-flex items-center gap-1.5 rounded-full bg-green-100 px-3 py-1 text-xs font-medium text-green-800 dark:bg-green-950/30 dark:text-green-400">
306
- <svg className="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
307
- <path
308
- strokeLinecap="round"
309
- strokeLinejoin="round"
310
- strokeWidth={2}
311
- d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"
312
- />
313
- </svg>
314
- {t('instantDownload')}
315
- </span>
316
- ) : (
317
- <StockBadge inventory={inventory} lowStockThreshold={5} />
318
- )}
319
-
320
- {/* Downloadable files info */}
321
- {product.isDownloadable && product.downloads && product.downloads.length > 0 && (
322
- <div className="bg-muted/50 rounded-lg border p-4">
323
- <p className="text-foreground mb-2 text-sm font-medium">
324
- {t('filesIncluded', { count: product.downloads.length })}
325
- </p>
326
- <ul className="space-y-1.5">
327
- {product.downloads.map((file: DownloadFile) => (
328
- <li
329
- key={file.id}
330
- className="text-muted-foreground flex items-center gap-2 text-sm"
331
- >
332
- <svg
333
- className="h-4 w-4 flex-shrink-0"
334
- fill="none"
335
- viewBox="0 0 24 24"
336
- stroke="currentColor"
337
- >
338
- <path
339
- strokeLinecap="round"
340
- strokeLinejoin="round"
341
- strokeWidth={1.5}
342
- d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"
343
- />
344
- </svg>
345
- <span className="truncate">{file.name}</span>
346
- {file.size && (
347
- <span className="flex-shrink-0 text-xs">
348
- (
349
- {file.size < 1024 * 1024
350
- ? `${(file.size / 1024).toFixed(0)} KB`
351
- : `${(file.size / (1024 * 1024)).toFixed(1)} MB`}
352
- )
353
- </span>
354
- )}
355
- </li>
356
- ))}
357
- </ul>
358
- </div>
359
- )}
360
-
361
- {/* Variant Selector */}
362
- {product.type === 'VARIABLE' && product.variants && product.variants.length > 0 && (
363
- <VariantSelector
364
- product={product}
365
- selectedVariant={selectedVariant}
366
- onVariantChange={setSelectedVariant}
367
- />
368
- )}
369
-
370
- {/* Quantity + Add to Cart */}
371
- <div className="flex items-center gap-4">
372
- <div className="border-border flex items-center rounded border">
373
- <button
374
- type="button"
375
- onClick={() => setQuantity((q) => Math.max(1, q - 1))}
376
- className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
377
- aria-label={t('decreaseQuantity')}
378
- >
379
- -
380
- </button>
381
- <span className="text-foreground min-w-[3rem] px-4 py-2 text-center text-sm font-medium">
382
- {quantity}
383
- </span>
384
- <button
385
- type="button"
386
- onClick={() => setQuantity((q) => q + 1)}
387
- className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
388
- aria-label={t('increaseQuantity')}
389
- >
390
- +
391
- </button>
392
- </div>
393
-
394
- <button
395
- type="button"
396
- onClick={handleAddToCart}
397
- disabled={!canPurchase || addingToCart}
398
- className={cn(
399
- 'flex-1 rounded px-6 py-3 text-sm font-medium transition-all',
400
- canPurchase
401
- ? 'bg-primary text-primary-foreground hover:opacity-90'
402
- : 'bg-muted text-muted-foreground cursor-not-allowed'
403
- )}
404
- >
405
- {addingToCart ? (
406
- <span className="inline-flex items-center gap-2">
407
- <LoadingSpinner
408
- size="sm"
409
- className="border-primary-foreground/30 border-t-primary-foreground"
410
- />
411
- {t('addingToCart')}
412
- </span>
413
- ) : addedMessage ? (
414
- t('addedToCart')
415
- ) : !canPurchase ? (
416
- t('outOfStock')
417
- ) : (
418
- t('addToCart')
419
- )}
420
- </button>
421
- </div>
422
-
423
- {/* Download after purchase note */}
424
- {product.isDownloadable && (
425
- <p className="text-muted-foreground text-sm">{t('downloadAfterPurchase')}</p>
426
- )}
427
-
428
- {/* Description */}
429
- {description && (
430
- <div className="border-border border-t pt-4">
431
- <h2 className="text-foreground mb-3 text-lg font-semibold">{t('description')}</h2>
432
- {'html' in description ? (
433
- <div
434
- className="prose prose-sm text-muted-foreground max-w-none"
435
- dangerouslySetInnerHTML={{ __html: description.html }}
436
- />
437
- ) : (
438
- <p className="text-muted-foreground whitespace-pre-wrap">{description.text}</p>
439
- )}
440
- </div>
441
- )}
442
-
443
- {/* Metafields / Specifications */}
444
- {product.metafields && product.metafields.length > 0 && (
445
- <div className="border-border border-t pt-4">
446
- <h2 className="text-foreground mb-3 text-lg font-semibold">{t('specifications')}</h2>
447
- <table className="w-full text-sm">
448
- <tbody>
449
- {product.metafields.map((field) => (
450
- <tr key={field.id} className="border-border border-b last:border-0">
451
- <td className="text-foreground whitespace-nowrap py-2 pe-4 font-medium">
452
- {field.definitionName}
453
- </td>
454
- <td className="text-muted-foreground py-2">
455
- <MetafieldValue field={field} />
456
- </td>
457
- </tr>
458
- ))}
459
- </tbody>
460
- </table>
461
- </div>
462
- )}
463
- </div>
464
- </div>
465
-
466
- {/* Upsells */}
467
- {recommendations?.upsells && recommendations.upsells.length > 0 && (
468
- <RecommendationSection
469
- title={t('upgradeYourChoice')}
470
- items={recommendations.upsells}
471
- className="mt-12"
472
- />
473
- )}
474
-
475
- {/* Related products */}
476
- {recommendations?.related && recommendations.related.length > 0 && (
477
- <RecommendationSection
478
- title={t('similarProducts')}
479
- items={recommendations.related}
480
- className="mt-12"
481
- />
482
- )}
483
- </div>
484
- );
485
- }
1
+ 'use client';
2
+
3
+ import { useEffect, useState, useMemo } from 'react';
4
+ import Image from 'next/image';
5
+ import type {
6
+ Product,
7
+ ProductVariant,
8
+ ProductImage,
9
+ ProductMetafield,
10
+ ProductRecommendationsResponse,
11
+ DownloadFile,
12
+ } from 'brainerce';
13
+
14
+ type ProductWithRecommendations = Product & {
15
+ recommendations?: ProductRecommendationsResponse;
16
+ };
17
+ import { getProductPriceInfo, getDescriptionContent } from 'brainerce';
18
+ import { useCart } from '@/providers/store-provider';
19
+ import { PriceDisplay } from '@/components/shared/price-display';
20
+ import { LoadingSpinner } from '@/components/shared/loading-spinner';
21
+ import { VariantSelector } from '@/components/products/variant-selector';
22
+ import { StockBadge } from '@/components/products/stock-badge';
23
+ import { RecommendationSection } from '@/components/products/recommendation-section';
24
+ import { useTranslations } from '@/lib/translations';
25
+ import { cn } from '@/lib/utils';
26
+
27
+ /** Render a metafield value based on its type */
28
+ function MetafieldValue({ field }: { field: ProductMetafield }) {
29
+ const tc = useTranslations('common');
30
+ switch (field.type) {
31
+ case 'IMAGE': {
32
+ if (!field.value) return <span className="text-muted-foreground">-</span>;
33
+ return (
34
+ <img
35
+ src={field.value}
36
+ alt={field.definitionName}
37
+ className="h-16 w-16 rounded object-cover"
38
+ />
39
+ );
40
+ }
41
+ case 'GALLERY': {
42
+ let urls: string[] = [];
43
+ try {
44
+ const parsed = JSON.parse(field.value);
45
+ urls = Array.isArray(parsed)
46
+ ? parsed.filter((u: unknown) => typeof u === 'string' && u)
47
+ : [];
48
+ } catch {
49
+ urls = field.value ? [field.value] : [];
50
+ }
51
+ if (urls.length === 0) return <span className="text-muted-foreground">-</span>;
52
+ return (
53
+ <div className="flex flex-wrap gap-2">
54
+ {urls.map((url, i) => (
55
+ <img
56
+ key={i}
57
+ src={url}
58
+ alt={`${field.definitionName} ${i + 1}`}
59
+ className="h-16 w-16 rounded object-cover"
60
+ />
61
+ ))}
62
+ </div>
63
+ );
64
+ }
65
+ case 'URL':
66
+ return field.value ? (
67
+ <a
68
+ href={field.value}
69
+ target="_blank"
70
+ rel="noopener noreferrer"
71
+ className="text-primary break-all hover:underline"
72
+ >
73
+ {field.value}
74
+ </a>
75
+ ) : (
76
+ <span className="text-muted-foreground">-</span>
77
+ );
78
+ case 'COLOR':
79
+ return field.value ? (
80
+ <span className="inline-flex items-center gap-2">
81
+ <span
82
+ className="border-border inline-block h-4 w-4 rounded-full border"
83
+ style={{ backgroundColor: field.value }}
84
+ />
85
+ {field.value}
86
+ </span>
87
+ ) : (
88
+ <span className="text-muted-foreground">-</span>
89
+ );
90
+ case 'BOOLEAN':
91
+ return <span>{field.value === 'true' ? tc('yes') : tc('no')}</span>;
92
+ case 'DATE':
93
+ case 'DATETIME': {
94
+ if (!field.value) return <span className="text-muted-foreground">-</span>;
95
+ try {
96
+ const date = new Date(field.value);
97
+ return (
98
+ <span>
99
+ {field.type === 'DATETIME' ? date.toLocaleString() : date.toLocaleDateString()}
100
+ </span>
101
+ );
102
+ } catch {
103
+ return <span>{field.value}</span>;
104
+ }
105
+ }
106
+ default:
107
+ return <span>{field.value || '-'}</span>;
108
+ }
109
+ }
110
+
111
+ interface ProductClientSectionProps {
112
+ product: Product;
113
+ }
114
+
115
+ export function ProductClientSection({ product: initialProduct }: ProductClientSectionProps) {
116
+ const { refreshCart } = useCart();
117
+ const t = useTranslations('productDetail');
118
+
119
+ const product = initialProduct as ProductWithRecommendations;
120
+ const recommendations = product?.recommendations ?? null;
121
+
122
+ const [selectedVariant, setSelectedVariant] = useState<ProductVariant | null>(
123
+ product.variants && product.variants.length > 0 ? product.variants[0] : null
124
+ );
125
+ const [selectedImageIndex, setSelectedImageIndex] = useState(0);
126
+ const [quantity, setQuantity] = useState(1);
127
+ const [addingToCart, setAddingToCart] = useState(false);
128
+ const [addedMessage, setAddedMessage] = useState(false);
129
+
130
+ // Images list - switch main image when variant changes
131
+ const images: ProductImage[] = useMemo(() => {
132
+ return product?.images || [];
133
+ }, [product]);
134
+
135
+ // When variant changes, update selected image to variant image if available
136
+ useEffect(() => {
137
+ if (!selectedVariant?.image || !product) return;
138
+
139
+ const variantImgUrl =
140
+ typeof selectedVariant.image === 'string' ? selectedVariant.image : selectedVariant.image.url;
141
+
142
+ // Find if variant image exists in product images
143
+ const idx = images.findIndex((img) => img.url === variantImgUrl);
144
+ if (idx >= 0) {
145
+ setSelectedImageIndex(idx);
146
+ } else {
147
+ // Variant image not in product images - select index 0 as fallback
148
+ setSelectedImageIndex(-1);
149
+ }
150
+ }, [selectedVariant, images, product]);
151
+
152
+ // Determine which image to show
153
+ const mainImageUrl = useMemo(() => {
154
+ if (selectedImageIndex === -1 && selectedVariant?.image) {
155
+ const img = selectedVariant.image;
156
+ return typeof img === 'string' ? img : img.url;
157
+ }
158
+ return images[selectedImageIndex]?.url || null;
159
+ }, [selectedImageIndex, selectedVariant, images]);
160
+
161
+ // Price info - use variant price if selected, else product price
162
+ const priceInfo = useMemo(() => {
163
+ if (selectedVariant?.price) {
164
+ return {
165
+ price: parseFloat(selectedVariant.salePrice || selectedVariant.price),
166
+ originalPrice: parseFloat(selectedVariant.price),
167
+ isOnSale:
168
+ selectedVariant.salePrice != null &&
169
+ parseFloat(selectedVariant.salePrice) < parseFloat(selectedVariant.price),
170
+ discountPercent:
171
+ selectedVariant.salePrice != null &&
172
+ parseFloat(selectedVariant.salePrice) < parseFloat(selectedVariant.price)
173
+ ? Math.round(
174
+ ((parseFloat(selectedVariant.price) - parseFloat(selectedVariant.salePrice)) /
175
+ parseFloat(selectedVariant.price)) *
176
+ 100
177
+ )
178
+ : 0,
179
+ };
180
+ }
181
+ return getProductPriceInfo(product);
182
+ }, [product, selectedVariant]);
183
+
184
+ // Inventory: use variant inventory if selected, else product inventory
185
+ const inventory = selectedVariant?.inventory ?? product?.inventory ?? null;
186
+ const canPurchase = inventory?.canPurchase !== false;
187
+
188
+ // Description
189
+ const description = useMemo(() => {
190
+ return product ? getDescriptionContent(product) : null;
191
+ }, [product]);
192
+
193
+ async function handleAddToCart() {
194
+ if (!product || addingToCart) return;
195
+
196
+ try {
197
+ setAddingToCart(true);
198
+ const { getClient } = await import('@/lib/brainerce');
199
+ const client = getClient();
200
+ await client.smartAddToCart({
201
+ productId: product.id,
202
+ variantId: selectedVariant?.id,
203
+ quantity,
204
+ name: product.name,
205
+ price: String(priceInfo.price),
206
+ image: mainImageUrl || undefined,
207
+ });
208
+ await refreshCart();
209
+ setAddedMessage(true);
210
+ setTimeout(() => setAddedMessage(false), 2000);
211
+ } catch (err) {
212
+ console.error('Failed to add to cart:', err);
213
+ } finally {
214
+ setAddingToCart(false);
215
+ }
216
+ }
217
+
218
+ return (
219
+ <div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
220
+ <div className="grid grid-cols-1 gap-8 lg:grid-cols-2 lg:gap-12">
221
+ {/* Image Gallery */}
222
+ <div className="space-y-4">
223
+ {/* Main Image */}
224
+ <div className="bg-muted relative aspect-square overflow-hidden rounded-lg">
225
+ {mainImageUrl ? (
226
+ <Image
227
+ src={mainImageUrl}
228
+ alt={product.name}
229
+ fill
230
+ sizes="(max-width: 1024px) 100vw, 50vw"
231
+ className="object-contain"
232
+ priority
233
+ />
234
+ ) : (
235
+ <div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
236
+ <svg className="h-24 w-24" fill="none" viewBox="0 0 24 24" stroke="currentColor">
237
+ <path
238
+ strokeLinecap="round"
239
+ strokeLinejoin="round"
240
+ strokeWidth={1}
241
+ 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"
242
+ />
243
+ </svg>
244
+ </div>
245
+ )}
246
+ </div>
247
+
248
+ {/* Thumbnails */}
249
+ {images.length > 1 && (
250
+ <div className="flex gap-2 overflow-x-auto pb-2">
251
+ {images.map((img, idx) => (
252
+ <button
253
+ key={idx}
254
+ type="button"
255
+ onClick={() => setSelectedImageIndex(idx)}
256
+ className={cn(
257
+ 'relative h-16 w-16 flex-shrink-0 overflow-hidden rounded border-2 transition-colors',
258
+ selectedImageIndex === idx
259
+ ? 'border-primary'
260
+ : 'border-border hover:border-muted-foreground'
261
+ )}
262
+ >
263
+ <Image
264
+ src={img.url}
265
+ alt={img.alt || `${product.name} ${idx + 1}`}
266
+ fill
267
+ sizes="64px"
268
+ className="object-cover"
269
+ />
270
+ </button>
271
+ ))}
272
+ </div>
273
+ )}
274
+ </div>
275
+
276
+ {/* Product Info */}
277
+ <div className="space-y-6">
278
+ {/* Categories */}
279
+ {product.categories && product.categories.length > 0 && (
280
+ <div className="flex flex-wrap gap-2">
281
+ {product.categories.map((cat) => (
282
+ <span
283
+ key={cat.id}
284
+ className="text-muted-foreground bg-muted rounded px-2 py-1 text-xs"
285
+ >
286
+ {cat.name}
287
+ </span>
288
+ ))}
289
+ </div>
290
+ )}
291
+
292
+ {/* Title */}
293
+ <h1 className="text-foreground text-2xl font-bold sm:text-3xl">{product.name}</h1>
294
+
295
+ {/* Price */}
296
+ <PriceDisplay
297
+ price={priceInfo.originalPrice}
298
+ salePrice={priceInfo.isOnSale ? priceInfo.price : undefined}
299
+ size="lg"
300
+ />
301
+
302
+ {/* Stock / Digital badge */}
303
+ {product.isDownloadable ? (
304
+ <span className="inline-flex items-center gap-1.5 rounded-full bg-green-100 px-3 py-1 text-xs font-medium text-green-800 dark:bg-green-950/30 dark:text-green-400">
305
+ <svg className="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
306
+ <path
307
+ strokeLinecap="round"
308
+ strokeLinejoin="round"
309
+ strokeWidth={2}
310
+ d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"
311
+ />
312
+ </svg>
313
+ {t('instantDownload')}
314
+ </span>
315
+ ) : (
316
+ <StockBadge inventory={inventory} lowStockThreshold={5} />
317
+ )}
318
+
319
+ {/* Downloadable files info */}
320
+ {product.isDownloadable && product.downloads && product.downloads.length > 0 && (
321
+ <div className="bg-muted/50 rounded-lg border p-4">
322
+ <p className="text-foreground mb-2 text-sm font-medium">
323
+ {t('filesIncluded', { count: product.downloads.length })}
324
+ </p>
325
+ <ul className="space-y-1.5">
326
+ {product.downloads.map((file: DownloadFile) => (
327
+ <li
328
+ key={file.id}
329
+ className="text-muted-foreground flex items-center gap-2 text-sm"
330
+ >
331
+ <svg
332
+ className="h-4 w-4 flex-shrink-0"
333
+ fill="none"
334
+ viewBox="0 0 24 24"
335
+ stroke="currentColor"
336
+ >
337
+ <path
338
+ strokeLinecap="round"
339
+ strokeLinejoin="round"
340
+ strokeWidth={1.5}
341
+ d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"
342
+ />
343
+ </svg>
344
+ <span className="truncate">{file.name}</span>
345
+ {file.size && (
346
+ <span className="flex-shrink-0 text-xs">
347
+ (
348
+ {file.size < 1024 * 1024
349
+ ? `${(file.size / 1024).toFixed(0)} KB`
350
+ : `${(file.size / (1024 * 1024)).toFixed(1)} MB`}
351
+ )
352
+ </span>
353
+ )}
354
+ </li>
355
+ ))}
356
+ </ul>
357
+ </div>
358
+ )}
359
+
360
+ {/* Variant Selector */}
361
+ {product.type === 'VARIABLE' && product.variants && product.variants.length > 0 && (
362
+ <VariantSelector
363
+ product={product}
364
+ selectedVariant={selectedVariant}
365
+ onVariantChange={setSelectedVariant}
366
+ />
367
+ )}
368
+
369
+ {/* Quantity + Add to Cart */}
370
+ <div className="flex items-center gap-4">
371
+ <div className="border-border flex items-center rounded border">
372
+ <button
373
+ type="button"
374
+ onClick={() => setQuantity((q) => Math.max(1, q - 1))}
375
+ className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
376
+ aria-label={t('decreaseQuantity')}
377
+ >
378
+ -
379
+ </button>
380
+ <span className="text-foreground min-w-[3rem] px-4 py-2 text-center text-sm font-medium">
381
+ {quantity}
382
+ </span>
383
+ <button
384
+ type="button"
385
+ onClick={() => setQuantity((q) => q + 1)}
386
+ className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
387
+ aria-label={t('increaseQuantity')}
388
+ >
389
+ +
390
+ </button>
391
+ </div>
392
+
393
+ <button
394
+ type="button"
395
+ onClick={handleAddToCart}
396
+ disabled={!canPurchase || addingToCart}
397
+ className={cn(
398
+ 'flex-1 rounded px-6 py-3 text-sm font-medium transition-all',
399
+ canPurchase
400
+ ? 'bg-primary text-primary-foreground hover:opacity-90'
401
+ : 'bg-muted text-muted-foreground cursor-not-allowed'
402
+ )}
403
+ >
404
+ {addingToCart ? (
405
+ <span className="inline-flex items-center gap-2">
406
+ <LoadingSpinner
407
+ size="sm"
408
+ className="border-primary-foreground/30 border-t-primary-foreground"
409
+ />
410
+ {t('addingToCart')}
411
+ </span>
412
+ ) : addedMessage ? (
413
+ t('addedToCart')
414
+ ) : !canPurchase ? (
415
+ t('outOfStock')
416
+ ) : (
417
+ t('addToCart')
418
+ )}
419
+ </button>
420
+ </div>
421
+
422
+ {/* Download after purchase note */}
423
+ {product.isDownloadable && (
424
+ <p className="text-muted-foreground text-sm">{t('downloadAfterPurchase')}</p>
425
+ )}
426
+
427
+ {/* Description */}
428
+ {description && (
429
+ <div className="border-border border-t pt-4">
430
+ <h2 className="text-foreground mb-3 text-lg font-semibold">{t('description')}</h2>
431
+ {'html' in description ? (
432
+ <div
433
+ className="prose prose-sm text-muted-foreground max-w-none"
434
+ dangerouslySetInnerHTML={{ __html: description.html }}
435
+ />
436
+ ) : (
437
+ <p className="text-muted-foreground whitespace-pre-wrap">{description.text}</p>
438
+ )}
439
+ </div>
440
+ )}
441
+
442
+ {/* Metafields / Specifications */}
443
+ {product.metafields && product.metafields.length > 0 && (
444
+ <div className="border-border border-t pt-4">
445
+ <h2 className="text-foreground mb-3 text-lg font-semibold">{t('specifications')}</h2>
446
+ <table className="w-full text-sm">
447
+ <tbody>
448
+ {product.metafields.map((field) => (
449
+ <tr key={field.id} className="border-border border-b last:border-0">
450
+ <td className="text-foreground whitespace-nowrap py-2 pe-4 font-medium">
451
+ {field.definitionName}
452
+ </td>
453
+ <td className="text-muted-foreground py-2">
454
+ <MetafieldValue field={field} />
455
+ </td>
456
+ </tr>
457
+ ))}
458
+ </tbody>
459
+ </table>
460
+ </div>
461
+ )}
462
+ </div>
463
+ </div>
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
+ }
@@ -1,14 +1,14 @@
1
- import type { MetadataRoute } from 'next';
2
-
3
- export default function robots(): MetadataRoute.Robots {
4
- const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'https://example.com';
5
-
6
- return {
7
- rules: {
8
- userAgent: '*',
9
- allow: '/',
10
- disallow: ['/api/', '/auth/', '/checkout/', '/account/'],
11
- },
12
- sitemap: `${baseUrl}/sitemap.xml`,
13
- };
14
- }
1
+ import type { MetadataRoute } from 'next';
2
+
3
+ export default function robots(): MetadataRoute.Robots {
4
+ const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'https://example.com';
5
+
6
+ return {
7
+ rules: {
8
+ userAgent: '*',
9
+ allow: '/',
10
+ disallow: ['/api/', '/auth/', '/checkout/', '/account/'],
11
+ },
12
+ sitemap: `${baseUrl}/sitemap.xml`,
13
+ };
14
+ }
@@ -1,25 +1,25 @@
1
- import type { MetadataRoute } from 'next';
2
- import { getServerClient } from '@/lib/brainerce';
3
-
4
- export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
5
- const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'https://example.com';
6
-
7
- const staticPages: MetadataRoute.Sitemap = [
8
- { url: baseUrl, lastModified: new Date(), priority: 1 },
9
- { url: `${baseUrl}/products`, lastModified: new Date(), priority: 0.9 },
10
- ];
11
-
12
- try {
13
- const client = getServerClient();
14
- const { data: products } = await client.getProducts({ limit: 1000 });
15
- const productPages: MetadataRoute.Sitemap = products.map((product) => ({
16
- url: `${baseUrl}/products/${product.slug}`,
17
- lastModified: product.updatedAt ? new Date(product.updatedAt) : new Date(),
18
- priority: 0.8,
19
- }));
20
-
21
- return [...staticPages, ...productPages];
22
- } catch {
23
- return staticPages;
24
- }
25
- }
1
+ import type { MetadataRoute } from 'next';
2
+ import { getServerClient } from '@/lib/brainerce';
3
+
4
+ export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
5
+ const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'https://example.com';
6
+
7
+ const staticPages: MetadataRoute.Sitemap = [
8
+ { url: baseUrl, lastModified: new Date(), priority: 1 },
9
+ { url: `${baseUrl}/products`, lastModified: new Date(), priority: 0.9 },
10
+ ];
11
+
12
+ try {
13
+ const client = getServerClient();
14
+ const { data: products } = await client.getProducts({ limit: 1000 });
15
+ const productPages: MetadataRoute.Sitemap = products.map((product) => ({
16
+ url: `${baseUrl}/products/${product.slug}`,
17
+ lastModified: product.updatedAt ? new Date(product.updatedAt) : new Date(),
18
+ priority: 0.8,
19
+ }));
20
+
21
+ return [...staticPages, ...productPages];
22
+ } catch {
23
+ return staticPages;
24
+ }
25
+ }
@@ -1,39 +1,39 @@
1
- import type { Product } from 'brainerce';
2
- import { getProductPriceInfo } from 'brainerce';
3
-
4
- interface ProductJsonLdProps {
5
- product: Product;
6
- url: string;
7
- }
8
-
9
- export function ProductJsonLd({ product, url }: ProductJsonLdProps) {
10
- const priceInfo = getProductPriceInfo(product);
11
- const imageUrl = product.images?.[0]?.url;
12
-
13
- const jsonLd = {
14
- '@context': 'https://schema.org',
15
- '@type': 'Product',
16
- name: product.name,
17
- description: product.description || product.name,
18
- image: imageUrl,
19
- url,
20
- sku: product.sku || product.id,
21
- offers: {
22
- '@type': 'Offer',
23
- price: priceInfo.price,
24
- priceCurrency: 'ILS',
25
- availability:
26
- product.inventory?.canPurchase !== false
27
- ? 'https://schema.org/InStock'
28
- : 'https://schema.org/OutOfStock',
29
- url,
30
- },
31
- };
32
-
33
- return (
34
- <script
35
- type="application/ld+json"
36
- dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
37
- />
38
- );
39
- }
1
+ import type { Product } from 'brainerce';
2
+ import { getProductPriceInfo } from 'brainerce';
3
+
4
+ interface ProductJsonLdProps {
5
+ product: Product;
6
+ url: string;
7
+ }
8
+
9
+ export function ProductJsonLd({ product, url }: ProductJsonLdProps) {
10
+ const priceInfo = getProductPriceInfo(product);
11
+ const imageUrl = product.images?.[0]?.url;
12
+
13
+ const jsonLd = {
14
+ '@context': 'https://schema.org',
15
+ '@type': 'Product',
16
+ name: product.name,
17
+ description: product.description || product.name,
18
+ image: imageUrl,
19
+ url,
20
+ sku: product.sku || product.id,
21
+ offers: {
22
+ '@type': 'Offer',
23
+ price: priceInfo.price,
24
+ priceCurrency: 'ILS',
25
+ availability:
26
+ product.inventory?.canPurchase !== false
27
+ ? 'https://schema.org/InStock'
28
+ : 'https://schema.org/OutOfStock',
29
+ url,
30
+ },
31
+ };
32
+
33
+ return (
34
+ <script
35
+ type="application/ld+json"
36
+ dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
37
+ />
38
+ );
39
+ }
@@ -45,5 +45,6 @@ export function getServerClient(): BrainerceClient {
45
45
  return new BrainerceClient({
46
46
  connectionId: CONNECTION_ID,
47
47
  baseUrl: apiUrl,
48
+ origin: process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000',
48
49
  });
49
50
  }