import { useRef, useState } from 'react'; export function useDebouncedState( initialValue: T, delay: number = 0 ): [T, (value: T) => Promise] { const [state, setState] = useState(initialValue); const timer = useRef(null); function debouncedSetState(value: T) { return new Promise(resolve => { if (timer.current) { clearTimeout(timer.current); } timer.current = window?.setTimeout(() => { setState(value); resolve(value); }, delay); }); } return [state, debouncedSetState]; }