"use client"; import { useCallback, useEffect, useState, type ReactNode } from "react"; import { FileUploadField } from "@/components/admin/FileUploadField"; export interface FieldDef { key: string; label: string; type?: "text" | "textarea" | "number" | "checkbox" | "select" | "image" | "file"; options?: { value: string; label: string }[]; required?: boolean; placeholder?: string; defaultValue?: string | number | boolean; } export interface ColumnDef { key: string; label: string; render?: (row: Record) => ReactNode; } export interface ResourceConfig { title: string; description?: string; basePath: string; // e.g. "categories" idKey?: string; // default "id" listKey?: string; // wrap key, e.g. "items"; omit if response is a bare array columns: ColumnDef[]; fields?: FieldDef[]; canCreate?: boolean; canEdit?: boolean; canDelete?: boolean; rowActions?: (row: Record, reload: () => void) => ReactNode; } const card = "rounded-xl border border-[#1e2235] bg-[#0f1120]"; const btn = "rounded-lg bg-indigo-600 px-3 py-1.5 text-xs font-medium text-white hover:bg-indigo-500 disabled:opacity-50"; const btnGhost = "rounded-lg border border-[#262b40] px-3 py-1.5 text-xs text-gray-300 hover:bg-[#161a2e]"; const inputCls = "w-full rounded-lg border border-[#262b40] bg-[#0c0e1a] px-3 py-2 text-sm text-gray-100 outline-none focus:border-indigo-500"; export function AdminResource({ config }: { config: ResourceConfig }) { const idKey = config.idKey ?? "id"; const [rows, setRows] = useState[]>([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [editing, setEditing] = useState | null>(null); const [creating, setCreating] = useState(false); const [form, setForm] = useState>({}); const [saving, setSaving] = useState(false); const url = (suffix = "") => `/api/admin/resource/${config.basePath}${suffix}`; const reload = useCallback(async () => { setLoading(true); setError(null); try { const res = await fetch(url(), { cache: "no-store" }); const data = await res.json(); if (!res.ok) throw new Error(data?.error ?? "Failed to load"); const list = config.listKey ? data?.[config.listKey] : data; setRows(Array.isArray(list) ? list : (data?.items ?? [])); } catch (e) { setError(e instanceof Error ? e.message : "Failed to load"); } finally { setLoading(false); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [config.basePath, config.listKey]); useEffect(() => { reload(); }, [reload]); const openCreate = () => { const init: Record = {}; config.fields?.forEach((f) => (init[f.key] = f.defaultValue ?? (f.type === "checkbox" ? false : ""))); setForm(init); setCreating(true); setEditing(null); }; const openEdit = (row: Record) => { const init: Record = {}; config.fields?.forEach((f) => (init[f.key] = row[f.key] ?? (f.type === "checkbox" ? false : ""))); setForm(init); setEditing(row); setCreating(false); }; const closeForm = () => { setCreating(false); setEditing(null); setForm({}); }; const submit = async () => { setSaving(true); setError(null); try { const isEdit = !!editing; const res = await fetch(isEdit ? url(`/${editing![idKey]}`) : url(), { method: isEdit ? "PUT" : "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(form), }); const data = await res.json().catch(() => null); if (!res.ok) throw new Error(data?.error ?? "Save failed"); closeForm(); reload(); } catch (e) { setError(e instanceof Error ? e.message : "Save failed"); } finally { setSaving(false); } }; const remove = async (row: Record) => { if (!confirm(`Delete this ${config.title.replace(/s$/, "").toLowerCase()}?`)) return; const res = await fetch(url(`/${row[idKey]}`), { method: "DELETE" }); if (res.ok) reload(); else { const d = await res.json().catch(() => null); setError(d?.error ?? "Delete failed"); } }; return (

{config.title}

{config.description &&

{config.description}

}
{config.canCreate && config.fields && ( )}
{error &&

{error}

}
{config.columns.map((c) => ( ))} {(config.canEdit || config.canDelete || config.rowActions) && ( )} {loading ? ( ) : rows.length === 0 ? ( ) : ( rows.map((row, i) => ( {config.columns.map((c) => ( ))} {(config.canEdit || config.canDelete || config.rowActions) && ( )} )) )}
{c.label}Actions
Loading…
No records.
{c.render ? c.render(row) : formatCell(row[c.key])}
{config.rowActions?.(row, reload)} {config.canEdit && config.fields && ( )} {config.canDelete && ( )}
{(creating || editing) && config.fields && (
e.stopPropagation()}>

{editing ? "Edit" : "New"} {config.title.replace(/s$/, "")}

{config.fields.map((f) => (
{f.type !== "checkbox" && ( )} {f.type === "textarea" ? (