create-brainerce-store 1.42.0 → 1.43.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.
Files changed (22) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +1 -1
  3. package/templates/nextjs/base/scripts/fetch-store-info.mjs +10 -4
  4. package/templates/nextjs/base/src/app/checkout/page.tsx +982 -981
  5. package/templates/nextjs/base/src/app/products/[slug]/page.tsx +118 -117
  6. package/templates/nextjs/base/src/components/account/order-history.tsx +368 -367
  7. package/templates/nextjs/base/src/components/cart/cart-bundle-offer.tsx +111 -112
  8. package/templates/nextjs/base/src/components/cart/cart-item.tsx +152 -153
  9. package/templates/nextjs/base/src/components/cart/cart-summary.tsx +108 -108
  10. package/templates/nextjs/base/src/components/cart/cart-upgrade-banner.tsx +141 -142
  11. package/templates/nextjs/base/src/components/cart/free-shipping-bar.tsx +62 -59
  12. package/templates/nextjs/base/src/components/checkout/order-bump-card.tsx +242 -243
  13. package/templates/nextjs/base/src/components/checkout/pickup-step.tsx +198 -199
  14. package/templates/nextjs/base/src/components/checkout/shipping-step.tsx +109 -110
  15. package/templates/nextjs/base/src/components/checkout/tax-display.tsx +64 -65
  16. package/templates/nextjs/base/src/components/products/frequently-bought-together.tsx +203 -202
  17. package/templates/nextjs/base/src/components/products/product-card.tsx +226 -226
  18. package/templates/nextjs/base/src/components/products/variant-selector.tsx +291 -292
  19. package/templates/nextjs/base/src/components/seo/product-json-ld.tsx +129 -125
  20. package/templates/nextjs/base/src/components/shared/price-display.tsx +61 -65
  21. package/templates/nextjs/base/src/lib/resolve-currency.ts +25 -0
  22. package/templates/nextjs/base/src/lib/use-currency.ts +24 -0
