Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download web/src/apps/reports/AppView.tsx from fsanyoto/runloopable: direct link, hf CLI and curl.
- Browser
- Download file 4.22 kB
-
https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/web/src/apps/reports/AppView.tsx
- Command line
-
hf download hf://spaces/fsanyoto/runloopable/web/src/apps/reports/AppView.tsx
-
curl -L -o AppView.tsx https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/web/src/apps/reports/AppView.tsx
4.22 kB
| import { useCallback, useEffect, useMemo, useRef, useState } from "react"; | |
| import { ROWS_STALE_EVENT, TOAST_EVENT, signal } from "../../apiContract"; | |
| import { AppFrame, AppTableGrid, clearTopicRowsCache, dropTableRowsCache, | |
| fetchWorkspace } from "../../customer-grid/seam"; | |
| import type { AppTableHost, AppViewProps } from "../../customer-grid/seam"; | |
| import { LogoLoader } from "../../ui/LogoLoader"; | |
| import { fetchReports, reportErrorMessage, saveReportFile, startReportBuild, waitForReportFile } from "./reportsApi"; | |
| import type { Report } from "./reportsApi"; | |
| import ReportSettings from "./ReportSettings"; | |
| const DATABASE = "ut__reports"; | |
| const COLUMNS: AppTableHost["columns"] = { widths: { | |
| name: 240, description: 400, format: 88, download: 120, share: 88, settings: 104, | |
| } }; | |
| export const ownsFrame = true; | |
| type ReportsState = { status: "loading" } | { status: "error"; message: string } | |
| | { status: "ready"; reports: ReadonlyMap<number, Report>; isAdmin: boolean }; | |
| function reloadRows(): void { | |
| clearTopicRowsCache(`tables/${DATABASE}/rows`); | |
| dropTableRowsCache(DATABASE); | |
| signal(ROWS_STALE_EVENT); | |
| } | |
| export default function AppView({ application }: AppViewProps) { | |
| const [state, setState] = useState<ReportsState>({ status: "loading" }); | |
| const [editing, setEditing] = useState<Report | null>(null); | |
| const closeSettings = useCallback(() => setEditing(null), []); | |
| const requests = useRef<AbortController | null>(null); | |
| const inFlight = useRef(new Set<string>()); | |
| useEffect(() => { | |
| const controller = new AbortController(); | |
| requests.current = controller; | |
| const admin = fetchWorkspace(DATABASE, { fieldsOnly: true }).then( | |
| workspace => workspace?.viewer?.isAdmin === true, () => false); | |
| void Promise.all([fetchReports(controller.signal), admin]).then(([reports, isAdmin]) => { | |
| if (!controller.signal.aborted) setState({ status: "ready", isAdmin, | |
| reports: new Map(reports.map(report => [report.pid, report])) }); | |
| }).catch(failure => { | |
| if (!controller.signal.aborted) setState({ status: "error", message: reportErrorMessage(failure) }); | |
| }); | |
| return () => controller.abort(); | |
| }, []); | |
| const download = useCallback(async (report: Report) => { | |
| const controller = requests.current; | |
| if (!controller || controller.signal.aborted || inFlight.current.has(report.key)) return; | |
| inFlight.current.add(report.key); | |
| try { | |
| const job = await startReportBuild(report.key, controller.signal); | |
| reloadRows(); | |
| const file = await waitForReportFile(job, controller.signal); | |
| if (!controller.signal.aborted) saveReportFile(file); | |
| } catch (failure) { | |
| if (!controller.signal.aborted) signal(TOAST_EVENT, reportErrorMessage(failure)); | |
| } finally { | |
| inFlight.current.delete(report.key); | |
| if (!controller.signal.aborted) reloadRows(); | |
| } | |
| }, []); | |
| const cellRenderers = useMemo<AppTableHost["cellRenderers"]>(() => { | |
| if (state.status !== "ready") return {}; | |
| return { | |
| download: { button: true, activate: row => { | |
| const report = state.reports.get(row.pid); | |
| if (!report) return false; | |
| void download(report); | |
| } }, | |
| ...(state.isAdmin ? { share: { button: true, activate: (row) => { | |
| const report = state.reports.get(row.pid); | |
| if (!report) return false; | |
| signal("aios:share-open", { kind: "report", id: report.key, label: report.name }); | |
| } }, settings: { button: true, activate: row => { | |
| const report = state.reports.get(row.pid); | |
| if (!report) return false; | |
| setEditing(report); | |
| } } } : {}), | |
| }; | |
| }, [state, download]); | |
| return <AppFrame appName={application.name}> | |
| {state.status === "loading" ? <LogoLoader data-app-reason="loading" /> | |
| : state.status === "error" ? <p className="AppGrid-message" role="status">{state.message}</p> | |
| : state.reports.size === 0 ? <p className="AppGrid-message" role="status">No reports shared with you</p> | |
| : <AppTableGrid data={{ database: DATABASE, append: false }} columns={COLUMNS} cellRenderers={cellRenderers} />} | |
| {editing ? <ReportSettings key={editing.key} report={editing} onClose={closeSettings} /> : null} | |
| </AppFrame>; | |
| } | |