Files
supabase/apps/studio/components/interfaces/Organization/OAuthApps/PublishAppSidePanel/DirectDatabaseAccess.tsx

87 lines
3.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<SetStateAction<OAuthScope[]>>
}
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 (
<div
id={DIRECT_DATABASE_ACCESS_ID}
className="grid grid-cols-1 sm:grid-cols-[minmax(0,1fr)_10rem] items-start gap-3 p-4"
>
<div className="flex min-w-0 flex-col">
<p className="text-sm font-medium text-foreground">Database token access</p>
<p className="text-sm text-foreground-lighter">{description}</p>
</div>
<div className={isMemberBound ? 'cursor-not-allowed' : undefined}>
<AccessLevelDropdown
invalid={hasScopeError}
describedBy={hasScopeError ? errorId : undefined}
permission="Database token access"
levels={accessLevels}
value={accessLevel}
disabled={isMemberBound}
disabledLevels={isEligible ? [] : ['Read only', 'Read and write']}
onChange={handleAccessChange}
/>
</div>
<p
id={errorId}
role="alert"
className={cn('text-sm text-destructive sm:col-span-2', !hasScopeError && 'sr-only')}
>
{hasScopeError &&
'This permission also requires “Database management” to have at least read access.'}
</p>
</div>
)
}