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,174 +1,182 @@
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
-
12
- interface FrequentlyBoughtTogetherProps {
13
- items: ProductRecommendation[];
14
- currentProduct: Product;
15
- className?: string;
16
- }
17
-
18
- // ⛔ STORE CURRENCY THROUGHOUT, deliberately. The cross-sells are
19
- // `ProductRecommendation`, a trimmed shape with no displayPrice/displayCurrency,
20
- // so there is nothing to convert them to. Converting only `currentProduct`
21
- // (which is a full Product and does carry FX fields) would add a euro amount to
22
- // two dollar amounts and print the result as one total. One currency wins, and
23
- // it has to be the one the cart actually charges.
24
- function getEffectivePrice(item: { basePrice: string; salePrice?: string | null }): number {
25
- const sale = item.salePrice ? parseFloat(item.salePrice) : null;
26
- const base = parseFloat(item.basePrice);
27
- return sale != null && sale < base ? sale : base;
28
- }
29
-
30
- function ProductThumb({
31
- name,
32
- imageUrl,
33
- price,
34
- currency,
35
- checked,
36
- onToggle,
37
- disabled,
38
- }: {
39
- name: string;
40
- imageUrl: string | null;
41
- price: number;
42
- currency: string;
43
- checked: boolean;
44
- onToggle?: () => void;
45
- disabled?: boolean;
46
- }) {
47
- return (
48
- <label className="inline-flex items-center gap-2">
49
- {onToggle && <input type="checkbox" checked={checked} onChange={onToggle} disabled={disabled} />}
50
- {/* `relative` + fixed box are layout-critical for next/image fill */}
51
- <span className="relative block h-20 w-20">
52
- {imageUrl ? <Image src={imageUrl} alt={name} fill sizes="80px" /> : null}
53
- </span>
54
- <span>{name}</span>
55
- <span>{formatPrice(price, { currency }) as string}</span>
56
- </label>
57
- );
58
- }
59
-
60
- /**
61
- * DESIGN ME — "frequently bought together" bundle on the product page:
62
- * current product + selectable cross-sells + combined total + add-all button;
63
- * items come from useProductPage().recommendations.crossSells, the feature
64
- * gate from useStoreInfo().upsell.
65
- *
66
- * Building blocks: shadcn/ui primitives in src/components/ui (Button, Card, Badge, Input, Select, Accordion, Dialog, Skeleton, ...) + lucide-react icons are installed and ready to compose with.
67
- */
68
- export function FrequentlyBoughtTogether({
69
- items,
70
- currentProduct,
71
- className,
72
- }: FrequentlyBoughtTogetherProps) {
73
- // Hooks must be called unconditionally and in the same order on every
74
- // render — keep all of them above any early `return null` branch.
75
- const { storeInfo } = useStoreInfo();
76
- const { refreshCart } = useCart();
77
- const t = useTranslations('productDetail');
78
- const currency = useCurrency();
79
-
80
- // Only show up to 3 cross-sells
81
- const crossSells = items.slice(0, 3);
82
-
83
- const [selected, setSelected] = useState<Set<string>>(() => new Set(crossSells.map((i) => i.id)));
84
- const [adding, setAdding] = useState(false);
85
-
86
- if (!storeInfo?.upsell?.frequentlyBoughtTogetherEnabled) return null;
87
- if (crossSells.length === 0) return null;
88
-
89
- const currentPrice = getEffectivePrice(currentProduct);
90
- const currentImage = currentProduct.images?.[0];
91
- const currentImageUrl = currentImage
92
- ? typeof currentImage === 'string'
93
- ? currentImage
94
- : currentImage.url
95
- : null;
96
-
97
- const totalPrice = crossSells
98
- .filter((item) => selected.has(item.id))
99
- .reduce((sum, item) => sum + getEffectivePrice(item), currentPrice);
100
-
101
- const toggleItem = (id: string) => {
102
- setSelected((prev) => {
103
- const next = new Set(prev);
104
- if (next.has(id)) {
105
- next.delete(id);
106
- } else {
107
- next.add(id);
108
- }
109
- return next;
110
- });
111
- };
112
-
113
- async function handleAddAll() {
114
- if (adding || selected.size === 0) return;
115
- try {
116
- setAdding(true);
117
- const { getClient } = await import('@/core/lib/brainerce');
118
- const client = getClient();
119
- const selectedItems = crossSells.filter((item) => selected.has(item.id));
120
- for (const item of selectedItems) {
121
- await client.smartAddToCart({ productId: item.id, quantity: 1 });
122
- }
123
- await refreshCart();
124
- } catch (err) {
125
- console.error('Failed to add items to cart:', err);
126
- } finally {
127
- setAdding(false);
128
- }
129
- }
130
-
131
- return (
132
- <section className={cn(className)}>
133
- <h2>{t('frequentlyBoughtTogether')}</h2>
134
-
135
- <div className="flex flex-wrap items-center gap-3">
136
- {/* Current product (always included, no checkbox) */}
137
- <ProductThumb
138
- name={currentProduct.name}
139
- imageUrl={currentImageUrl}
140
- price={currentPrice}
141
- currency={currency}
142
- checked={true}
143
- disabled
144
- />
145
-
146
- {crossSells.map((item) => {
147
- const img = item.images?.[0];
148
- const imgUrl = img ? (typeof img === 'string' ? img : img.url) : null;
149
- return (
150
- <div key={item.id} className="flex items-center gap-3">
151
- <span aria-hidden="true">+</span>
152
- <ProductThumb
153
- name={item.name}
154
- imageUrl={imgUrl}
155
- price={getEffectivePrice(item)}
156
- currency={currency}
157
- checked={selected.has(item.id)}
158
- onToggle={() => toggleItem(item.id)}
159
- />
160
- </div>
161
- );
162
- })}
163
- </div>
164
-
165
- {/* Total + Add button */}
166
- <div className="flex flex-wrap items-center gap-4">
167
- <span>{t('totalPrice', { price: formatPrice(totalPrice, { currency }) as string })}</span>
168
- <button type="button" onClick={handleAddAll} disabled={adding || selected.size === 0}>
169
- {adding ? t('addingAll') : t('addSelectedToCart')}
170
- </button>
171
- </div>
172
- </section>
173
- );
174
- }
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
+
12
+ interface FrequentlyBoughtTogetherProps {
13
+ items: ProductRecommendation[];
14
+ currentProduct: Product;
15
+ className?: string;
16
+ }
17
+
18
+ // ⛔ STORE CURRENCY THROUGHOUT, deliberately. The cross-sells are
19
+ // `ProductRecommendation`, a trimmed shape with no displayPrice/displayCurrency,
20
+ // so there is nothing to convert them to. Converting only `currentProduct`
21
+ // (which is a full Product and does carry FX fields) would add a euro amount to
22
+ // two dollar amounts and print the result as one total. One currency wins, and
23
+ // it has to be the one the cart actually charges.
24
+ function getEffectivePrice(item: { basePrice: string; salePrice?: string | null }): number {
25
+ const sale = item.salePrice ? parseFloat(item.salePrice) : null;
26
+ const base = parseFloat(item.basePrice);
27
+ return sale != null && sale < base ? sale : base;
28
+ }
29
+
30
+ function ProductThumb({
31
+ name,
32
+ imageUrl,
33
+ price,
34
+ currency,
35
+ checked,
36
+ onToggle,
37
+ disabled,
38
+ }: {
39
+ name: string;
40
+ imageUrl: string | null;
41
+ price: number;
42
+ currency: string;
43
+ checked: boolean;
44
+ onToggle?: () => void;
45
+ disabled?: boolean;
46
+ }) {
47
+ return (
48
+ <label className="inline-flex items-center gap-2">
49
+ {onToggle && <input type="checkbox" checked={checked} onChange={onToggle} disabled={disabled} />}
50
+ {/* `relative` + fixed box are layout-critical for next/image fill */}
51
+ <span className="relative block h-20 w-20">
52
+ {imageUrl ? <Image src={imageUrl} alt={name} fill sizes="80px" /> : null}
53
+ </span>
54
+ <span>{name}</span>
55
+ <span>{formatPrice(price, { currency }) as string}</span>
56
+ </label>
57
+ );
58
+ }
59
+
60
+ /**
61
+ * DESIGN ME — "frequently bought together" bundle on the product page:
62
+ * current product + selectable cross-sells + combined total + add-all button;
63
+ * items come from useProductPage().recommendations.crossSells, the feature
64
+ * gate from useStoreInfo().upsell.
65
+ *
66
+ * Building blocks: shadcn/ui primitives in src/components/ui (Button, Card, Badge, Input, Select, Accordion, Dialog, Skeleton, ...) + lucide-react icons are installed and ready to compose with.
67
+ */
68
+ export function FrequentlyBoughtTogether({
69
+ items,
70
+ currentProduct,
71
+ className,
72
+ }: FrequentlyBoughtTogetherProps) {
73
+ // Hooks must be called unconditionally and in the same order on every
74
+ // render — keep all of them above any early `return null` branch.
75
+ const { storeInfo } = useStoreInfo();
76
+ const { refreshCart } = useCart();
77
+ const t = useTranslations('productDetail');
78
+ const currency = useCurrency();
79
+
80
+ // KITs are dropped, not priced. A recommendation is a `ProductRecommendation`
81
+ // an UNRESOLVED list shape, and the recommendations endpoint does not resolve
82
+ // kits, so `item.basePrice` on a kit is the stored PLACEHOLDER, wrong for every
83
+ // kit priced SUM / SUM_MINUS_PERCENT. This block sums those numbers into one
84
+ // "Total" beside a button that adds them all, so a wrong figure here is a
85
+ // number the shopper commits to. Adding a kit by its own `productId` would in
86
+ // fact be correct — it is the displayed price that cannot be trusted — so a
87
+ // kit still sells fine from its own product page, where the API resolves it.
88
+ // Only show up to 3 cross-sells.
89
+ const crossSells = items.filter((item) => item.type !== 'KIT').slice(0, 3);
90
+
91
+ const [selected, setSelected] = useState<Set<string>>(() => new Set(crossSells.map((i) => i.id)));
92
+ const [adding, setAdding] = useState(false);
93
+
94
+ if (!storeInfo?.upsell?.frequentlyBoughtTogetherEnabled) return null;
95
+ if (crossSells.length === 0) return null;
96
+
97
+ const currentPrice = getEffectivePrice(currentProduct);
98
+ const currentImage = currentProduct.images?.[0];
99
+ const currentImageUrl = currentImage
100
+ ? typeof currentImage === 'string'
101
+ ? currentImage
102
+ : currentImage.url
103
+ : null;
104
+
105
+ const totalPrice = crossSells
106
+ .filter((item) => selected.has(item.id))
107
+ .reduce((sum, item) => sum + getEffectivePrice(item), currentPrice);
108
+
109
+ const toggleItem = (id: string) => {
110
+ setSelected((prev) => {
111
+ const next = new Set(prev);
112
+ if (next.has(id)) {
113
+ next.delete(id);
114
+ } else {
115
+ next.add(id);
116
+ }
117
+ return next;
118
+ });
119
+ };
120
+
121
+ async function handleAddAll() {
122
+ if (adding || selected.size === 0) return;
123
+ try {
124
+ setAdding(true);
125
+ const { getClient } = await import('@/core/lib/brainerce');
126
+ const client = getClient();
127
+ const selectedItems = crossSells.filter((item) => selected.has(item.id));
128
+ for (const item of selectedItems) {
129
+ await client.smartAddToCart({ productId: item.id, quantity: 1 });
130
+ }
131
+ await refreshCart();
132
+ } catch (err) {
133
+ console.error('Failed to add items to cart:', err);
134
+ } finally {
135
+ setAdding(false);
136
+ }
137
+ }
138
+
139
+ return (
140
+ <section className={cn(className)}>
141
+ <h2>{t('frequentlyBoughtTogether')}</h2>
142
+
143
+ <div className="flex flex-wrap items-center gap-3">
144
+ {/* Current product (always included, no checkbox) */}
145
+ <ProductThumb
146
+ name={currentProduct.name}
147
+ imageUrl={currentImageUrl}
148
+ price={currentPrice}
149
+ currency={currency}
150
+ checked={true}
151
+ disabled
152
+ />
153
+
154
+ {crossSells.map((item) => {
155
+ const img = item.images?.[0];
156
+ const imgUrl = img ? (typeof img === 'string' ? img : img.url) : null;
157
+ return (
158
+ <div key={item.id} className="flex items-center gap-3">
159
+ <span aria-hidden="true">+</span>
160
+ <ProductThumb
161
+ name={item.name}
162
+ imageUrl={imgUrl}
163
+ price={getEffectivePrice(item)}
164
+ currency={currency}
165
+ checked={selected.has(item.id)}
166
+ onToggle={() => toggleItem(item.id)}
167
+ />
168
+ </div>
169
+ );
170
+ })}
171
+ </div>
172
+
173
+ {/* Total + Add button */}
174
+ <div className="flex flex-wrap items-center gap-4">
175
+ <span>{t('totalPrice', { price: formatPrice(totalPrice, { currency }) as string })}</span>
176
+ <button type="button" onClick={handleAddAll} disabled={adding || selected.size === 0}>
177
+ {adding ? t('addingAll') : t('addSelectedToCart')}
178
+ </button>
179
+ </div>
180
+ </section>
181
+ );
182
+ }