import { Fragment, ReactNode } from 'react' import { Tooltip, TooltipContent, TooltipTrigger } from 'ui' import { Admonition } from 'ui-patterns/Admonition' import { formatPermissionName, groupScopesByLevel } from './Consent/OAuthAppsAuthorizeScreen.utils' import { OAuthGrantAccordionItem } from './OAuthGrantAccordionItem' import { TOOLTIP_UNDERLINE_CLASS_NAME } from '@/components/ui/tooltip-styles' import { DATABASE_TOKEN_ACCESS_PERMISSION, getOAuthDatabaseTokenAccess, } from '@/data/oauth-apps/types' import { pluralize } from '@/lib/helpers' export function OAuthGrantAccess({ id, title, scopes, projects, icon, expanded = false, }: { id: string title: string scopes: string[] projects?: { name: string; ref: string }[] expanded?: boolean icon: ReactNode }) { const databaseAccess = getOAuthDatabaseTokenAccess(scopes) const groups = groupScopesByLevel( databaseAccess === 'write' ? [...scopes, `${DATABASE_TOKEN_ACCESS_PERMISSION}:read`] : scopes ) const canRunSqlAsPostgres = scopes.includes('database:write') const levels = [ { label: 'Read and write', permissions: groups['read-write'] }, { label: 'Write', permissions: groups.write }, { label: 'Read only', permissions: groups.read }, ] const count = levels.reduce((total, { permissions }) => total + permissions.length, 0) const projectSummary = projects == null ? 'All projects' : `${projects.length} ${pluralize(projects.length, 'project')}` const details = (

Projects

{projects && projects.length > 0 ? (
{projects.map((project) => (
{project.name}
{project.ref}
))}
) : (

{projects ? 'No projects included.' : 'All current and future projects.'}

)}

Permissions

{count > 0 && (
{levels .filter(({ permissions }) => permissions.length > 0) .map(({ label, permissions }) => (

{label}

{permissions.map((permission, index) => ( {index > 0 && , } ))}

))}
)} {(databaseAccess || canRunSqlAsPostgres) && (

These permissions allow this app to:

    {databaseAccess &&
  • Read rows that your access rules normally hide
  • } {databaseAccess === 'write' &&
  • Change data and database structure
  • } {canRunSqlAsPostgres && (
  • Run SQL as postgres and reset the database password
  • )}
} /> )} {count === 0 &&

No permissions granted.

} ) if (expanded) return (

{title}

{details}
) return ( {details} ) } function PermissionLabel({ permission }: { permission: string }) { const descriptions: Record = { database: 'Access database settings. Write access also allows the app to run SQL.', environment: 'Access project branches.', [DATABASE_TOKEN_ACCESS_PERMISSION]: 'Connect to Postgres as the app’s database role with an access token.', } const label = permission === DATABASE_TOKEN_ACCESS_PERMISSION ? 'Database token access' : formatPermissionName(permission) if (!descriptions[permission]) return label return ( {label} {descriptions[permission]} ) }