Files
sign/apps/remix/app/components/general/document/document-page-view-dropdown.tsx

205 lines
6.6 KiB
TypeScript
Raw Normal View History

2024-02-12 17:30:23 +11:00
import { useState } from 'react';
2025-01-02 15:33:37 +11:00
import { msg } from '@lingui/core/macro';
2024-08-27 20:34:39 +09:00
import { useLingui } from '@lingui/react';
2025-01-02 15:33:37 +11:00
import { Trans } from '@lingui/react/macro';
import { DocumentStatus } from '@prisma/client';
import type { Document, Recipient, Team, User } from '@prisma/client';
import {
Copy,
Download,
Edit,
Loader,
MoreHorizontal,
ScrollTextIcon,
Share,
Trash2,
} from 'lucide-react';
2025-01-02 15:33:37 +11:00
import { Link } from 'react-router';
import { useNavigate } from 'react-router';
2024-02-12 17:30:23 +11:00
import { downloadPDF } from '@documenso/lib/client-only/download-pdf';
2025-01-02 15:33:37 +11:00
import { useSession } from '@documenso/lib/client-only/providers/session';
import { isDocumentCompleted } from '@documenso/lib/utils/document';
2024-02-12 17:30:23 +11:00
import { formatDocumentsPath } from '@documenso/lib/utils/teams';
import { trpc as trpcClient } from '@documenso/trpc/client';
import { DocumentShareButton } from '@documenso/ui/components/document/document-share-button';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuTrigger,
} from '@documenso/ui/primitives/dropdown-menu';
import { useToast } from '@documenso/ui/primitives/use-toast';
2025-01-02 15:33:37 +11:00
import { DocumentDeleteDialog } from '~/components/dialogs/document-delete-dialog';
import { DocumentDuplicateDialog } from '~/components/dialogs/document-duplicate-dialog';
import { DocumentResendDialog } from '~/components/dialogs/document-resend-dialog';
import { DocumentRecipientLinkCopyDialog } from '~/components/general/document/document-recipient-link-copy-dialog';
import { useOptionalCurrentTeam } from '~/providers/team';
2024-02-12 17:30:23 +11:00
export type DocumentPageViewDropdownProps = {
document: Document & {
2025-01-13 13:41:53 +11:00
user: Pick<User, 'id' | 'name' | 'email'>;
recipients: Recipient[];
2024-02-12 17:30:23 +11:00
team: Pick<Team, 'id' | 'url'> | null;
};
};
2025-01-02 15:33:37 +11:00
export const DocumentPageViewDropdown = ({ document }: DocumentPageViewDropdownProps) => {
const { user } = useSession();
2024-02-12 17:30:23 +11:00
const { toast } = useToast();
2024-08-27 20:34:39 +09:00
const { _ } = useLingui();
2024-02-12 17:30:23 +11:00
2025-01-02 15:33:37 +11:00
const navigate = useNavigate();
const team = useOptionalCurrentTeam();
2024-02-12 17:30:23 +11:00
const [isDeleteDialogOpen, setDeleteDialogOpen] = useState(false);
const [isDuplicateDialogOpen, setDuplicateDialogOpen] = useState(false);
2025-01-02 15:33:37 +11:00
const recipient = document.recipients.find((recipient) => recipient.email === user.email);
2024-02-12 17:30:23 +11:00
2025-01-02 15:33:37 +11:00
const isOwner = document.user.id === user.id;
2024-02-12 17:30:23 +11:00
const isDraft = document.status === DocumentStatus.DRAFT;
2024-11-06 21:34:06 +09:00
const isPending = document.status === DocumentStatus.PENDING;
const isDeleted = document.deletedAt !== null;
const isComplete = isDocumentCompleted(document);
2024-02-12 17:30:23 +11:00
const isCurrentTeamDocument = team && document.team?.url === team.url;
const canManageDocument = Boolean(isOwner || isCurrentTeamDocument);
2024-02-12 17:30:23 +11:00
const documentsPath = formatDocumentsPath(team?.url);
const onDownloadClick = async () => {
try {
const documentWithData = await trpcClient.document.getDocumentById.query(
{
documentId: document.id,
},
{
context: {
teamId: team?.id?.toString(),
},
},
);
2024-02-12 17:30:23 +11:00
const documentData = documentWithData?.documentData;
if (!documentData) {
return;
}
await downloadPDF({ documentData, fileName: document.title });
} catch (err) {
toast({
2024-08-27 20:34:39 +09:00
title: _(msg`Something went wrong`),
description: _(msg`An error occurred while downloading your document.`),
2024-02-12 17:30:23 +11:00
variant: 'destructive',
});
}
};
2025-01-13 13:41:53 +11:00
const nonSignedRecipients = document.recipients.filter((item) => item.signingStatus !== 'SIGNED');
2024-02-12 17:30:23 +11:00
return (
<DropdownMenu>
<DropdownMenuTrigger>
<MoreHorizontal className="text-muted-foreground h-5 w-5" />
</DropdownMenuTrigger>
<DropdownMenuContent className="w-52" align="end" forceMount>
2024-08-27 20:34:39 +09:00
<DropdownMenuLabel>
<Trans>Action</Trans>
</DropdownMenuLabel>
2024-02-12 17:30:23 +11:00
{(isOwner || isCurrentTeamDocument) && !isComplete && (
<DropdownMenuItem asChild>
2025-01-02 15:33:37 +11:00
<Link to={`${documentsPath}/${document.id}/edit`}>
2024-02-12 17:30:23 +11:00
<Edit className="mr-2 h-4 w-4" />
2024-08-27 20:34:39 +09:00
<Trans>Edit</Trans>
2024-02-12 17:30:23 +11:00
</Link>
</DropdownMenuItem>
)}
{isComplete && (
<DropdownMenuItem onClick={onDownloadClick}>
<Download className="mr-2 h-4 w-4" />
2024-08-27 20:34:39 +09:00
<Trans>Download</Trans>
2024-02-12 17:30:23 +11:00
</DropdownMenuItem>
)}
<DropdownMenuItem asChild>
2025-01-02 15:33:37 +11:00
<Link to={`${documentsPath}/${document.id}/logs`}>
<ScrollTextIcon className="mr-2 h-4 w-4" />
2024-08-27 20:34:39 +09:00
<Trans>Audit Log</Trans>
</Link>
</DropdownMenuItem>
2024-02-12 17:30:23 +11:00
<DropdownMenuItem onClick={() => setDuplicateDialogOpen(true)}>
<Copy className="mr-2 h-4 w-4" />
2024-08-27 20:34:39 +09:00
<Trans>Duplicate</Trans>
2024-02-12 17:30:23 +11:00
</DropdownMenuItem>
2025-02-17 22:46:36 +11:00
<DropdownMenuItem onClick={() => setDeleteDialogOpen(true)} disabled={isDeleted}>
2024-02-12 17:30:23 +11:00
<Trash2 className="mr-2 h-4 w-4" />
2024-08-27 20:34:39 +09:00
<Trans>Delete</Trans>
2024-02-12 17:30:23 +11:00
</DropdownMenuItem>
2024-08-27 20:34:39 +09:00
<DropdownMenuLabel>
<Trans>Share</Trans>
</DropdownMenuLabel>
2024-02-12 17:30:23 +11:00
2024-11-06 21:34:06 +09:00
{canManageDocument && (
<DocumentRecipientLinkCopyDialog
2025-01-13 13:41:53 +11:00
recipients={document.recipients}
2024-11-06 21:34:06 +09:00
trigger={
<DropdownMenuItem
disabled={!isPending || isDeleted}
onSelect={(e) => e.preventDefault()}
>
<Copy className="mr-2 h-4 w-4" />
<Trans>Signing Links</Trans>
</DropdownMenuItem>
}
/>
)}
2025-01-02 15:33:37 +11:00
<DocumentResendDialog document={document} recipients={nonSignedRecipients} />
2024-02-12 17:30:23 +11:00
<DocumentShareButton
documentId={document.id}
token={isOwner ? undefined : recipient?.token}
trigger={({ loading, disabled }) => (
<DropdownMenuItem disabled={disabled || isDraft} onSelect={(e) => e.preventDefault()}>
<div className="flex items-center">
{loading ? <Loader className="mr-2 h-4 w-4" /> : <Share className="mr-2 h-4 w-4" />}
2024-08-27 20:34:39 +09:00
<Trans>Share Signing Card</Trans>
2024-02-12 17:30:23 +11:00
</div>
</DropdownMenuItem>
)}
/>
</DropdownMenuContent>
2025-01-02 15:33:37 +11:00
<DocumentDeleteDialog
id={document.id}
status={document.status}
documentTitle={document.title}
open={isDeleteDialogOpen}
canManageDocument={canManageDocument}
onOpenChange={setDeleteDialogOpen}
2025-01-02 15:33:37 +11:00
onDelete={() => {
void navigate(documentsPath);
}}
/>
2024-02-12 17:30:23 +11:00
{isDuplicateDialogOpen && (
2025-01-02 15:33:37 +11:00
<DocumentDuplicateDialog
2024-02-12 17:30:23 +11:00
id={document.id}
open={isDuplicateDialogOpen}
onOpenChange={setDuplicateDialogOpen}
/>
)}
</DropdownMenu>
);
};