Files
charging_station/frontend/src/pages/History.tsx
T
julian 069d544bb5 feat: private session history, own-permissions view, plug-gated start button
- non-admins now see ONLY their own sessions in /api/sessions (own app
  starts + own linked cards); gun scopes grant control, not visibility;
  admins see everything
- Account page shows the logged-in user's permissions in plain words
  (Administrator badge, or the guns/relays they control), built solely
  from data already visible to them
- Start button arms only when the gun reports Preparing (cable in the
  car); idle unplugged gun shows an explanatory hint instead
- keep owner's app rename to 'Art IoT'
- smoke test: 59 checks (gun-scoped user sees no foreign sessions)
2026-07-12 09:04:06 +02:00

91 lines
3.3 KiB
TypeScript

import { isAdmin } from "../api";
import { useSessions, useStatus } from "../queries";
import { useApp } from "../store";
import type { Session } from "../types";
const fmtKwh = (wh: number | null) => (wh == null ? "—" : (wh / 1000).toFixed(2));
const fmtTime = (iso: string | null) =>
iso ? new Date(iso).toLocaleString([], { dateStyle: "medium", timeStyle: "short" }) : "—";
function fmtDuration(a: string, b: string | null): string {
if (!b) return "";
const mins = Math.max(0, Math.round((new Date(b).getTime() - new Date(a).getTime()) / 60000));
return mins >= 60 ? `${Math.floor(mins / 60)} h ${String(mins % 60).padStart(2, "0")} min` : `${mins} min`;
}
function MethodChip({ method }: { method: string | null }) {
if (method === "card") return <span className="chip chip--card">Card</span>;
if (method === "app") return <span className="chip chip--app">In-app</span>;
return <span className="chip">{method ?? "—"}</span>;
}
function gunLabel(s: Session, chargers: Record<string, { label?: string; guns?: Record<string, string> }>) {
const meta = chargers[s.cp];
return meta?.guns?.[String(s.connector)] ?? `${s.cp} · gun ${s.connector}`;
}
export default function History() {
const me = useApp((s) => s.me)!;
const admin = isAdmin(me.scopes);
const { data: sessions, isLoading } = useSessions();
const { data: status } = useStatus();
if (isLoading) return <p className="empty">Loading history…</p>;
if (!sessions?.length)
return (
<p className="empty">
{admin
? "No charging sessions yet. The first card tap or in-app start will appear here with who, when, how, and how many kWh."
: "No charging sessions of yours yet. Sessions you start — by card or in-app — appear here."}
</p>
);
const totalKwh = sessions.reduce((acc, s) => acc + (s.wh ?? 0), 0) / 1000;
return (
<section className="panel">
<header className="panel__head">
<h2>{admin ? "All charging sessions" : "Your charging sessions"}</h2>
<span className="panel__meta">
{sessions.length} session{sessions.length === 1 ? "" : "s"} ·{" "}
<strong className="mono">{totalKwh.toFixed(1)} kWh</strong> total
</span>
</header>
<div className="table-wrap">
<table className="table">
<thead>
<tr>
<th>Who</th>
<th>Via</th>
<th>Gun</th>
<th>Started</th>
<th>Duration</th>
<th className="num">kWh</th>
</tr>
</thead>
<tbody>
{sessions.map((s) => (
<tr key={s.tx}>
<td className="strong">{s.who || s.id_tag}</td>
<td>
<MethodChip method={s.method} />
</td>
<td>{gunLabel(s, status?.chargers ?? {})}</td>
<td className="mono">{fmtTime(s.started)}</td>
<td>
{s.ended ? (
fmtDuration(s.started, s.ended)
) : (
<span className="chip chip--live">charging</span>
)}
</td>
<td className="num mono strong">{fmtKwh(s.wh)}</td>
</tr>
))}
</tbody>
</table>
</div>
</section>
);
}