import { useEffect, useState } from 'react'; import { msg } from '@lingui/core/macro'; import { useLingui } from '@lingui/react'; import { Trans } from '@lingui/react/macro'; import { DocumentStatus } from '@prisma/client'; import { P, match } from 'ts-pattern'; import { useLimits } from '@documenso/ee/server-only/limits/provider/client'; import { trpc as trpcReact } from '@documenso/trpc/react'; import { Alert, AlertDescription } from '@documenso/ui/primitives/alert'; import { Button } from '@documenso/ui/primitives/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@documenso/ui/primitives/dialog'; import { Input } from '@documenso/ui/primitives/input'; import { useToast } from '@documenso/ui/primitives/use-toast'; type DocumentDeleteDialogProps = { id: number; open: boolean; onOpenChange: (_open: boolean) => void; onDelete?: () => Promise | void; status: DocumentStatus; documentTitle: string; canManageDocument: boolean; }; export const DocumentDeleteDialog = ({ id, open, onOpenChange, onDelete, status, documentTitle, canManageDocument, }: DocumentDeleteDialogProps) => { const { toast } = useToast(); const { refreshLimits } = useLimits(); const { _ } = useLingui(); const deleteMessage = msg`delete`; const [inputValue, setInputValue] = useState(''); const [isDeleteEnabled, setIsDeleteEnabled] = useState(status === DocumentStatus.DRAFT); const { mutateAsync: deleteDocument, isPending } = trpcReact.document.deleteDocument.useMutation({ onSuccess: async () => { void refreshLimits(); toast({ title: _(msg`Document deleted`), description: _(msg`"${documentTitle}" has been successfully deleted`), duration: 5000, }); await onDelete?.(); onOpenChange(false); }, onError: () => { toast({ title: _(msg`Something went wrong`), description: _(msg`This document could not be deleted at this time. Please try again.`), variant: 'destructive', duration: 7500, }); }, }); useEffect(() => { if (open) { setInputValue(''); setIsDeleteEnabled(status === DocumentStatus.DRAFT); } }, [open, status]); const onInputChange = (event: React.ChangeEvent) => { setInputValue(event.target.value); setIsDeleteEnabled(event.target.value === _(deleteMessage)); }; return ( !isPending && onOpenChange(value)}> Are you sure? {canManageDocument ? ( You are about to delete "{documentTitle}" ) : ( You are about to hide "{documentTitle}" )} {canManageDocument ? ( {match(status) .with(DocumentStatus.DRAFT, () => ( Please note that this action is irreversible. Once confirmed, this document will be permanently deleted. )) .with(DocumentStatus.PENDING, () => (

Please note that this action is irreversible.

Once confirmed, the following will occur:

  • Document will be permanently deleted
  • Document signing process will be cancelled
  • All inserted signatures will be voided
  • All recipients will be notified
)) .with(P.union(DocumentStatus.COMPLETED, DocumentStatus.REJECTED), () => (

By deleting this document, the following will occur:

  • The document will be hidden from your account
  • Recipients will still retain their copy of the document
)) .exhaustive()}
) : ( Please contact support if you would like to revert this action. )} {status !== DocumentStatus.DRAFT && canManageDocument && ( )}
); };