diff --git a/windows/src/App.tsx b/windows/src/App.tsx index a0963d18..589b78a4 100644 --- a/windows/src/App.tsx +++ b/windows/src/App.tsx @@ -36,6 +36,7 @@ import { ScopeControl, type Scope } from './components/ScopeControl' import type { DaySelection, Period } from './components/PeriodTabs' import { FooterBar } from './components/FooterBar' import { ErrorToast } from './components/ErrorToast' +import { FetchErrorOverlay } from './components/FetchErrorOverlay' import { Header } from './components/Header' import { applyAccent, savedAccent, type AccentPreset } from './lib/accent' import { SettingsPanel, type SettingsSection, type ThemeChoice } from './components/SettingsPanel' @@ -62,6 +63,17 @@ const REFRESH_ACTIVE_MS = 60_000 const REFRESH_IDLE_MS = 120_000 const STALE_MS = 60_000 +/// Stuck-load recovery, from the mac BurnLoadingOverlay task: while the popover has +/// nothing to show, retry the fetch on a doubling delay and then give up with a message +/// rather than spinning forever. The first delay is longer than a healthy CLI run. +const RECOVERY_FIRST_MS = 8_000 +const RECOVERY_MAX_MS = 60_000 +const RECOVERY_ATTEMPTS = 6 +/// A fetch older than the Rust side's own 60 s timeout is an orphan, not a slow run: its +/// in-flight mark can be cleared. A younger one is left alone so recovery never kills a +/// fetch that is still going to answer. +const FLIGHT_WATCHDOG_MS = 65_000 + type FetchOptions = { includeOptimize: boolean showOverlay: boolean @@ -136,6 +148,7 @@ export function App() { } payloadCache.set(key, json) if (isSelected()) { + setError(null) setPayload(json) // "updated Xs ago" describes what the user is looking at, so only a fetch of the // visible key may stamp it - a background today/all tick must not. @@ -207,6 +220,8 @@ export function App() { useEffect(() => { const key = selection.current const cached = payloadCache.get(key) + // The error describes the selection that failed, so it goes with it. + setError(null) setPayload(cached) if (!cliReady) return if (!cached) { @@ -252,6 +267,34 @@ export function App() { } useEffect(readDailyBudget, []) + // Nothing on screen and nothing coming: retry on a doubling delay, then say so. A + // fetch torn down across sleep can leave its in-flight mark behind, which would make + // every retry bail on the guard, so a mark older than the watchdog is cleared first. + const coldLoading = overlay && payload === null && error === null + useEffect(() => { + if (!coldLoading || !cliReady) return + let attempt = 0 + let delay = RECOVERY_FIRST_MS + let timer = 0 + const tick = () => { + attempt += 1 + const key = selection.current + if (payloadCache.flightAge(key) > FLIGHT_WATCHDOG_MS) payloadCache.clearInFlight(key) + fetchKey(key, { includeOptimize: false, showOverlay: true }) + if (attempt >= RECOVERY_ATTEMPTS) { + setError(`Could not load ${label}. Check that the codeburn CLI is installed and working.`) + return + } + delay = Math.min(delay * 2, RECOVERY_MAX_MS) + timer = window.setTimeout(tick, delay) + } + timer = window.setTimeout(tick, delay) + return () => window.clearTimeout(timer) + // `label` only decorates the give-up message; re-arming the ladder on a relabel + // would restart the clock for no reason. + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [coldLoading, cliReady, fetchKey]) + // The quota store polls only while something is watching it, so it starts here and stops // with the page. A manual Refresh asks it for a fresh answer too. useEffect(() => subscribeQuota(setQuota), []) @@ -391,6 +434,8 @@ export function App() { && (payload.current?.calls ?? 0) === 0 && (payload.current?.sessions ?? 0) === 0 && (payload.history?.daily?.length ?? 0) === 0 + const label = daySelectionLabel(days) ?? PERIOD_LABELS[period] + const footnote = [version ? `CodeBurn v${version}` : 'CodeBurn', lastUpdated ? `updated ${relativePast(lastUpdated)}` : null] .filter(Boolean) .join(' · ') @@ -433,7 +478,7 @@ export function App() { )} - {overlay && } + {/* The overlay only takes over on a cold cache, so a failed background + refresh leaves the numbers that are already on screen alone. */} + {payload === null && error !== null ? ( + { + setError(null) + refreshAll({ includeOptimize: false, showOverlay: true }) + }} + /> + ) : overlay ? ( + + ) : null} )} @@ -524,7 +582,10 @@ export function App() { - {error && setError(null)} />} + {/* With something already on screen the failure is an aside, not a wall. */} + {error !== null && payload !== null && ( + setError(null)} /> + )} ) } diff --git a/windows/src/components/FetchErrorOverlay.tsx b/windows/src/components/FetchErrorOverlay.tsx new file mode 100644 index 00000000..93f89bdc --- /dev/null +++ b/windows/src/components/FetchErrorOverlay.tsx @@ -0,0 +1,29 @@ +import { WarningIcon } from './Icons' + +/// Port of FetchErrorOverlay in mac/.../Views/MenuBarContent.swift. Shown in place of the +/// loading overlay when the fetch failed and there is nothing cached to fall back on, so a +/// failed CLI run reads as a failure with a way out rather than an endless spinner. + +const MAX_MESSAGE = 240 + +type Props = { + message: string + periodLabel: string + onRetry: () => void +} + +export function FetchErrorOverlay({ message, periodLabel, onRetry }: Props) { + const trimmed = message.trim() + const shown = trimmed.length <= MAX_MESSAGE ? trimmed : `${trimmed.slice(0, MAX_MESSAGE)}…` + + return ( +
+
+ +
Couldn't load {periodLabel}
+
{shown}
+ +
+
+ ) +} diff --git a/windows/src/lib/cache.ts b/windows/src/lib/cache.ts index 178ed642..5d3e207a 100644 --- a/windows/src/lib/cache.ts +++ b/windows/src/lib/cache.ts @@ -30,7 +30,7 @@ interface CacheEntry { export class PayloadCache { private store = new Map>() - private flights = new Set() + private flights = new Map() get(selection: Selection): T | null { return this.store.get(selectionKey(selection))?.data ?? null @@ -50,8 +50,15 @@ export class PayloadCache { return this.flights.has(selectionKey(selection)) } + /// Milliseconds since the in-flight fetch started, or 0 when there is none. Stuck-load + /// recovery uses this to tell an orphaned entry from a fetch that is simply slow. + flightAge(selection: Selection): number { + const started = this.flights.get(selectionKey(selection)) + return started === undefined ? 0 : Date.now() - started + } + markInFlight(selection: Selection): void { - this.flights.add(selectionKey(selection)) + this.flights.set(selectionKey(selection), Date.now()) } clearInFlight(selection: Selection): void { diff --git a/windows/src/styles.css b/windows/src/styles.css index 6089b68e..ba7235e6 100644 --- a/windows/src/styles.css +++ b/windows/src/styles.css @@ -203,6 +203,7 @@ code { font-family: var(--font-mono); } this drop shadow follows the card radius instead of the square window frame. That is why the window has no DWM shadow and no DWM rounding: both trace a rectangle. */ .popover { + position: relative; display: flex; flex-direction: column; margin: var(--popover-gutter); @@ -1762,6 +1763,41 @@ code { font-family: var(--font-mono); } .loading-content { display: flex; flex-direction: column; align-items: center; gap: 14px; } .loading-text { font-size: 11.5px; font-weight: 500; color: var(--label-2); } +/* Fetch error: the same sheet as the loading overlay, because it replaces it. */ +.fetch-error-overlay { + position: absolute; + inset: 0; + z-index: 30; + display: flex; + align-items: center; + justify-content: center; + padding: 0 20px; + background: var(--overlay-material); + backdrop-filter: blur(18px) saturate(160%); + -webkit-backdrop-filter: blur(18px) saturate(160%); + animation: fadeIn 200ms ease-in-out; +} +.fetch-error-content { + display: flex; + flex-direction: column; + align-items: center; + gap: 12px; + max-width: 280px; + text-align: center; +} +.fetch-error-icon { color: var(--brand-accent); } +.fetch-error-title { font-size: 12.5px; font-weight: 600; color: var(--label); } +.fetch-error-message { + font-size: 10.5px; + line-height: 1.4; + color: var(--label-2); + user-select: text; + display: -webkit-box; + -webkit-line-clamp: 3; + -webkit-box-orient: vertical; + overflow: hidden; +} + .burn-flame { position: relative; } .burn-flame svg { position: absolute; inset: 0; } .burn-flame-glow path { fill: var(--ember-glow); }