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 Card; if (method === "app") return In-app; return {method ?? "—"}; } function gunLabel(s: Session, chargers: Record }>) { 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

Loading history…

; if (!sessions?.length) return (

{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."}

); const totalKwh = sessions.reduce((acc, s) => acc + (s.wh ?? 0), 0) / 1000; return (

{admin ? "All charging sessions" : "Your charging sessions"}

{sessions.length} session{sessions.length === 1 ? "" : "s"} ·{" "} {totalKwh.toFixed(1)} kWh total
{sessions.map((s) => ( ))}
Who Via Gun Started Duration kWh
{s.who || s.id_tag} {gunLabel(s, status?.chargers ?? {})} {fmtTime(s.started)} {s.ended ? ( fmtDuration(s.started, s.ended) ) : ( charging )} {fmtKwh(s.wh)}
); }