import { useMemo, useState } from "react"; import { api, isAdmin, scopeAllowed } from "../api"; import { useAction, usePermissions, useUsers } from "../queries"; import { useApp } from "../store"; import type { Me, PermissionCatalog } from "../types"; /** The permission matrix: scopes rendered as checkboxes instead of raw strings. */ interface Matrix { admin: boolean; guns: string[]; // "cp:conn" allRelays: boolean; relays: string[]; } function toMatrix(scopes: string[], catalog: PermissionCatalog): Matrix { const nonAdmin = scopes.filter((s) => s !== "admin" && s !== "*"); const guns: string[] = []; for (const [cp, meta] of Object.entries(catalog.chargers)) for (const conn of Object.keys(meta.guns)) if (scopeAllowed(nonAdmin, `charger:${cp}:${conn}`)) guns.push(`${cp}:${conn}`); const allRelays = nonAdmin.includes("relay:*"); const relays = allRelays ? [] : Object.keys(catalog.relays).filter((r) => scopeAllowed(nonAdmin, `relay:${r}`)); return { admin: isAdmin(scopes), guns, allRelays, relays }; } function toScopes(m: Matrix): string[] { if (m.admin) return ["admin"]; return [ ...m.guns.map((g) => `charger:${g}`), ...(m.allRelays ? ["relay:*"] : m.relays.map((r) => `relay:${r}`)), ]; } function MatrixEditor({ catalog, value, onChange, }: { catalog: PermissionCatalog; value: Matrix; onChange: (m: Matrix) => void; }) { const toggleList = (list: string[], item: string) => list.includes(item) ? list.filter((x) => x !== item) : [...list, item]; return (
{!value.admin && ( <>
Charging guns {Object.entries(catalog.chargers).map(([cp, meta]) => Object.entries(meta.guns).map(([conn, label]) => { const gun = `${cp}:${conn}`; return ( ); }), )} {!Object.keys(catalog.chargers).length && ( no chargers known yet )}
Relays {!value.allRelays && Object.entries(catalog.relays).map(([name, meta]) => ( ))} {!value.allRelays && !Object.keys(catalog.relays).length && ( no relays registered yet )}
)}
); } function UserEditor({ user, catalog, onClose, }: { user: Me | null; // null = create new catalog: PermissionCatalog; onClose: () => void; }) { const toast = useApp((s) => s.toast); const [name, setName] = useState(user?.name ?? ""); const [password, setPassword] = useState(""); const [matrix, setMatrix] = useState(() => user ? toMatrix(user.scopes, catalog) : { admin: false, guns: [], allRelays: false, relays: [] }, ); const save = useAction( () => api.put(`/users/${encodeURIComponent(name.trim())}`, { ...(password ? { password } : {}), scopes: toScopes(matrix), }), [["users"]], () => { toast(user ? "User updated" : "User created"); onClose(); }, ); return (
{ e.preventDefault(); save.mutate(); }} >

{user ? `Edit ${user.name}` : "New user"}

); } function describeScopes(scopes: string[], catalog: PermissionCatalog): string { if (isAdmin(scopes)) return "Administrator"; const m = toMatrix(scopes, catalog); const parts: string[] = []; for (const gun of m.guns) { const [cp, conn] = gun.split(":"); const meta = catalog.chargers[cp]; parts.push(meta ? `${meta.label} ${meta.guns[conn] ?? `gun ${conn}`}` : gun); } if (m.allRelays) parts.push("all relays"); else parts.push(...m.relays.map((r) => catalog.relays[r]?.label ?? r)); return parts.length ? parts.join(" · ") : "No permissions"; } export default function Users() { const me = useApp((s) => s.me)!; const toast = useApp((s) => s.toast); const { data: users } = useUsers(); const { data: catalog } = usePermissions(); const [editing, setEditing] = useState(null); const remove = useAction( (name: string) => api.delete(`/users/${encodeURIComponent(name)}`), [["users"]], () => toast("User deleted (their sessions are now invalid)"), ); const sorted = useMemo( () => [...(users ?? [])].sort((a, b) => a.name.localeCompare(b.name)), [users], ); if (!catalog) return

Loading permission catalog…

; return (

Users

{!sorted.length && (

No users yet — the API is open until the first account exists. Create the admin account now.

)}
{sorted.map((u) => ( ))}
{u.name} {describeScopes(u.scopes, catalog)}
{editing !== null && ( setEditing(null)} /> )}
); }