Skip to content

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

feat: enable/disable dashboard features (#37921)

Alaister Young915ba4bd30ed
diff --git a/apps/studio/components/interfaces/UserDropdown.tsx b/apps/studio/components/interfaces/UserDropdown.tsx
index f9dc116ecfd..2a23a8950a0 100644
--- a/apps/studio/components/interfaces/UserDropdown.tsx
+++ b/apps/studio/components/interfaces/UserDropdown.tsx
@@ -4,6 +4,7 @@ import Link from 'next/link'
import { useRouter } from 'next/router'
import { ProfileImage } from 'components/ui/ProfileImage'
+import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled'
import { useSignOut } from 'lib/auth'
import { IS_PLATFORM } from 'lib/constants'
import { useProfile } from 'lib/profile'
@@ -33,6 +34,7 @@ export function UserDropdown() {
const appStateSnapshot = useAppStateSnapshot()
const setCommandMenuOpen = useSetCommandMenuOpen()
const { openFeaturePreviewModal } = useFeaturePreviewModal()
+ const profileShowEmailEnabled = useIsFeatureEnabled('profile:show_email')
return (
<DropdownMenu>
@@ -61,7 +63,7 @@ export function UserDropdown() {
>
{profile.username}
</span>
- {profile.primary_email !== profile.username && (
+ {profile.primary_email !== profile.username && profileShowEmailEnabled && (
<span
title={profile.primary_email}
className="w-full text-left text-foreground-light text-xs truncate"
diff --git a/apps/studio/data/profile/types.ts b/apps/studio/data/profile/types.ts
index 4b756ae7e08..0759884e697 100644
--- a/apps/studio/data/profile/types.ts
+++ b/apps/studio/data/profile/types.ts
@@ -3,5 +3,3 @@ import type { components } from 'data/api'
export type Profile = components['schemas']['ProfileResponse'] & {
profileImageUrl?: string
}
-
-export type Feature = Profile['disabled_features'][number]
diff --git a/apps/studio/hooks/misc/useIsFeatureEnabled.ts b/apps/studio/hooks/misc/useIsFeatureEnabled.ts
index 2542fb5ddb3..6811deba42c 100644
--- a/apps/studio/hooks/misc/useIsFeatureEnabled.ts
+++ b/apps/studio/hooks/misc/useIsFeatureEnabled.ts
@@ -1,43 +1,21 @@
-import type { Feature } from 'data/profile/types'
+import { isFeatureEnabled, type Feature } from 'common'
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
+): ReturnType<typeof isFeatureEnabled<T>>
+function useIsFeatureEnabled(features: Feature): ReturnType<typeof isFeatureEnabled>
function useIsFeatureEnabled<T extends Feature | Feature[]>(features: T) {
const { profile } = useProfile()
+ const disabledFeatures = profile?.disabled_features
+ // This code branch is to make the type checker happy, it's intentionally
+ // the same as the isFeatureEnabled function call below.
if (Array.isArray(features)) {
- return Object.fromEntries(
- features.map((feature) => [
- featureToCamelCase(feature),
- checkFeature(feature, profile?.disabled_features),
- ])
- )
+ return isFeatureEnabled(features, disabledFeatures)
}
- return checkFeature(features, profile?.disabled_features)
+ return isFeatureEnabled(features, disabledFeatures)
}
export { useIsFeatureEnabled }
diff --git a/packages/common/enabled-features/enabled-features.json b/packages/common/enabled-features/enabled-features.json
new file mode 100644
index 00000000000..523eda6e323
--- /dev/null
+++ b/packages/common/enabled-features/enabled-features.json
@@ -0,0 +1,6 @@
+{
+ "$schema": "./enabled-features.schema.json",
+
+ "profile:show_email": true,
+ "profile:update": true
+}
diff --git a/packages/common/enabled-features/enabled-features.schema.json b/packages/common/enabled-features/enabled-features.schema.json
new file mode 100644
index 00000000000..bef56000407
--- /dev/null
+++ b/packages/common/enabled-features/enabled-features.schema.json
@@ -0,0 +1,20 @@
+{
+ "$schema": "http://json-schema.org/draft-07/schema#",
+ "type": "object",
+ "properties": {
+ "$schema": {
+ "type": "string"
+ },
+
+ "profile:show_email": {
+ "type": "boolean",
+ "description": "Show the user's email address in the toolbar"
+ },
+ "profile:update": {
+ "type": "boolean",
+ "description": "Allow the user to change their profile information (first name, last name)"
+ }
+ },
+ "required": ["profile:show_email", "profile:update"],
+ "additionalProperties": false
+}
diff --git a/packages/common/enabled-features/index.ts b/packages/common/enabled-features/index.ts
new file mode 100644
index 00000000000..ec5987eb6dd
--- /dev/null
+++ b/packages/common/enabled-features/index.ts
@@ -0,0 +1,60 @@
+import type { components } from 'api-types'
+import enabledFeaturesRaw from './enabled-features.json'
+
+const enabledFeaturesStaticObj = enabledFeaturesRaw as Omit<typeof enabledFeaturesRaw, '$schema'>
+
+type Profile = components['schemas']['ProfileResponse']
+
+export type Feature = Profile['disabled_features'][number] | keyof typeof enabledFeaturesStaticObj
+
+const disabledFeaturesStaticArray = Object.entries(enabledFeaturesStaticObj)
+ .filter(([_, value]) => !value)
+ .map(([key]) => key as Feature)
+
+function checkFeature(feature: Feature, features: Set<Feature>) {
+ return !features.has(feature)
+}
+
+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 isFeatureEnabled<T extends Feature[]>(
+ features: T,
+ runtimeDisabledFeatures?: Feature[]
+): { [key in FeatureToCamelCase<T[number]>]: boolean }
+function isFeatureEnabled(features: Feature, runtimeDisabledFeatures?: Feature[]): boolean
+function isFeatureEnabled<T extends Feature | Feature[]>(
+ features: T,
+ runtimeDisabledFeatures?: Feature[]
+) {
+ const disabledFeatures = new Set([
+ ...(runtimeDisabledFeatures ?? []),
+ ...disabledFeaturesStaticArray,
+ ])
+
+ if (Array.isArray(features)) {
+ return Object.fromEntries(
+ features.map((feature) => [
+ featureToCamelCase(feature),
+ checkFeature(feature, disabledFeatures),
+ ])
+ )
+ }
+
+ return checkFeature(features, disabledFeatures)
+}
+
+export { isFeatureEnabled }
diff --git a/packages/common/index.tsx b/packages/common/index.tsx
index 863f4ebbc46..84a0675e8a4 100644
--- a/packages/common/index.tsx
+++ b/packages/common/index.tsx
@@ -2,10 +2,11 @@ export * from './auth'
export * from './consent-state'
export * from './constants'
export * from './database-types'
+export * from './enabled-features'
+export * from './feature-flags'
export * from './gotrue'
export * from './helpers'
export * from './hooks'
export * from './MetaFavicons/pages-router'
export * from './Providers'
export * from './telemetry'
-export * from './feature-flags'
diff --git a/packages/common/tsconfig.json b/packages/common/tsconfig.json
index cd6c94d6e8b..6bfd004e4e6 100644
--- a/packages/common/tsconfig.json
+++ b/packages/common/tsconfig.json
@@ -1,5 +1,8 @@
{
"extends": "tsconfig/react-library.json",
"include": ["."],
- "exclude": ["dist", "build", "node_modules"]
+ "exclude": ["dist", "build", "node_modules"],
+ "compilerOptions": {
+ "resolveJsonModule": true
+ }
}