mirror of
https://github.com/supabase/supabase.git
synced 2026-05-30 01:12:22 +08:00
Occasionally results in a 429 in API - there are plans to fix this on the API level, but it's not a quick change, so hoping to reduce the likeliness of 429 with a higher stale time. Subscriptions and project addons rarely change and if they are changed, it's invalidated on the client anyway.
63 lines
2.0 KiB
TypeScript
63 lines
2.0 KiB
TypeScript
import { PermissionAction } from '@supabase/shared-types/out/constants'
|
|
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
|
|
|
|
import { get, handleError } from 'data/fetchers'
|
|
import { useCheckPermissions } from 'hooks/misc/useCheckPermissions'
|
|
import type { ResponseError } from 'types'
|
|
import { subscriptionKeys } from './keys'
|
|
import { components } from 'api-types'
|
|
|
|
export type OrgSubscriptionVariables = {
|
|
orgSlug?: string
|
|
}
|
|
|
|
export type PlanType = components['schemas']['BillingPlanId']
|
|
|
|
export async function getOrgSubscription(
|
|
{ orgSlug }: OrgSubscriptionVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!orgSlug) throw new Error('orgSlug is required')
|
|
|
|
const { error, data } = await get('/platform/organizations/{slug}/billing/subscription', {
|
|
params: { path: { slug: orgSlug } },
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
export type OrgSubscriptionData = Awaited<ReturnType<typeof getOrgSubscription>>
|
|
export type OrgSubscriptionError = ResponseError
|
|
|
|
export const useOrgSubscriptionQuery = <TData = OrgSubscriptionData>(
|
|
{ orgSlug }: OrgSubscriptionVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseQueryOptions<OrgSubscriptionData, OrgSubscriptionError, TData> = {}
|
|
) => {
|
|
// [Joshen] Thinking it makes sense to add this check at the RQ level - prevent
|
|
// unnecessary requests, although this behaviour still needs handling on the UI
|
|
const canReadSubscriptions = useCheckPermissions(
|
|
PermissionAction.BILLING_READ,
|
|
'stripe.subscriptions'
|
|
)
|
|
|
|
return useQuery<OrgSubscriptionData, OrgSubscriptionError, TData>(
|
|
subscriptionKeys.orgSubscription(orgSlug),
|
|
({ signal }) => getOrgSubscription({ orgSlug }, signal),
|
|
{
|
|
enabled: enabled && canReadSubscriptions && typeof orgSlug !== 'undefined',
|
|
staleTime: 60 * 60 * 1000, // 60 minutes
|
|
...options,
|
|
}
|
|
)
|
|
}
|
|
|
|
export const useHasAccessToProjectLevelPermissions = (slug: string) => {
|
|
const { data: subscription } = useOrgSubscriptionQuery({ orgSlug: slug })
|
|
return subscription?.plan.id === 'enterprise'
|
|
}
|