runloopable / web /src /account /TurnAuditTable.tsx
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
89af3b2 verified
Raw History Blame Contribute Delete
5.88 kB
import { useCallback, useEffect, useRef, useState } from "react";
import { fmt } from "../ui/fmt";
import { loadTurnAudit } from "./turnAuditApi";
import type { TurnAuditRow, TurnAuditScope } from "./turnAuditApi";
export interface TurnAuditState {
phase: "loading" | "ready" | "failed";
turns: TurnAuditRow[];
nextOffset: number | null;
moreBusy: boolean;
message: string;
}
const INITIAL: TurnAuditState = {
phase: "loading", turns: [], nextOffset: null, moreBusy: false, message: "",
};
export function newestTurns(turns: TurnAuditRow[]): TurnAuditRow[] {
return [...turns].sort((a, b) => Date.parse(b.ts) - Date.parse(a.ts));
}
function label(value: string): string {
const words = value.replace(/[_-]+/g, " ").trim();
if (value === "whatsapp") return "WhatsApp";
return words.charAt(0).toUpperCase() + words.slice(1);
}
export function TurnAuditView({ state, onMore, onRetry }: {
state: TurnAuditState;
onMore: () => void;
onRetry: () => void;
}) {
return (
<section className="acct-turns" aria-label="Turns" aria-busy={state.phase === "loading" || state.moreBusy}>
<h2 className="acct-section">Turns</h2>
{state.phase === "loading" ? (
<div className="acct-turns-loading" role="status" aria-label="Loading turns">
<span className="acct-turns-placeholder" />
<span className="acct-turns-placeholder" />
<span className="acct-turns-placeholder" />
</div>
) : state.phase === "failed" ? (
<div className="acct-turns-feedback" role="alert">
<p className="acct-turns-note">{state.message}</p>
<button className="acct-turns-button" type="button" onClick={onRetry}>Try again</button>
</div>
) : (
<>
{state.turns.length === 0 ? <p className="acct-turns-note">No turns this week.</p> : (
<div className="acct-turns-scroll" role="region" aria-label="Turn audit table" tabIndex={0}>
<table className="acct-turns-table">
<thead><tr>
<th scope="col">Time</th><th scope="col">User</th><th scope="col">Door</th>
<th scope="col">Model</th><th scope="col" className="acct-turns-number">Tokens in</th>
<th scope="col" className="acct-turns-number">Tokens out</th>
<th scope="col" className="acct-turns-number">Cost</th><th scope="col">Tools</th>
<th scope="col" className="acct-turns-number">Duration</th><th scope="col">Outcome</th>
</tr></thead>
<tbody>{newestTurns(state.turns).map((turn, index) => (
<tr key={`${turn.ts}:${turn.user}:${turn.door}:${index}`}>
<td><time dateTime={turn.ts} title={turn.ts}>{new Date(turn.ts).toLocaleString(undefined, {
month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit",
})}</time></td>
<td>{turn.user}</td><td>{label(turn.door)}</td><td>{turn.model ?? "No model"}</td>
<td className="acct-turns-number">{turn.tokensIn === null ? "Not reported" : fmt.int(turn.tokensIn)}</td>
<td className="acct-turns-number">{turn.tokensOut === null ? "Not reported" : fmt.int(turn.tokensOut)}</td>
<td className="acct-turns-number">{turn.cost === null ? "No price on file" :
turn.cost.toLocaleString(undefined, { style: "currency", currency: "USD",
minimumFractionDigits: 2, maximumFractionDigits: 6 })}</td>
<td className="acct-turns-tools">{turn.tools.length ? turn.tools.join(", ") : "None"}</td>
<td className="acct-turns-number">{fmt.num(turn.durationMs / 1000, 1)} s</td>
<td><span className={"acct-turns-outcome" +
(turn.outcome === "failed" ? " acct-turns-outcome--failed" :
turn.outcome === "stopped" || turn.outcome === "paused" ? " acct-turns-outcome--stopped" : "")}
>{label(turn.outcome)}</span></td>
</tr>
))}</tbody>
</table>
</div>
)}
{state.message ? <p className="acct-turns-note" role="alert">{state.message}</p> : null}
{state.nextOffset !== null ? (
<button className="acct-turns-button" type="button" onClick={onMore} disabled={state.moreBusy}>
{state.moreBusy ? "Loading turns" : "Show more"}
</button>
) : null}
</>
)}
</section>
);
}
export default function TurnAuditTable({ scope }: { scope: TurnAuditScope }) {
const [state, setState] = useState<TurnAuditState>(INITIAL);
const generation = useRef(0);
const busy = useRef(false);
const read = useCallback((offset: number, more = false) => {
if (more && busy.current) return;
busy.current = true;
const mine = ++generation.current;
setState((previous) => more ? { ...previous, moreBusy: true, message: "" } : INITIAL);
void loadTurnAudit(scope, offset).then((result) => {
if (generation.current !== mine) return;
busy.current = false;
setState((previous) => result.ok ? {
phase: "ready", turns: more ? [...previous.turns, ...result.value.turns] : result.value.turns,
nextOffset: result.value.nextOffset, moreBusy: false, message: "",
} : more ? { ...previous, moreBusy: false, message: result.message } : {
...INITIAL, phase: "failed", message: result.message,
});
});
}, [scope]);
useEffect(() => {
read(0);
return () => { generation.current += 1; busy.current = false; };
}, [read]);
return <TurnAuditView state={state} onRetry={() => read(0)}
onMore={() => { if (state.nextOffset !== null) read(state.nextOffset, true); }} />;
}