import { useQuery, UseQueryOptions } from '@tanstack/react-query' import type { components } from 'data/api' import { get, handleError } from 'data/fetchers' import type { ResponseError } from 'types' import { replicaKeys } from './keys' export type LoadBalancersVariables = { projectRef?: string } export type LoadBalancer = components['schemas']['LoadBalancerDetailResponse'] 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 = ResponseError export const useLoadBalancersQuery = ( { projectRef }: LoadBalancersVariables, { enabled = true, ...options }: UseQueryOptions = {} ) => { return useQuery( replicaKeys.loadBalancers(projectRef), ({ signal }) => getLoadBalancers({ projectRef }, signal), { enabled: enabled && typeof projectRef !== 'undefined', ...options, } ) }