Files
supabase/apps/studio/hooks/misc/useIsFeatureEnabled.ts
T
Ivan Vasilov 436bdb10ae chore: Move the studio app to apps/studio (#18915)
* Move all studio files from /studio to /apps/studio.

* Move studio specific prettier ignores.

* Fix the ui references from studio.

* Fix the css imports.

* Fix all package.json issues.

* Fix the prettier setup for the studio app.

* Add .turbo folder to prettierignore.

* Fix the github workflows.
2023-11-15 12:38:55 +01:00

44 lines
1.3 KiB
TypeScript

import { Feature } from 'data/profile/types'
import { useProfile } from 'lib/profile'
function checkFeature(feature: Feature, features?: Feature[]) {
return !features?.includes(feature) ?? true
}
type SnakeToCamelCase<S extends string> = S extends `${infer First}_${infer Rest}`
? `${First}${SnakeToCamelCase<Capitalize<Rest>>}`
: S
type FeatureToCamelCase<S extends Feature> = S extends `${infer P}:${infer R}`
? `${SnakeToCamelCase<P>}${Capitalize<SnakeToCamelCase<R>>}`
: SnakeToCamelCase<S>
function featureToCamelCase(feature: Feature) {
return feature
.replace(/:/g, '_')
.split('_')
.map((word, index) => (index === 0 ? word : word[0].toUpperCase() + word.slice(1)))
.join('') as FeatureToCamelCase<typeof feature>
}
function useIsFeatureEnabled<T extends Feature[]>(
features: T
): { [key in FeatureToCamelCase<T[number]>]: boolean }
function useIsFeatureEnabled(features: Feature): boolean
function useIsFeatureEnabled<T extends Feature | Feature[]>(features: T) {
const { profile } = useProfile()
if (Array.isArray(features)) {
return Object.fromEntries(
features.map((feature) => [
featureToCamelCase(feature),
checkFeature(feature, profile?.disabled_features),
])
)
}
return checkFeature(features, profile?.disabled_features)
}
export { useIsFeatureEnabled }