import { useQuery } from '@tanstack/react-query' import { getExposedTables, type ExposedTable, type ExposedTablesError, type ExposedTablesVariables, } from './exposed-tables-infinite-query' import { privilegeKeys } from './keys' import type { UseCustomQueryOptions } from '@/types' // The API Docs panel renders every table without virtualization, so we fetch // the full grant-status list in a single request rather than paginating. const ALL_EXPOSED_ENTITIES_LIMIT = 100_000 export type ExposedTablesAllData = ExposedTable[] async function getAllExposedTables( { projectRef, connectionString }: ExposedTablesVariables, signal?: AbortSignal ): Promise { const { tables } = await getExposedTables( { projectRef, connectionString, page: 0, limit: ALL_EXPOSED_ENTITIES_LIMIT }, signal ) return tables } /** * Returns the grant status (`granted` | `custom` | `revoked`) for every table, * used to filter unexposed tables out of the autogenerated Data API docs. */ export const useExposedTablesQuery = ( { projectRef, connectionString }: ExposedTablesVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: privilegeKeys.exposedTablesAll(projectRef), queryFn: ({ signal }) => getAllExposedTables({ projectRef, connectionString }, signal), enabled: enabled && typeof projectRef !== 'undefined', ...options, })