Files

150 lines
5.2 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 {
AlertDialog,
AlertDialogAction,
AlertDialogBody,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
DialogSectionSeparator,
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
import {
DialogDisclosure,
DialogDisclosureContent,
DialogDisclosureTrigger,
} from 'ui-patterns/DialogDisclosure'
import type { JitUserRule } from './JitDbAccess.types'
import type { OAuthProjectDatabaseAccessData } from '@/data/oauth-apps/oauth-apps-project-database-access-query'
interface JitDbAccessToggleDialogProps {
nextEnabled: boolean | null
users: JitUserRule[]
apps: OAuthProjectDatabaseAccessData
canConfirm: boolean
canListApps: boolean
isSubmitting: boolean
error: string | null
onClose: () => void
onConfirm: () => Promise<void>
}
export function JitDbAccessToggleDialog({
nextEnabled,
users,
apps,
canConfirm,
canListApps,
isSubmitting,
error,
onClose,
onConfirm,
}: JitDbAccessToggleDialogProps) {
const isEnabling = nextEnabled === true
const memberCount = users.length
const appCount = apps.length
const members = `${memberCount} ${memberCount === 1 ? 'member' : 'members'}`
const affected = [
...(memberCount > 0 ? [members] : []),
...(appCount > 0 ? [`${appCount} authorized ${appCount === 1 ? 'app' : 'apps'}`] : []),
].join(' and ')
const appsNotice = canListApps
? ''
: ' Authorized apps couldn’t be loaded, so any apps using token access aren’t listed here.'
const enableDescription =
(affected
? `This lets ${affected} open new database connections using their existing access. Apps may need to finish database setup first.`
: 'Members with access rules and approved apps will be able to connect using access tokens.') +
appsNotice
const disableDescription =
(affected
? `${affected} will no longer be able to connect using access tokens. Existing connections will stay open.`
: 'Members and apps will no longer be able to connect using access tokens. Existing connections will stay open.') +
appsNotice
return (
<AlertDialog open={nextEnabled !== null} onOpenChange={(open) => !open && onClose()}>
<AlertDialogContent size="small">
<AlertDialogHeader>
<AlertDialogTitle>
{isEnabling ? 'Enable token access' : 'Disable token access'}
</AlertDialogTitle>
<AlertDialogDescription>
{isEnabling ? enableDescription : disableDescription}
</AlertDialogDescription>
</AlertDialogHeader>
{isEnabling && (memberCount > 0 || appCount > 0) && (
<>
<DialogSectionSeparator />
<DialogDisclosure>
<DialogDisclosureTrigger className="px-5 py-4">
Who will regain access?
</DialogDisclosureTrigger>
<DialogDisclosureContent>
<div className="mb-2 max-h-64 overflow-y-auto px-5 pt-1">
<Table>
<TableHeader className="[&>tr]:bg-transparent">
<TableRow>
<TableHead className="pl-0">Member or app</TableHead>
<TableHead className="pr-0">Access</TableHead>
</TableRow>
</TableHeader>
<TableBody className="[:where(&)>tr]:hover:bg-transparent">
{users.map((member) => (
<TableRow key={member.id}>
<TableCell className="pl-0 whitespace-normal break-all">
{member.name}
<p className="text-foreground-lighter">{member.email}</p>
</TableCell>
<TableCell className="pr-0">Member rule</TableCell>
</TableRow>
))}
{apps.map((app) => (
<TableRow key={app.id}>
<TableCell className="pl-0">{app.name}</TableCell>
<TableCell className="pr-0">
{app.access === 'write' ? 'Read and write' : 'Read only'}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
</DialogDisclosureContent>
</DialogDisclosure>
</>
)}
{error && (
<AlertDialogBody>
<Admonition
type="destructive"
title={`Unable to ${isEnabling ? 'enable' : 'disable'} database token access`}
description={error}
/>
</AlertDialogBody>
)}
<AlertDialogFooter>
<AlertDialogCancel disabled={isSubmitting}>Cancel</AlertDialogCancel>
<AlertDialogAction
variant={isEnabling ? 'primary' : 'danger'}
loading={isSubmitting}
disabled={!canConfirm}
onClick={onConfirm}
>
{isEnabling ? 'Enable' : 'Disable'}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)
}