import { useEffect, useState } from "react"; import { fetchOccupancy, fetchSiteConfig, setCapacity, type Occupancy } from "./api.js"; // Live occupancy + capacity. Occupancy is shown to everyone (it's a fold over the // signed ledger); the capacity field is admin-editable. The FULL gate (refuse // transient entry at capacity) is enforced server-side in the entry flow. // See wiki/concepts/capacity-occupancy.md. export function SiteSettings({ canEdit }: { canEdit: boolean }) { const [occ, setOcc] = useState(null); const [capInput, setCapInput] = useState(""); const [msg, setMsg] = useState(null); function reload() { fetchOccupancy().then(setOcc).catch(() => {}); } useEffect(() => { reload(); fetchSiteConfig() .then((c) => setCapInput(c.capacity == null ? "" : String(c.capacity))) .catch(() => {}); }, []); async function save() { setMsg(null); const raw = capInput.trim(); const capacity = raw === "" ? null : Math.round(Number(raw)); try { await setCapacity(capacity); reload(); setMsg("Capacity saved."); } catch (e) { setMsg((e as Error).message); } } return (
Occupancy:{" "} {occ == null ? ( "…" ) : ( <> {occ.count} {occ.capacity != null ? ` / ${occ.capacity}` : " (no capacity set)"} {occ.capacity != null && ( · {occ.free} free )} {occ.full && FULL}{" "} )} {canEdit && (
{" "} {msg && {msg}}
)}
); }