import { useMemo, useState } from 'react'; import { useSubmit } from 'react-router'; import Code from '~/components/Code'; import Dialog from '~/components/Dialog'; import Input from '~/components/Input'; interface Props { records: { name: string; type: 'A'; value: string }[]; } export default function AddDNS({ records }: Props) { const submit = useSubmit(); const [name, setName] = useState(''); const [ip, setIp] = useState(''); const isDuplicate = useMemo(() => { if (name.length === 0 || ip.length === 0) return false; const lookup = records.find((record) => record.name === name); if (!lookup) return false; return lookup.value === ip; }, [records, name, ip]); // TODO: Ditch useSubmit here (non JSON form) return ( Add DNS record { event.preventDefault(); if (!name || !ip) return; setName(''); setIp(''); submit( { 'dns.extra_records': [ ...records, { name, type: 'A', value: ip, }, ], }, { method: 'PATCH', encType: 'application/json', }, ); }} > Add DNS record Enter the domain and IP address for the new DNS record.
{isDuplicate ? (

A record with the domain name {name} and IP address{' '} {ip} already exists.

) : undefined}
); }