import { useState, useEffect, useCallback } from "react"; import { agentApiService } from "../api/agent.api.service"; import type { AgentInfo } from "../types/agent.types"; interface UseAgentsResult { agents: AgentInfo[]; isLoading: boolean; error: string | null; refetch: () => Promise; } export function useAgents(): UseAgentsResult { const [agents, setAgents] = useState([]); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const fetchAgents = useCallback(async () => { setIsLoading(true); setError(null); try { const data = await agentApiService.getAgents(); setAgents(data); } catch (err) { const message = err instanceof Error ? err.message : "Failed to fetch agents"; setError(message); } finally { setIsLoading(false); } }, []); useEffect(() => { fetchAgents(); }, [fetchAgents]); return { agents, isLoading, error, refetch: fetchAgents }; }