import { downloadPDF } from '@documenso/lib/client-only/download-pdf'; import { trpc } from '@documenso/trpc/react'; import { Button } from '@documenso/ui/primitives/button'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from '@documenso/ui/primitives/dialog'; import { Skeleton } from '@documenso/ui/primitives/skeleton'; import { useToast } from '@documenso/ui/primitives/use-toast'; import { Trans, useLingui } from '@lingui/react/macro'; import { DocumentStatus, type EnvelopeItem } from '@prisma/client'; import { DownloadIcon, FileTextIcon } from 'lucide-react'; import { useMemo, useState } from 'react'; type EnvelopeItemToDownload = Pick; type EnvelopeDownloadDialogProps = { envelopeId: string; envelopeStatus: DocumentStatus; /** * Whether the envelope is a legacy (v1) envelope. Only consulted to gate the * partial-download variant: legacy envelopes use a different field-rendering * pipeline that the partial PDF helper does not implement, so the Partial * button is hidden for them. * * Optional: omit it on call sites where the status can never be PENDING (DRAFT, * COMPLETED, REJECTED) or when a recipient token is set, since the Partial button * is also gated on those. Pass it from team-side call sites that can render the * dialog for a PENDING envelope. */ isLegacy?: boolean; envelopeItems?: EnvelopeItemToDownload[]; /** * The recipient token to download the document. * * If not provided, it will be assumed that the current user can access the document. */ token?: string; trigger: React.ReactNode; }; export const EnvelopeDownloadDialog = ({ envelopeId, envelopeStatus, isLegacy, envelopeItems: initialEnvelopeItems, token, trigger, }: EnvelopeDownloadDialogProps) => { const { toast } = useToast(); const { t } = useLingui(); const [open, setOpen] = useState(false); const [isDownloadingState, setIsDownloadingState] = useState<{ [envelopeItemIdAndVersion: string]: boolean; }>({}); const generateDownloadKey = (envelopeItemId: string, version: 'original' | 'signed' | 'pending') => `${envelopeItemId}-${version}`; // The dialog shows the original document alongside one of: // - "Signed" (when the envelope is COMPLETED) // - "Partial" (when the envelope is PENDING, not legacy, and we are on the // team/owner side; recipients are intentionally not offered this since the // partial PDF carries no PKI signature and would create a leak vector for // half-executed contracts; legacy envelopes use a different rendering // pipeline that the partial-download helper does not implement) // - nothing (DRAFT, REJECTED, PENDING with recipient token, or legacy PENDING) const secondaryDownload = useMemo<{ version: 'signed' | 'pending'; label: string } | null>(() => { if (envelopeStatus === DocumentStatus.COMPLETED) { return { version: 'signed', label: t({ message: 'Signed', context: 'Signed document (adjective)' }), }; } if (envelopeStatus === DocumentStatus.PENDING && !token && !isLegacy) { return { version: 'pending', label: t({ message: 'Partial', context: 'Partially signed document (adjective)' }), }; } return null; }, [envelopeStatus, isLegacy, token, t]); const { data: envelopeItemsPayload, isLoading: isLoadingEnvelopeItems } = trpc.envelope.item.getManyByToken.useQuery( { envelopeId, access: token ? { type: 'recipient', token } : { type: 'user' }, }, { initialData: initialEnvelopeItems ? { data: initialEnvelopeItems } : undefined, enabled: open, }, ); const envelopeItems = envelopeItemsPayload?.data || []; const onDownload = async (envelopeItem: EnvelopeItemToDownload, version: 'original' | 'signed' | 'pending') => { const { id: envelopeItemId } = envelopeItem; if (isDownloadingState[generateDownloadKey(envelopeItemId, version)]) { return; } setIsDownloadingState((prev) => ({ ...prev, [generateDownloadKey(envelopeItemId, version)]: true, })); try { await downloadPDF({ envelopeItem, token, fileName: envelopeItem.title, version, }); setIsDownloadingState((prev) => ({ ...prev, [generateDownloadKey(envelopeItemId, version)]: false, })); } catch (error) { setIsDownloadingState((prev) => ({ ...prev, [generateDownloadKey(envelopeItemId, version)]: false, })); console.error(error); toast({ title: t`Something went wrong`, description: t`This document could not be downloaded at this time. Please try again.`, variant: 'destructive', duration: 7500, }); } }; return ( setOpen(value)}> {trigger} Download Files Select the files you would like to download.
{isLoadingEnvelopeItems ? Array.from({ length: 1 }).map((_, index) => (
)) : envelopeItems.map((item) => (
{/* Todo: Envelopes - Fix overflow */}

{item.title}

PDF Document

{secondaryDownload && ( )}
))}
); };