import { Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, MenuItem, Stack, TextField, Typography } 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 but whether the name server runs, which the System tab switches: 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. The secondary is optional and is not the server's FQDN. onSave sends the * settings 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: Omit, publicAddress: string) => Promise onCancel: () => void }) { 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 === '' ? '' : !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({ 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}} The name server is {props.current.enabled ? 'on' : 'off'}; it is switched on and off on the System tab. 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 }} /> ) }