File
Blob: ui/client/CustomDomains.svelte
| 1 | <script lang="ts"> |
| 2 | import { onDestroy } from "svelte"; |
| 3 | import Button from "../shared/Button.svelte"; |
| 4 | import { INPUT_CLASSES } from "../shared/styles"; |
| 5 | import { errorMessage, requestAPI, type DomainRecord } from "./api"; |
| 6 | |
| 7 | type DomainAction = "acme" | "validate"; |
| 8 | type DomainResult = DomainRecord & { domain: string; verified: boolean }; |
| 9 | |
| 10 | let domain = $state(""); |
| 11 | let pending = $state<{ action: DomainAction; domain: string } | null>(null); |
| 12 | let output = $state<DomainResult | null>(null); |
| 13 | let error = $state(""); |
| 14 | let domainRevision = 0; |
| 15 | const requests = new AbortController(); |
| 16 | |
| 17 | function editDomain() { |
| 18 | domainRevision += 1; |
| 19 | output = null; |
| 20 | error = ""; |
| 21 | } |
| 22 | |
| 23 | async function domainAction(action: DomainAction) { |
| 24 | if (pending) return; |
| 25 | const submittedDomain = domain.trim(); |
| 26 | if (!submittedDomain || /[\s/:]/.test(submittedDomain)) { |
| 27 | error = "Enter a domain without a protocol or path."; |
| 28 | return; |
| 29 | } |
| 30 | const revision = domainRevision; |
| 31 | pending = { action, domain: submittedDomain }; |
| 32 | error = ""; |
| 33 | output = null; |
| 34 | try { |
| 35 | const record = await requestAPI<DomainRecord>(`/api/${action}/${encodeURIComponent(submittedDomain)}`, { signal: requests.signal }); |
| 36 | if (revision === domainRevision) |
| 37 | output = { ...record, domain: submittedDomain, verified: action === "validate" }; |
| 38 | } catch (cause) { |
| 39 | if (revision === domainRevision) error = `${submittedDomain}: ${errorMessage(cause)}`; |
| 40 | } finally { |
| 41 | pending = null; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | onDestroy(() => requests.abort()); |
| 46 | </script> |
| 47 | |
| 48 | <section id="domains" class="min-w-0 scroll-mt-6" aria-labelledby="domains-heading"> |
| 49 | <h2 id="domains-heading" class="mb-3.5 text-lg leading-snug font-semibold tracking-tight">Custom domains</h2> |
| 50 | <form onsubmit={(event) => { event.preventDefault(); void domainAction("acme"); }}> |
| 51 | <label for="domain" class="mb-1.5 block font-medium">Domain</label> |
| 52 | <input |
| 53 | id="domain" |
| 54 | name="domain" |
| 55 | type="text" |
| 56 | class={INPUT_CLASSES} |
| 57 | bind:value={domain} |
| 58 | oninput={editDomain} |
| 59 | placeholder="app.example.com" |
| 60 | autocomplete="off" |
| 61 | autocapitalize="none" |
| 62 | spellcheck="false" |
| 63 | maxlength="253" |
| 64 | required |
| 65 | aria-describedby="domain-hint domain-feedback" |
| 66 | aria-invalid={Boolean(error)} |
| 67 | /> |
| 68 | <p id="domain-hint" class="mt-1.5 mb-2.5 text-[0.8125rem] text-muted">Add the record in DNS, then verify.</p> |
| 69 | <div class="flex flex-wrap items-center gap-2"> |
| 70 | <Button type="submit" disabled={Boolean(pending) || !domain.trim()}>{pending?.action === "acme" ? "Getting record…" : "Get DNS record"}</Button> |
| 71 | <Button disabled={Boolean(pending) || !domain.trim()} onclick={() => domainAction("validate")}>{pending?.action === "validate" ? "Verifying…" : "Verify ownership"}</Button> |
| 72 | </div> |
| 73 | </form> |
| 74 | |
| 75 | <div id="domain-feedback" aria-live="polite" aria-busy={Boolean(pending)}> |
| 76 | {#if pending} |
| 77 | <p class="my-2.5 rounded-md border border-line bg-chrome px-3 py-2 wrap-anywhere"> |
| 78 | {pending.action === "validate" ? "Verifying" : "Fetching"} <span class="font-mono text-[0.8125rem]">{pending.domain}</span>… |
| 79 | </p> |
| 80 | {/if} |
| 81 | {#if error} |
| 82 | <div class="my-2.5 rounded-md border border-danger/40 bg-chrome px-3 py-2 text-danger wrap-anywhere"> |
| 83 | {#if error.length > 120} |
| 84 | <details><summary class="cursor-pointer py-1 font-medium text-zinc-200">Domain request failed</summary><p class="mt-2">{error}</p></details> |
| 85 | {:else} |
| 86 | {error} |
| 87 | {/if} |
| 88 | </div> |
| 89 | {/if} |
| 90 | {#if output} |
| 91 | <div class="mt-3.5 border-t border-line pt-3"> |
| 92 | <h3 class={['text-[0.9375rem] font-semibold wrap-anywhere', output.verified && 'text-success']}> |
| 93 | {output.verified ? "Verified" : "DNS record"} <span class="font-mono text-[0.8125rem]">{output.domain}</span> |
| 94 | </h3> |
| 95 | <dl class="mt-3 [&>div]:grid [&>div]:grid-cols-[4rem_minmax(0,1fr)] [&>div]:gap-4 [&>div]:py-1.5 [&_dd]:font-mono [&_dd]:text-[0.8125rem] [&_dd]:wrap-anywhere [&_dt]:text-muted"> |
| 96 | <div><dt>Name</dt><dd>{output.record}</dd></div> |
| 97 | <div><dt>Type</dt><dd>{output.type}</dd></div> |
| 98 | <div><dt>Value</dt><dd>{output.content}</dd></div> |
| 99 | </dl> |
| 100 | {#if output.verified}<a href="#configuration" class="text-accent-300 underline underline-offset-4 hover:text-accent-200">Add to configuration</a>{/if} |
| 101 | </div> |
| 102 | {/if} |
| 103 | </div> |
| 104 | </section> |