File size: 3,745 Bytes
1ad1eea | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import { useEffect, useState } from "react";
import { call } from "../query/queryApi";
import { worklistFigure } from "./WorklistTable";
import { ARTIFACT_UNAVAILABLE, worklistText } from "./worklistApi";
import "./artifacts.css";
interface Snapshot {
title: string; frozen_at: string; note: string | null;
columns: Array<{ key: string; label: string }>;
rows: Array<Record<string, unknown> | unknown[]>;
row_count: number; shown: number;
totals: { count: number; sum: Record<string, number | null> };
}
function readSnapshot(raw: unknown): Snapshot {
const row = (raw as { snapshot?: Snapshot } | null)?.snapshot;
if (!row || typeof row.title !== "string" || typeof row.frozen_at !== "string"
|| !Array.isArray(row.columns) || !Array.isArray(row.rows)
|| !Number.isSafeInteger(row.row_count) || !row.totals || typeof row.totals.sum !== "object"
|| row.columns.some(column => typeof column.key !== "string" || typeof column.label !== "string")) {
throw new Error("Invalid snapshot.");
}
return row;
}
export default function SnapshotTable({ id }: { id: string }) {
const [snapshot, setSnapshot] = useState<Snapshot | null>(null);
const [problem, setProblem] = useState("");
const [retry, setRetry] = useState(0);
useEffect(() => {
const controller = new AbortController();
setSnapshot(null); setProblem("");
void call(`/artifacts/${encodeURIComponent(id)}/snapshot`, { method: "GET", signal: controller.signal }, readSnapshot)
.then(result => {
if (controller.signal.aborted) return;
if (result.ok) setSnapshot(result.value);
else setProblem([403, 404, 410].includes(result.status) ? ARTIFACT_UNAVAILABLE : result.message);
}).catch(() => { if (!controller.signal.aborted) setProblem("This snapshot could not be read."); });
return () => controller.abort();
}, [id, retry]);
return <section className="ar-worklist" aria-label="Frozen snapshot" aria-busy={!snapshot && !problem}>
{snapshot ? <>
<p className="ar-meta">Frozen <time dateTime={snapshot.frozen_at}>{new Date(snapshot.frozen_at).toLocaleString()}</time></p>
<p className="ar-worklist-count">{snapshot.shown.toLocaleString()} of {snapshot.row_count.toLocaleString()} rows</p>
{snapshot.note ? <p className="ar-meta">{worklistText(snapshot.note)}</p> : null}
<dl className="ar-snapshot-totals" aria-label="Snapshot totals">
<div><dt>Rows</dt><dd>{worklistFigure(snapshot.totals.count)}</dd></div>
{Object.entries(snapshot.totals.sum).map(([key, value]) => <div key={key}>
<dt>{worklistText(snapshot.columns.find(column => column.key === key)?.label ?? key)}</dt>
<dd>{worklistFigure(value)}</dd></div>)}
</dl>
{snapshot.rows.length ? <div className="ar-worklist-scroll" tabIndex={0} role="region" aria-label="Snapshot table">
<table className="ar-worklist-table" aria-label={worklistText(snapshot.title)}>
<thead><tr>{snapshot.columns.map(column => <th scope="col" key={column.key}>{worklistText(column.label)}</th>)}</tr></thead>
<tbody>{snapshot.rows.map((row, rowIndex) => <tr key={rowIndex}>{snapshot.columns.map((column, index) =>
<td key={column.key}>{worklistFigure(Array.isArray(row) ? row[index] : row[column.key])}</td>)}</tr>)}</tbody>
</table></div> : <p className="ar-worklist-empty">Nothing found.</p>}
</> : problem ? <p className="ar-worklist-problem" role="status">{worklistText(problem)}{" "}
<button className="ar-retry" type="button" onClick={() => setRetry(value => value + 1)}>Try again</button></p>
: <div className="ar-worklist-loading" role="status" aria-label="Loading snapshot"><span /><span /></div>}
</section>;
}
|