@kidecms/core 0.1.11 → 0.13.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/LICENSE +21 -0
- package/admin/components/AssetsGrid.tsx +366 -173
- package/admin/components/BlockEditor.tsx +260 -679
- package/admin/components/CheckboxField.tsx +2 -2
- package/admin/components/CollaborationReviewBar.tsx +384 -0
- package/admin/components/ColorField.tsx +87 -0
- package/admin/components/CommandPalette.tsx +169 -0
- package/admin/components/ContentEditor.tsx +687 -0
- package/admin/components/DocumentActions.tsx +4 -2
- package/admin/components/DocumentsDataTable.tsx +91 -10
- package/admin/components/FieldControl.astro +151 -26
- package/admin/components/FocalPointSelector.tsx +27 -1
- package/admin/components/InternalLinkPicker.tsx +4 -3
- package/admin/components/LinkField.tsx +126 -0
- package/admin/components/RelationField.tsx +105 -26
- package/admin/components/RepeaterInput.tsx +46 -0
- package/admin/components/RichTextEditor.tsx +44 -9
- package/admin/components/SharedSectionBlockField.tsx +93 -0
- package/admin/components/SidebarUserMenu.tsx +1 -1
- package/admin/components/Toast.astro +1 -1
- package/admin/components/TreeItemsEditor.tsx +20 -19
- package/admin/components/TreeSelect.tsx +2 -2
- package/admin/components/YoutubeField.tsx +48 -0
- package/admin/components/block-fields.tsx +777 -0
- package/admin/components/content-block-spec.ts +114 -0
- package/admin/components/slash-command.tsx +242 -0
- package/admin/components/tree-utils.ts +14 -2
- package/admin/components/ui/badge.tsx +2 -3
- package/admin/components/ui/button.tsx +5 -4
- package/admin/components/ui/checkbox.tsx +1 -1
- package/admin/components/ui/input.tsx +1 -1
- package/admin/components/ui/select.tsx +1 -1
- package/admin/components/ui/table.tsx +1 -1
- package/admin/components/ui/textarea.tsx +1 -1
- package/admin/layouts/AdminLayout.astro +323 -123
- package/admin/lib/access.ts +54 -0
- package/admin/lib/collaboration.ts +178 -0
- package/admin/lib/edit-data.ts +448 -0
- package/admin/views/AccessDeniedView.astro +8 -0
- package/admin/views/EditView.astro +706 -0
- package/admin/views/ListView.astro +202 -0
- package/admin/views/RecentView.astro +76 -0
- package/admin/views/SinglesView.astro +65 -0
- package/admin/views/edit/EditScripts.astro +145 -0
- package/admin/views/edit/ReverseReferencesList.astro +68 -0
- package/core/admin.ts +193 -0
- package/core/ai.ts +87 -0
- package/core/api.ts +1392 -0
- package/core/assets.ts +336 -0
- package/core/audit.ts +84 -0
- package/core/auth-config.ts +44 -0
- package/core/auth.ts +303 -0
- package/core/collaboration.ts +276 -0
- package/core/content.ts +27 -0
- package/core/create-admin.ts +28 -0
- package/core/define.ts +625 -0
- package/core/field-model.ts +172 -0
- package/core/generator.ts +519 -0
- package/core/http.ts +42 -0
- package/core/image.ts +219 -0
- package/core/index.ts +211 -0
- package/core/locks.ts +52 -0
- package/core/log.ts +37 -0
- package/core/migrate.ts +278 -0
- package/core/rate-limit.ts +110 -0
- package/core/request-scope.ts +33 -0
- package/core/richtext.ts +1 -0
- package/core/runtime.ts +63 -0
- package/core/schema.ts +32 -0
- package/core/search.ts +489 -0
- package/core/seed.ts +101 -0
- package/core/shared-sections.ts +96 -0
- package/core/tasks.ts +176 -0
- package/core/values.ts +433 -0
- package/core/webhooks.ts +85 -0
- package/core/youtube.ts +39 -0
- package/internals/cli.mjs +34 -0
- package/internals/context.ts +67 -0
- package/internals/create-admin.ts +40 -0
- package/internals/describe.ts +25 -0
- package/internals/eject.ts +144 -0
- package/internals/generator.ts +11 -0
- package/internals/integration.ts +481 -0
- package/internals/mcp.ts +397 -0
- package/internals/project.ts +26 -0
- package/internals/push.ts +160 -0
- package/internals/reindex.ts +11 -0
- package/internals/restore.ts +257 -0
- package/internals/seed.data.ts +7 -0
- package/internals/seed.ts +11 -0
- package/internals/upgrade.ts +776 -0
- package/{virtual.d.ts → internals/virtual.d.ts} +39 -5
- package/middleware/auth.ts +130 -17
- package/middleware/push.ts +121 -0
- package/package.json +55 -67
- package/platform/cloudflare/cf-env.ts +60 -0
- package/platform/cloudflare/database.ts +28 -0
- package/platform/cloudflare/globals.d.ts +5 -0
- package/platform/cloudflare/storage.ts +33 -0
- package/platform/cloudflare/uploads-route.ts +37 -0
- package/platform/node/database.ts +39 -0
- package/platform/node/storage.ts +52 -0
- package/routes/admin/[...path].astro +71 -0
- package/routes/{pages/admin → admin}/assets/[id].astro +25 -19
- package/routes/admin/assets/index.astro +90 -0
- package/routes/{pages/admin/login.astro → admin/forgot-password.astro} +21 -19
- package/routes/{pages/admin → admin}/invite.astro +6 -6
- package/routes/admin/login.astro +103 -0
- package/routes/admin/reset-password.astro +73 -0
- package/routes/{pages/admin → admin}/setup.astro +31 -8
- package/routes/api/{cms/[collection] → [collection]}/[...path].ts +66 -14
- package/routes/api/admin/search.ts +89 -0
- package/routes/api/{cms/assets → assets}/[id].ts +24 -14
- package/routes/api/{cms/assets → assets}/index.ts +4 -2
- package/routes/api/assets/upload.ts +147 -0
- package/routes/api/auth/forgot-password.ts +77 -0
- package/routes/api/{cms/auth → auth}/invite.ts +41 -9
- package/routes/api/auth/login.ts +167 -0
- package/routes/api/{cms/auth → auth}/logout.ts +11 -2
- package/routes/api/auth/reset-password.ts +85 -0
- package/routes/api/auth/setup.ts +110 -0
- package/routes/api/auth/sso/[provider]/start.ts +34 -0
- package/routes/api/collaboration/index.ts +106 -0
- package/routes/api/cron/_authorize.ts +25 -0
- package/routes/api/{cms/cron → cron}/publish.ts +3 -9
- package/routes/api/cron/tasks.ts +33 -0
- package/routes/api/forms/submit/[slug].ts +143 -0
- package/routes/api/img/[...path].ts +65 -0
- package/routes/api/{cms/preview → preview}/render.ts +8 -3
- package/routes/api/{cms/references → references}/[collection]/[id].ts +31 -1
- package/routes/api/webhooks/[provider].ts +54 -0
- package/README.md +0 -28
- package/dist/admin.d.ts +0 -33
- package/dist/admin.js +0 -92
- package/dist/ai.d.ts +0 -15
- package/dist/ai.js +0 -70
- package/dist/api.d.ts +0 -14
- package/dist/api.js +0 -833
- package/dist/assets.d.ts +0 -51
- package/dist/assets.js +0 -163
- package/dist/auth.d.ts +0 -30
- package/dist/auth.js +0 -132
- package/dist/content.d.ts +0 -3
- package/dist/content.js +0 -29
- package/dist/create-admin.d.ts +0 -5
- package/dist/create-admin.js +0 -23
- package/dist/define.d.ts +0 -254
- package/dist/define.js +0 -36
- package/dist/generator.d.ts +0 -9
- package/dist/generator.js +0 -370
- package/dist/image.d.ts +0 -9
- package/dist/image.js +0 -69
- package/dist/index.d.ts +0 -28
- package/dist/index.js +0 -16
- package/dist/integration.d.ts +0 -14
- package/dist/integration.js +0 -327
- package/dist/locks.d.ts +0 -7
- package/dist/locks.js +0 -37
- package/dist/richtext.d.ts +0 -1
- package/dist/richtext.js +0 -1
- package/dist/runtime.d.ts +0 -24
- package/dist/runtime.js +0 -26
- package/dist/schema.d.ts +0 -17
- package/dist/schema.js +0 -13
- package/dist/seed.d.ts +0 -2
- package/dist/seed.js +0 -84
- package/dist/values.d.ts +0 -8
- package/dist/values.js +0 -102
- package/dist/webhooks.d.ts +0 -2
- package/dist/webhooks.js +0 -73
- package/routes/api/cms/assets/upload.ts +0 -112
- package/routes/api/cms/auth/login.ts +0 -124
- package/routes/api/cms/auth/setup.ts +0 -77
- package/routes/api/cms/img/[...path].ts +0 -24
- package/routes/pages/admin/[...path].astro +0 -1107
- package/routes/pages/admin/assets/index.astro +0 -63
- /package/routes/api/{cms/ai → ai}/alt-text.ts +0 -0
- /package/routes/api/{cms/ai → ai}/seo.ts +0 -0
- /package/routes/api/{cms/ai → ai}/translate.ts +0 -0
- /package/routes/api/{cms/assets → assets}/folders.ts +0 -0
- /package/routes/api/{cms/locks → locks}/[...path].ts +0 -0
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import { useState, useCallback, useRef } from "react";
|
|
3
|
+
import { useState, useCallback, useRef, useMemo, useEffect } from "react";
|
|
4
4
|
import {
|
|
5
|
+
ChevronLeft,
|
|
5
6
|
ChevronRight,
|
|
6
7
|
Folder,
|
|
7
8
|
FolderPlus,
|
|
8
9
|
GripVertical,
|
|
10
|
+
Images,
|
|
9
11
|
ImagePlus,
|
|
12
|
+
Inbox,
|
|
10
13
|
MoreHorizontal,
|
|
11
14
|
Pencil,
|
|
15
|
+
Search,
|
|
12
16
|
Trash2,
|
|
13
17
|
Upload,
|
|
14
18
|
} from "lucide-react";
|
|
@@ -52,6 +56,7 @@ type AssetItem = {
|
|
|
52
56
|
type FolderItem = {
|
|
53
57
|
_id: string;
|
|
54
58
|
name: string;
|
|
59
|
+
parent: string | null;
|
|
55
60
|
};
|
|
56
61
|
|
|
57
62
|
type Breadcrumb = {
|
|
@@ -64,9 +69,26 @@ type Props = {
|
|
|
64
69
|
folders: FolderItem[];
|
|
65
70
|
assets: AssetItem[];
|
|
66
71
|
breadcrumbs: Breadcrumb[];
|
|
67
|
-
|
|
72
|
+
// Active sidebar scope: "all" | "unfiled" | folderId.
|
|
73
|
+
scope: string;
|
|
74
|
+
search: string;
|
|
75
|
+
page: number;
|
|
76
|
+
totalPages: number;
|
|
77
|
+
total: number;
|
|
78
|
+
pageSize: number;
|
|
68
79
|
};
|
|
69
80
|
|
|
81
|
+
// Build a "/admin/assets" URL for a sidebar scope ("all" | "unfiled" | folderId).
|
|
82
|
+
// "all" is the bare route; "unfiled" and folder ids ride on the `folder` param.
|
|
83
|
+
function assetsUrl(opts: { scope?: string; q?: string; page?: number }) {
|
|
84
|
+
const sp = new URLSearchParams();
|
|
85
|
+
if (opts.scope && opts.scope !== "all") sp.set("folder", opts.scope);
|
|
86
|
+
if (opts.q?.trim()) sp.set("q", opts.q.trim());
|
|
87
|
+
if (opts.page && opts.page > 1) sp.set("page", String(opts.page));
|
|
88
|
+
const qs = sp.toString();
|
|
89
|
+
return qs ? `/admin/assets?${qs}` : "/admin/assets";
|
|
90
|
+
}
|
|
91
|
+
|
|
70
92
|
// -----------------------------------------------
|
|
71
93
|
// Draggable asset card
|
|
72
94
|
// -----------------------------------------------
|
|
@@ -151,83 +173,74 @@ function DraggableAssetCard({
|
|
|
151
173
|
}
|
|
152
174
|
|
|
153
175
|
// -----------------------------------------------
|
|
154
|
-
//
|
|
176
|
+
// Sidebar folder row (drop target + link)
|
|
155
177
|
// -----------------------------------------------
|
|
156
178
|
|
|
157
|
-
function
|
|
158
|
-
|
|
179
|
+
function FolderRow({
|
|
180
|
+
dropId,
|
|
181
|
+
dropFolderId,
|
|
182
|
+
droppable = true,
|
|
183
|
+
folderId,
|
|
184
|
+
label,
|
|
185
|
+
href,
|
|
186
|
+
depth,
|
|
187
|
+
active,
|
|
188
|
+
Icon,
|
|
159
189
|
onOpenMenu,
|
|
160
190
|
menuActive,
|
|
161
191
|
}: {
|
|
162
|
-
|
|
163
|
-
|
|
192
|
+
dropId: string;
|
|
193
|
+
// Folder value to move assets into when dropped here (null = unfiled/root).
|
|
194
|
+
dropFolderId: string | null;
|
|
195
|
+
// "All assets" is a view-only scope, not a drop target.
|
|
196
|
+
droppable?: boolean;
|
|
197
|
+
// Real folder id — enables the ⋯ options menu. null for "All"/"Unfiled".
|
|
198
|
+
folderId?: string | null;
|
|
199
|
+
label: string;
|
|
200
|
+
href: string;
|
|
201
|
+
depth: number;
|
|
202
|
+
active: boolean;
|
|
203
|
+
Icon: typeof Folder;
|
|
204
|
+
onOpenMenu?: (folderId: string, folderName: string, rect: DOMRect) => void;
|
|
164
205
|
menuActive?: boolean;
|
|
165
206
|
}) {
|
|
166
207
|
const { isOver, setNodeRef } = useDroppable({
|
|
167
|
-
id: `folder-${
|
|
168
|
-
data: { type: "folder", folderId:
|
|
208
|
+
id: `folder-${dropId}`,
|
|
209
|
+
data: { type: "folder", folderId: dropFolderId },
|
|
210
|
+
disabled: !droppable,
|
|
169
211
|
});
|
|
170
212
|
|
|
171
213
|
return (
|
|
172
|
-
<div ref={setNodeRef} className="group relative">
|
|
173
|
-
<a
|
|
174
|
-
|
|
175
|
-
className={cn(
|
|
176
|
-
"transition-shadow",
|
|
177
|
-
isOver ? "ring-foreground/40 bg-primary/8 ring-1" : "hover:ring-foreground/20 hover:ring-1",
|
|
178
|
-
)}
|
|
179
|
-
>
|
|
180
|
-
<CardContent className="flex items-center gap-3 py-3">
|
|
181
|
-
<Folder className="text-muted-foreground size-5" />
|
|
182
|
-
<span className="truncate text-sm font-medium">{folder.name}</span>
|
|
183
|
-
</CardContent>
|
|
184
|
-
</Card>
|
|
185
|
-
</a>
|
|
186
|
-
<div
|
|
214
|
+
<div ref={setNodeRef} className="group/row relative" style={{ paddingLeft: `${depth * 0.75}rem` }}>
|
|
215
|
+
<a
|
|
216
|
+
href={href}
|
|
187
217
|
className={cn(
|
|
188
|
-
"
|
|
189
|
-
|
|
218
|
+
"flex items-center gap-2 rounded-md py-1.5 pr-7 pl-2 text-sm transition-colors",
|
|
219
|
+
active ? "bg-foreground/10 text-foreground" : "text-foreground/70 hover:bg-foreground/5",
|
|
220
|
+
isOver && droppable && "bg-primary/15 text-foreground",
|
|
190
221
|
)}
|
|
191
222
|
>
|
|
223
|
+
<Icon className="text-muted-foreground size-4 shrink-0" />
|
|
224
|
+
<span className="truncate">{label}</span>
|
|
225
|
+
</a>
|
|
226
|
+
{onOpenMenu && folderId && (
|
|
192
227
|
<button
|
|
193
228
|
type="button"
|
|
194
229
|
title="Folder options"
|
|
195
|
-
className=
|
|
230
|
+
className={cn(
|
|
231
|
+
"text-muted-foreground hover:text-foreground absolute top-1/2 right-1 -translate-y-1/2 rounded p-1 transition-opacity",
|
|
232
|
+
menuActive ? "opacity-100" : "opacity-0 group-hover/row:opacity-100",
|
|
233
|
+
)}
|
|
196
234
|
onClick={(e) => {
|
|
197
235
|
e.preventDefault();
|
|
198
236
|
e.stopPropagation();
|
|
199
|
-
|
|
200
|
-
onOpenMenu(folder._id, folder.name, rect);
|
|
237
|
+
onOpenMenu(folderId, label, e.currentTarget.getBoundingClientRect());
|
|
201
238
|
}}
|
|
202
239
|
>
|
|
203
240
|
<MoreHorizontal className="size-4" />
|
|
204
241
|
</button>
|
|
205
|
-
</div>
|
|
206
|
-
</div>
|
|
207
|
-
);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// -----------------------------------------------
|
|
211
|
-
// Droppable breadcrumb
|
|
212
|
-
// -----------------------------------------------
|
|
213
|
-
|
|
214
|
-
function DroppableBreadcrumb({ href, folderId, label }: { href: string; folderId: string; label: string }) {
|
|
215
|
-
const { isOver, setNodeRef } = useDroppable({
|
|
216
|
-
id: `breadcrumb-${folderId}`,
|
|
217
|
-
data: { type: "folder", folderId: folderId || null },
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
return (
|
|
221
|
-
<a
|
|
222
|
-
ref={setNodeRef}
|
|
223
|
-
href={href}
|
|
224
|
-
className={cn(
|
|
225
|
-
"bg-muted/50 border-border hover:border-foreground/20 hover:text-foreground rounded-md border px-2.5 py-1 transition-colors",
|
|
226
|
-
isOver && "border-primary! bg-primary/12! text-foreground",
|
|
227
242
|
)}
|
|
228
|
-
>
|
|
229
|
-
{label}
|
|
230
|
-
</a>
|
|
243
|
+
</div>
|
|
231
244
|
);
|
|
232
245
|
}
|
|
233
246
|
|
|
@@ -237,12 +250,12 @@ function DroppableBreadcrumb({ href, folderId, label }: { href: string; folderId
|
|
|
237
250
|
|
|
238
251
|
function AssetDragOverlay({ asset }: { asset: AssetItem }) {
|
|
239
252
|
return (
|
|
240
|
-
<div className="w-
|
|
253
|
+
<div className="w-40" style={{ transform: "rotate(2deg) scale(0.95)" }}>
|
|
241
254
|
<Card className="overflow-hidden pt-0 shadow-lg">
|
|
242
255
|
<div className="relative">
|
|
243
256
|
{asset.mimeType.startsWith("image/") ? (
|
|
244
257
|
<div className="bg-muted/30 aspect-square w-full overflow-hidden">
|
|
245
|
-
<img src={asset.url} alt={asset.alt ?? asset.filename} className="size-full object-cover" />
|
|
258
|
+
<img src={thumbnail(asset.url)} alt={asset.alt ?? asset.filename} className="size-full object-cover" />
|
|
246
259
|
</div>
|
|
247
260
|
) : (
|
|
248
261
|
<div className="bg-muted/30 flex aspect-square items-center justify-center">
|
|
@@ -262,9 +275,34 @@ function AssetDragOverlay({ asset }: { asset: AssetItem }) {
|
|
|
262
275
|
// Main component
|
|
263
276
|
// -----------------------------------------------
|
|
264
277
|
|
|
265
|
-
|
|
278
|
+
// Match the Cmd+K command palette: short debounce, abortable in-flight request,
|
|
279
|
+
// keep the previous results visible while the next batch loads.
|
|
280
|
+
const SEARCH_DEBOUNCE_MS = 150;
|
|
281
|
+
|
|
282
|
+
export default function AssetsGrid({
|
|
283
|
+
folders,
|
|
284
|
+
assets: initialAssets,
|
|
285
|
+
scope,
|
|
286
|
+
search,
|
|
287
|
+
page: initialPage,
|
|
288
|
+
totalPages: initialTotalPages,
|
|
289
|
+
pageSize,
|
|
290
|
+
}: Props) {
|
|
266
291
|
const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set());
|
|
267
292
|
const [activeAsset, setActiveAsset] = useState<AssetItem | null>(null);
|
|
293
|
+
const [query, setQuery] = useState(search);
|
|
294
|
+
|
|
295
|
+
// Real folder id for the current scope (null for the "all"/"unfiled" scopes) —
|
|
296
|
+
// used for the upload target and the drag-drop reload.
|
|
297
|
+
const currentFolderId = scope === "all" || scope === "unfiled" ? null : scope;
|
|
298
|
+
|
|
299
|
+
// Live asset list: seeded from the SSR render, then updated in place by the
|
|
300
|
+
// debounced filter and client-side pagination (no full page reload).
|
|
301
|
+
const [items, setItems] = useState<AssetItem[]>(initialAssets);
|
|
302
|
+
const [page, setPage] = useState(initialPage);
|
|
303
|
+
const [totalPages, setTotalPages] = useState(initialTotalPages);
|
|
304
|
+
const [listLoading, setListLoading] = useState(false);
|
|
305
|
+
const abortRef = useRef<AbortController | null>(null);
|
|
268
306
|
|
|
269
307
|
// Folder dialog state
|
|
270
308
|
const [createOpen, setCreateOpen] = useState(false);
|
|
@@ -286,6 +324,92 @@ export default function AssetsGrid({ folders, assets, breadcrumbs, currentFolder
|
|
|
286
324
|
|
|
287
325
|
const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 5 } }));
|
|
288
326
|
|
|
327
|
+
// On lg+ the folder sidebar is sticky and always visible, so dnd-kit's
|
|
328
|
+
// edge auto-scroll only causes the page to lurch while dragging toward it.
|
|
329
|
+
// Below lg the folders sit above the grid and auto-scroll is needed.
|
|
330
|
+
const [sidebarVisible, setSidebarVisible] = useState(false);
|
|
331
|
+
useEffect(() => {
|
|
332
|
+
const query = window.matchMedia("(min-width: 1024px)");
|
|
333
|
+
const update = () => setSidebarVisible(query.matches);
|
|
334
|
+
update();
|
|
335
|
+
query.addEventListener("change", update);
|
|
336
|
+
return () => query.removeEventListener("change", update);
|
|
337
|
+
}, []);
|
|
338
|
+
|
|
339
|
+
// Flatten the folder tree into depth-tagged rows for the sidebar.
|
|
340
|
+
const folderTree = useMemo(() => {
|
|
341
|
+
const byParent = new Map<string, FolderItem[]>();
|
|
342
|
+
for (const f of folders) {
|
|
343
|
+
const key = f.parent ?? "";
|
|
344
|
+
if (!byParent.has(key)) byParent.set(key, []);
|
|
345
|
+
byParent.get(key)!.push(f);
|
|
346
|
+
}
|
|
347
|
+
const rows: Array<{ folder: FolderItem; depth: number }> = [];
|
|
348
|
+
const walk = (parentKey: string, depth: number) => {
|
|
349
|
+
const children = (byParent.get(parentKey) ?? []).sort((a, b) => a.name.localeCompare(b.name));
|
|
350
|
+
for (const folder of children) {
|
|
351
|
+
rows.push({ folder, depth });
|
|
352
|
+
walk(folder._id, depth + 1);
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
walk("", 0);
|
|
356
|
+
return rows;
|
|
357
|
+
}, [folders]);
|
|
358
|
+
|
|
359
|
+
// --- Live filter + pagination (client-side, mirrors the Cmd+K palette) ---
|
|
360
|
+
|
|
361
|
+
// Translate the sidebar scope into the API's `folder` param:
|
|
362
|
+
// "all" → omit (unscoped) · "unfiled" → "" (folder IS NULL) · id → that folder.
|
|
363
|
+
const scopeFolderParam = scope === "all" ? null : scope === "unfiled" ? "" : scope;
|
|
364
|
+
|
|
365
|
+
const fetchPage = useCallback(
|
|
366
|
+
async (nextPage: number, q: string) => {
|
|
367
|
+
abortRef.current?.abort();
|
|
368
|
+
const controller = new AbortController();
|
|
369
|
+
abortRef.current = controller;
|
|
370
|
+
setListLoading(true);
|
|
371
|
+
|
|
372
|
+
const sp = new URLSearchParams();
|
|
373
|
+
sp.set("limit", String(pageSize));
|
|
374
|
+
sp.set("offset", String((nextPage - 1) * pageSize));
|
|
375
|
+
if (scopeFolderParam !== null) sp.set("folder", scopeFolderParam);
|
|
376
|
+
if (q.trim()) sp.set("q", q.trim());
|
|
377
|
+
|
|
378
|
+
try {
|
|
379
|
+
const res = await fetch(`/api/cms/assets?${sp.toString()}`, {
|
|
380
|
+
signal: controller.signal,
|
|
381
|
+
credentials: "same-origin",
|
|
382
|
+
});
|
|
383
|
+
if (!res.ok) {
|
|
384
|
+
setListLoading(false);
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const data = (await res.json()) as { items?: AssetItem[]; total?: number };
|
|
388
|
+
const nextTotalPages = Math.max(1, Math.ceil((data.total ?? 0) / pageSize));
|
|
389
|
+
setItems(Array.isArray(data.items) ? data.items : []);
|
|
390
|
+
setTotalPages(nextTotalPages);
|
|
391
|
+
setPage(Math.min(nextPage, nextTotalPages));
|
|
392
|
+
setListLoading(false);
|
|
393
|
+
} catch (err) {
|
|
394
|
+
// Ignore aborts (a newer keystroke superseded this request).
|
|
395
|
+
if ((err as { name?: string })?.name !== "AbortError") setListLoading(false);
|
|
396
|
+
}
|
|
397
|
+
},
|
|
398
|
+
[pageSize, scopeFolderParam],
|
|
399
|
+
);
|
|
400
|
+
|
|
401
|
+
// Debounce the filter box: re-query from page 1 on every keystroke. Skip the
|
|
402
|
+
// initial mount — the SSR render already reflects the URL's `search`.
|
|
403
|
+
const didMount = useRef(false);
|
|
404
|
+
useEffect(() => {
|
|
405
|
+
if (!didMount.current) {
|
|
406
|
+
didMount.current = true;
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
const timer = setTimeout(() => fetchPage(1, query), SEARCH_DEBOUNCE_MS);
|
|
410
|
+
return () => clearTimeout(timer);
|
|
411
|
+
}, [query, fetchPage]);
|
|
412
|
+
|
|
289
413
|
// --- Selection ---
|
|
290
414
|
|
|
291
415
|
const toggleSelect = useCallback((id: string) => {
|
|
@@ -303,11 +427,11 @@ export default function AssetsGrid({ folders, assets, breadcrumbs, currentFolder
|
|
|
303
427
|
|
|
304
428
|
const handleDragStart = useCallback(
|
|
305
429
|
(event: DragStartEvent) => {
|
|
306
|
-
const asset =
|
|
430
|
+
const asset = items.find((a) => a._id === event.active.id);
|
|
307
431
|
if (asset) setActiveAsset(asset);
|
|
308
432
|
document.body.style.cursor = "grabbing";
|
|
309
433
|
},
|
|
310
|
-
[
|
|
434
|
+
[items],
|
|
311
435
|
);
|
|
312
436
|
|
|
313
437
|
const handleDragEnd = useCallback((event: DragEndEvent) => {
|
|
@@ -320,12 +444,10 @@ export default function AssetsGrid({ folders, assets, breadcrumbs, currentFolder
|
|
|
320
444
|
const dropData = over.data.current as { type: string; folderId: string | null } | undefined;
|
|
321
445
|
if (!dropData || dropData.type !== "folder") return;
|
|
322
446
|
|
|
323
|
-
const targetFolderId = dropData.folderId;
|
|
324
|
-
|
|
325
447
|
fetch(`/api/cms/assets/${assetId}`, {
|
|
326
448
|
method: "PATCH",
|
|
327
449
|
headers: { "Content-Type": "application/json" },
|
|
328
|
-
body: JSON.stringify({ folder:
|
|
450
|
+
body: JSON.stringify({ folder: dropData.folderId || null }),
|
|
329
451
|
}).then((res) => {
|
|
330
452
|
if (res.ok) location.reload();
|
|
331
453
|
});
|
|
@@ -411,144 +533,215 @@ export default function AssetsGrid({ folders, assets, breadcrumbs, currentFolder
|
|
|
411
533
|
location.reload();
|
|
412
534
|
}
|
|
413
535
|
|
|
414
|
-
const hasBreadcrumbs = breadcrumbs.length > 0;
|
|
415
|
-
const isEmpty = folders.length === 0 && assets.length === 0;
|
|
416
|
-
|
|
417
536
|
return (
|
|
418
537
|
<DndContext
|
|
419
538
|
sensors={sensors}
|
|
420
539
|
collisionDetection={pointerWithin}
|
|
540
|
+
autoScroll={!sidebarVisible}
|
|
421
541
|
onDragStart={handleDragStart}
|
|
422
542
|
onDragEnd={handleDragEnd}
|
|
423
543
|
onDragCancel={handleDragCancel}
|
|
424
544
|
>
|
|
425
|
-
<section className="
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
545
|
+
<section className="px-4 py-6 lg:py-8 lg:pr-8 lg:pl-12">
|
|
546
|
+
<h1 className="mb-6 text-2xl font-semibold tracking-tight">Assets</h1>
|
|
547
|
+
|
|
548
|
+
<div className="grid gap-8 lg:grid-cols-[230px_minmax(0,1fr)] lg:gap-0">
|
|
549
|
+
{/* ── Sticky sidebar: actions + folder tree (always-on drop targets) ── */}
|
|
550
|
+
<aside className="lg:sticky lg:top-8 lg:max-h-[calc(100vh-4rem)] lg:self-start lg:overflow-y-auto lg:pr-8">
|
|
551
|
+
<div className="mb-1 flex items-center justify-between px-2">
|
|
552
|
+
<span className="text-muted-foreground text-xs font-medium tracking-wide uppercase">Folders</span>
|
|
553
|
+
<button
|
|
554
|
+
type="button"
|
|
555
|
+
title="New folder"
|
|
556
|
+
className="text-muted-foreground hover:text-foreground rounded p-1"
|
|
557
|
+
onClick={() => {
|
|
558
|
+
setCreateName("");
|
|
559
|
+
resetDialogState();
|
|
560
|
+
setCreateOpen(true);
|
|
561
|
+
}}
|
|
562
|
+
>
|
|
563
|
+
<FolderPlus className="size-4" />
|
|
564
|
+
</button>
|
|
565
|
+
</div>
|
|
566
|
+
<nav className="space-y-0.5">
|
|
567
|
+
{/* View-only: shows every asset across all folders. Not a drop target. */}
|
|
568
|
+
<FolderRow
|
|
569
|
+
dropId="all"
|
|
570
|
+
dropFolderId={null}
|
|
571
|
+
droppable={false}
|
|
572
|
+
label="All assets"
|
|
573
|
+
href={assetsUrl({ scope: "all" })}
|
|
574
|
+
depth={0}
|
|
575
|
+
active={scope === "all"}
|
|
576
|
+
Icon={Images}
|
|
577
|
+
/>
|
|
578
|
+
{/* Assets not in any folder. Drop here to remove an asset from its folder. */}
|
|
579
|
+
<FolderRow
|
|
580
|
+
dropId="unfiled"
|
|
581
|
+
dropFolderId={null}
|
|
582
|
+
label="Unfiled"
|
|
583
|
+
href={assetsUrl({ scope: "unfiled" })}
|
|
584
|
+
depth={0}
|
|
585
|
+
active={scope === "unfiled"}
|
|
586
|
+
Icon={Inbox}
|
|
587
|
+
/>
|
|
588
|
+
{folderTree.map(({ folder, depth }) => (
|
|
589
|
+
<FolderRow
|
|
590
|
+
key={folder._id}
|
|
591
|
+
dropId={folder._id}
|
|
592
|
+
dropFolderId={folder._id}
|
|
593
|
+
folderId={folder._id}
|
|
594
|
+
label={folder.name}
|
|
595
|
+
href={assetsUrl({ scope: folder._id })}
|
|
596
|
+
depth={depth}
|
|
597
|
+
active={scope === folder._id}
|
|
598
|
+
Icon={Folder}
|
|
599
|
+
onOpenMenu={openMenu}
|
|
600
|
+
menuActive={menuOpen && activeFolderId === folder._id}
|
|
601
|
+
/>
|
|
602
|
+
))}
|
|
603
|
+
</nav>
|
|
604
|
+
</aside>
|
|
605
|
+
|
|
606
|
+
{/* ── Main: search, grid, pagination ── */}
|
|
607
|
+
<div className="min-w-0 space-y-4 lg:border-l lg:pl-8">
|
|
608
|
+
<div className="top-0 z-20 flex items-center justify-between gap-3 pt-1 pb-4">
|
|
609
|
+
<div className="relative w-full sm:max-w-xs">
|
|
610
|
+
<Search className="text-muted-foreground pointer-events-none absolute top-1/2 left-3 size-4 -translate-y-1/2" />
|
|
611
|
+
<Input
|
|
612
|
+
value={query}
|
|
613
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
614
|
+
placeholder="Filter by name or alt…"
|
|
615
|
+
className="pl-9 text-sm"
|
|
616
|
+
/>
|
|
617
|
+
</div>
|
|
618
|
+
<label className={cn(buttonVariants(), "shrink-0 cursor-pointer")}>
|
|
619
|
+
<Upload className="size-4" />
|
|
620
|
+
Upload
|
|
621
|
+
<form
|
|
622
|
+
ref={uploadFormRef}
|
|
623
|
+
method="post"
|
|
624
|
+
action="/api/cms/assets/upload"
|
|
625
|
+
encType="multipart/form-data"
|
|
626
|
+
className="hidden"
|
|
627
|
+
>
|
|
628
|
+
<input type="hidden" name="redirectTo" value={assetsUrl({ scope })} />
|
|
629
|
+
{currentFolderId && <input type="hidden" name="folder" value={currentFolderId} />}
|
|
630
|
+
<input
|
|
631
|
+
type="file"
|
|
632
|
+
name="file"
|
|
633
|
+
accept="image/*,video/*,audio/*,.pdf,.doc,.docx"
|
|
634
|
+
multiple
|
|
635
|
+
required
|
|
636
|
+
onChange={(e) => {
|
|
637
|
+
if (e.target.value) uploadFormRef.current?.submit();
|
|
638
|
+
}}
|
|
639
|
+
/>
|
|
640
|
+
</form>
|
|
641
|
+
</label>
|
|
642
|
+
</div>
|
|
643
|
+
|
|
644
|
+
{/* Selection toolbar */}
|
|
645
|
+
{selectedIds.size > 0 && (
|
|
646
|
+
<div className="bg-muted/40 flex items-center justify-between gap-3 rounded-lg border px-3 py-2">
|
|
647
|
+
<span className="text-sm font-medium">{selectedIds.size} selected</span>
|
|
648
|
+
<div className="flex items-center gap-2">
|
|
649
|
+
<Button
|
|
650
|
+
variant="destructive"
|
|
651
|
+
size="sm"
|
|
652
|
+
onClick={() => {
|
|
653
|
+
resetDialogState();
|
|
654
|
+
setBulkDeleteOpen(true);
|
|
655
|
+
}}
|
|
656
|
+
>
|
|
657
|
+
<Trash2 className="size-3.5" />
|
|
658
|
+
Delete
|
|
659
|
+
</Button>
|
|
660
|
+
<Button variant="outline" size="sm" onClick={clearSelection}>
|
|
661
|
+
Cancel
|
|
662
|
+
</Button>
|
|
663
|
+
</div>
|
|
664
|
+
</div>
|
|
665
|
+
)}
|
|
666
|
+
|
|
667
|
+
{/* Grid or empty state. Keep the current grid visible while a new
|
|
668
|
+
filter/page loads (dimmed) to avoid an empty-then-fill flash. */}
|
|
669
|
+
{items.length > 0 ? (
|
|
670
|
+
<div
|
|
671
|
+
className={cn(
|
|
672
|
+
"grid gap-4 transition-opacity sm:grid-cols-2 lg:grid-cols-3 2xl:grid-cols-4",
|
|
673
|
+
listLoading && "opacity-60",
|
|
674
|
+
)}
|
|
675
|
+
>
|
|
676
|
+
{items.map((asset) => (
|
|
677
|
+
<DraggableAssetCard
|
|
678
|
+
key={asset._id}
|
|
679
|
+
asset={asset}
|
|
680
|
+
selected={selectedIds.has(asset._id)}
|
|
681
|
+
onToggleSelect={() => toggleSelect(asset._id)}
|
|
682
|
+
/>
|
|
438
683
|
))}
|
|
439
|
-
</
|
|
684
|
+
</div>
|
|
685
|
+
) : listLoading ? (
|
|
686
|
+
<Card>
|
|
687
|
+
<CardContent className="text-muted-foreground py-12 text-center text-sm">Searching…</CardContent>
|
|
688
|
+
</Card>
|
|
689
|
+
) : (
|
|
690
|
+
<Card>
|
|
691
|
+
<CardContent className="py-12">
|
|
692
|
+
<div className="text-center">
|
|
693
|
+
<ImagePlus className="text-muted-foreground/30 mx-auto size-12" />
|
|
694
|
+
<p className="text-muted-foreground mt-3 text-sm">
|
|
695
|
+
{query.trim()
|
|
696
|
+
? `No assets match “${query.trim()}”.`
|
|
697
|
+
: scope === "unfiled"
|
|
698
|
+
? "No unfiled assets."
|
|
699
|
+
: scope === "all"
|
|
700
|
+
? "No assets uploaded yet."
|
|
701
|
+
: "This folder is empty."}
|
|
702
|
+
</p>
|
|
703
|
+
</div>
|
|
704
|
+
</CardContent>
|
|
705
|
+
</Card>
|
|
440
706
|
)}
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
{
|
|
444
|
-
|
|
445
|
-
<span className="text-muted-foreground text-sm">{selectedIds.size} selected</span>
|
|
707
|
+
|
|
708
|
+
{/* Pagination (client-side — keeps the active filter without a reload) */}
|
|
709
|
+
{totalPages > 1 && (
|
|
710
|
+
<div className="flex items-center justify-center gap-2 border-t pt-5">
|
|
446
711
|
<Button
|
|
447
|
-
variant="
|
|
712
|
+
variant="outline"
|
|
448
713
|
size="sm"
|
|
449
|
-
onClick={() =>
|
|
450
|
-
|
|
451
|
-
setBulkDeleteOpen(true);
|
|
452
|
-
}}
|
|
714
|
+
onClick={() => fetchPage(page - 1, query)}
|
|
715
|
+
disabled={page <= 1 || listLoading}
|
|
453
716
|
>
|
|
454
|
-
<
|
|
455
|
-
|
|
456
|
-
</Button>
|
|
457
|
-
<Button variant="outline" size="sm" onClick={clearSelection}>
|
|
458
|
-
Cancel
|
|
717
|
+
<ChevronLeft className="size-4" />
|
|
718
|
+
Previous
|
|
459
719
|
</Button>
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
720
|
+
<span className="text-muted-foreground px-2 text-sm">
|
|
721
|
+
Page {page} of {totalPages}
|
|
722
|
+
</span>
|
|
463
723
|
<Button
|
|
464
724
|
variant="outline"
|
|
465
725
|
size="sm"
|
|
466
|
-
onClick={() =>
|
|
467
|
-
|
|
468
|
-
resetDialogState();
|
|
469
|
-
setCreateOpen(true);
|
|
470
|
-
}}
|
|
726
|
+
onClick={() => fetchPage(page + 1, query)}
|
|
727
|
+
disabled={page >= totalPages || listLoading}
|
|
471
728
|
>
|
|
472
|
-
|
|
473
|
-
|
|
729
|
+
Next
|
|
730
|
+
<ChevronRight className="size-4" />
|
|
474
731
|
</Button>
|
|
475
|
-
|
|
476
|
-
<Upload className="size-4" />
|
|
477
|
-
Upload
|
|
478
|
-
<form
|
|
479
|
-
ref={uploadFormRef}
|
|
480
|
-
method="post"
|
|
481
|
-
action="/api/cms/assets/upload"
|
|
482
|
-
encType="multipart/form-data"
|
|
483
|
-
className="hidden"
|
|
484
|
-
>
|
|
485
|
-
<input type="hidden" name="redirectTo" value="/admin/assets" />
|
|
486
|
-
{currentFolderId && <input type="hidden" name="folder" value={currentFolderId} />}
|
|
487
|
-
<input
|
|
488
|
-
type="file"
|
|
489
|
-
name="file"
|
|
490
|
-
accept="image/*,video/*,audio/*,.pdf,.doc,.docx"
|
|
491
|
-
required
|
|
492
|
-
onChange={(e) => {
|
|
493
|
-
if (e.target.value) uploadFormRef.current?.submit();
|
|
494
|
-
}}
|
|
495
|
-
/>
|
|
496
|
-
</form>
|
|
497
|
-
</label>
|
|
498
|
-
</>
|
|
732
|
+
</div>
|
|
499
733
|
)}
|
|
500
734
|
</div>
|
|
501
735
|
</div>
|
|
502
|
-
|
|
503
|
-
{/* Folders */}
|
|
504
|
-
{folders.length > 0 && (
|
|
505
|
-
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 2xl:grid-cols-5">
|
|
506
|
-
{folders.map((folder) => (
|
|
507
|
-
<DroppableFolder
|
|
508
|
-
key={folder._id}
|
|
509
|
-
folder={folder}
|
|
510
|
-
onOpenMenu={openMenu}
|
|
511
|
-
menuActive={menuOpen && activeFolderId === folder._id}
|
|
512
|
-
/>
|
|
513
|
-
))}
|
|
514
|
-
</div>
|
|
515
|
-
)}
|
|
516
|
-
|
|
517
|
-
{/* Assets or empty state */}
|
|
518
|
-
{isEmpty ? (
|
|
519
|
-
<Card>
|
|
520
|
-
<CardContent className="py-12">
|
|
521
|
-
<div className="text-center">
|
|
522
|
-
<ImagePlus className="text-muted-foreground/30 mx-auto size-12" />
|
|
523
|
-
<p className="text-muted-foreground mt-3 text-sm">
|
|
524
|
-
{currentFolderId ? "This folder is empty." : "No assets uploaded yet."}
|
|
525
|
-
</p>
|
|
526
|
-
</div>
|
|
527
|
-
</CardContent>
|
|
528
|
-
</Card>
|
|
529
|
-
) : (
|
|
530
|
-
assets.length > 0 && (
|
|
531
|
-
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 2xl:grid-cols-5">
|
|
532
|
-
{assets.map((asset) => (
|
|
533
|
-
<DraggableAssetCard
|
|
534
|
-
key={asset._id}
|
|
535
|
-
asset={asset}
|
|
536
|
-
selected={selectedIds.has(asset._id)}
|
|
537
|
-
onToggleSelect={() => toggleSelect(asset._id)}
|
|
538
|
-
/>
|
|
539
|
-
))}
|
|
540
|
-
</div>
|
|
541
|
-
)
|
|
542
|
-
)}
|
|
543
736
|
</section>
|
|
544
737
|
|
|
545
738
|
{/* Drag overlay */}
|
|
546
739
|
<DragOverlay dropAnimation={null}>{activeAsset && <AssetDragOverlay asset={activeAsset} />}</DragOverlay>
|
|
547
740
|
|
|
548
|
-
{/*
|
|
741
|
+
{/* Folder context menu */}
|
|
549
742
|
{menuOpen && (
|
|
550
743
|
<div
|
|
551
|
-
className="bg-popover text-popover-foreground
|
|
744
|
+
className="bg-popover text-popover-foreground fixed z-50 min-w-40 rounded-md border p-1 shadow-md"
|
|
552
745
|
style={{ top: menuPos.top, left: menuPos.left }}
|
|
553
746
|
onClick={(e) => e.stopPropagation()}
|
|
554
747
|
>
|
|
@@ -660,8 +853,8 @@ export default function AssetsGrid({ folders, assets, breadcrumbs, currentFolder
|
|
|
660
853
|
<DialogHeader>
|
|
661
854
|
<DialogTitle>Delete folder</DialogTitle>
|
|
662
855
|
<DialogDescription>
|
|
663
|
-
Are you sure you want to delete “{activeFolderName}”?
|
|
664
|
-
|
|
856
|
+
Are you sure you want to delete “{activeFolderName}”? Its assets move to Unfiled, and any
|
|
857
|
+
subfolders move to the top level.
|
|
665
858
|
</DialogDescription>
|
|
666
859
|
</DialogHeader>
|
|
667
860
|
{error && <p className="text-destructive text-sm">{error}</p>}
|