import dayjs from 'dayjs' import type { Control } from 'react-hook-form' import { Checkbox, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, WarningIcon, } from 'ui' import { TimestampInfo } from 'ui-patterns' import { Admonition } from 'ui-patterns/admonition' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import { SingleValueFieldArray } from 'ui-patterns/form/SingleValueFieldArray/SingleValueFieldArray' import type { JitRoleGrantDraft, JitRoleOption, JitUserRuleDraft } from './JitDbAccess.types' import { createEmptyIpRange, getRelativeDatetimeByMode } from './JitDbAccess.utils' import { DatePicker } from '@/components/ui/DatePicker' import { InlineLink } from '@/components/ui/InlineLink' import { DOCS_URL } from '@/lib/constants' const EXPIRY_MODE_OPTIONS: Array<{ value: JitRoleGrantDraft['expiryMode']; label: string }> = [ { value: '1h', label: '1 hour' }, { value: '1d', label: '1 day' }, { value: '7d', label: '7 days' }, { value: '30d', label: '30 days' }, { value: 'custom', label: 'Custom' }, { value: 'never', label: 'Never' }, ] const MAX_CUSTOM_EXPIRY_YEARS = 1 const BRANCH_SCOPE_OPTIONS = [ { value: 'all', label: 'All project databases' }, { value: 'preview', label: 'Preview branches only' }, ] as const interface JitDbAccessRoleGrantFieldsProps { control: Control grantIndex: number role: JitRoleOption grant: JitRoleGrantDraft onChange: (next: JitRoleGrantDraft) => void } export function JitDbAccessRoleGrantFields({ control, grantIndex, role, grant, onChange, }: JitDbAccessRoleGrantFieldsProps) { const isSuperuserRole = role.id === 'postgres' const isReadOnlyRole = role.id === 'briven_read_only_user' const checkboxId = `jit-role-${role.id}` return (
{grant.enabled && (
{isSuperuserRole && ( Consider using a{' '} custom Postgres role {' '} with only the permissions required. } /> )} {isReadOnlyRole && ( Consider using a{' '} custom Postgres role {' '} with only the permissions required. } className="mb-3" /> )} {grant.branchesOnly ? 'Can only be requested from preview branch databases.' : 'Can be requested from production and preview branch databases.'}

} >
Expires at{' '}

) : grant.expiryMode === 'never' ? (
No expiry means ongoing database access until manually revoked.
) : undefined } >
{grant.expiryMode === 'custom' && ( { const selectedDate = value.to || value.from || '' onChange({ ...grant, hasExpiry: true, expiry: selectedDate, }) }} triggerButtonClassName="min-w-[120px]" > {grant.expiry ? dayjs(grant.expiry).format('DD MMM, HH:mm') : 'Select date'} )}
Restricted IP addresses{' '} (optional)

} >
)}
) }