import { skipToken, useQuery } from '@tanstack/react-query' import { IS_PLATFORM } from 'common' import { BannedIPKeys } from './keys' import { handleError, post, unguarded } from '@/data/fetchers' import { useProjectDetailQuery } from '@/data/projects/project-detail-query' import { PROVIDERS } from '@/lib/constants' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' type BannedIPVariables = { projectRef?: string } export async function getBannedIPs({ projectRef }: BannedIPVariables, signal: AbortSignal) { if (!projectRef) throw new Error('projectRef is required') const { data, error } = await post(`/v1/projects/{ref}/network-bans/retrieve`, { params: { path: { ref: projectRef } }, signal, grant: unguarded.migrating, }) if (error) handleError(error) return data } export type IPData = Awaited> export type IPError = StudioError export const useBannedIPsQuery = ( { projectRef }: BannedIPVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => { const { data: project } = useProjectDetailQuery( { ref: projectRef }, { enabled: enabled && IS_PLATFORM } ) const isSupported = !!project && !project.high_availability && project.cloud_provider !== PROVIDERS.AWS_K8S.id return useQuery({ queryKey: BannedIPKeys.list(projectRef), queryFn: isSupported ? ({ signal }) => getBannedIPs({ projectRef }, signal) : skipToken, enabled: enabled && IS_PLATFORM && typeof projectRef !== 'undefined' && isSupported, retry: false, refetchOnWindowFocus: false, staleTime: 60_000, ...options, }) }