create-nextblock 0.13.8 → 0.13.10
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/package.json +1 -1
- package/templates/nextblock-template/app/actions/productGridActions.ts +40 -0
- package/templates/nextblock-template/app/api/cms/ecommerce/product-picker/route.ts +151 -0
- package/templates/nextblock-template/app/cms/CmsClientLayout.tsx +3 -0
- package/templates/nextblock-template/app/cms/blocks/components/BlockEditorArea.tsx +10 -1
- package/templates/nextblock-template/app/cms/blocks/components/BlockTypeSelector.tsx +17 -5
- package/templates/nextblock-template/app/cms/blocks/components/ColumnEditor.tsx +5 -1
- package/templates/nextblock-template/app/cms/blocks/components/EditableBlock.tsx +55 -24
- package/templates/nextblock-template/app/cms/blocks/components/MultiEntityPicker.tsx +263 -0
- package/templates/nextblock-template/app/cms/blocks/editors/ProductGridBlockEditor.tsx +375 -18
- package/templates/nextblock-template/app/cms/components/EcommerceActiveContext.tsx +27 -0
- package/templates/nextblock-template/components/blocks/ProductGridClient.tsx +114 -0
- package/templates/nextblock-template/lib/blocks/ProductGridBlock.tsx +78 -139
- package/templates/nextblock-template/lib/blocks/blockRegistry.ts +24 -4
- package/templates/nextblock-template/lib/blocks/blockTypes.ts +19 -0
- package/templates/nextblock-template/lib/blocks/ecommerce-block-schemas.ts +61 -2
- package/templates/nextblock-template/lib/blocks/product-grid-data.ts +210 -0
- package/templates/nextblock-template/next-env.d.ts +1 -1
- package/templates/nextblock-template/package.json +1 -1
- package/templates/nextblock-template/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { Check, ChevronsUpDown, Loader2, Search, X } from 'lucide-react';
|
|
5
|
+
import {
|
|
6
|
+
Badge,
|
|
7
|
+
Button,
|
|
8
|
+
Input,
|
|
9
|
+
Popover,
|
|
10
|
+
PopoverContent,
|
|
11
|
+
PopoverTrigger,
|
|
12
|
+
} from '@nextblock-cms/ui';
|
|
13
|
+
import { cn } from '@nextblock-cms/utils';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A checkbox *look* without the interactive element. The whole option row is the
|
|
17
|
+
* button, and Radix's `Checkbox` renders its own `<button>` — nesting the two is
|
|
18
|
+
* invalid HTML and breaks hydration.
|
|
19
|
+
*/
|
|
20
|
+
function CheckIndicator({ checked }: { checked: boolean }) {
|
|
21
|
+
return (
|
|
22
|
+
<span
|
|
23
|
+
aria-hidden="true"
|
|
24
|
+
className={cn(
|
|
25
|
+
'flex h-3.5 w-3.5 shrink-0 items-center justify-center rounded-[4px] border border-primary transition-colors',
|
|
26
|
+
checked && 'bg-primary text-primary-foreground'
|
|
27
|
+
)}
|
|
28
|
+
>
|
|
29
|
+
{checked && <Check className="h-3 w-3" />}
|
|
30
|
+
</span>
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface PickerOption {
|
|
35
|
+
id: string;
|
|
36
|
+
label: string;
|
|
37
|
+
/** Secondary line under the label, e.g. a slug or SKU. */
|
|
38
|
+
description?: string | null;
|
|
39
|
+
/** Small trailing tag, e.g. a language code or publish status. */
|
|
40
|
+
badge?: string | null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface MultiEntityPickerProps {
|
|
44
|
+
options: PickerOption[];
|
|
45
|
+
/**
|
|
46
|
+
* Labels for already-selected ids that the current `options` page may not
|
|
47
|
+
* contain (server-filtered lists). Used for the chips only, never listed.
|
|
48
|
+
*/
|
|
49
|
+
selectedOptions?: PickerOption[];
|
|
50
|
+
selectedIds: string[];
|
|
51
|
+
onChange: (ids: string[]) => void;
|
|
52
|
+
placeholder?: string;
|
|
53
|
+
searchPlaceholder?: string;
|
|
54
|
+
emptyMessage?: string;
|
|
55
|
+
/** Singular / plural noun used in the trigger label, e.g. ['category', 'categories']. */
|
|
56
|
+
nouns: [string, string];
|
|
57
|
+
isLoading?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Provide to filter server-side (the component then stops filtering locally).
|
|
60
|
+
* Omit for a fully client-side list.
|
|
61
|
+
*/
|
|
62
|
+
onSearchChange?: (query: string) => void;
|
|
63
|
+
/** Number the selected chips — use when selection order is display order. */
|
|
64
|
+
showOrder?: boolean;
|
|
65
|
+
maxSelected?: number;
|
|
66
|
+
/** Extra line under the control, e.g. "Showing the first 50 products". */
|
|
67
|
+
hint?: React.ReactNode;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export default function MultiEntityPicker({
|
|
71
|
+
options,
|
|
72
|
+
selectedOptions: selectedOptionsProp,
|
|
73
|
+
selectedIds,
|
|
74
|
+
onChange,
|
|
75
|
+
placeholder = 'Select…',
|
|
76
|
+
searchPlaceholder = 'Search…',
|
|
77
|
+
emptyMessage = 'Nothing found.',
|
|
78
|
+
nouns,
|
|
79
|
+
isLoading = false,
|
|
80
|
+
onSearchChange,
|
|
81
|
+
showOrder = false,
|
|
82
|
+
maxSelected,
|
|
83
|
+
hint,
|
|
84
|
+
}: MultiEntityPickerProps) {
|
|
85
|
+
const [open, setOpen] = React.useState(false);
|
|
86
|
+
const [searchQuery, setSearchQuery] = React.useState('');
|
|
87
|
+
const isServerFiltered = typeof onSearchChange === 'function';
|
|
88
|
+
|
|
89
|
+
const labelsById = React.useMemo(
|
|
90
|
+
() =>
|
|
91
|
+
new Map(
|
|
92
|
+
[...options, ...(selectedOptionsProp ?? [])].map((option) => [option.id, option])
|
|
93
|
+
),
|
|
94
|
+
[options, selectedOptionsProp]
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
// Selection order is meaningful (it drives display order), so walk selectedIds
|
|
98
|
+
// rather than options. Ids whose option has not loaded yet still get a chip.
|
|
99
|
+
const selectedOptions = React.useMemo(
|
|
100
|
+
() =>
|
|
101
|
+
selectedIds.map(
|
|
102
|
+
(id) => labelsById.get(id) ?? { id, label: 'Loading…', description: null }
|
|
103
|
+
),
|
|
104
|
+
[selectedIds, labelsById]
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
const filteredOptions = React.useMemo(() => {
|
|
108
|
+
if (isServerFiltered || !searchQuery.trim()) return options;
|
|
109
|
+
const query = searchQuery.trim().toLowerCase();
|
|
110
|
+
return options.filter(
|
|
111
|
+
(option) =>
|
|
112
|
+
option.label.toLowerCase().includes(query) ||
|
|
113
|
+
(option.description ?? '').toLowerCase().includes(query)
|
|
114
|
+
);
|
|
115
|
+
}, [options, searchQuery, isServerFiltered]);
|
|
116
|
+
|
|
117
|
+
const atLimit = typeof maxSelected === 'number' && selectedIds.length >= maxSelected;
|
|
118
|
+
|
|
119
|
+
const handleSearchChange = (value: string) => {
|
|
120
|
+
setSearchQuery(value);
|
|
121
|
+
onSearchChange?.(value);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const toggle = (id: string) => {
|
|
125
|
+
if (selectedIds.includes(id)) {
|
|
126
|
+
onChange(selectedIds.filter((selectedId) => selectedId !== id));
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (atLimit) return;
|
|
130
|
+
onChange([...selectedIds, id]);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const remove = (id: string) => onChange(selectedIds.filter((selectedId) => selectedId !== id));
|
|
134
|
+
|
|
135
|
+
const [singular, plural] = nouns;
|
|
136
|
+
const triggerLabel =
|
|
137
|
+
selectedIds.length > 0
|
|
138
|
+
? `${selectedIds.length} ${selectedIds.length === 1 ? singular : plural} selected`
|
|
139
|
+
: placeholder;
|
|
140
|
+
|
|
141
|
+
return (
|
|
142
|
+
<div className="space-y-2">
|
|
143
|
+
{selectedOptions.length > 0 && (
|
|
144
|
+
<ul className="flex flex-wrap gap-1.5 rounded-md border bg-muted/30 p-1.5">
|
|
145
|
+
{selectedOptions.map((option, index) => (
|
|
146
|
+
<li key={option.id}>
|
|
147
|
+
<Badge
|
|
148
|
+
variant="secondary"
|
|
149
|
+
className="flex items-center gap-1 rounded-full py-0.5 pl-2 pr-1 text-xs font-normal"
|
|
150
|
+
>
|
|
151
|
+
{showOrder && (
|
|
152
|
+
<span className="text-[10px] font-semibold tabular-nums text-muted-foreground">
|
|
153
|
+
{index + 1}
|
|
154
|
+
</span>
|
|
155
|
+
)}
|
|
156
|
+
<span className="max-w-[180px] truncate">{option.label}</span>
|
|
157
|
+
<button
|
|
158
|
+
type="button"
|
|
159
|
+
onClick={() => remove(option.id)}
|
|
160
|
+
aria-label={`Remove ${option.label}`}
|
|
161
|
+
className="rounded-full p-0.5 text-muted-foreground transition-colors hover:bg-destructive/10 hover:text-destructive"
|
|
162
|
+
>
|
|
163
|
+
<X className="h-3 w-3" />
|
|
164
|
+
</button>
|
|
165
|
+
</Badge>
|
|
166
|
+
</li>
|
|
167
|
+
))}
|
|
168
|
+
</ul>
|
|
169
|
+
)}
|
|
170
|
+
|
|
171
|
+
<Popover open={open} onOpenChange={setOpen}>
|
|
172
|
+
<PopoverTrigger asChild>
|
|
173
|
+
<Button
|
|
174
|
+
type="button"
|
|
175
|
+
variant="outline"
|
|
176
|
+
role="combobox"
|
|
177
|
+
aria-expanded={open}
|
|
178
|
+
className="h-9 w-full justify-between text-xs font-normal"
|
|
179
|
+
>
|
|
180
|
+
<span className="truncate">{triggerLabel}</span>
|
|
181
|
+
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
|
|
182
|
+
</Button>
|
|
183
|
+
</PopoverTrigger>
|
|
184
|
+
<PopoverContent className="w-[--radix-popover-trigger-width] p-0" align="start">
|
|
185
|
+
<div className="flex items-center border-b px-3">
|
|
186
|
+
{isLoading ? (
|
|
187
|
+
<Loader2 className="mr-2 h-4 w-4 shrink-0 animate-spin opacity-50" />
|
|
188
|
+
) : (
|
|
189
|
+
<Search className="mr-2 h-4 w-4 shrink-0 opacity-50" />
|
|
190
|
+
)}
|
|
191
|
+
<Input
|
|
192
|
+
autoFocus
|
|
193
|
+
className="h-9 w-full border-0 bg-transparent py-2 text-xs shadow-none focus-visible:ring-0 focus-visible:ring-offset-0"
|
|
194
|
+
placeholder={searchPlaceholder}
|
|
195
|
+
value={searchQuery}
|
|
196
|
+
onChange={(event) => handleSearchChange(event.target.value)}
|
|
197
|
+
/>
|
|
198
|
+
</div>
|
|
199
|
+
|
|
200
|
+
<div
|
|
201
|
+
role="listbox"
|
|
202
|
+
aria-multiselectable="true"
|
|
203
|
+
className="max-h-60 overflow-y-auto p-1"
|
|
204
|
+
>
|
|
205
|
+
{filteredOptions.length === 0 ? (
|
|
206
|
+
<p className="py-6 text-center text-xs text-muted-foreground">
|
|
207
|
+
{isLoading ? 'Loading…' : emptyMessage}
|
|
208
|
+
</p>
|
|
209
|
+
) : (
|
|
210
|
+
filteredOptions.map((option) => {
|
|
211
|
+
const isChecked = selectedIds.includes(option.id);
|
|
212
|
+
const isBlocked = !isChecked && atLimit;
|
|
213
|
+
return (
|
|
214
|
+
<button
|
|
215
|
+
key={option.id}
|
|
216
|
+
type="button"
|
|
217
|
+
role="option"
|
|
218
|
+
aria-selected={isChecked}
|
|
219
|
+
disabled={isBlocked}
|
|
220
|
+
onClick={() => toggle(option.id)}
|
|
221
|
+
className={cn(
|
|
222
|
+
'relative flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-left text-xs outline-none transition-colors',
|
|
223
|
+
isBlocked
|
|
224
|
+
? 'cursor-not-allowed opacity-40'
|
|
225
|
+
: 'cursor-pointer hover:bg-accent hover:text-accent-foreground',
|
|
226
|
+
isChecked && 'bg-accent/40'
|
|
227
|
+
)}
|
|
228
|
+
>
|
|
229
|
+
<CheckIndicator checked={isChecked} />
|
|
230
|
+
<span className="flex min-w-0 flex-col">
|
|
231
|
+
<span className="truncate font-medium">{option.label}</span>
|
|
232
|
+
{option.description && (
|
|
233
|
+
<span className="truncate font-mono text-[10px] leading-tight text-muted-foreground">
|
|
234
|
+
{option.description}
|
|
235
|
+
</span>
|
|
236
|
+
)}
|
|
237
|
+
</span>
|
|
238
|
+
{option.badge && (
|
|
239
|
+
<Badge
|
|
240
|
+
variant="outline"
|
|
241
|
+
className="ml-auto shrink-0 px-1.5 py-0 text-[9px] uppercase"
|
|
242
|
+
>
|
|
243
|
+
{option.badge}
|
|
244
|
+
</Badge>
|
|
245
|
+
)}
|
|
246
|
+
</button>
|
|
247
|
+
);
|
|
248
|
+
})
|
|
249
|
+
)}
|
|
250
|
+
</div>
|
|
251
|
+
|
|
252
|
+
{atLimit && (
|
|
253
|
+
<p className="border-t px-3 py-2 text-[11px] text-muted-foreground">
|
|
254
|
+
Limit reached ({maxSelected}). Remove one to add another.
|
|
255
|
+
</p>
|
|
256
|
+
)}
|
|
257
|
+
</PopoverContent>
|
|
258
|
+
</Popover>
|
|
259
|
+
|
|
260
|
+
{hint && <p className="text-[11px] leading-snug text-muted-foreground">{hint}</p>}
|
|
261
|
+
</div>
|
|
262
|
+
);
|
|
263
|
+
}
|
|
@@ -1,32 +1,238 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
import React from 'react';
|
|
4
|
-
import {
|
|
4
|
+
import { AlertTriangle, Clock, Folder, ListChecks } from 'lucide-react';
|
|
5
|
+
import { Label, Input, Checkbox } from "@nextblock-cms/ui";
|
|
6
|
+
import { cn } from "@nextblock-cms/utils";
|
|
5
7
|
import { BlockEditorProps } from '../components/BlockEditorModal';
|
|
6
|
-
import {
|
|
8
|
+
import MultiEntityPicker, { type PickerOption } from '../components/MultiEntityPicker';
|
|
9
|
+
import {
|
|
10
|
+
PRODUCT_GRID_DEFAULT_PAGE_SIZE,
|
|
11
|
+
PRODUCT_GRID_MAX_CATEGORIES,
|
|
12
|
+
PRODUCT_GRID_MAX_LIMIT,
|
|
13
|
+
PRODUCT_GRID_MAX_PRODUCTS,
|
|
14
|
+
PRODUCT_GRID_UNLIMITED,
|
|
15
|
+
resolveProductGridCategoryIds,
|
|
16
|
+
resolveProductGridLimit,
|
|
17
|
+
resolveProductGridProductIds,
|
|
18
|
+
type ProductGridBlockContent,
|
|
19
|
+
} from '../../../../lib/blocks/ecommerce-block-schemas';
|
|
20
|
+
|
|
21
|
+
type SourceType = NonNullable<ProductGridBlockContent['type']>;
|
|
22
|
+
|
|
23
|
+
const SOURCE_OPTIONS: { value: SourceType; label: string; icon: React.ElementType; hint: string }[] = [
|
|
24
|
+
{
|
|
25
|
+
value: 'latest',
|
|
26
|
+
label: 'Latest',
|
|
27
|
+
icon: Clock,
|
|
28
|
+
hint: 'Newest products first — the grid updates itself as you add products.',
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
value: 'category',
|
|
32
|
+
label: 'By category',
|
|
33
|
+
icon: Folder,
|
|
34
|
+
hint: 'Products belonging to any of the selected categories, newest first.',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
value: 'manual',
|
|
38
|
+
label: 'Hand-picked',
|
|
39
|
+
icon: ListChecks,
|
|
40
|
+
hint: 'Exactly the products you choose, shown in the order you add them.',
|
|
41
|
+
},
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
interface PickerProduct {
|
|
45
|
+
id: string;
|
|
46
|
+
title: string;
|
|
47
|
+
sku: string | null;
|
|
48
|
+
status: string | null;
|
|
49
|
+
languageCode: string | null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface PickerData {
|
|
53
|
+
categories: { id: string; name: string; slug: string }[];
|
|
54
|
+
products: PickerProduct[];
|
|
55
|
+
hasMore: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const EMPTY_DATA: PickerData = { categories: [], products: [], hasMore: false };
|
|
59
|
+
|
|
60
|
+
function toProductOption(product: PickerProduct): PickerOption {
|
|
61
|
+
return {
|
|
62
|
+
id: product.id,
|
|
63
|
+
label: product.title,
|
|
64
|
+
description: product.sku ? `SKU ${product.sku}` : null,
|
|
65
|
+
badge:
|
|
66
|
+
[product.languageCode, product.status !== 'active' ? product.status : null]
|
|
67
|
+
.filter(Boolean)
|
|
68
|
+
.join(' · ') || null,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
7
71
|
|
|
8
72
|
export default function ProductGridBlockEditor({ content, onChange }: BlockEditorProps<Partial<ProductGridBlockContent>>) {
|
|
73
|
+
const [data, setData] = React.useState<PickerData>(EMPTY_DATA);
|
|
74
|
+
const [isLoading, setIsLoading] = React.useState(true);
|
|
75
|
+
const [loadError, setLoadError] = React.useState<string | null>(null);
|
|
76
|
+
const [productSearch, setProductSearch] = React.useState('');
|
|
77
|
+
// Every product seen so far, so a chip keeps its label after the search that
|
|
78
|
+
// surfaced it is cleared.
|
|
79
|
+
const [knownProducts, setKnownProducts] = React.useState<Map<string, PickerProduct>>(
|
|
80
|
+
() => new Map()
|
|
81
|
+
);
|
|
9
82
|
|
|
10
|
-
const
|
|
83
|
+
const sourceType: SourceType = content.type ?? 'latest';
|
|
84
|
+
const selectedCategoryIds = React.useMemo(
|
|
85
|
+
() => resolveProductGridCategoryIds(content),
|
|
86
|
+
[content]
|
|
87
|
+
);
|
|
88
|
+
const selectedProductIds = React.useMemo(
|
|
89
|
+
() => resolveProductGridProductIds(content),
|
|
90
|
+
[content]
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
// Only the ids present when the editor mounts need hydrating — anything picked
|
|
94
|
+
// afterwards is already in `data.products`. Keeping this out of the fetch
|
|
95
|
+
// dependencies avoids a refetch on every selection change.
|
|
96
|
+
const initialProductIdsRef = React.useRef(selectedProductIds.join(','));
|
|
97
|
+
|
|
98
|
+
React.useEffect(() => {
|
|
99
|
+
const controller = new AbortController();
|
|
100
|
+
const handle = setTimeout(() => {
|
|
101
|
+
setIsLoading(true);
|
|
102
|
+
const params = new URLSearchParams();
|
|
103
|
+
if (productSearch.trim()) params.set('search', productSearch.trim());
|
|
104
|
+
if (initialProductIdsRef.current) params.set('ids', initialProductIdsRef.current);
|
|
105
|
+
|
|
106
|
+
fetch(`/api/cms/ecommerce/product-picker?${params.toString()}`, {
|
|
107
|
+
signal: controller.signal,
|
|
108
|
+
})
|
|
109
|
+
.then(async (response) => {
|
|
110
|
+
if (!response.ok) {
|
|
111
|
+
const body = await response.json().catch(() => ({}));
|
|
112
|
+
throw new Error(body?.error || 'Failed to load products.');
|
|
113
|
+
}
|
|
114
|
+
return response.json();
|
|
115
|
+
})
|
|
116
|
+
.then((payload: PickerData) => {
|
|
117
|
+
const products = payload.products ?? [];
|
|
118
|
+
setData({
|
|
119
|
+
categories: payload.categories ?? [],
|
|
120
|
+
products,
|
|
121
|
+
hasMore: Boolean(payload.hasMore),
|
|
122
|
+
});
|
|
123
|
+
setKnownProducts((previous) => {
|
|
124
|
+
const next = new Map(previous);
|
|
125
|
+
for (const product of products) next.set(product.id, product);
|
|
126
|
+
return next;
|
|
127
|
+
});
|
|
128
|
+
setLoadError(null);
|
|
129
|
+
})
|
|
130
|
+
.catch((error: unknown) => {
|
|
131
|
+
if (error instanceof DOMException && error.name === 'AbortError') return;
|
|
132
|
+
setLoadError(error instanceof Error ? error.message : 'Failed to load products.');
|
|
133
|
+
})
|
|
134
|
+
.finally(() => {
|
|
135
|
+
if (!controller.signal.aborted) setIsLoading(false);
|
|
136
|
+
});
|
|
137
|
+
// Debounce so typing in the product search does not fire a request per keystroke.
|
|
138
|
+
}, productSearch ? 250 : 0);
|
|
139
|
+
|
|
140
|
+
return () => {
|
|
141
|
+
controller.abort();
|
|
142
|
+
clearTimeout(handle);
|
|
143
|
+
};
|
|
144
|
+
}, [productSearch]);
|
|
145
|
+
|
|
146
|
+
const handleChange = <K extends keyof ProductGridBlockContent>(
|
|
147
|
+
field: K,
|
|
148
|
+
value: ProductGridBlockContent[K]
|
|
149
|
+
) => {
|
|
11
150
|
onChange({ ...content, [field]: value });
|
|
12
151
|
};
|
|
13
152
|
|
|
153
|
+
/**
|
|
154
|
+
* Writing `categoryIds` has to drop the legacy `categoryId`, otherwise
|
|
155
|
+
* `resolveProductGridCategoryIds` falls back to it and a category the author
|
|
156
|
+
* just removed comes straight back.
|
|
157
|
+
*/
|
|
158
|
+
const handleCategoryChange = (ids: string[]) => {
|
|
159
|
+
const { categoryId: _legacyCategoryId, ...rest } = content;
|
|
160
|
+
onChange({ ...rest, categoryIds: ids });
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const limit = resolveProductGridLimit(content);
|
|
164
|
+
const isUnlimited = limit === PRODUCT_GRID_UNLIMITED;
|
|
165
|
+
const showPagination = Boolean(content.showPagination);
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Pagination needs a page size, so switching it on for an unlimited grid picks
|
|
169
|
+
* a sensible one instead of leaving the block in a contradictory state.
|
|
170
|
+
*/
|
|
171
|
+
const handlePaginationChange = (checked: boolean) => {
|
|
172
|
+
onChange({
|
|
173
|
+
...content,
|
|
174
|
+
showPagination: checked,
|
|
175
|
+
limit: checked && isUnlimited ? PRODUCT_GRID_DEFAULT_PAGE_SIZE : limit,
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// The field is backed by a draft string so it can be emptied mid-edit; only
|
|
180
|
+
// parseable values are committed to the block.
|
|
181
|
+
const [limitDraft, setLimitDraft] = React.useState(() => String(limit));
|
|
182
|
+
React.useEffect(() => {
|
|
183
|
+
setLimitDraft(String(limit));
|
|
184
|
+
}, [limit]);
|
|
185
|
+
|
|
186
|
+
const handleLimitChange = (raw: string) => {
|
|
187
|
+
setLimitDraft(raw);
|
|
188
|
+
const parsed = parseInt(raw, 10);
|
|
189
|
+
if (Number.isNaN(parsed)) return;
|
|
190
|
+
const floor = showPagination ? 1 : PRODUCT_GRID_UNLIMITED;
|
|
191
|
+
handleChange('limit', Math.min(Math.max(parsed, floor), PRODUCT_GRID_MAX_LIMIT));
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const handleSourceChange = (nextType: SourceType) => {
|
|
195
|
+
const { categoryId: _legacyCategoryId, ...rest } = content;
|
|
196
|
+
onChange({
|
|
197
|
+
...rest,
|
|
198
|
+
type: nextType,
|
|
199
|
+
// Both selections are kept so switching source back and forth is
|
|
200
|
+
// non-destructive; the renderer only reads the one matching `type`.
|
|
201
|
+
categoryIds: selectedCategoryIds,
|
|
202
|
+
productIds: selectedProductIds,
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const categoryOptions: PickerOption[] = React.useMemo(
|
|
207
|
+
() =>
|
|
208
|
+
data.categories.map((category) => ({
|
|
209
|
+
id: category.id,
|
|
210
|
+
label: category.name,
|
|
211
|
+
description: `/${category.slug}`,
|
|
212
|
+
})),
|
|
213
|
+
[data.categories]
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
const productOptions: PickerOption[] = React.useMemo(
|
|
217
|
+
() => data.products.map(toProductOption),
|
|
218
|
+
[data.products]
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
const selectedProductOptions: PickerOption[] = React.useMemo(
|
|
222
|
+
() =>
|
|
223
|
+
selectedProductIds
|
|
224
|
+
.map((id) => knownProducts.get(id))
|
|
225
|
+
.filter((product): product is PickerProduct => Boolean(product))
|
|
226
|
+
.map(toProductOption),
|
|
227
|
+
[selectedProductIds, knownProducts]
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
const activeSource = SOURCE_OPTIONS.find((option) => option.value === sourceType) ?? SOURCE_OPTIONS[0];
|
|
231
|
+
|
|
14
232
|
return (
|
|
15
|
-
<div className="space-y-
|
|
16
|
-
<div>
|
|
17
|
-
<Label htmlFor="pg-limit">Item Limit</Label>
|
|
18
|
-
<Input
|
|
19
|
-
id="pg-limit"
|
|
20
|
-
type="number"
|
|
21
|
-
min={1}
|
|
22
|
-
max={20}
|
|
23
|
-
value={content.limit || 6}
|
|
24
|
-
onChange={(e) => handleChange('limit', parseInt(e.target.value) || 6)}
|
|
25
|
-
className="mt-1"
|
|
26
|
-
/>
|
|
27
|
-
</div>
|
|
233
|
+
<div className="mt-2 space-y-5 border-t p-3">
|
|
28
234
|
<div>
|
|
29
|
-
<Label htmlFor="pg-title">Grid
|
|
235
|
+
<Label htmlFor="pg-title">Grid title (optional)</Label>
|
|
30
236
|
<Input
|
|
31
237
|
id="pg-title"
|
|
32
238
|
value={content.title || ""}
|
|
@@ -34,8 +240,159 @@ export default function ProductGridBlockEditor({ content, onChange }: BlockEdito
|
|
|
34
240
|
placeholder="New Arrivals"
|
|
35
241
|
className="mt-1"
|
|
36
242
|
/>
|
|
243
|
+
<p className="mt-1 text-[11px] text-muted-foreground">
|
|
244
|
+
Shown as a heading above the grid. Leave empty for no heading.
|
|
245
|
+
</p>
|
|
246
|
+
</div>
|
|
247
|
+
|
|
248
|
+
<div>
|
|
249
|
+
<Label>Which products?</Label>
|
|
250
|
+
<div
|
|
251
|
+
role="radiogroup"
|
|
252
|
+
aria-label="Product source"
|
|
253
|
+
className="mt-1 grid grid-cols-3 gap-1 rounded-lg border bg-muted/40 p-1"
|
|
254
|
+
>
|
|
255
|
+
{SOURCE_OPTIONS.map((option) => {
|
|
256
|
+
const Icon = option.icon;
|
|
257
|
+
const isActive = option.value === sourceType;
|
|
258
|
+
return (
|
|
259
|
+
<button
|
|
260
|
+
key={option.value}
|
|
261
|
+
type="button"
|
|
262
|
+
role="radio"
|
|
263
|
+
aria-checked={isActive}
|
|
264
|
+
onClick={() => handleSourceChange(option.value)}
|
|
265
|
+
className={cn(
|
|
266
|
+
'flex items-center justify-center gap-1.5 rounded-md px-2 py-1.5 text-xs transition-colors',
|
|
267
|
+
isActive
|
|
268
|
+
? 'bg-background font-medium text-foreground shadow-sm'
|
|
269
|
+
: 'text-muted-foreground hover:text-foreground'
|
|
270
|
+
)}
|
|
271
|
+
>
|
|
272
|
+
<Icon className="h-3.5 w-3.5" />
|
|
273
|
+
{option.label}
|
|
274
|
+
</button>
|
|
275
|
+
);
|
|
276
|
+
})}
|
|
277
|
+
</div>
|
|
278
|
+
<p className="mt-1.5 text-[11px] leading-snug text-muted-foreground">{activeSource.hint}</p>
|
|
279
|
+
</div>
|
|
280
|
+
|
|
281
|
+
{loadError && (
|
|
282
|
+
<p className="flex items-start gap-1.5 rounded-md bg-destructive/10 p-2 text-[11px] text-destructive">
|
|
283
|
+
<AlertTriangle className="mt-px h-3.5 w-3.5 shrink-0" />
|
|
284
|
+
{loadError}
|
|
285
|
+
</p>
|
|
286
|
+
)}
|
|
287
|
+
|
|
288
|
+
{sourceType === 'category' && (
|
|
289
|
+
<div>
|
|
290
|
+
<Label>Categories</Label>
|
|
291
|
+
<div className="mt-1">
|
|
292
|
+
<MultiEntityPicker
|
|
293
|
+
options={categoryOptions}
|
|
294
|
+
selectedIds={selectedCategoryIds}
|
|
295
|
+
onChange={handleCategoryChange}
|
|
296
|
+
nouns={['category', 'categories']}
|
|
297
|
+
placeholder="Select categories…"
|
|
298
|
+
searchPlaceholder="Search categories…"
|
|
299
|
+
emptyMessage="No categories yet."
|
|
300
|
+
isLoading={isLoading}
|
|
301
|
+
maxSelected={PRODUCT_GRID_MAX_CATEGORIES}
|
|
302
|
+
hint={
|
|
303
|
+
selectedCategoryIds.length === 0
|
|
304
|
+
? 'No category selected — the grid falls back to the latest products.'
|
|
305
|
+
: 'A product appears if it belongs to any of these categories.'
|
|
306
|
+
}
|
|
307
|
+
/>
|
|
308
|
+
</div>
|
|
309
|
+
</div>
|
|
310
|
+
)}
|
|
311
|
+
|
|
312
|
+
{sourceType === 'manual' && (
|
|
313
|
+
<div>
|
|
314
|
+
<Label>Products</Label>
|
|
315
|
+
<div className="mt-1">
|
|
316
|
+
<MultiEntityPicker
|
|
317
|
+
options={productOptions}
|
|
318
|
+
selectedOptions={selectedProductOptions}
|
|
319
|
+
selectedIds={selectedProductIds}
|
|
320
|
+
onChange={(ids) => handleChange('productIds', ids)}
|
|
321
|
+
nouns={['product', 'products']}
|
|
322
|
+
placeholder="Select products…"
|
|
323
|
+
searchPlaceholder="Search by title or SKU…"
|
|
324
|
+
emptyMessage="No products match that search."
|
|
325
|
+
isLoading={isLoading}
|
|
326
|
+
onSearchChange={setProductSearch}
|
|
327
|
+
showOrder
|
|
328
|
+
maxSelected={PRODUCT_GRID_MAX_PRODUCTS}
|
|
329
|
+
hint={
|
|
330
|
+
selectedProductIds.length === 0
|
|
331
|
+
? 'Pick at least one product — an empty list renders nothing on the page.'
|
|
332
|
+
: `Shown in this order. ${data.hasMore ? 'Search to find products beyond the 50 most recent. ' : ''}Products are matched to the page language automatically.`
|
|
333
|
+
}
|
|
334
|
+
/>
|
|
335
|
+
</div>
|
|
336
|
+
</div>
|
|
337
|
+
)}
|
|
338
|
+
|
|
339
|
+
<div className="space-y-3">
|
|
340
|
+
{/* A hand-picked list has no cap to set — the picks are the cap — so the
|
|
341
|
+
count field only appears there once it means "per page". */}
|
|
342
|
+
{(sourceType !== 'manual' || showPagination) && (
|
|
343
|
+
<div>
|
|
344
|
+
<Label htmlFor="pg-limit">
|
|
345
|
+
{showPagination ? 'Products per page' : 'Products to show'}
|
|
346
|
+
</Label>
|
|
347
|
+
<Input
|
|
348
|
+
id="pg-limit"
|
|
349
|
+
type="number"
|
|
350
|
+
min={showPagination ? 1 : PRODUCT_GRID_UNLIMITED}
|
|
351
|
+
max={PRODUCT_GRID_MAX_LIMIT}
|
|
352
|
+
value={limitDraft}
|
|
353
|
+
onChange={(e) => handleLimitChange(e.target.value)}
|
|
354
|
+
onBlur={() => setLimitDraft(String(limit))}
|
|
355
|
+
className="mt-1 w-28"
|
|
356
|
+
/>
|
|
357
|
+
<p className="mt-1 text-[11px] leading-snug text-muted-foreground">
|
|
358
|
+
{showPagination ? (
|
|
359
|
+
<>
|
|
360
|
+
How many products fill one page (1–{PRODUCT_GRID_MAX_LIMIT}). Visitors page
|
|
361
|
+
through {sourceType === 'manual' ? 'the whole list' : 'everything that matches'}.
|
|
362
|
+
</>
|
|
363
|
+
) : isUnlimited ? (
|
|
364
|
+
<>
|
|
365
|
+
<strong className="font-medium text-foreground">0 = unlimited</strong> — every
|
|
366
|
+
matching product is shown on one page.
|
|
367
|
+
</>
|
|
368
|
+
) : (
|
|
369
|
+
<>
|
|
370
|
+
Maximum number of products in this grid (max {PRODUCT_GRID_MAX_LIMIT}). Set it to{' '}
|
|
371
|
+
<strong className="font-medium text-foreground">0</strong> to show them all.
|
|
372
|
+
</>
|
|
373
|
+
)}
|
|
374
|
+
</p>
|
|
375
|
+
</div>
|
|
376
|
+
)}
|
|
377
|
+
|
|
378
|
+
<div className="flex items-start gap-2">
|
|
379
|
+
<Checkbox
|
|
380
|
+
id="pg-pagination"
|
|
381
|
+
checked={showPagination}
|
|
382
|
+
onCheckedChange={(checked) => handlePaginationChange(checked === true)}
|
|
383
|
+
className="mt-0.5"
|
|
384
|
+
/>
|
|
385
|
+
<div>
|
|
386
|
+
<Label htmlFor="pg-pagination" className="cursor-pointer">
|
|
387
|
+
Paginate
|
|
388
|
+
</Label>
|
|
389
|
+
<p className="text-[11px] leading-snug text-muted-foreground">
|
|
390
|
+
Adds Previous / Next controls so visitors can reach every product
|
|
391
|
+
{isUnlimited ? `, ${PRODUCT_GRID_DEFAULT_PAGE_SIZE} at a time.` : '.'}
|
|
392
|
+
</p>
|
|
393
|
+
</div>
|
|
394
|
+
</div>
|
|
37
395
|
</div>
|
|
38
|
-
{/* Type selection could be added here, currently defaulting to 'latest' */}
|
|
39
396
|
</div>
|
|
40
397
|
);
|
|
41
398
|
}
|