create-brainerce-store 1.15.1 → 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.1",
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(
@@ -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(
@@ -2,8 +2,10 @@
2
2
 
3
3
  import { useMemo } from 'react';
4
4
  import type { Product, ProductVariant } from 'brainerce';
5
- import { getVariantOptions, getProductSwatches, getStockStatus, formatPrice } from 'brainerce';
5
+ import { getVariantOptions, getProductSwatches, formatPrice } from 'brainerce';
6
+ import type { InventoryInfo } from 'brainerce';
6
7
  import { useStoreInfo } from '@/providers/store-provider';
8
+ import { useTranslations } from '@/lib/translations';
7
9
  import { cn } from '@/lib/utils';
8
10
 
9
11
  interface VariantSelectorProps {
@@ -32,6 +34,7 @@ export function VariantSelector({
32
34
  className,
33
35
  }: VariantSelectorProps) {
34
36
  const { storeInfo } = useStoreInfo();
37
+ const t = useTranslations('productDetail');
35
38
  const currency = storeInfo?.currency || 'USD';
36
39
  const variants = useMemo(() => product.variants || [], [product.variants]);
37
40
 
@@ -269,9 +272,21 @@ export function VariantSelector({
269
272
  }
270
273
  </span>
271
274
  )}
272
- <span>{getStockStatus(selectedVariant.inventory)}</span>
275
+ <span>{getTranslatedStockStatus(selectedVariant.inventory, t)}</span>
273
276
  </div>
274
277
  )}
275
278
  </div>
276
279
  );
277
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
+ }