import { FC, useState } from "react"; import { Controller, useForm } from "react-hook-form"; // hooks import { useInstance } from "@/hooks"; // ui import { Button, TOAST_TYPE, ToggleSwitch, setToast } from "@plane/ui"; // components import { ControllerInput, TControllerInputFormField } from "components/common"; import { SendTestEmailModal } from "./test-email-modal"; // types import { IFormattedInstanceConfiguration, TInstanceEmailConfigurationKeys } from "@plane/types"; type IInstanceEmailForm = { config: IFormattedInstanceConfiguration; }; type EmailFormValues = Record; export const InstanceEmailForm: FC = (props) => { const { config } = props; // states const [isSendTestEmailModalOpen, setIsSendTestEmailModalOpen] = useState(false); // store hooks const { updateInstanceConfigurations } = useInstance(); // form data const { handleSubmit, watch, control, formState: { errors, isSubmitting }, } = useForm({ defaultValues: { EMAIL_HOST: config["EMAIL_HOST"], EMAIL_PORT: config["EMAIL_PORT"], EMAIL_HOST_USER: config["EMAIL_HOST_USER"], EMAIL_HOST_PASSWORD: config["EMAIL_HOST_PASSWORD"], EMAIL_USE_TLS: config["EMAIL_USE_TLS"], // EMAIL_USE_SSL: config["EMAIL_USE_SSL"], EMAIL_FROM: config["EMAIL_FROM"], }, }); const emailFormFields: TControllerInputFormField[] = [ { key: "EMAIL_HOST", type: "text", label: "Host", placeholder: "email.google.com", error: Boolean(errors.EMAIL_HOST), required: true, }, { key: "EMAIL_PORT", type: "text", label: "Port", placeholder: "8080", error: Boolean(errors.EMAIL_PORT), required: true, }, { key: "EMAIL_HOST_USER", type: "text", label: "Username", placeholder: "getitdone@projectplane.so", error: Boolean(errors.EMAIL_HOST_USER), required: true, }, { key: "EMAIL_HOST_PASSWORD", type: "password", label: "Password", placeholder: "Password", error: Boolean(errors.EMAIL_HOST_PASSWORD), required: true, }, { key: "EMAIL_FROM", type: "text", label: "From address", description: "This is the email address your users will see when getting emails from this instance. You will need to verify this address.", placeholder: "no-reply@projectplane.so", error: Boolean(errors.EMAIL_FROM), required: true, }, ]; const onSubmit = async (formData: EmailFormValues) => { const payload: Partial = { ...formData }; await updateInstanceConfigurations(payload) .then(() => setToast({ type: TOAST_TYPE.SUCCESS, title: "Success", message: "Email Settings updated successfully", }) ) .catch((err) => console.error(err)); }; return (
setIsSendTestEmailModalOpen(false)} />
{emailFormFields.map((field) => ( ))}
Turn TLS {Boolean(parseInt(watch("EMAIL_USE_TLS"))) ? "off" : "on"}
Use this if your email domain supports TLS.
( { Boolean(parseInt(value)) === true ? onChange("0") : onChange("1"); }} size="sm" /> )} />
); };