import { useQuery } from '@tanstack/react-query'; import { api } from '../client'; import type { SearchRequest, ExecutionStats, ExecutionSummary, StatsTimeseries, ExecutionDetail, } from '../schema-types'; export function useExecutionStats(timeFrom: string | undefined, timeTo: string | undefined) { return useQuery({ queryKey: ['executions', 'stats', timeFrom, timeTo], queryFn: async () => { const { data, error } = await api.GET('/search/stats', { params: { query: { from: timeFrom!, to: timeTo || undefined, }, }, }); if (error) throw new Error('Failed to load stats'); return data as unknown as ExecutionStats; }, enabled: !!timeFrom, placeholderData: (prev) => prev, refetchInterval: 10_000, }); } export function useSearchExecutions(filters: SearchRequest, live = false) { return useQuery({ queryKey: ['executions', 'search', filters], queryFn: async () => { const { data, error } = await api.POST('/search/executions', { body: filters, }); if (error) throw new Error('Search failed'); return data as unknown as { data: ExecutionSummary[]; total: number; offset: number; limit: number }; }, placeholderData: (prev) => prev, refetchInterval: live ? 5_000 : false, }); } export function useStatsTimeseries(timeFrom: string | undefined, timeTo: string | undefined) { return useQuery({ queryKey: ['executions', 'timeseries', timeFrom, timeTo], queryFn: async () => { const { data, error } = await api.GET('/search/stats/timeseries', { params: { query: { from: timeFrom!, to: timeTo || undefined, buckets: 24, }, }, }); if (error) throw new Error('Failed to load timeseries'); return data as unknown as StatsTimeseries; }, enabled: !!timeFrom, placeholderData: (prev) => prev, refetchInterval: 30_000, }); } export function useExecutionDetail(executionId: string | null) { return useQuery({ queryKey: ['executions', 'detail', executionId], queryFn: async () => { const { data, error } = await api.GET('/executions/{executionId}', { params: { path: { executionId: executionId! } }, }); if (error) throw new Error('Failed to load execution detail'); return data as unknown as ExecutionDetail; }, enabled: !!executionId, }); } export function useProcessorSnapshot( executionId: string | null, index: number | null, ) { return useQuery({ queryKey: ['executions', 'snapshot', executionId, index], queryFn: async () => { const { data, error } = await api.GET( '/executions/{executionId}/processors/{index}/snapshot', { params: { path: { executionId: executionId!, index: index! }, }, }, ); if (error) throw new Error('Failed to load snapshot'); return data as unknown as Record; }, enabled: !!executionId && index !== null, }); }