import { OAuthScope } from '@supabase/shared-types/out/constants' import { useId, type Dispatch, type SetStateAction } from 'react' import { cn } from 'ui' import { isDatabaseManagementReadRequired } from '../OAuthApps.utils' import { AccessLevelDropdown, type AccessLevel } from './AccessLevelDropdown' const accessLevels: AccessLevel[] = ['No access', 'Read only', 'Read and write'] export const DIRECT_DATABASE_ACCESS_ID = 'direct-database-access' interface DirectDatabaseAccessProps { isMemberBound: boolean isEligible: boolean scopes: OAuthScope[] setScopes: Dispatch> } export function DirectDatabaseAccess({ isMemberBound, isEligible, scopes, setScopes, }: DirectDatabaseAccessProps) { const errorId = useId() const hasScopeError = isDatabaseManagementReadRequired(scopes) let accessLevel: AccessLevel = 'No access' if (scopes.includes(OAuthScope.DATABASE_TOKEN_ACCESS_READ)) accessLevel = 'Read only' if (scopes.includes(OAuthScope.DATABASE_TOKEN_ACCESS_WRITE)) accessLevel = 'Read and write' let description = 'Connect to Postgres as your app’s database role with its access token.' if (isMemberBound) { description = 'This app uses each person’s permissions. To request database token access, use an app that an organization owner approves for the whole organization.' } else if (!isEligible) { description = 'Database token access is unavailable for this organization. Remove this permission to save changes.' } const handleAccessChange = (level: AccessLevel) => { setScopes((current) => { const next: OAuthScope[] = current.filter( (scope) => scope !== OAuthScope.DATABASE_TOKEN_ACCESS_READ && scope !== OAuthScope.DATABASE_TOKEN_ACCESS_WRITE ) if (level === 'Read only' || level === 'Read and write') { next.push(OAuthScope.DATABASE_TOKEN_ACCESS_READ) } if (level === 'Read and write') next.push(OAuthScope.DATABASE_TOKEN_ACCESS_WRITE) return next }) } return (

Database token access

{description}

) }