import { useState, useCallback } from 'react'; interface LoadingState { data: T | null; isLoading: boolean; error: Error | null; execute: (...args: any[]) => Promise; reset: () => void; } /** * Custom hook for managing loading states with error handling * Reduces boilerplate code for async operations */ export function useLoadingState( asyncFunction: (...args: any[]) => Promise ): LoadingState { const [data, setData] = useState(null); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const execute = useCallback( async (...args: any[]): Promise => { try { setIsLoading(true); setError(null); const result = await asyncFunction(...args); setData(result); return result; } catch (err) { const error = err instanceof Error ? err : new Error('An error occurred'); setError(error); throw error; } finally { setIsLoading(false); } }, [asyncFunction] ); const reset = useCallback(() => { setData(null); setError(null); setIsLoading(false); }, []); return { data, isLoading, error, execute, reset }; }