import { useCallback, useEffect, useState } from "react"; type AsyncStatus = "idle" | "loading" | "ready" | "error"; interface UseAsyncDataResult { data: T | null; status: AsyncStatus; error: string | null; reload: () => void; } export function useAsyncData( fetcher: () => Promise, deps: React.DependencyList = [] ): UseAsyncDataResult { const [data, setData] = useState(null); const [status, setStatus] = useState("idle"); const [error, setError] = useState(null); const load = useCallback(async () => { setStatus("loading"); setError(null); try { const result = await fetcher(); setData(result); setStatus("ready"); } catch (err) { setError(err instanceof Error ? err.message : "Failed to load data"); setStatus("error"); } }, deps); const reload = useCallback(() => { void load(); }, [load]); useEffect(() => { void load(); }, [load]); return { data, status, error, reload }; }