37 lines
1016 B
TypeScript
37 lines
1016 B
TypeScript
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<void>;
|
|
}
|
|
|
|
export function useAgents(): UseAgentsResult {
|
|
const [agents, setAgents] = useState<AgentInfo[]>([]);
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(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 };
|
|
}
|