|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useTRPC } from '@/lib/trpc/utils'; |
| 4 | +import { useQuery } from '@tanstack/react-query'; |
| 5 | + |
| 6 | +type CloudAgentNextFilters = { |
| 7 | + /** Inclusive ISO datetime lower bound for observed-outcome reporting. */ |
| 8 | + startDate: string; |
| 9 | + /** Exclusive ISO datetime upper bound for observed-outcome reporting. */ |
| 10 | + endDate: string; |
| 11 | +}; |
| 12 | + |
| 13 | +export type CloudAgentNextHealthFilters = CloudAgentNextFilters & { |
| 14 | + bucket: 'hour' | 'day'; |
| 15 | + createdOnPlatform?: string | null; |
| 16 | +}; |
| 17 | + |
| 18 | +type CloudAgentNextHealthError = { |
| 19 | + source: 'setup' | 'run'; |
| 20 | + stage: string; |
| 21 | + code: string; |
| 22 | +}; |
| 23 | + |
| 24 | +function enabledForInterval(params: CloudAgentNextFilters) { |
| 25 | + return Boolean(params.startDate && params.endDate); |
| 26 | +} |
| 27 | + |
| 28 | +export function useCloudAgentNextHealthPlatforms() { |
| 29 | + const trpc = useTRPC(); |
| 30 | + return useQuery(trpc.admin.cloudAgentNext.listHealthPlatforms.queryOptions()); |
| 31 | +} |
| 32 | + |
| 33 | +export function useCloudAgentNextHealthOverview( |
| 34 | + params: CloudAgentNextHealthFilters, |
| 35 | + enabled = true |
| 36 | +) { |
| 37 | + const trpc = useTRPC(); |
| 38 | + return useQuery({ |
| 39 | + ...trpc.admin.cloudAgentNext.getHealthOverview.queryOptions(params), |
| 40 | + enabled: enabled && enabledForInterval(params), |
| 41 | + refetchOnReconnect: false, |
| 42 | + refetchOnWindowFocus: false, |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +export function useCloudAgentNextHealthErrorSessions( |
| 47 | + params: CloudAgentNextHealthFilters, |
| 48 | + error: CloudAgentNextHealthError | null |
| 49 | +) { |
| 50 | + const trpc = useTRPC(); |
| 51 | + return useQuery({ |
| 52 | + ...trpc.admin.cloudAgentNext.listHealthErrorSessions.queryOptions({ |
| 53 | + startDate: params.startDate, |
| 54 | + endDate: params.endDate, |
| 55 | + source: error?.source ?? 'run', |
| 56 | + stage: error?.stage ?? 'not-selected', |
| 57 | + code: error?.code ?? 'not-selected', |
| 58 | + createdOnPlatform: params.createdOnPlatform, |
| 59 | + }), |
| 60 | + enabled: enabledForInterval(params) && Boolean(error), |
| 61 | + }); |
| 62 | +} |
0 commit comments