File
Blob: ui/operator/DNSConfiguration.svelte
| 1 | <script lang="ts"> |
| 2 | import { tick } from 'svelte'; |
| 3 | import Button from '../shared/Button.svelte'; |
| 4 | import SaveNotice from '../shared/SaveNotice.svelte'; |
| 5 | import { createEditor } from '../shared/editor.svelte'; |
| 6 | import { INPUT_CLASSES } from '../shared/styles'; |
| 7 | import SnapshotTime from './SnapshotTime.svelte'; |
| 8 | import { requestDNSConfig } from './dns-api'; |
| 9 | |
| 10 | type Draft = { zone: string; nameservers: { host: string; addresses: string }[] }; |
| 11 | let form = $state<HTMLFormElement>(); |
| 12 | const editor = createEditor({ |
| 13 | key: () => '/_internal/api/config/dns', |
| 14 | defaults: { zone: '', nameservers: [{ host: '', addresses: '' }] } as Draft, |
| 15 | load: (signal, after) => requestDNSConfig(signal, undefined, after), |
| 16 | save: (value, version, signal) => requestDNSConfig(signal, JSON.stringify({ |
| 17 | expected_version: version, |
| 18 | config: { zone: value.zone.trim(), nameservers: value.nameservers.map(ns => ({ host: ns.host.trim(), addresses: ns.addresses.split(/[\s,]+/).filter(Boolean) })) }, |
| 19 | })), |
| 20 | draft: snapshot => ({ zone: snapshot.config?.zone ?? '', nameservers: (snapshot.config?.nameservers ?? [{ host: '', addresses: [] }]).map(ns => ({ host: ns.host, addresses: ns.addresses.join('\n') })) }), |
| 21 | editable: snapshot => !snapshot.error && !(snapshot.version && !snapshot.config), |
| 22 | }); |
| 23 | const snapshot = $derived(editor.query.data); |
| 24 | const draft = $derived(editor.values); |
| 25 | const fields = $derived(editor.fields); |
| 26 | const readError = $derived(editor.query.error?.message ?? ''); |
| 27 | |
| 28 | function fieldError(path: string): string { |
| 29 | return Object.entries(fields).filter(([key]) => key === path || key.startsWith(`${path}.`)).map(([, message]) => message).join(' '); |
| 30 | } |
| 31 | |
| 32 | async function save(event: SubmitEvent) { |
| 33 | await editor.submit(event); |
| 34 | if (Object.keys(fields).length) { |
| 35 | await tick(); |
| 36 | form?.querySelector<HTMLElement>('[aria-invalid="true"]')?.focus(); |
| 37 | } |
| 38 | } |
| 39 | </script> |
| 40 | |
| 41 | <div class="mb-2 flex flex-wrap items-center justify-between gap-3"> |
| 42 | <h1 class="text-[22px] leading-tight font-semibold tracking-tight sm:text-2xl">ACME DNS</h1> |
| 43 | <Button variant="secondary" onclick={() => editor.refresh()} disabled={editor.query.isFetching || editor.saving}>{editor.query.isFetching ? 'Refreshing…' : 'Refresh'}</Button> |
| 44 | </div> |
| 45 | |
| 46 | {#if snapshot} |
| 47 | <p class="text-sm text-muted wrap-anywhere"> |
| 48 | {snapshot.version ? 'Saved on' : 'Server'} <code class="text-foreground">{snapshot.server.address}</code> |
| 49 | · <SnapshotTime value={snapshot.observed_at} /> |
| 50 | </p> |
| 51 | {/if} |
| 52 | |
| 53 | {#if readError} |
| 54 | <div class="my-4 rounded-md border border-danger/40 bg-surface px-4 py-3" role="alert"> |
| 55 | <p class="font-medium text-danger">{snapshot ? 'Refresh failed. Showing previous saved state.' : 'Could not load DNS configuration.'}</p> |
| 56 | <details class="mt-1"> |
| 57 | <summary class="min-h-8 cursor-pointer py-1 text-muted">Request error</summary> |
| 58 | <p class="mt-1 max-w-[72ch] font-mono text-xs wrap-anywhere">{readError}</p> |
| 59 | </details> |
| 60 | </div> |
| 61 | {/if} |
| 62 | |
| 63 | {#if snapshot} |
| 64 | {#if editor.readOnly} |
| 65 | <div class="mt-4 rounded-md border border-warning/40 bg-surface px-4 py-3" role="alert"> |
| 66 | <p class="font-medium text-warning">Saved configuration cannot be edited by this server.</p> |
| 67 | <p class="mt-1 max-w-[72ch] text-sm wrap-anywhere">{snapshot.error || 'The saved DNS document could not be read.'}</p> |
| 68 | </div> |
| 69 | {:else if editor.initialized} |
| 70 | {#if !snapshot.version} |
| 71 | <p class="mt-4 text-sm text-muted">No DNS configuration saved. Add the zone and nameservers to create it.</p> |
| 72 | {/if} |
| 73 | |
| 74 | {#if editor.stale} |
| 75 | <div class="mt-4 rounded-md border border-warning/40 bg-surface px-4 py-3" role="alert"> |
| 76 | <p class="font-medium text-warning">Saved configuration changed. Your draft is unchanged.</p> |
| 77 | <p class="mt-1 text-sm">Load the latest saved values before editing again.</p> |
| 78 | </div> |
| 79 | {/if} |
| 80 | |
| 81 | <SaveNotice outcome={editor.outcome} saved={editor.outcome?.kind === 'saved' ? `Saved on ${editor.outcome.snapshot.server.address}.` : ''} failed={Object.keys(fields).length ? 'Check the highlighted fields.' : 'Could not save DNS configuration.'} /> |
| 82 | |
| 83 | {#if editor.stale} |
| 84 | <details class="mt-3 rounded-md border border-line px-4 py-2" open> |
| 85 | <summary class="min-h-8 cursor-pointer py-1 font-medium">Latest saved values</summary> |
| 86 | {#if snapshot.config} |
| 87 | <p class="mt-2 text-sm text-muted">Zone <code class="text-foreground">{snapshot.config.zone}</code></p> |
| 88 | <dl class="my-3 grid gap-x-4 gap-y-2 text-sm sm:grid-cols-[minmax(0,1fr)_minmax(0,2fr)]"> |
| 89 | {#each snapshot.config.nameservers as ns} |
| 90 | <dt class="font-mono wrap-anywhere">{ns.host}</dt> |
| 91 | <dd class="font-mono text-muted wrap-anywhere">{ns.addresses.join(', ')}</dd> |
| 92 | {/each} |
| 93 | </dl> |
| 94 | {:else} |
| 95 | <p class="my-2 text-sm text-muted">No DNS configuration saved.</p> |
| 96 | {/if} |
| 97 | <Button size="sm" onclick={() => editor.accept()} disabled={editor.query.isFetching || editor.saving || Boolean(readError)}>Load latest and discard draft</Button> |
| 98 | </details> |
| 99 | {/if} |
| 100 | |
| 101 | <form bind:this={form} onsubmit={save} novalidate class="mt-5" aria-label="DNS configuration"> |
| 102 | <fieldset disabled={editor.saving}> |
| 103 | <legend class="sr-only">DNS records</legend> |
| 104 | <div class="max-w-lg"> |
| 105 | <label for="dns-zone" class="mb-1 block text-sm font-medium">Zone</label> |
| 106 | <input id="dns-zone" class={INPUT_CLASSES + ' font-mono'} value={draft.zone} oninput={(event) => { editor.form.setFieldValue('zone', event.currentTarget.value); editor.edited(); }} autocomplete="off" autocapitalize="none" spellcheck={false} required readonly={Boolean(snapshot.config)} aria-invalid={Boolean(fields.zone)} aria-describedby={fields.zone ? 'dns-zone-error' : undefined} /> |
| 107 | {#if fields.zone}<p id="dns-zone-error" class="mt-1 text-sm text-danger">{fields.zone}</p>{/if} |
| 108 | </div> |
| 109 | |
| 110 | <div class="mt-5 mb-2 flex flex-wrap items-center justify-between gap-2"> |
| 111 | <h2 class="text-lg font-semibold">Nameservers</h2> |
| 112 | <Button size="sm" onclick={() => { editor.form.pushFieldValue('nameservers', { host: '', addresses: '' }); editor.edited(); }}>Add nameserver</Button> |
| 113 | </div> |
| 114 | {#if fields.nameservers}<p class="mb-2 text-sm text-danger">{fields.nameservers}</p>{/if} |
| 115 | <div class="border-t border-line"> |
| 116 | {#each draft.nameservers as ns, index} |
| 117 | {@const hostError = fieldError(`nameservers.${index}.host`)} |
| 118 | {@const addressError = fieldError(`nameservers.${index}.addresses`)} |
| 119 | <div class="grid items-start gap-3 border-b border-line py-3 sm:grid-cols-[minmax(0,1fr)_minmax(0,1.4fr)_auto]"> |
| 120 | <div class="min-w-0"> |
| 121 | <label for={`dns-host-${index}`} class="mb-1 block text-sm font-medium">Nameserver {index + 1} (NS)</label> |
| 122 | <input id={`dns-host-${index}`} class={INPUT_CLASSES + ' font-mono'} value={ns.host} oninput={(event) => { editor.form.setFieldValue(`nameservers[${index}].host`, event.currentTarget.value); editor.edited(); }} autocomplete="off" autocapitalize="none" spellcheck={false} required aria-invalid={Boolean(hostError)} aria-describedby={hostError ? `dns-host-error-${index}` : undefined} /> |
| 123 | {#if hostError}<p id={`dns-host-error-${index}`} class="mt-1 text-sm text-danger">{hostError}</p>{/if} |
| 124 | </div> |
| 125 | <div class="min-w-0"> |
| 126 | <label for={`dns-address-${index}`} class="mb-1 block text-sm font-medium">IP addresses (A / AAAA)</label> |
| 127 | <textarea id={`dns-address-${index}`} class={INPUT_CLASSES + ' block resize-y font-mono'} rows="2" value={ns.addresses} oninput={(event) => { editor.form.setFieldValue(`nameservers[${index}].addresses`, event.currentTarget.value); editor.edited(); }} autocomplete="off" autocapitalize="none" spellcheck={false} required aria-invalid={Boolean(addressError)} aria-describedby={`dns-address-help-${index}${addressError ? ` dns-address-error-${index}` : ''}`}></textarea> |
| 128 | <p id={`dns-address-help-${index}`} class="mt-1 text-xs text-muted">One address per line.</p> |
| 129 | {#if addressError}<p id={`dns-address-error-${index}`} class="mt-1 text-sm text-danger">{addressError}</p>{/if} |
| 130 | </div> |
| 131 | <Button size="sm" variant="ghost" class="justify-self-start sm:mt-6" disabled={draft.nameservers.length === 1} aria-label={`Remove nameserver ${index + 1}`} onclick={() => { editor.form.removeFieldValue('nameservers', index); editor.edited(); }}>Remove</Button> |
| 132 | </div> |
| 133 | {/each} |
| 134 | </div> |
| 135 | </fieldset> |
| 136 | <div class="mt-4 flex flex-wrap items-center gap-2"> |
| 137 | <Button variant="primary" type="submit" disabled={!editor.canSave}>{editor.saving ? 'Saving…' : 'Save DNS configuration'}</Button> |
| 138 | {#if editor.dirty && !editor.stale}<Button variant="ghost" onclick={() => editor.accept()} disabled={editor.saving}>Discard changes</Button>{/if} |
| 139 | {#if editor.dirty}<span class="text-sm text-muted">Unsaved changes</span>{/if} |
| 140 | </div> |
| 141 | </form> |
| 142 | {/if} |
| 143 | {:else if editor.query.isFetching} |
| 144 | <div class="mt-5 space-y-3" role="status"> |
| 145 | <span class="sr-only">Loading DNS configuration…</span> |
| 146 | <div class="h-4 w-48 max-w-full rounded bg-surface" aria-hidden="true"></div> |
| 147 | <div class="h-36 rounded-md border border-line bg-chrome" aria-hidden="true"></div> |
| 148 | </div> |
| 149 | {/if} |