Download web/src/artifacts/SnapshotTable.tsx from fsanyoto/loopable: direct link, hf CLI and curl.
- Browser
- Download file 3.75 kB
-
https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/web/src/artifacts/SnapshotTable.tsx
- Command line
-
hf download hf://spaces/fsanyoto/loopable/web/src/artifacts/SnapshotTable.tsx
-
curl -L -o SnapshotTable.tsx https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/web/src/artifacts/SnapshotTable.tsx
3.75 kB
| 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>; | |
| } | |