import { ZNameSchema } from '@documenso/lib/types/name'; 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, useLingui } from '@lingui/react/macro'; import { useForm } from 'react-hook-form'; import { z } from 'zod'; const ZEmailTransportFormSchema = z.object({ name: ZNameSchema, fromName: ZNameSchema, fromAddress: z.string().email(), type: z.enum(['SMTP_AUTH', 'SMTP_API', 'RESEND', 'MAILCHANNELS']), host: z.string().optional(), port: z.coerce.number().int().positive().optional(), secure: z.boolean().optional(), ignoreTLS: z.boolean().optional(), username: z.string().optional(), password: z.string().optional(), service: z.string().optional(), apiKey: z.string().optional(), apiKeyUser: z.string().optional(), endpoint: z.string().optional(), }); export type EmailTransportFormValues = z.infer; type EmailTransportFormProps = { defaultValues?: Partial; isEdit?: boolean; onFormSubmit: (values: EmailTransportFormValues) => Promise; formSubmitTrigger?: React.ReactNode; }; export const EmailTransportForm = ({ defaultValues, isEdit = false, onFormSubmit, formSubmitTrigger, }: EmailTransportFormProps) => { const { t } = useLingui(); const form = useForm({ resolver: zodResolver(ZEmailTransportFormSchema), defaultValues: { name: '', fromName: '', fromAddress: '', type: 'SMTP_AUTH', secure: false, ignoreTLS: false, ...defaultValues, }, }); const type = form.watch('type'); const secretPlaceholder = isEdit ? t`Leave blank to keep current` : undefined; return (
( Name )} />
( From name )} /> ( From address )} />
( Transport type {isEdit && ( Transport type cannot be changed after creation. )} )} /> {(type === 'SMTP_AUTH' || type === 'SMTP_API') && (
( Host )} /> ( Port )} />
)} {type === 'SMTP_AUTH' && ( <> ( Username )} /> ( Password )} /> )} {type === 'SMTP_API' && ( ( API key )} /> )} {(type === 'RESEND' || type === 'MAILCHANNELS') && ( ( API key )} /> )} {type === 'MAILCHANNELS' && ( ( Endpoint (optional) )} /> )} {formSubmitTrigger}
); }; /** * Maps flat form values to the tRPC `config` discriminated union. */ export const emailTransportFormToConfig = (values: EmailTransportFormValues) => { switch (values.type) { case 'SMTP_AUTH': return { type: 'SMTP_AUTH' as const, host: values.host ?? '', port: values.port ?? 587, secure: values.secure ?? false, ignoreTLS: values.ignoreTLS ?? false, username: values.username || undefined, password: values.password || undefined, service: values.service || undefined, }; case 'SMTP_API': return { type: 'SMTP_API' as const, host: values.host ?? '', port: values.port ?? 587, secure: values.secure ?? false, apiKey: values.apiKey || '', apiKeyUser: values.apiKeyUser || undefined, }; case 'RESEND': return { type: 'RESEND' as const, apiKey: values.apiKey || '' }; case 'MAILCHANNELS': return { type: 'MAILCHANNELS' as const, apiKey: values.apiKey || '', endpoint: values.endpoint || undefined, }; } };