import { useId, useState, type FormEvent } from "react"; import { useSuspenseQuery } from "@tanstack/react-query"; import { localRecordCreateMutation, localRecordDeleteMutation, localRecordUpdateMutation, localRecordsQuery, } from "@/lib/queries"; import type { LocalRecord, LocalRecordInput, LocalRecordType } from "@/lib/types"; import InlineError from "@/lib/InlineError"; import { useCrudForm } from "@/ui/useCrudForm"; import { formCardClass, inputClass, largeButtonClass, largePrimaryButtonClass, rowButtonClass, tableWrapClass, } from "@/ui/classes"; import { READ_ONLY_HINT, useReadOnlyConfig } from "@/features/settings/authority"; const RTYPES: readonly LocalRecordType[] = ["A", "AAAA", "CNAME"]; function RecordForm({ initial, busy, readOnly, error, onSubmit, onCancel, }: { initial?: LocalRecord; busy: boolean; readOnly: boolean; error: unknown; onSubmit: (input: LocalRecordInput) => void; onCancel: () => void; }) { const id = useId(); const [name, setName] = useState(initial?.name ?? ""); const [rtype, setRtype] = useState(initial?.rtype ?? "A"); const [value, setValue] = useState(initial?.value ?? ""); const [ttl, setTtl] = useState(initial === undefined ? "" : String(initial.ttl)); function submit(event: FormEvent) { event.preventDefault(); const input: LocalRecordInput = { name: name.trim(), rtype, value: value.trim() }; if (ttl.trim() !== "") input.ttl = Number(ttl); onSubmit(input); } return (

{initial === undefined ? "New record" : `Edit ${initial.name}`}

setName(event.target.value)} placeholder="nas.lan.home" className={inputClass} />
setValue(event.target.value)} placeholder={ rtype === "CNAME" ? "target.example.com" : rtype === "AAAA" ? "fd00::10" : "192.168.1.10" } className={inputClass} />
setTtl(event.target.value)} placeholder="300" className={inputClass} />
); } export default function RecordsTab() { const records = useSuspenseQuery(localRecordsQuery()).data; const { create, update, remove, form, openForm, closeForm, onSubmit, onDelete } = useCrudForm< LocalRecord, LocalRecordInput >({ create: localRecordCreateMutation, update: localRecordUpdateMutation, remove: localRecordDeleteMutation, confirmDelete: (record) => `Delete record "${record.name}"?`, }); const readOnly = useReadOnlyConfig(); return (

Answers served directly for LAN names. Changes apply live.

{form?.mode === "create" && ( )}
{records.length === 0 && ( )} {records.map((record) => ( {form?.mode === "edit" && form.entity.id === record.id ? ( ) : ( <> )} ))}
Name Type Value TTL Actions
No local records yet.
{record.name} {record.rtype} {record.value} {record.ttl}
); }