"use client"; import { Button } from "@app/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@app/components/ui/form"; import { Input } from "@app/components/ui/input"; import { useToast } from "@app/hooks/useToast"; import { zodResolver } from "@hookform/resolvers/zod"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { Credenza, CredenzaBody, CredenzaClose, CredenzaContent, CredenzaDescription, CredenzaFooter, CredenzaHeader, CredenzaTitle } from "@app/components/Credenza"; import { createApiClient } from "@app/lib/api"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { useTranslations } from "next-intl"; import { formatAxiosError } from "@app/lib/api"; import { CreateDomainResponse } from "@server/routers/domain/createOrgDomain"; import { StrategySelect } from "@app/components/StrategySelect"; import { AxiosResponse } from "axios"; import { Alert, AlertDescription, AlertTitle } from "@app/components/ui/alert"; import { InfoIcon, AlertTriangle } from "lucide-react"; import CopyToClipboard from "@app/components/CopyToClipboard"; import { InfoSection, InfoSectionContent, InfoSections, InfoSectionTitle } from "@app/components/InfoSection"; import { useOrgContext } from "@app/hooks/useOrgContext"; import { build } from "@server/build"; const formSchema = z.object({ baseDomain: z.string().min(1, "Domain is required"), type: z.enum(["ns", "cname", "wildcard"]) }); type FormValues = z.infer; type CreateDomainFormProps = { open: boolean; setOpen: (open: boolean) => void; onCreated?: (domain: CreateDomainResponse) => void; }; export default function CreateDomainForm({ open, setOpen, onCreated }: CreateDomainFormProps) { const [loading, setLoading] = useState(false); const [createdDomain, setCreatedDomain] = useState(null); const api = createApiClient(useEnvContext()); const t = useTranslations(); const { toast } = useToast(); const { org } = useOrgContext(); const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { baseDomain: "", type: build == "oss" ? "wildcard" : "ns" } }); function reset() { form.reset(); setLoading(false); setCreatedDomain(null); } async function onSubmit(values: FormValues) { setLoading(true); try { const response = await api.put>( `/org/${org.org.orgId}/domain`, values ); const domainData = response.data.data; setCreatedDomain(domainData); toast({ title: t("success"), description: t("domainCreatedDescription") }); onCreated?.(domainData); } catch (e) { toast({ title: t("error"), description: formatAxiosError(e), variant: "destructive" }); } finally { setLoading(false); } } const domainType = form.watch("type"); const baseDomain = form.watch("baseDomain"); let domainOptions: any = []; if (build == "enterprise" || build == "saas") { domainOptions = [ { id: "ns", title: t("selectDomainTypeNsName"), description: t("selectDomainTypeNsDescription") }, { id: "cname", title: t("selectDomainTypeCnameName"), description: t("selectDomainTypeCnameDescription") } ]; } else if (build == "oss") { domainOptions = [ { id: "wildcard", title: t("selectDomainTypeWildcardName"), description: t("selectDomainTypeWildcardDescription") } ]; } return ( { setOpen(val); reset(); }} > {t("domainAdd")} {t("domainAddDescription")} {!createdDomain ? (
( )} /> ( {t("domain")} )} /> ) : (
{t("createDomainAddDnsRecords")} {t("createDomainAddDnsRecordsDescription")}
{createdDomain.nsRecords && createdDomain.nsRecords.length > 0 && (

{t("createDomainNsRecords")}

{t("createDomainRecord")}
{t( "createDomainType" )} NS
{t( "createDomainName" )} {baseDomain}
{t( "createDomainValue" )} {createdDomain.nsRecords.map( ( nsRecord, index ) => (
) )}
)} {createdDomain.cnameRecords && createdDomain.cnameRecords.length > 0 && (

{t("createDomainCnameRecords")}

{createdDomain.cnameRecords.map( (cnameRecord, index) => ( {t( "createDomainRecordNumber", { number: index + 1 } )}
{t( "createDomainType" )} CNAME
{t( "createDomainName" )} { cnameRecord.baseDomain }
{t( "createDomainValue" )}
) )}
)} {createdDomain.aRecords && createdDomain.aRecords.length > 0 && (

{t("createDomainARecords")}

{createdDomain.aRecords.map( (aRecord, index) => ( {t( "createDomainRecordNumber", { number: index + 1 } )}
{t( "createDomainType" )} A
{t( "createDomainName" )} { aRecord.baseDomain }
{t( "createDomainValue" )} { aRecord.value }
) )}
)} {createdDomain.txtRecords && createdDomain.txtRecords.length > 0 && (

{t("createDomainTxtRecords")}

{createdDomain.txtRecords.map( (txtRecord, index) => ( {t( "createDomainRecordNumber", { number: index + 1 } )}
{t( "createDomainType" )} TXT
{t( "createDomainName" )} { txtRecord.baseDomain }
{t( "createDomainValue" )}
) )}
)}
{build == "saas" || (build == "enterprise" && ( {t("createDomainSaveTheseRecords")} {t( "createDomainSaveTheseRecordsDescription" )} ))} {t("createDomainDnsPropagation")} {t("createDomainDnsPropagationDescription")}
)}
{!createdDomain && ( )}
); }