From 4e3c721ef51a2bd05be3ed37a59f78fe67adc27b Mon Sep 17 00:00:00 2001 From: mnovak Date: Tue, 17 Nov 2026 20:41:53 +0100 Subject: [PATCH] =?UTF-8?q?[web]=20Previews=20and=20the=20team:=20a=20Prev?= =?UTF-8?q?iews=20screen=20(see:=201=20click,=20close:=202),=20sign-in=20f?= =?UTF-8?q?rom=20a=20setup=20or=20invite=20link,=20Settings=20=E2=86=92=20?= =?UTF-8?q?Team=20(invite:=203=20clicks),=20previews-only=20API=20tokens;?= =?UTF-8?q?=20click-counted=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web/src/App.svelte | 47 ++++++++- apps/web/src/components/TeamTab.svelte | 137 +++++++++++++++++++++++++ apps/web/src/lib/api.ts | 2 +- apps/web/src/lib/app.svelte.ts | 2 + apps/web/src/lib/types.ts | 34 ++++++ apps/web/src/views/Previews.svelte | 103 +++++++++++++++++++ apps/web/src/views/Settings.svelte | 19 ++-- apps/web/tests/flows.test.ts | 31 ++++++ apps/web/tests/mockApi.ts | 14 ++- 9 files changed, 377 insertions(+), 12 deletions(-) create mode 100644 apps/web/src/components/TeamTab.svelte create mode 100644 apps/web/src/views/Previews.svelte diff --git a/apps/web/src/App.svelte b/apps/web/src/App.svelte index f073ddc..22b687a 100644 --- a/apps/web/src/App.svelte +++ b/apps/web/src/App.svelte @@ -15,6 +15,7 @@ import Certificates from "./views/Certificates.svelte"; import History from "./views/History.svelte"; import Hosts from "./views/Hosts.svelte"; + import Previews from "./views/Previews.svelte"; import RenewByHand from "./views/RenewByHand.svelte"; import Settings from "./views/Settings.svelte"; import SignedIn from "./views/SignedIn.svelte"; @@ -24,6 +25,7 @@ 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] === "previews") return { view: "previews" 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" }; @@ -32,18 +34,51 @@ const NAV: { href: string; label: string; icon: IconName; views: string[] }[] = [ { href: "/", label: "Hosts", icon: "building", views: ["hosts"] }, + { href: "/previews", label: "Previews", icon: "link", views: ["previews"] }, { 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 = { hosts: "Hosts", history: "History", certs: "Certificates", renew: "Certificates", settings: "Settings", "signed-in": "Signed in" }; + const TITLES: Record = { hosts: "Hosts", history: "History", previews: "Previews", certs: "Certificates", renew: "Certificates", settings: "Settings", "signed-in": "Signed in" }; $effect(() => { document.title = `${TITLES[route.view] ?? "Hosts"} — Ianitor Domi`; }); + // A setup or invite link (`#signin=`, crate::team): one click on the link signs in. + async function signInFromLink(): Promise { + const m = /^#signin=([A-Za-z0-9_-]{8,100})$/.exec(location.hash); + if (!m) return; + history.replaceState(null, "", location.pathname + location.search); + try { + const r = await fetch("/api/v1/session", { + method: "POST", + headers: { "Content-Type": "application/json", "X-Ianitor-UI": "1" }, + body: JSON.stringify({ code: m[1], device: navigator.platform || "Browser" }), + credentials: "same-origin", + }); + if (!r.ok) { + const j = await r.json().catch(() => null); + app.linkProblem = j?.error?.message ?? "This sign-in link does not work."; + } + } catch { + app.linkProblem = "Ianitor did not answer the sign-in."; + } + } + + async function signOutSession() { + try { + await fetch("/api/v1/session/end", { method: "POST", headers: { "X-Ianitor-UI": "1" }, credentials: "same-origin" }); + } finally { + location.href = "/"; + } + } + onMount(() => { if (route.view === "signed-in") return; + if (location.hash.startsWith("#signin=")) { + void signInFromLink().then(() => refresh()); + } const stop = startPolling(); // Resume an apply this tab was watching before a reload or a sign-in (§3). const id = remembered(); @@ -101,7 +136,8 @@ go(e, n.href)}>{n.label} {/each}
-

{viewer?.via === "authelia" ? `Signed in as ${viewer.name} through Authelia` : viewer ? `Using the API token "${viewer.name}"` : "Signed in through Authelia"}

+

{viewer?.via === "authelia" ? `Signed in as ${viewer.name} through Authelia` : viewer?.via === "session" ? `Signed in as ${viewer.name}` : viewer ? `Using the API token "${viewer.name}"` : "Not signed in"}

+ {#if viewer?.via === "session"}{/if}

Control on 127.0.0.1 only

{#if app.overview?.site?.auth_logout_url}Sign out{/if}
@@ -109,8 +145,11 @@
IANITOR DOMI{#if app.phone}{/if}
+ {#if app.linkProblem}{/if} {#if route.view === "hosts"} + {:else if route.view === "previews"} + {:else if route.view === "history"} {#key route.host}{/key} {:else if route.view === "certs"} @@ -141,7 +180,7 @@ {#if app.signedOut} {}}> -

Sign in again; this page keeps what you typed.

+

Sign in again; this page keeps what you typed. Without Authelia, open your sign-in link again or ask an admin of your team for a new one.

{#snippet footer()} @@ -167,7 +206,7 @@ .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 { display: grid; grid-template-columns: repeat(5, 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); } diff --git a/apps/web/src/components/TeamTab.svelte b/apps/web/src/components/TeamTab.svelte new file mode 100644 index 0000000..44582d2 --- /dev/null +++ b/apps/web/src/components/TeamTab.svelte @@ -0,0 +1,137 @@ + + + +{#if problem} (problem = null)}>{#snippet title()}{problem}{/snippet}{/if} +{#if team?.authelia} +

Authelia signs people in on this server. Team links work too.

+{/if} +

People

+
    + {#each team?.members ?? [] as m (m.id)} +
  • +
    {m.name}{team?.me?.id === m.id ? " (you)" : ""} + {m.role} · since {shortDate(m.added_at)}{m.match_code ? ` · Domi identity ${m.match_code}` : ""}
    + {#if admin && team?.me?.id !== m.id} + {#if confirmRemove === m.id} + Remove {m.name}? They are signed out everywhere. + + + {:else} + + {/if} + {/if} +
  • + {/each} +
+ +{#if admin} +
+
+ + +
+ + +
+

Members change hosts and previews. Admins also manage people, API tokens, the engine and certificates.

+ {#if link} +
+ (e.currentTarget as HTMLInputElement).select()} /> + +
+ {/if} +{/if} + +{#if team?.me} +

Your Domi identity

+
+
+ + +
+ +
+

Your teammates see its match code next to your name, the same digits Custos shows.

+{/if} + + diff --git a/apps/web/src/lib/api.ts b/apps/web/src/lib/api.ts index 5eedc8f..59b4ad0 100644 --- a/apps/web/src/lib/api.ts +++ b/apps/web/src/lib/api.ts @@ -139,7 +139,7 @@ export async function call( await sleep(Math.min(60, env.error.retry_after_s ?? 1) * 1000); continue; } - if (res.status === 401 && env.error.code.startsWith("token.")) { + if (res.status === 401 && (env.error.code.startsWith("token.") || env.error.code.startsWith("session."))) { // The UI holds no token: a token refusal means the sign-in did not reach control. events.onSignedOut?.(); throw new SignedOut(env.error.message); diff --git a/apps/web/src/lib/app.svelte.ts b/apps/web/src/lib/app.svelte.ts index be2af13..3f73ec6 100644 --- a/apps/web/src/lib/app.svelte.ts +++ b/apps/web/src/lib/app.svelte.ts @@ -14,6 +14,8 @@ export interface Toast { } export const app = $state({ + /** Why a `#signin=` link did not sign this browser in. */ + linkProblem: null as string | null, overview: null as Overview | null, etag: null as string | null, loadError: false, diff --git a/apps/web/src/lib/types.ts b/apps/web/src/lib/types.ts index 73bffda..63bf738 100644 --- a/apps/web/src/lib/types.ts +++ b/apps/web/src/lib/types.ts @@ -212,6 +212,7 @@ export interface TokenRow { last_used_at: string | null; expires_at: string | null; revoked_at?: string | null; + scope?: "full" | "previews"; } export interface CreatedToken { @@ -249,3 +250,36 @@ export interface Bundle { filename: string; qr_svg: string | null; } + +// T4: branch previews and the team (docs/previews.md, crate::team). +export interface PreviewView { + hostname: string; + url: string; + project: string; + branch: string; + upstream: string; + expires_at: string; + state: "applying" | "live" | "failed"; + problem?: string | null; +} + +export interface PreviewList { + items: PreviewView[]; + domain: string | null; +} + +export interface MemberView { + id: string; + name: string; + role: "admin" | "member"; + added_at: string; + tessera_root: string | null; + match_code: string | null; +} + +export interface TeamView { + me: MemberView | null; + members: MemberView[]; + invites: { id: string; name: string | null; role: string; expires_at: string }[]; + authelia: boolean; +} diff --git a/apps/web/src/views/Previews.svelte b/apps/web/src/views/Previews.svelte new file mode 100644 index 0000000..554f0d7 --- /dev/null +++ b/apps/web/src/views/Previews.svelte @@ -0,0 +1,103 @@ + + + +

Previews

+{#if problem} (problem = null)}>{#snippet title()}{problem}{/snippet}{/if} + +{#if list && !list.domain} +
+

Previews are off

+

Set a preview domain in control.toml ([previews] domain) and point a wildcard DNS record at this server. Then every branch Fabrica builds gets its own address here.

+
+{:else if list && list.items.length === 0} +
+

No previews open

+

When Fabrica builds a branch, its preview appears here as <branch>--<project>.{list.domain}. It closes on merge or when its time runs out.

+
+{:else if list} +

Under {list.domain}. Fabrica opens them with a token that can only manage previews.

+
    + {#each list.items as p (p.hostname)} + {@const [kind, label] = badge(p)} +
  • + +
    + {p.hostname} + {p.branch} · {p.project} · to {p.upstream} · closes {whenInline(p.expires_at, app.now)} + {#if p.problem}{p.problem}{/if} +
    + {#if confirm === p.hostname} + Close the preview of {p.branch}? Its address stops working. + + + {:else} + + {/if} +
  • + {/each} +
+{/if} + + diff --git a/apps/web/src/views/Settings.svelte b/apps/web/src/views/Settings.svelte index 2cd2f0f..30a0c38 100644 --- a/apps/web/src/views/Settings.svelte +++ b/apps/web/src/views/Settings.svelte @@ -6,6 +6,7 @@ // accepted 4th), Share links and About. import { onMount } from "svelte"; import { Banner, Button, Dialog, Segmented } from "@ianitor/ui"; + import TeamTab from "../components/TeamTab.svelte"; 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"; @@ -14,6 +15,7 @@ let { tab }: { tab: string } = $props(); const TABS = [ { id: "general", label: "General" }, + { id: "team", label: "Team" }, { id: "api", label: "API" }, { id: "share-links", label: "Share links" }, { id: "about", label: "About" }, @@ -25,6 +27,7 @@ let links = $state([]); let problem = $state(null); let expiry = $state<"year" | "never">("year"); + let scope = $state<"full" | "previews">("full"); let made = $state(null); let tokenName = $state(""); let tokenCopied = $state(false); @@ -69,8 +72,8 @@ 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 }; + const name = scope === "previews" ? `Fabrica previews ${shortDate(new Date().toISOString())}` : `Token made ${shortDate(new Date().toISOString())}`; + const body = expiry === "never" ? { name, never_expires: true, scope } : { name, expires_in_days: 365, scope }; made = await post("/tokens", body); tokenName = made.name; tokenCopied = false; @@ -164,18 +167,22 @@
{#if tab === "general"}

Routes repository

-

{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."}

+

{settings?.repo ? `${settings.repo.name} · ${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."}

Health watch after apply

{settings?.watch_seconds ?? app.overview?.site?.watch_seconds ?? 60} seconds

Signed in

-

{viewer ? (viewer.via === "authelia" ? `${viewer.name} through Authelia` : `API token "${viewer.name}"`) : "Through Authelia"}

+

{viewer ? (viewer.via === "authelia" ? `${viewer.name} through Authelia` : viewer.via === "session" ? viewer.name : `API token "${viewer.name}"`) : "Not signed in"}

{#if app.overview?.site?.auth_logout_url}

Sign out

{/if} + {:else if tab === "team"} + {:else if tab === "api"}

API tokens

+
+

Fabrica needs a previews-only token: it can open and close branch previews and touch nothing else. Keep it in the Custos team vault.

The API listens on 127.0.0.1 only. Ianitor keeps a hash of each token, never the token.

{#if tokens.length === 0}

No API tokens

Scripts and other Domi apps use a token to talk to Ianitor.

@@ -184,7 +191,7 @@ {#each tokens as t (t.id)}
  • - {t.name} + {t.name}{#if t.scope === "previews"} previews only{/if} {#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} @@ -233,7 +240,7 @@ {/if} {/if} {:else} -

    Ianitor Domi 0.1.0

    +

    Ianitor Domi 0.2.0

    MPL-2.0

    Built, tested and signed by Fabrica Domi

    Source on Gitea

    diff --git a/apps/web/tests/flows.test.ts b/apps/web/tests/flows.test.ts index 8a21e87..6ff7534 100644 --- a/apps/web/tests/flows.test.ts +++ b/apps/web/tests/flows.test.ts @@ -170,6 +170,37 @@ describe("ROADMAP §4 flows, clicks counted", () => { }); }); +describe("T4 flows, clicks counted", () => { + it("See active previews = 1 click", async () => { + start(); + const nav = (await screen.findAllByRole("navigation", { name: "Main" }, T))[0]; + await click(within(nav).getByRole("link", { name: "Previews" })); + await screen.findByRole("link", { name: "fix-42--shop.preview.home.test" }, T); + expect(clicks).toBe(1); + }); + + it("Kill a preview = 2 clicks (Close → Close preview)", async () => { + start("/previews"); + await click(await screen.findByRole("button", { name: "Close" }, T)); + await click(await screen.findByRole("button", { name: "Close preview" })); + await waitFor(() => expect(called("DELETE", "/previews/fix-42--shop.preview.home.test")).toBe(true), T); + await screen.findByRole("heading", { name: "No previews open" }, T); + expect(clicks).toBe(2); + }); + + it("Invite a teammate: Settings → Team → Invite = 3 clicks, the link shown", async () => { + start(); + const nav = (await screen.findAllByRole("navigation", { name: "Main" }, T))[0]; + await click(within(nav).getByRole("link", { name: "Settings" })); + await click(await screen.findByRole("tab", { name: "Team" })); + await user.type(await screen.findByLabelText("Name"), "Petra"); + await click(screen.getByRole("button", { name: "Invite" })); + const link = (await screen.findByLabelText("Invite link", {}, T)) as HTMLInputElement; + expect(link.value).toMatch(/\/#signin=iin_testcode$/); + expect(clicks).toBe(3); + }); +}); + describe("focus", () => { it("+ Host puts the cursor in Hostname, so typing right after the click lands there", async () => { start(); diff --git a/apps/web/tests/mockApi.ts b/apps/web/tests/mockApi.ts index d849ccc..8e86dbe 100644 --- a/apps/web/tests/mockApi.ts +++ b/apps/web/tests/mockApi.ts @@ -12,6 +12,7 @@ export interface Mock { calls: { method: string; path: string; body: unknown }[]; state: { engine: "pingora" | "caddy"; + previewClosed: boolean; switching: number; intermediate: "ok" | "due"; renewal: boolean; @@ -65,7 +66,7 @@ export type Route = [string, RegExp, (mm: RegExpMatchArray, body: Json) => [numb export function newMock(): Mock { return { calls: [], - state: { engine: "pingora", switching: 0, intermediate: "due", renewal: false, requestPolls: 0, hostsAdded: [] }, + state: { engine: "pingora", switching: 0, intermediate: "due", renewal: false, requestPolls: 0, hostsAdded: [], previewClosed: false }, }; } @@ -170,6 +171,17 @@ export function mockRoutes(m: Mock): Route[] { ["GET", /^\/tokens$/, () => [200, { items: [] }]], ["POST", /^\/tokens$/, (_, b) => [201, { id: "tk_1", name: b.name, token: "ian_tk1_secretvalue", expires_at: later(365 * 86400) }]], ["GET", /^\/share-links$/, () => [200, { items: [], epoch: 0 }]], + // T4: previews and the team (invented .test names only). + ["GET", /^\/previews$/, () => [200, { + domain: "preview.home.test", + items: m.state.previewClosed ? [] : [{ hostname: "fix-42--shop.preview.home.test", url: "https://fix-42--shop.preview.home.test/", project: "team/shop", branch: "fix-42", upstream: "http://10.0.0.7:8080", expires_at: later(86400), state: "live" }], + }]], + ["DELETE", /^\/previews\/fix-42--shop\.preview\.home\.test$/, () => { + m.state.previewClosed = true; + return [202, { apply: apply("ch_pv", "queued", "Closed the preview of fix-42 (team/shop)", ["fix-42--shop.preview.home.test"]) }]; + }], + ["GET", /^\/team$/, () => [200, { me: { id: "m1", name: "Mark", role: "admin", added_at: now(), tessera_root: null, match_code: null }, members: [{ id: "m1", name: "Mark", role: "admin", added_at: now(), tessera_root: null, match_code: null }], invites: [], authelia: false }]], + ["POST", /^\/team\/invites$/, (_, b) => [201, { invite: { id: "iv1", name: b.name, role: b.role, expires_at: later(7 * 86400) }, link_fragment: "#signin=iin_testcode" }]], ]; }