import { Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, MenuItem, Stack, Switch, TextField } from '@mui/material' import { useState, type FormEvent } from 'react' import { ADDRESS_PATTERN, LISTEN_CHOICES, ZONE_PATTERN, type DnsSettings, type Listen } from './api.ts' /** * A dialog that edits the server's dns section: whether the name server runs, the secondary name server of every * domain, the interfaces it answers on and the TTL, and wan.public_address, the server's public IP the records of the * sites with Public IP take. The primary name server is the server's FQDN, nameserver, shown and not edited. An enabled * name server needs a secondary other than the server's FQDN. onSave sends the section and the public address; the * dialog closes when it succeeds and shows the server's message when it fails. */ export default function DnsSettingsDialog(props: { current: DnsSettings nameserver: string publicAddress: string onSave: (settings: DnsSettings, publicAddress: string) => Promise onCancel: () => void }) { const [enabled, setEnabled] = useState(props.current.enabled) const [secondary, setSecondary] = useState(props.current.secondary) const [publicAddress, setPublicAddress] = useState(props.publicAddress) const [listen, setListen] = useState(props.current.listen) const [ttl, setTtl] = useState(String(props.current.ttl)) const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const secondaryProblem = secondary === '' ? (enabled ? 'Required while enabled.' : '') : !ZONE_PATTERN.test(secondary) ? 'A DNS name in lower case.' : secondary === props.nameserver ? 'The secondary is not the server\'s FQDN, the primary.' : '' const addressProblem = publicAddress === '' || ADDRESS_PATTERN.test(publicAddress) ? '' : 'Not an IP address.' const ttlProblem = /^\d{2,5}$/.test(ttl) && Number(ttl) >= 60 && Number(ttl) <= 86400 ? '' : '60 to 86400 seconds.' const blocked = secondaryProblem !== '' || addressProblem !== '' || ttlProblem !== '' async function submit(event: FormEvent) { event.preventDefault() if (blocked) { return } setSaving(true) setError(null) try { await props.onSave({ enabled, secondary, listen, ttl: Number(ttl) }, publicAddress) } catch (caught) { setError((caught as Error).message) setSaving(false) } } return ( reason !== 'backdropClick' && props.onCancel()} maxWidth="sm" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}> Name server settings {error && {error}} setEnabled(event.target.checked)} />} label="Enabled" /> setSecondary(event.target.value.trim().toLowerCase())} /> setPublicAddress(event.target.value.trim().toLowerCase())} /> setListen(event.target.value as Listen)} sx={{ flex: 1 }}> {LISTEN_CHOICES.map((item) => {item})} setTtl(event.target.value.trim())} sx={{ flex: 1 }} /> ) }