Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
34 lines
1.3 KiB
TypeScript
34 lines
1.3 KiB
TypeScript
// Global refresh rate: one setting for every live view. Kept in localStorage for instant
|
|
// use and mirrored to the hub's UI prefs so it follows the login.
|
|
import { useEffect, useState } from 'react'
|
|
|
|
export const REFRESH_OPTIONS = [250, 500, 1000, 2000, 5000] as const
|
|
export const DEFAULT_REFRESH = 2000
|
|
// No floors: the rate means exactly what it says on every page, at the host's expense.
|
|
export const LIST_FLOOR = 0
|
|
const KEY = 'hamlab.refresh'
|
|
|
|
export function getRefreshMs(): number {
|
|
const v = Number(localStorage.getItem(KEY))
|
|
return v >= 100 && v <= 60000 ? v : DEFAULT_REFRESH
|
|
}
|
|
|
|
export function setRefreshMs(ms: number) {
|
|
if (!(ms >= 100 && ms <= 60000)) return // never accept a nonsense rate, wherever it came from
|
|
localStorage.setItem(KEY, String(ms))
|
|
window.dispatchEvent(new CustomEvent('hamlab:refresh', { detail: ms }))
|
|
}
|
|
|
|
export function fmtMs(ms: number): string { return ms >= 1000 ? `${ms / 1000} s` : `${ms} ms` }
|
|
|
|
// useRefreshMs re-renders the caller when the global rate changes.
|
|
export function useRefreshMs(floor = 0): number {
|
|
const [ms, setMs] = useState(getRefreshMs())
|
|
useEffect(() => {
|
|
const h = (e: Event) => setMs((e as CustomEvent<number>).detail)
|
|
window.addEventListener('hamlab:refresh', h)
|
|
return () => window.removeEventListener('hamlab:refresh', h)
|
|
}, [])
|
|
return Math.max(ms, floor)
|
|
}
|