133 lines
5.4 KiB
Svelte
133 lines
5.4 KiB
Svelte
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
|
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
- file, You can obtain one at https://mozilla.org/MPL/2.0/. -->
|
|
<script lang="ts">
|
|
// Handoff §5: the main screen.
|
|
import { tick } from "svelte";
|
|
import { Banner, Button } from "@ianitor/ui";
|
|
import ChangeBar from "../components/ChangeBar.svelte";
|
|
import EngineChip from "../components/EngineChip.svelte";
|
|
import HostList from "../components/HostList.svelte";
|
|
import HostPanel from "../components/HostPanel.svelte";
|
|
import IntermediateBanner from "../components/IntermediateBanner.svelte";
|
|
import ShareDialog from "../components/ShareDialog.svelte";
|
|
import { call, newKey, post, sentence } from "../lib/api";
|
|
import { app, changesBlocked, navigate, refresh, toast } from "../lib/app.svelte";
|
|
import { atTime, countsSentence } from "../lib/format";
|
|
import type { ApplyView, HostSpec } from "../lib/types";
|
|
|
|
let { route }: { route: { newHost: boolean; edit?: string } } = $props();
|
|
|
|
const o = $derived(app.overview);
|
|
let prefill = $state<HostSpec | null>(null);
|
|
let focusField = $state<string | undefined>(undefined);
|
|
let problem = $state<string | null>(null);
|
|
|
|
const panelOpen = $derived(route.newHost || !!route.edit || app.panel?.kind === "host");
|
|
const blocked = $derived(changesBlocked());
|
|
|
|
async function closePanel() {
|
|
prefill = null;
|
|
focusField = undefined;
|
|
if (app.panel?.kind === "host") app.panel = null;
|
|
if (route.newHost || route.edit) navigate("/");
|
|
await refresh();
|
|
const h = app.focusHost;
|
|
app.focusHost = null;
|
|
if (h) {
|
|
await tick();
|
|
document.querySelector<HTMLElement>(`[aria-label="More for ${h.replace(/["\\]/g, "")}"]`)?.focus();
|
|
}
|
|
}
|
|
|
|
async function remove(h: string) {
|
|
problem = null;
|
|
try {
|
|
await call("DELETE", `/hosts/${encodeURIComponent(h)}?base_rev=${encodeURIComponent(o?.rev ?? "")}`, undefined, { idem: newKey() });
|
|
await refresh();
|
|
} catch (e) {
|
|
problem = sentence(e);
|
|
}
|
|
}
|
|
|
|
async function retry(h: string) {
|
|
try {
|
|
await post(`/hosts/${encodeURIComponent(h)}/certificate/retry`);
|
|
toast("Trying Let's Encrypt again.");
|
|
} catch (e) {
|
|
problem = sentence(e);
|
|
}
|
|
}
|
|
|
|
function editAgain(a: ApplyView, field?: string) {
|
|
prefill = a.attempted_host ?? null;
|
|
focusField = field;
|
|
if (a.kind === "add" || !a.hosts[0]) {
|
|
navigate("/hosts/new");
|
|
} else {
|
|
navigate(`/hosts/${encodeURIComponent(a.hosts[0])}/edit`);
|
|
}
|
|
}
|
|
|
|
</script>
|
|
|
|
<div class="head">
|
|
<div>
|
|
<h1 class="ia-display">Hosts</h1>
|
|
{#if o}<p class={app.phone ? "ia-sub" : "ia-text2"}>{countsSentence(o.counts, app.phone)}</p>{/if}
|
|
</div>
|
|
<div class="actions">
|
|
{#if !app.phone}<EngineChip />{/if}
|
|
<Button variant="primary" icon="plus" label="Add a host" disabled={!!blocked} describedby={blocked ? "add-blocked" : undefined} onclick={() => navigate("/hosts/new")}>Host</Button>
|
|
{#if blocked}<span class="sr-only" id="add-blocked">{blocked}</span>{/if}
|
|
</div>
|
|
</div>
|
|
|
|
<IntermediateBanner />
|
|
{#if problem}<Banner tone="notice" live="status" ondismiss={() => (problem = null)}>{#snippet title()}{problem}{/snippet}</Banner>{/if}
|
|
|
|
<ChangeBar onshowsteps={(id) => (app.panel = { kind: "host", resume: id })} onedit={editAgain} />
|
|
|
|
{#if !o && !app.loadError}
|
|
<div class="ia-list" aria-busy="true"><span class="sr-only">Loading hosts…</span>
|
|
{#each [0, 1, 2] as i (i)}<div class="ia-row"><span class="ia-placeholder"></span><span class="ia-placeholder"></span><span class="ia-placeholder"></span><span></span></div>{/each}
|
|
</div>
|
|
{:else if app.loadError && !o}
|
|
<Banner tone="down" live="alert">{#snippet title()}This page could not load. Ianitor's control service may be restarting.{/snippet}
|
|
{#snippet actions()}<Button onclick={() => refresh()}>Try again now</Button>{/snippet}</Banner>
|
|
{:else if o && o.hosts.length === 0}
|
|
<div class="ia-frame"><h2 class="ia-h3">No hosts yet</h2><p class="ia-text2">Add the first one: the name people type, and where Ianitor should send them.</p></div>
|
|
{:else if o}
|
|
<HostList hosts={o.hosts}
|
|
onedit={(h) => navigate(`/hosts/${encodeURIComponent(h)}/edit`)}
|
|
onremove={remove}
|
|
onshare={(h) => (app.panel = { kind: "share", hostname: h })}
|
|
onhistory={(h) => navigate(`/history?host=${encodeURIComponent(h)}`)}
|
|
onretry={retry} />
|
|
{/if}
|
|
|
|
{#if o?.repo}
|
|
<hr class="ia-ornament" />
|
|
<p class="ia-muted footer">
|
|
Routes live in the {o.repo.name} repository, one file per host.
|
|
{#if o.repo.last_push_at}Last pushed to Gitea {atTime(o.repo.last_push_at, app.now)}.{/if}
|
|
{#if o.repo.unpushed}{o.repo.unpushed} changes not yet pushed.{/if}
|
|
<button type="button" class="domi-btn link" onclick={() => navigate("/history")}>See history</button>
|
|
</p>
|
|
{/if}
|
|
|
|
{#if panelOpen}
|
|
{#key route.edit ?? (app.panel?.kind === "host" ? app.panel.resume : "new")}
|
|
<HostPanel hostname={route.edit} resume={app.panel?.kind === "host" ? app.panel.resume : undefined} {prefill} {focusField} onclose={closePanel} />
|
|
{/key}
|
|
{/if}
|
|
{#if app.panel?.kind === "share"}
|
|
<ShareDialog hostname={app.panel.hostname} onclose={() => { app.panel = null; void refresh(); }} />
|
|
{/if}
|
|
|
|
<style>
|
|
.head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-end; gap: 16px; }
|
|
.actions { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
|
|
.footer { text-align: center; }
|
|
</style>
|