289 lines
13 KiB
Svelte
289 lines
13 KiB
Svelte
<!-- SPDX-License-Identifier: CPAL-1.0
|
|
- Copyright (c) 2026 Marek Novák. Part of Ianitor, under the Common Public Attribution
|
|
- License 1.0: see LICENSE (its Exhibit A is this file's notice). -->
|
|
<script lang="ts">
|
|
// Handoff §8.3: Settings with General, API (⚙ → API → New token = 3 clicks, Copy token the
|
|
// accepted 4th), Share links and About.
|
|
import { onMount } from "svelte";
|
|
import { DomiCredit, DomiError, TextField } from "@domi/ui";
|
|
import { Banner, Button, Dialog, Segmented } from "@ianitor/ui";
|
|
import { ApiError, call, del, get, newKey, patch, post, sentence } from "../lib/api";
|
|
import { app, navigate, toast } from "../lib/app.svelte";
|
|
import { longDate, shortDate, whenInline } from "../lib/format";
|
|
import type { CreatedToken, ShareLink, TokenRow } from "../lib/types";
|
|
|
|
let { tab }: { tab: string } = $props();
|
|
const TABS = [
|
|
{ id: "general", label: "General" },
|
|
{ id: "api", label: "API" },
|
|
{ id: "share-links", label: "Share links" },
|
|
{ id: "about", label: "About" },
|
|
];
|
|
|
|
let settings = $state<{ repo: { name: string; last_push_at: string | null } | null; watch_seconds: number } | null>(null);
|
|
let tokens = $state<TokenRow[]>([]);
|
|
let revoked = $state<Record<string, string>>({});
|
|
let links = $state<ShareLink[]>([]);
|
|
let problem = $state<string | null>(null);
|
|
let expiry = $state<"year" | "never">("year");
|
|
let made = $state<CreatedToken | null>(null);
|
|
let tokenName = $state("");
|
|
let tokenCopied = $state(false);
|
|
let askClose = $state(false);
|
|
let nameErr = $state<string | null>(null);
|
|
let confirmRevoke = $state<string | null>(null);
|
|
let confirmStopAll = $state(false);
|
|
let creating = $state(false);
|
|
|
|
async function load() {
|
|
try {
|
|
if (tab === "general") settings = await get("/settings");
|
|
if (tab === "api") tokens = (await get<{ items: TokenRow[] }>("/tokens")).items;
|
|
if (tab === "share-links") links = (await get<{ items: ShareLink[] }>("/share-links")).items;
|
|
problem = null;
|
|
} catch (e) {
|
|
problem = sentence(e);
|
|
}
|
|
}
|
|
$effect(() => {
|
|
void tab;
|
|
void load();
|
|
});
|
|
onMount(() => {
|
|
const f = () => void load();
|
|
window.addEventListener("focus", f);
|
|
return () => window.removeEventListener("focus", f);
|
|
});
|
|
|
|
function tabKey(e: KeyboardEvent, i: number) {
|
|
let j = i;
|
|
if (e.key === "ArrowRight") j = (i + 1) % TABS.length;
|
|
else if (e.key === "ArrowLeft") j = (i - 1 + TABS.length) % TABS.length;
|
|
else if (e.key === "Home") j = 0;
|
|
else if (e.key === "End") j = TABS.length - 1;
|
|
else return;
|
|
e.preventDefault();
|
|
navigate(`/settings/${TABS[j].id}`);
|
|
setTimeout(() => document.getElementById(`tab-${TABS[j].id}`)?.focus(), 0);
|
|
}
|
|
|
|
async function newToken() {
|
|
creating = true;
|
|
try {
|
|
const name = `Token made ${shortDate(new Date().toISOString())}`;
|
|
const body = expiry === "never" ? { name, never_expires: true } : { name, expires_in_days: 365 };
|
|
made = await post<CreatedToken>("/tokens", body);
|
|
tokenName = made.name;
|
|
tokenCopied = false;
|
|
askClose = false;
|
|
} catch (e) {
|
|
problem = sentence(e);
|
|
} finally {
|
|
creating = false;
|
|
}
|
|
}
|
|
|
|
async function copyToken() {
|
|
if (!made) return;
|
|
try {
|
|
await navigator.clipboard.writeText(made.token);
|
|
tokenCopied = true;
|
|
} catch {
|
|
problem = "Your browser did not allow copying. Select the token and press Ctrl+C.";
|
|
}
|
|
}
|
|
|
|
// Renames the new token now (its field's seal) or when the dialog closes. False when the name was refused.
|
|
async function rename(): Promise<boolean> {
|
|
if (!made || !tokenName.trim() || tokenName.trim() === made.name) return true;
|
|
nameErr = null;
|
|
try {
|
|
await patch(`/tokens/${encodeURIComponent(made.id)}`, { name: tokenName.trim() });
|
|
made.name = tokenName.trim();
|
|
toast("Renamed.");
|
|
} catch (e) {
|
|
if (e instanceof ApiError && e.code === "token.name") {
|
|
nameErr = e.body.message;
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
async function closeToken(force = false) {
|
|
if (!made) return;
|
|
if (!tokenCopied && !force) {
|
|
askClose = true;
|
|
return;
|
|
}
|
|
if (!(await rename())) return;
|
|
// The token leaves memory with the dialog.
|
|
made = null;
|
|
tokenName = "";
|
|
void load();
|
|
}
|
|
|
|
async function revoke(t: TokenRow) {
|
|
try {
|
|
await del(`/tokens/${encodeURIComponent(t.id)}`);
|
|
revoked = { ...revoked, [t.id]: new Date().toISOString() };
|
|
confirmRevoke = null;
|
|
toast(`Revoked. Anything using "${t.name}" stops working now.`);
|
|
} catch (e) {
|
|
problem = sentence(e);
|
|
}
|
|
}
|
|
|
|
async function stop(l: ShareLink) {
|
|
try {
|
|
await del(`/share-links/${encodeURIComponent(l.id)}`);
|
|
toast("Stopped. The link stops working within a few seconds.");
|
|
void load();
|
|
} catch (e) {
|
|
problem = sentence(e);
|
|
}
|
|
}
|
|
|
|
async function stopAll() {
|
|
try {
|
|
await call("POST", "/share-links/stop-all", {}, { idem: newKey() });
|
|
toast("All share links are stopped. New ones work as usual.");
|
|
confirmStopAll = false;
|
|
void load();
|
|
} catch (e) {
|
|
problem = sentence(e);
|
|
}
|
|
}
|
|
|
|
const active = $derived(links.filter((l) => l.state === "active"));
|
|
const ended = $derived(links.filter((l) => l.state !== "active"));
|
|
const viewer = $derived(app.overview?.viewer ?? null);
|
|
</script>
|
|
|
|
<h1 class="ia-display">Settings</h1>
|
|
<div class="tabs" role="tablist" aria-label="Settings">
|
|
{#each TABS as t, i (t.id)}
|
|
<button type="button" role="tab" id="tab-{t.id}" aria-selected={tab === t.id} aria-controls="panel-{t.id}" tabindex={tab === t.id ? 0 : -1}
|
|
onclick={() => navigate(`/settings/${t.id}`)} onkeydown={(e) => tabKey(e, i)}>{t.label}</button>
|
|
{/each}
|
|
</div>
|
|
{#if problem}<Banner tone="notice" live="status" ondismiss={() => (problem = null)}>{#snippet title()}{problem}{/snippet}</Banner>{/if}
|
|
|
|
<div role="tabpanel" id="panel-{tab}" aria-labelledby="tab-{tab}" class="panel">
|
|
{#if tab === "general"}
|
|
<p class="ia-label">Routes repository</p>
|
|
<p>{settings?.repo ? `${settings.repo.name} on Gitea · ${settings.repo.last_push_at ? "pushed " + whenInline(settings.repo.last_push_at, app.now) : "not pushed yet"}` : "No place to push to is set up."}</p>
|
|
<p class="ia-label">Health watch after apply</p>
|
|
<p>{settings?.watch_seconds ?? app.overview?.site?.watch_seconds ?? 60} seconds</p>
|
|
<p class="ia-label">Signed in</p>
|
|
<p>{viewer ? (viewer.via === "authelia" ? `${viewer.name} through Authelia` : `API token "${viewer.name}"`) : "Through Authelia"}</p>
|
|
{#if app.overview?.site?.auth_logout_url}<p><a class="ia-link" href={app.overview.site.auth_logout_url}>Sign out</a></p>{/if}
|
|
{:else if tab === "api"}
|
|
<div class="row">
|
|
<h2 class="ia-h3">API tokens</h2>
|
|
<Button variant="outline" icon="plus" onclick={newToken} pending={creating}>New token</Button>
|
|
<Segmented legend="New tokens expire after" name="token-expiry" bind:value={expiry} options={[{ value: "year", label: "1 year" }, { value: "never", label: "Never" }]} />
|
|
</div>
|
|
<p class="ia-hint">The API listens on 127.0.0.1 only. Ianitor keeps a hash of each token, never the token.</p>
|
|
{#if tokens.length === 0}
|
|
<div class="ia-frame"><h2 class="ia-h3">No API tokens</h2><p class="ia-text2">Scripts and other Domi apps use a token to talk to Ianitor.</p></div>
|
|
{:else}
|
|
<ul class="rows">
|
|
{#each tokens as t (t.id)}
|
|
<li>
|
|
<div class="grow">
|
|
<span class="tname" class:struck={!!revoked[t.id] || !!t.revoked_at}>{t.name}</span>
|
|
<span class="ia-sub" style="display:block">
|
|
{#if revoked[t.id]}Revoked {whenInline(revoked[t.id], app.now)}{:else}Made {shortDate(t.created_at)} · {t.last_used_at ? `last used ${whenInline(t.last_used_at, app.now)}` : "never used"} · {t.expires_at ? `expires ${shortDate(t.expires_at)}` : "never expires"}{/if}
|
|
</span>
|
|
</div>
|
|
{#if !revoked[t.id] && !t.revoked_at}
|
|
{#if confirmRevoke === t.id}
|
|
<span>Revoke "{t.name}"? Anything using it stops working now.</span>
|
|
<Button small onclick={() => (confirmRevoke = null)} data-autofocus>Cancel</Button>
|
|
<Button small variant="danger" onclick={() => revoke(t)}>Revoke</Button>
|
|
{:else}
|
|
<Button small variant="danger" onclick={() => (confirmRevoke = t.id)}>Revoke</Button>
|
|
{/if}
|
|
{/if}
|
|
</li>
|
|
{/each}
|
|
</ul>
|
|
{/if}
|
|
{:else if tab === "share-links"}
|
|
<div class="row">
|
|
<h2 class="ia-h3">Share links</h2>
|
|
{#if active.length}<Button variant="danger" onclick={() => (confirmStopAll = true)}>Stop all share links</Button>{/if}
|
|
</div>
|
|
{#if app.overview?.engine.running === "caddy"}
|
|
<Banner tone="notice">{#snippet title()}Caddy is serving, so these links do not work right now. They work again after switching back to Pingora.{/snippet}</Banner>
|
|
{/if}
|
|
{#if confirmStopAll}
|
|
<p>Stop all {active.length} active links? Every link made so far stops working. New ones work as usual.
|
|
<Button small onclick={() => (confirmStopAll = false)}>Cancel</Button> <Button small variant="danger" onclick={stopAll}>Stop all</Button></p>
|
|
{/if}
|
|
{#if links.length === 0}
|
|
<div class="ia-frame"><h2 class="ia-h3">No share links</h2><p class="ia-text2">Make one from a host's menu (⋯), Share link.</p></div>
|
|
{:else}
|
|
<ul class="rows">
|
|
{#each active as l (l.id)}
|
|
<li>
|
|
<div class="grow"><strong>{l.hostname}</strong>{l.path_prefix !== "/" ? l.path_prefix : ""}
|
|
<span class="ia-sub" style="display:block">Made {whenInline(l.created_at, app.now)} · expires {whenInline(l.expires_at, app.now)}{l.has_password ? " · password" : ""}</span></div>
|
|
<Button small onclick={() => stop(l)}>Stop this link</Button>
|
|
</li>
|
|
{/each}
|
|
</ul>
|
|
{#if ended.length}
|
|
<details><summary>Ended ({ended.length})</summary>
|
|
<ul class="rows">{#each ended as l (l.id)}<li><span class="grow">{l.hostname}{l.path_prefix !== "/" ? l.path_prefix : ""}</span><span class="ia-sub">{l.state} {shortDate(l.expires_at)}</span></li>{/each}</ul>
|
|
</details>
|
|
{/if}
|
|
{/if}
|
|
{:else}
|
|
<p>Ianitor Domi 0.1.0</p>
|
|
<p>CPAL-1.0 (Common Public Attribution License 1.0). Ianitor 0.2.0 and earlier stay MPL-2.0.</p>
|
|
<div class="about-credit"><DomiCredit /></div>
|
|
<p>Built, tested and signed by Fabrica Domi</p>
|
|
<p>Using Ianitor over a network counts as distribution under CPAL-1.0 (§15), so its source, with any changes, stays
|
|
available: <a class="ia-link" href="https://gitea.marek-novak.cz/mnovak/Ianitor" rel="noreferrer">Source on Gitea</a></p>
|
|
{/if}
|
|
</div>
|
|
|
|
{#if made}
|
|
<Dialog title="Your new token" escape={false} onclose={() => closeToken()}>
|
|
<p><strong>Copy it now and save it in Custos.</strong></p>
|
|
<p class="ia-text2">Ianitor cannot show it again; if it is lost, make a new one and revoke this one.</p>
|
|
<input class="ia-secret" readonly value={made.token} aria-label="New API token" onfocus={(e) => (e.currentTarget as HTMLInputElement).select()} />
|
|
<div class="grid">
|
|
<TextField id="token-name" label="Name" bind:value={tokenName} autocomplete="off" action="save" actionLabel="Rename the token"
|
|
onsend={async () => { if (!(await rename())) throw new DomiError(nameErr ?? "That name was not accepted."); }}
|
|
error={nameErr ?? undefined} hint="Rename it now or later, so you know what uses it." />
|
|
<Button variant={tokenCopied ? "quiet" : "primary"} icon="copy" onclick={copyToken} data-autofocus>{tokenCopied ? "Copy again" : "Copy token"}</Button>
|
|
</div>
|
|
{#if tokenCopied}<p role="status">Copied. Paste it into Custos now. Ianitor cannot show it again.</p>{/if}
|
|
<p class="ia-muted">{made.expires_at ? `Expires on ${longDate(made.expires_at, false)}.` : "Never expires."}</p>
|
|
{#if askClose}
|
|
<p role="alert">Close without copying? The token cannot be shown again.
|
|
<Button small onclick={() => (askClose = false)}>Go back</Button> <Button small variant="danger" onclick={() => closeToken(true)}>Close anyway</Button></p>
|
|
{/if}
|
|
{#snippet footer()}<Button onclick={() => closeToken()}>I have saved it</Button>{/snippet}
|
|
</Dialog>
|
|
{/if}
|
|
|
|
<style>
|
|
.about-credit :global(.domi-credit) { justify-content: flex-start; }
|
|
.tabs { display: flex; gap: 4px; overflow-x: auto; }
|
|
.tabs [role="tab"] { min-height: 44px; padding: 0 1rem; border-radius: var(--radius-sm); border: 0; background: none; color: var(--domi-fg-2); font-weight: 600; cursor: pointer; }
|
|
.tabs [role="tab"][aria-selected="true"] { background: var(--domi-hover); color: var(--domi-fg); }
|
|
.panel { display: flex; flex-direction: column; gap: 10px; }
|
|
.row { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
|
|
.rows { list-style: none; margin: 0; padding: 0; }
|
|
.rows li { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; min-height: 44px; padding: 8px 0; border-top: 1px solid var(--domi-line); }
|
|
.grow { flex: 1 1 200px; min-width: 0; }
|
|
.tname { font-weight: 600; }
|
|
.struck { text-decoration: line-through; color: var(--domi-muted); }
|
|
.grid { display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: end; }
|
|
@media (max-width: 719.98px) { .grid { grid-template-columns: 1fr; } }
|
|
</style>
|