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,65 +1,64 @@
1
- 'use client';
2
-
3
- import type { TaxBreakdown } from 'brainerce';
4
- import { formatPrice } from 'brainerce';
5
- import { useTranslations } from '@/lib/translations';
6
- import { useStoreInfo } from '@/providers/store-provider';
7
- import { cn } from '@/lib/utils';
8
-
9
- interface TaxDisplayProps {
10
- /** Whether shipping address has been set */
11
- addressSet: boolean;
12
- /** Tax amount string from checkout (only available after address is set) */
13
- taxAmount?: string;
14
- /** Detailed tax breakdown (optional) */
15
- taxBreakdown?: TaxBreakdown | null;
16
- className?: string;
17
- }
18
-
19
- export function TaxDisplay({ addressSet, taxAmount, taxBreakdown, className }: TaxDisplayProps) {
20
- const t = useTranslations('checkout');
21
- const tc = useTranslations('common');
22
- const { storeInfo } = useStoreInfo();
23
- const currency = storeInfo?.currency || process.env.NEXT_PUBLIC_STORE_CURRENCY || 'USD';
24
-
25
- // Before address is set
26
- if (!addressSet) {
27
- return (
28
- <div className={cn('flex items-center justify-between text-sm', className)}>
29
- <span className="text-muted-foreground">{tc('tax')}</span>
30
- <span className="text-muted-foreground text-xs">{t('calculatedAfterAddress')}</span>
31
- </div>
32
- );
33
- }
34
-
35
- // After address, show tax amount
36
- const tax = taxAmount ? parseFloat(taxAmount) : 0;
37
-
38
- return (
39
- <div className={cn('space-y-1', className)}>
40
- <div className="flex items-center justify-between text-sm">
41
- <span className="text-muted-foreground">{tc('tax')}</span>
42
- <span className="text-foreground font-medium">
43
- {tax > 0 ? (formatPrice(tax, { currency }) as string) : t('noTax')}
44
- </span>
45
- </div>
46
-
47
- {/* Tax breakdown details */}
48
- {taxBreakdown && taxBreakdown.breakdown?.length > 0 && tax > 0 && (
49
- <div className="space-y-0.5 ps-4">
50
- {taxBreakdown.breakdown.map((item, index) => (
51
- <div
52
- key={index}
53
- className="text-muted-foreground flex items-center justify-between text-xs"
54
- >
55
- <span>
56
- {item.name} ({(item.rate * 100).toFixed(1)}%)
57
- </span>
58
- <span>{formatPrice(item.amount, { currency }) as string}</span>
59
- </div>
60
- ))}
61
- </div>
62
- )}
63
- </div>
64
- );
65
- }
1
+ 'use client';
2
+
3
+ import type { TaxBreakdown } from 'brainerce';
4
+ import { formatPrice } from 'brainerce';
5
+ import { useTranslations } from '@/lib/translations';
6
+ import { useCurrency } from '@/lib/use-currency';
7
+ import { cn } from '@/lib/utils';
8
+
9
+ interface TaxDisplayProps {
10
+ /** Whether shipping address has been set */
11
+ addressSet: boolean;
12
+ /** Tax amount string from checkout (only available after address is set) */
13
+ taxAmount?: string;
14
+ /** Detailed tax breakdown (optional) */
15
+ taxBreakdown?: TaxBreakdown | null;
16
+ className?: string;
17
+ }
18
+
19
+ export function TaxDisplay({ addressSet, taxAmount, taxBreakdown, className }: TaxDisplayProps) {
20
+ const t = useTranslations('checkout');
21
+ const tc = useTranslations('common');
22
+ const currency = useCurrency();
23
+
24
+ // Before address is set
25
+ if (!addressSet) {
26
+ return (
27
+ <div className={cn('flex items-center justify-between text-sm', className)}>
28
+ <span className="text-muted-foreground">{tc('tax')}</span>
29
+ <span className="text-muted-foreground text-xs">{t('calculatedAfterAddress')}</span>
30
+ </div>
31
+ );
32
+ }
33
+
34
+ // After address, show tax amount
35
+ const tax = taxAmount ? parseFloat(taxAmount) : 0;
36
+
37
+ return (
38
+ <div className={cn('space-y-1', className)}>
39
+ <div className="flex items-center justify-between text-sm">
40
+ <span className="text-muted-foreground">{tc('tax')}</span>
41
+ <span className="text-foreground font-medium">
42
+ {tax > 0 ? (formatPrice(tax, { currency }) as string) : t('noTax')}
43
+ </span>
44
+ </div>
45
+
46
+ {/* Tax breakdown details */}
47
+ {taxBreakdown && taxBreakdown.breakdown?.length > 0 && tax > 0 && (
48
+ <div className="space-y-0.5 ps-4">
49
+ {taxBreakdown.breakdown.map((item, index) => (
50
+ <div
51
+ key={index}
52
+ className="text-muted-foreground flex items-center justify-between text-xs"
53
+ >
54
+ <span>
55
+ {item.name} ({(item.rate * 100).toFixed(1)}%)
56
+ </span>
57
+ <span>{formatPrice(item.amount, { currency }) as string}</span>
58
+ </div>
59
+ ))}
60
+ </div>
61
+ )}
62
+ </div>
63
+ );
64
+ }
@@ -1,202 +1,203 @@
1
- 'use client';
2
-
3
- import { useState } from 'react';
4
- import Image from 'next/image';
5
- import type { Product, ProductRecommendation } from 'brainerce';
6
- import { formatPrice } from 'brainerce';
7
- import { useCart } from '@/providers/store-provider';
8
- import { useStoreInfo } from '@/providers/store-provider';
9
- import { useTranslations } from '@/lib/translations';
10
- import { cn } from '@/lib/utils';
11
-
12
- interface FrequentlyBoughtTogetherProps {
13
- items: ProductRecommendation[];
14
- currentProduct: Product;
15
- className?: string;
16
- }
17
-
18
- function getEffectivePrice(item: { basePrice: string; salePrice?: string | null }): number {
19
- const sale = item.salePrice ? parseFloat(item.salePrice) : null;
20
- const base = parseFloat(item.basePrice);
21
- return sale != null && sale < base ? sale : base;
22
- }
23
-
24
- function ProductThumb({
25
- name,
26
- imageUrl,
27
- price,
28
- currency,
29
- checked,
30
- onToggle,
31
- disabled,
32
- }: {
33
- name: string;
34
- imageUrl: string | null;
35
- price: number;
36
- currency: string;
37
- checked: boolean;
38
- onToggle?: () => void;
39
- disabled?: boolean;
40
- }) {
41
- return (
42
- <label
43
- className={cn(
44
- 'border-border bg-background relative flex cursor-pointer flex-col items-center rounded-lg border p-3 transition-all',
45
- checked ? 'ring-primary ring-2' : 'opacity-60',
46
- disabled && 'pointer-events-none'
47
- )}
48
- >
49
- {onToggle && (
50
- <input
51
- type="checkbox"
52
- checked={checked}
53
- onChange={onToggle}
54
- className="absolute start-2 top-2 h-4 w-4 rounded"
55
- />
56
- )}
57
- <div className="bg-muted relative mb-2 h-20 w-20 overflow-hidden rounded">
58
- {imageUrl ? (
59
- <Image src={imageUrl} alt={name} fill sizes="80px" className="object-cover" />
60
- ) : (
61
- <div className="flex h-full w-full items-center justify-center">
62
- <svg
63
- className="text-muted-foreground h-8 w-8"
64
- fill="none"
65
- viewBox="0 0 24 24"
66
- stroke="currentColor"
67
- >
68
- <path
69
- strokeLinecap="round"
70
- strokeLinejoin="round"
71
- strokeWidth={1.5}
72
- 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"
73
- />
74
- </svg>
75
- </div>
76
- )}
77
- </div>
78
- <span className="text-foreground line-clamp-2 text-center text-xs font-medium">{name}</span>
79
- <span className="text-muted-foreground mt-1 text-xs">
80
- {formatPrice(price, { currency }) as string}
81
- </span>
82
- </label>
83
- );
84
- }
85
-
86
- export function FrequentlyBoughtTogether({
87
- items,
88
- currentProduct,
89
- className,
90
- }: FrequentlyBoughtTogetherProps) {
91
- const { storeInfo } = useStoreInfo();
92
- const { refreshCart } = useCart();
93
- const t = useTranslations('productDetail');
94
-
95
- // Only show up to 3 cross-sells
96
- const crossSells = items.slice(0, 3);
97
-
98
- const [selected, setSelected] = useState<Set<string>>(() => new Set(crossSells.map((i) => i.id)));
99
- const [adding, setAdding] = useState(false);
100
-
101
- if (!storeInfo?.upsell?.frequentlyBoughtTogetherEnabled) return null;
102
- if (crossSells.length === 0) return null;
103
-
104
- const currency = storeInfo.currency || process.env.NEXT_PUBLIC_STORE_CURRENCY || 'USD';
105
-
106
- const currentPrice = getEffectivePrice(currentProduct);
107
- const currentImage = currentProduct.images?.[0];
108
- const currentImageUrl = currentImage
109
- ? typeof currentImage === 'string'
110
- ? currentImage
111
- : currentImage.url
112
- : null;
113
-
114
- const totalPrice = crossSells
115
- .filter((item) => selected.has(item.id))
116
- .reduce((sum, item) => sum + getEffectivePrice(item), currentPrice);
117
-
118
- const toggleItem = (id: string) => {
119
- setSelected((prev) => {
120
- const next = new Set(prev);
121
- if (next.has(id)) {
122
- next.delete(id);
123
- } else {
124
- next.add(id);
125
- }
126
- return next;
127
- });
128
- };
129
-
130
- async function handleAddAll() {
131
- if (adding || selected.size === 0) return;
132
- try {
133
- setAdding(true);
134
- const { getClient } = await import('@/lib/brainerce');
135
- const client = getClient();
136
- const selectedItems = crossSells.filter((item) => selected.has(item.id));
137
- for (const item of selectedItems) {
138
- await client.smartAddToCart({ productId: item.id, quantity: 1 });
139
- }
140
- await refreshCart();
141
- } catch (err) {
142
- console.error('Failed to add items to cart:', err);
143
- } finally {
144
- setAdding(false);
145
- }
146
- }
147
-
148
- return (
149
- <div className={cn('border-border rounded-lg border p-6', className)}>
150
- <h2 className="text-foreground mb-4 text-xl font-semibold">
151
- {t('frequentlyBoughtTogether')}
152
- </h2>
153
-
154
- <div className="flex flex-wrap items-center gap-3">
155
- {/* Current product (always included, no checkbox) */}
156
- <ProductThumb
157
- name={currentProduct.name}
158
- imageUrl={currentImageUrl}
159
- price={currentPrice}
160
- currency={currency}
161
- checked={true}
162
- disabled
163
- />
164
-
165
- {crossSells.map((item) => {
166
- const img = item.images?.[0];
167
- const imgUrl = img ? (typeof img === 'string' ? img : img.url) : null;
168
- return (
169
- <div key={item.id} className="flex items-center gap-3">
170
- <span className="text-muted-foreground text-lg font-light">+</span>
171
- <ProductThumb
172
- name={item.name}
173
- imageUrl={imgUrl}
174
- price={getEffectivePrice(item)}
175
- currency={currency}
176
- checked={selected.has(item.id)}
177
- onToggle={() => toggleItem(item.id)}
178
- />
179
- </div>
180
- );
181
- })}
182
- </div>
183
-
184
- {/* Total + Add button */}
185
- <div className="mt-4 flex flex-wrap items-center gap-4">
186
- <span className="text-foreground text-lg font-semibold">
187
- {t('totalPrice', { price: formatPrice(totalPrice, { currency }) as string })}
188
- </span>
189
- <button
190
- onClick={handleAddAll}
191
- disabled={adding || selected.size === 0}
192
- className={cn(
193
- 'bg-primary text-primary-foreground rounded px-5 py-2.5 text-sm font-medium transition-opacity hover:opacity-90',
194
- 'disabled:cursor-not-allowed disabled:opacity-50'
195
- )}
196
- >
197
- {adding ? t('addingAll') : t('addSelectedToCart')}
198
- </button>
199
- </div>
200
- </div>
201
- );
202
- }
1
+ 'use client';
2
+
3
+ import { useState } from 'react';
4
+ import Image from 'next/image';
5
+ import type { Product, ProductRecommendation } from 'brainerce';
6
+ import { formatPrice } from 'brainerce';
7
+ import { useCart, useStoreInfo } from '@/providers/store-provider';
8
+ import { useCurrency } from '@/lib/use-currency';
9
+ import { useTranslations } from '@/lib/translations';
10
+ import { cn } from '@/lib/utils';
11
+
12
+ interface FrequentlyBoughtTogetherProps {
13
+ items: ProductRecommendation[];
14
+ currentProduct: Product;
15
+ className?: string;
16
+ }
17
+
18
+ function getEffectivePrice(item: { basePrice: string; salePrice?: string | null }): number {
19
+ const sale = item.salePrice ? parseFloat(item.salePrice) : null;
20
+ const base = parseFloat(item.basePrice);
21
+ return sale != null && sale < base ? sale : base;
22
+ }
23
+
24
+ function ProductThumb({
25
+ name,
26
+ imageUrl,
27
+ price,
28
+ currency,
29
+ checked,
30
+ onToggle,
31
+ disabled,
32
+ }: {
33
+ name: string;
34
+ imageUrl: string | null;
35
+ price: number;
36
+ currency: string;
37
+ checked: boolean;
38
+ onToggle?: () => void;
39
+ disabled?: boolean;
40
+ }) {
41
+ return (
42
+ <label
43
+ className={cn(
44
+ 'border-border bg-background relative flex cursor-pointer flex-col items-center rounded-lg border p-3 transition-all',
45
+ checked ? 'ring-primary ring-2' : 'opacity-60',
46
+ disabled && 'pointer-events-none'
47
+ )}
48
+ >
49
+ {onToggle && (
50
+ <input
51
+ type="checkbox"
52
+ checked={checked}
53
+ onChange={onToggle}
54
+ className="absolute start-2 top-2 h-4 w-4 rounded"
55
+ />
56
+ )}
57
+ <div className="bg-muted relative mb-2 h-20 w-20 overflow-hidden rounded">
58
+ {imageUrl ? (
59
+ <Image src={imageUrl} alt={name} fill sizes="80px" className="object-cover" />
60
+ ) : (
61
+ <div className="flex h-full w-full items-center justify-center">
62
+ <svg
63
+ className="text-muted-foreground h-8 w-8"
64
+ fill="none"
65
+ viewBox="0 0 24 24"
66
+ stroke="currentColor"
67
+ >
68
+ <path
69
+ strokeLinecap="round"
70
+ strokeLinejoin="round"
71
+ strokeWidth={1.5}
72
+ 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"
73
+ />
74
+ </svg>
75
+ </div>
76
+ )}
77
+ </div>
78
+ <span className="text-foreground line-clamp-2 text-center text-xs font-medium">{name}</span>
79
+ <span className="text-muted-foreground mt-1 text-xs">
80
+ {formatPrice(price, { currency }) as string}
81
+ </span>
82
+ </label>
83
+ );
84
+ }
85
+
86
+ export function FrequentlyBoughtTogether({
87
+ items,
88
+ currentProduct,
89
+ className,
90
+ }: FrequentlyBoughtTogetherProps) {
91
+ // Hooks must be called unconditionally and in the same order on every
92
+ // render keep all of them above any early `return null` branch.
93
+ const { storeInfo } = useStoreInfo();
94
+ const { refreshCart } = useCart();
95
+ const t = useTranslations('productDetail');
96
+ const currency = useCurrency();
97
+
98
+ // Only show up to 3 cross-sells
99
+ const crossSells = items.slice(0, 3);
100
+
101
+ const [selected, setSelected] = useState<Set<string>>(() => new Set(crossSells.map((i) => i.id)));
102
+ const [adding, setAdding] = useState(false);
103
+
104
+ if (!storeInfo?.upsell?.frequentlyBoughtTogetherEnabled) return null;
105
+ if (crossSells.length === 0) return null;
106
+
107
+ const currentPrice = getEffectivePrice(currentProduct);
108
+ const currentImage = currentProduct.images?.[0];
109
+ const currentImageUrl = currentImage
110
+ ? typeof currentImage === 'string'
111
+ ? currentImage
112
+ : currentImage.url
113
+ : null;
114
+
115
+ const totalPrice = crossSells
116
+ .filter((item) => selected.has(item.id))
117
+ .reduce((sum, item) => sum + getEffectivePrice(item), currentPrice);
118
+
119
+ const toggleItem = (id: string) => {
120
+ setSelected((prev) => {
121
+ const next = new Set(prev);
122
+ if (next.has(id)) {
123
+ next.delete(id);
124
+ } else {
125
+ next.add(id);
126
+ }
127
+ return next;
128
+ });
129
+ };
130
+
131
+ async function handleAddAll() {
132
+ if (adding || selected.size === 0) return;
133
+ try {
134
+ setAdding(true);
135
+ const { getClient } = await import('@/lib/brainerce');
136
+ const client = getClient();
137
+ const selectedItems = crossSells.filter((item) => selected.has(item.id));
138
+ for (const item of selectedItems) {
139
+ await client.smartAddToCart({ productId: item.id, quantity: 1 });
140
+ }
141
+ await refreshCart();
142
+ } catch (err) {
143
+ console.error('Failed to add items to cart:', err);
144
+ } finally {
145
+ setAdding(false);
146
+ }
147
+ }
148
+
149
+ return (
150
+ <div className={cn('border-border rounded-lg border p-6', className)}>
151
+ <h2 className="text-foreground mb-4 text-xl font-semibold">
152
+ {t('frequentlyBoughtTogether')}
153
+ </h2>
154
+
155
+ <div className="flex flex-wrap items-center gap-3">
156
+ {/* Current product (always included, no checkbox) */}
157
+ <ProductThumb
158
+ name={currentProduct.name}
159
+ imageUrl={currentImageUrl}
160
+ price={currentPrice}
161
+ currency={currency}
162
+ checked={true}
163
+ disabled
164
+ />
165
+
166
+ {crossSells.map((item) => {
167
+ const img = item.images?.[0];
168
+ const imgUrl = img ? (typeof img === 'string' ? img : img.url) : null;
169
+ return (
170
+ <div key={item.id} className="flex items-center gap-3">
171
+ <span className="text-muted-foreground text-lg font-light">+</span>
172
+ <ProductThumb
173
+ name={item.name}
174
+ imageUrl={imgUrl}
175
+ price={getEffectivePrice(item)}
176
+ currency={currency}
177
+ checked={selected.has(item.id)}
178
+ onToggle={() => toggleItem(item.id)}
179
+ />
180
+ </div>
181
+ );
182
+ })}
183
+ </div>
184
+
185
+ {/* Total + Add button */}
186
+ <div className="mt-4 flex flex-wrap items-center gap-4">
187
+ <span className="text-foreground text-lg font-semibold">
188
+ {t('totalPrice', { price: formatPrice(totalPrice, { currency }) as string })}
189
+ </span>
190
+ <button
191
+ onClick={handleAddAll}
192
+ disabled={adding || selected.size === 0}
193
+ className={cn(
194
+ 'bg-primary text-primary-foreground rounded px-5 py-2.5 text-sm font-medium transition-opacity hover:opacity-90',
195
+ 'disabled:cursor-not-allowed disabled:opacity-50'
196
+ )}
197
+ >
198
+ {adding ? t('addingAll') : t('addSelectedToCart')}
199
+ </button>
200
+ </div>
201
+ </div>
202
+ );
203
+ }