import { useQuery } from '@tanstack/react-query' import { components } from 'api-types' import { useIsLoggedIn } from 'common' import { serviceStatusKeys } from './keys' import { get, handleError, unguarded } from '@/data/fetchers' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' export type ProjectServiceStatusVariables = { projectRef?: string } // Omit the 'healthy' field as it's equivalent to status = 'ACTIVE_HEALTHY' export type ServiceHealthResponse = Omit< components['schemas']['V1ServiceHealthResponse_Output'], 'healthy' > export type ProjectServiceStatus = ServiceHealthResponse['status'] export async function getProjectServiceStatus( { projectRef }: ProjectServiceStatusVariables, signal?: AbortSignal ) { if (!projectRef) throw new Error('projectRef is required') const { data, error } = await get(`/v1/projects/{ref}/health`, { params: { path: { ref: projectRef }, query: { services: ['auth', 'realtime', 'rest', 'storage', 'db'], }, }, signal, grant: unguarded.migrating, }) if (error) handleError(error) return data as ServiceHealthResponse[] } export type ProjectServiceStatusData = Awaited> export type ProjectServiceStatusError = StudioError export const useProjectServiceStatusQuery = ( { projectRef }: ProjectServiceStatusVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => { const isLoggedIn = useIsLoggedIn() return useQuery({ queryKey: serviceStatusKeys.serviceStatus(projectRef), queryFn: ({ signal }) => getProjectServiceStatus({ projectRef }, signal), enabled: enabled && isLoggedIn && typeof projectRef !== 'undefined', ...options, }) }