mirror of
https://github.com/supabase/supabase.git
synced 2026-10-12 04:45:08 +03:00
Forward compatible changes to support new platform plan (similar handling to Enterprise)
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import { get, handleError } from 'data/fetchers'
|
|
import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization'
|
|
import { IS_PLATFORM } from 'lib/constants'
|
|
import type { ResponseError, UseCustomQueryOptions } from 'types'
|
|
import { orgSSOKeys } from './keys'
|
|
|
|
export type OrgSSOConfigVariables = {
|
|
orgSlug?: string
|
|
}
|
|
|
|
export async function getOrgSSOConfig({ orgSlug }: OrgSSOConfigVariables, signal?: AbortSignal) {
|
|
if (!orgSlug) throw new Error('Organization slug is required')
|
|
|
|
const { data, error } = await get('/platform/organizations/{slug}/sso', {
|
|
params: { path: { slug: orgSlug } },
|
|
signal,
|
|
})
|
|
|
|
if (error) {
|
|
const ssoNotSetUp =
|
|
(error as any)?.code === 404 &&
|
|
(error as any)?.message?.includes('Failed to find an existing SSO Provider')
|
|
|
|
if (ssoNotSetUp) {
|
|
return null
|
|
} else {
|
|
handleError(error)
|
|
}
|
|
}
|
|
return data
|
|
}
|
|
|
|
export type OrgSSOConfigData = Awaited<ReturnType<typeof getOrgSSOConfig>>
|
|
export type OrgSSOConfigError = ResponseError
|
|
|
|
export const useOrgSSOConfigQuery = <TData = OrgSSOConfigData>(
|
|
{ orgSlug }: OrgSSOConfigVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<OrgSSOConfigData, OrgSSOConfigError, TData> = {}
|
|
) => {
|
|
const { data: organization } = useSelectedOrganizationQuery()
|
|
const plan = organization?.plan.id
|
|
const canSetupSSOConfig = ['team', 'enterprise', 'platform'].includes(plan ?? '')
|
|
|
|
return useQuery<OrgSSOConfigData, OrgSSOConfigError, TData>({
|
|
queryKey: orgSSOKeys.orgSSOConfig(orgSlug),
|
|
queryFn: ({ signal }) => getOrgSSOConfig({ orgSlug }, signal),
|
|
enabled: enabled && IS_PLATFORM && typeof orgSlug !== 'undefined' && canSetupSSOConfig,
|
|
...options,
|
|
})
|
|
}
|