"use client"; import { useState, useTransition } from "react"; interface PluginRow { key: string; name: string; description: string; category: string; version: string; enabled: boolean; config: Record; } const CATEGORY_LABEL: Record = { visual: "Visuels", business: "Métier", content: "Contenus", i18n: "Internationalisation", core: "Core", }; export default function PluginToggleTable({ plugins: initial }: { plugins: PluginRow[] }) { const [plugins, setPlugins] = useState(initial); const [pending, startTransition] = useTransition(); const [busyKey, setBusyKey] = useState(null); const [error, setError] = useState(null); const byCategory = plugins.reduce>((acc, p) => { (acc[p.category] ??= []).push(p); return acc; }, {}); async function toggle(key: string, next: boolean) { setError(null); setBusyKey(key); try { const res = await fetch(`/api/admin/plugins/${encodeURIComponent(key)}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ enabled: next }), }); if (!res.ok) { const body = await res.json().catch(() => ({})); throw new Error(body?.error || `HTTP ${res.status}`); } const updated = await res.json(); startTransition(() => { setPlugins((curr) => curr.map((p) => (p.key === key ? { ...p, enabled: !!updated.enabled } : p)), ); }); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setBusyKey(null); } } return (
{error && (
{error}
)} {Object.entries(byCategory).map(([category, rows]) => (

{CATEGORY_LABEL[category] ?? category}

    {rows.map((p) => (
  • {p.name} {p.key} v{p.version}

    {p.description}

  • ))}
))}
); }