feat: React control panel; pending-card review workflow; auth-method audit
Frontend rewrite (frontend/, replacing backend/static/): - Vite + TypeScript + React 19, React Router 7, zustand, TanStack Query 5 - dev: vite proxy /api -> :9000; prod: npm run build -> dist/, served by the backend with SPA fallback - pages: Control (gun bays w/ scope+readiness-gated start/stop, relays), History (who/via/gun/when/duration/kWh audit), Cards (pending review, enrolled, rejected), Users (permission checkbox matrix) - self-contained: fonts bundled, no CDN; light/dark; reduced-motion safe Backend: - learn mode removed: unknown cards are always refused and queued in pending_cards; admin approves (name + owner + gun restrictions in one step) or rejects (denied_cards, silent, un-rejectable); pending count rides /api/status as the notification signal - sessions record the auth method (card tap vs in-app button) for audit - GET /api/permissions: grantable-permission catalog driving the UI matrix - SPA static serving with path-traversal guard; 503 hint when not built Smoke test: 54 checks incl. approve/reject round-trips over a fake charge point and method audit assertions.
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
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;
|
||||
tx: (ActiveTx & { txId: string }) | null;
|
||||
}
|
||||
|
||||
function gunsFrom(status: Status): Gun[] {
|
||||
const txByGun = new Map<string, ActiveTx & { txId: string }>();
|
||||
for (const [txId, tx] of Object.entries(status.active_transactions))
|
||||
txByGun.set(`${tx.cp}/${tx.connector}`, { ...tx, txId });
|
||||
|
||||
return Object.entries(status.connectors)
|
||||
.map(([key, ocppStatus]) => {
|
||||
const [cp, conn] = [key.slice(0, key.lastIndexOf("/")), key.slice(key.lastIndexOf("/") + 1)];
|
||||
return { cp, connector: Number(conn), ocppStatus };
|
||||
})
|
||||
.filter((g) => g.connector > 0)
|
||||
.sort((a, b) => a.cp.localeCompare(b.cp) || a.connector - b.connector)
|
||||
.map((g) => {
|
||||
const meta = status.chargers[g.cp] ?? {};
|
||||
return {
|
||||
...g,
|
||||
label: meta.guns?.[String(g.connector)] ?? `Gun ${g.connector}`,
|
||||
chargerLabel: meta.label ?? g.cp,
|
||||
tx: txByGun.get(`${g.cp}/${g.connector}`) ?? null,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function GunBay({ gun, scopes }: { gun: Gun; scopes: string[] }) {
|
||||
const toast = useApp((s) => s.toast);
|
||||
const may = scopeAllowed(scopes, `charger:${gun.cp}:${gun.connector}`);
|
||||
const ready = !gun.tx && (gun.ocppStatus === "Preparing" || gun.ocppStatus === "Available");
|
||||
|
||||
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}`),
|
||||
);
|
||||
|
||||
return (
|
||||
<article className="bay">
|
||||
<header className="bay__head">
|
||||
<span className="bay__eyebrow">{gun.chargerLabel}</span>
|
||||
<Lamp tone={statusTone(gun.ocppStatus, !!gun.tx)} title={gun.ocppStatus} />
|
||||
</header>
|
||||
<h2 className="bay__title">{gun.label}</h2>
|
||||
<div className="bay__status">
|
||||
<span className={`chip chip--${gun.tx ? "live" : gun.ocppStatus.toLowerCase()}`}>
|
||||
{gun.tx ? "Charging" : gun.ocppStatus}
|
||||
</span>
|
||||
{gun.ocppStatus === "Preparing" && !gun.tx && (
|
||||
<span className="bay__hint">cable plugged — ready to start</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{gun.tx && (
|
||||
<dl className="bay__telemetry">
|
||||
<div>
|
||||
<dt>By</dt>
|
||||
<dd>{gun.tx.who || gun.tx.id_tag}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>Via</dt>
|
||||
<dd>{gun.tx.method === "app" ? "In-app" : gun.tx.method === "card" ? "Card" : gun.tx.method}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>Running</dt>
|
||||
<dd>{fmtSince(gun.tx.started)}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
)}
|
||||
|
||||
<footer className="bay__actions">
|
||||
{gun.tx ? (
|
||||
<button
|
||||
className="btn btn--danger"
|
||||
disabled={!may || stop.isPending}
|
||||
onClick={() => stop.mutate()}
|
||||
>
|
||||
{stop.isPending ? "Stopping…" : "Stop charging"}
|
||||
</button>
|
||||
) : (
|
||||
<button
|
||||
className="btn btn--primary"
|
||||
disabled={!may || !ready || start.isPending}
|
||||
onClick={() => start.mutate()}
|
||||
>
|
||||
{start.isPending ? "Starting…" : "Start charging"}
|
||||
</button>
|
||||
)}
|
||||
{!may && <span className="bay__hint">no permission for this gun</span>}
|
||||
</footer>
|
||||
</article>
|
||||
);
|
||||
}
|
||||
|
||||
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 (
|
||||
<section className="panel">
|
||||
<header className="panel__head">
|
||||
<h2>Relays</h2>
|
||||
<button
|
||||
className="btn btn--ghost btn--sm"
|
||||
onClick={() => live.refetch()}
|
||||
disabled={live.isFetching}
|
||||
>
|
||||
{live.isFetching ? "Reading…" : "Read states"}
|
||||
</button>
|
||||
</header>
|
||||
<ul className="relay-list">
|
||||
{names.map((name) => {
|
||||
const state = live.data?.[name];
|
||||
const on = state && "on" in state ? state.on : undefined;
|
||||
return (
|
||||
<li key={name} className="relay">
|
||||
<Lamp tone={on === undefined ? "off" : on ? "green" : "off"} />
|
||||
<span className="relay__label">{status.relays[name].label}</span>
|
||||
<span className="relay__state">
|
||||
{state?.error ? "unreachable" : on === undefined ? "—" : on ? "on" : "off"}
|
||||
</span>
|
||||
<button
|
||||
className="btn btn--ghost btn--sm"
|
||||
disabled={!scopeAllowed(scopes, `relay:${name}`) || toggle.isPending}
|
||||
onClick={() => toggle.mutate(name)}
|
||||
>
|
||||
Toggle
|
||||
</button>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
export default function Dashboard() {
|
||||
const me = useApp((s) => s.me)!;
|
||||
const { data: status, isLoading, error } = useStatus();
|
||||
|
||||
if (isLoading) return <p className="empty">Reading charger status…</p>;
|
||||
if (error || !status) return <p className="empty">Status unavailable: {(error as Error)?.message}</p>;
|
||||
|
||||
const guns = gunsFrom(status);
|
||||
const offline = Object.keys(status.connected).length === 0;
|
||||
|
||||
return (
|
||||
<>
|
||||
{offline && (
|
||||
<div className="panel panel--offline">
|
||||
<Lamp tone="red" />
|
||||
<div>
|
||||
<h2>Charger offline</h2>
|
||||
<p>Waiting for the charge point to dial in. Guns appear here as soon as it connects.</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<div className="bays">
|
||||
{guns.map((g) => (
|
||||
<GunBay key={`${g.cp}/${g.connector}`} gun={g} scopes={me.scopes} />
|
||||
))}
|
||||
</div>
|
||||
<Relays status={status} scopes={me.scopes} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user