mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
This PR is handles the changes required to support envelopes. The new envelope editor/signing page will be hidden during release. The core changes here is to migrate the documents and templates model to a centralized envelopes model. Even though Documents and Templates are removed, from the user perspective they will still exist as we remap envelopes to documents and templates.
142 lines
4.5 KiB
TypeScript
142 lines
4.5 KiB
TypeScript
import { msg } from '@lingui/core/macro';
|
|
import { useLingui } from '@lingui/react';
|
|
import { Trans } from '@lingui/react/macro';
|
|
import { DocumentStatus, RecipientRole, SigningStatus } from '@prisma/client';
|
|
import { CheckCircle, Download, Edit, EyeIcon, Pencil } from 'lucide-react';
|
|
import { Link } from 'react-router';
|
|
import { match } from 'ts-pattern';
|
|
|
|
import { downloadPDF } from '@documenso/lib/client-only/download-pdf';
|
|
import { useSession } from '@documenso/lib/client-only/providers/session';
|
|
import type { TDocumentMany as TDocumentRow } from '@documenso/lib/types/document';
|
|
import { isDocumentCompleted } from '@documenso/lib/utils/document';
|
|
import { formatDocumentsPath } from '@documenso/lib/utils/teams';
|
|
import { trpc as trpcClient } from '@documenso/trpc/client';
|
|
import { Button } from '@documenso/ui/primitives/button';
|
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
|
|
|
import { useCurrentTeam } from '~/providers/team';
|
|
|
|
export type DocumentsTableActionButtonProps = {
|
|
row: TDocumentRow;
|
|
};
|
|
|
|
export const DocumentsTableActionButton = ({ row }: DocumentsTableActionButtonProps) => {
|
|
const { user } = useSession();
|
|
const { toast } = useToast();
|
|
const { _ } = useLingui();
|
|
|
|
const team = useCurrentTeam();
|
|
|
|
const recipient = row.recipients.find((recipient) => recipient.email === user.email);
|
|
|
|
const isOwner = row.user.id === user.id;
|
|
const isRecipient = !!recipient;
|
|
const isDraft = row.status === DocumentStatus.DRAFT;
|
|
const isPending = row.status === DocumentStatus.PENDING;
|
|
const isComplete = isDocumentCompleted(row.status);
|
|
const isSigned = recipient?.signingStatus === SigningStatus.SIGNED;
|
|
const role = recipient?.role;
|
|
const isCurrentTeamDocument = team && row.team?.url === team.url;
|
|
|
|
const documentsPath = formatDocumentsPath(team.url);
|
|
const formatPath = `${documentsPath}/${row.envelopeId}/edit`;
|
|
|
|
const onDownloadClick = async () => {
|
|
try {
|
|
const document = !recipient
|
|
? await trpcClient.document.get.query(
|
|
{
|
|
documentId: row.id,
|
|
},
|
|
{
|
|
context: {
|
|
teamId: team?.id?.toString(),
|
|
},
|
|
},
|
|
)
|
|
: await trpcClient.document.getDocumentByToken.query({
|
|
token: recipient.token,
|
|
});
|
|
|
|
const documentData = document?.documentData;
|
|
|
|
if (!documentData) {
|
|
throw Error('No document available');
|
|
}
|
|
|
|
await downloadPDF({ documentData, fileName: row.title });
|
|
} catch (err) {
|
|
toast({
|
|
title: _(msg`Something went wrong`),
|
|
description: _(msg`An error occurred while downloading your document.`),
|
|
variant: 'destructive',
|
|
});
|
|
}
|
|
};
|
|
|
|
// TODO: Consider if want to keep this logic for hiding viewing for CC'ers
|
|
if (recipient?.role === RecipientRole.CC && isComplete === false) {
|
|
return null;
|
|
}
|
|
|
|
return match({
|
|
isOwner,
|
|
isRecipient,
|
|
isDraft,
|
|
isPending,
|
|
isComplete,
|
|
isSigned,
|
|
isCurrentTeamDocument,
|
|
})
|
|
.with(
|
|
isOwner ? { isDraft: true, isOwner: true } : { isDraft: true, isCurrentTeamDocument: true },
|
|
() => (
|
|
<Button className="w-32" asChild>
|
|
<Link to={formatPath}>
|
|
<Edit className="-ml-1 mr-2 h-4 w-4" />
|
|
<Trans>Edit</Trans>
|
|
</Link>
|
|
</Button>
|
|
),
|
|
)
|
|
.with({ isRecipient: true, isPending: true, isSigned: false }, () => (
|
|
<Button className="w-32" asChild>
|
|
<Link to={`/sign/${recipient?.token}`}>
|
|
{match(role)
|
|
.with(RecipientRole.SIGNER, () => (
|
|
<>
|
|
<Pencil className="-ml-1 mr-2 h-4 w-4" />
|
|
<Trans>Sign</Trans>
|
|
</>
|
|
))
|
|
.with(RecipientRole.APPROVER, () => (
|
|
<>
|
|
<CheckCircle className="-ml-1 mr-2 h-4 w-4" />
|
|
<Trans>Approve</Trans>
|
|
</>
|
|
))
|
|
.otherwise(() => (
|
|
<>
|
|
<EyeIcon className="-ml-1 mr-2 h-4 w-4" />
|
|
<Trans>View</Trans>
|
|
</>
|
|
))}
|
|
</Link>
|
|
</Button>
|
|
))
|
|
.with({ isPending: true, isSigned: true }, () => (
|
|
<Button className="w-32" disabled={true}>
|
|
<EyeIcon className="-ml-1 mr-2 h-4 w-4" />
|
|
<Trans>View</Trans>
|
|
</Button>
|
|
))
|
|
.with({ isComplete: true }, () => (
|
|
<Button className="w-32" onClick={onDownloadClick}>
|
|
<Download className="-ml-1 mr-2 inline h-4 w-4" />
|
|
<Trans>Download</Trans>
|
|
</Button>
|
|
))
|
|
.otherwise(() => <div></div>);
|
|
};
|