create-brainerce-store 1.33.2 → 1.34.2

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.
@@ -0,0 +1,242 @@
1
+ 'use client';
2
+
3
+ /**
4
+ * <ModifierGroupSelector> — reference React component for storefront authors
5
+ * (PRD §8.4).
6
+ *
7
+ * Drop-in renderer for a single modifier group (toppings, sauce, bread type, …).
8
+ * Storefront authors are encouraged to copy this file and tailor the styling /
9
+ * markup to their site — the data contract is what matters, not the markup.
10
+ *
11
+ * Behavior pinned by the contract:
12
+ * - SINGLE → renders as <input type="radio">, max picks = 1
13
+ * - MULTIPLE → renders as <input type="checkbox">, picks bounded by min..max
14
+ * - effectiveMax === 0 → group is hidden entirely (variant-disable convention)
15
+ * - available === false → modifier is disabled with "Sold out" badge
16
+ * - free counter shows "{used} of {freeQuantity} free" when freeQuantity > 0
17
+ * - defaultModifierIds + isDefault drive first-render checked state
18
+ * - client-side guards mirror min/max (UX); the SERVER is authoritative — wire
19
+ * `MODIFIER_VALIDATION_FAILED` errors back to the user when add-to-cart fails
20
+ *
21
+ * The price-delta side: this component does NOT compute totals. The server
22
+ * runs the free-allocation policy and returns the line's `unitPrice` /
23
+ * `modifiers[]` / `modifiersTotal` snapshot — render those after add-to-cart.
24
+ */
25
+
26
+ import type { Modifier, ModifierGroup, ModifierSelection } from 'brainerce';
27
+ import { AllergenChips } from './allergen-chips';
28
+
29
+ interface ModifierGroupSelectorProps {
30
+ /**
31
+ * Effective group as returned by `GET /products/:id` (overrides already
32
+ * applied — `min`, `max`, `freeQuantity`, etc. are the values the server
33
+ * will validate against for the active variant).
34
+ */
35
+ group: ModifierGroup;
36
+ /** Currently-selected modifier IDs in click-order. */
37
+ value: string[];
38
+ onChange: (next: string[]) => void;
39
+ /** Disable all controls (e.g., while add-to-cart is in flight). */
40
+ disabled?: boolean;
41
+ }
42
+
43
+ export function ModifierGroupSelector({
44
+ group,
45
+ value,
46
+ onChange,
47
+ disabled = false,
48
+ }: ModifierGroupSelectorProps) {
49
+ // PRD §7.2.2: variant-disable convention — group with effectiveMax=0 is hidden.
50
+ if (group.max === 0) return null;
51
+
52
+ const isSingle = group.selectionType === 'SINGLE';
53
+ // Client-side bound — server is authoritative.
54
+ const effectiveMax = isSingle ? 1 : (group.max ?? Infinity);
55
+
56
+ const handleToggle = (modifierId: string, checked: boolean) => {
57
+ if (disabled) return;
58
+
59
+ if (isSingle) {
60
+ // Radio behavior: replace the selection.
61
+ onChange(checked ? [modifierId] : []);
62
+ return;
63
+ }
64
+
65
+ if (checked) {
66
+ if (value.length >= effectiveMax) return; // would exceed max — ignore the click
67
+ if (value.includes(modifierId)) return;
68
+ onChange([...value, modifierId]);
69
+ } else {
70
+ onChange(value.filter((id) => id !== modifierId));
71
+ }
72
+ };
73
+
74
+ const sortedModifiers = [...group.modifiers].sort((a, b) => a.position - b.position);
75
+
76
+ // Counter: how many of the picked items are within the "free" window.
77
+ const usedFreeSlots = Math.min(value.length, group.freeQuantity);
78
+ const showFreeCounter = group.freeQuantity > 0;
79
+
80
+ return (
81
+ <fieldset className="modifier-group">
82
+ <legend className="modifier-group__legend">
83
+ <span className="modifier-group__name">{group.name}</span>
84
+ {group.required && <span className="modifier-group__required"> *</span>}
85
+ <span className="modifier-group__rules">{ruleSummary(group)}</span>
86
+ </legend>
87
+
88
+ {group.description && <p className="modifier-group__description">{group.description}</p>}
89
+
90
+ {showFreeCounter && (
91
+ <p className="modifier-group__counter" aria-live="polite">
92
+ {usedFreeSlots} of {group.freeQuantity} free
93
+ </p>
94
+ )}
95
+
96
+ <ul className="modifier-group__options">
97
+ {sortedModifiers.map((modifier) => {
98
+ const isChecked = value.includes(modifier.id);
99
+ const isAtCap = !isSingle && !isChecked && value.length >= effectiveMax;
100
+ const isDisabled = disabled || !modifier.available || isAtCap;
101
+
102
+ return (
103
+ <li key={modifier.id} className="modifier-option">
104
+ <label className="modifier-option__label">
105
+ <input
106
+ type={isSingle ? 'radio' : 'checkbox'}
107
+ name={`modifier-group-${group.id}`}
108
+ value={modifier.id}
109
+ checked={isChecked}
110
+ disabled={isDisabled}
111
+ onChange={(e) => handleToggle(modifier.id, e.target.checked)}
112
+ className="modifier-option__input"
113
+ />
114
+ <span className="modifier-option__name">{modifier.name}</span>
115
+ <ModifierPriceLabel modifier={modifier} />
116
+ {!modifier.available && (
117
+ <span
118
+ className="modifier-option__badge modifier-option__badge--soldout"
119
+ aria-label="Sold out"
120
+ >
121
+ Sold out
122
+ </span>
123
+ )}
124
+ {modifier.allergens && modifier.allergens.length > 0 && (
125
+ <AllergenChips allergens={modifier.allergens} />
126
+ )}
127
+ </label>
128
+ {modifier.description && (
129
+ <p className="modifier-option__description">{modifier.description}</p>
130
+ )}
131
+ </li>
132
+ );
133
+ })}
134
+ </ul>
135
+ </fieldset>
136
+ );
137
+ }
138
+
139
+ /**
140
+ * Renders the modifier's price contribution next to its name.
141
+ *
142
+ * Negative deltas display with a leading minus sign (downsell modifiers per
143
+ * PRD §6.3.4); zero-delta modifiers render nothing — silence is the right
144
+ * affordance for a free option.
145
+ */
146
+ function ModifierPriceLabel({ modifier }: { modifier: Modifier }) {
147
+ const delta = parseFloat(modifier.priceDelta);
148
+ if (Number.isNaN(delta) || delta === 0) return null;
149
+ const sign = delta > 0 ? '+' : '';
150
+ return (
151
+ <span className="modifier-option__price">
152
+ {sign}
153
+ {modifier.priceDelta}
154
+ </span>
155
+ );
156
+ }
157
+
158
+ function ruleSummary(group: ModifierGroup): string {
159
+ const parts: string[] = [];
160
+ if (group.selectionType === 'SINGLE') {
161
+ parts.push('Pick one');
162
+ } else if (group.max != null) {
163
+ if (group.min === 0) parts.push(`Up to ${group.max}`);
164
+ else if (group.min === group.max) parts.push(`Pick exactly ${group.min}`);
165
+ else parts.push(`${group.min}–${group.max}`);
166
+ } else if (group.min > 0) {
167
+ parts.push(`At least ${group.min}`);
168
+ }
169
+ return parts.length ? `(${parts.join(', ')})` : '';
170
+ }
171
+
172
+ /**
173
+ * Lightweight client-side validation that mirrors the server's checks. Use
174
+ * this to gate the add-to-cart button — but always treat the server's 400
175
+ * (`MODIFIER_VALIDATION_FAILED`) as the authoritative answer.
176
+ */
177
+ export function validateSelections(
178
+ groups: ModifierGroup[],
179
+ selections: Record<string, string[]>
180
+ ): string | null {
181
+ for (const group of groups) {
182
+ if (group.max === 0) continue; // disabled-for-variant — skip
183
+ const picks = selections[group.id] ?? [];
184
+ if (group.required && picks.length === 0) {
185
+ return `${group.name} is required`;
186
+ }
187
+ if (picks.length < group.min) {
188
+ return `Pick at least ${group.min} from ${group.name}`;
189
+ }
190
+ if (group.max != null && picks.length > group.max) {
191
+ return `Pick at most ${group.max} from ${group.name}`;
192
+ }
193
+ if (group.selectionType === 'SINGLE' && picks.length > 1) {
194
+ return `Only one option allowed in ${group.name}`;
195
+ }
196
+ }
197
+ return null;
198
+ }
199
+
200
+ /**
201
+ * Adapter: convert the local Record<groupId, modifierIds> shape used by this
202
+ * component into the wire-format `ModifierSelection[]` array the SDK expects
203
+ * on `addToCart` / `updateCartItem`.
204
+ */
205
+ export function toModifierSelections(
206
+ groups: ModifierGroup[],
207
+ selections: Record<string, string[]>
208
+ ): ModifierSelection[] {
209
+ const out: ModifierSelection[] = [];
210
+ for (const group of groups) {
211
+ if (group.max === 0) continue;
212
+ const picks = selections[group.id] ?? [];
213
+ if (picks.length === 0) continue;
214
+ out.push({ modifierGroupId: group.id, modifierIds: picks });
215
+ }
216
+ return out;
217
+ }
218
+
219
+ /**
220
+ * Build the initial selection map from the group's defaults — call once on
221
+ * first render for each group. Honors `defaultModifierIds` first (per-attach
222
+ * defaults), falling back to per-modifier `isDefault` flags. Sold-out
223
+ * modifiers are filtered out so the user doesn't start with an invalid pick.
224
+ */
225
+ export function buildInitialSelections(groups: ModifierGroup[]): Record<string, string[]> {
226
+ const out: Record<string, string[]> = {};
227
+ for (const group of groups) {
228
+ if (group.max === 0) continue;
229
+ const fromDefaults = group.defaultModifierIds ?? [];
230
+ const fromIsDefault = group.modifiers
231
+ .filter((m) => m.isDefault && m.available)
232
+ .map((m) => m.id);
233
+ const merged =
234
+ fromDefaults.length > 0
235
+ ? fromDefaults.filter((id) => group.modifiers.some((m) => m.id === id && m.available))
236
+ : fromIsDefault;
237
+ // Cap by max for SINGLE groups — defaults can't violate selection rules.
238
+ const capped = group.selectionType === 'SINGLE' ? merged.slice(0, 1) : merged;
239
+ if (capped.length > 0) out[group.id] = capped;
240
+ }
241
+ return out;
242
+ }
@@ -4,7 +4,12 @@
4
4
  * The token is managed server-side via httpOnly cookies — never exposed to JS.
