import { useQuery } from '@tanstack/react-query'; import { api } from '../client'; import { config } from '../../config'; import { useAuthStore } from '../../auth/auth-store'; import { useRefreshInterval } from './use-refresh-interval'; export function useAgents(status?: string, application?: string) { const refetchInterval = useRefreshInterval(10_000); return useQuery({ queryKey: ['agents', status, application], queryFn: async () => { const { data, error } = await api.GET('/agents', { params: { query: { ...(status ? { status } : {}), ...(application ? { application } : {}) } }, }); if (error) throw new Error('Failed to load agents'); return data!; }, refetchInterval, }); } export function useAgentEvents(appId?: string, agentId?: string, limit = 50) { const refetchInterval = useRefreshInterval(15_000); return useQuery({ queryKey: ['agents', 'events', appId, agentId, limit], queryFn: async () => { const token = useAuthStore.getState().accessToken; const params = new URLSearchParams(); if (appId) params.set('appId', appId); if (agentId) params.set('agentId', agentId); params.set('limit', String(limit)); const res = await fetch(`${config.apiBaseUrl}/agents/events-log?${params}`, { headers: { Authorization: `Bearer ${token}`, 'X-Cameleer-Protocol-Version': '1', }, }); if (!res.ok) throw new Error('Failed to load agent events'); return res.json(); }, refetchInterval, }); }