import { useCallback, useEffect, useRef } from 'react'; interface Props { callback: ((args?: any) => any) | ((args?: any) => Promise); delay?: number; } export function useDebounceCallback({ callback, delay = 250 }: Props) { const timeoutRef = useRef(null); const callbackRef = useRef(callback); useEffect(() => { callbackRef.current = callback; }, [callback]); const abort = useCallback(() => { if (timeoutRef.current) { clearTimeout(timeoutRef.current); timeoutRef.current = null; } }, []); const debouncedCallback = useCallback( (args?: any) => { if (timeoutRef.current) { clearTimeout(timeoutRef.current); } timeoutRef.current = setTimeout(() => { callbackRef.current(args); }, delay); }, [delay] ); useEffect(() => { return () => { if (timeoutRef.current) { clearTimeout(timeoutRef.current); } }; }, []); return { debouncedCallback, abort }; }