import { useQuery, useQueryClient } from '@tanstack/react-query' import { useCallback } from 'react' import { authKeys } from './keys' import type { components } from '@/data/api' import { get, handleError } from '@/data/fetchers' import { IS_PLATFORM } from '@/lib/constants' import type { ResponseError, UseCustomQueryOptions } from '@/types' export type AuthConfigVariables = { projectRef?: string } export type AuthConfigResponse = components['schemas']['GoTrueConfigResponse'] export async function getProjectAuthConfig( { projectRef }: AuthConfigVariables, signal?: AbortSignal ) { if (!projectRef) throw new Error('projectRef is required') const { data, error } = await get('/platform/auth/{ref}/config', { params: { path: { ref: projectRef } }, signal, }) if (error) handleError(error) return data } export type ProjectAuthConfigData = Awaited> export type ProjectAuthConfigError = ResponseError export const useAuthConfigQuery = ( { projectRef }: AuthConfigVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: authKeys.authConfig(projectRef), queryFn: ({ signal }) => getProjectAuthConfig({ projectRef }, signal), enabled: enabled && IS_PLATFORM && typeof projectRef !== 'undefined' && projectRef !== '_', ...options, }) export const useAuthConfigPrefetch = ({ projectRef }: AuthConfigVariables) => { const client = useQueryClient() return useCallback(() => { if (projectRef) { client.prefetchQuery({ queryKey: authKeys.authConfig(projectRef), queryFn: ({ signal }) => getProjectAuthConfig({ projectRef }, signal), }) } }, [client, projectRef]) }