useIsSchemaExposed.ts74 lines · main
| 1 | import { useMemo } from 'react' |
| 2 | |
| 3 | import { |
| 4 | parseDbSchemaString, |
| 5 | useProjectPostgrestConfigQuery, |
| 6 | } from '@/data/config/project-postgrest-config-query' |
| 7 | |
| 8 | type UseIsSchemaExposedParams = { |
| 9 | projectRef?: string |
| 10 | schemaName?: string |
| 11 | } |
| 12 | |
| 13 | type UseIsSchemaExposedOptions = { |
| 14 | enabled?: boolean |
| 15 | } |
| 16 | |
| 17 | export type UseIsSchemaExposedReturn = |
| 18 | | { |
| 19 | status: 'pending' |
| 20 | data: undefined |
| 21 | isPending: true |
| 22 | isError: false |
| 23 | isSuccess: false |
| 24 | } |
| 25 | | { |
| 26 | status: 'error' |
| 27 | data: undefined |
| 28 | isPending: false |
| 29 | isError: true |
| 30 | isSuccess: false |
| 31 | } |
| 32 | | { |
| 33 | status: 'success' |
| 34 | data: boolean |
| 35 | isPending: false |
| 36 | isError: false |
| 37 | isSuccess: true |
| 38 | } |
| 39 | |
| 40 | export const useIsSchemaExposed = ( |
| 41 | { projectRef, schemaName }: UseIsSchemaExposedParams, |
| 42 | { enabled = true }: UseIsSchemaExposedOptions = {} |
| 43 | ): UseIsSchemaExposedReturn => { |
| 44 | const shouldQueryConfig = enabled && !!projectRef && !!schemaName |
| 45 | const { |
| 46 | data: dbSchemaString, |
| 47 | isPending: isConfigPending, |
| 48 | isError: isConfigError, |
| 49 | } = useProjectPostgrestConfigQuery( |
| 50 | { projectRef }, |
| 51 | { enabled: shouldQueryConfig, select: ({ db_schema }) => db_schema } |
| 52 | ) |
| 53 | |
| 54 | const exposedSchemas = useMemo(() => { |
| 55 | if (!dbSchemaString) return [] |
| 56 | return parseDbSchemaString(dbSchemaString) |
| 57 | }, [dbSchemaString]) |
| 58 | |
| 59 | if (!shouldQueryConfig || isConfigPending) { |
| 60 | return { status: 'pending', data: undefined, isPending: true, isError: false, isSuccess: false } |
| 61 | } |
| 62 | |
| 63 | if (isConfigError) { |
| 64 | return { status: 'error', data: undefined, isPending: false, isError: true, isSuccess: false } |
| 65 | } |
| 66 | |
| 67 | return { |
| 68 | status: 'success', |
| 69 | data: exposedSchemas.includes(schemaName), |
| 70 | isPending: false, |
| 71 | isError: false, |
| 72 | isSuccess: true, |
| 73 | } |
| 74 | } |