create-brainerce-store 1.15.0 → 1.15.2

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.15.0",
34
+ version: "1.15.2",
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/messages/en.json CHANGED
@@ -76,6 +76,10 @@
76
76
  "addingToCart": "Adding...",
77
77
  "addedToCart": "Added to Cart!",
78
78
  "outOfStock": "Out of Stock",
79
+ "inStock": "In Stock",
80
+ "unavailable": "Unavailable",
81
+ "onlyLeft": "Only {available} left",
82
+ "availableInStock": "{available} in stock",
79
83
  "decreaseQuantity": "Decrease quantity",
80
84
  "increaseQuantity": "Increase quantity",
81
85
  "youMayAlsoLike": "You May Also Like",
package/messages/he.json CHANGED
@@ -76,6 +76,10 @@
76
76
  "addingToCart": "...מוסיף",
77
77
  "addedToCart": "נוסף לעגלה!",
78
78
  "outOfStock": "אזל מהמלאי",
79
+ "inStock": "במלאי",
80
+ "unavailable": "לא זמין",
81
+ "onlyLeft": "נותרו {available} בלבד",
82
+ "availableInStock": "{available} במלאי",
79
83
  "decreaseQuantity": "הפחת כמות",
80
84
  "increaseQuantity": "הגדל כמות",
81
85
  "youMayAlsoLike": "אולי גם תאהבו",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-brainerce-store",
3
- "version": "1.15.0",
3
+ "version": "1.15.2",
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,6 +1,6 @@
1
1
  'use client';
2
2
 
3
- import { Suspense, useEffect, useState, useCallback } from 'react';
3
+ import { Suspense, useEffect, useState, useCallback, useRef } from 'react';
4
4
  import { useSearchParams } from 'next/navigation';
5
5
  import Image from 'next/image';
6
6
  import Link from 'next/link';
@@ -76,53 +76,62 @@ function CheckoutContent() {
76
76
  .catch(() => {});
77
77
  }, [isLoggedIn]);
78
78
 
