import { useState, useEffect, useCallback, useRef } from "react"; type OptimisticStateResult = [T, (newValue: T) => void]; const useOptimisticState = ( initialState: T, setState: (newValue: T) => void, delay: number = 20, ): OptimisticStateResult => { const [optimisticValue, setOptimisticValue] = useState(initialState); const debounceTimeout = useRef | null>(null); const handleValueChange = useCallback( (newValue: T) => { // Update the optimistic value immediately setOptimisticValue(newValue); // Clear any pending debounce timeout if (debounceTimeout.current) { clearTimeout(debounceTimeout.current); } // Set a new debounce timeout debounceTimeout.current = setTimeout(() => { // Update the actual value using the provided setter function setState(newValue); }, delay); }, [delay, setState], ); useEffect(() => { return () => { if (debounceTimeout.current) { clearTimeout(debounceTimeout.current); } }; }, []); return [optimisticValue, handleValueChange]; }; export default useOptimisticState;