create-brainerce-store 1.18.0 → 1.20.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/LICENSE +0 -0
- package/dist/index.js +31 -9
- package/messages/en.json +366 -362
- package/messages/he.json +366 -362
- package/package.json +8 -8
- package/templates/nextjs/base/next.config.ts +31 -31
- package/templates/nextjs/base/scripts/fetch-store-info.mjs +81 -81
- package/templates/nextjs/base/src/app/.well-known/apple-developer-merchantid-domain-association/route.ts +26 -26
- package/templates/nextjs/base/src/app/account/layout.tsx +9 -9
- package/templates/nextjs/base/src/app/account/page.tsx +122 -122
- package/templates/nextjs/base/src/app/api/auth/logout/route.ts +14 -14
- package/templates/nextjs/base/src/app/api/auth/me/route.ts +56 -56
- package/templates/nextjs/base/src/app/api/auth/oauth-callback/route.ts +59 -59
- package/templates/nextjs/base/src/app/api/auth/reset-callback/route.ts +41 -41
- package/templates/nextjs/base/src/app/api/auth/reset-password/route.ts +77 -77
- package/templates/nextjs/base/src/app/api/store/[...path]/route.ts +198 -198
- package/templates/nextjs/base/src/app/auth/callback/page.tsx +92 -92
- package/templates/nextjs/base/src/app/cart/layout.tsx +9 -9
- package/templates/nextjs/base/src/app/cart/page.tsx +204 -204
- package/templates/nextjs/base/src/app/checkout/layout.tsx +9 -9
- package/templates/nextjs/base/src/app/checkout/page.tsx +860 -860
- package/templates/nextjs/base/src/app/forgot-password/page.tsx +112 -112
- package/templates/nextjs/base/src/app/layout.tsx.ejs +75 -0
- package/templates/nextjs/base/src/app/login/layout.tsx +9 -9
- package/templates/nextjs/base/src/app/login/page.tsx +59 -59
- package/templates/nextjs/base/src/app/order-confirmation/layout.tsx +9 -9
- package/templates/nextjs/base/src/app/order-confirmation/page.tsx +17 -0
- package/templates/nextjs/base/src/app/payment-complete/page.tsx +59 -0
- package/templates/nextjs/base/src/app/products/[slug]/page.tsx +67 -67
- package/templates/nextjs/base/src/app/products/[slug]/product-client-section.tsx +486 -486
- package/templates/nextjs/base/src/app/products/layout.tsx +18 -18
- package/templates/nextjs/base/src/app/products/page.tsx +431 -431
- package/templates/nextjs/base/src/app/register/layout.tsx +9 -9
- package/templates/nextjs/base/src/app/register/page.tsx +65 -65
- package/templates/nextjs/base/src/app/reset-password/page.tsx +132 -132
- package/templates/nextjs/base/src/app/robots.ts +14 -14
- package/templates/nextjs/base/src/app/sitemap.ts +25 -25
- package/templates/nextjs/base/src/app/verify-email/page.tsx +258 -258
- package/templates/nextjs/base/src/components/account/address-book.tsx +432 -432
- package/templates/nextjs/base/src/components/account/order-history.tsx +350 -350
- package/templates/nextjs/base/src/components/auth/oauth-buttons.tsx +137 -137
- package/templates/nextjs/base/src/components/auth/register-form.tsx +232 -232
- package/templates/nextjs/base/src/components/cart/cart-bundle-offer.tsx +247 -111
- package/templates/nextjs/base/src/components/cart/cart-item.tsx +153 -153
- package/templates/nextjs/base/src/components/cart/cart-upgrade-banner.tsx +142 -142
- package/templates/nextjs/base/src/components/cart/free-shipping-bar.tsx +59 -59
- package/templates/nextjs/base/src/components/checkout/checkout-form.tsx +415 -415
- package/templates/nextjs/base/src/components/checkout/order-bump-card.tsx +243 -83
- package/templates/nextjs/base/src/components/checkout/payment-step.tsx +49 -3
- package/templates/nextjs/base/src/components/layout/footer.tsx +41 -41
- package/templates/nextjs/base/src/components/layout/header.tsx +336 -336
- package/templates/nextjs/base/src/components/layout/language-switcher.tsx.ejs +63 -0
- package/templates/nextjs/base/src/components/products/discount-badge.tsx +22 -22
- package/templates/nextjs/base/src/components/products/frequently-bought-together.tsx +202 -202
- package/templates/nextjs/base/src/components/products/product-card.tsx +218 -218
- package/templates/nextjs/base/src/components/products/recommendation-section.tsx +107 -107
- package/templates/nextjs/base/src/components/products/stock-badge.tsx +63 -63
- package/templates/nextjs/base/src/components/products/variant-selector.tsx +292 -292
- package/templates/nextjs/base/src/components/seo/product-json-ld.tsx +72 -72
- package/templates/nextjs/base/src/i18n.ts.ejs +21 -0
- package/templates/nextjs/base/src/lib/auth.ts +149 -149
- package/templates/nextjs/base/src/lib/brainerce.ts.ejs +9 -0
- package/templates/nextjs/base/src/lib/translations.ts.ejs +31 -0
- package/templates/nextjs/base/src/middleware.ts.ejs +81 -0
- package/templates/nextjs/base/src/providers/store-provider.tsx.ejs +41 -0
- package/templates/nextjs/base/src/lib/translations.ts +0 -11
- package/templates/nextjs/base/src/middleware.ts +0 -25
|
@@ -1,292 +1,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 { 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
|
-
}
|
|
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
|
+
}
|