Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 1x 5x 5x 3x 5x 2x 5x | import { useEffect, useState } from "react";
/**
* EI-3209 — auto-refresh ticker for analytics dashboards.
*
* Returns a `tick` counter that increments every `intervalMs`, so a dashboard can
* poll (re-fetch) its data without a manual reload — pass `tick` as a `refreshKey`
* prop and include it in the consuming section's fetch useEffect deps. Pausable via
* `enabled`; the interval is cleared on unmount.
*
* @param {number} intervalMs polling interval in ms (default 30s)
* @param {boolean} enabled when false, no polling (tick stays 0)
* @returns {number} tick 0 on mount, +1 every interval
*/
export const useAutoRefresh = (intervalMs = 30000, enabled = true) => {
const [tick, setTick] = useState(0);
useEffect(() => {
if (!enabled || !intervalMs) return undefined;
const id = setInterval(() => setTick((t) => t + 1), intervalMs);
return () => clearInterval(id);
}, [intervalMs, enabled]);
return tick;
};
export default useAutoRefresh;
|