import { useEffect, useState } from "preact/hooks"; type Perm = { id: string; nom: string }; type Role = { id: number; nom: string; permissions: string[] }; const ROLE_COLORS = [ "#22c55e", "#d4a017", "#e07020", "#8b5cf6", "#06b6d4", "#ec4899", ]; function roleColor(roleId: number): string { return ROLE_COLORS[(roleId - 1) % ROLE_COLORS.length]; } export default function AdminPermissions() { const [permissions, setPermissions] = useState([]); const [roles, setRoles] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { async function load() { try { const [pRes, rRes] = await Promise.all([ fetch("/admin/api/permissions"), fetch("/admin/api/roles"), ]); if (!pRes.ok) throw new Error("Impossible de charger les permissions"); setPermissions(await pRes.json()); if (rRes.ok) setRoles(await rRes.json()); } catch (e) { setError(e instanceof Error ? e.message : "Erreur"); } finally { setLoading(false); } } load(); }, []); function rolesForPerm(permId: string): Role[] { return roles.filter((r) => r.permissions.includes(permId)); } const MAX_ROLE_CHIPS = 2; return (

Permissions

Les permissions sont définies statiquement par le serveur.

Elles ne peuvent pas être créées ou supprimées via l'API.

{error &&

{error}

} {loading ?

Chargement…

: (
{permissions.map((p) => { const associated = rolesForPerm(p.id); const shown = associated.slice(0, MAX_ROLE_CHIPS); const overflow = associated.length - MAX_ROLE_CHIPS; return ( ); })}
idPermission nomPermission Rôles associés
{p.id} {p.nom}
{shown.map((r) => ( {r.nom} ))} {overflow > 0 && ( +{overflow} )} {associated.length === 0 && ( )}
)}
); }