create-brainerce-store 1.12.4 → 1.13.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.12.4",
34
+ version: "1.13.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.12.4",
3
+ "version": "1.13.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"
@@ -1,5 +1,8 @@
1
1
  # Brainerce Connection
2
2
  NEXT_PUBLIC_BRAINERCE_CONNECTION_ID=<%= connectionId %>
3
3
 
4
- # Backend API URL (server-side only — used by BFF proxy, never exposed to browser)
4
+ # Backend API URL (server-side only — used by BFF proxy and SSR, never exposed to browser)
5
5
  BRAINERCE_API_URL=<%= apiBaseUrl %>
6
+
7
+ # Public site URL — used for sitemap, robots.txt, and SEO metadata
8
+ NEXT_PUBLIC_SITE_URL=http://localhost:3000
@@ -1,527 +1,63 @@
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 {
8
- Product,
9
- ProductVariant,
10
- ProductImage,
11
- ProductMetafield,
12
- ProductRecommendationsResponse,
13
- DownloadFile,
14
- } from 'brainerce';
15
-
16
- type ProductWithRecommendations = Product & {
17
- recommendations?: ProductRecommendationsResponse;
1
+ import type { Metadata } from 'next';
2
+ import { notFound } from 'next/navigation';
3
+ import { getServerClient } from '@/lib/brainerce';
4
+ import { ProductJsonLd } from '@/components/seo/product-json-ld';
5
+ import { ProductClientSection } from './product-client-section';
6
+
7
+ type Props = {
8
+ params: Promise<{ slug: string }>;
18
9
  };
19
- import { getProductPriceInfo, getDescriptionContent } from 'brainerce';
20
- import { getClient } from '@/lib/brainerce';
21
- import { useCart } from '@/providers/store-provider';
22
- import { PriceDisplay } from '@/components/shared/price-display';
23
- import { LoadingSpinner } from '@/components/shared/loading-spinner';
24
- import { VariantSelector } from '@/components/products/variant-selector';
25
- import { StockBadge } from '@/components/products/stock-badge';
26
- import { RecommendationSection } from '@/components/products/recommendation-section';
27
- import { useTranslations } from '@/lib/translations';
28
- import { cn } from '@/lib/utils';
29
10
 
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 className="text-muted-foreground">-</span>;
36
- return (
37
- <img
38
- src={field.value}
39
- alt={field.definitionName}
40
- className="h-16 w-16 rounded 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 className="text-muted-foreground">-</span>;
55
- return (
56
- <div 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 object-cover"
63
- />
64
- ))}
65
- </div>
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 break-all hover:underline"
75
- >
76
- {field.value}
77
- </a>
78
- ) : (
79
- <span className="text-muted-foreground">-</span>
80
- );
81
- case 'COLOR':
82
- return field.value ? (
83
- <span className="inline-flex items-center gap-2">
84
- <span
85
- className="border-border inline-block h-4 w-4 rounded-full border"
86
- style={{ backgroundColor: field.value }}
87
- />
88
- {field.value}
89
- </span>
90
- ) : (
91
- <span className="text-muted-foreground">-</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 className="text-muted-foreground">-</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>;
11
+ export async function generateMetadata({ params }: Props): Promise<Metadata> {
12
+ const { slug } = await params;
13
+
14
+ try {
15
+ const client = getServerClient();
16
+ const product = await client.getProductBySlug(slug);
17
+ const imageUrl = product.images?.[0]?.url;
18
+ const description = product.description?.substring(0, 160) || product.name;
19
+
20
+ return {
21
+ title: product.name,
22
+ description,
23
+ openGraph: {
24
+ title: product.name,
25
+ description,
26
+ images: imageUrl ? [{ url: imageUrl, alt: product.name }] : [],
27
+ type: 'website',
28
+ },
29
+ twitter: {
30
+ card: 'summary_large_image',
31
+ title: product.name,
32
+ description,
33
+ images: imageUrl ? [imageUrl] : [],
34
+ },
35
+ };
36
+ } catch {
37
+ return {
38
+ title: 'Product not found',
39
+ };
111
40
  }
112
41
  }
113
42
 
114
- export default function ProductDetailPage() {
115
- const params = useParams();
116
- const slug = params.slug as string;
117
- const { refreshCart } = useCart();
118
- const t = useTranslations('productDetail');
119
- const tc = useTranslations('common');
120
- const [product, setProduct] = useState<ProductWithRecommendations | null>(null);
121
- const [loading, setLoading] = useState(true);
122
- const [error, setError] = useState<string | null>(null);
123
- const [selectedVariant, setSelectedVariant] = useState<ProductVariant | null>(null);
124
- const [selectedImageIndex, setSelectedImageIndex] = useState(0);
125
- const [quantity, setQuantity] = useState(1);
126
- const [addingToCart, setAddingToCart] = useState(false);
127
- const [addedMessage, setAddedMessage] = useState(false);
128
-
129
- const recommendations = product?.recommendations ?? null;
130
-
131
- // Load product
132
- useEffect(() => {
133
- async function load() {
134
- try {
135
- setLoading(true);
136
- setError(null);
137
- const client = getClient();
138
- const p = await client.getProductBySlug(slug);
139
- setProduct(p as ProductWithRecommendations);
140
-
141
- // Auto-select first variant
142
- if (p.variants && p.variants.length > 0) {
143
- setSelectedVariant(p.variants[0]);
144
- }
145
- } catch {
146
- setError(t('notFound'));
147
- } finally {
148
- setLoading(false);
149
- }
150
- }
151
- load();
152
- }, [slug]);
153
-
154
- // Images list - switch main image when variant changes
155
- const images: ProductImage[] = useMemo(() => {
156
- return product?.images || [];
157
- }, [product]);
158
-
159
- // When variant changes, update selected image to variant image if available
160
- useEffect(() => {
161
- if (!selectedVariant?.image || !product) return;
162
-
163
- const variantImgUrl =
164
- typeof selectedVariant.image === 'string' ? selectedVariant.image : selectedVariant.image.url;
43
+ export default async function ProductDetailPage({ params }: Props) {
44
+ const { slug } = await params;
165
45
 
166
- // Find if variant image exists in product images
167
- const idx = images.findIndex((img) => img.url === variantImgUrl);
168
- if (idx >= 0) {
169
- setSelectedImageIndex(idx);
170
- } else {
171
- // Variant image not in product images - select index 0 as fallback
172
- // (The variant image will be shown as the main image via override)
173
- setSelectedImageIndex(-1);
174
- }
175
- }, [selectedVariant, images, product]);
176
-
177
- // Determine which image to show
178
- const mainImageUrl = useMemo(() => {
179
- if (selectedImageIndex === -1 && selectedVariant?.image) {
180
- const img = selectedVariant.image;
181
- return typeof img === 'string' ? img : img.url;
182
- }
183
- return images[selectedImageIndex]?.url || null;
184
- }, [selectedImageIndex, selectedVariant, images]);
185
-
186
- // Price info - use variant price if selected, else product price
187
- const priceInfo = useMemo(() => {
188
- if (selectedVariant?.price) {
189
- return {
190
- price: parseFloat(selectedVariant.salePrice || selectedVariant.price),
191
- originalPrice: parseFloat(selectedVariant.price),
192
- isOnSale:
193
- selectedVariant.salePrice != null &&
194
- parseFloat(selectedVariant.salePrice) < parseFloat(selectedVariant.price),
195
- discountPercent:
196
- selectedVariant.salePrice != null &&
197
- parseFloat(selectedVariant.salePrice) < parseFloat(selectedVariant.price)
198
- ? Math.round(
199
- ((parseFloat(selectedVariant.price) - parseFloat(selectedVariant.salePrice)) /
200
- parseFloat(selectedVariant.price)) *
201
- 100
202
- )
203
- : 0,
204
- };
205
- }
206
- return getProductPriceInfo(product);
207
- }, [product, selectedVariant]);
208
-
209
- // Inventory: use variant inventory if selected, else product inventory
210
- const inventory = selectedVariant?.inventory ?? product?.inventory ?? null;
211
- const canPurchase = inventory?.canPurchase !== false;
212
-
213
- // Description
214
- const description = useMemo(() => {
215
- return product ? getDescriptionContent(product) : null;
216
- }, [product]);
217
-
218
- async function handleAddToCart() {
219
- if (!product || addingToCart) return;
220
-
221
- try {
222
- setAddingToCart(true);
223
- const client = getClient();
224
- await client.smartAddToCart({
225
- productId: product.id,
226
- variantId: selectedVariant?.id,
227
- quantity,
228
- name: product.name,
229
- price: String(priceInfo.price),
230
- image: mainImageUrl || undefined,
231
- });
232
- await refreshCart();
233
- setAddedMessage(true);
234
- setTimeout(() => setAddedMessage(false), 2000);
235
- } catch (err) {
236
- console.error('Failed to add to cart:', err);
237
- } finally {
238
- setAddingToCart(false);
239
- }
46
+ let product;
47
+ try {
48
+ const client = getServerClient();
49
+ product = await client.getProductBySlug(slug);
50
+ } catch {
51
+ notFound();
240
52
  }
241
53
 
242
- if (loading) {
243
- return (
244
- <div className="flex min-h-[60vh] items-center justify-center">
245
- <LoadingSpinner size="lg" />
246
- </div>
247
- );
248
- }
249
-
250
- if (error || !product) {
251
- return (
252
- <div className="mx-auto max-w-7xl px-4 py-16 text-center sm:px-6 lg:px-8">
253
- <h1 className="text-foreground text-2xl font-bold">{error || t('notFound')}</h1>
254
- <Link href="/products" className="text-primary mt-4 inline-block hover:underline">
255
- {t('backToProducts')}
256
- </Link>
257
- </div>
258
- );
259
- }
54
+ const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || '';
55
+ const productUrl = `${baseUrl}/products/${slug}`;
260
56
 
261
57
  return (
262
- <div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
263
- <div className="grid grid-cols-1 gap-8 lg:grid-cols-2 lg:gap-12">
264
- {/* Image Gallery */}
265
- <div className="space-y-4">
266
- {/* Main Image */}
267
- <div className="bg-muted relative aspect-square overflow-hidden rounded-lg">
268
- {mainImageUrl ? (
269
- <Image
270
- src={mainImageUrl}
271
- alt={product.name}
272
- fill
273
- sizes="(max-width: 1024px) 100vw, 50vw"
274
- className="object-contain"
275
- priority
276
- />
277
- ) : (
278
- <div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
279
- <svg className="h-24 w-24" fill="none" viewBox="0 0 24 24" stroke="currentColor">
280
- <path
281
- strokeLinecap="round"
282
- strokeLinejoin="round"
283
- strokeWidth={1}
284
- 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"
285
- />
286
- </svg>
287
- </div>
288
- )}
289
- </div>
290
-
291
- {/* Thumbnails */}
292
- {images.length > 1 && (
293
- <div className="flex gap-2 overflow-x-auto pb-2">
294
- {images.map((img, idx) => (
295
- <button
296
- key={idx}
297
- type="button"
298
- onClick={() => setSelectedImageIndex(idx)}
299
- className={cn(
300
- 'relative h-16 w-16 flex-shrink-0 overflow-hidden rounded border-2 transition-colors',
301
- selectedImageIndex === idx
302
- ? 'border-primary'
303
- : 'border-border hover:border-muted-foreground'
304
- )}
305
- >
306
- <Image
307
- src={img.url}
308
- alt={img.alt || `${product.name} ${idx + 1}`}
309
- fill
310
- sizes="64px"
311
- className="object-cover"
312
- />
313
- </button>
314
- ))}
315
- </div>
316
- )}
317
- </div>
318
-
319
- {/* Product Info */}
320
- <div className="space-y-6">
321
- {/* Categories */}
322
- {product.categories && product.categories.length > 0 && (
323
- <div className="flex flex-wrap gap-2">
324
- {product.categories.map((cat) => (
325
- <span
326
- key={cat.id}
327
- className="text-muted-foreground bg-muted rounded px-2 py-1 text-xs"
328
- >
329
- {cat.name}
330
- </span>
331
- ))}
332
- </div>
333
- )}
334
-
335
- {/* Title */}
336
- <h1 className="text-foreground text-2xl font-bold sm:text-3xl">{product.name}</h1>
337
-
338
- {/* Price */}
339
- <PriceDisplay
340
- price={priceInfo.originalPrice}
341
- salePrice={priceInfo.isOnSale ? priceInfo.price : undefined}
342
- size="lg"
343
- />
344
-
345
- {/* Stock / Digital badge */}
346
- {product.isDownloadable ? (
347
- <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">
348
- <svg className="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
349
- <path
350
- strokeLinecap="round"
351
- strokeLinejoin="round"
352
- strokeWidth={2}
353
- d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"
354
- />
355
- </svg>
356
- {t('instantDownload')}
357
- </span>
358
- ) : (
359
- <StockBadge inventory={inventory} lowStockThreshold={5} />
360
- )}
361
-
362
- {/* Downloadable files info */}
363
- {product.isDownloadable && product.downloads && product.downloads.length > 0 && (
364
- <div className="bg-muted/50 rounded-lg border p-4">
365
- <p className="text-foreground mb-2 text-sm font-medium">
366
- {t('filesIncluded', { count: product.downloads.length })}
367
- </p>
368
- <ul className="space-y-1.5">
369
- {product.downloads.map((file: DownloadFile) => (
370
- <li
371
- key={file.id}
372
- className="text-muted-foreground flex items-center gap-2 text-sm"
373
- >
374
- <svg
375
- className="h-4 w-4 flex-shrink-0"
376
- fill="none"
377
- viewBox="0 0 24 24"
378
- stroke="currentColor"
379
- >
380
- <path
381
- strokeLinecap="round"
382
- strokeLinejoin="round"
383
- strokeWidth={1.5}
384
- 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"
385
- />
386
- </svg>
387
- <span className="truncate">{file.name}</span>
388
- {file.size && (
389
- <span className="flex-shrink-0 text-xs">
390
- (
391
- {file.size < 1024 * 1024
392
- ? `${(file.size / 1024).toFixed(0)} KB`
393
- : `${(file.size / (1024 * 1024)).toFixed(1)} MB`}
394
- )
395
- </span>
396
- )}
397
- </li>
398
- ))}
399
- </ul>
400
- </div>
401
- )}
402
-
403
- {/* Variant Selector */}
404
- {product.type === 'VARIABLE' && product.variants && product.variants.length > 0 && (
405
- <VariantSelector
406
- product={product}
407
- selectedVariant={selectedVariant}
408
- onVariantChange={setSelectedVariant}
409
- />
410
- )}
411
-
412
- {/* Quantity + Add to Cart */}
413
- <div className="flex items-center gap-4">
414
- <div className="border-border flex items-center rounded border">
415
- <button
416
- type="button"
417
- onClick={() => setQuantity((q) => Math.max(1, q - 1))}
418
- className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
419
- aria-label={t('decreaseQuantity')}
420
- >
421
- -
422
- </button>
423
- <span className="text-foreground min-w-[3rem] px-4 py-2 text-center text-sm font-medium">
424
- {quantity}
425
- </span>
426
- <button
427
- type="button"
428
- onClick={() => setQuantity((q) => q + 1)}
429
- className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
430
- aria-label={t('increaseQuantity')}
431
- >
432
- +
433
- </button>
434
- </div>
435
-
436
- <button
437
- type="button"
438
- onClick={handleAddToCart}
439
- disabled={!canPurchase || addingToCart}
440
- className={cn(
441
- 'flex-1 rounded px-6 py-3 text-sm font-medium transition-all',
442
- canPurchase
443
- ? 'bg-primary text-primary-foreground hover:opacity-90'
444
- : 'bg-muted text-muted-foreground cursor-not-allowed'
445
- )}
446
- >
447
- {addingToCart ? (
448
- <span className="inline-flex items-center gap-2">
449
- <LoadingSpinner
450
- size="sm"
451
- className="border-primary-foreground/30 border-t-primary-foreground"
452
- />
453
- {t('addingToCart')}
454
- </span>
455
- ) : addedMessage ? (
456
- t('addedToCart')
457
- ) : !canPurchase ? (
458
- t('outOfStock')
459
- ) : (
460
- t('addToCart')
461
- )}
462
- </button>
463
- </div>
464
-
465
- {/* Download after purchase note */}
466
- {product.isDownloadable && (
467
- <p className="text-muted-foreground text-sm">{t('downloadAfterPurchase')}</p>
468
- )}
469
-
470
- {/* Description */}
471
- {description && (
472
- <div className="border-border border-t pt-4">
473
- <h2 className="text-foreground mb-3 text-lg font-semibold">{t('description')}</h2>
474
- {'html' in description ? (
475
- <div
476
- className="prose prose-sm text-muted-foreground max-w-none"
477
- dangerouslySetInnerHTML={{ __html: description.html }}
478
- />
479
- ) : (
480
- <p className="text-muted-foreground whitespace-pre-wrap">{description.text}</p>
481
- )}
482
- </div>
483
- )}
484
-
485
- {/* Metafields / Specifications */}
486
- {product.metafields && product.metafields.length > 0 && (
487
- <div className="border-border border-t pt-4">
488
- <h2 className="text-foreground mb-3 text-lg font-semibold">{t('specifications')}</h2>
489
- <table className="w-full text-sm">
490
- <tbody>
491
- {product.metafields.map((field) => (
492
- <tr key={field.id} className="border-border border-b last:border-0">
493
- <td className="text-foreground whitespace-nowrap py-2 pe-4 font-medium">
494
- {field.definitionName}
495
- </td>
496
- <td className="text-muted-foreground py-2">
497
- <MetafieldValue field={field} />
498
- </td>
499
- </tr>
500
- ))}
501
- </tbody>
502
- </table>
503
- </div>
504
- )}
505
- </div>
506
- </div>
507
-
508
- {/* Upsells — premium alternatives (product page) */}
509
- {recommendations?.upsells && recommendations.upsells.length > 0 && (
510
- <RecommendationSection
511
- title={t('upgradeYourChoice')}
512
- items={recommendations.upsells}
513
- className="mt-12"
514
- />
515
- )}
516
-
517
- {/* Related products — similar items (bottom of product page) */}
518
- {recommendations?.related && recommendations.related.length > 0 && (
519
- <RecommendationSection
520
- title={t('similarProducts')}
521
- items={recommendations.related}
522
- className="mt-12"
523
- />
524
- )}
525
- </div>
58
+ <>
59
+ <ProductJsonLd product={product} url={productUrl} />
60
+ <ProductClientSection product={product} />
61
+ </>
526
62
  );
527
63
  }
@@ -0,0 +1,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
+ 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
+ }
@@ -0,0 +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
+ }
@@ -0,0 +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
+ }
@@ -0,0 +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
+ }
@@ -37,3 +37,13 @@ export function setStoredCartId(cartId: string | null): void {
37
37
  export function initClient(): BrainerceClient {
38
38
  return getClient();
39
39
  }
40
+
41
+ // Server-side client — calls backend directly (no proxy needed for public data)
42
+ // Used by Server Components for SSR data fetching (generateMetadata, page rendering)
43
+ export function getServerClient(): BrainerceClient {
44
+ const apiUrl = process.env.BRAINERCE_API_URL || 'https://api.brainerce.com';
45
+ return new BrainerceClient({
46
+ connectionId: CONNECTION_ID,
47
+ baseUrl: apiUrl,
48
+ });
49
+ }