import { useState, type FormEvent } from "react"; import { api, type NewDnsZoneInput } from "../api/client"; import { ErrorNotice } from "./Notice"; import { readableError } from "../utils/errors"; import { t } from "../i18n"; type Kind = NewDnsZoneInput["type"]; type Protocol = "Udp" | "Tcp" | "Tls" | "Https" | "Quic"; const KINDS: { value: Kind; label: string; hint: string }[] = [ { value: "Primary", get label() { return t("Primary zone"); }, hint: "This server holds the zone: its records are made and changed here. It starts with the records Technitium makes for it; the rest are added under Records." }, { value: "Secondary", get label() { return t("Secondary zone"); }, hint: "A copy of a zone another server holds. This server fetches it from there and keeps it up to date; it can only be read here." }, { value: "Forwarder", get label() { return t("Forwarder zone"); }, hint: "Questions about this name (and under it) are passed on to another DNS server — for a domain another server answers, such as a company network." }, ]; /** The servers a person typed, one to a line or separated by commas or spaces. */ const serversOf = (text: string) => text.split(/[\s,]+/).map((s) => s.trim()).filter(Boolean); /** * Making a zone on a DNS provider that can: Technitium. A name (a domain such as home.lan, or an address or network such as 192.168.1.0/24 for a reverse zone) and what kind. What the provider says * when it won't — a zone that is there already — is shown as it was said. An administrator's alone, as it makes the server an authority for the name. */ export default function DnsNewZone({ providerId, providerName, onCreated, onCancel }: { providerId: number; providerName: string; onCreated: (zone: { id: string; name: string }) => void; onCancel: () => void }) { const [kind, setKind] = useState("Primary"); const [name, setName] = useState(""); const [primaries, setPrimaries] = useState(""); const [forwarder, setForwarder] = useState(""); const [protocol, setProtocol] = useState("Udp"); const [dnssec, setDnssec] = useState(false); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); async function submit(e: FormEvent) { e.preventDefault(); if (busy) return; setBusy(true); setError(null); try { const body: NewDnsZoneInput = kind === "Primary" ? { type: "Primary", name } : kind === "Secondary" ? { type: "Secondary", name, primaryNameServers: serversOf(primaries) } : { type: "Forwarder", name, forwarder: forwarder.trim(), protocol, dnssecValidation: dnssec }; const { zone } = await api.dns.zones.create(providerId, body); onCreated(zone); } catch (err) { setError(readableError(err)); setBusy(false); } } return (

New zone on {providerName}

{KINDS.find((k) => k.value === kind)!.hint}
setName(e.target.value)} />
Written in lower case, ASCII (a name with other letters in its xn-- form), with no dot at the end.
{kind === "Secondary" && (
setPrimaries(e.target.value)} />
{t("Addresses or host names, separated by commas.")}
)} {kind === "Forwarder" && (
setForwarder(e.target.value)} />
)}
); }