import * as React from "react"; export function useThrottle( value: TValue, limit: number, onValueChange?: (value: TValue) => void, ) { const [throttledValue, setThrottledValue] = React.useState(value); const lastRan = React.useRef(Date.now()); React.useEffect(() => { const handler = setTimeout(() => { if (Date.now() - lastRan.current >= limit) { setThrottledValue(value); if (onValueChange) { onValueChange(value); } lastRan.current = Date.now(); } }, limit - (Date.now() - lastRan.current)); return () => { clearTimeout(handler); }; }, [value, limit]); return throttledValue; }