import { useEffect, useState, type FormEvent } from "react"; import { api, type DnsZone, type DnsZoneOptionsChange, type DnsZoneOptionsInfo } from "../api/client"; import { ErrorNotice } from "./Notice"; import { confirmDialog } from "../utils/dialogs"; import { readableError } from "../utils/errors"; import { toast } from "../utils/toast"; const QUERY_ACCESS: [string, string][] = [ ["Allow", "Everyone"], ["Deny", "Nobody"], ["AllowOnlyPrivateNetworks", "Only private networks"], ["AllowOnlyZoneNameServers", "Only the zone's name servers"], ["UseSpecifiedNetworkACL", "Only the networks listed"], ["AllowZoneNameServersAndUseSpecifiedNetworkACL", "The zone's name servers and the networks listed"], ]; const ZONE_TRANSFER: [string, string][] = [ ["Deny", "Nobody"], ["Allow", "Everyone"], ["AllowOnlyZoneNameServers", "Only the zone's name servers"], ["UseSpecifiedNetworkACL", "Only the networks listed"], ["AllowZoneNameServersAndUseSpecifiedNetworkACL", "The zone's name servers and the networks listed"], ]; const NOTIFY: [string, string][] = [ ["None", "Nobody"], ["ZoneNameServers", "The zone's name servers"], ["SpecifiedNameServers", "Only the servers listed"], ["BothZoneAndSpecifiedNameServers", "The zone's name servers and the servers listed"], ]; const usesAcl = (mode: string) => mode === "UseSpecifiedNetworkACL" || mode === "AllowZoneNameServersAndUseSpecifiedNetworkACL"; const usesServers = (mode: string) => mode === "SpecifiedNameServers" || mode === "BothZoneAndSpecifiedNameServers"; /** What was typed as a list: one to a line, or separated by commas or spaces. */ const listOf = (text: string) => text.split(/[\s,]+/).map((s) => s.trim()).filter(Boolean); const NAMES: Record = { queryAccess: "who may ask", zoneTransfer: "who may copy it", notify: "who is told" }; function Choice({ id, label, value, options, onChange }: { id: string; label: string; value: string; options: [string, string][]; onChange: (v: string) => void }) { return (
); } /** * Editing one zone of a provider that can (Technitium): switching it off and on, and who may ask it, who may copy it and who is told when it changes. It reads the zone's options when it opens; * what Save sends is checked against the kind of zone, and if the server also changed something that was not asked, this says so. Only the groups that apply to the kind of zone are shown. */ export default function DnsZoneEditor({ providerId, zone, onChanged, onClose }: { providerId: number; zone: DnsZone; onChanged: () => void; onClose: () => void }) { const [options, setOptions] = useState(null); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); const [unexpected, setUnexpected] = useState([]); const [queryAccess, setQueryAccess] = useState("Allow"); const [queryAcl, setQueryAcl] = useState(""); const [transfer, setTransfer] = useState("Deny"); const [transferAcl, setTransferAcl] = useState(""); const [keys, setKeys] = useState([]); const [notify, setNotify] = useState("None"); const [notifyServers, setNotifyServers] = useState(""); const fill = (o: DnsZoneOptionsInfo) => { setOptions(o); setQueryAccess(o.queryAccess ?? "Allow"); setQueryAcl(o.queryAccessNetworkACL.join("\n")); setTransfer(o.zoneTransfer ?? "Deny"); setTransferAcl(o.zoneTransferNetworkACL.join("\n")); setKeys(o.zoneTransferTsigKeyNames); setNotify(o.notify ?? "None"); setNotifyServers(o.notifyNameServers.join("\n")); }; useEffect(() => { setOptions(null); setError(null); setUnexpected([]); api.dns.zones .options(providerId, zone.id) .then((r) => fill(r.options)) .catch((err) => setError(readableError(err))); }, [providerId, zone.id]); const type = options?.type ?? zone.type ?? ""; const hasQuery = type !== "SecondaryCatalog"; const hasTransferAndNotify = type === "Primary" || type === "Secondary"; async function switchTo(on: boolean) { if (!options) return; if (!on && !(await confirmDialog({ title: "Switch off zone", message: `Switch off "${zone.name}"? The server stops answering for it. Its records stay, and switching it on again brings it all back.`, confirmLabel: "Switch off", danger: true }))) return; setBusy(true); setError(null); try { await api.dns.zones.setEnabled(providerId, zone.id, on); toast.success(`${zone.name} is ${on ? "on" : "off"}.`); setOptions({ ...options, disabled: !on }); onChanged(); } catch (err) { setError(readableError(err)); } finally { setBusy(false); } } async function save(e: FormEvent) { e.preventDefault(); if (!options || busy) return; const change: DnsZoneOptionsChange = {}; if (hasQuery) { change.queryAccess = queryAccess; if (usesAcl(queryAccess)) change.queryAccessNetworkACL = listOf(queryAcl); } if (hasTransferAndNotify) { change.zoneTransfer = transfer; if (usesAcl(transfer)) change.zoneTransferNetworkACL = listOf(transferAcl); change.zoneTransferTsigKeyNames = keys; change.notify = notify; if (usesServers(notify)) change.notifyNameServers = listOf(notifyServers); } setBusy(true); setError(null); setUnexpected([]); try { const r = await api.dns.zones.updateOptions(providerId, zone.id, change); if (r.options) fill(r.options); setUnexpected(r.unexpected); toast.success(r.changed.length === 0 ? "Nothing was different." : `Saved: ${r.changed.map((c) => NAMES[c] ?? c).join(", ")}.`); } catch (err) { setError(readableError(err)); } finally { setBusy(false); } } return (

Edit {zone.name}

{!options && !error &&
Reading how the zone is set…
} {options && ( <>
{type ? `${type} zone — ` : ""} {options.disabled ? switched off: the server does not answer for it. : "on."}
{!hasQuery && !hasTransferAndNotify ? (
There are no options to edit on this kind of zone from here.
) : (
{hasQuery && (
Who may ask this zone {usesAcl(queryAccess) && (