create-brainerce-store 1.78.0 → 1.80.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 (36) hide show
  1. package/dist/index.js +27 -2
  2. package/messages/en.json +12 -3
  3. package/messages/he.json +12 -3
  4. package/package.json +10 -1
  5. package/templates/nextjs/base/.eslintrc.json +2 -0
  6. package/templates/nextjs/base/AGENTS.md.ejs +23 -5
  7. package/templates/nextjs/base/CLAUDE.md.ejs +23 -5
  8. package/templates/nextjs/base/src/app/checkout/page.tsx +19 -2
  9. package/templates/nextjs/base/src/app/order-status/page.tsx +18 -3
  10. package/templates/nextjs/base/src/app/products/[slug]/page.tsx +220 -214
  11. package/templates/nextjs/base/src/components/account/order-history.tsx +11 -4
  12. package/templates/nextjs/base/src/components/account/profile-section.tsx +7 -1
  13. package/templates/nextjs/base/src/components/auth/register-form.tsx +18 -1
  14. package/templates/nextjs/base/src/components/checkout/payment-step.tsx +14 -2
  15. package/templates/nextjs/base/src/components/tracking-bootstrap.tsx +1 -1
  16. package/templates/nextjs/base/src/core/hooks/use-product-page.ts +343 -328
  17. package/templates/nextjs/base/src/core/lib/kit.ts +88 -0
  18. package/templates/nextjs/base/src/ui/cart/gift-card-input.tsx +87 -12
  19. package/templates/nextjs/base/src/ui/layout/newsletter-signup.tsx +59 -12
  20. package/templates/nextjs/base/src/ui/product/frequently-bought-together.tsx +205 -197
  21. package/templates/nextjs/base/src/ui/product/product-card.tsx +230 -221
  22. package/templates/nextjs/base/src/ui/product/product-client-section.tsx +524 -493
  23. package/templates/nextjs/base/src/ui/product/recommendation-section.tsx +117 -108
  24. package/templates/nextjs/base/src/ui/product/stock-badge.tsx +23 -3
  25. package/templates/nextjs/designs/atelier/ui/product/frequently-bought-together.tsx +210 -202
  26. package/templates/nextjs/designs/atelier/ui/product/product-card.tsx +251 -242
  27. package/templates/nextjs/designs/atelier/ui/product/product-client-section.tsx +540 -509
  28. package/templates/nextjs/designs/atelier/ui/product/recommendation-section.tsx +110 -101
  29. package/templates/nextjs/designs/atelier/ui/product/stock-badge.tsx +22 -2
  30. package/templates/nextjs/ui-canvas/cart/gift-card-input.tsx +41 -11
  31. package/templates/nextjs/ui-canvas/layout/newsletter-signup.tsx +50 -8
  32. package/templates/nextjs/ui-canvas/product/frequently-bought-together.tsx +182 -174
  33. package/templates/nextjs/ui-canvas/product/product-card.tsx +174 -165
  34. package/templates/nextjs/ui-canvas/product/product-client-section.tsx +31 -0
  35. package/templates/nextjs/ui-canvas/product/recommendation-section.tsx +114 -105
  36. package/templates/nextjs/ui-canvas/product/stock-badge.tsx +22 -2
