@eutobias/serenojs 0.0.0-stage → 0.1.1
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/README.md +682 -2
- package/admin-dist/assets/index-B3EgqtT6.js +9 -0
- package/admin-dist/assets/index-D2m0Eb4k.css +2 -0
- package/admin-dist/index.html +17 -0
- package/dist/auth/auth.server.d.ts +31 -0
- package/dist/auth/auth.server.js +141 -0
- package/dist/auth/config.d.ts +32 -0
- package/dist/auth/config.js +25 -0
- package/dist/auth/cookies.server.d.ts +7 -0
- package/dist/auth/cookies.server.js +65 -0
- package/dist/auth/http.server.d.ts +8 -0
- package/dist/auth/http.server.js +30 -0
- package/dist/auth/password.server.d.ts +2 -0
- package/dist/auth/password.server.js +21 -0
- package/dist/auth/session.server.d.ts +2 -0
- package/dist/auth/session.server.js +14 -0
- package/dist/auth/users.server.d.ts +16 -0
- package/dist/auth/users.server.js +123 -0
- package/dist/config.d.ts +91 -0
- package/dist/config.js +39 -0
- package/dist/core/backup.server.d.ts +52 -0
- package/dist/core/backup.server.js +349 -0
- package/dist/core/db.server.d.ts +24 -0
- package/dist/core/db.server.js +526 -0
- package/dist/core/media.d.ts +22 -0
- package/dist/core/media.js +76 -0
- package/dist/core/media.server.d.ts +18 -0
- package/dist/core/media.server.js +102 -0
- package/dist/core/schema.d.ts +26 -0
- package/dist/core/schema.js +99 -0
- package/dist/core/types.d.ts +143 -0
- package/dist/core/types.js +1 -0
- package/dist/core/validation.d.ts +16 -0
- package/dist/core/validation.js +192 -0
- package/dist/locale.d.ts +2 -0
- package/dist/locale.js +14 -0
- package/dist/runtime.d.ts +8 -0
- package/dist/runtime.js +37 -0
- package/dist/server/app.d.ts +20 -0
- package/dist/server/app.js +661 -0
- package/dist/ui/admin/admin-shell.d.ts +16 -0
- package/dist/ui/admin/admin-shell.js +38 -0
- package/dist/ui/admin/backup-manager.d.ts +5 -0
- package/dist/ui/admin/backup-manager.js +165 -0
- package/dist/ui/admin/collection-icon.d.ts +22 -0
- package/dist/ui/admin/collection-icon.js +34 -0
- package/dist/ui/admin/collection-manager.d.ts +7 -0
- package/dist/ui/admin/collection-manager.js +212 -0
- package/dist/ui/admin/display.d.ts +7 -0
- package/dist/ui/admin/display.js +65 -0
- package/dist/ui/admin/field-control.d.ts +14 -0
- package/dist/ui/admin/field-control.js +58 -0
- package/dist/ui/admin/i18n.d.ts +27 -0
- package/dist/ui/admin/i18n.js +324 -0
- package/dist/ui/admin/item-form.d.ts +20 -0
- package/dist/ui/admin/item-form.js +60 -0
- package/dist/ui/admin/list-control.d.ts +13 -0
- package/dist/ui/admin/list-control.js +31 -0
- package/dist/ui/admin/locales.d.ts +4 -0
- package/dist/ui/admin/locales.js +8 -0
- package/dist/ui/admin/login-form.d.ts +3 -0
- package/dist/ui/admin/login-form.js +52 -0
- package/dist/ui/admin/media-control.d.ts +14 -0
- package/dist/ui/admin/media-control.js +74 -0
- package/dist/ui/admin/media-list-control.d.ts +14 -0
- package/dist/ui/admin/media-list-control.js +91 -0
- package/dist/ui/admin/pagination.d.ts +11 -0
- package/dist/ui/admin/pagination.js +14 -0
- package/dist/ui/admin/relation-control.d.ts +14 -0
- package/dist/ui/admin/relation-control.js +33 -0
- package/dist/ui/admin/router.d.ts +17 -0
- package/dist/ui/admin/router.js +58 -0
- package/dist/ui/admin/runtime.d.ts +28 -0
- package/dist/ui/admin/runtime.js +29 -0
- package/dist/ui/admin/sidebar.d.ts +13 -0
- package/dist/ui/admin/sidebar.js +29 -0
- package/dist/ui/admin/standalone-manager.d.ts +11 -0
- package/dist/ui/admin/standalone-manager.js +51 -0
- package/dist/ui/admin/table.d.ts +21 -0
- package/dist/ui/admin/table.js +23 -0
- package/dist/ui/admin/toast.d.ts +16 -0
- package/dist/ui/admin/toast.js +59 -0
- package/dist/ui/admin/users-manager.d.ts +6 -0
- package/dist/ui/admin/users-manager.js +157 -0
- package/dist/ui/badge.d.ts +9 -0
- package/dist/ui/badge.js +14 -0
- package/dist/ui/box.d.ts +17 -0
- package/dist/ui/box.js +14 -0
- package/dist/ui/button.d.ts +25 -0
- package/dist/ui/button.js +28 -0
- package/dist/ui/card.d.ts +45 -0
- package/dist/ui/card.js +49 -0
- package/dist/ui/container.d.ts +13 -0
- package/dist/ui/container.js +13 -0
- package/dist/ui/drawer.d.ts +15 -0
- package/dist/ui/drawer.js +31 -0
- package/dist/ui/field-wrapper.d.ts +15 -0
- package/dist/ui/field-wrapper.js +11 -0
- package/dist/ui/index.d.ts +8 -0
- package/dist/ui/index.js +8 -0
- package/dist/ui/text.d.ts +19 -0
- package/dist/ui/text.js +40 -0
- package/package.json +81 -3
- package/src/admin/app.tsx +371 -0
- package/src/admin/index.html +16 -0
- package/src/admin/main.tsx +42 -0
- package/src/admin/styles.css +9 -0
- package/src/auth/auth.server.ts +203 -0
- package/src/auth/config.ts +46 -0
- package/src/auth/cookies.server.ts +84 -0
- package/src/auth/http.server.ts +32 -0
- package/src/auth/password.server.ts +25 -0
- package/src/auth/session.server.ts +16 -0
- package/src/auth/users.server.ts +159 -0
- package/src/config.ts +120 -0
- package/src/core/backup.server.ts +446 -0
- package/src/core/db.server.ts +685 -0
- package/src/core/media.server.ts +136 -0
- package/src/core/media.ts +91 -0
- package/src/core/schema.ts +126 -0
- package/src/core/types.ts +194 -0
- package/src/core/validation.ts +215 -0
- package/src/global.d.ts +1 -0
- package/src/locale.ts +16 -0
- package/src/runtime.ts +54 -0
- package/src/server/app.ts +737 -0
- package/src/ui/admin/admin-shell.tsx +106 -0
- package/src/ui/admin/backup-manager.tsx +327 -0
- package/src/ui/admin/collection-icon.tsx +69 -0
- package/src/ui/admin/collection-manager.tsx +408 -0
- package/src/ui/admin/display.tsx +138 -0
- package/src/ui/admin/field-control.tsx +214 -0
- package/src/ui/admin/i18n.tsx +401 -0
- package/src/ui/admin/item-form.tsx +200 -0
- package/src/ui/admin/list-control.tsx +121 -0
- package/src/ui/admin/locales.ts +12 -0
- package/src/ui/admin/login-form.tsx +98 -0
- package/src/ui/admin/media-control.tsx +161 -0
- package/src/ui/admin/media-list-control.tsx +242 -0
- package/src/ui/admin/pagination.tsx +58 -0
- package/src/ui/admin/relation-control.tsx +142 -0
- package/src/ui/admin/router.tsx +105 -0
- package/src/ui/admin/runtime.tsx +58 -0
- package/src/ui/admin/sidebar.tsx +178 -0
- package/src/ui/admin/standalone-manager.tsx +103 -0
- package/src/ui/admin/table.tsx +129 -0
- package/src/ui/admin/toast.tsx +126 -0
- package/src/ui/admin/users-manager.tsx +344 -0
- package/src/ui/badge.tsx +27 -0
- package/src/ui/box.tsx +33 -0
- package/src/ui/button.tsx +77 -0
- package/src/ui/card.tsx +92 -0
- package/src/ui/container.tsx +28 -0
- package/src/ui/drawer.tsx +104 -0
- package/src/ui/field-wrapper.tsx +54 -0
- package/src/ui/index.ts +8 -0
- package/src/ui/text.tsx +93 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useState } from "react";
|
|
4
|
+
import { useRouter } from "./router.js";
|
|
5
|
+
import { Box } from "../box.js";
|
|
6
|
+
import { Button } from "../button.js";
|
|
7
|
+
import { Text } from "../text.js";
|
|
8
|
+
import { useCmsUrls } from "./runtime.js";
|
|
9
|
+
import { Sidebar } from "./sidebar.js";
|
|
10
|
+
import { ToastProvider } from "./toast.js";
|
|
11
|
+
import { useI18n } from "./i18n.js";
|
|
12
|
+
/**
|
|
13
|
+
* Responsive admin chrome: static sidebar on desktop, off-canvas drawer on
|
|
14
|
+
* mobile, plus the toast provider for the whole admin area.
|
|
15
|
+
*/
|
|
16
|
+
export const AdminShell = ({ title, collections, user, children, onLoggedOut }) => {
|
|
17
|
+
const [menuOpen, setMenuOpen] = useState(false);
|
|
18
|
+
const { path, navigate } = useRouter();
|
|
19
|
+
const { auth } = useCmsUrls();
|
|
20
|
+
const { t } = useI18n();
|
|
21
|
+
// Close the mobile menu on every navigation.
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
setMenuOpen(false);
|
|
24
|
+
}, [path]);
|
|
25
|
+
const handleLogout = async () => {
|
|
26
|
+
try {
|
|
27
|
+
await fetch(auth("logout"), {
|
|
28
|
+
method: "POST",
|
|
29
|
+
headers: { "Content-Type": "application/json" },
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
onLoggedOut?.();
|
|
34
|
+
navigate("/login");
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
return (_jsx(ToastProvider, { children: _jsxs(Box, { className: "min-h-screen w-full flex-col bg-gray-50 lg:flex-row", children: [_jsxs(Box, { className: "sticky top-0 z-30 items-center justify-between gap-3 border-b border-gray-200 bg-white px-4 py-3 lg:hidden", children: [_jsx(Button, { type: "button", variant: "ghost", size: "sm", "aria-label": t("sidebar.openMenu"), "aria-expanded": menuOpen, onClick: () => setMenuOpen(true), children: _jsx("span", { "aria-hidden": true, className: "text-base leading-none", children: "\u2630" }) }), _jsx(Text, { size: "caption", className: "font-semibold text-gray-900", children: title }), _jsx("span", { "aria-hidden": true, className: "w-9" })] }), menuOpen ? (_jsx(Box, { onClick: () => setMenuOpen(false), "aria-hidden": true, className: "fixed inset-0 z-40 bg-gray-900/40 lg:hidden" })) : null, _jsx(Box, { className: `fixed inset-y-0 left-0 z-50 flex transition-transform duration-200 ease-out lg:static lg:z-auto lg:translate-x-0 ${menuOpen ? "translate-x-0" : "-translate-x-full"}`, children: _jsx(Sidebar, { title: title, collections: collections, user: user, onLogout: () => void handleLogout(), onNavigate: () => setMenuOpen(false) }) }), _jsx(Box, { as: "main", className: "min-w-0 flex-1 flex-col", children: children })] }) }));
|
|
38
|
+
};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useCallback, useRef, useState } from "react";
|
|
4
|
+
import { Badge } from "../badge.js";
|
|
5
|
+
import { Box } from "../box.js";
|
|
6
|
+
import { Button, IconButton } from "../button.js";
|
|
7
|
+
import { Drawer } from "../drawer.js";
|
|
8
|
+
import { Text } from "../text.js";
|
|
9
|
+
import { FiDatabase, FiDownload, FiPlus, FiRefreshCw, FiTrash2, FiUpload, } from "react-icons/fi";
|
|
10
|
+
import { DataTable } from "./table.js";
|
|
11
|
+
import { useToast } from "./toast.js";
|
|
12
|
+
import { useI18n } from "./i18n.js";
|
|
13
|
+
import { useCmsUrls } from "./runtime.js";
|
|
14
|
+
/** Deterministic (UTC) date format to avoid hydration mismatches. */
|
|
15
|
+
function formatDateTime(value, locale) {
|
|
16
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2})/.exec(value);
|
|
17
|
+
if (!match)
|
|
18
|
+
return value || "—";
|
|
19
|
+
const date = locale === "en" ? `${match[2]}/${match[3]}/${match[1]}` : `${match[3]}/${match[2]}/${match[1]}`;
|
|
20
|
+
return `${date} ${match[4]}:${match[5]}`;
|
|
21
|
+
}
|
|
22
|
+
function formatSize(bytes) {
|
|
23
|
+
if (bytes < 1024)
|
|
24
|
+
return `${bytes} B`;
|
|
25
|
+
if (bytes < 1024 * 1024)
|
|
26
|
+
return `${(bytes / 1024).toFixed(1)} KB`;
|
|
27
|
+
return `${(bytes / 1024 / 1024).toFixed(2)} MB`;
|
|
28
|
+
}
|
|
29
|
+
export const BackupManager = ({ initial }) => {
|
|
30
|
+
const toast = useToast();
|
|
31
|
+
const { backup: backupUrl } = useCmsUrls();
|
|
32
|
+
const { t, locale } = useI18n();
|
|
33
|
+
const [backups, setBackups] = useState(initial);
|
|
34
|
+
const [drawer, setDrawer] = useState({ kind: "closed" });
|
|
35
|
+
const [busy, setBusy] = useState(false);
|
|
36
|
+
const [loading, setLoading] = useState(false);
|
|
37
|
+
const fileRef = useRef(null);
|
|
38
|
+
const reload = useCallback(async () => {
|
|
39
|
+
setLoading(true);
|
|
40
|
+
try {
|
|
41
|
+
const response = await fetch(backupUrl());
|
|
42
|
+
if (!response.ok)
|
|
43
|
+
return;
|
|
44
|
+
const json = (await response.json());
|
|
45
|
+
if (json.backups)
|
|
46
|
+
setBackups(json.backups);
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
setLoading(false);
|
|
50
|
+
}
|
|
51
|
+
}, []);
|
|
52
|
+
const closeDrawer = () => setDrawer({ kind: "closed" });
|
|
53
|
+
const createBackup = async () => {
|
|
54
|
+
setBusy(true);
|
|
55
|
+
try {
|
|
56
|
+
const response = await fetch(backupUrl(), { method: "POST" });
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
toast.error(t("backup.createError"));
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
toast.success(t("backup.created"));
|
|
62
|
+
await reload();
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
toast.error(t("backup.networkCreate"));
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
setBusy(false);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const downloadCurrent = () => {
|
|
72
|
+
window.location.assign(backupUrl("latest=1"));
|
|
73
|
+
};
|
|
74
|
+
const pickFile = () => fileRef.current?.click();
|
|
75
|
+
const onFileChange = (event) => {
|
|
76
|
+
const file = event.target.files?.[0];
|
|
77
|
+
event.target.value = "";
|
|
78
|
+
if (!file)
|
|
79
|
+
return;
|
|
80
|
+
setDrawer({ kind: "restore", file });
|
|
81
|
+
};
|
|
82
|
+
const confirmRestore = async () => {
|
|
83
|
+
if (drawer.kind !== "restore")
|
|
84
|
+
return;
|
|
85
|
+
setBusy(true);
|
|
86
|
+
try {
|
|
87
|
+
const response = await fetch(backupUrl(), {
|
|
88
|
+
method: "PUT",
|
|
89
|
+
headers: { "Content-Type": "application/octet-stream" },
|
|
90
|
+
body: drawer.file,
|
|
91
|
+
});
|
|
92
|
+
const json = (await response.json().catch(() => null));
|
|
93
|
+
if (!response.ok) {
|
|
94
|
+
toast.error(json?.message ?? t("backup.restoreError"));
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
closeDrawer();
|
|
98
|
+
toast.success(t("backup.restored"));
|
|
99
|
+
// The database (and possibly the session) changed underneath us.
|
|
100
|
+
setTimeout(() => window.location.reload(), 1_200);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
toast.error(t("backup.networkRestore"));
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
setBusy(false);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
const confirmDelete = async () => {
|
|
110
|
+
if (drawer.kind !== "delete")
|
|
111
|
+
return;
|
|
112
|
+
setBusy(true);
|
|
113
|
+
try {
|
|
114
|
+
const response = await fetch(backupUrl(`name=${encodeURIComponent(drawer.backup.name)}`), { method: "DELETE" });
|
|
115
|
+
if (!response.ok) {
|
|
116
|
+
toast.error(t("backup.removeError"));
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
closeDrawer();
|
|
120
|
+
toast.success(t("backup.removed"));
|
|
121
|
+
await reload();
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
toast.error(t("backup.networkRemove"));
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
setBusy(false);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
const columns = [
|
|
131
|
+
{
|
|
132
|
+
key: "name",
|
|
133
|
+
header: t("backup.file"),
|
|
134
|
+
render: (backup) => (_jsxs(Box, { className: "items-center gap-2", children: [_jsx(FiDatabase, { "aria-hidden": true, className: "h-4 w-4 shrink-0 text-gray-400" }), _jsx(Text, { as: "span", size: "caption", className: "truncate font-medium text-gray-900", children: backup.name })] })),
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
key: "createdAt",
|
|
138
|
+
header: t("common.createdAt"),
|
|
139
|
+
width: "160px",
|
|
140
|
+
render: (backup) => (_jsx(Text, { as: "span", size: "caption", className: "whitespace-nowrap text-gray-500", children: formatDateTime(backup.createdAt, locale) })),
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
key: "size",
|
|
144
|
+
header: t("backup.size"),
|
|
145
|
+
width: "110px",
|
|
146
|
+
render: (backup) => (_jsx(Text, { as: "span", size: "caption", className: "whitespace-nowrap text-gray-500", children: formatSize(backup.size) })),
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
key: "__actions",
|
|
150
|
+
header: "",
|
|
151
|
+
width: "110px",
|
|
152
|
+
align: "right",
|
|
153
|
+
render: (backup) => (_jsxs(Box, { className: "items-center justify-end gap-1", children: [_jsx("a", { href: backupUrl(`name=${encodeURIComponent(backup.name)}`), download: backup.name, title: t("backup.download"), "aria-label": t("backup.download"), className: "inline-flex h-8 w-8 items-center justify-center rounded-md text-gray-500 transition-colors hover:bg-gray-100 hover:text-gray-900", children: _jsx(FiDownload, { "aria-hidden": true }) }), _jsx(IconButton, { type: "button", label: t("common.remove"), className: "text-red-600 hover:bg-red-50 hover:text-red-700", onClick: () => setDrawer({ kind: "delete", backup }), children: _jsx(FiTrash2, { "aria-hidden": true }) })] })),
|
|
154
|
+
},
|
|
155
|
+
];
|
|
156
|
+
return (_jsxs(Box, { className: "flex-col gap-6 p-4 sm:p-6 lg:p-8", children: [_jsxs(Box, { className: "flex-col items-start justify-between gap-3 sm:flex-row sm:gap-4", children: [_jsxs(Box, { className: "flex-col gap-1", children: [_jsxs(Box, { className: "items-center gap-3", children: [_jsx(Text, { size: "title", className: "font-semibold text-gray-900", children: t("backup.title") }), _jsx(Badge, { tone: "gray", children: backups.length })] }), _jsx(Text, { size: "caption", className: "text-gray-500", children: t("backup.subtitle") })] }), _jsxs(Box, { className: "flex-wrap items-center gap-2", children: [_jsxs(Button, { type: "button", variant: "secondary", onClick: () => void reload(), disabled: loading, children: [_jsx(FiRefreshCw, { "aria-hidden": true }), " ", t("backup.refresh")] }), _jsxs(Button, { type: "button", variant: "secondary", onClick: downloadCurrent, disabled: busy, children: [_jsx(FiDownload, { "aria-hidden": true }), " ", t("backup.downloadCurrent")] }), _jsxs(Button, { type: "button", variant: "secondary", onClick: pickFile, disabled: busy, children: [_jsx(FiUpload, { "aria-hidden": true }), " ", t("backup.restoreAction")] }), _jsxs(Button, { type: "button", variant: "primary", onClick: () => void createBackup(), disabled: busy, children: [_jsx(FiPlus, { "aria-hidden": true }), " ", t("backup.create")] })] })] }), _jsx(Box, { className: "rounded-lg border border-gray-200 bg-white p-4", children: _jsx(Text, { size: "caption", className: "text-gray-500", children: t("backup.description") }) }), _jsx("input", { ref: fileRef, type: "file", accept: ".serenojs,application/gzip", className: "hidden", onChange: onFileChange }), _jsx(DataTable, { columns: columns, rows: backups, rowKey: (backup) => backup.name, loading: loading, empty: t("backup.empty") }), _jsxs(Drawer, { open: drawer.kind !== "closed", onClose: closeDrawer, title: drawer.kind === "restore"
|
|
157
|
+
? t("backup.restoreTitle")
|
|
158
|
+
: drawer.kind === "delete"
|
|
159
|
+
? t("backup.deleteTitle")
|
|
160
|
+
: "", description: drawer.kind === "restore"
|
|
161
|
+
? t("backup.restoreWarning")
|
|
162
|
+
: drawer.kind === "delete"
|
|
163
|
+
? t("common.deleteWarning")
|
|
164
|
+
: undefined, children: [drawer.kind === "restore" ? (_jsxs(Box, { className: "flex-col gap-6", children: [_jsx(Text, { className: "text-gray-700", children: t("backup.restoreConfirm", { name: drawer.file.name }) }), _jsxs(Box, { className: "items-center justify-end gap-2 border-t border-gray-200 pt-4", children: [_jsx(Button, { type: "button", variant: "secondary", onClick: closeDrawer, disabled: busy, children: t("common.cancel") }), _jsx(Button, { type: "button", variant: "danger", onClick: () => void confirmRestore(), disabled: busy, children: busy ? t("backup.restoring") : t("backup.restoreAction") })] })] })) : null, drawer.kind === "delete" ? (_jsxs(Box, { className: "flex-col gap-6", children: [_jsx(Text, { className: "text-gray-700", children: t("backup.deleteConfirm", { name: drawer.backup.name }) }), _jsxs(Box, { className: "items-center justify-end gap-2 border-t border-gray-200 pt-4", children: [_jsx(Button, { type: "button", variant: "secondary", onClick: closeDrawer, disabled: busy, children: t("common.cancel") }), _jsx(Button, { type: "button", variant: "danger", onClick: () => void confirmDelete(), disabled: busy, children: busy ? t("common.removing") : t("common.remove") })] })] })) : null] })] }));
|
|
165
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ComponentType } from "react";
|
|
2
|
+
import type { CollectionIconName } from "../../core/types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Curated registry of Feather icons available as collection icons.
|
|
5
|
+
*
|
|
6
|
+
* Add a new icon here (and to the map) to make it selectable in a
|
|
7
|
+
* `CollectionDef`. Only registered icons are bundled.
|
|
8
|
+
*/
|
|
9
|
+
export declare const collectionIcons: Record<CollectionIconName, ComponentType<{
|
|
10
|
+
className?: string | undefined;
|
|
11
|
+
"aria-hidden"?: boolean | undefined;
|
|
12
|
+
}>>;
|
|
13
|
+
export declare const DEFAULT_COLLECTION_ICON: CollectionIconName;
|
|
14
|
+
export declare function isCollectionIconName(value: string | undefined): value is CollectionIconName;
|
|
15
|
+
export type CollectionIconProps = {
|
|
16
|
+
name?: CollectionIconName | undefined;
|
|
17
|
+
className?: string | undefined;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Renders the Feather icon for a collection, falling back to a default icon.
|
|
21
|
+
*/
|
|
22
|
+
export declare const CollectionIcon: ({ name, className }: CollectionIconProps) => import("react").JSX.Element;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { FiArchive, FiBox, FiFileText, FiFolder, FiGrid, FiImage, FiLayout, FiSettings, FiStar, FiTag, FiUser, FiUsers, } from "react-icons/fi";
|
|
3
|
+
/**
|
|
4
|
+
* Curated registry of Feather icons available as collection icons.
|
|
5
|
+
*
|
|
6
|
+
* Add a new icon here (and to the map) to make it selectable in a
|
|
7
|
+
* `CollectionDef`. Only registered icons are bundled.
|
|
8
|
+
*/
|
|
9
|
+
export const collectionIcons = {
|
|
10
|
+
archive: FiArchive,
|
|
11
|
+
box: FiBox,
|
|
12
|
+
fileText: FiFileText,
|
|
13
|
+
folder: FiFolder,
|
|
14
|
+
grid: FiGrid,
|
|
15
|
+
image: FiImage,
|
|
16
|
+
layout: FiLayout,
|
|
17
|
+
settings: FiSettings,
|
|
18
|
+
star: FiStar,
|
|
19
|
+
tag: FiTag,
|
|
20
|
+
user: FiUser,
|
|
21
|
+
users: FiUsers,
|
|
22
|
+
};
|
|
23
|
+
export const DEFAULT_COLLECTION_ICON = "grid";
|
|
24
|
+
export function isCollectionIconName(value) {
|
|
25
|
+
return value !== undefined && value in collectionIcons;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Renders the Feather icon for a collection, falling back to a default icon.
|
|
29
|
+
*/
|
|
30
|
+
export const CollectionIcon = ({ name, className }) => {
|
|
31
|
+
const Icon = collectionIcons[name ?? DEFAULT_COLLECTION_ICON] ??
|
|
32
|
+
collectionIcons[DEFAULT_COLLECTION_ICON];
|
|
33
|
+
return _jsx(Icon, { "aria-hidden": true, className: className });
|
|
34
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { CollectionDef, ListResult, RelationOptionsMap } from "../../core/types.js";
|
|
2
|
+
export type CollectionManagerProps = {
|
|
3
|
+
collection: CollectionDef;
|
|
4
|
+
initial: ListResult;
|
|
5
|
+
relationOptions: RelationOptionsMap;
|
|
6
|
+
};
|
|
7
|
+
export declare const CollectionManager: ({ collection, initial, relationOptions, }: CollectionManagerProps) => import("react").JSX.Element;
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useCallback, useEffect, useState } from "react";
|
|
4
|
+
import { itemLabel, valueForLocale } from "../../core/schema.js";
|
|
5
|
+
import { Badge } from "../badge.js";
|
|
6
|
+
import { Box } from "../box.js";
|
|
7
|
+
import { Button, IconButton } from "../button.js";
|
|
8
|
+
import { Drawer } from "../drawer.js";
|
|
9
|
+
import { Text } from "../text.js";
|
|
10
|
+
import { FiEdit2, FiTrash2 } from "react-icons/fi";
|
|
11
|
+
import { renderFieldValue } from "./display.js";
|
|
12
|
+
import { ItemForm } from "./item-form.js";
|
|
13
|
+
import { Pagination } from "./pagination.js";
|
|
14
|
+
import { DataTable } from "./table.js";
|
|
15
|
+
import { useToast } from "./toast.js";
|
|
16
|
+
import { useI18n } from "./i18n.js";
|
|
17
|
+
import { useCmsUrls } from "./runtime.js";
|
|
18
|
+
const LIMIT = 10;
|
|
19
|
+
/** Deterministic (UTC) date format to avoid SSR/client hydration mismatches. */
|
|
20
|
+
function formatDateTime(value, locale) {
|
|
21
|
+
if (typeof value !== "string" || !value)
|
|
22
|
+
return "—";
|
|
23
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2})/.exec(value);
|
|
24
|
+
if (!match)
|
|
25
|
+
return value;
|
|
26
|
+
const date = locale === "en" ? `${match[2]}/${match[3]}/${match[1]}` : `${match[3]}/${match[2]}/${match[1]}`;
|
|
27
|
+
return `${date} ${match[4]}:${match[5]}`;
|
|
28
|
+
}
|
|
29
|
+
export const CollectionManager = ({ collection, initial, relationOptions, }) => {
|
|
30
|
+
const [data, setData] = useState(initial);
|
|
31
|
+
const [loading, setLoading] = useState(false);
|
|
32
|
+
const [query, setQuery] = useState("");
|
|
33
|
+
const [search, setSearch] = useState("");
|
|
34
|
+
const [drawer, setDrawer] = useState({ kind: "closed" });
|
|
35
|
+
const [submitting, setSubmitting] = useState(false);
|
|
36
|
+
const [errors, setErrors] = useState({});
|
|
37
|
+
const [formError, setFormError] = useState(null);
|
|
38
|
+
const toast = useToast();
|
|
39
|
+
const { api } = useCmsUrls();
|
|
40
|
+
const { t, locale } = useI18n();
|
|
41
|
+
// Reset local state whenever the collection changes so navigating between
|
|
42
|
+
// models never shows stale data.
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
setData(initial);
|
|
45
|
+
setQuery("");
|
|
46
|
+
setSearch("");
|
|
47
|
+
setDrawer({ kind: "closed" });
|
|
48
|
+
setErrors({});
|
|
49
|
+
setFormError(null);
|
|
50
|
+
setLoading(false);
|
|
51
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
52
|
+
}, [collection.slug]);
|
|
53
|
+
const load = useCallback(async (nextPage, nextSearch) => {
|
|
54
|
+
setLoading(true);
|
|
55
|
+
try {
|
|
56
|
+
const params = new URLSearchParams({
|
|
57
|
+
page: String(nextPage),
|
|
58
|
+
limit: String(LIMIT),
|
|
59
|
+
q: nextSearch,
|
|
60
|
+
});
|
|
61
|
+
const response = await fetch(`${api(collection.slug)}?${params.toString()}`);
|
|
62
|
+
if (!response.ok)
|
|
63
|
+
throw new Error(t("collection.loadError"));
|
|
64
|
+
const json = (await response.json());
|
|
65
|
+
setData(json);
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
toast.error(error instanceof Error ? error.message : t("app.unexpected"));
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
setLoading(false);
|
|
72
|
+
}
|
|
73
|
+
}, [collection.slug, toast]);
|
|
74
|
+
const closeDrawer = useCallback(() => {
|
|
75
|
+
setDrawer({ kind: "closed" });
|
|
76
|
+
setErrors({});
|
|
77
|
+
setFormError(null);
|
|
78
|
+
}, []);
|
|
79
|
+
const openCreate = () => {
|
|
80
|
+
setErrors({});
|
|
81
|
+
setFormError(null);
|
|
82
|
+
setDrawer({ kind: "form", item: null });
|
|
83
|
+
};
|
|
84
|
+
const openEdit = (item) => {
|
|
85
|
+
setErrors({});
|
|
86
|
+
setFormError(null);
|
|
87
|
+
setDrawer({ kind: "form", item });
|
|
88
|
+
};
|
|
89
|
+
const openDelete = (item) => {
|
|
90
|
+
setErrors({});
|
|
91
|
+
setFormError(null);
|
|
92
|
+
setDrawer({ kind: "delete", item });
|
|
93
|
+
};
|
|
94
|
+
const submitForm = async (values) => {
|
|
95
|
+
if (drawer.kind !== "form")
|
|
96
|
+
return;
|
|
97
|
+
const editing = drawer.item;
|
|
98
|
+
setSubmitting(true);
|
|
99
|
+
setErrors({});
|
|
100
|
+
setFormError(null);
|
|
101
|
+
try {
|
|
102
|
+
const response = await fetch(editing
|
|
103
|
+
? api(`${collection.slug}/${editing.id}`)
|
|
104
|
+
: api(collection.slug), {
|
|
105
|
+
method: editing ? "PATCH" : "POST",
|
|
106
|
+
headers: { "Content-Type": "application/json" },
|
|
107
|
+
body: JSON.stringify(values),
|
|
108
|
+
});
|
|
109
|
+
const json = (await response.json());
|
|
110
|
+
if (!response.ok) {
|
|
111
|
+
setErrors(json.errors ?? {});
|
|
112
|
+
setFormError(json.message ?? t("collection.saveError"));
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
closeDrawer();
|
|
116
|
+
toast.success(editing ? t("collection.updated") : t("collection.created"));
|
|
117
|
+
await load(editing ? data.page : 1, search);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
setFormError(t("collection.networkSave"));
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
setSubmitting(false);
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
const confirmDelete = async () => {
|
|
127
|
+
if (drawer.kind !== "delete")
|
|
128
|
+
return;
|
|
129
|
+
setSubmitting(true);
|
|
130
|
+
try {
|
|
131
|
+
const response = await fetch(api(`${collection.slug}/${drawer.item.id}`), { method: "DELETE" });
|
|
132
|
+
if (!response.ok)
|
|
133
|
+
throw new Error(t("collection.removeError"));
|
|
134
|
+
closeDrawer();
|
|
135
|
+
toast.success(t("collection.removed"));
|
|
136
|
+
await load(data.page, search);
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
toast.error(error instanceof Error ? error.message : t("app.unexpected"));
|
|
140
|
+
}
|
|
141
|
+
finally {
|
|
142
|
+
setSubmitting(false);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const fieldColumn = (field) => ({
|
|
146
|
+
key: field.name,
|
|
147
|
+
header: field.label,
|
|
148
|
+
render: (row) => renderFieldValue(field, valueForLocale(collection, field, row), relationOptions[field.name] ?? []),
|
|
149
|
+
});
|
|
150
|
+
const idColumn = {
|
|
151
|
+
key: "id",
|
|
152
|
+
header: "ID",
|
|
153
|
+
width: "70px",
|
|
154
|
+
render: (row) => (_jsxs(Text, { as: "span", size: "caption", className: "text-gray-400", children: ["#", row.id] })),
|
|
155
|
+
};
|
|
156
|
+
const createdAtColumn = {
|
|
157
|
+
key: "createdAt",
|
|
158
|
+
header: t("common.createdAt"),
|
|
159
|
+
width: "150px",
|
|
160
|
+
render: (row) => (_jsx(Text, { as: "span", size: "caption", className: "whitespace-nowrap text-gray-500", children: formatDateTime(row.createdAt, locale) })),
|
|
161
|
+
};
|
|
162
|
+
const updatedAtColumn = {
|
|
163
|
+
key: "updatedAt",
|
|
164
|
+
header: t("common.updatedAt"),
|
|
165
|
+
width: "150px",
|
|
166
|
+
render: (row) => (_jsx(Text, { as: "span", size: "caption", className: "whitespace-nowrap text-gray-500", children: formatDateTime(row.updatedAt, locale) })),
|
|
167
|
+
};
|
|
168
|
+
const actionsColumn = {
|
|
169
|
+
key: "__actions",
|
|
170
|
+
header: "",
|
|
171
|
+
width: "110px",
|
|
172
|
+
align: "right",
|
|
173
|
+
render: (row) => (_jsxs(Box, { className: "items-center justify-end gap-1", children: [_jsx(IconButton, { type: "button", label: "Editar", onClick: () => openEdit(row), children: _jsx(FiEdit2, { "aria-hidden": true }) }), _jsx(IconButton, { type: "button", label: "Remover", className: "text-red-600 hover:bg-red-50 hover:text-red-700", onClick: () => openDelete(row), children: _jsx(FiTrash2, { "aria-hidden": true }) })] })),
|
|
174
|
+
};
|
|
175
|
+
const fieldsByName = new Map(collection.fields.map((field) => [field.name, field]));
|
|
176
|
+
const configuredColumns = collection.list && collection.list.length > 0
|
|
177
|
+
? collection.list
|
|
178
|
+
: [
|
|
179
|
+
"id",
|
|
180
|
+
...collection.fields.map((field) => field.name),
|
|
181
|
+
"createdAt",
|
|
182
|
+
"updatedAt",
|
|
183
|
+
];
|
|
184
|
+
const columns = [
|
|
185
|
+
...configuredColumns.flatMap((name) => {
|
|
186
|
+
if (name === "id")
|
|
187
|
+
return [idColumn];
|
|
188
|
+
if (name === "createdAt")
|
|
189
|
+
return [createdAtColumn];
|
|
190
|
+
if (name === "updatedAt")
|
|
191
|
+
return [updatedAtColumn];
|
|
192
|
+
const field = fieldsByName.get(name);
|
|
193
|
+
return field ? [fieldColumn(field)] : [];
|
|
194
|
+
}),
|
|
195
|
+
actionsColumn,
|
|
196
|
+
];
|
|
197
|
+
return (_jsxs(Box, { className: "flex-col gap-6 p-4 sm:p-6 lg:p-8", children: [_jsxs(Box, { className: "flex-col items-start justify-between gap-3 sm:flex-row sm:gap-4", children: [_jsxs(Box, { className: "flex-col gap-1", children: [_jsxs(Box, { className: "items-center gap-3", children: [_jsx(Text, { size: "title", className: "font-semibold text-gray-900", children: collection.label }), _jsx(Badge, { tone: "gray", children: data.total })] }), collection.description ? (_jsx(Text, { size: "caption", className: "text-gray-500", children: collection.description })) : null] }), _jsx(Button, { type: "button", variant: "primary", onClick: openCreate, children: t("collection.newItem") })] }), _jsx(Box, { className: "flex-col items-stretch justify-between gap-3 sm:flex-row sm:items-center sm:gap-4", children: _jsxs(Box, { as: "form", className: "items-stretch gap-2 flex-row sm:items-center", onSubmit: (event) => {
|
|
198
|
+
event.preventDefault();
|
|
199
|
+
setSearch(query);
|
|
200
|
+
void load(1, query);
|
|
201
|
+
}, children: [_jsx("input", { type: "search", value: query, placeholder: t("collection.searchPlaceholder"), onChange: (event) => setQuery(event.target.value), className: "w-full rounded-md border border-gray-300 bg-white px-3 py-1.5 text-sm text-gray-900 outline-none focus:border-gray-900 focus:ring-1 focus:ring-gray-900 sm:w-64" }), _jsxs(Box, { className: "items-center gap-2", children: [_jsx(Button, { type: "submit", variant: "secondary", size: "sm", children: t("common.search") }), search ? (_jsx(Button, { type: "button", variant: "ghost", size: "sm", onClick: () => {
|
|
202
|
+
setQuery("");
|
|
203
|
+
setSearch("");
|
|
204
|
+
void load(1, "");
|
|
205
|
+
}, children: t("common.clear") })) : null] })] }) }), _jsx(DataTable, { columns: columns, rows: data.items, rowKey: (row) => row.id, loading: loading, empty: t("collection.empty") }), _jsx(Pagination, { page: data.page, totalPages: data.totalPages, total: data.total, limit: data.limit, onPage: (next) => void load(next, search) }), _jsxs(Drawer, { open: drawer.kind !== "closed", onClose: closeDrawer, title: drawer.kind === "form"
|
|
206
|
+
? drawer.item
|
|
207
|
+
? t("collection.editTitle", { id: drawer.item.id })
|
|
208
|
+
: t("collection.newTitle", { label: collection.label })
|
|
209
|
+
: t("collection.removeTitle"), description: drawer.kind === "delete"
|
|
210
|
+
? t("common.deleteWarning")
|
|
211
|
+
: undefined, width: drawer.kind === "form" ? "lg" : "md", children: [drawer.kind === "form" ? (_jsx(ItemForm, { collection: collection, item: drawer.item, submitting: submitting, errors: errors, formError: formError, relationOptions: relationOptions, onSubmit: submitForm, onCancel: closeDrawer }, drawer.item ? drawer.item.id : "new")) : null, drawer.kind === "delete" ? (_jsxs(Box, { className: "flex-col gap-6", children: [_jsx(Box, { className: "flex-col gap-2", children: _jsx(Text, { className: "text-gray-700", children: t("collection.removeConfirm", { label: itemLabel(collection, drawer.item) }) }) }), _jsxs(Box, { className: "items-center justify-end gap-2 border-t border-gray-200 pt-4", children: [_jsx(Button, { type: "button", variant: "secondary", onClick: closeDrawer, disabled: submitting, children: t("common.cancel") }), _jsx(Button, { type: "button", variant: "danger", onClick: () => void confirmDelete(), disabled: submitting, children: submitting ? t("common.removing") : t("common.remove") })] })] })) : null] })] }));
|
|
212
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import type { FieldDef, FieldValue, RelationOption } from "../../core/types.js";
|
|
3
|
+
import type { AdminLocale } from "./i18n.js";
|
|
4
|
+
/**
|
|
5
|
+
* Turns a stored value into a presentational node for tables.
|
|
6
|
+
*/
|
|
7
|
+
export declare function renderFieldValue(field: FieldDef, value: FieldValue, options?: RelationOption[], locale?: AdminLocale): ReactNode;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { relationIsMany } from "../../core/schema.js";
|
|
3
|
+
import { isImageUrl } from "../../core/media.js";
|
|
4
|
+
import { Badge } from "../badge.js";
|
|
5
|
+
import { Box } from "../box.js";
|
|
6
|
+
import { Text } from "../text.js";
|
|
7
|
+
import { translate } from "./i18n.js";
|
|
8
|
+
function labelFor(options, id) {
|
|
9
|
+
return options.find((option) => option.id === id)?.label ?? `#${id}`;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Turns a stored value into a presentational node for tables.
|
|
13
|
+
*/
|
|
14
|
+
export function renderFieldValue(field, value, options = [], locale = "en") {
|
|
15
|
+
if (field.type === "boolean") {
|
|
16
|
+
return _jsx(Badge, { tone: value ? "green" : "gray", children: value ? translate(locale, "common.yes") : translate(locale, "common.no") });
|
|
17
|
+
}
|
|
18
|
+
if (field.type === "select") {
|
|
19
|
+
const option = field.options?.find((entry) => entry.value === value);
|
|
20
|
+
return _jsx(Badge, { tone: "blue", children: option?.label ?? String(value ?? "—") });
|
|
21
|
+
}
|
|
22
|
+
if (field.type === "media") {
|
|
23
|
+
if (value === null || value === undefined || value === "") {
|
|
24
|
+
return _jsx(Text, { as: "span", size: "caption", className: "text-gray-300", children: "\u2014" });
|
|
25
|
+
}
|
|
26
|
+
const url = String(value);
|
|
27
|
+
if (isImageUrl(url)) {
|
|
28
|
+
// eslint-disable-next-line @next/next/no-img-element
|
|
29
|
+
return (_jsx("img", { src: url, alt: "", className: "h-10 w-16 rounded border border-gray-200 object-cover" }));
|
|
30
|
+
}
|
|
31
|
+
return (_jsx("a", { href: url, target: "_blank", rel: "noreferrer", className: "truncate text-sm text-gray-600 underline hover:text-gray-900", children: url.split("/").pop() ?? url }));
|
|
32
|
+
}
|
|
33
|
+
if (field.type === "list") {
|
|
34
|
+
const items = Array.isArray(value) ? value : [];
|
|
35
|
+
if (items.length === 0) {
|
|
36
|
+
return _jsx(Text, { as: "span", size: "caption", className: "text-gray-300", children: "\u2014" });
|
|
37
|
+
}
|
|
38
|
+
return (_jsxs(Box, { className: "flex-wrap items-center gap-1", children: [items.slice(0, 2).map((item, index) => (_jsx(Badge, { tone: "gray", children: item }, `${item}-${index}`))), items.length > 2 ? _jsxs(Badge, { tone: "gray", children: ["+", items.length - 2] }) : null] }));
|
|
39
|
+
}
|
|
40
|
+
if (field.type === "media-list") {
|
|
41
|
+
const items = Array.isArray(value) ? value : [];
|
|
42
|
+
if (items.length === 0) {
|
|
43
|
+
return _jsx(Text, { as: "span", size: "caption", className: "text-gray-300", children: "\u2014" });
|
|
44
|
+
}
|
|
45
|
+
return (_jsxs(Box, { className: "items-center gap-1", children: [items.slice(0, 2).map((item, index) => isImageUrl(item.url) ? (_jsx("img", { src: item.url, alt: item.alt ?? "", className: "h-10 w-16 rounded border border-gray-200 object-cover" }, `${item.url}-${index}`)) : null), items.length > 2 ? _jsxs(Badge, { tone: "gray", children: ["+", items.length - 2] }) : null] }));
|
|
46
|
+
}
|
|
47
|
+
if (field.type === "relation") {
|
|
48
|
+
if (relationIsMany(field)) {
|
|
49
|
+
const ids = Array.isArray(value) ? value : [];
|
|
50
|
+
if (ids.length === 0)
|
|
51
|
+
return _jsx(Text, { as: "span", size: "caption", className: "text-gray-300", children: "\u2014" });
|
|
52
|
+
const visible = ids.slice(0, 2);
|
|
53
|
+
const rest = ids.length - visible.length;
|
|
54
|
+
return (_jsxs(Box, { className: "items-center gap-1", children: [visible.map((id) => (_jsx(Badge, { tone: "amber", children: labelFor(options, id) }, id))), rest > 0 ? _jsxs(Badge, { tone: "gray", children: ["+", rest] }) : null] }));
|
|
55
|
+
}
|
|
56
|
+
if (value === null || value === undefined || value === "") {
|
|
57
|
+
return _jsx(Text, { as: "span", size: "caption", className: "text-gray-300", children: "\u2014" });
|
|
58
|
+
}
|
|
59
|
+
return _jsx(Badge, { tone: "amber", children: labelFor(options, Number(value)) });
|
|
60
|
+
}
|
|
61
|
+
if (value === null || value === undefined || value === "") {
|
|
62
|
+
return (_jsx(Text, { as: "span", size: "caption", className: "text-gray-300", children: "\u2014" }));
|
|
63
|
+
}
|
|
64
|
+
return (_jsx(Text, { as: "span", size: "caption", className: "block truncate text-gray-700", children: String(value) }));
|
|
65
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { FieldDef, FieldValue, RelationOption } from "../../core/types.js";
|
|
2
|
+
export type FieldControlProps = {
|
|
3
|
+
field: FieldDef;
|
|
4
|
+
collectionSlug: string;
|
|
5
|
+
value: FieldValue;
|
|
6
|
+
error?: string | undefined;
|
|
7
|
+
disabled?: boolean | undefined;
|
|
8
|
+
relationOptions?: RelationOption[] | undefined;
|
|
9
|
+
onChange: (value: FieldValue) => void;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Renders a single descriptor field as the right input control.
|
|
13
|
+
*/
|
|
14
|
+
export declare const FieldControl: ({ field, collectionSlug, value, error, disabled, relationOptions, onChange, }: FieldControlProps) => import("react").JSX.Element;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Box } from "../box.js";
|
|
3
|
+
import { FieldWrapper, inputClass } from "../field-wrapper.js";
|
|
4
|
+
import { Text } from "../text.js";
|
|
5
|
+
import { MediaControl } from "./media-control.js";
|
|
6
|
+
import { MediaListControl } from "./media-list-control.js";
|
|
7
|
+
import { ListControl } from "./list-control.js";
|
|
8
|
+
import { RelationControl } from "./relation-control.js";
|
|
9
|
+
import { useI18n } from "./i18n.js";
|
|
10
|
+
/**
|
|
11
|
+
* Renders a single descriptor field as the right input control.
|
|
12
|
+
*/
|
|
13
|
+
export const FieldControl = ({ field, collectionSlug, value, error, disabled, relationOptions, onChange, }) => {
|
|
14
|
+
const { t } = useI18n();
|
|
15
|
+
const id = `field-${field.name}`;
|
|
16
|
+
if (field.type === "media") {
|
|
17
|
+
return (_jsx(MediaControl, { field: field, collectionSlug: collectionSlug, value: typeof value === "string" ? value : "", error: error, disabled: disabled, onChange: onChange }));
|
|
18
|
+
}
|
|
19
|
+
if (field.type === "media-list") {
|
|
20
|
+
return (_jsx(MediaListControl, { field: field, collectionSlug: collectionSlug, value: Array.isArray(value) ? value : [], error: error, disabled: disabled, onChange: onChange }));
|
|
21
|
+
}
|
|
22
|
+
if (field.type === "list") {
|
|
23
|
+
return (_jsx(ListControl, { field: field, value: Array.isArray(value) ? value : [], error: error, disabled: disabled, onChange: onChange }));
|
|
24
|
+
}
|
|
25
|
+
if (field.type === "relation") {
|
|
26
|
+
return (_jsx(RelationControl, { field: field, value: value, options: relationOptions ?? [], error: error, disabled: disabled, onChange: onChange }));
|
|
27
|
+
}
|
|
28
|
+
if (field.type === "boolean") {
|
|
29
|
+
return (_jsxs(Box, { className: "items-start gap-3 rounded-md border border-gray-200 bg-gray-50 px-3 py-2.5", children: [_jsx("input", { id: id, type: "checkbox", checked: Boolean(value), disabled: disabled, onChange: (event) => onChange(event.target.checked), className: "mt-0.5 h-4 w-4 cursor-pointer accent-gray-900" }), _jsxs(Box, { className: "flex-col", children: [_jsx(Text, { as: "label", htmlFor: id, size: "caption", className: "cursor-pointer font-medium text-gray-800", children: field.label }), field.help ? (_jsx(Text, { size: "caption", className: "text-gray-400", children: field.help })) : null] })] }));
|
|
30
|
+
}
|
|
31
|
+
const shared = {
|
|
32
|
+
id,
|
|
33
|
+
disabled,
|
|
34
|
+
"aria-invalid": Boolean(error),
|
|
35
|
+
className: `${inputClass} ${error ? "border-red-400 focus:border-red-500 focus:ring-red-500" : "border-gray-300"}`,
|
|
36
|
+
};
|
|
37
|
+
let control;
|
|
38
|
+
switch (field.type) {
|
|
39
|
+
case "textarea":
|
|
40
|
+
control = (_jsx("textarea", { ...shared, rows: 4, value: String(value ?? ""), placeholder: field.placeholder, onChange: (event) => onChange(event.target.value) }));
|
|
41
|
+
break;
|
|
42
|
+
case "select":
|
|
43
|
+
control = (_jsxs("select", { ...shared, value: String(value ?? ""), onChange: (event) => onChange(event.target.value), children: [_jsx("option", { value: "", children: t("field.select") }), (field.options ?? []).map((option) => (_jsx("option", { value: option.value, children: option.label }, option.value)))] }));
|
|
44
|
+
break;
|
|
45
|
+
case "number":
|
|
46
|
+
control = (_jsx("input", { ...shared, type: "number", min: field.min, max: field.max, value: value === null || value === undefined ? "" : String(value), placeholder: field.placeholder, onChange: (event) => onChange(event.target.value === "" ? "" : Number(event.target.value)) }));
|
|
47
|
+
break;
|
|
48
|
+
case "email":
|
|
49
|
+
control = (_jsx("input", { ...shared, type: "email", value: String(value ?? ""), placeholder: field.placeholder, onChange: (event) => onChange(event.target.value) }));
|
|
50
|
+
break;
|
|
51
|
+
case "date":
|
|
52
|
+
control = (_jsx("input", { ...shared, type: "date", value: String(value ?? ""), onChange: (event) => onChange(event.target.value) }));
|
|
53
|
+
break;
|
|
54
|
+
default:
|
|
55
|
+
control = (_jsx("input", { ...shared, type: "text", value: String(value ?? ""), placeholder: field.placeholder, onChange: (event) => onChange(event.target.value) }));
|
|
56
|
+
}
|
|
57
|
+
return (_jsx(FieldWrapper, { label: field.label, htmlFor: id, required: field.required, help: field.help, error: error, children: control }));
|
|
58
|
+
};
|