Files
supabase/apps/studio/hooks/misc/useIsSchemaExposed.ts
Charis 180ce515f6 style: require @ imports and sort imports for studio/hooks (#44444)
* **Chores**
* Updated internal module import paths across hook files to use
standardized path aliases for improved code consistency and
maintainability.
2026-04-01 11:48:02 -04:00

75 lines
1.7 KiB
TypeScript

import { useMemo } from 'react'
import {
parseDbSchemaString,
useProjectPostgrestConfigQuery,
} from '@/data/config/project-postgrest-config-query'
type UseIsSchemaExposedParams = {
projectRef?: string
schemaName?: string
}
type UseIsSchemaExposedOptions = {
enabled?: boolean
}
export type UseIsSchemaExposedReturn =
| {
status: 'pending'
data: undefined
isPending: true
isError: false
isSuccess: false
}
| {
status: 'error'
data: undefined
isPending: false
isError: true
isSuccess: false
}
| {
status: 'success'
data: boolean
isPending: false
isError: false
isSuccess: true
}
export const useIsSchemaExposed = (
{ projectRef, schemaName }: UseIsSchemaExposedParams,
{ enabled = true }: UseIsSchemaExposedOptions = {}
): UseIsSchemaExposedReturn => {
const shouldQueryConfig = enabled && !!projectRef && !!schemaName
const {
data: dbSchemaString,
isPending: isConfigPending,
isError: isConfigError,
} = useProjectPostgrestConfigQuery(
{ projectRef },
{ enabled: shouldQueryConfig, select: ({ db_schema }) => db_schema }
)
const exposedSchemas = useMemo(() => {
if (!dbSchemaString) return []
return parseDbSchemaString(dbSchemaString)
}, [dbSchemaString])
if (!shouldQueryConfig || isConfigPending) {
return { status: 'pending', data: undefined, isPending: true, isError: false, isSuccess: false }
}
if (isConfigError) {
return { status: 'error', data: undefined, isPending: false, isError: true, isSuccess: false }
}
return {
status: 'success',
data: exposedSchemas.includes(schemaName),
isPending: false,
isError: false,
isSuccess: true,
}
}