import { useEffect, useId, useRef, useState } from "react"; import { BodyPortal, useOverlayLayer } from "../customer-grid/OverlaySurface"; import { readRefreshHistory } from "./sheetApi"; import type { SheetRefreshEntry, SheetRefreshHistory } from "./sheetApi"; import "../styles/sdlc73/sheet.css"; function scopeLabel(scope: SheetRefreshEntry["scope"]): string { if ("workbook" in scope) return "Workbook"; if ("cells" in scope) return `${scope.cells.length} cells`; return ("cell" in scope ? scope.cell : scope.range).split("!").pop() || "Cells"; } function timeLabel(at: string): string { const date = new Date(at); return Number.isNaN(date.getTime()) ? "Time unavailable" : date.toLocaleString(); } function refreshLabel(entry: SheetRefreshEntry): string { if (entry.kind !== "auto") return entry.state === "held" ? "Held" : entry.state === "committed" ? "Committed" : "Attempted"; return entry.reason === "sync" ? "Automatic sync" : entry.reason === "rollover" ? "Automatic rollover" : "Automatic refresh"; } export default function RefreshHistory({ workbookId, revision, busy, onRetry }: { workbookId: string; revision: number; busy: boolean; onRetry: (id: string) => Promise; }) { const [open, setOpen] = useState(false); const [history, setHistory] = useState({ entries: [], lastGoodAt: null, sources: [] }); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); const [retrying, setRetrying] = useState(null); const [reload, setReload] = useState(0); const live = useRef(true); useEffect(() => { live.current = true; return () => { live.current = false; }; }, []); useEffect(() => { if (!open) return; setLoading(true); let active = true; let pending: AbortController | null = null; const read = async () => { if (pending) return; const controller = new AbortController(); pending = controller; const timeout = window.setTimeout(() => controller.abort(), 4000); try { const result = await readRefreshHistory(workbookId, controller.signal); if (!active) return; if (result.ok) { setHistory(result.value); setError(""); } else { setError(result.message); if (result.status === 403 || result.status === 404) { setHistory({ entries: [], lastGoodAt: null, sources: [] }); setOpen(false); } } } finally { if (active) setLoading(false); pending = null; window.clearTimeout(timeout); } }; void read(); const timer = window.setInterval(() => { void read(); }, 2000); return () => { active = false; window.clearInterval(timer); pending?.abort(); }; }, [workbookId, revision, open, reload]); const retry = async (id: string) => { if (retrying || busy) return; setRetrying(id); try { await onRetry(id); } catch { if (live.current) setError("Refresh could not finish."); } finally { if (live.current) { setRetrying(null); setReload(value => value + 1); } } }; return <> {open && { void retry(id); }} onClose={() => setOpen(false)} />} ; } function HistoryPanel({ history, error, loading, busy, onRetry, onClose }: { history: SheetRefreshHistory; error: string; loading: boolean; busy: boolean; onRetry: (id: string) => void; onClose: () => void; }) { const panelRef = useRef(null); const titleId = useId(); useOverlayLayer({ panelRef, onDismiss: onClose, dismissOnOutside: false, initialFocus: "[data-overlay-autofocus]", restoreFocus: true, trapFocus: true }); return

Refresh history

Last good refresh: {history.lastGoodAt ? : "None yet"}

{error &&

{error}

}

Sources

{loading &&

Loading sources

} {!loading && !error && history.sources.length === 0 &&

No bound databases

} {!loading && !error &&
    {history.sources.map(source =>
  • {source.label} {source.connector || "No connector"} {" · "}{source.cadence || "Cadence unavailable"}{" · "}Last sync: {source.lastSyncAt ? : "Unavailable"}
  • )}
}
{!error && history.entries.length === 0 &&

No refreshes yet

}
    {history.entries.map(entry =>
  1. {refreshLabel(entry)} {entry.canRetry && }
    {entry.kind === "auto" ? entry.state === "committed" ? "Committed" : entry.state === "held" ? "Held" : "Attempted" : entry.byName || entry.by} · {scopeLabel(entry.scope)} {entry.changedCells} {entry.changedCells === 1 ? "cell" : "cells"} changed · {entry.state === "held" && {entry.reason === "store_catching_up" ? "Database updating" : entry.kind === "auto" ? "Refresh incomplete" : "Source unavailable"}}
  2. )}
; }