import { useQuery } from '@tanstack/react-query' import { replicaKeys } from './keys' import type { components } from '@/data/api' import { get, handleError } from '@/data/fetchers' import { IS_PLATFORM } from '@/lib/constants' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' export type LoadBalancersVariables = { projectRef?: string } export type LoadBalancer = components['schemas']['LoadBalancerDetailResponse_Output'] export async function getLoadBalancers( { projectRef }: LoadBalancersVariables, signal?: AbortSignal ) { if (!projectRef) throw new Error('Project ref is required') const { data, error } = await get(`/platform/projects/{ref}/load-balancers`, { params: { path: { ref: projectRef } }, signal, }) if (error) handleError(error) return data } export type LoadBalancersData = Awaited> export type LoadBalancersError = StudioError export const useLoadBalancersQuery = ( { projectRef }: LoadBalancersVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => { return useQuery({ queryKey: replicaKeys.loadBalancers(projectRef), queryFn: ({ signal }) => getLoadBalancers({ projectRef }, signal), enabled: enabled && typeof projectRef !== 'undefined' && IS_PLATFORM, ...options, }) }