openadminjs 0.1.16 → 0.1.17
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/create-project.js +4 -1
- package/dist/index.js +127 -0
- package/package.json +2 -2
- package/template/apps/admin/.storybook/main.ts +12 -0
- package/template/apps/admin/.storybook/preview.ts +10 -0
- package/template/apps/admin/app/dashboard/page.tsx +357 -277
- package/template/apps/admin/app/layout.tsx +5 -1
- package/template/apps/admin/app/login/page.tsx +38 -0
- package/template/apps/admin/app/resources/[resource]/page.tsx +15 -1
- package/template/apps/admin/app/settings/page.tsx +40 -2
- package/template/apps/admin/app/system-logs/page.tsx +212 -0
- package/template/apps/admin/components/api-error-banner.tsx +26 -0
- package/template/apps/admin/components/app-shell.tsx +92 -18
- package/template/apps/admin/components/logo.stories.tsx +24 -0
- package/template/apps/admin/components/resource-form.tsx +47 -0
- package/template/apps/admin/components/resource-table.tsx +417 -44
- package/template/apps/admin/lib/api.ts +44 -1
- package/template/apps/admin/lib/resource-table-prefs.ts +57 -0
- package/template/apps/admin/package.json +6 -0
- package/template/apps/admin/storybook-static/370.e922f076.iframe.bundle.js +9 -0
- package/template/apps/admin/storybook-static/389.c0dd6bde.iframe.bundle.js +818 -0
- package/template/apps/admin/storybook-static/990.36428b6c.iframe.bundle.js +25 -0
- package/template/apps/admin/storybook-static/favicon-wrapper.svg +46 -0
- package/template/apps/admin/storybook-static/favicon.svg +1 -0
- package/template/apps/admin/storybook-static/iframe.html +560 -0
- package/template/apps/admin/storybook-static/index.html +139 -0
- package/template/apps/admin/storybook-static/index.json +1 -0
- package/template/apps/admin/storybook-static/logo-stories.c470fc8c.iframe.bundle.js +1 -0
- package/template/apps/admin/storybook-static/main.743ea6f2.iframe.bundle.js +2550 -0
- package/template/apps/admin/storybook-static/mocker-runtime-injected.js +2 -0
- package/template/apps/admin/storybook-static/nunito-sans-bold-italic.woff2 +0 -0
- package/template/apps/admin/storybook-static/nunito-sans-bold.woff2 +0 -0
- package/template/apps/admin/storybook-static/nunito-sans-italic.woff2 +0 -0
- package/template/apps/admin/storybook-static/nunito-sans-regular.woff2 +0 -0
- package/template/apps/admin/storybook-static/project.json +1 -0
- package/template/apps/admin/storybook-static/runtime~main.6f822e3b.iframe.bundle.js +2 -0
- package/template/apps/admin/storybook-static/sb-addons/onboarding-1/manager-bundle.js +97 -0
- package/template/apps/admin/storybook-static/sb-addons/storybook-core-server-presets-0/common-manager-bundle.js +630 -0
- package/template/apps/admin/storybook-static/sb-common-assets/favicon-wrapper.svg +46 -0
- package/template/apps/admin/storybook-static/sb-common-assets/favicon.svg +1 -0
- package/template/apps/admin/storybook-static/sb-common-assets/nunito-sans-bold-italic.woff2 +0 -0
- package/template/apps/admin/storybook-static/sb-common-assets/nunito-sans-bold.woff2 +0 -0
- package/template/apps/admin/storybook-static/sb-common-assets/nunito-sans-italic.woff2 +0 -0
- package/template/apps/admin/storybook-static/sb-common-assets/nunito-sans-regular.woff2 +0 -0
- package/template/apps/admin/storybook-static/sb-manager/globals-runtime.js +76309 -0
- package/template/apps/admin/storybook-static/sb-manager/globals.js +24 -0
- package/template/apps/admin/storybook-static/sb-manager/manager-stores.js +23 -0
- package/template/apps/admin/storybook-static/sb-manager/runtime.js +26078 -0
- package/template/apps/admin/storybook-static/sb-preview/globals.js +20 -0
- package/template/apps/admin/storybook-static/sb-preview/runtime.js +35472 -0
- package/template/apps/api/package.json +13 -2
- package/template/apps/api/plugins.manifest.example.json +12 -0
- package/template/apps/api/src/admin/admin-logs.controller.ts +31 -0
- package/template/apps/api/src/admin/admin-plugins.controller.ts +5 -0
- package/template/apps/api/src/admin/admin-plugins.service.ts +11 -0
- package/template/apps/api/src/admin/admin-resource.service.ts +235 -1
- package/template/apps/api/src/admin/admin.controller.ts +35 -0
- package/template/apps/api/src/admin/admin.module.ts +6 -3
- package/template/apps/api/src/admin/dto/resource-bulk.dto.ts +17 -0
- package/template/apps/api/src/admin/dto/resource-reorder.dto.ts +17 -0
- package/template/apps/api/src/admin/dto/resource-upload.dto.ts +20 -0
- package/template/apps/api/src/app.module.ts +45 -5
- package/template/apps/api/src/auth/auth.module.ts +4 -2
- package/template/apps/api/src/auth/auth.service.test.ts +6 -3
- package/template/apps/api/src/auth/auth.service.ts +84 -0
- package/template/apps/api/src/auth/sso-oidc.controller.ts +78 -0
- package/template/apps/api/src/auth/sso-oidc.service.test.ts +84 -0
- package/template/apps/api/src/auth/sso-oidc.service.ts +281 -0
- package/template/apps/api/src/common/app-logger.service.ts +138 -0
- package/template/apps/api/src/common/auth.guard.test.ts +1 -0
- package/template/apps/api/src/common/auth.guard.ts +15 -1
- package/template/apps/api/src/common/current-user.decorator.ts +6 -2
- package/template/apps/api/src/common/logging.module.ts +10 -0
- package/template/apps/api/src/common/request-logging.interceptor.ts +70 -0
- package/template/apps/api/src/graphql/admin-resources.resolver.test.ts +44 -0
- package/template/apps/api/src/graphql/admin-resources.resolver.ts +65 -0
- package/template/apps/api/src/graphql/graphql.module.ts +26 -0
- package/template/apps/api/src/main.ts +2 -0
- package/template/apps/api/src/plugins/bundled/registry.ts +3 -1
- package/template/apps/api/src/plugins/bundled/webhook.plugin.ts +64 -0
- package/template/apps/api/src/resources/categories.resource.ts +1 -0
- package/template/apps/api/src/resources/system.resource.ts +1 -0
- package/template/packages/core/src/index.ts +5 -0
- package/template/packages/core/vitest.config.ts +9 -0
- package/template/packages/resource/package.json +11 -0
- package/template/packages/resource/src/index.test.ts +45 -0
- package/template/packages/resource/src/index.ts +63 -0
- package/template/prisma/schema.prisma +5 -0
- package/template/packages/core/src/index.js +0 -1
- package/template/packages/permissions/src/blueprints.js +0 -57
- package/template/packages/permissions/src/index.js +0 -11
- package/template/packages/resource/src/index.js +0 -177
|
@@ -164,6 +164,7 @@ function RelationPicker({ field, value, onChange, disabled }: RelationPickerProp
|
|
|
164
164
|
export function ResourceForm({ resource, mode, recordId, initialData }: Props) {
|
|
165
165
|
const router = useRouter();
|
|
166
166
|
const [submitting, setSubmitting] = useState(false);
|
|
167
|
+
const [selectedUpload, setSelectedUpload] = useState<File | null>(null);
|
|
167
168
|
const [error, setError] = useState<string | null>(null);
|
|
168
169
|
const [fieldErrors, setFieldErrors] = useState<Record<string, string[]>>({});
|
|
169
170
|
const [values, setValues] = useState<Record<string, string>>({});
|
|
@@ -189,6 +190,23 @@ export function ResourceForm({ resource, mode, recordId, initialData }: Props) {
|
|
|
189
190
|
setError(null);
|
|
190
191
|
setFieldErrors({});
|
|
191
192
|
try {
|
|
193
|
+
if (resource.name === "files" && mode === "create" && selectedUpload) {
|
|
194
|
+
const contentBase64 = await fileToBase64(selectedUpload);
|
|
195
|
+
const created = await api<Record<string, unknown>>(
|
|
196
|
+
"/admin/resources/files/upload",
|
|
197
|
+
{
|
|
198
|
+
method: "POST",
|
|
199
|
+
body: JSON.stringify({
|
|
200
|
+
filename: selectedUpload.name,
|
|
201
|
+
mimeType: selectedUpload.type || "application/octet-stream",
|
|
202
|
+
contentBase64
|
|
203
|
+
})
|
|
204
|
+
}
|
|
205
|
+
);
|
|
206
|
+
router.push(`/resources/${resource.name}/${String(created.id ?? "")}`);
|
|
207
|
+
router.refresh();
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
192
210
|
const payload = Object.fromEntries(
|
|
193
211
|
Object.entries(values).map(([name, value]) => [
|
|
194
212
|
name,
|
|
@@ -258,6 +276,22 @@ export function ResourceForm({ resource, mode, recordId, initialData }: Props) {
|
|
|
258
276
|
|
|
259
277
|
{/* Fields card */}
|
|
260
278
|
<div className="card stagger-2 p-6 space-y-5 dark:border-slate-700 dark:bg-slate-900">
|
|
279
|
+
{resource.name === "files" && mode === "create" && (
|
|
280
|
+
<label className="block space-y-1.5">
|
|
281
|
+
<span className="text-sm font-medium text-slate-700 dark:text-slate-300">Upload file</span>
|
|
282
|
+
<input
|
|
283
|
+
type="file"
|
|
284
|
+
onChange={(e) => setSelectedUpload(e.target.files?.[0] ?? null)}
|
|
285
|
+
className="input-base"
|
|
286
|
+
disabled={submitting}
|
|
287
|
+
/>
|
|
288
|
+
{selectedUpload && (
|
|
289
|
+
<p className="text-xs text-slate-500">
|
|
290
|
+
{selectedUpload.name} · {(selectedUpload.size / 1024).toFixed(1)} KB
|
|
291
|
+
</p>
|
|
292
|
+
)}
|
|
293
|
+
</label>
|
|
294
|
+
)}
|
|
261
295
|
{fields.map(([name, field]) => {
|
|
262
296
|
const errs = fieldErrors[name];
|
|
263
297
|
return (
|
|
@@ -347,3 +381,16 @@ export function ResourceForm({ resource, mode, recordId, initialData }: Props) {
|
|
|
347
381
|
</form>
|
|
348
382
|
);
|
|
349
383
|
}
|
|
384
|
+
|
|
385
|
+
function fileToBase64(file: File): Promise<string> {
|
|
386
|
+
return new Promise((resolve, reject) => {
|
|
387
|
+
const reader = new FileReader();
|
|
388
|
+
reader.onload = () => {
|
|
389
|
+
const value = typeof reader.result === "string" ? reader.result : "";
|
|
390
|
+
const base64 = value.includes(",") ? value.split(",")[1] ?? "" : value;
|
|
391
|
+
resolve(base64);
|
|
392
|
+
};
|
|
393
|
+
reader.onerror = () => reject(reader.error ?? new Error("Failed to read file"));
|
|
394
|
+
reader.readAsDataURL(file);
|
|
395
|
+
});
|
|
396
|
+
}
|
|
@@ -1,8 +1,22 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
import Link from "next/link";
|
|
4
|
-
import {
|
|
4
|
+
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
5
|
+
import {
|
|
6
|
+
ArrowDown,
|
|
7
|
+
ArrowUp,
|
|
8
|
+
ArrowUpDown,
|
|
9
|
+
ChevronLeft,
|
|
10
|
+
ChevronRight,
|
|
11
|
+
GripVertical,
|
|
12
|
+
Plus,
|
|
13
|
+
Search,
|
|
14
|
+
SlidersHorizontal,
|
|
15
|
+
Trash2
|
|
16
|
+
} from "lucide-react";
|
|
5
17
|
import type { ResourceMeta } from "../lib/api";
|
|
18
|
+
import { api } from "../lib/api";
|
|
19
|
+
import { loadColumnOrder, loadDensity, saveColumnOrder, saveDensity, type TableDensity } from "../lib/resource-table-prefs";
|
|
6
20
|
|
|
7
21
|
export type SortState = { field: string; dir: "asc" | "desc" } | null;
|
|
8
22
|
|
|
@@ -13,6 +27,8 @@ export type PaginationMeta = {
|
|
|
13
27
|
pages: number;
|
|
14
28
|
};
|
|
15
29
|
|
|
30
|
+
type FieldEntry = [string, ResourceMeta["fields"][string]];
|
|
31
|
+
|
|
16
32
|
type Props = {
|
|
17
33
|
resource: ResourceMeta;
|
|
18
34
|
rows: Record<string, unknown>[];
|
|
@@ -24,6 +40,7 @@ type Props = {
|
|
|
24
40
|
onPageChange?: (page: number) => void;
|
|
25
41
|
loading?: boolean;
|
|
26
42
|
error?: string | null;
|
|
43
|
+
onRefresh?: () => void | Promise<void>;
|
|
27
44
|
};
|
|
28
45
|
|
|
29
46
|
function format(value: unknown, type?: string) {
|
|
@@ -64,6 +81,11 @@ function SortIcon({ field, sort }: { field: string; sort?: SortState }) {
|
|
|
64
81
|
: <ArrowDown className="h-3 w-3 text-[#2454ff]" />;
|
|
65
82
|
}
|
|
66
83
|
|
|
84
|
+
function canInlineEdit(field: ResourceMeta["fields"][string], canUpdate: boolean): boolean {
|
|
85
|
+
if (!canUpdate || field.edit === false || field.sensitive) return false;
|
|
86
|
+
return ["text", "number", "boolean", "select", "badge"].includes(field.type);
|
|
87
|
+
}
|
|
88
|
+
|
|
67
89
|
export function ResourceTable({
|
|
68
90
|
resource,
|
|
69
91
|
rows,
|
|
@@ -74,11 +96,55 @@ export function ResourceTable({
|
|
|
74
96
|
pagination,
|
|
75
97
|
onPageChange,
|
|
76
98
|
loading = false,
|
|
77
|
-
error
|
|
99
|
+
error,
|
|
100
|
+
onRefresh
|
|
78
101
|
}: Props) {
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
.slice(0,
|
|
102
|
+
const canUpdate = Boolean(resource.permissions.update);
|
|
103
|
+
const listFieldEntries = useMemo(
|
|
104
|
+
() => Object.entries(resource.fields).filter(([, f]) => f.list !== false).slice(0, 12) as FieldEntry[],
|
|
105
|
+
[resource.fields]
|
|
106
|
+
);
|
|
107
|
+
const defaultColumnKeys = useMemo(() => listFieldEntries.map(([n]) => n), [listFieldEntries]);
|
|
108
|
+
|
|
109
|
+
const [columnOrder, setColumnOrder] = useState<string[]>(defaultColumnKeys);
|
|
110
|
+
const [density, setDensity] = useState<TableDensity>("comfortable");
|
|
111
|
+
const [selected, setSelected] = useState<Set<string>>(() => new Set());
|
|
112
|
+
const [bulkBusy, setBulkBusy] = useState(false);
|
|
113
|
+
const [inlineBusy, setInlineBusy] = useState<string | null>(null);
|
|
114
|
+
const [tableMessage, setTableMessage] = useState<string | null>(null);
|
|
115
|
+
const [editing, setEditing] = useState<{ rowId: string; field: string; value: string } | null>(null);
|
|
116
|
+
const [dragRowId, setDragRowId] = useState<string | null>(null);
|
|
117
|
+
const [dragCol, setDragCol] = useState<string | null>(null);
|
|
118
|
+
|
|
119
|
+
const listReorderEnabled =
|
|
120
|
+
resource.fields.order?.type === "number" && resource.fields.order.list !== false && canUpdate;
|
|
121
|
+
const bulkEnabled =
|
|
122
|
+
Boolean(resource.permissions.delete) || Boolean(resource.actions && Object.keys(resource.actions).length > 0);
|
|
123
|
+
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
setColumnOrder(loadColumnOrder(resource.name, defaultColumnKeys));
|
|
126
|
+
setDensity(loadDensity(resource.name));
|
|
127
|
+
}, [resource.name, defaultColumnKeys]);
|
|
128
|
+
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
setSelected(new Set());
|
|
131
|
+
}, [resource.name, rows, search, sort, pagination?.page]);
|
|
132
|
+
|
|
133
|
+
const fields = useMemo(() => {
|
|
134
|
+
const map = new Map(listFieldEntries);
|
|
135
|
+
const ordered: FieldEntry[] = [];
|
|
136
|
+
for (const key of columnOrder) {
|
|
137
|
+
const f = resource.fields[key];
|
|
138
|
+
if (f && f.list !== false) ordered.push([key, f]);
|
|
139
|
+
}
|
|
140
|
+
for (const [k, f] of listFieldEntries) {
|
|
141
|
+
if (!ordered.some(([n]) => n === k)) ordered.push([k, f]);
|
|
142
|
+
}
|
|
143
|
+
return ordered.slice(0, 12);
|
|
144
|
+
}, [columnOrder, listFieldEntries, resource.fields]);
|
|
145
|
+
|
|
146
|
+
const padClass = density === "compact" ? "px-3 py-2" : "px-4 py-3.5";
|
|
147
|
+
const textClass = density === "compact" ? "text-xs" : "text-sm";
|
|
82
148
|
|
|
83
149
|
function handleSort(name: string) {
|
|
84
150
|
if (!onSortChange) return;
|
|
@@ -91,9 +157,138 @@ export function ResourceTable({
|
|
|
91
157
|
}
|
|
92
158
|
}
|
|
93
159
|
|
|
160
|
+
function toggleSelect(id: string) {
|
|
161
|
+
setSelected((prev) => {
|
|
162
|
+
const next = new Set(prev);
|
|
163
|
+
if (next.has(id)) next.delete(id);
|
|
164
|
+
else next.add(id);
|
|
165
|
+
return next;
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function toggleSelectAllOnPage() {
|
|
170
|
+
const pageIds = rows.map((r) => String(r.id)).filter(Boolean);
|
|
171
|
+
const allOn = pageIds.length > 0 && pageIds.every((id) => selected.has(id));
|
|
172
|
+
setSelected(() => {
|
|
173
|
+
if (allOn) return new Set();
|
|
174
|
+
return new Set(pageIds);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const persistReorder = useCallback(
|
|
179
|
+
async (orderedIds: string[]) => {
|
|
180
|
+
const base =
|
|
181
|
+
pagination && pagination.limit ? Math.max(0, (pagination.page - 1) * pagination.limit) : 0;
|
|
182
|
+
try {
|
|
183
|
+
await api(`/admin/resources/${resource.name}/reorder`, {
|
|
184
|
+
method: "POST",
|
|
185
|
+
body: JSON.stringify({ ids: orderedIds, baseIndex: base })
|
|
186
|
+
});
|
|
187
|
+
setTableMessage(null);
|
|
188
|
+
await onRefresh?.();
|
|
189
|
+
} catch (e) {
|
|
190
|
+
setTableMessage(e instanceof Error ? e.message : "Reorder failed");
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
[resource.name, pagination, onRefresh]
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
const onRowDragStart = (id: string) => setDragRowId(id);
|
|
197
|
+
const onRowDragEnd = () => setDragRowId(null);
|
|
198
|
+
|
|
199
|
+
const onRowDrop = (targetId: string) => {
|
|
200
|
+
if (!listReorderEnabled || !dragRowId || dragRowId === targetId) return;
|
|
201
|
+
const ids = rows.map((r) => String(r.id));
|
|
202
|
+
const from = ids.indexOf(dragRowId);
|
|
203
|
+
const to = ids.indexOf(targetId);
|
|
204
|
+
if (from < 0 || to < 0) return;
|
|
205
|
+
const next = [...rows];
|
|
206
|
+
const removed = next.splice(from, 1);
|
|
207
|
+
const moved = removed[0];
|
|
208
|
+
if (moved === undefined) return;
|
|
209
|
+
next.splice(to, 0, moved);
|
|
210
|
+
void persistReorder(next.map((r) => String(r.id)));
|
|
211
|
+
setDragRowId(null);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const runBulkAction = async (action: string) => {
|
|
215
|
+
const ids = [...selected];
|
|
216
|
+
if (!ids.length) return;
|
|
217
|
+
setBulkBusy(true);
|
|
218
|
+
setTableMessage(null);
|
|
219
|
+
try {
|
|
220
|
+
const res = await api<{ ok: number; failures: { id: string; message: string }[] }>(`/admin/resources/${resource.name}/bulk`, {
|
|
221
|
+
method: "POST",
|
|
222
|
+
body: JSON.stringify({ action, ids })
|
|
223
|
+
});
|
|
224
|
+
if (res.failures?.length) {
|
|
225
|
+
setTableMessage(`${res.ok} ok, ${res.failures.length} failed: ${res.failures[0]?.message ?? ""}`);
|
|
226
|
+
} else {
|
|
227
|
+
setTableMessage(`Applied to ${res.ok} record(s).`);
|
|
228
|
+
}
|
|
229
|
+
setSelected(new Set());
|
|
230
|
+
await onRefresh?.();
|
|
231
|
+
} catch (e) {
|
|
232
|
+
setTableMessage(e instanceof Error ? e.message : "Bulk action failed");
|
|
233
|
+
} finally {
|
|
234
|
+
setBulkBusy(false);
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const saveInline = async (rowId: string, fieldName: string, raw: string, field: ResourceMeta["fields"][string]) => {
|
|
239
|
+
let value: unknown = raw;
|
|
240
|
+
if (field.type === "number") {
|
|
241
|
+
const n = Number(raw);
|
|
242
|
+
if (Number.isNaN(n)) {
|
|
243
|
+
setTableMessage("Invalid number");
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
value = n;
|
|
247
|
+
} else if (field.type === "boolean") value = raw === "true" || raw === "1";
|
|
248
|
+
setInlineBusy(`${rowId}:${fieldName}`);
|
|
249
|
+
setTableMessage(null);
|
|
250
|
+
try {
|
|
251
|
+
await api(`/admin/resources/${resource.name}/${rowId}`, {
|
|
252
|
+
method: "PATCH",
|
|
253
|
+
body: JSON.stringify({ [fieldName]: value })
|
|
254
|
+
});
|
|
255
|
+
setEditing(null);
|
|
256
|
+
await onRefresh?.();
|
|
257
|
+
} catch (e) {
|
|
258
|
+
setTableMessage(e instanceof Error ? e.message : "Update failed");
|
|
259
|
+
} finally {
|
|
260
|
+
setInlineBusy(null);
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const densityToggle = () => {
|
|
265
|
+
const next = density === "comfortable" ? "compact" : "comfortable";
|
|
266
|
+
setDensity(next);
|
|
267
|
+
saveDensity(resource.name, next);
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
const onColDragStart = (name: string) => setDragCol(name);
|
|
271
|
+
const onColDragEnd = () => setDragCol(null);
|
|
272
|
+
const onColDrop = (target: string) => {
|
|
273
|
+
if (!dragCol || dragCol === target) return;
|
|
274
|
+
const keys = fields.map(([n]) => n);
|
|
275
|
+
const from = keys.indexOf(dragCol);
|
|
276
|
+
const to = keys.indexOf(target);
|
|
277
|
+
if (from < 0 || to < 0) return;
|
|
278
|
+
const next = [...keys];
|
|
279
|
+
const removed = next.splice(from, 1);
|
|
280
|
+
const c = removed[0];
|
|
281
|
+
if (c === undefined) return;
|
|
282
|
+
next.splice(to, 0, c);
|
|
283
|
+
setColumnOrder(next);
|
|
284
|
+
saveColumnOrder(resource.name, next);
|
|
285
|
+
setDragCol(null);
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
const extraCols = (bulkEnabled ? 1 : 0) + (listReorderEnabled ? 1 : 0) + 1;
|
|
289
|
+
|
|
94
290
|
return (
|
|
95
291
|
<div className="space-y-5">
|
|
96
|
-
{/* Page header */}
|
|
97
292
|
<div className="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between animate-in stagger-1">
|
|
98
293
|
<div>
|
|
99
294
|
<p className="text-[11px] font-bold uppercase tracking-widest text-[#0ea5a4]">Resource</p>
|
|
@@ -115,9 +310,8 @@ export function ResourceTable({
|
|
|
115
310
|
)}
|
|
116
311
|
</div>
|
|
117
312
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
<div className="flex flex-1 items-center gap-2.5 rounded-xl border border-slate-200 bg-white px-3.5 py-2.5 shadow-[0_1px_3px_rgba(0,0,0,0.04)] transition-all focus-within:border-[#2454ff] focus-within:shadow-[0_0_0_3px_rgba(36,84,255,0.1)] dark:border-slate-700 dark:bg-slate-900">
|
|
313
|
+
<div className="flex flex-wrap items-center gap-2 animate-in stagger-2">
|
|
314
|
+
<div className="flex min-w-[200px] flex-1 items-center gap-2.5 rounded-xl border border-slate-200 bg-white px-3.5 py-2.5 shadow-[0_1px_3px_rgba(0,0,0,0.04)] transition-all focus-within:border-[#2454ff] focus-within:shadow-[0_0_0_3px_rgba(36,84,255,0.1)] dark:border-slate-700 dark:bg-slate-900">
|
|
121
315
|
<Search className="h-4 w-4 shrink-0 text-slate-400" />
|
|
122
316
|
<input
|
|
123
317
|
value={search}
|
|
@@ -131,12 +325,60 @@ export function ResourceTable({
|
|
|
131
325
|
</button>
|
|
132
326
|
)}
|
|
133
327
|
</div>
|
|
134
|
-
<button
|
|
328
|
+
<button
|
|
329
|
+
type="button"
|
|
330
|
+
onClick={densityToggle}
|
|
331
|
+
className="btn-ghost flex items-center gap-2 dark:border-slate-700 dark:bg-slate-900 dark:text-slate-300"
|
|
332
|
+
title={density === "comfortable" ? "Switch to compact density" : "Switch to comfortable density"}
|
|
333
|
+
>
|
|
135
334
|
<SlidersHorizontal className="h-4 w-4" />
|
|
136
|
-
<span className="hidden sm:inline">
|
|
335
|
+
<span className="hidden sm:inline">{density === "comfortable" ? "Comfortable" : "Compact"}</span>
|
|
137
336
|
</button>
|
|
138
337
|
</div>
|
|
139
338
|
|
|
339
|
+
{selected.size > 0 && bulkEnabled && (
|
|
340
|
+
<div className="flex flex-wrap items-center gap-2 rounded-xl border border-[#2454ff]/30 bg-[#eef3ff] px-4 py-3 text-sm dark:border-blue-500/30 dark:bg-slate-800/80">
|
|
341
|
+
<span className="font-semibold text-slate-800 dark:text-slate-200">{selected.size} selected</span>
|
|
342
|
+
{resource.permissions.delete && (
|
|
343
|
+
<button
|
|
344
|
+
type="button"
|
|
345
|
+
disabled={bulkBusy}
|
|
346
|
+
className="inline-flex items-center gap-1 rounded-lg border border-red-200 bg-white px-3 py-1.5 text-xs font-semibold text-red-600 hover:bg-red-50 disabled:opacity-50 dark:border-red-900 dark:bg-slate-900 dark:text-red-400"
|
|
347
|
+
onClick={() => {
|
|
348
|
+
if (typeof window !== "undefined" && !window.confirm(`Delete ${selected.size} record(s)?`)) return;
|
|
349
|
+
void runBulkAction("delete");
|
|
350
|
+
}}
|
|
351
|
+
>
|
|
352
|
+
<Trash2 className="h-3.5 w-3.5" />
|
|
353
|
+
Delete
|
|
354
|
+
</button>
|
|
355
|
+
)}
|
|
356
|
+
{resource.actions &&
|
|
357
|
+
Object.entries(resource.actions).map(([key, action]) => (
|
|
358
|
+
<button
|
|
359
|
+
key={key}
|
|
360
|
+
type="button"
|
|
361
|
+
disabled={bulkBusy}
|
|
362
|
+
className="rounded-lg border border-slate-200 bg-white px-3 py-1.5 text-xs font-semibold text-slate-700 hover:bg-slate-50 disabled:opacity-50 dark:border-slate-600 dark:bg-slate-900 dark:text-slate-200"
|
|
363
|
+
onClick={() => {
|
|
364
|
+
if (action.confirm && typeof window !== "undefined" && !window.confirm(`Run “${action.label}” on ${selected.size} row(s)?`)) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
void runBulkAction(key);
|
|
368
|
+
}}
|
|
369
|
+
>
|
|
370
|
+
{action.label}
|
|
371
|
+
</button>
|
|
372
|
+
))}
|
|
373
|
+
</div>
|
|
374
|
+
)}
|
|
375
|
+
|
|
376
|
+
{tableMessage && (
|
|
377
|
+
<div className="rounded-xl border border-slate-200 bg-slate-50 px-4 py-2 text-sm text-slate-700 dark:border-slate-600 dark:bg-slate-800 dark:text-slate-300">
|
|
378
|
+
{tableMessage}
|
|
379
|
+
</div>
|
|
380
|
+
)}
|
|
381
|
+
|
|
140
382
|
{error && (
|
|
141
383
|
<div className="flex items-center gap-2 rounded-xl bg-red-50 px-4 py-3 text-sm text-red-600 fade-in">
|
|
142
384
|
<div className="h-1.5 w-1.5 shrink-0 rounded-full bg-red-500" />
|
|
@@ -144,17 +386,38 @@ export function ResourceTable({
|
|
|
144
386
|
</div>
|
|
145
387
|
)}
|
|
146
388
|
|
|
147
|
-
{/* Table */}
|
|
148
389
|
<div className="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-[0_1px_3px_rgba(0,0,0,0.04),0_8px_24px_rgba(0,0,0,0.04)] animate-in stagger-3 dark:border-slate-700 dark:bg-slate-900">
|
|
149
390
|
<div className="overflow-x-auto">
|
|
150
|
-
<table className=
|
|
391
|
+
<table className={`w-full text-left ${textClass}`}>
|
|
151
392
|
<thead className="border-b border-slate-100 bg-slate-50/70 dark:border-slate-700 dark:bg-slate-800/50">
|
|
152
393
|
<tr>
|
|
394
|
+
{listReorderEnabled && (
|
|
395
|
+
<th className={`${padClass} w-8`} aria-hidden />
|
|
396
|
+
)}
|
|
397
|
+
{bulkEnabled && (
|
|
398
|
+
<th className={`${padClass} w-10`}>
|
|
399
|
+
<input
|
|
400
|
+
type="checkbox"
|
|
401
|
+
className="rounded border-slate-300"
|
|
402
|
+
checked={rows.length > 0 && rows.every((r) => selected.has(String(r.id)))}
|
|
403
|
+
onChange={toggleSelectAllOnPage}
|
|
404
|
+
aria-label="Select all on this page"
|
|
405
|
+
/>
|
|
406
|
+
</th>
|
|
407
|
+
)}
|
|
153
408
|
{fields.map(([name, field]) => {
|
|
154
409
|
const sortable = Boolean(field.sortable) && Boolean(onSortChange);
|
|
155
410
|
return (
|
|
156
|
-
<th
|
|
157
|
-
|
|
411
|
+
<th
|
|
412
|
+
key={name}
|
|
413
|
+
draggable
|
|
414
|
+
onDragStart={() => onColDragStart(name)}
|
|
415
|
+
onDragEnd={() => onColDragEnd()}
|
|
416
|
+
onDragOver={(e) => e.preventDefault()}
|
|
417
|
+
onDrop={() => onColDrop(name)}
|
|
418
|
+
className={`${padClass} ${sortable ? "cursor-pointer select-none" : "cursor-grab active:cursor-grabbing"}`}
|
|
419
|
+
onClick={() => sortable && handleSort(name)}
|
|
420
|
+
>
|
|
158
421
|
<div className="group flex items-center gap-1 text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400">
|
|
159
422
|
{field.label ?? name}
|
|
160
423
|
{sortable && <SortIcon field={name} sort={sort} />}
|
|
@@ -162,43 +425,155 @@ export function ResourceTable({
|
|
|
162
425
|
</th>
|
|
163
426
|
);
|
|
164
427
|
})}
|
|
165
|
-
<th className=
|
|
428
|
+
<th className={`${padClass} text-right text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400`}>
|
|
166
429
|
Actions
|
|
167
430
|
</th>
|
|
168
431
|
</tr>
|
|
169
432
|
</thead>
|
|
170
433
|
<tbody className="divide-y divide-slate-100 dark:divide-slate-800">
|
|
171
434
|
{loading ? (
|
|
172
|
-
Array.from({ length: 6 }).map((_, i) =>
|
|
435
|
+
Array.from({ length: 6 }).map((_, i) => (
|
|
436
|
+
<SkeletonRow
|
|
437
|
+
key={i}
|
|
438
|
+
cols={fields.length + (bulkEnabled ? 1 : 0) + (listReorderEnabled ? 1 : 0)}
|
|
439
|
+
/>
|
|
440
|
+
))
|
|
173
441
|
) : rows.length > 0 ? (
|
|
174
|
-
rows.map((row, index) =>
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
442
|
+
rows.map((row, index) => {
|
|
443
|
+
const rowId = String(row.id ?? index);
|
|
444
|
+
return (
|
|
445
|
+
<tr
|
|
446
|
+
key={rowId}
|
|
447
|
+
className={`group transition-colors hover:bg-blue-50/30 dark:hover:bg-slate-800/50 ${dragRowId === rowId ? "opacity-70" : ""}`}
|
|
448
|
+
onDragOver={(e) => listReorderEnabled && e.preventDefault()}
|
|
449
|
+
onDrop={() => onRowDrop(rowId)}
|
|
450
|
+
>
|
|
451
|
+
{listReorderEnabled && (
|
|
452
|
+
<td
|
|
453
|
+
className={`${padClass} w-8 cursor-grab text-slate-400`}
|
|
454
|
+
draggable
|
|
455
|
+
onDragStart={() => onRowDragStart(rowId)}
|
|
456
|
+
onDragEnd={() => onRowDragEnd()}
|
|
457
|
+
title="Drag to reorder"
|
|
458
|
+
>
|
|
459
|
+
<GripVertical className="h-4 w-4" />
|
|
460
|
+
</td>
|
|
461
|
+
)}
|
|
462
|
+
{bulkEnabled && (
|
|
463
|
+
<td className={padClass}>
|
|
464
|
+
<input
|
|
465
|
+
type="checkbox"
|
|
466
|
+
className="rounded border-slate-300"
|
|
467
|
+
checked={selected.has(rowId)}
|
|
468
|
+
onChange={() => toggleSelect(rowId)}
|
|
469
|
+
aria-label={`Select row ${rowId}`}
|
|
470
|
+
/>
|
|
471
|
+
</td>
|
|
472
|
+
)}
|
|
473
|
+
{fields.map(([name, field]) => {
|
|
474
|
+
const showEdit =
|
|
475
|
+
editing?.rowId === rowId && editing.field === name && canInlineEdit(field, canUpdate);
|
|
476
|
+
const busy = inlineBusy === `${rowId}:${name}`;
|
|
477
|
+
const cellEdit = () => {
|
|
478
|
+
if (!canInlineEdit(field, canUpdate) || busy) return;
|
|
479
|
+
setEditing({
|
|
480
|
+
rowId,
|
|
481
|
+
field: name,
|
|
482
|
+
value: String(row[name] ?? "")
|
|
483
|
+
});
|
|
484
|
+
};
|
|
485
|
+
return (
|
|
486
|
+
<td
|
|
487
|
+
key={name}
|
|
488
|
+
className={`${padClass} text-slate-700 dark:text-slate-300 ${canInlineEdit(field, canUpdate) ? "cursor-cell" : ""}`}
|
|
489
|
+
onDoubleClick={cellEdit}
|
|
490
|
+
>
|
|
491
|
+
{showEdit ? (
|
|
492
|
+
<div className="flex items-center gap-1">
|
|
493
|
+
{field.type === "boolean" ? (
|
|
494
|
+
<select
|
|
495
|
+
autoFocus
|
|
496
|
+
className="input-base max-w-[10rem] py-1 text-xs"
|
|
497
|
+
value={editing.value === "true" || editing.value === "1" ? "true" : "false"}
|
|
498
|
+
onChange={(e) =>
|
|
499
|
+
setEditing((prev) => (prev ? { ...prev, value: e.target.value } : prev))
|
|
500
|
+
}
|
|
501
|
+
onBlur={() =>
|
|
502
|
+
void saveInline(rowId, name, editing.value === "true" ? "true" : "false", field)
|
|
503
|
+
}
|
|
504
|
+
onKeyDown={(e) => {
|
|
505
|
+
if (e.key === "Enter") {
|
|
506
|
+
void saveInline(rowId, name, editing.value === "true" ? "true" : "false", field);
|
|
507
|
+
}
|
|
508
|
+
if (e.key === "Escape") setEditing(null);
|
|
509
|
+
}}
|
|
510
|
+
>
|
|
511
|
+
<option value="true">Yes</option>
|
|
512
|
+
<option value="false">No</option>
|
|
513
|
+
</select>
|
|
514
|
+
) : field.type === "select" || (field.type === "badge" && (field.options?.length ?? 0) > 0) ? (
|
|
515
|
+
<select
|
|
516
|
+
autoFocus
|
|
517
|
+
className="input-base max-w-[12rem] py-1 text-xs"
|
|
518
|
+
value={editing.value}
|
|
519
|
+
onChange={(e) =>
|
|
520
|
+
setEditing((prev) => (prev ? { ...prev, value: e.target.value } : prev))
|
|
521
|
+
}
|
|
522
|
+
onBlur={() => void saveInline(rowId, name, editing.value, field)}
|
|
523
|
+
onKeyDown={(e) => {
|
|
524
|
+
if (e.key === "Enter") void saveInline(rowId, name, editing.value, field);
|
|
525
|
+
if (e.key === "Escape") setEditing(null);
|
|
526
|
+
}}
|
|
527
|
+
>
|
|
528
|
+
{(field.options ?? []).map((opt) => (
|
|
529
|
+
<option key={opt} value={opt}>
|
|
530
|
+
{opt}
|
|
531
|
+
</option>
|
|
532
|
+
))}
|
|
533
|
+
</select>
|
|
534
|
+
) : (
|
|
535
|
+
<input
|
|
536
|
+
autoFocus
|
|
537
|
+
className="input-base max-w-[14rem] py-1 text-xs"
|
|
538
|
+
type={field.type === "number" ? "number" : "text"}
|
|
539
|
+
value={editing.value}
|
|
540
|
+
onChange={(e) =>
|
|
541
|
+
setEditing((prev) => (prev ? { ...prev, value: e.target.value } : prev))
|
|
542
|
+
}
|
|
543
|
+
onBlur={() => void saveInline(rowId, name, editing.value, field)}
|
|
544
|
+
onKeyDown={(e) => {
|
|
545
|
+
if (e.key === "Enter") void saveInline(rowId, name, editing.value, field);
|
|
546
|
+
if (e.key === "Escape") setEditing(null);
|
|
547
|
+
}}
|
|
548
|
+
/>
|
|
549
|
+
)}
|
|
550
|
+
</div>
|
|
551
|
+
) : field.type === "badge" || name === "status" ? (
|
|
552
|
+
<span className={statusBadgeClass(format(row[name], field.type))}>
|
|
553
|
+
{format(row[name], field.type)}
|
|
554
|
+
</span>
|
|
555
|
+
) : (
|
|
556
|
+
<span className="line-clamp-1 block max-w-[220px]">
|
|
557
|
+
{busy ? "…" : format(row[name], field.type)}
|
|
558
|
+
</span>
|
|
559
|
+
)}
|
|
560
|
+
</td>
|
|
561
|
+
);
|
|
562
|
+
})}
|
|
563
|
+
<td className={`${padClass} text-right`}>
|
|
564
|
+
<Link
|
|
565
|
+
href={`/resources/${resource.name}/${rowId}`}
|
|
566
|
+
className="inline-flex items-center gap-1 rounded-lg bg-slate-100 px-2.5 py-1 text-xs font-semibold text-slate-600 transition-all hover:bg-[#eef3ff] hover:text-[#2454ff] dark:bg-slate-800 dark:text-slate-400 dark:hover:bg-slate-700"
|
|
567
|
+
>
|
|
568
|
+
Open
|
|
569
|
+
</Link>
|
|
187
570
|
</td>
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
href={`/resources/${resource.name}/${String(row.id ?? "new")}`}
|
|
192
|
-
className="inline-flex items-center gap-1 rounded-lg bg-slate-100 px-2.5 py-1 text-xs font-semibold text-slate-600 transition-all hover:bg-[#eef3ff] hover:text-[#2454ff] dark:bg-slate-800 dark:text-slate-400 dark:hover:bg-slate-700"
|
|
193
|
-
>
|
|
194
|
-
Open
|
|
195
|
-
</Link>
|
|
196
|
-
</td>
|
|
197
|
-
</tr>
|
|
198
|
-
))
|
|
571
|
+
</tr>
|
|
572
|
+
);
|
|
573
|
+
})
|
|
199
574
|
) : (
|
|
200
575
|
<tr>
|
|
201
|
-
<td className="py-16 text-center" colSpan={fields.length +
|
|
576
|
+
<td className="py-16 text-center" colSpan={fields.length + extraCols}>
|
|
202
577
|
<div className="flex flex-col items-center gap-3 text-slate-400">
|
|
203
578
|
<div className="flex h-12 w-12 items-center justify-center rounded-2xl bg-slate-100 dark:bg-slate-800">
|
|
204
579
|
<Search className="h-5 w-5" />
|
|
@@ -217,7 +592,6 @@ export function ResourceTable({
|
|
|
217
592
|
</table>
|
|
218
593
|
</div>
|
|
219
594
|
|
|
220
|
-
{/* Pagination footer */}
|
|
221
595
|
{!loading && pagination && pagination.pages > 1 && (
|
|
222
596
|
<div className="flex items-center justify-between border-t border-slate-100 px-4 py-3 dark:border-slate-800">
|
|
223
597
|
<p className="text-xs text-slate-400">
|
|
@@ -263,7 +637,6 @@ export function ResourceTable({
|
|
|
263
637
|
</div>
|
|
264
638
|
)}
|
|
265
639
|
|
|
266
|
-
{/* Simple row count when no pagination */}
|
|
267
640
|
{!loading && rows.length > 0 && (!pagination || pagination.pages <= 1) && (
|
|
268
641
|
<div className="border-t border-slate-100 px-4 py-3 dark:border-slate-800">
|
|
269
642
|
<p className="text-xs text-slate-400">
|