79
- // Initialize or resume checkout
80
- const initCheckout = useCallback(async () => {
81
- try {
82
- setInitializing(true);
83
- setError(null);
84
- const client = getClient();
85
-
86
- // Fetch shipping destinations and pickup locations in parallel
87
- client
88
- .getShippingDestinations()
89
- .then(setDestinations)
90
- .catch(() => {});
79
+ // Initialize or resume checkout (only once)
80
+ const checkoutInitRef = useRef(false);
81
+ const cartIdRef = useRef<string | null>(null);
91
82
 
92
- const locations = await client.getPickupLocations().catch(() => [] as PickupLocation[]);
93
- setPickupLocations(locations);
83
+ useEffect(() => {
84
+ // Only init once, or if cart ID actually changed (e.g. cart was replaced)
85
+ if (!cart?.id) return;
86
+ if (checkoutInitRef.current && cartIdRef.current === cart.id) return;
87
+ checkoutInitRef.current = true;
88
+ cartIdRef.current = cart.id;
94
89
 
95
- // If returning with existing checkout ID, resume it
96
- if (existingCheckoutId) {
97
- const existing = await client.getCheckout(existingCheckoutId);
98
- setCheckout(existing);
90
+ const initCheckout = async () => {
91
+ try {
92
+ setInitializing(true);
93
+ setError(null);
94
+ const client = getClient();
99
95
 
100
- // Determine step based on checkout state
101
- const allDigital = existing.lineItems.every(
102
- (i) => (i.product as unknown as { isDownloadable?: boolean }).isDownloadable
103
- );
104
- setIsAllDigital(allDigital);
105
- if (allDigital) {
106
- // Digital products: show contact info step if email not set, else payment
107
- setStep(existing.email ? 'payment' : 'address');
108
- } else if (existing.deliveryType === 'pickup' && existing.pickupLocation) {
109
- setDeliveryType('pickup');
110
- setStep('payment');
111
- } else if (existing.shippingAddress && existing.shippingRateId) {
112
- setStep('payment');
113
- } else if (existing.shippingAddress) {
114
- // Fetch shipping rates
115
- const rates = await client.getShippingRates(existing.id);
116
- setShippingRates(rates);
117
- setStep('shipping');
118
- } else if (locations.length > 0) {
119
- setStep('method');
96
+ // Fetch shipping destinations and pickup locations in parallel
97
+ client
98
+ .getShippingDestinations()
99
+ .then(setDestinations)
100
+ .catch(() => {});
101
+
102
+ const locations = await client.getPickupLocations().catch(() => [] as PickupLocation[]);
103
+ setPickupLocations(locations);
104
+
105
+ // If returning with existing checkout ID, resume it
106
+ if (existingCheckoutId) {
107
+ const existing = await client.getCheckout(existingCheckoutId);
108
+ setCheckout(existing);
109
+
110
+ // Determine step based on checkout state
111
+ const allDigital = existing.lineItems.every(
112
+ (i) => (i.product as unknown as { isDownloadable?: boolean }).isDownloadable
113
+ );
114
+ setIsAllDigital(allDigital);
115
+ if (allDigital) {
116
+ // Digital products: show contact info step if email not set, else payment
117
+ setStep(existing.email ? 'payment' : 'address');
118
+ } else if (existing.deliveryType === 'pickup' && existing.pickupLocation) {
119
+ setDeliveryType('pickup');
120
+ setStep('payment');
121
+ } else if (existing.shippingAddress && existing.shippingRateId) {
122
+ setStep('payment');
123
+ } else if (existing.shippingAddress) {
124
+ // Fetch shipping rates
125
+ const rates = await client.getShippingRates(existing.id);
126
+ setShippingRates(rates);
127
+ setStep('shipping');
128
+ } else if (locations.length > 0) {
129
+ setStep('method');
130
+ }
131
+ return;
120
132
  }
121
- return;
122
- }
123
133
 
124
- // Create new checkout — cart is always server-side now
125
- if (cart && cart.id) {
134
+ // Create new checkout — cart is always server-side now
126
135
  const newCheckout = await client.createCheckout({ cartId: cart.id });
127
136
  setCheckout(newCheckout);
128
137
 
@@ -135,28 +144,22 @@ function CheckoutContent() {
135
144
  setStep('address');
136
145
  return;
137
146
  }
138
- } else {
139
- setError(t('cartIsEmpty'));
140
- }
141
147
 
142
- // If pickup locations exist, start with delivery method selection
143
- if (locations.length > 0) {
144
- setStep('method');
148
+ // If pickup locations exist, start with delivery method selection
149
+ if (locations.length > 0) {
150
+ setStep('method');
151
+ }
152
+ } catch (err) {
153
+ const message = err instanceof Error ? err.message : t('failedToInitCheckout');
154
+ setError(message);
155
+ } finally {
156
+ setInitializing(false);
145
157
  }
146
- } catch (err) {
147
- const message = err instanceof Error ? err.message : t('failedToInitCheckout');
148
- setError(message);
149
- } finally {
150
- setInitializing(false);
151
- }
152
- }, [existingCheckoutId, cart]);
158
+ };
153
159
 
154
- const cartLoaded = cart !== null;
155
- useEffect(() => {
156
- if (cartLoaded) {
157
- initCheckout();
158
- }
159
- }, [cartLoaded, initCheckout]);
160
+ initCheckout();
161
+ // eslint-disable-next-line react-hooks/exhaustive-deps
162
+ }, [cart?.id, existingCheckoutId]);
160
163
 
161
164
  // Handle shipping address submission
162
165
  async function handleAddressSubmit(
@@ -189,147 +189,159 @@ export function CheckoutForm({
189
189
 
190
190
  {!emailOnly && (
191
191
  <>
192
- {/* Country + Region row */}
193
- <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
194
- <div>
195
- <label htmlFor="country" className="text-foreground mb-1 block text-sm font-medium">
196
- {t('country')} <span className="text-destructive">*</span>
197
- </label>
198
- {hasCountryOptions ? (
199
- <select
200
- id="country"
201
- value={formData.country}
202
- onChange={(e) => updateField('country', e.target.value)}
203
- className={cn(selectClass, errors.country ? 'border-destructive' : 'border-border')}
204
- >
205
- <option value="">{t('selectCountry')}</option>
206
- {destinations.countries.map((c) => (
207
- <option key={c.code} value={c.code}>
208
- {c.name}
209
- </option>
210
- ))}
211
- </select>
212
- ) : (
192
+ {/* Country + Region row */}
193
+ <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
194
+ <div>
195
+ <label htmlFor="country" className="text-foreground mb-1 block text-sm font-medium">
196
+ {t('country')} <span className="text-destructive">*</span>
197
+ </label>
198
+ {hasCountryOptions ? (
199
+ <select
200
+ id="country"
201
+ value={formData.country}
202
+ onChange={(e) => updateField('country', e.target.value)}
203
+ className={cn(
204
+ selectClass,
205
+ errors.country ? 'border-destructive' : 'border-border'
206
+ )}
207
+ >
208
+ <option value="">{t('selectCountry')}</option>
209
+ {destinations.countries.map((c) => (
210
+ <option key={c.code} value={c.code}>
211
+ {c.name}
212
+ </option>
213
+ ))}
214
+ </select>
215
+ ) : (
216
+ <input
217
+ id="country"
218
+ type="text"
219
+ value={formData.country}
220
+ onChange={(e) => updateField('country', e.target.value)}
221
+ className={cn(
222
+ inputClass,
223
+ errors.country ? 'border-destructive' : 'border-border'
224
+ )}
225
+ placeholder={t('countryPlaceholder')}
226
+ />
227
+ )}
228
+ {errors.country && <p className="text-destructive mt-1 text-xs">{errors.country}</p>}
229
+ </div>
230
+
231
+ <div>
232
+ <label htmlFor="region" className="text-foreground mb-1 block text-sm font-medium">
233
+ {t('stateRegion')}
234
+ </label>
235
+ {hasRegionOptions ? (
236
+ <select
237
+ id="region"
238
+ value={formData.region || ''}
239
+ onChange={(e) => updateField('region', e.target.value)}
240
+ className={cn(selectClass, 'border-border')}
241
+ >
242
+ <option value="">{t('selectRegion')}</option>
243
+ {countryRegions.map((r) => (
244
+ <option key={r.code} value={r.code}>
245
+ {r.name}
246
+ </option>
247
+ ))}
248
+ </select>
249
+ ) : (
250
+ <input
251
+ id="region"
252
+ type="text"
253
+ value={formData.region || ''}
254
+ onChange={(e) => updateField('region', e.target.value)}
255
+ className={cn(inputClass, 'border-border')}
256
+ />
257
+ )}
258
+ </div>
259
+ </div>
260
+
261
+ {/* Address line 1 */}
262
+ <div>
263
+ <label htmlFor="line1" className="text-foreground mb-1 block text-sm font-medium">
264
+ {t('address')} <span className="text-destructive">*</span>
265
+ </label>
213
266
  <input
214
- id="country"
267
+ id="line1"
215
268
  type="text"
216
- value={formData.country}
217
- onChange={(e) => updateField('country', e.target.value)}
218
- className={cn(inputClass, errors.country ? 'border-destructive' : 'border-border')}
219
- placeholder={t('countryPlaceholder')}
269
+ value={formData.line1}
270
+ onChange={(e) => updateField('line1', e.target.value)}
271
+ className={cn(inputClass, errors.line1 ? 'border-destructive' : 'border-border')}
272
+ placeholder={t('streetAddress')}
220
273
  />
221
- )}
222
- {errors.country && <p className="text-destructive mt-1 text-xs">{errors.country}</p>}
223
- </div>
274
+ {errors.line1 && <p className="text-destructive mt-1 text-xs">{errors.line1}</p>}
275
+ </div>
224
276
 
225
- <div>
226
- <label htmlFor="region" className="text-foreground mb-1 block text-sm font-medium">
227
- {t('stateRegion')}
228
- </label>
229
- {hasRegionOptions ? (
230
- <select
231
- id="region"
232
- value={formData.region || ''}
233
- onChange={(e) => updateField('region', e.target.value)}
234
- className={cn(selectClass, 'border-border')}
235
- >
236
- <option value="">{t('selectRegion')}</option>
237
- {countryRegions.map((r) => (
238
- <option key={r.code} value={r.code}>
239
- {r.name}
240
- </option>
241
- ))}
242
- </select>
243
- ) : (
277
+ {/* Address line 2 */}
278
+ <div>
279
+ <label htmlFor="line2" className="text-foreground mb-1 block text-sm font-medium">
280
+ {t('apartmentSuite')}
281
+ </label>
244
282
  <input
245
- id="region"
283
+ id="line2"
246
284
  type="text"
247
- value={formData.region || ''}
248
- onChange={(e) => updateField('region', e.target.value)}
285
+ value={formData.line2 || ''}
286
+ onChange={(e) => updateField('line2', e.target.value)}
249
287
  className={cn(inputClass, 'border-border')}
288
+ placeholder={t('aptPlaceholder')}
250
289
  />
251
- )}
252
- </div>
253
- </div>
254
-
255
- {/* Address line 1 */}
256
- <div>
257
- <label htmlFor="line1" className="text-foreground mb-1 block text-sm font-medium">
258
- {t('address')} <span className="text-destructive">*</span>
259
- </label>
260
- <input
261
- id="line1"
262
- type="text"
263
- value={formData.line1}
264
- onChange={(e) => updateField('line1', e.target.value)}
265
- className={cn(inputClass, errors.line1 ? 'border-destructive' : 'border-border')}
266
- placeholder={t('streetAddress')}
267
- />
268
- {errors.line1 && <p className="text-destructive mt-1 text-xs">{errors.line1}</p>}
269
- </div>
270
-
271
- {/* Address line 2 */}
272
- <div>
273
- <label htmlFor="line2" className="text-foreground mb-1 block text-sm font-medium">
274
- {t('apartmentSuite')}
275
- </label>
276
- <input
277
- id="line2"
278
- type="text"
279
- value={formData.line2 || ''}
280
- onChange={(e) => updateField('line2', e.target.value)}
281
- className={cn(inputClass, 'border-border')}
282
- placeholder={t('aptPlaceholder')}
283
- />
284
- </div>
290
+ </div>
285
291
 
286
- {/* City + Postal code row */}
287
- <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
288
- <div>
289
- <label htmlFor="city" className="text-foreground mb-1 block text-sm font-medium">
290
- {t('city')} <span className="text-destructive">*</span>
291
- </label>
292
- <input
293
- id="city"
294
- type="text"
295
- value={formData.city}
296
- onChange={(e) => updateField('city', e.target.value)}
297
- className={cn(inputClass, errors.city ? 'border-destructive' : 'border-border')}
298
- />
299
- {errors.city && <p className="text-destructive mt-1 text-xs">{errors.city}</p>}
300
- </div>
292
+ {/* City + Postal code row */}
293
+ <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
294
+ <div>
295
+ <label htmlFor="city" className="text-foreground mb-1 block text-sm font-medium">
296
+ {t('city')} <span className="text-destructive">*</span>
297
+ </label>
298
+ <input
299
+ id="city"
300
+ type="text"
301
+ value={formData.city}
302
+ onChange={(e) => updateField('city', e.target.value)}
303
+ className={cn(inputClass, errors.city ? 'border-destructive' : 'border-border')}
304
+ />
305
+ {errors.city && <p className="text-destructive mt-1 text-xs">{errors.city}</p>}
306
+ </div>
301
307
 
302
- <div>
303
- <label htmlFor="postalCode" className="text-foreground mb-1 block text-sm font-medium">
304
- {t('postalCode')} <span className="text-destructive">*</span>
305
- </label>
306
- <input
307
- id="postalCode"
308
- type="text"
309
- value={formData.postalCode}
310
- onChange={(e) => updateField('postalCode', e.target.value)}
311
- className={cn(inputClass, errors.postalCode ? 'border-destructive' : 'border-border')}
312
- />
313
- {errors.postalCode && (
314
- <p className="text-destructive mt-1 text-xs">{errors.postalCode}</p>
315
- )}
316
- </div>
317
- </div>
308
+ <div>
309
+ <label
310
+ htmlFor="postalCode"
311
+ className="text-foreground mb-1 block text-sm font-medium"
312
+ >
313
+ {t('postalCode')} <span className="text-destructive">*</span>
314
+ </label>
315
+ <input
316
+ id="postalCode"
317
+ type="text"
318
+ value={formData.postalCode}
319
+ onChange={(e) => updateField('postalCode', e.target.value)}
320
+ className={cn(
321
+ inputClass,
322
+ errors.postalCode ? 'border-destructive' : 'border-border'
323
+ )}
324
+ />
325
+ {errors.postalCode && (
326
+ <p className="text-destructive mt-1 text-xs">{errors.postalCode}</p>
327
+ )}
328
+ </div>
329
+ </div>
318
330
 
319
- {/* Phone */}
320
- <div>
321
- <label htmlFor="phone" className="text-foreground mb-1 block text-sm font-medium">
322
- {t('phone')}
323
- </label>
324
- <input
325
- id="phone"
326
- type="tel"
327
- value={formData.phone || ''}
328
- onChange={(e) => updateField('phone', e.target.value)}
329
- className={cn(inputClass, 'border-border')}
330
- placeholder={t('phonePlaceholder')}
331
- />
332
- </div>
331
+ {/* Phone */}
332
+ <div>
333
+ <label htmlFor="phone" className="text-foreground mb-1 block text-sm font-medium">
334
+ {t('phone')}
335
+ </label>
336
+ <input
337
+ id="phone"
338
+ type="tel"
339
+ value={formData.phone || ''}
340
+ onChange={(e) => updateField('phone', e.target.value)}
341
+ className={cn(inputClass, 'border-border')}
342
+ placeholder={t('phonePlaceholder')}
343
+ />
344
+ </div>
333
345
  </>
334
346
  )}
335
347
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  import type { InventoryInfo } from 'brainerce';
4
4
  import { cn } from '@/lib/utils';
5
+ import { useTranslations } from '@/lib/translations';
5
6
 
6
7
  interface StockBadgeProps {
7
8
  inventory: InventoryInfo | null | undefined;
@@ -10,7 +11,8 @@ interface StockBadgeProps {
10
11
  }
11
12
 
12
13
  export function StockBadge({ inventory, lowStockThreshold = 5, className }: StockBadgeProps) {
13
- const label = getStockLabel(inventory, lowStockThreshold);
14
+ const t = useTranslations('productDetail');
15
+ const label = getStockLabel(inventory, lowStockThreshold, t);
14
16
  const color = getStockColor(inventory, lowStockThreshold);
15
17
 
16
18
  return (
@@ -28,21 +30,22 @@ export function StockBadge({ inventory, lowStockThreshold = 5, className }: Stoc
28
30
 
29
31
  function getStockLabel(
30
32
  inventory: InventoryInfo | null | undefined,
31
- lowStockThreshold: number
33
+ lowStockThreshold: number,
34
+ t: (key: string) => string
32
35
  ): string {
33
- if (!inventory) return 'Out of Stock';
36
+ if (!inventory) return t('outOfStock');
34
37
 
35
38
  const { trackingMode, inStock, available } = inventory;
36
39
 
37
- if (trackingMode === 'DISABLED') return 'Unavailable';
38
- if (!inStock) return 'Out of Stock';
39
- if (trackingMode === 'UNLIMITED') return 'In Stock';
40
+ if (trackingMode === 'DISABLED') return t('unavailable');
41
+ if (!inStock) return t('outOfStock');
42
+ if (trackingMode === 'UNLIMITED') return t('inStock');
40
43
 
41
44
  // TRACKED — show actual quantity
42
45
  if (available <= lowStockThreshold) {
43
- return `Only ${available} left`;
46
+ return t('onlyLeft').replace('{available}', String(available));
44
47
  }
45
- return `${available} in stock`;
48
+ return t('availableInStock').replace('{available}', String(available));
46
49
  }
47
50
 
48
51
  function getStockColor(
@@ -1,147 +1,292 @@
1
- 'use client';
2
-
3
- import { useMemo } from 'react';
4
- import type { Product, ProductVariant } from 'brainerce';
5
- import { getVariantOptions, getStockStatus, formatPrice } from 'brainerce';
6
- import { useStoreInfo } from '@/providers/store-provider';
7
- import { cn } from '@/lib/utils';
8
-
9
- interface VariantSelectorProps {
10
- product: Product;
11
- selectedVariant: ProductVariant | null;
12
- onVariantChange: (variant: ProductVariant) => void;
13
- className?: string;
14
- }
15
-
16
- interface AttributeGroup {
17
- name: string;
18
- values: Array<{
19
- value: string;
20
- variants: ProductVariant[];
21
- }>;
22
- }
23
-
24
- export function VariantSelector({
25
- product,
26
- selectedVariant,
27
- onVariantChange,
28
- className,
29
- }: VariantSelectorProps) {
30
- const { storeInfo } = useStoreInfo();
31
- const currency = storeInfo?.currency || 'USD';
32
- const variants = useMemo(() => product.variants || [], [product.variants]);
33
-
34
- // Build attribute groups from product attribute options or variant data
35
- const attributeGroups = useMemo<AttributeGroup[]>(() => {
36
- const groups = new Map<string, Map<string, ProductVariant[]>>();
37
-
38
- for (const variant of variants) {
39
- const options = getVariantOptions(variant);
40
- for (const { name, value } of options) {
41
- if (!groups.has(name)) {
42
- groups.set(name, new Map());
43
- }
44
- const valuesMap = groups.get(name)!;
45
- if (!valuesMap.has(value)) {
46
- valuesMap.set(value, []);
47
- }
48
- valuesMap.get(value)!.push(variant);
49
- }
50
- }
51
-
52
- return Array.from(groups.entries()).map(([name, valuesMap]) => ({
53
- name,
54
- values: Array.from(valuesMap.entries()).map(([value, variantList]) => ({
55
- value,
56
- variants: variantList,
57
- })),
58
- }));
59
- }, [variants]);
60
-
61
- // Get currently selected attribute values
62
- const selectedOptions = useMemo(() => {
63
- if (!selectedVariant) return new Map<string, string>();
64
- const opts = getVariantOptions(selectedVariant);
65
- return new Map(opts.map(({ name, value }) => [name, value]));
66
- }, [selectedVariant]);
67
-
68
- // Find the variant that matches all selected attributes
69
- function findMatchingVariant(
70
- attributeName: string,
71
- newValue: string
72
- ): ProductVariant | undefined {
73
- const nextSelection = new Map(selectedOptions);
74
- nextSelection.set(attributeName, newValue);
75
-
76
- return variants.find((v) => {
77
- const opts = getVariantOptions(v);
78
- return Array.from(nextSelection.entries()).every(([name, value]) =>
79
- opts.some((o) => o.name === name && o.value === value)
80
- );
81
- });
82
- }
83
-
84
- if (attributeGroups.length === 0) return null;
85
-
86
- return (
87
- <div className={cn('space-y-4', className)}>
88
- {attributeGroups.map((group) => (
89
- <div key={group.name}>
90
- <label className="text-foreground mb-2 block text-sm font-medium">
91
- {group.name}
92
- {selectedOptions.get(group.name) && (
93
- <span className="text-muted-foreground ms-1 font-normal">
94
- : {selectedOptions.get(group.name)}
95
- </span>
96
- )}
97
- </label>
98
- <div className="flex flex-wrap gap-2">
99
- {group.values.map(({ value, variants: matchingVariants }) => {
100
- const isSelected = selectedOptions.get(group.name) === value;
101
- const matchedVariant = findMatchingVariant(group.name, value);
102
- const isAvailable = matchedVariant?.inventory?.canPurchase !== false;
103
-
104
- return (
105
- <button
106
- key={value}
107
- type="button"
108
- disabled={!isAvailable}
109
- onClick={() => {
110
- const variant = matchedVariant || matchingVariants[0];
111
- if (variant) onVariantChange(variant);
112
- }}
113
- className={cn(
114
- 'rounded border px-4 py-2 text-sm transition-colors',
115
- isSelected
116
- ? 'border-primary bg-primary text-primary-foreground'
117
- : isAvailable
118
- ? 'border-border bg-background text-foreground hover:border-primary'
119
- : 'border-border bg-muted text-muted-foreground cursor-not-allowed line-through opacity-50'
120
- )}
121
- >
122
- {value}
123
- </button>
124
- );
125
- })}
126
- </div>
127
- </div>
128
- ))}
129
-
130
- {/* Variant-specific info */}
131
- {selectedVariant && (
132
- <div className="text-muted-foreground flex items-center gap-3 pt-1 text-sm">
133
- {selectedVariant.price && (
134
- <span>
135
- {
136
- formatPrice(selectedVariant.salePrice || selectedVariant.price, {
137
- currency,
138
- }) as string
139
- }
140
- </span>
141
- )}
142
- <span>{getStockStatus(selectedVariant.inventory)}</span>
143
- </div>
144
- )}
145
- </div>
146
- );
147
- }
1
+ 'use client';
2
+
3
+ import { useMemo } from 'react';
4
+ import type { Product, ProductVariant } from 'brainerce';
5
+ import { getVariantOptions, getProductSwatches, formatPrice } from 'brainerce';
6
+ import type { InventoryInfo } from 'brainerce';
7
+ import { useStoreInfo } from '@/providers/store-provider';
8
+ import { useTranslations } from '@/lib/translations';
9
+ import { cn } from '@/lib/utils';
10
+
11
+ interface VariantSelectorProps {
12
+ product: Product;
13
+ selectedVariant: ProductVariant | null;
14
+ onVariantChange: (variant: ProductVariant) => void;
15
+ className?: string;
16
+ }
17
+
18
+ interface AttributeGroup {
19
+ name: string;
20
+ displayType: string;
21
+ values: Array<{
22
+ value: string;
23
+ swatchColor?: string | null;
24
+ swatchColor2?: string | null;
25
+ swatchImageUrl?: string | null;
26
+ variants: ProductVariant[];
27
+ }>;
28
+ }
29
+
30
+ export function VariantSelector({
31
+ product,
32
+ selectedVariant,
33
+ onVariantChange,
34
+ className,
35
+ }: VariantSelectorProps) {
36
+ const { storeInfo } = useStoreInfo();
37
+ const t = useTranslations('productDetail');
38
+ const currency = storeInfo?.currency || 'USD';
39
+ const variants = useMemo(() => product.variants || [], [product.variants]);
40
+
41
+ // Get swatch metadata from product attribute options
42
+ const swatchData = useMemo(() => getProductSwatches(product), [product]);
43
+ const swatchMap = useMemo(() => {
44
+ const map = new Map<
45
+ string,
46
+ {
47
+ displayType: string;
48
+ options: Map<
49
+ string,
50
+ {
51
+ swatchColor?: string | null;
52
+ swatchColor2?: string | null;
53
+ swatchImageUrl?: string | null;
54
+ }
55
+ >;
56
+ }
57
+ >();
58
+ for (const attr of swatchData) {
59
+ const optMap = new Map<
60
+ string,
61
+ {
62
+ swatchColor?: string | null;
63
+ swatchColor2?: string | null;
64
+ swatchImageUrl?: string | null;
65
+ }
66
+ >();
67
+ for (const opt of attr.options) {
68
+ optMap.set(opt.name, {
69
+ swatchColor: opt.swatchColor,
70
+ swatchColor2: opt.swatchColor2,
71
+ swatchImageUrl: opt.swatchImageUrl,
72
+ });
73
+ }
74
+ map.set(attr.attributeName, { displayType: attr.displayType, options: optMap });
75
+ }
76
+ return map;
77
+ }, [swatchData]);
78
+
79
+ // Build attribute groups from variant data, enriched with swatch info
80
+ const attributeGroups = useMemo<AttributeGroup[]>(() => {
81
+ const groups = new Map<string, Map<string, ProductVariant[]>>();
82
+
83
+ for (const variant of variants) {
84
+ const options = getVariantOptions(variant);
85
+ for (const { name, value } of options) {
86
+ if (!groups.has(name)) {
87
+ groups.set(name, new Map());
88
+ }
89
+ const valuesMap = groups.get(name)!;
90
+ if (!valuesMap.has(value)) {
91
+ valuesMap.set(value, []);
92
+ }
93
+ valuesMap.get(value)!.push(variant);
94
+ }
95
+ }
96
+
97
+ return Array.from(groups.entries()).map(([name, valuesMap]) => {
98
+ const attrSwatch = swatchMap.get(name);
99
+ return {
100
+ name,
101
+ displayType: attrSwatch?.displayType || 'DROPDOWN',
102
+ values: Array.from(valuesMap.entries()).map(([value, variantList]) => {
103
+ const optSwatch = attrSwatch?.options.get(value);
104
+ return {
105
+ value,
106
+ swatchColor: optSwatch?.swatchColor,
107
+ swatchColor2: optSwatch?.swatchColor2,
108
+ swatchImageUrl: optSwatch?.swatchImageUrl,
109
+ variants: variantList,
110
+ };
111
+ }),
112
+ };
113
+ });
114
+ }, [variants, swatchMap]);
115
+
116
+ // Get currently selected attribute values
117
+ const selectedOptions = useMemo(() => {
118
+ if (!selectedVariant) return new Map<string, string>();
119
+ const opts = getVariantOptions(selectedVariant);
120
+ return new Map(opts.map(({ name, value }) => [name, value]));
121
+ }, [selectedVariant]);
122
+
123
+ // Find the variant that matches all selected attributes
124
+ function findMatchingVariant(
125
+ attributeName: string,
126
+ newValue: string
127
+ ): ProductVariant | undefined {
128
+ const nextSelection = new Map(selectedOptions);
129
+ nextSelection.set(attributeName, newValue);
130
+
131
+ return variants.find((v) => {
132
+ const opts = getVariantOptions(v);
133
+ return Array.from(nextSelection.entries()).every(([name, value]) =>
134
+ opts.some((o) => o.name === name && o.value === value)
135
+ );
136
+ });
137
+ }
138
+
139
+ if (attributeGroups.length === 0) return null;
140
+
141
+ return (
142
+ <div className={cn('space-y-4', className)}>
143
+ {attributeGroups.map((group) => (
144
+ <div key={group.name}>
145
+ <label className="text-foreground mb-2 block text-sm font-medium">
146
+ {group.name}
147
+ {selectedOptions.get(group.name) && (
148
+ <span className="text-muted-foreground ms-1 font-normal">
149
+ : {selectedOptions.get(group.name)}
150
+ </span>
151
+ )}
152
+ </label>
153
+ <div className="flex flex-wrap gap-2">
154
+ {group.values.map(
155
+ ({
156
+ value,
157
+ swatchColor,
158
+ swatchColor2,
159
+ swatchImageUrl,
160
+ variants: matchingVariants,
161
+ }) => {
162
+ const isSelected = selectedOptions.get(group.name) === value;
163
+ const matchedVariant = findMatchingVariant(group.name, value);
164
+ const isAvailable = matchedVariant?.inventory?.canPurchase !== false;
165
+
166
+ // Color swatch rendering
167
+ if (group.displayType === 'COLOR_SWATCH' && swatchColor) {
168
+ return (
169
+ <button
170
+ key={value}
171
+ type="button"
172
+ disabled={!isAvailable}
173
+ title={value}
174
+ onClick={() => {
175
+ const variant = matchedVariant || matchingVariants[0];
176
+ if (variant) onVariantChange(variant);
177
+ }}
178
+ className={cn(
179
+ 'h-9 w-9 rounded-full border-2 transition-all',
180
+ isSelected
181
+ ? 'border-primary ring-primary/30 ring-2'
182
+ : isAvailable
183
+ ? 'border-border hover:border-primary'
184
+ : 'cursor-not-allowed opacity-40'
185
+ )}
186
+ style={{
187
+ background: swatchColor2
188
+ ? `linear-gradient(135deg, ${swatchColor} 50%, ${swatchColor2} 50%)`
189
+ : swatchColor,
190
+ }}
191
+ >
192
+ {!isAvailable && (
193
+ <span
194
+ className="bg-muted-foreground block h-full w-full rounded-full opacity-50"
195
+ style={{
196
+ backgroundImage:
197
+ 'linear-gradient(135deg, transparent 45%, currentColor 45%, currentColor 55%, transparent 55%)',
198
+ }}
199
+ />
200
+ )}
201
+ </button>
202
+ );
203
+ }
204
+
205
+ // Image swatch rendering
206
+ if (group.displayType === 'IMAGE_SWATCH' && swatchImageUrl) {
207
+ return (
208
+ <button
209
+ key={value}
210
+ type="button"
211
+ disabled={!isAvailable}
212
+ title={value}
213
+ onClick={() => {
214
+ const variant = matchedVariant || matchingVariants[0];
215
+ if (variant) onVariantChange(variant);
216
+ }}
217
+ className={cn(
218
+ 'h-10 w-10 overflow-hidden rounded-lg border-2 transition-all',
219
+ isSelected
220
+ ? 'border-primary ring-primary/30 ring-2'
221
+ : isAvailable
222
+ ? 'border-border hover:border-primary'
223
+ : 'cursor-not-allowed opacity-40'
224
+ )}
225
+ >
226
+ <img
227
+ src={swatchImageUrl}
228
+ alt={value}
229
+ className="h-full w-full object-cover"
230
+ />
231
+ </button>
232
+ );
233
+ }
234
+
235
+ // Default button rendering (BUTTON, DROPDOWN, or fallback)
236
+ return (
237
+ <button
238
+ key={value}
239
+ type="button"
240
+ disabled={!isAvailable}
241
+ onClick={() => {
242
+ const variant = matchedVariant || matchingVariants[0];
243
+ if (variant) onVariantChange(variant);
244
+ }}
245
+ className={cn(
246
+ 'rounded border px-4 py-2 text-sm transition-colors',
247
+ isSelected
248
+ ? 'border-primary bg-primary text-primary-foreground'
249
+ : isAvailable
250
+ ? 'border-border bg-background text-foreground hover:border-primary'
251
+ : 'border-border bg-muted text-muted-foreground cursor-not-allowed line-through opacity-50'
252
+ )}
253
+ >
254
+ {value}
255
+ </button>
256
+ );
257
+ }
258
+ )}
259
+ </div>
260
+ </div>
261
+ ))}
262
+
263
+ {/* Variant-specific info */}
264
+ {selectedVariant && (
265
+ <div className="text-muted-foreground flex items-center gap-3 pt-1 text-sm">
266
+ {selectedVariant.price && (
267
+ <span>
268
+ {
269
+ formatPrice(selectedVariant.salePrice || selectedVariant.price, {
270
+ currency,
271
+ }) as string
272
+ }
273
+ </span>
274
+ )}
275
+ <span>{getTranslatedStockStatus(selectedVariant.inventory, t)}</span>
276
+ </div>
277
+ )}
278
+ </div>
279
+ );
280
+ }
281
+
282
+ function getTranslatedStockStatus(
283
+ inventory: InventoryInfo | null | undefined,
284
+ t: (key: string) => string
285
+ ): string {
286
+ if (!inventory) return t('outOfStock');
287
+ const { trackingMode, inStock, available } = inventory;
288
+ if (trackingMode === 'DISABLED') return t('unavailable');
289
+ if (!inStock) return t('outOfStock');
290
+ if (trackingMode === 'UNLIMITED') return t('inStock');
291
+ return t('availableInStock').replace('{available}', String(available));
292
+ }