import { useCallback, useEffect, useRef } from 'react'; // biome-ignore lint/suspicious/noExplicitAny: needed for variadic generic type AnyFunction = (...args: Array) => void; interface Props { functionToDebounce: T; memoProps?: Array; wait: number; } export const useDebounce = ({ functionToDebounce, wait, memoProps = [] }: Props): ((...args: Parameters) => void) => { const timeoutRef = useRef | null>(null); const ref = useRef(undefined); useEffect(() => { ref.current = functionToDebounce; }, [functionToDebounce]); return useCallback( (...args: Parameters): void => { if (timeoutRef.current) { clearTimeout(timeoutRef.current); } timeoutRef.current = setTimeout(() => { ref.current?.(...args); }, wait); }, [...memoProps, wait] ); };