create-brainerce-store 1.36.0 → 1.37.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.
package/dist/index.js
CHANGED
|
@@ -31,7 +31,7 @@ var require_package = __commonJS({
|
|
|
31
31
|
"package.json"(exports2, module2) {
|
|
32
32
|
module2.exports = {
|
|
33
33
|
name: "create-brainerce-store",
|
|
34
|
-
version: "1.
|
|
34
|
+
version: "1.37.0",
|
|
35
35
|
description: "Scaffold a production-ready e-commerce storefront connected to Brainerce",
|
|
36
36
|
bin: {
|
|
37
37
|
"create-brainerce-store": "dist/index.js"
|
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import { Suspense, useEffect, useState, useCallback, useRef } from 'react';
|
|
3
|
+
import { Suspense, useEffect, useMemo, useState, useCallback, useRef } from 'react';
|
|
4
4
|
import { useSearchParams } from 'next/navigation';
|
|
5
5
|
import { useRouter } from '@/lib/navigation';
|
|
6
|
-
import type { Product } from 'brainerce';
|
|
7
|
-
import type { ProductQueryParams } from 'brainerce';
|
|
6
|
+
import type { Product, ProductQueryParams, PublicMetafieldDefinition } from 'brainerce';
|
|
8
7
|
import { getClient } from '@/lib/brainerce';
|
|
9
8
|
import { ProductGrid } from '@/components/products/product-grid';
|
|
10
9
|
import { LoadingSpinner } from '@/components/shared/loading-spinner';
|
|
@@ -13,6 +12,18 @@ import { cn } from '@/lib/utils';
|
|
|
13
12
|
|
|
14
13
|
const PAGE_SIZE = 20;
|
|
15
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Metafield (custom-field) values are passed in the URL as `mf_<key>=v1,v2`
|
|
17
|
+
* for readability (no JSON encoding). The SDK assembles them back into
|
|
18
|
+
* `{ [key]: string[] }` before calling `client.getProducts`.
|
|
19
|
+
*
|
|
20
|
+
* Only definitions with `filterable: true` of type SELECT / MULTI_SELECT /
|
|
21
|
+
* BOOLEAN are honored server-side — the storefront renders facets only for
|
|
22
|
+
* those.
|
|
23
|
+
*/
|
|
24
|
+
const METAFIELD_URL_PREFIX = 'mf_';
|
|
25
|
+
const FILTERABLE_TYPES = new Set(['SELECT', 'MULTI_SELECT', 'BOOLEAN']);
|
|
26
|
+
|
|
16
27
|
type SortOption = {
|
|
17
28
|
labelKey: 'sortNewest' | 'sortNameAZ' | 'sortNameZA' | 'sortPriceLow' | 'sortPriceHigh';
|
|
18
29
|
sortBy: ProductQueryParams['sortBy'];
|
|
@@ -239,26 +250,52 @@ function ProductsContent() {
|
|
|
239
250
|
const [categories, setCategories] = useState<CategoryNode[]>([]);
|
|
240
251
|
const [brands, setBrands] = useState<Array<{ id: string; name: string }>>([]);
|
|
241
252
|
const [tags, setTags] = useState<Array<{ id: string; name: string }>>([]);
|
|
253
|
+
const [metafieldDefs, setMetafieldDefs] = useState<PublicMetafieldDefinition[]>([]);
|
|
242
254
|
|
|
243
255
|
const sortIndex = parseInt(sortParam, 10) || 0;
|
|
244
256
|
const currentSort = sortOptions[sortIndex] || sortOptions[0];
|
|
245
257
|
|
|
246
|
-
// Load categories, brands, and
|
|
258
|
+
// Load categories, brands, tags, and metafield definitions (for custom-field facets).
|
|
259
|
+
// The metafield-definitions endpoint already returns only definitions
|
|
260
|
+
// published to this sales channel; we filter client-side to the ones flagged
|
|
261
|
+
// `filterable: true` and of a UI-renderable type.
|
|
247
262
|
useEffect(() => {
|
|
248
263
|
async function loadFilters() {
|
|
249
264
|
const client = getClient();
|
|
250
|
-
const [catRes, brandRes, tagRes] = await Promise.allSettled([
|
|
265
|
+
const [catRes, brandRes, tagRes, mfRes] = await Promise.allSettled([
|
|
251
266
|
client.getCategories(),
|
|
252
267
|
client.getBrands(),
|
|
253
268
|
client.getTags(),
|
|
269
|
+
client.getPublicMetafieldDefinitions(),
|
|
254
270
|
]);
|
|
255
271
|
if (catRes.status === 'fulfilled') setCategories(catRes.value.categories as CategoryNode[]);
|
|
256
272
|
if (brandRes.status === 'fulfilled') setBrands(brandRes.value.brands);
|
|
257
273
|
if (tagRes.status === 'fulfilled') setTags(tagRes.value.tags);
|
|
274
|
+
if (mfRes.status === 'fulfilled') {
|
|
275
|
+
setMetafieldDefs(
|
|
276
|
+
mfRes.value.definitions.filter(
|
|
277
|
+
(d) => d.filterable === true && FILTERABLE_TYPES.has(d.type)
|
|
278
|
+
)
|
|
279
|
+
);
|
|
280
|
+
}
|
|
258
281
|
}
|
|
259
282
|
loadFilters();
|
|
260
283
|
}, []);
|
|
261
284
|
|
|
285
|
+
// Stable serialized signature of the selected metafield facets — used as
|
|
286
|
+
// the `loadProducts` dependency (and to derive the SDK shape). When no
|
|
287
|
+
// facets are selected, this is `''` so the SDK call omits `metafields`
|
|
288
|
+
// entirely and the backend short-circuits the filter branch.
|
|
289
|
+
const metafieldsKey = useMemo(() => {
|
|
290
|
+
if (metafieldDefs.length === 0) return '';
|
|
291
|
+
const parts: string[] = [];
|
|
292
|
+
for (const def of metafieldDefs) {
|
|
293
|
+
const raw = searchParams.get(`${METAFIELD_URL_PREFIX}${def.key}`);
|
|
294
|
+
if (raw) parts.push(`${def.key}=${raw}`);
|
|
295
|
+
}
|
|
296
|
+
return parts.sort().join('&');
|
|
297
|
+
}, [metafieldDefs, searchParams]);
|
|
298
|
+
|
|
262
299
|
// Load products when filters change
|
|
263
300
|
const loadProducts = useCallback(
|
|
264
301
|
async (pageNum: number, append: boolean) => {
|
|
@@ -281,6 +318,24 @@ function ProductsContent() {
|
|
|
281
318
|
if (categoryId) params.categories = categoryId;
|
|
282
319
|
if (brandId) params.brands = brandId;
|
|
283
320
|
if (tagId) params.tags = tagId;
|
|
321
|
+
// Build the `metafields` shape from `metafieldsKey` (each segment is
|
|
322
|
+
// `key=v1,v2`). Equivalent to reading the URL params directly but
|
|
323
|
+
// tied to the stable signature already in this callback's deps.
|
|
324
|
+
if (metafieldsKey) {
|
|
325
|
+
const mf: Record<string, string[]> = {};
|
|
326
|
+
for (const segment of metafieldsKey.split('&')) {
|
|
327
|
+
const eq = segment.indexOf('=');
|
|
328
|
+
if (eq < 0) continue;
|
|
329
|
+
const k = segment.slice(0, eq);
|
|
330
|
+
const v = segment
|
|
331
|
+
.slice(eq + 1)
|
|
332
|
+
.split(',')
|
|
333
|
+
.map((s) => s.trim())
|
|
334
|
+
.filter(Boolean);
|
|
335
|
+
if (v.length > 0) mf[k] = v;
|
|
336
|
+
}
|
|
337
|
+
if (Object.keys(mf).length > 0) params.metafields = mf;
|
|
338
|
+
}
|
|
284
339
|
|
|
285
340
|
const result = await client.getProducts(params);
|
|
286
341
|
|
|
@@ -299,7 +354,15 @@ function ProductsContent() {
|
|
|
299
354
|
setLoadingMore(false);
|
|
300
355
|
}
|
|
301
356
|
},
|
|
302
|
-
[
|
|
357
|
+
[
|
|
358
|
+
searchQuery,
|
|
359
|
+
categoryId,
|
|
360
|
+
brandId,
|
|
361
|
+
tagId,
|
|
362
|
+
currentSort.sortBy,
|
|
363
|
+
currentSort.sortOrder,
|
|
364
|
+
metafieldsKey,
|
|
365
|
+
]
|
|
303
366
|
);
|
|
304
367
|
|
|
305
368
|
useEffect(() => {
|
|
@@ -326,6 +389,24 @@ function ProductsContent() {
|
|
|
326
389
|
updateParam('category', id);
|
|
327
390
|
}
|
|
328
391
|
|
|
392
|
+
/** Read the currently-selected values for a given metafield definition. */
|
|
393
|
+
function getSelectedMetafieldValues(key: string): string[] {
|
|
394
|
+
const raw = searchParams.get(`${METAFIELD_URL_PREFIX}${key}`);
|
|
395
|
+
if (!raw) return [];
|
|
396
|
+
return raw
|
|
397
|
+
.split(',')
|
|
398
|
+
.map((v) => v.trim())
|
|
399
|
+
.filter(Boolean);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Toggle one value on/off in a metafield facet's URL param. Multi-select
|
|
404
|
+
* adds/removes from the list; single-select / boolean replace the value.
|
|
405
|
+
*/
|
|
406
|
+
function setMetafieldValue(key: string, values: string[]) {
|
|
407
|
+
updateParam(`${METAFIELD_URL_PREFIX}${key}`, values.join(','));
|
|
408
|
+
}
|
|
409
|
+
|
|
329
410
|
return (
|
|
330
411
|
<div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
|
|
331
412
|
{/* Page Header */}
|
|
@@ -402,6 +483,88 @@ function ProductsContent() {
|
|
|
402
483
|
</select>
|
|
403
484
|
)}
|
|
404
485
|
|
|
486
|
+
{/* Custom-field (metafield) facets — rendered per definition type.
|
|
487
|
+
Only definitions flagged `filterable: true` and published to
|
|
488
|
+
this sales channel reach this list (see `metafieldDefs` filter
|
|
489
|
+
above). The merchant's display name (`def.name`) is shown as-is
|
|
490
|
+
— `enumValues` come from the merchant's dashboard config. */}
|
|
491
|
+
{metafieldDefs.map((def) => {
|
|
492
|
+
const selected = getSelectedMetafieldValues(def.key);
|
|
493
|
+
|
|
494
|
+
if (def.type === 'BOOLEAN') {
|
|
495
|
+
const isOn = selected.includes('true');
|
|
496
|
+
return (
|
|
497
|
+
<label
|
|
498
|
+
key={def.id}
|
|
499
|
+
className="border-border text-foreground hover:border-primary inline-flex h-9 cursor-pointer items-center gap-2 rounded border px-3 text-sm transition-colors"
|
|
500
|
+
>
|
|
501
|
+
<input
|
|
502
|
+
type="checkbox"
|
|
503
|
+
checked={isOn}
|
|
504
|
+
onChange={(e) => setMetafieldValue(def.key, e.target.checked ? ['true'] : [])}
|
|
505
|
+
className="accent-primary h-4 w-4"
|
|
506
|
+
/>
|
|
507
|
+
{def.name}
|
|
508
|
+
</label>
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
if (def.type === 'SELECT') {
|
|
513
|
+
return (
|
|
514
|
+
<select
|
|
515
|
+
key={def.id}
|
|
516
|
+
value={selected[0] || ''}
|
|
517
|
+
onChange={(e) =>
|
|
518
|
+
setMetafieldValue(def.key, e.target.value ? [e.target.value] : [])
|
|
519
|
+
}
|
|
520
|
+
aria-label={def.name}
|
|
521
|
+
className="border-border bg-background text-foreground focus:ring-primary/20 focus:border-primary h-9 rounded border px-3 text-sm focus:outline-none focus:ring-2"
|
|
522
|
+
>
|
|
523
|
+
<option value="">{`${tc('all')} ${def.name}`}</option>
|
|
524
|
+
{(def.enumValues || []).map((v) => (
|
|
525
|
+
<option key={v} value={v}>
|
|
526
|
+
{v}
|
|
527
|
+
</option>
|
|
528
|
+
))}
|
|
529
|
+
</select>
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// MULTI_SELECT — chip toggles. Each click adds/removes one value.
|
|
534
|
+
return (
|
|
535
|
+
<div
|
|
536
|
+
key={def.id}
|
|
537
|
+
className="border-border inline-flex items-center gap-1 rounded border px-2 py-1"
|
|
538
|
+
aria-label={def.name}
|
|
539
|
+
>
|
|
540
|
+
<span className="text-muted-foreground px-1 text-xs">{def.name}:</span>
|
|
541
|
+
{(def.enumValues || []).map((v) => {
|
|
542
|
+
const active = selected.includes(v);
|
|
543
|
+
return (
|
|
544
|
+
<button
|
|
545
|
+
key={v}
|
|
546
|
+
type="button"
|
|
547
|
+
onClick={() =>
|
|
548
|
+
setMetafieldValue(
|
|
549
|
+
def.key,
|
|
550
|
+
active ? selected.filter((s) => s !== v) : [...selected, v]
|
|
551
|
+
)
|
|
552
|
+
}
|
|
553
|
+
className={cn(
|
|
554
|
+
'rounded-full border px-2 py-0.5 text-xs transition-colors',
|
|
555
|
+
active
|
|
556
|
+
? 'bg-primary text-primary-foreground border-primary'
|
|
557
|
+
: 'border-border text-muted-foreground hover:border-primary hover:text-foreground'
|
|
558
|
+
)}
|
|
559
|
+
>
|
|
560
|
+
{v}
|
|
561
|
+
</button>
|
|
562
|
+
);
|
|
563
|
+
})}
|
|
564
|
+
</div>
|
|
565
|
+
);
|
|
566
|
+
})}
|
|
567
|
+
|
|
405
568
|
{/* Sort */}
|
|
406
569
|
<div className="flex items-center gap-2 sm:ms-auto">
|
|
407
570
|
<label htmlFor="sort" className="text-muted-foreground whitespace-nowrap text-sm">
|