chore: refactor

This commit is contained in:
Ephraim Atta-Duncan
2025-11-18 20:07:04 +00:00
parent 13bd5815d9
commit 8e2ca94020
13 changed files with 221 additions and 247 deletions

View File

@ -1,7 +1,6 @@
import { type ReactNode, useState } from 'react';
import { useLingui } from '@lingui/react/macro';
import { Trans } from '@lingui/react/macro';
import { Trans, useLingui } from '@lingui/react/macro';
import { EnvelopeType } from '@prisma/client';
import { Loader } from 'lucide-react';
import {
@ -27,14 +26,14 @@ import type { TCreateEnvelopePayload } from '@documenso/trpc/server/envelope-rou
import { cn } from '@documenso/ui/lib/utils';
import { useToast } from '@documenso/ui/primitives/use-toast';
import { DocumentAiPromptDialog } from '~/components/dialogs/document-ai-prompt-dialog';
import { DocumentAiRecipientsDialog } from '~/components/dialogs/document-ai-recipients-dialog';
import { RecipientDetectionPromptDialog } from '~/components/dialogs/recipient-detection-prompt-dialog';
import { SuggestedRecipientsDialog } from '~/components/dialogs/suggested-recipients-dialog';
import { useCurrentTeam } from '~/providers/team';
import {
type RecipientForCreation,
analyzeRecipientsFromDocument,
detectRecipientsInDocument,
ensureRecipientEmails,
} from '~/utils/analyze-ai-recipients';
} from '~/utils/detect-document-recipients';
export interface EnvelopeDropZoneWrapperProps {
children: ReactNode;
@ -59,10 +58,10 @@ export const EnvelopeDropZoneWrapper = ({
const organisation = useCurrentOrganisation();
const [isLoading, setIsLoading] = useState(false);
const [showAiPromptDialog, setShowAiPromptDialog] = useState(false);
const [showRecipientDetectionPrompt, setShowRecipientDetectionPrompt] = useState(false);
const [uploadedDocumentId, setUploadedDocumentId] = useState<string | null>(null);
const [pendingRecipients, setPendingRecipients] = useState<RecipientForCreation[] | null>(null);
const [showAiRecipientsDialog, setShowAiRecipientsDialog] = useState(false);
const [showSuggestedRecipientsDialog, setShowSuggestedRecipientsDialog] = useState(false);
const [shouldNavigateAfterPromptClose, setShouldNavigateAfterPromptClose] = useState(true);
const userTimezone =
@ -125,9 +124,9 @@ export const EnvelopeDropZoneWrapper = ({
// Show AI prompt dialog for documents
setUploadedDocumentId(id);
setPendingRecipients(null);
setShowAiRecipientsDialog(false);
setShowSuggestedRecipientsDialog(false);
setShouldNavigateAfterPromptClose(true);
setShowAiPromptDialog(true);
setShowRecipientDetectionPrompt(true);
} else {
// Templates - navigate immediately
const pathPrefix = formatTemplatesPath(team.url);
@ -228,13 +227,13 @@ export const EnvelopeDropZoneWrapper = ({
void navigate(`${pathPrefix}/${uploadedDocumentId}/edit`);
};
const handleAiAccept = async () => {
const handleStartRecipientDetection = async () => {
if (!uploadedDocumentId) {
return;
}
try {
const recipients = await analyzeRecipientsFromDocument(uploadedDocumentId);
const recipients = await detectRecipientsInDocument(uploadedDocumentId);
if (recipients.length === 0) {
toast({
@ -250,14 +249,14 @@ export const EnvelopeDropZoneWrapper = ({
setPendingRecipients(recipientsWithEmails);
setShouldNavigateAfterPromptClose(false);
setShowAiPromptDialog(false);
setShowAiRecipientsDialog(true);
setShowRecipientDetectionPrompt(false);
setShowSuggestedRecipientsDialog(true);
} catch (error) {
if (!(error instanceof Error && error.message === 'NO_RECIPIENTS_DETECTED')) {
const parsedError = AppError.parseError(error);
toast({
title: t`Failed to analyze recipients`,
title: t`Failed to detect recipients`,
description: parsedError.userMessage || t`You can add recipients manually in the editor`,
variant: 'destructive',
duration: 7500,
@ -268,14 +267,14 @@ export const EnvelopeDropZoneWrapper = ({
}
};
const handleAiSkip = () => {
const handleSkipRecipientDetection = () => {
setShouldNavigateAfterPromptClose(true);
setShowAiPromptDialog(false);
setShowRecipientDetectionPrompt(false);
navigateToEnvelopeEditor();
};
const handleRecipientsCancel = () => {
setShowAiRecipientsDialog(false);
setShowSuggestedRecipientsDialog(false);
setPendingRecipients(null);
navigateToEnvelopeEditor();
};
@ -297,7 +296,7 @@ export const EnvelopeDropZoneWrapper = ({
duration: 5000,
});
setShowAiRecipientsDialog(false);
setShowSuggestedRecipientsDialog(false);
setPendingRecipients(null);
navigateToEnvelopeEditor();
} catch (error) {
@ -315,7 +314,7 @@ export const EnvelopeDropZoneWrapper = ({
};
const handlePromptDialogOpenChange = (open: boolean) => {
setShowAiPromptDialog(open);
setShowRecipientDetectionPrompt(open);
if (open) {
setShouldNavigateAfterPromptClose(true);
@ -394,21 +393,21 @@ export const EnvelopeDropZoneWrapper = ({
</div>
)}
<DocumentAiPromptDialog
open={showAiPromptDialog}
<RecipientDetectionPromptDialog
open={showRecipientDetectionPrompt}
onOpenChange={handlePromptDialogOpenChange}
onAccept={handleAiAccept}
onSkip={handleAiSkip}
onAccept={handleStartRecipientDetection}
onSkip={handleSkipRecipientDetection}
/>
<DocumentAiRecipientsDialog
open={showAiRecipientsDialog}
<SuggestedRecipientsDialog
open={showSuggestedRecipientsDialog}
recipients={pendingRecipients}
onOpenChange={(open) => {
if (!open) {
handleRecipientsCancel();
} else {
setShowAiRecipientsDialog(true);
setShowSuggestedRecipientsDialog(true);
}
}}
onCancel={handleRecipientsCancel}

View File

@ -27,14 +27,14 @@ import {
} from '@documenso/ui/primitives/tooltip';
import { useToast } from '@documenso/ui/primitives/use-toast';
import { DocumentAiPromptDialog } from '~/components/dialogs/document-ai-prompt-dialog';
import { DocumentAiRecipientsDialog } from '~/components/dialogs/document-ai-recipients-dialog';
import { RecipientDetectionPromptDialog } from '~/components/dialogs/recipient-detection-prompt-dialog';
import { SuggestedRecipientsDialog } from '~/components/dialogs/suggested-recipients-dialog';
import { useCurrentTeam } from '~/providers/team';
import {
type RecipientForCreation,
analyzeRecipientsFromDocument,
detectRecipientsInDocument,
ensureRecipientEmails,
} from '~/utils/analyze-ai-recipients';
} from '~/utils/detect-document-recipients';
export type EnvelopeUploadButtonProps = {
className?: string;
@ -62,10 +62,10 @@ export const EnvelopeUploadButton = ({ className, type, folderId }: EnvelopeUplo
const { quota, remaining, refreshLimits, maximumEnvelopeItemCount } = useLimits();
const [isLoading, setIsLoading] = useState(false);
const [showAiPromptDialog, setShowAiPromptDialog] = useState(false);
const [showRecipientDetectionPrompt, setShowAiPromptDialog] = useState(false);
const [uploadedDocumentId, setUploadedDocumentId] = useState<string | null>(null);
const [pendingRecipients, setPendingRecipients] = useState<RecipientForCreation[] | null>(null);
const [showAiRecipientsDialog, setShowAiRecipientsDialog] = useState(false);
const [showSuggestedRecipientsDialog, setShowAiRecipientsDialog] = useState(false);
const [shouldNavigateAfterPromptClose, setShouldNavigateAfterPromptClose] = useState(true);
const { mutateAsync: createEnvelope } = trpc.envelope.create.useMutation();
@ -204,13 +204,13 @@ export const EnvelopeUploadButton = ({ className, type, folderId }: EnvelopeUplo
void navigate(`${pathPrefix}/${uploadedDocumentId}/edit`);
};
const handleAiAccept = async () => {
const handleStartRecipientDetection = async () => {
if (!uploadedDocumentId) {
return;
}
try {
const recipients = await analyzeRecipientsFromDocument(uploadedDocumentId);
const recipients = await detectRecipientsInDocument(uploadedDocumentId);
if (recipients.length === 0) {
toast({
@ -244,7 +244,7 @@ export const EnvelopeUploadButton = ({ className, type, folderId }: EnvelopeUplo
}
};
const handleAiSkip = () => {
const handleSkipRecipientDetection = () => {
setShouldNavigateAfterPromptClose(true);
setShowAiPromptDialog(false);
navigateToEnvelopeEditor();
@ -336,15 +336,15 @@ export const EnvelopeUploadButton = ({ className, type, folderId }: EnvelopeUplo
</Tooltip>
</TooltipProvider>
<DocumentAiPromptDialog
open={showAiPromptDialog}
<RecipientDetectionPromptDialog
open={showRecipientDetectionPrompt}
onOpenChange={handlePromptDialogOpenChange}
onAccept={handleAiAccept}
onSkip={handleAiSkip}
onAccept={handleStartRecipientDetection}
onSkip={handleSkipRecipientDetection}
/>
<DocumentAiRecipientsDialog
open={showAiRecipientsDialog}
<SuggestedRecipientsDialog
open={showSuggestedRecipientsDialog}
recipients={pendingRecipients}
onOpenChange={(open) => {
if (!open) {