import { useCallback, useState } from 'react'; /** * Hook for managing async operations with consistent loading and error states */ export function useAsyncOperation( operation: (...args: T) => Promise, options: { onSuccess?: () => void; onError?: (error: Error) => void; } = {} ) { const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const execute = useCallback( async (...args: T) => { setIsLoading(true); setError(null); try { await operation(...args); options.onSuccess?.(); } catch (err) { const error = err instanceof Error ? err : new Error(String(err)); setError(error); options.onError?.(error); } finally { setIsLoading(false); } }, [operation, options] ); const reset = useCallback(() => { setError(null); }, []); return { execute, isLoading, error, reset, }; }