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 { useAuthStore } from '../../auth/auth-store';
|
||||||
import { useEnvironmentStore } from '../environment-store';
|
import { useEnvironmentStore } from '../environment-store';
|
||||||
import { useRefreshInterval } from './use-refresh-interval';
|
import { useRefreshInterval } from './use-refresh-interval';
|
||||||
|
import { useInfiniteStream, type UseInfiniteStreamResult } from '../../hooks/useInfiniteStream';
|
||||||
|
|
||||||
export function useAgents(status?: string, application?: string) {
|
export function useAgents(status?: string, application?: string) {
|
||||||
const environment = useEnvironmentStore((s) => s.environment);
|
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 environment = useEnvironmentStore((s) => s.environment);
|
||||||
const refetchInterval = useRefreshInterval(15_000);
|
const pageSize = args.pageSize ?? 50;
|
||||||
return useQuery({
|
|
||||||
queryKey: ['agents', 'events', environment, appId, agentId, limit, toOverride],
|
return useInfiniteStream<AgentEventResponse>({
|
||||||
queryFn: async () => {
|
queryKey: ['agents', 'events', 'infinite', environment ?? '', args.appId ?? '', args.agentId ?? '', pageSize],
|
||||||
|
enabled: !!environment,
|
||||||
|
isAtTop: args.isAtTop,
|
||||||
|
fetchPage: async (cursor) => {
|
||||||
const token = useAuthStore.getState().accessToken;
|
const token = useAuthStore.getState().accessToken;
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
if (appId) params.set('appId', appId);
|
if (args.appId) params.set('appId', args.appId);
|
||||||
if (agentId) params.set('agentId', agentId);
|
if (args.agentId) params.set('agentId', args.agentId);
|
||||||
if (toOverride) params.set('to', toOverride);
|
if (cursor) params.set('cursor', cursor);
|
||||||
params.set('limit', String(limit));
|
params.set('limit', String(pageSize));
|
||||||
const res = await fetch(
|
const res = await fetch(
|
||||||
`${config.apiBaseUrl}/environments/${encodeURIComponent(environment!)}/agents/events?${params}`,
|
`${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');
|
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