import { useParams } from 'common' import { EllipsisVerticalIcon, PencilIcon, PlusIcon, Trash2Icon } from 'lucide-react' import { Badge, Button, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, Skeleton, Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from 'ui' import type { JitUserRule } from './JitDbAccess.types' import { getJitStatusDisplay } from './JitDbAccess.utils' import { JitDbAccessInfo } from './JitDbAccessInfo' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' import { useJitDbAccessStatusQuery } from '@/data/jit-db-access/jit-db-access-status-query' import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2' interface JitDbAccessRulesTableProps { users: JitUserRule[] isLoading?: boolean canUpdate: boolean disableActions?: boolean allProjectMembersHaveRules?: boolean onAddRule: () => void onEditRule: (user: JitUserRule) => void onDeleteRule: (user: JitUserRule) => void } export function JitDbAccessRulesTable({ users, isLoading = false, canUpdate, disableActions = false, allProjectMembersHaveRules = false, onAddRule, onEditRule, onDeleteRule, }: JitDbAccessRulesTableProps) { const { ref: projectRef } = useParams() const addDisabled = disableActions || !canUpdate || allProjectMembersHaveRules const addRuleTooltip = !canUpdate ? 'Additional permissions required' : allProjectMembersHaveRules ? 'All project members already have access rules' : undefined const { can: canManageJitDbAccess } = useAsyncCheckPermissionsV2( FGA_PERMISSIONS.PROJECT.DATABASE_JIT_WRITE ) const { data: jitDbAccess, isPending: isLoadingUserJitDbAccess } = useJitDbAccessStatusQuery({ projectRef, }) if (isLoading || isLoadingUserJitDbAccess) { return (
) } return ( <>
Members Members with token access to this database.
{canManageJitDbAccess && ( } onClick={onAddRule} disabled={addDisabled} tooltip={{ content: { side: 'bottom', text: addRuleTooltip } }} > Add member )}
{canManageJitDbAccess && ( Member Roles Status Actions {users.length === 0 ? (

No rules yet

Add a member access rule to grant a database role

) : ( users.map((user) => { const statusDisplay = getJitStatusDisplay(user.status) const enabledGrants = user.grants.filter((grant) => grant.enabled) const rowIsInteractive = canUpdate && !disableActions return ( { if ((event.target as HTMLElement).closest('button')) return onEditRule(user) } : undefined } onKeyDown={ rowIsInteractive ? (event) => { if ((event.target as HTMLElement).closest('button')) return if (event.key === 'Enter' || event.key === ' ') { event.preventDefault() onEditRule(user) } } : undefined } tabIndex={rowIsInteractive ? 0 : undefined} > {user.name &&

{user.name}

}

{user.email}

{enabledGrants.length} role {enabledGrants.length === 1 ? '' : s} {statusDisplay.badges.length > 0 ? ( {statusDisplay.badges.map((badge) => ( {badge.label} ))} ) : null}
)}
{!canManageJitDbAccess && jitDbAccess?.user_id !== undefined && (

You need additional permissions to manage member access

)} ) }