@@ -1,292 +1,291 @@
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 || process.env.NEXT_PUBLIC_STORE_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
- }
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 { useCurrency } from '@/lib/use-currency';
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 t = useTranslations('productDetail');
37
+ const currency = useCurrency();
38
+ const variants = useMemo(() => product.variants || [], [product.variants]);
39
+
40
+ // Get swatch metadata from product attribute options
41
+ const swatchData = useMemo(() => getProductSwatches(product), [product]);
42
+ const swatchMap = useMemo(() => {
43
+ const map = new Map<
44
+ string,
45
+ {
46
+ displayType: string;
47
+ options: Map<
48
+ string,
49
+ {
50
+ swatchColor?: string | null;
51
+ swatchColor2?: string | null;
52
+ swatchImageUrl?: string | null;
53
+ }
54
+ >;
55
+ }
56
+ >();
57
+ for (const attr of swatchData) {
58
+ const optMap = new Map<
59
+ string,
60
+ {
61
+ swatchColor?: string | null;
62
+ swatchColor2?: string | null;
63
+ swatchImageUrl?: string | null;
64
+ }
65
+ >();
66
+ for (const opt of attr.options) {
67
+ optMap.set(opt.name, {
68
+ swatchColor: opt.swatchColor,
69
+ swatchColor2: opt.swatchColor2,
70
+ swatchImageUrl: opt.swatchImageUrl,
71
+ });
72
+ }
73
+ map.set(attr.attributeName, { displayType: attr.displayType, options: optMap });
74
+ }
75
+ return map;
76
+ }, [swatchData]);
77
+
78
+ // Build attribute groups from variant data, enriched with swatch info
79
+ const attributeGroups = useMemo<AttributeGroup[]>(() => {
80
+ const groups = new Map<string, Map<string, ProductVariant[]>>();
81
+
82
+ for (const variant of variants) {
83
+ const options = getVariantOptions(variant);
84
+ for (const { name, value } of options) {
85
+ if (!groups.has(name)) {
86
+ groups.set(name, new Map());
87
+ }
88
+ const valuesMap = groups.get(name)!;
89
+ if (!valuesMap.has(value)) {
90
+ valuesMap.set(value, []);
91
+ }
92
+ valuesMap.get(value)!.push(variant);
93
+ }
94
+ }
95
+
96
+ return Array.from(groups.entries()).map(([name, valuesMap]) => {
97
+ const attrSwatch = swatchMap.get(name);
98
+ return {
99
+ name,
100
+ displayType: attrSwatch?.displayType || 'DROPDOWN',
101
+ values: Array.from(valuesMap.entries()).map(([value, variantList]) => {
102
+ const optSwatch = attrSwatch?.options.get(value);
103
+ return {
104
+ value,
105
+ swatchColor: optSwatch?.swatchColor,
106
+ swatchColor2: optSwatch?.swatchColor2,
107
+ swatchImageUrl: optSwatch?.swatchImageUrl,
108
+ variants: variantList,
109
+ };
110
+ }),
111
+ };
112
+ });
113
+ }, [variants, swatchMap]);
114
+
115
+ // Get currently selected attribute values
116
+ const selectedOptions = useMemo(() => {
117
+ if (!selectedVariant) return new Map<string, string>();
118
+ const opts = getVariantOptions(selectedVariant);
119
+ return new Map(opts.map(({ name, value }) => [name, value]));
120
+ }, [selectedVariant]);
121
+
122
+ // Find the variant that matches all selected attributes
123
+ function findMatchingVariant(
124
+ attributeName: string,
125
+ newValue: string
126
+ ): ProductVariant | undefined {
127
+ const nextSelection = new Map(selectedOptions);
128
+ nextSelection.set(attributeName, newValue);
129
+
130
+ return variants.find((v) => {
131
+ const opts = getVariantOptions(v);
132
+ return Array.from(nextSelection.entries()).every(([name, value]) =>
133
+ opts.some((o) => o.name === name && o.value === value)
134
+ );
135
+ });
136
+ }
137
+
138
+ if (attributeGroups.length === 0) return null;
139
+
140
+ return (
141
+ <div className={cn('space-y-4', className)}>
142
+ {attributeGroups.map((group) => (
143
+ <div key={group.name}>
144
+ <label className="text-foreground mb-2 block text-sm font-medium">
145
+ {group.name}
146
+ {selectedOptions.get(group.name) && (
147
+ <span className="text-muted-foreground ms-1 font-normal">
148
+ : {selectedOptions.get(group.name)}
149
+ </span>
150
+ )}
151
+ </label>
152
+ <div className="flex flex-wrap gap-2">
153
+ {group.values.map(
154
+ ({
155
+ value,
156
+ swatchColor,
157
+ swatchColor2,
158
+ swatchImageUrl,
159
+ variants: matchingVariants,
160
+ }) => {
161
+ const isSelected = selectedOptions.get(group.name) === value;
162
+ const matchedVariant = findMatchingVariant(group.name, value);
163
+ const isAvailable = matchedVariant?.inventory?.canPurchase !== false;
164
+
165
+ // Color swatch rendering
166
+ if (group.displayType === 'COLOR_SWATCH' && swatchColor) {
167
+ return (
168
+ <button
169
+ key={value}
170
+ type="button"
171
+ disabled={!isAvailable}
172
+ title={value}
173
+ onClick={() => {
174
+ const variant = matchedVariant || matchingVariants[0];
175
+ if (variant) onVariantChange(variant);
176
+ }}
177
+ className={cn(
178
+ 'h-9 w-9 rounded-full border-2 transition-all',
179
+ isSelected
180
+ ? 'border-primary ring-primary/30 ring-2'
181
+ : isAvailable
182
+ ? 'border-border hover:border-primary'
183
+ : 'cursor-not-allowed opacity-40'
184
+ )}
185
+ style={{
186
+ background: swatchColor2
187
+ ? `linear-gradient(135deg, ${swatchColor} 50%, ${swatchColor2} 50%)`
188
+ : swatchColor,
189
+ }}
190
+ >
191
+ {!isAvailable && (
192
+ <span
193
+ className="bg-muted-foreground block h-full w-full rounded-full opacity-50"
194
+ style={{
195
+ backgroundImage:
196
+ 'linear-gradient(135deg, transparent 45%, currentColor 45%, currentColor 55%, transparent 55%)',
197
+ }}
198
+ />
199
+ )}
200
+ </button>
201
+ );
202
+ }
203
+
204
+ // Image swatch rendering
205
+ if (group.displayType === 'IMAGE_SWATCH' && swatchImageUrl) {
206
+ return (
207
+ <button
208
+ key={value}
209
+ type="button"
210
+ disabled={!isAvailable}
211
+ title={value}
212
+ onClick={() => {
213
+ const variant = matchedVariant || matchingVariants[0];
214
+ if (variant) onVariantChange(variant);
215
+ }}
216
+ className={cn(
217
+ 'h-10 w-10 overflow-hidden rounded-lg border-2 transition-all',
218
+ isSelected
219
+ ? 'border-primary ring-primary/30 ring-2'
220
+ : isAvailable
221
+ ? 'border-border hover:border-primary'
222
+ : 'cursor-not-allowed opacity-40'
223
+ )}
224
+ >
225
+ <img
226
+ src={swatchImageUrl}
227
+ alt={value}
228
+ className="h-full w-full object-cover"
229
+ />
230
+ </button>
231
+ );
232
+ }
233
+
234
+ // Default button rendering (BUTTON, DROPDOWN, or fallback)
235
+ return (
236
+ <button
237
+ key={value}
238
+ type="button"
239
+ disabled={!isAvailable}
240
+ onClick={() => {
241
+ const variant = matchedVariant || matchingVariants[0];
242
+ if (variant) onVariantChange(variant);
243
+ }}
244
+ className={cn(
245
+ 'rounded border px-4 py-2 text-sm transition-colors',
246
+ isSelected
247
+ ? 'border-primary bg-primary text-primary-foreground'
248
+ : isAvailable
249
+ ? 'border-border bg-background text-foreground hover:border-primary'
250
+ : 'border-border bg-muted text-muted-foreground cursor-not-allowed line-through opacity-50'
251
+ )}
252
+ >
253
+ {value}
254
+ </button>
255
+ );
256
+ }
257
+ )}
258
+ </div>
259
+ </div>
260
+ ))}
261
+
262
+ {/* Variant-specific info */}
263
+ {selectedVariant && (
264
+ <div className="text-muted-foreground flex items-center gap-3 pt-1 text-sm">
265
+ {selectedVariant.price && (
266
+ <span>
267
+ {
268
+ formatPrice(selectedVariant.salePrice || selectedVariant.price, {
269
+ currency,
270
+ }) as string
271
+ }
272
+ </span>
273
+ )}
274
+ <span>{getTranslatedStockStatus(selectedVariant.inventory, t)}</span>
275
+ </div>
276
+ )}
277
+ </div>
278
+ );
279
+ }
280
+
281
+ function getTranslatedStockStatus(
282
+ inventory: InventoryInfo | null | undefined,
283
+ t: (key: string) => string
284
+ ): string {
285
+ if (!inventory) return t('outOfStock');
286
+ const { trackingMode, inStock, available } = inventory;
287
+ if (trackingMode === 'DISABLED') return t('unavailable');
288
+ if (!inStock) return t('outOfStock');
289
+ if (trackingMode === 'UNLIMITED') return t('inStock');
290
+ return t('availableInStock').replace('{available}', String(available));
291
+ }