import { zodResolver } from '@hookform/resolvers/zod'; import { msg } from '@lingui/core/macro'; import { useLingui } from '@lingui/react/macro'; import { Trans } from '@lingui/react/macro'; import type { TeamGlobalSettings } from '@prisma/client'; import { DocumentVisibility, OrganisationType } from '@prisma/client'; import { useForm } from 'react-hook-form'; import { z } from 'zod'; import { useCurrentOrganisation } from '@documenso/lib/client-only/providers/organisation'; import { useSession } from '@documenso/lib/client-only/providers/session'; import { DATE_FORMATS } from '@documenso/lib/constants/date-formats'; import { DOCUMENT_SIGNATURE_TYPES, DocumentSignatureType } from '@documenso/lib/constants/document'; import { SUPPORTED_LANGUAGES, SUPPORTED_LANGUAGE_CODES, isValidLanguageCode, } from '@documenso/lib/constants/i18n'; import { TIME_ZONES } from '@documenso/lib/constants/time-zones'; import { type TDocumentMetaDateFormat, ZDocumentMetaTimezoneSchema, } from '@documenso/lib/types/document-meta'; import { isPersonalLayout } from '@documenso/lib/utils/organisations'; import { extractTeamSignatureSettings } from '@documenso/lib/utils/teams'; import { DocumentSignatureSettingsTooltip } from '@documenso/ui/components/document/document-signature-settings-tooltip'; import { Alert } from '@documenso/ui/primitives/alert'; import { Button } from '@documenso/ui/primitives/button'; import { Combobox } from '@documenso/ui/primitives/combobox'; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@documenso/ui/primitives/form/form'; import { MultiSelectCombobox } from '@documenso/ui/primitives/multi-select-combobox'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@documenso/ui/primitives/select'; /** * Can't infer this from the schema since we need to keep the schema inside the component to allow * it to be dynamic. */ export type TDocumentPreferencesFormSchema = { documentVisibility: DocumentVisibility | null; documentLanguage: (typeof SUPPORTED_LANGUAGE_CODES)[number] | null; documentTimezone: string | null; documentDateFormat: TDocumentMetaDateFormat | null; includeSenderDetails: boolean | null; includeSigningCertificate: boolean | null; includeAuditLog: boolean | null; signatureTypes: DocumentSignatureType[]; }; type SettingsSubset = Pick< TeamGlobalSettings, | 'documentVisibility' | 'documentLanguage' | 'documentTimezone' | 'documentDateFormat' | 'includeSenderDetails' | 'includeSigningCertificate' | 'includeAuditLog' | 'typedSignatureEnabled' | 'uploadSignatureEnabled' | 'drawSignatureEnabled' >; export type DocumentPreferencesFormProps = { settings: SettingsSubset; canInherit: boolean; onFormSubmit: (data: TDocumentPreferencesFormSchema) => Promise; }; export const DocumentPreferencesForm = ({ settings, onFormSubmit, canInherit, }: DocumentPreferencesFormProps) => { const { t } = useLingui(); const { user, organisations } = useSession(); const currentOrganisation = useCurrentOrganisation(); const isPersonalLayoutMode = isPersonalLayout(organisations); const isPersonalOrganisation = currentOrganisation.type === OrganisationType.PERSONAL; const placeholderEmail = user.email ?? 'user@example.com'; const ZDocumentPreferencesFormSchema = z.object({ documentVisibility: z.nativeEnum(DocumentVisibility).nullable(), documentLanguage: z.enum(SUPPORTED_LANGUAGE_CODES).nullable(), documentTimezone: z.string().nullable(), documentDateFormat: ZDocumentMetaTimezoneSchema.nullable(), includeSenderDetails: z.boolean().nullable(), includeSigningCertificate: z.boolean().nullable(), includeAuditLog: z.boolean().nullable(), signatureTypes: z.array(z.nativeEnum(DocumentSignatureType)).min(canInherit ? 0 : 1, { message: msg`At least one signature type must be enabled`.id, }), }); const form = useForm({ defaultValues: { documentVisibility: settings.documentVisibility, documentLanguage: isValidLanguageCode(settings.documentLanguage) ? settings.documentLanguage : null, documentTimezone: settings.documentTimezone, // eslint-disable-next-line @typescript-eslint/consistent-type-assertions documentDateFormat: settings.documentDateFormat as TDocumentMetaDateFormat | null, includeSenderDetails: settings.includeSenderDetails, includeSigningCertificate: settings.includeSigningCertificate, includeAuditLog: settings.includeAuditLog, signatureTypes: extractTeamSignatureSettings({ ...settings }), }, resolver: zodResolver(ZDocumentPreferencesFormSchema), }); return (
{!isPersonalLayoutMode && ( ( Default Document Visibility Controls the default visibility of an uploaded document. )} /> )} ( Default Document Language Controls the default language of an uploaded document. This will be used as the language in email communications with the recipients. )} /> ( Default Date Format )} /> ( Default Time Zone field.onChange(value)} testId="document-timezone-trigger" /> )} /> ( Default Signature Settings ({ label: t(option.label), value: option.value, }))} selectedValues={field.value} onChange={field.onChange} className="bg-background w-full" enableSearch={false} emptySelectionPlaceholder={ canInherit ? t`Inherit from organisation` : t`Select signature types` } testId="signature-types-trigger" /> {form.formState.errors.signatureTypes ? ( ) : ( Controls which signatures are allowed to be used when signing a document. )} )} /> {!isPersonalLayoutMode && !isPersonalOrganisation && ( ( Send on Behalf of Team
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.
)} /> )} ( Include the Signing Certificate in the Document Controls whether the signing certificate will be included in the document when it is downloaded. The signing certificate can still be downloaded from the logs page separately. )} /> ( Include the Audit Logs in the Document Controls whether the audit logs will be included in the document when it is downloaded. The audit logs can still be downloaded from the logs page separately. )} />
); };