headplane/app/routes/dns/dialogs/add-record.tsx

80 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-12-31 10:30:14 +05:30
import { useMemo, useState } from 'react';
import Code from '~/components/Code';
import Dialog from '~/components/Dialog';
2025-01-28 16:02:47 -05:00
import Input from '~/components/Input';
2025-04-26 12:04:00 -04:00
import Select from '~/components/Select';
interface Props {
2025-04-26 12:04:00 -04:00
records: { name: string; type: 'A' | 'AAAA' | string; value: string }[];
}
export default function AddRecord({ records }: Props) {
const [type, setType] = useState<'A' | 'AAAA' | string>('A');
2024-12-31 10:30:14 +05:30
const [name, setName] = useState('');
const [ip, setIp] = useState('');
const isDuplicate = useMemo(() => {
2024-12-31 10:30:14 +05:30
if (name.length === 0 || ip.length === 0) return false;
const lookup = records.find((record) => record.name === name);
if (!lookup) return false;
2024-12-31 10:30:14 +05:30
return lookup.value === ip;
}, [records, name, ip]);
return (
<Dialog>
2024-12-31 10:30:14 +05:30
<Dialog.Button>Add DNS record</Dialog.Button>
<Dialog.Panel
onSubmit={() => {
setName('');
setIp('');
}}
>
<Dialog.Title>Add DNS record</Dialog.Title>
<Dialog.Text>
Enter the domain and IP address for the new DNS record.
</Dialog.Text>
2025-01-28 16:02:47 -05:00
<div className="flex flex-col gap-2 mt-4">
<input type="hidden" name="action_id" value="add_record" />
2025-04-26 12:04:00 -04:00
<Select
isRequired
label="Record Type"
name="record_type"
defaultInputValue={type}
onSelectionChange={(v) => {
if (v) setType(v.toString() as 'A' | 'AAAA');
}}
2025-04-26 12:04:00 -04:00
>
<Select.Item key="A">A</Select.Item>
<Select.Item key="AAAA">AAAA</Select.Item>
</Select>
2025-01-28 16:02:47 -05:00
<Input
isRequired
label="Domain"
placeholder="test.example.com"
name="record_name"
2025-01-28 16:02:47 -05:00
onChange={setName}
isInvalid={isDuplicate}
/>
<Input
isRequired
label="IP Address"
placeholder={
type === 'AAAA' ? '2001:db8::ff00:42:8329' : '101.101.101.101'
}
name="record_value"
2025-01-28 16:02:47 -05:00
onChange={setIp}
isInvalid={isDuplicate}
/>
{isDuplicate ? (
<p className="text-sm opacity-50">
A record with the domain name <Code>{name}</Code> and IP address{' '}
<Code>{ip}</Code> already exists.
</p>
) : undefined}
</div>
</Dialog.Panel>
</Dialog>
2024-12-31 10:30:14 +05:30
);
}