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';
|
2024-07-07 14:52:47 -04:00
|
|
|
|
|
|
|
|
interface Props {
|
2025-02-13 12:29:16 -05:00
|
|
|
records: { name: string; type: 'A' | string; value: string }[];
|
2024-07-07 14:52:47 -04:00
|
|
|
}
|
|
|
|
|
|
2025-02-13 12:29:16 -05:00
|
|
|
export default function AddRecord({ records }: Props) {
|
2024-12-31 10:30:14 +05:30
|
|
|
const [name, setName] = useState('');
|
|
|
|
|
const [ip, setIp] = useState('');
|
2024-07-07 14:52:47 -04:00
|
|
|
|
|
|
|
|
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-07-07 14:52:47 -04:00
|
|
|
|
2024-12-31 10:30:14 +05:30
|
|
|
return lookup.value === ip;
|
|
|
|
|
}, [records, name, ip]);
|
2024-07-07 14:52:47 -04:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog>
|
2024-12-31 10:30:14 +05:30
|
|
|
<Dialog.Button>Add DNS record</Dialog.Button>
|
2025-02-13 12:29:16 -05:00
|
|
|
<Dialog.Panel>
|
2025-01-26 15:04:13 -05:00
|
|
|
<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">
|
2025-02-13 12:29:16 -05:00
|
|
|
<input type="hidden" name="action_id" value="add_record" />
|
|
|
|
|
<input type="hidden" name="record_type" value="A" />
|
2025-01-28 16:02:47 -05:00
|
|
|
<Input
|
|
|
|
|
isRequired
|
|
|
|
|
label="Domain"
|
|
|
|
|
placeholder="test.example.com"
|
2025-02-13 12:29:16 -05:00
|
|
|
name="record_name"
|
2025-01-28 16:02:47 -05:00
|
|
|
onChange={setName}
|
|
|
|
|
isInvalid={isDuplicate}
|
|
|
|
|
/>
|
|
|
|
|
<Input
|
|
|
|
|
isRequired
|
|
|
|
|
label="IP Address"
|
|
|
|
|
placeholder="101.101.101.101"
|
2025-02-13 12:29:16 -05:00
|
|
|
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>
|
2024-07-07 14:52:47 -04:00
|
|
|
</Dialog.Panel>
|
|
|
|
|
</Dialog>
|
2024-12-31 10:30:14 +05:30
|
|
|
);
|
2024-07-07 14:52:47 -04:00
|
|
|
}
|