import { Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, MenuItem, Stack, TextField } from '@mui/material' import { useState, type FormEvent } from 'react' import { DNS_TYPES } from './api.ts' /** The content each type of record takes. */ const CONTENT_HELP: Record = { A: 'An IPv4 address.', AAAA: 'An IPv6 address.', CNAME: 'The host name the name points to.', TXT: 'The text.', MX: ' ', SRV: ' ', CAA: ' ""', } /** * A dialog that adds a record to a domain: its name, relative to the domain, @ for the domain itself, its type, its * content and an optional TTL. onSave sends it; the dialog closes when it succeeds and shows the server's message when * it fails. */ export default function DnsRecordDialog(props: { domain: string onSave: (record: { name: string; type: string; content: string; ttl?: number }) => Promise onCancel: () => void }) { const [name, setName] = useState('') const [type, setType] = useState('A') const [content, setContent] = useState('') const [ttl, setTtl] = useState('') const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const ttlProblem = ttl === '' || (/^\d{2,5}$/.test(ttl) && Number(ttl) >= 60 && Number(ttl) <= 86400) ? '' : '60 to 86400 seconds.' const blocked = content.trim() === '' || ttlProblem !== '' async function submit(event: FormEvent) { event.preventDefault() if (blocked) { return } setSaving(true) setError(null) try { await props.onSave({ name: name.trim() || '@', type, content: content.trim(), ...(ttl ? { ttl: Number(ttl) } : {}) }) } catch (caught) { setError((caught as Error).message) setSaving(false) } } return ( reason !== 'backdropClick' && props.onCancel()} maxWidth="sm" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}> Add record {error && {error}} setName(event.target.value)} /> setType(event.target.value)}> {DNS_TYPES.map((item) => {item})} setContent(event.target.value)} /> setTtl(event.target.value.trim())} /> ) }