Files
ianitor/apps/web/src/components/HostList.svelte
T

143 lines
7.1 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 §2.5 and §5.1: the host list, sorted down, degraded, pending, unknown, healthy.
import { Status } from "@ianitor/ui";
import type { StatusKind } from "@ianitor/ui";
import HostMenu from "./HostMenu.svelte";
import { app, changesBlocked } from "../lib/app.svelte";
import { ago, duration } from "../lib/format";
import type { HostRow } from "../lib/types";
let {
hosts,
onedit,
onremove,
onshare,
onhistory,
onretry,
}: {
hosts: HostRow[];
onedit: (h: string) => void;
onremove: (h: string) => void;
onshare: (h: string) => void;
onhistory: (h: string) => void;
onretry: (h: string) => void;
} = $props();
const ORDER: Record<string, number> = { down: 0, degraded: 1, pending: 2, unknown: 3, healthy: 4 };
const sorted = $derived([...hosts].sort((a, b) => (ORDER[a.health.state] ?? 5) - (ORDER[b.health.state] ?? 5) || a.hostname.localeCompare(b.hostname)));
const running = $derived(app.overview?.active_apply ? (app.overview.last_apply?.hosts ?? []) : []);
const underCaddy = $derived(app.overview?.engine.running === "caddy");
function kind(h: HostRow): [StatusKind, string] {
switch (h.health.state) {
case "healthy": return ["healthy", "healthy"];
case "degraded": return ["degraded", "degraded"];
case "down": return ["down", "down"];
case "pending": return ["unknown", "waiting"];
default: return ["unknown", "not checked"];
}
}
function since(h: HostRow): string {
return h.health.since ? duration((app.now - Date.parse(h.health.since)) / 1000) : "a while";
}
function sub(h: HostRow): { text: string; problem: boolean } {
const f = h.health.failure;
const visitors = 'Visitors get an error page.';
switch (h.health.state) {
case "healthy": {
const added = h.added_at && app.now - Date.parse(h.added_at) < 86_400_000 ? "Added today · c" : "C";
const lat = h.health.latency_ms != null && !underCaddy ? ` · ${h.health.latency_ms} ms` : "";
return { text: `${added}hecked ${ago(h.health.last_check_at, app.now)}${lat}`, problem: false };
}
case "degraded": {
const ok = h.health.upstreams.filter((u) => u.state === "healthy").length;
const m = h.health.upstreams.length;
if (m > 1) return { text: `${ok} of ${m} addresses answer. Visitors are sent to the ones that do.`, problem: true };
return { text: `Answering only some checks.`, problem: true };
}
case "down":
if (f?.kind === "refused") return { text: `Connection refused for ${since(h)}. ${visitors}`, problem: true };
if (f?.kind === "timeout") return { text: `Not answering for ${since(h)}. ${visitors}`, problem: true };
if (f?.kind === "name") return { text: `The name ${f.upstream ?? ""} cannot be found. ${visitors}`, problem: true };
if (f?.kind === "tls") return { text: `The certificate of ${f.upstream ?? ""} cannot be checked.`, problem: true };
return { text: `Answering with an error page for ${since(h)}.`, problem: true };
case "pending": return { text: "Waiting for the first check.", problem: false };
default: return { text: "No health check set for this host.", problem: false };
}
}
function phoneDetail(h: HostRow): string {
if (h.health.state === "healthy" && h.health.latency_ms != null) return `${h.health.latency_ms} ms`;
if (h.health.state === "down" && h.health.failure?.kind === "refused") return `refused for ${since(h)}`;
if (h.health.state === "unknown") return "no health check";
if (h.added_at && app.now - Date.parse(h.added_at) < 86_400_000) return "added today";
return h.share_links ? "share links on" : "";
}
function chips(h: HostRow): string[] {
const c = [h.tls === "domi" ? "Domi certificate" : "Let's Encrypt"];
c.push(h.auth === "forward-auth" ? "Sign-in first" : "Open to anyone");
if (h.share_links) c.push("Share links on");
if (h.device_certs) c.push("Device certificate needed");
if (!h.websockets) c.push("No websockets");
return c;
}
const blocked = $derived(changesBlocked());
const shareReason = $derived(!app.overview?.site?.share_links_available ? (underCaddy ? "Share links are off while Caddy serves." : "Share links are not set up on this server.") : null);
</script>
<div class="ia-list" role="table" aria-label="Hosts">
<div class="ia-row head" role="row">
<span class="ia-label col-status" role="columnheader">Status</span>
<span class="ia-label" role="columnheader">Host</span>
<span class="ia-label desktop" role="columnheader">Sends traffic to</span>
<span role="columnheader"><span class="sr-only">More</span></span>
</div>
{#each sorted as h (h.hostname)}
{@const [k, w] = kind(h)}
{@const s = sub(h)}
<div class="ia-row" role="row">
<span class="col-status" role="cell"><Status kind={k} word={w} /></span>
<span role="cell">
<span class="phone-dot"><Status kind={k} word="" /></span>
<span class="ia-host">{h.hostname}</span>
<span class="ia-chips desktop">{#each chips(h) as c (c)}<span class="ia-chip">{c}</span>{/each}</span>
<span class="ia-sub phone">{w}{phoneDetail(h) ? ` · ${phoneDetail(h)}` : ""}</span>
</span>
<span role="cell" class="desktop">
<span>{h.upstreams[0] ?? ""}</span>{#if h.upstreams.length > 1}{" "}<span class="ia-muted">+{h.upstreams.length - 1} more</span>{/if}
<span class="ia-sub" class:problem={s.problem} style="display:block">{running.includes(h.hostname) ? "Changing now · " : ""}{s.text}</span>
{#if h.certificate.state === "pending"}<span class="ia-sub" style="display:block">Waiting for Let's Encrypt to issue the certificate.</span>{/if}
{#if h.certificate.state === "failed"}
<span class="ia-sub" style="display:block;color:var(--down)">Let's Encrypt could not confirm <strong>{h.hostname}</strong>. The name must point to this server, and port 80 must be open from the internet.
<button type="button" class="domi-btn link" onclick={() => onretry(h.hostname)}>Try again</button></span>
{/if}
</span>
<span role="cell">
<HostMenu hostname={h.hostname} items={[
{ label: "Share link", icon: "link", reason: shareReason, run: () => onshare(h.hostname) },
{ label: "Edit host", icon: "pencil", reason: blocked, run: () => onedit(h.hostname) },
{ label: "Show in history", icon: "history", run: () => onhistory(h.hostname) },
{ label: "Remove host", icon: "trash", danger: true, divider: true, reason: blocked, run: () => onremove(h.hostname) },
]} />
</span>
</div>
{/each}
</div>
<style>
.phone, .phone-dot { display: none; }
.problem { color: var(--text-2) !important; }
@media (max-width: 719.98px) {
.desktop { display: none !important; }
.phone { display: block; padding-left: 22px; }
.phone-dot { display: inline; }
}
</style>