Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: storage settings entitlements (#39879)

Ignacio Dobronich1f2bfb91ba0b
diff --git a/apps/studio/components/interfaces/DiskManagement/DiskManagementForm.tsx b/apps/studio/components/interfaces/DiskManagement/DiskManagementForm.tsx
index 19ab6c1881f..bd3756bd664 100644
--- a/apps/studio/components/interfaces/DiskManagement/DiskManagementForm.tsx
+++ b/apps/studio/components/interfaces/DiskManagement/DiskManagementForm.tsx
@@ -89,7 +89,7 @@ export function DiskManagementForm() {
},
})
- const { hasAccess, entitlementConfig } = useCheckEntitlements('instances.compute_update')
+ const { hasAccess } = useCheckEntitlements('instances.compute_update')
const [isDialogOpen, setIsDialogOpen] = useState<boolean>(false)
const [refetchInterval, setRefetchInterval] = useState<number | false>(false)
diff --git a/apps/studio/components/interfaces/Storage/StorageSettings/StorageSettings.tsx b/apps/studio/components/interfaces/Storage/StorageSettings/StorageSettings.tsx
index 431f40e6522..14254f3aef2 100644
--- a/apps/studio/components/interfaces/Storage/StorageSettings/StorageSettings.tsx
+++ b/apps/studio/components/interfaces/Storage/StorageSettings/StorageSettings.tsx
@@ -52,6 +52,7 @@ import {
StorageSizeUnits,
} from './StorageSettings.constants'
import { convertFromBytes, convertToBytes } from './StorageSettings.utils'
+import { useCheckEntitlements } from 'hooks/misc/useCheckEntitlements'
const formId = 'storage-settings-form'
@@ -87,6 +88,8 @@ export const StorageSettings = () => {
!!config && !config.capabilities.list_v2 && config.external.upstreamTarget === 'canary'
const { data: organization } = useSelectedOrganizationQuery()
+ const { getEntitlementNumericValue, isEntitlementUnlimited } =
+ useCheckEntitlements('storage.max_file_size')
const isFreeTier = organization?.plan.id === 'free'
const isSpendCapOn =
organization?.plan.id === 'pro' && organization?.usage_billing_enabled === false
@@ -109,14 +112,12 @@ export const StorageSettings = () => {
})
const maxBytes = useMemo(() => {
- if (organization?.plan.id === 'free') {
- return STORAGE_FILE_SIZE_LIMIT_MAX_BYTES_FREE_PLAN
- } else if (organization?.usage_billing_enabled) {
+ if (organization?.usage_billing_enabled || isEntitlementUnlimited()) {
return STORAGE_FILE_SIZE_LIMIT_MAX_BYTES_UNCAPPED
} else {
- return STORAGE_FILE_SIZE_LIMIT_MAX_BYTES_CAPPED
+ return getEntitlementNumericValue() ?? STORAGE_FILE_SIZE_LIMIT_MAX_BYTES_CAPPED
}
- }, [organization])
+ }, [organization, isEntitlementUnlimited, getEntitlementNumericValue])
const FormSchema = z
.object({
diff --git a/apps/studio/data/entitlements/entitlements-query.ts b/apps/studio/data/entitlements/entitlements-query.ts
index c06df0e7dc9..991539c6571 100644
--- a/apps/studio/data/entitlements/entitlements-query.ts
+++ b/apps/studio/data/entitlements/entitlements-query.ts
@@ -11,6 +11,7 @@ export type EntitlementsVariables = {
export type EntitlementConfig =
components['schemas']['ListEntitlementsResponse']['entitlements'][0]['config']
export type Entitlement = components['schemas']['ListEntitlementsResponse']['entitlements'][0]
+export type EntitlementType = Entitlement['type']
export async function getEntitlements({ slug }: EntitlementsVariables, signal?: AbortSignal) {
if (!slug) throw new Error('slug is required')
diff --git a/apps/studio/hooks/misc/useCheckEntitlements.ts b/apps/studio/hooks/misc/useCheckEntitlements.ts
index bf55549b277..e82dbe0573c 100644
--- a/apps/studio/hooks/misc/useCheckEntitlements.ts
+++ b/apps/studio/hooks/misc/useCheckEntitlements.ts
@@ -1,7 +1,57 @@
import { useEntitlementsQuery } from 'data/entitlements/entitlements-query'
import { useMemo } from 'react'
import { useSelectedOrganizationQuery } from './useSelectedOrganization'
-import type { EntitlementConfig } from 'data/entitlements/entitlements-query'
+import type {
+ Entitlement,
+ EntitlementConfig,
+ EntitlementType,
+} from 'data/entitlements/entitlements-query'
+
+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 isBooleanConfig(
+ config: EntitlementConfig,
+ type: EntitlementType
+): config is { enabled: boolean } {
+ return type === 'boolean'
+}
+
+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
+ : []
+}
export function useCheckEntitlements(
featureKey: string,
@@ -29,21 +79,19 @@ export function useCheckEntitlements(
isSuccess: isSuccessEntitlements,
} = useEntitlementsQuery({ slug: finalOrgSlug! }, { enabled })
- const { hasAccess, entitlementConfig } = useMemo((): {
- hasAccess: boolean
- entitlementConfig: EntitlementConfig
+ const { entitlement } = useMemo((): {
+ entitlement: Entitlement | null
} => {
// If no organization slug, no access
- if (!finalOrgSlug) return { hasAccess: false, entitlementConfig: { enabled: false } }
+ if (!finalOrgSlug) return { entitlement: null }
const entitlement = entitlementsData?.entitlements.find(
(entitlement) => entitlement.feature.key === featureKey
)
- const entitlementConfig = entitlement?.config ?? { enabled: false }
-
- if (!entitlement) return { hasAccess: false, entitlementConfig: { enabled: false } }
- return { hasAccess: entitlement.hasAccess, entitlementConfig }
+ return {
+ entitlement: entitlement ?? null,
+ }
}, [entitlementsData, featureKey, finalOrgSlug])
const isLoading = shouldGetSelectedOrg ? isLoadingSelectedOrg : isLoadingEntitlements
@@ -51,5 +99,12 @@ export function useCheckEntitlements(
? isSuccessSelectedOrg && isSuccessEntitlements
: isSuccessEntitlements
- return { hasAccess, entitlementConfig, isLoading, isSuccess }
+ return {
+ hasAccess: entitlement?.hasAccess ?? false,
+ isLoading,
+ isSuccess,
+ getEntitlementNumericValue: () => getEntitlementNumericValue(entitlement),
+ isEntitlementUnlimited: () => isEntitlementUnlimited(entitlement),
+ getEntitlementSetValues: () => getEntitlementSetValues(entitlement),
+ }
}