@@ -1,202 +1,210 @@
1
- 'use client';
2
-
3
- import { useState } from 'react';
4
- import { CdnImage as Image } from '@/ui/shared/cdn-image';
5
- import type { Product, ProductRecommendation } from 'brainerce';
6
- import { formatPrice } from 'brainerce';
7
- import { useCart, useStoreInfo } from '@/core/providers/store-provider';
8
- import { useCurrency } from '@/core/lib/use-currency';
9
- import { useTranslations } from '@/core/lib/translations';
10
- import { cn } from '@/core/lib/utils';
11
- import { IconPlus, IconBag } from '@/ui/shared/icons';
12
-
13
- interface FrequentlyBoughtTogetherProps {
14
- items: ProductRecommendation[];
15
- currentProduct: Product;
16
- className?: string;
17
- }
18
-
19
- // ⛔ STORE CURRENCY THROUGHOUT, deliberately. The cross-sells are
20
- // `ProductRecommendation`, a trimmed shape with no displayPrice/displayCurrency,
21
- // so there is nothing to convert them to. Converting only `currentProduct`
22
- // (which is a full Product and does carry FX fields) would add a euro amount to
23
- // two dollar amounts and print the result as one total. One currency wins, and
24
- // it has to be the one the cart actually charges.
25
- function getEffectivePrice(item: { basePrice: string; salePrice?: string | null }): number {
26
- const sale = item.salePrice ? parseFloat(item.salePrice) : null;
27
- const base = parseFloat(item.basePrice);
28
- return sale != null && sale < base ? sale : base;
29
- }
30
-
31
- function ProductThumb({
32
- name,
33
- imageUrl,
34
- price,
35
- currency,
36
- checked,
37
- onToggle,
38
- disabled,
39
- }: {
40
- name: string;
41
- imageUrl: string | null;
42
- price: number;
43
- currency: string;
44
- checked: boolean;
45
- onToggle?: () => void;
46
- disabled?: boolean;
47
- }) {
48
- return (
49
- <label
50
- className={cn(
51
- 'flex w-40 cursor-pointer flex-col gap-2 rounded-xl border bg-background p-3 transition-colors',
52
- checked ? 'border-primary' : 'border-border',
53
- disabled && 'cursor-default'
54
- )}
55
- >
56
- {/* `relative` + fixed box are layout-critical for next/image fill */}
57
- <span className="relative block aspect-square overflow-hidden rounded-lg bg-secondary">
58
- {imageUrl ? <Image src={imageUrl} alt={name} fill sizes="160px" className="object-cover" /> : null}
59
- {onToggle && (
60
- <input
61
- type="checkbox"
62
- checked={checked}
63
- onChange={onToggle}
64
- disabled={disabled}
65
- className="absolute end-2 top-2 h-[18px] w-[18px] accent-primary"
66
- aria-label={name}
67
- />
68
- )}
69
- </span>
70
- <span className="line-clamp-2 text-xs font-semibold leading-snug text-foreground">
71
- {name}
72
- </span>
73
- <span className="text-sm font-semibold text-foreground">
74
- {formatPrice(price, { currency }) as string}
75
- </span>
76
- </label>
77
- );
78
- }
79
-
80
- /**
81
- * "Frequently bought together" bundle on the product page: current product +
82
- * selectable cross-sells + combined total + add-all button; items come from
83
- * useProductPage().recommendations.crossSells, the feature gate from
84
- * useStoreInfo().upsell.
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('@/core/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
- <section className={cn('rounded-2xl border bg-secondary/50 p-6 sm:p-8', className)}>
151
- <h2 className="mb-6 text-2xl">{t('frequentlyBoughtTogether')}</h2>
152
-
153
- <div className="flex flex-wrap items-center gap-3">
154
- {/* Current product (always included, no checkbox) */}
155
- <ProductThumb
156
- name={currentProduct.name}
157
- imageUrl={currentImageUrl}
158
- price={currentPrice}
159
- currency={currency}
160
- checked={true}
161
- disabled
162
- />
163
-
164
- {crossSells.map((item) => {
165
- const img = item.images?.[0];
166
- const imgUrl = img ? (typeof img === 'string' ? img : img.url) : null;
167
- return (
168
- <div key={item.id} className="flex items-center gap-3">
169
- <span aria-hidden="true" className="text-muted-foreground">
170
- <IconPlus size={20} />
171
- </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-6 flex flex-wrap items-center gap-4 border-t pt-5">
187
- <span className="text-lg font-semibold text-foreground">
188
- {t('totalPrice').replace('{price}', formatPrice(totalPrice, { currency }) as string)}
189
- </span>
190
- <button
191
- type="button"
192
- onClick={handleAddAll}
193
- disabled={adding || selected.size === 0}
194
- className="btn-primary btn-md"
195
- >
196
- <IconBag size={18} />
197
- {adding ? t('addingAll') : t('addSelectedToCart')}
198
- </button>
199
- </div>
200
- </section>
201
- );
202
- }
1
+ 'use client';
2
+
3
+ import { useState } from 'react';
4
+ import { CdnImage as Image } from '@/ui/shared/cdn-image';
5
+ import type { Product, ProductRecommendation } from 'brainerce';
6
+ import { formatPrice } from 'brainerce';
7
+ import { useCart, useStoreInfo } from '@/core/providers/store-provider';
8
+ import { useCurrency } from '@/core/lib/use-currency';
9
+ import { useTranslations } from '@/core/lib/translations';
10
+ import { cn } from '@/core/lib/utils';
11
+ import { IconPlus, IconBag } from '@/ui/shared/icons';
12
+
13
+ interface FrequentlyBoughtTogetherProps {
14
+ items: ProductRecommendation[];
15
+ currentProduct: Product;
16
+ className?: string;
17
+ }
18
+
19
+ // ⛔ STORE CURRENCY THROUGHOUT, deliberately. The cross-sells are
20
+ // `ProductRecommendation`, a trimmed shape with no displayPrice/displayCurrency,
21
+ // so there is nothing to convert them to. Converting only `currentProduct`
22
+ // (which is a full Product and does carry FX fields) would add a euro amount to
23
+ // two dollar amounts and print the result as one total. One currency wins, and
24
+ // it has to be the one the cart actually charges.
25
+ function getEffectivePrice(item: { basePrice: string; salePrice?: string | null }): number {
26
+ const sale = item.salePrice ? parseFloat(item.salePrice) : null;
27
+ const base = parseFloat(item.basePrice);
28
+ return sale != null && sale < base ? sale : base;
29
+ }
30
+
31
+ function ProductThumb({
32
+ name,
33
+ imageUrl,
34
+ price,
35
+ currency,
36
+ checked,
37
+ onToggle,
38
+ disabled,
39
+ }: {
40
+ name: string;
41
+ imageUrl: string | null;
42
+ price: number;
43
+ currency: string;
44
+ checked: boolean;
45
+ onToggle?: () => void;
46
+ disabled?: boolean;
47
+ }) {
48
+ return (
49
+ <label
50
+ className={cn(
51
+ 'flex w-40 cursor-pointer flex-col gap-2 rounded-xl border bg-background p-3 transition-colors',
52
+ checked ? 'border-primary' : 'border-border',
53
+ disabled && 'cursor-default'
54
+ )}
55
+ >
56
+ {/* `relative` + fixed box are layout-critical for next/image fill */}
57
+ <span className="relative block aspect-square overflow-hidden rounded-lg bg-secondary">
58
+ {imageUrl ? <Image src={imageUrl} alt={name} fill sizes="160px" className="object-cover" /> : null}
59
+ {onToggle && (
60
+ <input
61
+ type="checkbox"
62
+ checked={checked}
63
+ onChange={onToggle}
64
+ disabled={disabled}
65
+ className="absolute end-2 top-2 h-[18px] w-[18px] accent-primary"
66
+ aria-label={name}
67
+ />
68
+ )}
69
+ </span>
70
+ <span className="line-clamp-2 text-xs font-semibold leading-snug text-foreground">
71
+ {name}
72
+ </span>
73
+ <span className="text-sm font-semibold text-foreground">
74
+ {formatPrice(price, { currency }) as string}
75
+ </span>
76
+ </label>
77
+ );
78
+ }
79
+
80
+ /**
81
+ * "Frequently bought together" bundle on the product page: current product +
82
+ * selectable cross-sells + combined total + add-all button; items come from
83
+ * useProductPage().recommendations.crossSells, the feature gate from
84
+ * useStoreInfo().upsell.
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
+ // KITs are dropped, not priced. A recommendation is a `ProductRecommendation`
99
+ // an UNRESOLVED list shape, and the recommendations endpoint does not resolve
100
+ // kits, so `item.basePrice` on a kit is the stored PLACEHOLDER, wrong for every
101
+ // kit priced SUM / SUM_MINUS_PERCENT. This block sums those numbers into one
102
+ // "Total" beside a button that adds them all, so a wrong figure here is a
103
+ // number the shopper commits to. Adding a kit by its own `productId` would in
104
+ // fact be correct — it is the displayed price that cannot be trusted — so a
105
+ // kit still sells fine from its own product page, where the API resolves it.
106
+ // Only show up to 3 cross-sells.
107
+ const crossSells = items.filter((item) => item.type !== 'KIT').slice(0, 3);
108
+
109
+ const [selected, setSelected] = useState<Set<string>>(() => new Set(crossSells.map((i) => i.id)));
110
+ const [adding, setAdding] = useState(false);
111
+
112
+ if (!storeInfo?.upsell?.frequentlyBoughtTogetherEnabled) return null;
113
+ if (crossSells.length === 0) return null;
114
+
115
+ const currentPrice = getEffectivePrice(currentProduct);
116
+ const currentImage = currentProduct.images?.[0];
117
+ const currentImageUrl = currentImage
118
+ ? typeof currentImage === 'string'
119
+ ? currentImage
120
+ : currentImage.url
121
+ : null;
122
+
123
+ const totalPrice = crossSells
124
+ .filter((item) => selected.has(item.id))
125
+ .reduce((sum, item) => sum + getEffectivePrice(item), currentPrice);
126
+
127
+ const toggleItem = (id: string) => {
128
+ setSelected((prev) => {
129
+ const next = new Set(prev);
130
+ if (next.has(id)) {
131
+ next.delete(id);
132
+ } else {
133
+ next.add(id);
134
+ }
135
+ return next;
136
+ });
137
+ };
138
+
139
+ async function handleAddAll() {
140
+ if (adding || selected.size === 0) return;
141
+ try {
142
+ setAdding(true);
143
+ const { getClient } = await import('@/core/lib/brainerce');
144
+ const client = getClient();
145
+ const selectedItems = crossSells.filter((item) => selected.has(item.id));
146
+ for (const item of selectedItems) {
147
+ await client.smartAddToCart({ productId: item.id, quantity: 1 });
148
+ }
149
+ await refreshCart();
150
+ } catch (err) {
151
+ console.error('Failed to add items to cart:', err);
152
+ } finally {
153
+ setAdding(false);
154
+ }
155
+ }
156
+
157
+ return (
158
+ <section className={cn('rounded-2xl border bg-secondary/50 p-6 sm:p-8', className)}>
159
+ <h2 className="mb-6 text-2xl">{t('frequentlyBoughtTogether')}</h2>
160
+
161
+ <div className="flex flex-wrap items-center gap-3">
162
+ {/* Current product (always included, no checkbox) */}
163
+ <ProductThumb
164
+ name={currentProduct.name}
165
+ imageUrl={currentImageUrl}
166
+ price={currentPrice}
167
+ currency={currency}
168
+ checked={true}
169
+ disabled
170
+ />
171
+
172
+ {crossSells.map((item) => {
173
+ const img = item.images?.[0];
174
+ const imgUrl = img ? (typeof img === 'string' ? img : img.url) : null;
175
+ return (
176
+ <div key={item.id} className="flex items-center gap-3">
177
+ <span aria-hidden="true" className="text-muted-foreground">
178
+ <IconPlus size={20} />
179
+ </span>
180
+ <ProductThumb
181
+ name={item.name}
182
+ imageUrl={imgUrl}
183
+ price={getEffectivePrice(item)}
184
+ currency={currency}
185
+ checked={selected.has(item.id)}
186
+ onToggle={() => toggleItem(item.id)}
187
+ />
188
+ </div>
189
+ );
190
+ })}
191
+ </div>
192
+
193
+ {/* Total + Add button */}
194
+ <div className="mt-6 flex flex-wrap items-center gap-4 border-t pt-5">
195
+ <span className="text-lg font-semibold text-foreground">
196
+ {t('totalPrice').replace('{price}', formatPrice(totalPrice, { currency }) as string)}
197
+ </span>
198
+ <button
199
+ type="button"
200
+ onClick={handleAddAll}
201
+ disabled={adding || selected.size === 0}
202
+ className="btn-primary btn-md"
203
+ >
204
+ <IconBag size={18} />
205
+ {adding ? t('addingAll') : t('addSelectedToCart')}
206
+ </button>
207
+ </div>
208
+ </section>
209
+ );
210
+ }