import { P, match } from 'ts-pattern'; import { DocumentAuth, type TRecipientActionAuth, type TRecipientActionAuthTypes, } from '@documenso/lib/types/document-auth'; import type { FieldType } from '@documenso/prisma/client'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@documenso/ui/primitives/dialog'; import { DocumentActionAuth2FA } from './document-action-auth-2fa'; import { DocumentActionAuthAccount } from './document-action-auth-account'; import { DocumentActionAuthPasskey } from './document-action-auth-passkey'; import { useRequiredDocumentAuthContext } from './document-auth-provider'; export type DocumentActionAuthDialogProps = { title?: string; documentAuthType: TRecipientActionAuthTypes; description?: string; actionTarget: FieldType | 'DOCUMENT'; open: boolean; onOpenChange: (value: boolean) => void; /** * The callback to run when the reauth form is filled out. */ onReauthFormSubmit: (values?: TRecipientActionAuth) => Promise | void; }; export const DocumentActionAuthDialog = ({ title, description, documentAuthType, open, onOpenChange, onReauthFormSubmit, }: DocumentActionAuthDialogProps) => { const { recipient, user, isCurrentlyAuthenticating } = useRequiredDocumentAuthContext(); const handleOnOpenChange = (value: boolean) => { if (isCurrentlyAuthenticating) { return; } onOpenChange(value); }; return ( {title || 'Sign field'} {description || 'Reauthentication is required to sign this field'} {match({ documentAuthType, user }) .with( { documentAuthType: DocumentAuth.ACCOUNT }, { user: P.when((user) => !user || user.email !== recipient.email) }, // Assume all current auth methods requires them to be logged in. () => , ) .with({ documentAuthType: DocumentAuth.PASSKEY }, () => ( )) .with({ documentAuthType: DocumentAuth.TWO_FACTOR_AUTH }, () => ( )) .with({ documentAuthType: DocumentAuth.EXPLICIT_NONE }, () => null) .exhaustive()} ); };