ruvector/studio/hooks/misc/useCheckEntitlements.ts
rUv 814f595995 feat(studio): Add complete RuVector Studio application
Major additions:
- Complete Next.js studio application with 1600+ components
- Docker support (Dockerfile.combined, docker-compose.yml)
- GCP deployment documentation and benchmarks
- SQL benchmark scripts for performance testing
- Sentry integration for monitoring
- Comprehensive test suite and mocks

Studio features:
- Dashboard and admin interfaces
- Data visualization components
- Authentication and user management
- API integration with RuVector backend
- Static data and public assets

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-06 23:04:48 +00:00

111 lines
3.4 KiB
TypeScript

import { useEntitlementsQuery } from 'data/entitlements/entitlements-query'
import { useMemo } from 'react'
import { useSelectedOrganizationQuery } from './useSelectedOrganization'
import type {
Entitlement,
EntitlementConfig,
EntitlementType,
} 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 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,
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,
isLoading: isLoadingSelectedOrg,
isSuccess: isSuccessSelectedOrg,
} = useSelectedOrganizationQuery({
enabled: shouldGetSelectedOrg,
})
const finalOrgSlug = organizationSlug || selectedOrg?.slug
const enabled = IS_PLATFORM ? options?.enabled !== false && !!finalOrgSlug : false
const {
data: entitlementsData,
isLoading: 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
const isSuccess = shouldGetSelectedOrg
? isSuccessSelectedOrg && isSuccessEntitlements
: isSuccessEntitlements
return {
hasAccess: IS_PLATFORM ? entitlement?.hasAccess ?? false : true,
isLoading,
isSuccess,
getEntitlementNumericValue: () => getEntitlementNumericValue(entitlement),
isEntitlementUnlimited: () => isEntitlementUnlimited(entitlement),
getEntitlementSetValues: () => getEntitlementSetValues(entitlement),
}
}