import { api, scopeAllowed } from "../api"; import { Lamp, statusTone } from "../components/Lamp"; import { useAction, useLiveRelays, useStatus } from "../queries"; import { useApp } from "../store"; import type { ActiveTx, Status } from "../types"; function fmtSince(iso: string): string { const mins = Math.max(0, Math.round((Date.now() - new Date(iso).getTime()) / 60000)); return mins >= 60 ? `${Math.floor(mins / 60)} h ${mins % 60} min` : `${mins} min`; } interface Gun { cp: string; connector: number; label: string; chargerLabel: string; ocppStatus: string; // OCPP status, or "Offline" / "NoStatus" synthesized here tx: (ActiveTx & { txId: string }) | null; } /** Every gun the system knows about is ALWAYS shown: the chargers registry * defines the cards, live connector reports and transactions overlay state. */ function gunsFrom(status: Status): Gun[] { const guns = new Map(); for (const [cp, meta] of Object.entries(status.chargers)) { const online = cp in status.connected; for (const [conn, label] of Object.entries(meta.guns ?? {})) { guns.set(`${cp}/${conn}`, { cp, connector: Number(conn), label, chargerLabel: meta.label ?? cp, ocppStatus: online ? "NoStatus" : "Offline", tx: null, }); } } for (const [key, ocppStatus] of Object.entries(status.connectors)) { const cp = key.slice(0, key.lastIndexOf("/")); const connector = Number(key.slice(key.lastIndexOf("/") + 1)); if (connector <= 0) continue; // connector 0 = the whole charge point const existing = guns.get(`${cp}/${connector}`); if (existing) existing.ocppStatus = ocppStatus; else guns.set(`${cp}/${connector}`, { cp, connector, label: `Gun ${connector}`, chargerLabel: status.chargers[cp]?.label ?? cp, ocppStatus, tx: null, }); } for (const [txId, tx] of Object.entries(status.active_transactions)) { const gun = guns.get(`${tx.cp}/${tx.connector}`); if (gun) gun.tx = { ...tx, txId }; } return [...guns.values()].sort( (a, b) => a.cp.localeCompare(b.cp) || a.connector - b.connector, ); } /** Chip text + tone per the owner's state language. */ function gunState(gun: Gun): { label: string; cls: string } { if (gun.tx) return { label: "Charging", cls: "charging" }; switch (gun.ocppStatus) { case "Charging": return { label: "Charging", cls: "charging" }; case "Preparing": return { label: "Plugged in", cls: "plugged" }; case "SuspendedEV": case "SuspendedEVSE": return { label: "Paused", cls: "plugged" }; case "Finishing": return { label: "Finishing", cls: "plugged" }; case "Available": return { label: "Idle", cls: "idle" }; case "Offline": return { label: "Offline", cls: "offline" }; case "NoStatus": return { label: "No status yet", cls: "offline" }; case "Faulted": return { label: "Fault", cls: "faulted" }; default: return { label: gun.ocppStatus, cls: "idle" }; } } function GunBay({ gun, scopes }: { gun: Gun; scopes: string[] }) { const toast = useApp((s) => s.toast); const may = scopeAllowed(scopes, `charger:${gun.cp}:${gun.connector}`); // Start arms ONLY with a cable in the car (Preparing / pulsing blue) — // an idle, unplugged gun keeps the button disabled (owner, 2026-07-12) const ready = !gun.tx && gun.ocppStatus === "Preparing"; const start = useAction( () => api.post<{ remote_start: string }>( `/charger/start?connector=${gun.connector}&cp=${encodeURIComponent(gun.cp)}`, ), [["status"], ["sessions"]], (r) => toast(`Start command: ${r.remote_start}`), ); const stop = useAction( () => api.post<{ remote_stop: string }>(`/charger/stop/${gun.tx!.txId}`), [["status"], ["sessions"]], (r) => toast(`Stop command: ${r.remote_stop}`), ); const state = gunState(gun); return (
{gun.chargerLabel}

{gun.label}

{state.label} {gun.ocppStatus === "Preparing" && !gun.tx && ( cable in the socket — ready to start )}
{gun.tx && (
By
{gun.tx.who || gun.tx.id_tag}
Via
{gun.tx.method === "app" ? "In-app" : gun.tx.method === "card" ? "Card" : gun.tx.method}
Running
{fmtSince(gun.tx.started)}
)}
{gun.tx ? ( ) : ( )} {!may && no permission for this gun} {may && !gun.tx && !ready && gun.ocppStatus === "Available" && ( plug the cable into the car to enable )}
); } function Relays({ status, scopes }: { status: Status; scopes: string[] }) { const toast = useApp((s) => s.toast); const live = useLiveRelays(); const names = Object.keys(status.relays); const toggle = useAction( (name: string) => api.post<{ relay: string; on: boolean }>(`/relays/${encodeURIComponent(name)}/toggle`), [["relays-live"]], (r) => toast(`${r.relay}: ${r.on ? "on" : "off"}`), ); if (!names.length) return null; return (

Relays

); } export default function Dashboard() { const me = useApp((s) => s.me)!; const { data: status, isLoading, error } = useStatus(); if (isLoading) return

Reading charger status…

; if (error || !status) return

Status unavailable: {(error as Error)?.message}

; const guns = gunsFrom(status); const offline = Object.keys(status.connected).length === 0; return ( <> {offline && (

Charger offline

Waiting for the charge point to dial in — controls enable as soon as it connects.

)} {!guns.length && !offline && (

Connected, waiting for the first gun status report…

)}
{guns.map((g) => ( ))}
); }