Files
supabase/apps/studio/hooks/misc/useCheckEntitlements.ts
Ali Waseem 3fcf980b0a fix(studio): batch of production Sentry crash fixes (array/null guards) (#47460)
Fixes a batch of production Studio crashes from Sentry (all caught by
the global error boundary). Most are missing array/null guards where an
endpoint typed as an array — or with a nested array field — returned a
non-array body in production; a few are one-off render crashes.

Resolves FE-3748.

## Issues fixed

| Sentry | Error | Fix |
| --- | --- | --- |
| [J7R](https://supabase.sentry.io/issues/7492997940/) | Maximum update
depth exceeded | Disable RadialBar animation in disk-cooldown countdown
|
| [JR5](https://supabase.sentry.io/issues/7548484681/) |
resourceWarnings.find is not a function | Guard in
ResourceExhaustionWarningBanner |
| [JCJ](https://supabase.sentry.io/issues/7506024989/) |
resourceWarnings.find is not a function | Guard in ProjectLayout +
normalize query |
| [K1Y](https://supabase.sentry.io/issues/7584792331/) | snippet.name on
undefined | Optional-chain SQL editor download filename |
| [B3K](https://supabase.sentry.io/issues/7141649636/) |
pagination.count on undefined | Guard pagination in projects infinite
query |
| [JVP](https://supabase.sentry.io/issues/7560437621/) | schemas.some /
extensions.find | Coerce pg-meta lists to arrays in
useInstalledIntegrations |
| [JR2](https://supabase.sentry.io/issues/7548339272/) | extensions.find
is not a function | (same fix as JVP) |
| [JQR](https://supabase.sentry.io/issues/7547163939/) | lints.filter is
not a function | Normalize project lints query |
| [JR3](https://supabase.sentry.io/issues/7548433501/) |
entitlements.find is not a function | Guard call sites + normalize
entitlements query |
| [JQS](https://supabase.sentry.io/issues/7547557098/) |
selected_addons.find is not a function | Normalize addons query arrays |


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Bug Fixes**
* Improved stability across several Studio screens by handling missing
or unexpected data more safely.
* Downloads now use a fallback name when a snippet name isn’t available.
* Project, entitlement, schema, addon, warning, and extension views are
less likely to break when data is missing or not in the expected format.
* Pagination and countdown visuals now behave more consistently, with
reduced chance of runtime errors or animation-related glitches.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2026-07-02 08:38:17 +00:00

136 lines
4.2 KiB
TypeScript

import { useCallback, useMemo } from 'react'
import { useSelectedOrganizationQuery } from './useSelectedOrganization'
import type {
Entitlement,
EntitlementConfig,
EntitlementType,
FeatureKey,
} from '@/data/entitlements/entitlements-query'
import { useEntitlementsQuery } from '@/data/entitlements/entitlements-query'
import { IS_PLATFORM } from '@/lib/constants'
function isNumericConfig(
_config: EntitlementConfig,
type: EntitlementType
): _config is { enabled: boolean; unlimited: boolean; value: number } {
return type === 'numeric'
}
function isSetConfig(
_config: EntitlementConfig,
type: EntitlementType
): _config is { enabled: boolean; set: string[] } {
return type === 'set'
}
function getEntitlementNumericValue(entitlement: Entitlement | null): number | undefined {
const entitlementConfig = entitlement?.config
return entitlementConfig &&
entitlement.type &&
isNumericConfig(entitlementConfig, entitlement.type)
? entitlementConfig.value
: undefined
}
function isEntitlementUnlimited(entitlement: Entitlement | null): boolean {
const entitlementConfig = entitlement?.config
return entitlementConfig &&
entitlement.type &&
isNumericConfig(entitlementConfig, entitlement.type)
? entitlementConfig.unlimited
: false
}
function getEntitlementSetValues(entitlement: Entitlement | null): string[] {
const entitlementConfig = entitlement?.config
return entitlementConfig && entitlement.type && isSetConfig(entitlementConfig, entitlement.type)
? entitlementConfig.set
: []
}
function getEntitlementMax(entitlement: Entitlement | null): number | undefined {
return isEntitlementUnlimited(entitlement)
? Number.MAX_SAFE_INTEGER
: getEntitlementNumericValue(entitlement)
}
export function useHasEntitlementAccess(organizationSlug?: string) {
const shouldGetSelectedOrg = !organizationSlug
const { data: selectedOrg } = useSelectedOrganizationQuery({
enabled: shouldGetSelectedOrg,
})
const finalOrgSlug = organizationSlug || selectedOrg?.slug
const enabled = IS_PLATFORM && !!finalOrgSlug
const { data: entitlementsData } = useEntitlementsQuery({ slug: finalOrgSlug! }, { enabled })
return useCallback(
(key: string) =>
IS_PLATFORM
? (entitlementsData?.entitlements?.find((e) => e.feature.key === key)?.hasAccess ?? false)
: true,
[entitlementsData]
)
}
export function useCheckEntitlements(
featureKey: FeatureKey,
organizationSlug?: string,
options?: {
enabled?: boolean
}
) {
// If no organizationSlug provided, try to get it from the selected organization
const shouldGetSelectedOrg = !organizationSlug && options?.enabled !== false
const {
data: selectedOrg,
isPending: isLoadingSelectedOrg,
isSuccess: isSuccessSelectedOrg,
} = useSelectedOrganizationQuery({
enabled: shouldGetSelectedOrg,
})
const finalOrgSlug = organizationSlug || selectedOrg?.slug
const enabled = IS_PLATFORM ? options?.enabled !== false && !!finalOrgSlug : false
const {
data: entitlementsData,
isPending: isLoadingEntitlements,
isSuccess: isSuccessEntitlements,
} = useEntitlementsQuery({ slug: finalOrgSlug! }, { enabled })
const { entitlement } = useMemo((): {
entitlement: Entitlement | null
} => {
// If no organization slug, no access
if (!finalOrgSlug) return { entitlement: null }
const entitlement = entitlementsData?.entitlements?.find(
(entitlement) => entitlement.feature.key === featureKey
)
return {
entitlement: entitlement ?? null,
}
}, [entitlementsData, featureKey, finalOrgSlug])
const isLoading = shouldGetSelectedOrg
? isLoadingSelectedOrg || isLoadingEntitlements
: isLoadingEntitlements
const isSuccess = shouldGetSelectedOrg
? isSuccessSelectedOrg && isSuccessEntitlements
: isSuccessEntitlements
return {
hasAccess: IS_PLATFORM ? (entitlement?.hasAccess ?? false) : true,
isLoading: IS_PLATFORM ? isLoading : false,
isSuccess: IS_PLATFORM ? isSuccess : true,
getEntitlementNumericValue: () => getEntitlementNumericValue(entitlement),
isEntitlementUnlimited: () => isEntitlementUnlimited(entitlement),
getEntitlementSetValues: () => getEntitlementSetValues(entitlement),
getEntitlementMax: () => getEntitlementMax(entitlement),
}
}