feat(ui): replace useAgentEvents with useInfiniteAgentEvents
Cursor-paginated timeline stream matching the new /agents/events endpoint. Consumers (AgentHealth, AgentInstance) updated in follow-up commits.
This commit is contained in:
@@ -3,6 +3,7 @@ import { config } from '../../config';
|
||||
import { useAuthStore } from '../../auth/auth-store';
|
||||
import { useEnvironmentStore } from '../environment-store';
|
||||
import { useRefreshInterval } from './use-refresh-interval';
|
||||
import { useInfiniteStream, type UseInfiniteStreamResult } from '../../hooks/useInfiniteStream';
|
||||
|
||||
export function useAgents(status?: string, application?: string) {
|
||||
const environment = useEnvironmentStore((s) => s.environment);
|
||||
@@ -31,18 +32,45 @@ export function useAgents(status?: string, application?: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export function useAgentEvents(appId?: string, agentId?: string, limit = 50, toOverride?: string) {
|
||||
export interface AgentEventResponse {
|
||||
id: number;
|
||||
instanceId: string;
|
||||
applicationId: string;
|
||||
eventType: string;
|
||||
detail: string;
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
interface AgentEventPageResponse {
|
||||
data: AgentEventResponse[];
|
||||
nextCursor: string | null;
|
||||
hasMore: boolean;
|
||||
}
|
||||
|
||||
export interface UseInfiniteAgentEventsArgs {
|
||||
appId?: string;
|
||||
agentId?: string;
|
||||
isAtTop: boolean;
|
||||
pageSize?: number;
|
||||
}
|
||||
|
||||
export function useInfiniteAgentEvents(
|
||||
args: UseInfiniteAgentEventsArgs,
|
||||
): UseInfiniteStreamResult<AgentEventResponse> {
|
||||
const environment = useEnvironmentStore((s) => s.environment);
|
||||
const refetchInterval = useRefreshInterval(15_000);
|
||||
return useQuery({
|
||||
queryKey: ['agents', 'events', environment, appId, agentId, limit, toOverride],
|
||||
queryFn: async () => {
|
||||
const pageSize = args.pageSize ?? 50;
|
||||
|
||||
return useInfiniteStream<AgentEventResponse>({
|
||||
queryKey: ['agents', 'events', 'infinite', environment ?? '', args.appId ?? '', args.agentId ?? '', pageSize],
|
||||
enabled: !!environment,
|
||||
isAtTop: args.isAtTop,
|
||||
fetchPage: async (cursor) => {
|
||||
const token = useAuthStore.getState().accessToken;
|
||||
const params = new URLSearchParams();
|
||||
if (appId) params.set('appId', appId);
|
||||
if (agentId) params.set('agentId', agentId);
|
||||
if (toOverride) params.set('to', toOverride);
|
||||
params.set('limit', String(limit));
|
||||
if (args.appId) params.set('appId', args.appId);
|
||||
if (args.agentId) params.set('agentId', args.agentId);
|
||||
if (cursor) params.set('cursor', cursor);
|
||||
params.set('limit', String(pageSize));
|
||||
const res = await fetch(
|
||||
`${config.apiBaseUrl}/environments/${encodeURIComponent(environment!)}/agents/events?${params}`,
|
||||
{
|
||||
@@ -52,9 +80,8 @@ export function useAgentEvents(appId?: string, agentId?: string, limit = 50, toO
|
||||
},
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to load agent events');
|
||||
return res.json();
|
||||
const page: AgentEventPageResponse = await res.json();
|
||||
return { data: page.data, nextCursor: page.nextCursor, hasMore: page.hasMore };
|
||||
},
|
||||
enabled: !!environment,
|
||||
refetchInterval,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user