create-brainerce-store 1.28.20 → 1.29.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.
@@ -0,0 +1,478 @@
1
+ 'use client';
2
+
3
+ import { useState } from 'react';
4
+ import type { ProductCustomizationField } from 'brainerce';
5
+ import { useTranslations } from '@/lib/translations';
6
+ import { cn } from '@/lib/utils';
7
+ import { LoadingSpinner } from '@/components/shared/loading-spinner';
8
+
9
+ export type CustomizationValues = Record<string, unknown>;
10
+
11
+ interface CustomizationFieldsProps {
12
+ fields: ProductCustomizationField[];
13
+ values: CustomizationValues;
14
+ onChange: (values: CustomizationValues) => void;
15
+ errors?: Record<string, string>;
16
+ }
17
+
18
+ export function CustomizationFields({
19
+ fields,
20
+ values,
21
+ onChange,
22
+ errors = {},
23
+ }: CustomizationFieldsProps) {
24
+ const t = useTranslations('customization');
25
+
26
+ if (fields.length === 0) return null;
27
+
28
+ const sorted = [...fields].sort((a, b) => a.position - b.position);
29
+
30
+ const setValue = (key: string, value: unknown) => {
31
+ onChange({ ...values, [key]: value });
32
+ };
33
+
34
+ return (
35
+ <div className="border-border space-y-4 border-t pt-4">
36
+ <h2 className="text-foreground text-lg font-semibold">{t('title')}</h2>
37
+ <div className="space-y-4">
38
+ {sorted.map((field) => (
39
+ <CustomizationFieldRow
40
+ key={field.definitionId}
41
+ field={field}
42
+ value={values[field.key]}
43
+ error={errors[field.key]}
44
+ onChange={(v) => setValue(field.key, v)}
45
+ />
46
+ ))}
47
+ </div>
48
+ </div>
49
+ );
50
+ }
51
+
52
+ interface RowProps {
53
+ field: ProductCustomizationField;
54
+ value: unknown;
55
+ error?: string;
56
+ onChange: (value: unknown) => void;
57
+ }
58
+
59
+ function CustomizationFieldRow({ field, value, error, onChange }: RowProps) {
60
+ const t = useTranslations('customization');
61
+ const labelText = (
62
+ <span className="text-foreground mb-1.5 block text-sm font-medium">
63
+ {field.name}
64
+ {field.required && <span className="text-destructive ms-1">*</span>}
65
+ </span>
66
+ );
67
+
68
+ const help = field.description ? (
69
+ <p className="text-muted-foreground mt-1 text-xs">{field.description}</p>
70
+ ) : null;
71
+
72
+ const errorHint = error ? <p className="text-destructive mt-1 text-xs">{error}</p> : null;
73
+
74
+ const inputBase =
75
+ 'border-border bg-background focus:border-primary w-full rounded border px-3 py-2 text-sm outline-none focus:ring-1';
76
+
77
+ switch (field.type) {
78
+ case 'TEXTAREA':
79
+ return (
80
+ <div>
81
+ {labelText}
82
+ <textarea
83
+ value={typeof value === 'string' ? value : ''}
84
+ onChange={(e) => onChange(e.target.value)}
85
+ minLength={field.minLength ?? undefined}
86
+ maxLength={field.maxLength ?? undefined}
87
+ required={field.required}
88
+ rows={3}
89
+ className={cn(inputBase, 'resize-y')}
90
+ />
91
+ {help}
92
+ {errorHint}
93
+ </div>
94
+ );
95
+
96
+ case 'NUMBER':
97
+ return (
98
+ <div>
99
+ {labelText}
100
+ <input
101
+ type="number"
102
+ value={typeof value === 'number' ? value : ''}
103
+ onChange={(e) => onChange(e.target.value === '' ? undefined : Number(e.target.value))}
104
+ min={field.minValue ?? undefined}
105
+ max={field.maxValue ?? undefined}
106
+ required={field.required}
107
+ className={inputBase}
108
+ />
109
+ {help}
110
+ {errorHint}
111
+ </div>
112
+ );
113
+
114
+ case 'BOOLEAN':
115
+ return (
116
+ <div>
117
+ <label className="inline-flex items-center gap-2">
118
+ <input
119
+ type="checkbox"
120
+ checked={value === true}
121
+ onChange={(e) => onChange(e.target.checked)}
122
+ className="h-4 w-4"
123
+ />
124
+ <span className="text-foreground text-sm font-medium">
125
+ {field.name}
126
+ {field.required && <span className="text-destructive ms-1">*</span>}
127
+ </span>
128
+ </label>
129
+ {help}
130
+ {errorHint}
131
+ </div>
132
+ );
133
+
134
+ case 'DATE':
135
+ return (
136
+ <div>
137
+ {labelText}
138
+ <input
139
+ type="date"
140
+ value={typeof value === 'string' ? value : ''}
141
+ onChange={(e) => onChange(e.target.value)}
142
+ required={field.required}
143
+ className={inputBase}
144
+ />
145
+ {help}
146
+ {errorHint}
147
+ </div>
148
+ );
149
+
150
+ case 'DATETIME':
151
+ return (
152
+ <div>
153
+ {labelText}
154
+ <input
155
+ type="datetime-local"
156
+ value={typeof value === 'string' ? value : ''}
157
+ onChange={(e) => onChange(e.target.value)}
158
+ required={field.required}
159
+ className={inputBase}
160
+ />
161
+ {help}
162
+ {errorHint}
163
+ </div>
164
+ );
165
+
166
+ case 'COLOR':
167
+ return (
168
+ <div>
169
+ {labelText}
170
+ <input
171
+ type="color"
172
+ value={typeof value === 'string' && value ? value : '#000000'}
173
+ onChange={(e) => onChange(e.target.value)}
174
+ required={field.required}
175
+ className="border-border h-10 w-20 rounded border p-1"
176
+ />
177
+ {help}
178
+ {errorHint}
179
+ </div>
180
+ );
181
+
182
+ case 'SELECT': {
183
+ const options = field.enumValues ?? [];
184
+ return (
185
+ <div>
186
+ {labelText}
187
+ <select
188
+ value={typeof value === 'string' ? value : ''}
189
+ onChange={(e) => onChange(e.target.value || undefined)}
190
+ required={field.required}
191
+ className={inputBase}
192
+ >
193
+ <option value="">{t('selectPlaceholder')}</option>
194
+ {options.map((opt) => (
195
+ <option key={opt} value={opt}>
196
+ {opt}
197
+ </option>
198
+ ))}
199
+ </select>
200
+ {help}
201
+ {errorHint}
202
+ </div>
203
+ );
204
+ }
205
+
206
+ case 'MULTI_SELECT': {
207
+ const options = field.enumValues ?? [];
208
+ const current = Array.isArray(value) ? (value as string[]) : [];
209
+ const toggle = (opt: string) => {
210
+ onChange(current.includes(opt) ? current.filter((v) => v !== opt) : [...current, opt]);
211
+ };
212
+ return (
213
+ <div>
214
+ {labelText}
215
+ <div className="space-y-1.5">
216
+ {options.map((opt) => (
217
+ <label key={opt} className="me-4 inline-flex items-center gap-2">
218
+ <input
219
+ type="checkbox"
220
+ checked={current.includes(opt)}
221
+ onChange={() => toggle(opt)}
222
+ className="h-4 w-4"
223
+ />
224
+ <span className="text-foreground text-sm">{opt}</span>
225
+ </label>
226
+ ))}
227
+ </div>
228
+ {help}
229
+ {errorHint}
230
+ </div>
231
+ );
232
+ }
233
+
234
+ case 'IMAGE':
235
+ return (
236
+ <SingleImageField
237
+ field={field}
238
+ value={typeof value === 'string' ? value : undefined}
239
+ onChange={onChange}
240
+ labelText={labelText}
241
+ help={help}
242
+ errorHint={errorHint}
243
+ />
244
+ );
245
+
246
+ case 'GALLERY':
247
+ return (
248
+ <GalleryField
249
+ field={field}
250
+ value={Array.isArray(value) ? (value as string[]) : []}
251
+ onChange={onChange}
252
+ labelText={labelText}
253
+ help={help}
254
+ errorHint={errorHint}
255
+ />
256
+ );
257
+
258
+ case 'URL':
259
+ return (
260
+ <div>
261
+ {labelText}
262
+ <input
263
+ type="url"
264
+ value={typeof value === 'string' ? value : ''}
265
+ onChange={(e) => onChange(e.target.value)}
266
+ required={field.required}
267
+ className={inputBase}
268
+ />
269
+ {help}
270
+ {errorHint}
271
+ </div>
272
+ );
273
+
274
+ default:
275
+ return (
276
+ <div>
277
+ {labelText}
278
+ <input
279
+ type="text"
280
+ value={typeof value === 'string' ? value : ''}
281
+ onChange={(e) => onChange(e.target.value)}
282
+ minLength={field.minLength ?? undefined}
283
+ maxLength={field.maxLength ?? undefined}
284
+ required={field.required}
285
+ className={inputBase}
286
+ />
287
+ {help}
288
+ {errorHint}
289
+ </div>
290
+ );
291
+ }
292
+ }
293
+
294
+ interface FileFieldProps {
295
+ field: ProductCustomizationField;
296
+ labelText: React.ReactNode;
297
+ help: React.ReactNode;
298
+ errorHint: React.ReactNode;
299
+ onChange: (value: unknown) => void;
300
+ }
301
+
302
+ function SingleImageField({
303
+ field,
304
+ value,
305
+ onChange,
306
+ labelText,
307
+ help,
308
+ errorHint,
309
+ }: FileFieldProps & { value?: string }) {
310
+ const t = useTranslations('customization');
311
+ const [uploading, setUploading] = useState(false);
312
+ const [uploadError, setUploadError] = useState<string | null>(null);
313
+
314
+ async function handleFile(file: File | null) {
315
+ if (!file) return;
316
+ setUploading(true);
317
+ setUploadError(null);
318
+ try {
319
+ const { getClient } = await import('@/lib/brainerce');
320
+ const client = getClient();
321
+ const result = await client.uploadCustomizationFile(file);
322
+ onChange(result.url);
323
+ } catch (err) {
324
+ console.error('Upload failed', err);
325
+ setUploadError(t('uploadFailed'));
326
+ } finally {
327
+ setUploading(false);
328
+ }
329
+ }
330
+
331
+ return (
332
+ <div>
333
+ {labelText}
334
+ <input
335
+ type="file"
336
+ accept="image/*"
337
+ onChange={(e) => handleFile(e.target.files?.[0] ?? null)}
338
+ disabled={uploading}
339
+ required={field.required && !value}
340
+ className="text-foreground text-sm"
341
+ />
342
+ {uploading && (
343
+ <p className="text-muted-foreground mt-2 inline-flex items-center gap-2 text-xs">
344
+ <LoadingSpinner size="sm" />
345
+ {t('uploading')}
346
+ </p>
347
+ )}
348
+ {value && !uploading && (
349
+ <div className="mt-2">
350
+ <img src={value} alt="" className="border-border h-20 w-20 rounded border object-cover" />
351
+ </div>
352
+ )}
353
+ {uploadError && <p className="text-destructive mt-1 text-xs">{uploadError}</p>}
354
+ {help}
355
+ {errorHint}
356
+ </div>
357
+ );
358
+ }
359
+
360
+ function GalleryField({
361
+ field,
362
+ value,
363
+ onChange,
364
+ labelText,
365
+ help,
366
+ errorHint,
367
+ }: FileFieldProps & { value: string[] }) {
368
+ const t = useTranslations('customization');
369
+ const [uploading, setUploading] = useState(false);
370
+ const [uploadError, setUploadError] = useState<string | null>(null);
371
+
372
+ async function handleFiles(fileList: FileList | null) {
373
+ if (!fileList || fileList.length === 0) return;
374
+ setUploading(true);
375
+ setUploadError(null);
376
+ try {
377
+ const { getClient } = await import('@/lib/brainerce');
378
+ const client = getClient();
379
+ const newUrls: string[] = [];
380
+ for (const file of Array.from(fileList)) {
381
+ const result = await client.uploadCustomizationFile(file);
382
+ newUrls.push(result.url);
383
+ }
384
+ onChange([...value, ...newUrls]);
385
+ } catch (err) {
386
+ console.error('Upload failed', err);
387
+ setUploadError(t('uploadFailed'));
388
+ } finally {
389
+ setUploading(false);
390
+ }
391
+ }
392
+
393
+ function removeAt(idx: number) {
394
+ onChange(value.filter((_, i) => i !== idx));
395
+ }
396
+
397
+ return (
398
+ <div>
399
+ {labelText}
400
+ <input
401
+ type="file"
402
+ accept="image/*"
403
+ multiple
404
+ onChange={(e) => handleFiles(e.target.files)}
405
+ disabled={uploading}
406
+ className="text-foreground text-sm"
407
+ />
408
+ {uploading && (
409
+ <p className="text-muted-foreground mt-2 inline-flex items-center gap-2 text-xs">
410
+ <LoadingSpinner size="sm" />
411
+ {t('uploading')}
412
+ </p>
413
+ )}
414
+ {value.length > 0 && (
415
+ <div className="mt-2 flex flex-wrap gap-2">
416
+ {value.map((url, idx) => (
417
+ <div key={`${url}-${idx}`} className="relative">
418
+ <img
419
+ src={url}
420
+ alt=""
421
+ className="border-border h-16 w-16 rounded border object-cover"
422
+ />
423
+ <button
424
+ type="button"
425
+ onClick={() => removeAt(idx)}
426
+ aria-label={t('removeImage')}
427
+ className="bg-destructive text-destructive-foreground absolute -end-1.5 -top-1.5 flex h-5 w-5 items-center justify-center rounded-full text-xs"
428
+ >
429
+ ×
430
+ </button>
431
+ </div>
432
+ ))}
433
+ </div>
434
+ )}
435
+ {uploadError && <p className="text-destructive mt-1 text-xs">{uploadError}</p>}
436
+ {help}
437
+ {errorHint}
438
+ </div>
439
+ );
440
+ }
441
+
442
+ export function validateCustomization(
443
+ fields: ProductCustomizationField[],
444
+ values: CustomizationValues
445
+ ): Record<string, string> {
446
+ const errors: Record<string, string> = {};
447
+ for (const field of fields) {
448
+ const value = values[field.key];
449
+ const empty =
450
+ value === undefined ||
451
+ value === null ||
452
+ value === '' ||
453
+ (Array.isArray(value) && value.length === 0);
454
+
455
+ if (field.required && empty) {
456
+ errors[field.key] = 'required';
457
+ continue;
458
+ }
459
+ if (empty) continue;
460
+
461
+ if (field.type === 'SELECT') {
462
+ const allowed = field.enumValues ?? [];
463
+ if (typeof value !== 'string' || !allowed.includes(value)) {
464
+ errors[field.key] = 'invalidOption';
465
+ }
466
+ }
467
+ if (field.type === 'MULTI_SELECT') {
468
+ const allowed = field.enumValues ?? [];
469
+ if (
470
+ !Array.isArray(value) ||
471
+ !(value as unknown[]).every((v) => typeof v === 'string' && allowed.includes(v as string))
472
+ ) {
473
+ errors[field.key] = 'invalidOption';
474
+ }
475
+ }
476
+ }
477
+ return errors;
478
+ }
@@ -1,45 +1,60 @@
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
- ];
13
-
14
- export function isAllowedPaymentUrl(url: string): boolean {
15
- if (!url || typeof url !== 'string') return false;
16
-
17
- let parsed: URL;
18
- try {
19
- parsed = new URL(url);
20
- } catch {
21
- return false;
22
- }
23
-
24
- if (parsed.protocol !== 'https:') return false;
25
-
26
- const hostname = parsed.hostname.toLowerCase();
27
- return ALLOWED_PAYMENT_HOSTS.some((host) => hostname === host || hostname.endsWith('.' + host));
28
- }
29
-
30
- export function safePaymentRedirect(url: string): void {
31
- if (!isAllowedPaymentUrl(url)) {
32
- throw new Error('Payment redirect URL is not in the allowlist');
33
- }
34
- if (typeof window !== 'undefined') {
35
- window.location.href = url;
36
- }
37
- }
38
-
39
- // CUID format used by Prisma for Checkout.id — c + 24 lowercase alphanumeric chars.
40
- // Allow a small range to tolerate cuid2 (slightly different length).
41
- const CHECKOUT_ID_RE = /^c[a-z0-9]{20,30}$/;
42
-
43
- export function isValidCheckoutId(id: unknown): id is string {
44
- return typeof id === 'string' && CHECKOUT_ID_RE.test(id);
45
- }
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
+ }
@@ -32,7 +32,7 @@ function buildCsp(nonce: string): string {
32
32
  "style-src 'self' 'unsafe-inline' https://cdn.meshulam.co.il",
33
33
  "img-src 'self' data: blob: https:",
34
34
  "font-src 'self' data:",
35
- "frame-src 'self' https://meshulam.co.il https://*.meshulam.co.il https://grow.link https://*.grow.link https://grow.security https://*.grow.security https://creditguard.co.il https://*.creditguard.co.il https://js.stripe.com https://hooks.stripe.com https://pay.google.com https://secure.cardcom.solutions https://checkout.stripe.com https://www.paypal.com https://www.sandbox.paypal.com",
35
+ "frame-src 'self' https://meshulam.co.il https://*.meshulam.co.il https://grow.link https://*.grow.link https://grow.security https://*.grow.security https://creditguard.co.il https://*.creditguard.co.il https://js.stripe.com https://hooks.stripe.com https://pay.google.com https://secure.cardcom.solutions https://checkout.stripe.com https://www.paypal.com https://www.sandbox.paypal.com https://*.brainerce.com",
36
36
  "connect-src 'self' https://*.meshulam.co.il https://grow.link https://*.grow.link https://*.grow.security https://pay.google.com https://*.stripe.com https://*.creditguard.co.il",
37
37
  "worker-src 'self' blob:",
38
38
  // 'self' (not 'none') so iframe-based payment providers (e.g. Cardcom)
@@ -149,7 +149,7 @@ function buildCsp(nonce: string): string {
149
149
  "style-src 'self' 'unsafe-inline' https://cdn.meshulam.co.il",
150
150
  "img-src 'self' data: blob: https:",
151
151
  "font-src 'self' data:",
152
- "frame-src 'self' https://meshulam.co.il https://*.meshulam.co.il https://grow.link https://*.grow.link https://grow.security https://*.grow.security https://creditguard.co.il https://*.creditguard.co.il https://js.stripe.com https://hooks.stripe.com https://pay.google.com https://secure.cardcom.solutions https://checkout.stripe.com https://www.paypal.com https://www.sandbox.paypal.com",
152
+ "frame-src 'self' https://meshulam.co.il https://*.meshulam.co.il https://grow.link https://*.grow.link https://grow.security https://*.grow.security https://creditguard.co.il https://*.creditguard.co.il https://js.stripe.com https://hooks.stripe.com https://pay.google.com https://secure.cardcom.solutions https://checkout.stripe.com https://www.paypal.com https://www.sandbox.paypal.com https://*.brainerce.com",
153
153
  "connect-src 'self' https://*.meshulam.co.il https://grow.link https://*.grow.link https://*.grow.security https://pay.google.com https://*.stripe.com https://*.creditguard.co.il",
154
154
  "worker-src 'self' blob:",
155
155
  // 'self' (not 'none') so iframe-based payment providers (e.g. Cardcom)
package/dist/index.d.ts DELETED
@@ -1 +0,0 @@
1
- #!/usr/bin/env node