import type { TEnvelopeExpirationDurationPeriod, TEnvelopeExpirationPeriod, } from '@documenso/lib/constants/envelope-expiration'; import { Input } from '@documenso/ui/primitives/input'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@documenso/ui/primitives/select'; import { Plural, Trans } from '@lingui/react/macro'; type ExpirationMode = 'duration' | 'disabled' | 'inherit'; const getMode = (value: TEnvelopeExpirationPeriod | null | undefined): ExpirationMode => { if (!value) { return 'inherit'; } if ('disabled' in value) { return 'disabled'; } return 'duration'; }; const getAmount = (value: TEnvelopeExpirationPeriod | null | undefined): number => { if (value && 'amount' in value) { return value.amount; } return 1; }; const getUnit = (value: TEnvelopeExpirationPeriod | null | undefined): TEnvelopeExpirationDurationPeriod['unit'] => { if (value && 'unit' in value) { return value.unit; } return 'month'; }; export type ExpirationPeriodPickerProps = { value: TEnvelopeExpirationPeriod | null | undefined; onChange: (value: TEnvelopeExpirationPeriod | null) => void; disabled?: boolean; inheritLabel?: string; }; export const ExpirationPeriodPicker = ({ value, onChange, disabled = false, inheritLabel, }: ExpirationPeriodPickerProps) => { const mode = getMode(value); const amount = getAmount(value); const unit = getUnit(value); const onModeChange = (newMode: string) => { if (newMode === 'inherit') { onChange(null); return; } if (newMode === 'disabled') { onChange({ disabled: true }); return; } onChange({ unit, amount }); }; const onAmountChange = (newAmount: number) => { const clamped = Math.max(1, Math.floor(newAmount)); onChange({ unit, amount: clamped }); }; const onUnitChange = (newUnit: string) => { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions onChange({ unit: newUnit as TEnvelopeExpirationDurationPeriod['unit'], amount }); }; return (