87 lines
3.4 KiB
TypeScript
87 lines
3.4 KiB
TypeScript
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<string, string> = {
|
|
A: 'An IPv4 address.',
|
|
AAAA: 'An IPv6 address.',
|
|
CNAME: 'The host name the name points to.',
|
|
TXT: 'The text.',
|
|
MX: '<preference> <host>',
|
|
SRV: '<priority> <weight> <port> <host>',
|
|
CAA: '<flags> <tag> "<value>"',
|
|
}
|
|
|
|
/**
|
|
* 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<void>
|
|
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<string | null>(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 (
|
|
<Dialog open onClose={(_event, reason) => reason !== 'backdropClick' && props.onCancel()} maxWidth="sm" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}>
|
|
<DialogTitle>Add record</DialogTitle>
|
|
<DialogContent>
|
|
<Stack sx={{ gap: 2, pt: 1 }}>
|
|
{error && <Alert severity="error" sx={{ whiteSpace: 'pre-line' }}>{error}</Alert>}
|
|
<TextField
|
|
label="Name"
|
|
value={name}
|
|
autoFocus
|
|
autoComplete="off"
|
|
helperText={`The name in ${props.domain}; @ or empty for ${props.domain} itself.`}
|
|
onChange={(event) => setName(event.target.value)}
|
|
/>
|
|
<TextField select label="Type" value={type} onChange={(event) => setType(event.target.value)}>
|
|
{DNS_TYPES.map((item) => <MenuItem key={item} value={item}>{item}</MenuItem>)}
|
|
</TextField>
|
|
<TextField label="Content" value={content} autoComplete="off" helperText={CONTENT_HELP[type]} onChange={(event) => setContent(event.target.value)} />
|
|
<TextField
|
|
label="TTL (seconds)"
|
|
value={ttl}
|
|
autoComplete="off"
|
|
error={ttlProblem !== ''}
|
|
helperText={ttlProblem || 'Empty for the domain\'s TTL.'}
|
|
slotProps={{ htmlInput: { inputMode: 'numeric' } }}
|
|
onChange={(event) => setTtl(event.target.value.trim())}
|
|
/>
|
|
</Stack>
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<Button onClick={props.onCancel} disabled={saving}>Cancel</Button>
|
|
<Button type="submit" variant="contained" disabled={saving || blocked}>Add</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
)
|
|
}
|