import { useCallback, useEffect, useRef, useState } from "react"; /** * Like useState, but a truthy value auto-clears back to the initial falsy value after * `ms` milliseconds. Every call to the setter cancels any pending timer and (if the new * value is truthy) arms a fresh one, so rapid-fire updates reset the countdown instead of * cutting it short. Meant for post-action confirmation/error banners that shouldn't linger. */ export function useDismissingState(initial: T, ms = 7000) { const [value, setValue] = useState(initial); const timeoutRef = useRef | null>(null); const set = useCallback((next: T) => { if (timeoutRef.current) clearTimeout(timeoutRef.current); setValue(next); if (next) { timeoutRef.current = setTimeout(() => setValue(initial), ms); } }, [ms, initial]); useEffect(() => () => { if (timeoutRef.current) clearTimeout(timeoutRef.current); }, []); return [value, set] as const; }