import { useCurrentOrganisation } from '@documenso/lib/client-only/providers/organisation'; import { useSession } from '@documenso/lib/client-only/providers/session'; import { FROM_ADDRESS } from '@documenso/lib/constants/email'; import { DEFAULT_DOCUMENT_EMAIL_SETTINGS, ZDocumentEmailSettingsSchema } from '@documenso/lib/types/document-email'; import { zEmail } from '@documenso/lib/utils/zod'; import { trpc } from '@documenso/trpc/react'; import { DocumentEmailCheckboxes } from '@documenso/ui/components/document/document-email-checkboxes'; import { Alert } from '@documenso/ui/primitives/alert'; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@documenso/ui/primitives/form/form'; import { Input } from '@documenso/ui/primitives/input'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@documenso/ui/primitives/select'; import { zodResolver } from '@hookform/resolvers/zod'; import { Trans } from '@lingui/react/macro'; import { OrganisationType, type TeamGlobalSettings } from '@prisma/client'; import { useForm } from 'react-hook-form'; import { z } from 'zod'; import { FormStickySaveBar } from './form-sticky-save-bar'; import { InheritableField } from './inheritable-field'; const ZEmailPreferencesFormSchema = z.object({ emailId: z.string().nullable(), emailReplyTo: zEmail().nullable(), // emailReplyToName: z.string(), emailDocumentSettings: ZDocumentEmailSettingsSchema.nullable(), includeSenderDetails: z.boolean().nullable(), }); export type TEmailPreferencesFormSchema = z.infer; type SettingsSubset = Pick< TeamGlobalSettings, 'emailId' | 'emailReplyTo' | 'emailDocumentSettings' | 'includeSenderDetails' >; export type EmailPreferencesFormProps = { settings: SettingsSubset; canInherit: boolean; onFormSubmit: (data: TEmailPreferencesFormSchema) => Promise; }; export const EmailPreferencesForm = ({ settings, onFormSubmit, canInherit }: EmailPreferencesFormProps) => { const { user } = useSession(); const organisation = useCurrentOrganisation(); const isPersonalOrganisation = organisation.type === OrganisationType.PERSONAL; const placeholderEmail = user.email ?? 'user@example.com'; const form = useForm({ defaultValues: { emailId: settings.emailId, emailReplyTo: settings.emailReplyTo, // emailReplyToName: settings.emailReplyToName, emailDocumentSettings: settings.emailDocumentSettings, includeSenderDetails: settings.includeSenderDetails, }, resolver: zodResolver(ZEmailPreferencesFormSchema), }); const { data: emailData, isLoading: isLoadingEmails } = trpc.enterprise.organisation.email.find.useQuery({ organisationId: organisation.id, perPage: 100, }); const emails = emailData?.data || []; const handleFormSubmit = form.handleSubmit(async (data) => { try { await onFormSubmit(data); } catch { // The page handler surfaces its own error toast. Keep the form dirty so // the save bar stays visible and the user can retry. return; } form.reset(data); }); return (
{organisation.organisationClaim.flags.emailDomains && ( ( Default Email The default email to use when sending emails to recipients )} /> )} ( Reply to email} testId="email-reply-to" > field.onChange(value.target.value || null)} placeholder="noreply@example.com" type="email" /> The email address which will show up in the "Reply To" field in emails {canInherit && ( {'. '} Leave blank to inherit from the organisation. )} )} /> {/* ( Reply to name )} /> */} ( Default Email Settings} testId="email-document-settings" > {canInherit && ( )} {field.value && (
field.onChange(value)} />
)} Controls the default email settings when new documents or templates are created. Updating these settings will not affect existing documents or templates.
)} /> {!isPersonalOrganisation && ( ( Send on Behalf of Team} testId="include-sender-details" >
Preview
{field.value ? ( "{placeholderEmail}" on behalf of "Team Name" has invited you to sign "example document". ) : ( "Team Name" has invited you to sign "example document". )}
Controls the formatting of the message that will be sent when inviting a recipient to sign a document. If a custom message has been provided while configuring the document, it will be used instead.
)} /> )} form.reset()} />
); };