Files
ianitor/apps/web/src/App.svelte
T

176 lines
8.8 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">
// The shell (handoff §2.16): sidebar on desktop, top and bottom bars on phone, the routes,
// the banners every screen starts with, toasts and the signed-out dialog (§10).
import { onMount } from "svelte";
import { Button, Dialog, Icon } from "@ianitor/ui";
import type { IconName } from "@ianitor/ui";
import EngineChip from "./components/EngineChip.svelte";
import EnginePopover from "./components/EnginePopover.svelte";
import TopBanners from "./components/TopBanners.svelte";
import { app, navigate, refresh, startPolling } from "./lib/app.svelte";
import { remembered } from "./lib/applies";
import Certificates from "./views/Certificates.svelte";
import History from "./views/History.svelte";
import Hosts from "./views/Hosts.svelte";
import RenewByHand from "./views/RenewByHand.svelte";
import Settings from "./views/Settings.svelte";
import SignedIn from "./views/SignedIn.svelte";
const route = $derived.by(() => {
const u = new URL(app.path, "http://x");
const p = u.pathname.replace(/\/+$/, "") || "/";
const seg = p.split("/").filter(Boolean).map(decodeURIComponent);
if (p === "/signed-in") return { view: "signed-in" as const };
if (seg[0] === "history") return { view: "history" as const, host: u.searchParams.get("host") ?? undefined, pick: seg[1] };
if (seg[0] === "certificates") return { view: seg[1] === "renew-by-hand" ? ("renew" as const) : ("certs" as const), newDevice: seg[1] === "new" };
if (seg[0] === "settings") return { view: "settings" as const, tab: seg[1] ?? "general" };
return { view: "hosts" as const, newHost: seg[0] === "hosts" && seg[1] === "new", edit: seg[0] === "hosts" && seg[2] === "edit" ? seg[1] : undefined };
});
const NAV: { href: string; label: string; icon: IconName; views: string[] }[] = [
{ href: "/", label: "Hosts", icon: "building", views: ["hosts"] },
{ href: "/history", label: "History", icon: "history", views: ["history"] },
{ href: "/certificates", label: "Certificates", icon: "shield", views: ["certs", "renew"] },
{ href: "/settings/general", label: "Settings", icon: "gear", views: ["settings"] },
];
const TITLES: Record<string, string> = { hosts: "Hosts", history: "History", certs: "Certificates", renew: "Certificates", settings: "Settings", "signed-in": "Signed in" };
$effect(() => {
document.title = `${TITLES[route.view] ?? "Hosts"} — Ianitor Domi`;
});
onMount(() => {
if (route.view === "signed-in") return;
const stop = startPolling();
// Resume an apply this tab was watching before a reload or a sign-in (§3).
const id = remembered();
if (id && route.view === "hosts") app.panel = { kind: "host", resume: id };
let ch: BroadcastChannel | undefined;
try {
ch = new BroadcastChannel("ianitor-auth");
ch.onmessage = (m) => {
if (m.data === "signed-in") {
app.signedOut = false;
void refresh();
}
};
} catch {
/* no BroadcastChannel: "I have signed in" still works */
}
return () => {
stop();
ch?.close();
};
});
function go(e: MouseEvent, href: string) {
if (e.metaKey || e.ctrlKey || e.shiftKey) return;
e.preventDefault();
navigate(href);
}
function signIn() {
const w = window.open("/signed-in", "ianitor-sign-in");
if (!w) {
// The new tab was blocked: this tab goes to the sign-in, so an open form is saved first
// (sessionStorage, this tab only) and comes back after it (§10).
window.dispatchEvent(new Event("ianitor:save-draft"));
location.href = `/signed-in?back=${encodeURIComponent(app.path)}`;
}
}
async function signedInAgain() {
app.signedOut = false;
await refresh();
}
const viewer = $derived(app.overview?.viewer ?? null);
</script>
{#if route.view === "signed-in"}
<SignedIn />
{:else}
<a class="skip domi-btn" href="#main">Skip to content</a>
<div class="shell">
<nav class="sidebar" aria-label="Main">
<div class="ia-wordmark brand">IANITOR DOMI</div>
{#each NAV as n (n.href)}
<a href={n.href} class="nav" aria-current={n.views.includes(route.view) ? "page" : undefined} onclick={(e) => go(e, n.href)}><Icon name={n.icon} />{n.label}</a>
{/each}
<div class="foot ia-muted">
<p>{viewer?.via === "authelia" ? `Signed in as ${viewer.name} through Authelia` : viewer ? `Using the API token "${viewer.name}"` : "Signed in through Authelia"}</p>
<p>Control on 127.0.0.1 only</p>
{#if app.overview?.site?.auth_logout_url}<a class="domi-btn link" href={app.overview.site.auth_logout_url}>Sign out</a>{/if}
</div>
</nav>
<header class="topbar"><span class="ia-wordmark">IANITOR DOMI</span>{#if app.phone}<EngineChip />{/if}</header>
<main id="main" tabindex="-1">
<TopBanners />
{#if route.view === "hosts"}
<Hosts route={{ newHost: route.newHost, edit: route.edit }} />
{:else if route.view === "history"}
{#key route.host}<History host={route.host} pick={route.pick} />{/key}
{:else if route.view === "certs"}
<Certificates newDevice={route.newDevice} />
{:else if route.view === "renew"}
<RenewByHand />
{:else if route.view === "settings"}
<Settings tab={route.tab} />
{/if}
</main>
<nav class="bottombar" aria-label="Main">
{#each NAV as n (n.href)}
<a href={n.href} aria-current={n.views.includes(route.view) ? "page" : undefined} onclick={(e) => go(e, n.href)}><Icon name={n.icon} /><span>{n.label}</span></a>
{/each}
</nav>
</div>
{#if app.panel?.kind === "engine"}
<EnginePopover onclose={() => {
app.panel = null;
document.querySelector<HTMLElement>("[data-engine-chip]")?.focus();
}} />
{/if}
<div class="ia-toasts" role="status" aria-live="polite">
{#each app.toasts as t (t.id)}<div class="ia-toast"><Icon name="check" />{t.text}</div>{/each}
</div>
{#if app.signedOut}
<Dialog title="Your sign-in has ended" role="alertdialog" escape={false} closable={false} onclose={() => {}}>
<p>Sign in again; this page keeps what you typed.</p>
{#snippet footer()}
<Button onclick={signedInAgain}>I have signed in</Button>
<Button variant="primary" onclick={signIn} data-autofocus>Sign in</Button>
{/snippet}
</Dialog>
{/if}
{/if}
<style>
.shell { display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); min-height: 100dvh; }
.sidebar { background: var(--colonnade); border-right: 1px solid var(--hairline); padding: 22px 14px; display: flex; flex-direction: column; gap: 4px; position: sticky; top: 0; height: 100dvh; }
.brand { padding: 4px 10px 18px; }
.nav { display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 0 12px; border-radius: var(--radius-sm); color: var(--text-2); font-weight: 600; font-size: var(--fs-nav); text-decoration: none; border: 1px solid transparent; }
.nav:hover { background: var(--surface); color: var(--text); }
.nav[aria-current="page"] { background: var(--surface-2); color: var(--text); box-shadow: var(--inset-lit); border-color: color-mix(in srgb, var(--accent) 30%, transparent); }
.nav[aria-current="page"] :global(svg) { color: var(--accent); }
.foot { margin-top: auto; font-size: 0.82rem; display: flex; flex-direction: column; gap: 4px; padding: 0 10px; }
main { padding: var(--main-pad); display: flex; flex-direction: column; gap: 18px; min-width: 0; outline: none; }
.topbar, .bottombar { display: none; }
@media (max-width: 719.98px) {
.shell { grid-template-columns: minmax(0, 1fr); }
.sidebar { display: none; }
.topbar { display: flex; align-items: center; justify-content: space-between; background: var(--colonnade); border-bottom: 1px solid var(--hairline); padding: 10px 16px; }
.topbar .ia-wordmark { font-size: 12px; letter-spacing: 0.26em; }
main { padding: 18px 16px calc(90px + env(safe-area-inset-bottom)); gap: 14px; }
.bottombar { display: grid; grid-template-columns: repeat(4, 1fr); position: fixed; left: 0; right: 0; bottom: 0; background: var(--colonnade); border-top: 1px solid var(--hairline); padding: 6px 8px calc(10px + env(safe-area-inset-bottom)); z-index: 40; }
.bottombar a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-height: var(--phone-bar-h); border-radius: var(--radius-sm); color: var(--text-2); font-weight: 600; font-size: var(--fs-tab-phone); text-decoration: none; }
.bottombar a[aria-current="page"] { background: var(--surface-2); color: var(--text); }
.bottombar a[aria-current="page"] :global(svg) { color: var(--accent); }
}
</style>