import { createContext, useContext, useEffect, useState } from 'react' import { t } from '../i18n' import type { CodeburnBridge } from './types' // The auto-refresh cadence, chosen in Settings > General and persisted at // localStorage `codeburn.refreshInterval`. usePolled reads the resolved // interval from context as its default when the caller passes none. `Manual` // means no setInterval — data refreshes only on deps change and ⌘R / refresh. // `label` is a getter (not a plain field) so each read re-resolves t() under // the current locale — Settings.tsx reads `option.label` directly, and this // array is a module-level constant that never gets re-evaluated on its own. export const REFRESH_OPTIONS: ReadonlyArray<{ value: string; label: string; ms: number | null }> = [ { value: 'manual', get label() { return t('shell.refreshCadence.manual') }, ms: null }, { value: '30s', get label() { return t('shell.refreshCadence.30s') }, ms: 30_000 }, { value: '1m', get label() { return t('shell.refreshCadence.1m') }, ms: 60_000 }, { value: '3m', get label() { return t('shell.refreshCadence.3m') }, ms: 180_000 }, { value: '5m', get label() { return t('shell.refreshCadence.5m') }, ms: 300_000 }, { value: '10m', get label() { return t('shell.refreshCadence.10m') }, ms: 600_000 }, ] // 60s is the default cadence: it halves idle CLI spawns versus the old 30s while // staying fresh enough for a usage dashboard. 30s is still offered for anyone who // wants it. Only an explicit choice is persisted (persistRefreshValue runs solely // from Settings), so bumping this default silently migrates users who never chose, // while any stored value below keeps overriding it. export const DEFAULT_REFRESH_VALUE = '1m' const DEFAULT_MS = 60_000 const STORAGE_KEY = 'codeburn.refreshInterval' export function refreshValueToMs(value: string): number | null { const option = REFRESH_OPTIONS.find(o => o.value === value) return option ? option.ms : DEFAULT_MS } /** Persisted cadence at boot; falls back to the 30s default (prior behavior). */ export function readRefreshValue(): string { try { const saved = globalThis.localStorage?.getItem(STORAGE_KEY) if (saved && REFRESH_OPTIONS.some(o => o.value === saved)) return saved } catch { /* storage can be unavailable */ } return DEFAULT_REFRESH_VALUE } export function persistRefreshValue(value: string): void { try { globalThis.localStorage?.setItem(STORAGE_KEY, value) } catch { /* storage can be unavailable */ } } // The SLOW tier intervals, shared by every section that shows the same report. // Neither report moves minute to minute and each costs a full CLI spawn // (`act report --json` is not even served by the resident child), so they // refresh on mount, on a manual refresh, and on these timers — never on a live // tick. usePolled additionally floors them at the user's live cadence. export const ACT_SLOW_MS = 600_000 export const YIELD_SLOW_MS = 300_000 // On battery the live poll runs half as often. The user's choice stays the base // — only the resolved interval moves, and it moves back the moment AC returns. export const BATTERY_CADENCE_FACTOR = 2 export function resolveCadenceMs(value: string, onBattery: boolean): number | null { const base = refreshValueToMs(value) return base == null ? null : base * (onBattery ? BATTERY_CADENCE_FACTOR : 1) } /** Whether the machine is running on battery, per Electron's powerMonitor. * Read from the bridge at call time (like lib/platform.ts) so a preload that * lacks the method, or no bridge at all, simply reads as AC. */ export function useOnBattery(): boolean { const [onBattery, setOnBattery] = useState(false) useEffect(() => { const bridge = typeof window === 'undefined' ? undefined : (window as unknown as { codeburn?: CodeburnBridge }).codeburn if (typeof bridge?.powerStatus !== 'function') return let live = true bridge.powerStatus().then(value => { if (live) setOnBattery(value) }).catch(() => {}) const unsubscribe = bridge.onPowerStatus?.(value => { if (live) setOnBattery(value) }) return () => { live = false; unsubscribe?.() } }, []) return onBattery } export type RefreshCadence = { value: string /** Resolved poll interval in ms, or null for Manual (no auto-poll). */ intervalMs: number | null setValue: (value: string) => void } // Default matches the historical 30s hardcoded interval so any consumer rendered // without a provider (e.g. isolated hook/component tests) behaves as before. export const RefreshCadenceContext = createContext({ value: DEFAULT_REFRESH_VALUE, intervalMs: DEFAULT_MS, setValue: () => {}, }) export function useRefreshCadence(): RefreshCadence { return useContext(RefreshCadenceContext) }