import { useQuery, useQueryClient } from '@tanstack/react-query' import { useEffect } from 'react' import { customDomainKeys } from './keys' import { handleError, post, unguarded } from '@/data/fetchers' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' export type CustomDomainReverifyVariables = { projectRef?: string } export async function reverifyCustomDomain({ projectRef }: CustomDomainReverifyVariables) { if (!projectRef) throw new Error('projectRef is required') const { data, error } = await post(`/v1/projects/{ref}/custom-hostname/reverify`, { params: { path: { ref: projectRef } }, grant: unguarded.migrating, }) if (error) handleError(error) return data } export type CustomDomainReverifyData = Awaited> export type CustomDomainReverifyError = StudioError export const useCustomDomainReverifyQuery = ( { projectRef }: CustomDomainReverifyVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => { const client = useQueryClient() const query = useQuery({ queryKey: customDomainKeys.reverify(projectRef), queryFn: () => reverifyCustomDomain({ projectRef }), enabled: enabled && typeof projectRef !== 'undefined', ...options, }) useEffect(() => { if (!query.isSuccess) return client.invalidateQueries({ queryKey: customDomainKeys.list(projectRef) }) }, [query.isSuccess, projectRef, client]) return query }