mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-12-03 03:29:51 +00:00
- One 30s refresh + manual ⌘R; footer shows real last-fetch time; getOverview fetched once (no double-poll).
- Keyboard nav: ⌘1–⌘5/⌘, switch sections, ⌘R refresh.
- Verified period/provider re-polls all sections.
- Shared CliErrorPanel: amber permission ("Full Disk Access") vs red error, used by every section.
- DRY: shared lib/format.ts (USD), lib/period.ts (Overview migrated to it), lib/modelSeries.ts (unified model→series).
- Settings review-minor tests (!paired filter, empty/not-found states) + App.test.tsx.
- app/README.md incl. the approved M2 delivery model (self-contained, bundles engine, Electron auto-updater).
- 76 tests, typecheck clean. Live GUI smoke (npm --prefix app run dev) still pending a display.
Implemented by Codex gpt-5.5 (high); committed by Fable (git blocked in Codex sandbox).
70 lines
2.4 KiB
TypeScript
70 lines
2.4 KiB
TypeScript
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
|
|
import { normalizeCliError } from '../lib/ipc'
|
|
import type { CliError } from '../lib/types'
|
|
|
|
export type Polled<T> = {
|
|
data: T | null
|
|
error: CliError | null
|
|
loading: boolean
|
|
/** Wall-clock timestamp for the most recent successful fetch. */
|
|
lastSuccessAt: number | null
|
|
/** Re-run the fetcher immediately (period/provider change, manual refresh). */
|
|
refresh: () => void
|
|
}
|
|
|
|
/**
|
|
* Generic CLI-backed data hook: fetches on mount + whenever `deps` change, then
|
|
* re-polls every `intervalMs`. Errors are normalized to the CliError shape so
|
|
* sections can branch on `error.kind`. Last-good data is retained on error.
|
|
*/
|
|
export function usePolled<T>(fetcher: () => Promise<T>, deps: unknown[], intervalMs = 30_000): Polled<T> {
|
|
const [data, setData] = useState<T | null>(null)
|
|
const [error, setError] = useState<CliError | null>(null)
|
|
const [loading, setLoading] = useState(true)
|
|
const [lastSuccessAt, setLastSuccessAt] = useState<number | null>(null)
|
|
// Generation counter: every load() (mount, deps change, interval, refresh)
|
|
// claims the next epoch; a fetch applies its result only while its epoch is
|
|
// still current. This is what keeps a slow fetch from an older deps/period
|
|
// from clobbering a newer one that already resolved.
|
|
const epochRef = useRef(0)
|
|
|
|
const load = useCallback(() => {
|
|
const epoch = ++epochRef.current
|
|
setLoading(true)
|
|
fetcher()
|
|
.then(result => {
|
|
if (epochRef.current !== epoch) return
|
|
setData(result)
|
|
setError(null)
|
|
setLastSuccessAt(Date.now())
|
|
})
|
|
.catch(err => {
|
|
if (epochRef.current !== epoch) return
|
|
setError(normalizeCliError(err))
|
|
})
|
|
.finally(() => {
|
|
if (epochRef.current !== epoch) return
|
|
setLoading(false)
|
|
})
|
|
// deps are intentionally the caller-provided dependency list.
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, deps)
|
|
|
|
useEffect(() => {
|
|
load()
|
|
const id = setInterval(() => load(), intervalMs)
|
|
return () => {
|
|
clearInterval(id)
|
|
// Retire this generation so an in-flight fetch can't resolve into state
|
|
// after unmount or a deps change.
|
|
epochRef.current++
|
|
}
|
|
}, [load, intervalMs])
|
|
|
|
const refresh = useCallback(() => {
|
|
load()
|
|
}, [load])
|
|
|
|
return { data, error, loading, lastSuccessAt, refresh }
|
|
}
|