5
5
  */
6
6
 
7
- const CONNECTION_ID = process.env.NEXT_PUBLIC_BRAINERCE_CONNECTION_ID || '';
7
+ // Read either env var name. The new one is preferred; the old one is a soft
8
+ // alias kept for backwards compatibility — both are accepted by the SDK.
9
+ const CONNECTION_ID =
10
+ process.env.NEXT_PUBLIC_BRAINERCE_SALES_CHANNEL_ID ||
11
+ process.env.NEXT_PUBLIC_BRAINERCE_CONNECTION_ID ||
12
+ '';
8
13
 
9
14
  const CSRF_HEADERS: Record<string, string> = {
10
15
  'Content-Type': 'application/json',
@@ -1,6 +1,11 @@
1
1
  import { BrainerceClient } from 'brainerce';
2
2
 
3
- const CONNECTION_ID = process.env.NEXT_PUBLIC_BRAINERCE_CONNECTION_ID || '<%= connectionId %>';
3
+ // Read either env var name. The new one is preferred; the old one is a soft
4
+ // alias kept for backwards compatibility — both are accepted by the SDK.
5
+ const SALES_CHANNEL_ID =
6
+ process.env.NEXT_PUBLIC_BRAINERCE_SALES_CHANNEL_ID ||
7
+ process.env.NEXT_PUBLIC_BRAINERCE_CONNECTION_ID ||
8
+ '<%= connectionId %>';
4
9
 
5
10
  // Singleton SDK client — routes through same-origin BFF proxy for httpOnly cookie auth
6
11
  let clientInstance: BrainerceClient | null = null;
@@ -8,7 +13,7 @@ let clientInstance: BrainerceClient | null = null;
8
13
  export function getClient(): BrainerceClient {
9
14
  if (!clientInstance) {
10
15
  clientInstance = new BrainerceClient({
11
- connectionId: CONNECTION_ID,
16
+ salesChannelId: SALES_CHANNEL_ID,
12
17
  baseUrl: '/api/store', // same-origin proxy handles auth via httpOnly cookie
13
18
  proxyMode: true, // skip client-side token checks; proxy adds Authorization header
14
19
  });
@@ -52,7 +57,7 @@ export function initClient(): BrainerceClient {
52
57
  export function getServerClient(locale?: string): BrainerceClient {
53
58
  const apiUrl = process.env.BRAINERCE_API_URL || 'https://api.brainerce.com';
54
59
  const client = new BrainerceClient({
55
- connectionId: CONNECTION_ID,
60
+ salesChannelId: SALES_CHANNEL_ID,
56
61
  baseUrl: apiUrl,
57
62
  origin: process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000',
58
63
  });
@@ -1,60 +1,38 @@
1
- const ALLOWED_PAYMENT_HOSTS: readonly string[] = [
2
- 'checkout.stripe.com',
3
- 'js.stripe.com',
4
- 'hooks.stripe.com',
5
- 'www.paypal.com',
6
- 'www.sandbox.paypal.com',
7
- 'secure.cardcom.solutions',
8
- 'meshulam.co.il',
9
- 'grow.link',
10
- 'grow.security',
11
- 'creditguard.co.il',
12
- // Brainerce-hosted payment embeds (cardcom-payments /embed/:lpCode etc.).
13
- // These are platform-owned iframe shells that wrap provider-specific flows
14
- // and relay postMessage events back to the storefront.
15
- 'brainerce.com',
16
- ];
17
-
18
- export function isAllowedPaymentUrl(url: string): boolean {
19
- if (!url || typeof url !== 'string') return false;
20
-
21
- let parsed: URL;
22
- try {
23
- parsed = new URL(url);
24
- } catch {
25
- return false;
26
- }
27
-
28
- const hostname = parsed.hostname.toLowerCase();
29
-
30
- // Dev-only: allow http://localhost|127.0.0.1 so the local storefront can
31
- // iframe the local backend's embed proxy. Stripped in production builds.
32
- if (
33
- process.env.NODE_ENV !== 'production' &&
34
- parsed.protocol === 'http:' &&
35
- (hostname === 'localhost' || hostname === '127.0.0.1')
36
- ) {
37
- return true;
38
- }
39
-
40
- if (parsed.protocol !== 'https:') return false;
41
-
42
- return ALLOWED_PAYMENT_HOSTS.some((host) => hostname === host || hostname.endsWith('.' + host));
43
- }
44
-
45
- export function safePaymentRedirect(url: string): void {
46
- if (!isAllowedPaymentUrl(url)) {
47
- throw new Error('Payment redirect URL is not in the allowlist');
48
- }
49
- if (typeof window !== 'undefined') {
50
- window.location.href = url;
51
- }
52
- }
53
-
54
- // CUID format used by Prisma for Checkout.id — c + 24 lowercase alphanumeric chars.
55
- // Allow a small range to tolerate cuid2 (slightly different length).
56
- const CHECKOUT_ID_RE = /^c[a-z0-9]{20,30}$/;
57
-
58
- export function isValidCheckoutId(id: unknown): id is string {
59
- return typeof id === 'string' && CHECKOUT_ID_RE.test(id);
60
- }
1
+ // Re-export the platform-maintained payment URL allowlist from the SDK.
2
+ //
3
+ // Why a re-export and not a direct import everywhere: keeping a stable
4
+ // `@/lib/safe-redirect` import path means future SDK API changes (e.g. an
5
+ // `extraHosts` rename) only need editing here, and merchants don't have to
6
+ // touch every component that does payment redirects. It also leaves a clear
7
+ // place to plug in store-specific extras (custom self-hosted PSP) without
8
+ // scattering option objects across components.
9
+ //
10
+ // The allowlist itself now lives in the `brainerce` package — running
11
+ // `npm update brainerce` picks up new payment providers and platform
12
+ // embed domains automatically.
13
+
14
+ import {
15
+ isAllowedPaymentUrl as sdkIsAllowedPaymentUrl,
16
+ safePaymentRedirect as sdkSafePaymentRedirect,
17
+ } from 'brainerce';
18
+
19
+ // Dev-only: allow http://localhost|127.0.0.1 so the local storefront can
20
+ // iframe the local backend's embed proxy. Inlined to a literal at build time
21
+ // by Next.js, so the allow path is stripped from production bundles.
22
+ const allowLocalhost = process.env.NODE_ENV !== 'production';
23
+
24
+ export function isAllowedPaymentUrl(url: string): boolean {
25
+ return sdkIsAllowedPaymentUrl(url, { allowLocalhost });
26
+ }
27
+
28
+ export function safePaymentRedirect(url: string): void {
29
+ sdkSafePaymentRedirect(url, { allowLocalhost });
30
+ }
31
+
32
+ // CUID format used by Prisma for Checkout.id — c + 24 lowercase alphanumeric chars.
33
+ // Allow a small range to tolerate cuid2 (slightly different length).
34
+ const CHECKOUT_ID_RE = /^c[a-z0-9]{20,30}$/;
35
+
36
+ export function isValidCheckoutId(id: unknown): id is string {
37
+ return typeof id === 'string' && CHECKOUT_ID_RE.test(id);
38
+ }