Skip to content

The Open Source DocuSign Alternative.

fix: sync envelope state after direct link changes (#2257)

Ephraim Duncaned7a0011c733
diff --git a/apps/remix/app/components/dialogs/template-direct-link-dialog.tsx b/apps/remix/app/components/dialogs/template-direct-link-dialog.tsx
index 4e267b0e5..b644045d6 100644
--- a/apps/remix/app/components/dialogs/template-direct-link-dialog.tsx
+++ b/apps/remix/app/components/dialogs/template-direct-link-dialog.tsx
@@ -54,6 +54,8 @@ type TemplateDirectLinkDialogProps = {
directLink?: Pick<TemplateDirectLink, 'token' | 'enabled'> | null;
recipients: Recipient[];
trigger?: React.ReactNode;
+ onCreateSuccess?: () => Promise<void> | void;
+ onDeleteSuccess?: () => Promise<void> | void;
};
type TemplateDirectLinkStep = 'ONBOARD' | 'SELECT_RECIPIENT' | 'MANAGE' | 'CONFIRM_DELETE';
@@ -63,6 +65,8 @@ export const TemplateDirectLinkDialog = ({
directLink,
recipients,
trigger,
+ onCreateSuccess,
+ onDeleteSuccess,
}: TemplateDirectLinkDialogProps) => {
const { toast } = useToast();
const { quota, remaining } = useLimits();
@@ -97,6 +101,7 @@ export const TemplateDirectLinkDialog = ({
} = trpcReact.template.createTemplateDirectLink.useMutation({
onSuccess: async (data) => {
await revalidate();
+ await onCreateSuccess?.();
setToken(data.token);
setIsEnabled(data.enabled);
@@ -142,6 +147,7 @@ export const TemplateDirectLinkDialog = ({
trpcReact.template.deleteTemplateDirectLink.useMutation({
onSuccess: async () => {
await revalidate();
+ await onDeleteSuccess?.();
setOpen(false);
setToken(null);
@@ -234,7 +240,7 @@ export const TemplateDirectLinkDialog = ({
</div>
<h3 className="font-semibold">{_(step.title)}</h3>
- <p className="text-muted-foreground mt-1 text-sm">{_(step.description)}</p>
+ <p className="mt-1 text-sm text-muted-foreground">{_(step.description)}</p>
</li>
))}
</ul>
@@ -320,13 +326,13 @@ export const TemplateDirectLinkDialog = ({
onClick={async () => onRecipientTableRowClick(row.id)}
>
<TableCell>
- <div className="text-muted-foreground text-sm">
+ <div className="text-sm text-muted-foreground">
<p>{row.name}</p>
- <p className="text-muted-foreground/70 text-xs">{row.email}</p>
+ <p className="text-xs text-muted-foreground/70">{row.email}</p>
</div>
</TableCell>
- <TableCell className="text-muted-foreground text-sm">
+ <TableCell className="text-sm text-muted-foreground">
{_(RECIPIENT_ROLES_DESCRIPTION[row.role].roleName)}
</TableCell>
@@ -350,7 +356,7 @@ export const TemplateDirectLinkDialog = ({
<DialogFooter className="mx-auto">
<div className="flex flex-col items-center justify-center">
{validDirectTemplateRecipients.length !== 0 && (
- <p className="text-muted-foreground text-sm">
+ <p className="text-sm text-muted-foreground">
<Trans>Or</Trans>
</p>
)}
@@ -392,7 +398,7 @@ export const TemplateDirectLinkDialog = ({
<TooltipTrigger tabIndex={-1} className="ml-2">
<InfoIcon className="h-4 w-4" />
</TooltipTrigger>
- <TooltipContent className="text-foreground z-9999 max-w-md p-4">
+ <TooltipContent className="z-9999 max-w-md p-4 text-foreground">
<Trans>
Disabling direct link signing will prevent anyone from accessing the
link.
diff --git a/apps/remix/app/components/general/envelope-editor/envelope-editor-recipient-form.tsx b/apps/remix/app/components/general/envelope-editor/envelope-editor-recipient-form.tsx
index 7b17e9d97..ab83b3189 100644
--- a/apps/remix/app/components/general/envelope-editor/envelope-editor-recipient-form.tsx
+++ b/apps/remix/app/components/general/envelope-editor/envelope-editor-recipient-form.tsx
@@ -1,4 +1,4 @@
-import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react';
+import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import {
DragDropContext,
@@ -7,28 +7,23 @@ import {
Droppable,
type SensorAPI,
} from '@hello-pangea/dnd';
-import { zodResolver } from '@hookform/resolvers/zod';
import { plural } from '@lingui/core/macro';
import { Trans, useLingui } from '@lingui/react/macro';
import { DocumentSigningOrder, EnvelopeType, RecipientRole, SendStatus } from '@prisma/client';
import { motion } from 'framer-motion';
import { GripVerticalIcon, HelpCircleIcon, PlusIcon, SparklesIcon, TrashIcon } from 'lucide-react';
-import { useFieldArray, useForm, useWatch } from 'react-hook-form';
+import { useFieldArray, useWatch } from 'react-hook-form';
import { useRevalidator, useSearchParams } from 'react-router';
-import { isDeepEqual, prop, sortBy } from 'remeda';
-import { z } from 'zod';
+import { isDeepEqual } from 'remeda';
import { useLimits } from '@documenso/ee/server-only/limits/provider/client';
import { useDebouncedValue } from '@documenso/lib/client-only/hooks/use-debounced-value';
+import { ZEditorRecipientsFormSchema } from '@documenso/lib/client-only/hooks/use-editor-recipients';
import { useCurrentEnvelopeEditor } from '@documenso/lib/client-only/providers/envelope-editor-provider';
import { useCurrentOrganisation } from '@documenso/lib/client-only/providers/organisation';
import { useSession } from '@documenso/lib/client-only/providers/session';
import type { TDetectedRecipientSchema } from '@documenso/lib/server-only/ai/envelope/detect-recipients/schema';
-import {
- ZRecipientActionAuthTypesSchema,
- ZRecipientAuthOptionsSchema,
-} from '@documenso/lib/types/document-auth';
-import { ZRecipientEmailSchema } from '@documenso/lib/types/recipient';
+import { ZRecipientAuthOptionsSchema } from '@documenso/lib/types/document-auth';
import { nanoid } from '@documenso/lib/universal/id';
import { canRecipientBeModified as utilCanRecipientBeModified } from '@documenso/lib/utils/recipients';
import { trpc } from '@documenso/trpc/react';
@@ -67,26 +62,9 @@ import { AiFeaturesEnableDialog } from '~/components/dialogs/ai-features-enable-
import { AiRecipientDetectionDialog } from '~/components/dialogs/ai-recipient-detection-dialog';
import { useCurrentTeam } from '~/providers/team';
-const ZEnvelopeRecipientsForm = z.object({
- signers: z.array(
- z.object({
- formId: z.string().min(1),
- id: z.number().optional(),
- email: ZRecipientEmailSchema,
- name: z.string(),
- role: z.nativeEnum(RecipientRole),
- signingOrder: z.number().optional(),
- actionAuth: z.array(ZRecipientActionAuthTypesSchema).optional().default([]),
- }),
- ),
- signingOrder: z.nativeEnum(DocumentSigningOrder),
- allowDictateNextSigner: z.boolean().default(false),
-});
-
-type TEnvelopeRecipientsForm = z.infer<typeof ZEnvelopeRecipientsForm>;
-
export const EnvelopeEditorRecipientForm = () => {
- const { envelope, setRecipientsDebounced, updateEnvelope } = useCurrentEnvelopeEditor();
+ const { envelope, setRecipientsDebounced, updateEnvelope, editorRecipients } =
+ useCurrentEnvelopeEditor();
const organisation = useCurrentOrganisation();
const team = useCurrentTeam();
@@ -145,7 +123,6 @@ export const EnvelopeEditorRecipientForm = () => {
const debouncedRecipientSearchQuery = useDebouncedValue(recipientSearchQuery, 500);
- const initialId = useId();
const $sensorApi = useRef<SensorAPI | null>(null);
const isFirstRender = useRef(true);
const { recipients, fields } = envelope;
@@ -161,42 +138,7 @@ export const EnvelopeEditorRecipientForm = () => {
const recipientSuggestions = recipientSuggestionsData?.results || [];
- const defaultRecipients = [
- {
- formId: initialId,
- name: '',
- email: '',
- role: RecipientRole.SIGNER,
- signingOrder: 1,
- actionAuth: [],
- },
- ];
-
- const form = useForm<TEnvelopeRecipientsForm>({
- resolver: zodResolver(ZEnvelopeRecipientsForm),
- mode: 'onChange', // Used for autosave purposes, maybe can try onBlur instead?
- defaultValues: {
- signers:
- recipients.length > 0
- ? sortBy(
- recipients.map((recipient, index) => ({
- id: recipient.id,
- formId: String(recipient.id),
- name: recipient.name,
- email: recipient.email,
- role: recipient.role,
- signingOrder: recipient.signingOrder ?? index + 1,
- actionAuth:
- ZRecipientAuthOptionsSchema.parse(recipient.authOptions)?.actionAuth ?? undefined,
- })),
- [prop('signingOrder'), 'asc'],
- [prop('id'), 'asc'],
- )
- : defaultRecipients,
- signingOrder: envelope.documentMeta.signingOrder,
- allowDictateNextSigner: envelope.documentMeta.allowDictateNextSigner,
- },
- });
+ const { form } = editorRecipients;
const recipientHasAuthSettings = useMemo(() => {
const recipientHasAuthOptions = recipients.find((recipient) => {
@@ -588,7 +530,7 @@ export const EnvelopeEditorRecipientForm = () => {
return;
}
- const validatedFormValues = ZEnvelopeRecipientsForm.safeParse(formValues);
+ const validatedFormValues = ZEditorRecipientsFormSchema.safeParse(formValues);
if (!validatedFormValues.success) {
return;
@@ -848,246 +790,205 @@ export const EnvelopeEditorRecipientForm = () => {
ref={provided.innerRef}
className="flex w-full flex-col gap-y-2"
>
- {signers.map((signer, index) => (
- <Draggable
- key={`${signer.nativeId}-${signer.signingOrder}`}
- draggableId={signer['nativeId']}
- index={index}
- isDragDisabled={
- !isSigningOrderSequential ||
- isSubmitting ||
- !canRecipientBeModified(signer.id) ||
- !signer.signingOrder
- }
- >
- {(provided, snapshot) => (
- <div
- ref={provided.innerRef}
- {...provided.draggableProps}
- {...provided.dragHandleProps}
- className={cn('py-1', {
- 'pointer-events-none rounded-md bg-widget-foreground pt-2':
- snapshot.isDragging,
- })}
- >
- <motion.fieldset
- data-native-id={signer.id}
- disabled={isSubmitting || !canRecipientBeModified(signer.id)}
- className={cn('pb-2', {
- 'border-b pb-4':
- showAdvancedSettings && index !== signers.length - 1,
- 'pt-2': showAdvancedSettings && index === 0,
- 'pr-3': isSigningOrderSequential,
+ {signers.map((signer, index) => {
+ const isDirectRecipient =
+ envelope.type === EnvelopeType.TEMPLATE &&
+ envelope.directLink !== null &&
+ signer.id === envelope.directLink.directTemplateRecipientId;
+
+ return (
+ <Draggable
+ key={`${signer.nativeId}-${signer.signingOrder}`}
+ draggableId={signer['nativeId']}
+ index={index}
+ isDragDisabled={
+ !isSigningOrderSequential ||
+ isSubmitting ||
+ !canRecipientBeModified(signer.id) ||
+ !signer.signingOrder
+ }
+ >
+ {(provided, snapshot) => (
+ <div
+ ref={provided.innerRef}
+ {...provided.draggableProps}
+ {...provided.dragHandleProps}
+ className={cn('py-1', {
+ 'pointer-events-none rounded-md bg-widget-foreground pt-2':
+ snapshot.isDragging,
})}
>
- <div className="flex flex-row items-center gap-x-2">
- {isSigningOrderSequential && (
+ <motion.fieldset
+ data-native-id={signer.id}
+ disabled={isSubmitting || !canRecipientBeModified(signer.id)}
+ className={cn('pb-2', {
+ 'border-b pb-4':
+ showAdvancedSettings && index !== signers.length - 1,
+ 'pt-2': showAdvancedSettings && index === 0,
+ 'pr-3': isSigningOrderSequential,
+ })}
+ >
+ <div className="flex flex-row items-center gap-x-2">
+ {isSigningOrderSequential && (
+ <FormField
+ control={form.control}
+ name={`signers.${index}.signingOrder`}
+ render={({ field }) => (
+ <FormItem
+ className={cn(
+ 'mt-auto flex items-center gap-x-1 space-y-0',
+ {
+ 'mb-6':
+ form.formState.errors.signers?.[index] &&
+ !form.formState.errors.signers[index]?.signingOrder,
+ },
+ )}
+ >
+ <GripVerticalIcon className="h-5 w-5 flex-shrink-0 opacity-40" />
+ <FormControl>
+ <Input
+ type="number"
+ max={signers.length}
+ data-testid="signing-order-input"
+ className={cn(
+ 'w-10 text-center',
+ '[appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none',
+ )}
+ {...field}
+ onChange={(e) => {
+ field.onChange(e);
+ handleSigningOrderChange(index, e.target.value);
+ }}
+ onBlur={(e) => {
+ field.onBlur();
+ handleSigningOrderChange(index, e.target.value);
+ }}
+ disabled={
+ snapshot.isDragging ||
+ isSubmitting ||
+ !canRecipientBeModified(signer.id)
+ }
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ )}
+
<FormField
control={form.control}
- name={`signers.${index}.signingOrder`}
+ name={`signers.${index}.email`}
render={({ field }) => (
<FormItem
- className={cn(
- 'mt-auto flex items-center gap-x-1 space-y-0',
- {
- 'mb-6':
- form.formState.errors.signers?.[index] &&
- !form.formState.errors.signers[index]?.signingOrder,
- },
+ className={cn('relative w-full', {
+ 'mb-6':
+ form.formState.errors.signers?.[index] &&
+ !form.formState.errors.signers[index]?.email,
+ })}
+ >
+ {!showAdvancedSettings && index === 0 && (
+ <FormLabel>
+ <Trans>Email</Trans>
+ </FormLabel>
)}
+
+ <FormControl>
+ <RecipientAutoCompleteInput
+ type="email"
+ placeholder={t`Email`}
+ value={field.value}
+ disabled={
+ snapshot.isDragging ||
+ isSubmitting ||
+ !canRecipientBeModified(signer.id) ||
+ isDirectRecipient
+ }
+ options={recipientSuggestions}
+ onSelect={(suggestion) =>
+ handleRecipientAutoCompleteSelect(index, suggestion)
+ }
+ onSearchQueryChange={(query) => {
+ field.onChange(query);
+ setRecipientSearchQuery(query);
+ }}
+ loading={isLoading}
+ data-testid="signer-email-input"
+ maxLength={254}
+ />
+ </FormControl>
+
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ <FormField
+ control={form.control}
+ name={`signers.${index}.name`}
+ render={({ field }) => (
+ <FormItem
+ className={cn('w-full', {
+ 'mb-6':
+ form.formState.errors.signers?.[index] &&
+ !form.formState.errors.signers[index]?.name,
+ })}
>
- <GripVerticalIcon className="h-5 w-5 flex-shrink-0 opacity-40" />
+ {!showAdvancedSettings && index === 0 && (
+ <FormLabel>
+ <Trans>Name</Trans>
+ </FormLabel>
+ )}
+
<FormControl>
- <Input
- type="number"
- max={signers.length}
- data-testid="signing-order-input"
- className={cn(
- 'w-10 text-center',
- '[appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none',
- )}
+ <RecipientAutoCompleteInput
+ type="text"
+ placeholder={t`Recipient ${index + 1}`}
{...field}
- onChange={(e) => {
- field.onChange(e);
- handleSigningOrderChange(index, e.target.value);
- }}
- onBlur={(e) => {
- field.onBlur();
- handleSigningOrderChange(index, e.target.value);
- }}
disabled={
snapshot.isDragging ||
isSubmitting ||
- !canRecipientBeModified(signer.id)
+ !canRecipientBeModified(signer.id) ||
+ isDirectRecipient
+ }
+ options={recipientSuggestions}
+ onSelect={(suggestion) =>
+ handleRecipientAutoCompleteSelect(index, suggestion)
}
+ onSearchQueryChange={(query) => {
+ field.onChange(query);
+ setRecipientSearchQuery(query);
+ }}
+ loading={isLoading}
+ maxLength={255}
/>
</FormControl>
+
<FormMessage />
</FormItem>
)}
/>
- )}
-
- <FormField
- control={form.control}
- name={`signers.${index}.email`}
- render={({ field }) => (
- <FormItem
- className={cn('relative w-full', {
- 'mb-6':
- form.formState.errors.signers?.[index] &&
- !form.formState.errors.signers[index]?.email,
- })}
- >
- {!showAdvancedSettings && index === 0 && (
- <FormLabel>
- <Trans>Email</Trans>
- </FormLabel>
- )}
-
- <FormControl>
- <RecipientAutoCompleteInput
- type="email"
- placeholder={t`Email`}
- value={field.value}
- disabled={
- snapshot.isDragging ||
- isSubmitting ||
- !canRecipientBeModified(signer.id)
- }
- options={recipientSuggestions}
- onSelect={(suggestion) =>
- handleRecipientAutoCompleteSelect(index, suggestion)
- }
- onSearchQueryChange={(query) => {
- field.onChange(query);
- setRecipientSearchQuery(query);
- }}
- loading={isLoading}
- data-testid="signer-email-input"
- maxLength={254}
- />
- </FormControl>
-
- <FormMessage />
- </FormItem>
- )}
- />
-
- <FormField
- control={form.control}
- name={`signers.${index}.name`}
- render={({ field }) => (
- <FormItem
- className={cn('w-full', {
- 'mb-6':
- form.formState.errors.signers?.[index] &&
- !form.formState.errors.signers[index]?.name,
- })}
- >
- {!showAdvancedSettings && index === 0 && (
- <FormLabel>
- <Trans>Name</Trans>
- </FormLabel>
- )}
- <FormControl>
- <RecipientAutoCompleteInput
- type="text"
- placeholder={t`Recipient ${index + 1}`}
- {...field}
- disabled={
- snapshot.isDragging ||
- isSubmitting ||
- !canRecipientBeModified(signer.id)
- }
- options={recipientSuggestions}
- onSelect={(suggestion) =>
- handleRecipientAutoCompleteSelect(index, suggestion)
- }
- onSearchQueryChange={(query) => {
- field.onChange(query);
- setRecipientSearchQuery(query);
- }}
- loading={isLoading}
- maxLength={255}
- />
- </FormControl>
-
- <FormMessage />
- </FormItem>
- )}
- />
-
- <FormField
- control={form.control}
- name={`signers.${index}.role`}
- render={({ field }) => (
- <FormItem
- className={cn('mt-auto w-fit', {
- 'mb-6':
- form.formState.errors.signers?.[index] &&
- !form.formState.errors.signers[index]?.role,
- })}
- >
- <FormControl>
- <RecipientRoleSelect
- {...field}
- isAssistantEnabled={isSigningOrderSequential}
- onValueChange={(value) => {
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
- handleRoleChange(index, value as RecipientRole);
- field.onChange(value);
- }}
- disabled={
- snapshot.isDragging ||
- isSubmitting ||
- !canRecipientBeModified(signer.id)
- }
- />
- </FormControl>
-
- <FormMessage />
- </FormItem>
- )}
- />
-
- <Button
- variant="ghost"
- className={cn('mt-auto px-2', {
- 'mb-6': form.formState.errors.signers?.[index],
- })}
- data-testid="remove-signer-button"
- disabled={
- snapshot.isDragging ||
- isSubmitting ||
- !canRecipientBeModified(signer.id) ||
- signers.length === 1
- }
- onClick={() => onRemoveSigner(index)}
- >
- <TrashIcon className="h-4 w-4" />
- </Button>
- </div>
-
- {showAdvancedSettings &&
- organisation.organisationClaim.flags.cfr21 && (
<FormField
control={form.control}
- name={`signers.${index}.actionAuth`}
+ name={`signers.${index}.role`}
render={({ field }) => (
<FormItem
- className={cn('mt-2 w-full', {
+ className={cn('mt-auto w-fit', {
'mb-6':
form.formState.errors.signers?.[index] &&
- !form.formState.errors.signers[index]?.actionAuth,
- 'pl-6': isSigningOrderSequential,
+ !form.formState.errors.signers[index]?.role,
})}
>
<FormControl>
- <RecipientActionAuthSelect
+ <RecipientRoleSelect
{...field}
- onValueChange={field.onChange}
+ isAssistantEnabled={isSigningOrderSequential}
+ onValueChange={(value) => {
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
+ handleRoleChange(index, value as RecipientRole);
+ field.onChange(value);
+ }}
disabled={
snapshot.isDragging ||
isSubmitting ||
@@ -1100,12 +1001,63 @@ export const EnvelopeEditorRecipientForm = () => {
</FormItem>
)}
/>
- )}
- </motion.fieldset>
- </div>
- )}
- </Draggable>
- ))}
+
+ <Button
+ variant="ghost"
+ className={cn('mt-auto px-2', {
+ 'mb-6': form.formState.errors.signers?.[index],
+ })}
+ data-testid="remove-signer-button"
+ disabled={
+ snapshot.isDragging ||
+ isSubmitting ||
+ !canRecipientBeModified(signer.id) ||
+ signers.length === 1 ||
+ isDirectRecipient
+ }
+ onClick={() => onRemoveSigner(index)}
+ >
+ <TrashIcon className="h-4 w-4" />
+ </Button>
+ </div>
+
+ {showAdvancedSettings &&
+ organisation.organisationClaim.flags.cfr21 && (
+ <FormField
+ control={form.control}
+ name={`signers.${index}.actionAuth`}
+ render={({ field }) => (
+ <FormItem
+ className={cn('mt-2 w-full', {
+ 'mb-6':
+ form.formState.errors.signers?.[index] &&
+ !form.formState.errors.signers[index]?.actionAuth,
+ 'pl-6': isSigningOrderSequential,
+ })}
+ >
+ <FormControl>
+ <RecipientActionAuthSelect
+ {...field}
+ onValueChange={field.onChange}
+ disabled={
+ snapshot.isDragging ||
+ isSubmitting ||
+ !canRecipientBeModified(signer.id)
+ }
+ />
+ </FormControl>
+
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ )}
+ </motion.fieldset>
+ </div>
+ )}
+ </Draggable>
+ );
+ })}
{provided.placeholder}
</div>
diff --git a/apps/remix/app/components/general/envelope-editor/envelope-editor.tsx b/apps/remix/app/components/general/envelope-editor/envelope-editor.tsx
index af5eecdd6..cf4da6970 100644
--- a/apps/remix/app/components/general/envelope-editor/envelope-editor.tsx
+++ b/apps/remix/app/components/general/envelope-editor/envelope-editor.tsx
@@ -81,7 +81,7 @@ export default function EnvelopeEditor() {
isAutosaving,
flushAutosave,
relativePath,
- editorFields,
+ syncEnvelope,
} = useCurrentEnvelopeEditor();
const [searchParams, setSearchParams] = useSearchParams();
@@ -278,6 +278,8 @@ export default function EnvelopeEditor() {
templateId={mapSecondaryIdToTemplateId(envelope.secondaryId)}
directLink={envelope.directLink}
recipients={envelope.recipients}
+ onCreateSuccess={async () => await syncEnvelope()}
+ onDeleteSuccess={async () => await syncEnvelope()}
trigger={
<Button variant="ghost" size="sm" className="w-full justify-start">
<LinkIcon className="mr-2 h-4 w-4" />
diff --git a/packages/lib/client-only/hooks/use-editor-recipients.ts b/packages/lib/client-only/hooks/use-editor-recipients.ts
new file mode 100644
index 000000000..314695c02
--- /dev/null
+++ b/packages/lib/client-only/hooks/use-editor-recipients.ts
@@ -0,0 +1,106 @@
+import { useId } from 'react';
+
+import { zodResolver } from '@hookform/resolvers/zod';
+import { DocumentSigningOrder, type Recipient, RecipientRole } from '@prisma/client';
+import type { UseFormReturn } from 'react-hook-form';
+import { useForm } from 'react-hook-form';
+import { prop, sortBy } from 'remeda';
+import { z } from 'zod';
+
+import {
+ ZRecipientActionAuthTypesSchema,
+ ZRecipientAuthOptionsSchema,
+} from '@documenso/lib/types/document-auth';
+import { ZRecipientEmailSchema } from '@documenso/lib/types/recipient';
+
+import type { TEnvelope } from '../../types/envelope';
+
+const LocalRecipientSchema = z.object({
+ formId: z.string().min(1),
+ id: z.number().optional(),
+ email: ZRecipientEmailSchema,
+ name: z.string(),
+ role: z.nativeEnum(RecipientRole),
+ signingOrder: z.number().optional(),
+ actionAuth: z.array(ZRecipientActionAuthTypesSchema).optional().default([]),
+});
+
+type TLocalRecipient = z.infer<typeof LocalRecipientSchema>;
+
+export const ZEditorRecipientsFormSchema = z.object({
+ signers: z.array(LocalRecipientSchema),
+ signingOrder: z.nativeEnum(DocumentSigningOrder),
+ allowDictateNextSigner: z.boolean().default(false),
+});
+
+export type TEditorRecipientsFormSchema = z.infer<typeof ZEditorRecipientsFormSchema>;
+
+type EditorRecipientsProps = {
+ envelope: TEnvelope;
+};
+
+type ResetFormOptions = {
+ recipients?: Recipient[];
+ documentMeta?: TEnvelope['documentMeta'];
+};
+
+type UseEditorRecipientsResponse = {
+ form: UseFormReturn<TEditorRecipientsFormSchema>;
+ resetForm: (options?: ResetFormOptions) => void;
+};
+
+export const useEditorRecipients = ({
+ envelope,
+}: EditorRecipientsProps): UseEditorRecipientsResponse => {
+ const initialId = useId();
+
+ const generateDefaultValues = (options?: ResetFormOptions) => {
+ const { recipients, documentMeta } = options ?? {};
+
+ const formRecipients = (recipients || envelope.recipients).map((recipient, index) => ({
+ id: recipient.id,
+ formId: String(recipient.id),
+ name: recipient.name,
+ email: recipient.email,
+ role: recipient.role,
+ signingOrder: recipient.signingOrder ?? index + 1,
+ actionAuth: ZRecipientAuthOptionsSchema.parse(recipient.authOptions)?.actionAuth ?? undefined,
+ }));
+
+ const signers: TLocalRecipient[] =
+ formRecipients.length > 0
+ ? sortBy(formRecipients, [prop('signingOrder'), 'asc'], [prop('id'), 'asc'])
+ : [
+ {
+ formId: initialId,
+ name: '',
+ email: '',
+ role: RecipientRole.SIGNER,
+ signingOrder: 1,
+ actionAuth: [],
+ },
+ ];
+
+ return {
+ signers,
+ signingOrder: documentMeta?.signingOrder ?? envelope.documentMeta.signingOrder,
+ allowDictateNextSigner:
+ documentMeta?.allowDictateNextSigner ?? envelope.documentMeta.allowDictateNextSigner,
+ };
+ };
+
+ const form = useForm<TEditorRecipientsFormSchema>({
+ defaultValues: generateDefaultValues(),
+ resolver: zodResolver(ZEditorRecipientsFormSchema),
+ mode: 'onChange', // Used for autosave purposes, maybe can try onBlur instead?
+ });
+
+ const resetForm = (options?: ResetFormOptions) => {
+ form.reset(generateDefaultValues(options));
+ };
+
+ return {
+ form,
+ resetForm,
+ };
+};
diff --git a/packages/lib/client-only/providers/envelope-editor-provider.tsx b/packages/lib/client-only/providers/envelope-editor-provider.tsx
index c50c0ff50..bdcfd3217 100644
--- a/packages/lib/client-only/providers/envelope-editor-provider.tsx
+++ b/packages/lib/client-only/providers/envelope-editor-provider.tsx
@@ -15,6 +15,7 @@ import type { TEnvelope } from '../../types/envelope';
import { formatDocumentsPath, formatTemplatesPath } from '../../utils/teams';
import { useEditorFields } from '../hooks/use-editor-fields';
import type { TLocalField } from '../hooks/use-editor-fields';
+import { useEditorRecipients } from '../hooks/use-editor-recipients';
import { useEnvelopeAutosave } from '../hooks/use-envelope-autosave';
export const useDebounceFunction = <Args extends unknown[]>(
@@ -53,6 +54,7 @@ type EnvelopeEditorProviderValue = {
getRecipientColorKey: (recipientId: number) => TRecipientColor;
editorFields: ReturnType<typeof useEditorFields>;
+ editorRecipients: ReturnType<typeof useEditorRecipients>;
isAutosaving: boolean;
flushAutosave: () => Promise<void>;
@@ -101,6 +103,10 @@ export const EnvelopeEditorProvider = ({
handleFieldsUpdate: (fields) => setFieldsDebounced(fields),
});
+ const editorRecipients = useEditorRecipients({
+ envelope,
+ });
+
const envelopeUpdateMutationQuery = trpc.envelope.update.useMutation({
onSuccess: (response, input) => {
setEnvelope({
@@ -291,6 +297,12 @@ export const EnvelopeEditorProvider = ({
if (fetchedEnvelopeData.data) {
setEnvelope(fetchedEnvelopeData.data);
+
+ editorRecipients.resetForm({
+ recipients: fetchedEnvelopeData.data.recipients,
+ documentMeta: fetchedEnvelopeData.data.documentMeta,
+ });
+ editorFields.resetForm(fetchedEnvelopeData.data.fields);
}
};
@@ -348,6 +360,7 @@ export const EnvelopeEditorProvider = ({
setRecipientsDebounced,
setRecipientsAsync,
editorFields,
+ editorRecipients,
autosaveError,
flushAutosave,
isAutosaving,