"use client"; import { SettingsSection, SettingsSectionBody, SettingsSectionDescription, SettingsSectionForm, SettingsSectionHeader, SettingsSectionTitle } from "@app/components/Settings"; import { zodResolver } from "@hookform/resolvers/zod"; import { useTranslations } from "next-intl"; import z from "zod"; import { createPolicySchema, type PolicyFormValues } from "."; import { SwitchInput } from "@app/components/SwitchInput"; import { Tag, TagInput } from "@app/components/tags/tag-input"; import { Alert, AlertDescription, AlertTitle } from "@app/components/ui/alert"; import { Button } from "@app/components/ui/button"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel } from "@app/components/ui/form"; import { InfoPopup } from "@app/components/ui/info-popup"; import { InfoIcon, Plus } from "lucide-react"; import { useEffect, useState } from "react"; import { type UseFormReturn, useForm, useWatch } from "react-hook-form"; // ─── CreatePolicyOtpEmailSectionForm ────────────────────────────────────────── export type CreatePolicyOtpEmailSectionFormProps = { form: UseFormReturn; emailEnabled: boolean; }; export function CreatePolicyOtpEmailSectionForm({ form: parentForm, emailEnabled }: CreatePolicyOtpEmailSectionFormProps) { const t = useTranslations(); const [isExpanded, setIsExpanded] = useState(false); const [activeEmailTagIndex, setActiveEmailTagIndex] = useState< number | null >(null); const form = useForm({ resolver: zodResolver( createPolicySchema.pick({ emailWhitelistEnabled: true, emails: true }) ), defaultValues: { emailWhitelistEnabled: false, emails: [] } }); useEffect(() => { const subscription = form.watch((values) => { parentForm.setValue( "emailWhitelistEnabled", values.emailWhitelistEnabled as boolean ); parentForm.setValue("emails", values.emails as [Tag, ...Tag[]]); }); return () => subscription.unsubscribe(); }, [form, parentForm]); const whitelistEnabled = useWatch({ control: form.control, name: "emailWhitelistEnabled" }); if (!isExpanded) { return ( {t("otpEmailTitle")} {t("otpEmailTitleDescription")} ); } return (
{t("otpEmailTitle")} {t("otpEmailTitleDescription")} {!emailEnabled && ( {t("otpEmailSmtpRequired")} {t("otpEmailSmtpRequiredDescription")} )} { form.setValue("emailWhitelistEnabled", val); }} disabled={!emailEnabled} /> {whitelistEnabled && emailEnabled && ( ( {/* @ts-ignore */} { return z .email() .or( z .string() .regex( /^\*@[\w.-]+\.[a-zA-Z]{2,}$/, { message: t( "otpEmailErrorInvalid" ) } ) ) .safeParse(tag).success; }} setActiveTagIndex={ setActiveEmailTagIndex } placeholder={t("otpEmailEnter")} tags={form.getValues().emails} setTags={(newEmails) => { form.setValue( "emails", newEmails as [ Tag, ...Tag[] ] ); }} allowDuplicates={false} sortTags={true} /> {t("otpEmailEnterDescription")} )} /> )}
); }