import Add from '@mui/icons-material/Add' import Delete from '@mui/icons-material/Delete' import Edit from '@mui/icons-material/Edit' import Refresh from '@mui/icons-material/Refresh' import Settings from '@mui/icons-material/Settings' import { Alert, Box, Button, Chip, IconButton, Table, TableBody, TableCell, TableHead, TableRow, Tooltip } from '@mui/material' import { useCallback, useEffect, useState } from 'react' import ConfirmDialog, { type ConfirmDialogProps } from './ConfirmDialog.tsx' import DnsDomainDialog from './DnsDomainDialog.tsx' import DnsRecordDialog from './DnsRecordDialog.tsx' import DnsSettingsDialog from './DnsSettingsDialog.tsx' import { Pairs, Section } from './ServerPanel.tsx' import Toolbar from './Toolbar.tsx' import { ApiError, DNS_DOMAIN_REMOVED_SHAPE, DNS_DOMAIN_SHAPE, DNS_RECORD_SHAPE, DNS_REMOVED_SHAPE, DNS_SETTINGS_SHAPE, DNS_SHAPE, request, type Dns, type DnsDomain, type DnsRecord, } from './api.ts' /** A table of records: name, type, content and TTL, with a remove action when onRemove is given. */ function Records(props: { records: DnsRecord[]; busy: boolean; onRemove?: (record: DnsRecord) => void }) { return ( Name Type Content TTL {props.onRemove && Actions} {props.records.length === 0 && ( None. )} {props.records.map((record) => ( {record.name} {record.type} {record.content} {record.ttl} {props.onRemove && ( props.onRemove?.(record)}> )} ))}
) } /** * The server's name server, from its /dns: the settings of the dns section with whether PowerDNS is installed and runs, * its primary name server, the server's FQDN, and the server's public IP, edited with Settings; the domains it is the * primary of: add, edit and remove; and for the selected domain its manual records: add and remove, and the records it * serves. The records of the web sites follow the DNS setting of each site on the Sites page. */ export default function DnsPanel() { const [dns, setDns] = useState(null) const [error, setError] = useState(null) const [unavailable, setUnavailable] = useState(false) const [busy, setBusy] = useState(false) const [settings, setSettings] = useState(false) const [selected, setSelected] = useState(null) const [editing, setEditing] = useState(null) const [addingRecord, setAddingRecord] = useState(false) const [confirm, setConfirm] = useState(null) const refresh = useCallback(async () => { setBusy(true) try { setDns(await request('GET', '/dns', DNS_SHAPE)) setError(null) setUnavailable(false) } catch (caught) { const failure = caught as ApiError setDns(null) setUnavailable(failure.status === 404) setError(failure.message) } finally { setBusy(false) } }, []) useEffect(() => { void refresh() }, [refresh]) /** Ask for a confirmation, then send the removal and refresh. */ function confirmRemove(title: string, message: string, send: () => Promise) { setConfirm({ title, message, confirmLabel: 'Remove', destructive: true, onCancel: () => setConfirm(null), onConfirm: async () => { setConfirm(null) setBusy(true) try { await send() setError(null) } catch (caught) { setError((caught as Error).message) } await refresh() }, }) } const section = dns?.settings const domain = dns?.domains.find((item) => item.name === selected) ?? null const path = domain ? `/dns/domains/${encodeURIComponent(domain.name)}` : '' function removeDomain(item: DnsDomain) { confirmRemove('Remove domain', `Remove the domain ${item.name} with its manual records?`, () => request('DELETE', `/dns/domains/${encodeURIComponent(item.name)}`, DNS_DOMAIN_REMOVED_SHAPE)) } function removeRecord(record: DnsRecord) { const query = new URLSearchParams({ name: `${record.name}.`, type: record.type, content: record.content }) confirmRemove('Remove record', `Remove the ${record.type} record of ${record.name}?`, () => request('DELETE', `${path}/records?${query.toString()}`, DNS_REMOVED_SHAPE)) } return ( {error && {error}} {dns?.problem && section?.enabled && {dns.problem}} {dns && section && (
], ['PowerDNS', dns.installed ? 'installed' : 'not installed'], ['Primary name server', dns.nameserver || '-'], ['Secondary name server', section.secondary || (section.enabled ? 'not set' : '-')], ['Public IP address', dns.public_address || '-'], ['Listen', section.listen], ['TTL', `${section.ttl} seconds`], ]} />
Domain Manual records TTL Actions {dns.domains.length === 0 && ( None. )} {dns.domains.map((item) => ( setSelected(item.name)}> {item.name} {item.manual.length} {item.ttl ?? section.ttl} event.stopPropagation()}> setEditing(item)}> removeDomain(item)}> ))}
{domain && ( <>
)}
)} {settings && section && ( setSettings(false)} onSave={async (values, publicAddress) => { await request('PUT', '/dns', DNS_SETTINGS_SHAPE, { ...values, public_address: publicAddress }) setSettings(false) await refresh() }} /> )} {editing && ( setEditing(null)} onSave={async (values) => { if (editing === 'new') { await request('POST', '/dns/domains', DNS_DOMAIN_SHAPE, values) } else { await request('PUT', `/dns/domains/${encodeURIComponent(editing.name)}`, DNS_DOMAIN_SHAPE, { ttl: values.ttl }) } setEditing(null) setSelected(values.name) await refresh() }} /> )} {addingRecord && domain && ( setAddingRecord(false)} onSave={async (record) => { await request('POST', `${path}/records`, DNS_RECORD_SHAPE, record) setAddingRecord(false) await refresh() }} /> )} {confirm && }
) }