Repository Public
1// Application state machine: loading → welcome / join / enrol / recover / locked → main app; public share viewer.2import { useEffect, useRef, useState } from "react";3import { engine, errText, getPrefs, loadPrefs, notice, useEngine } from "./state.ts";4import { Btn, ConfirmHost, ErrorText, Logo, ToastHost } from "./ui/common.tsx";5import { AuthLayout, Join, Locked, Recover, Welcome } from "./ui/Onboarding.tsx";6import { EnrolBrowser } from "./ui/Pairing.tsx";7import { PublicShare } from "./ui/PublicShare.tsx";8import { Shell } from "./ui/Shell.tsx";910type Route = { k: "join"; token: string } | { k: "share"; id: string; secret: string } | { k: "app" };11type Phase = { k: "loading" } | { k: "error"; message: string } | { k: "welcome" } | { k: "locked" } | { k: "join"; token: string } | { k: "enrol" } | { k: "recover"; handle?: string } | { k: "share"; id: string; secret: string } | { k: "app" };1213/** Reads the hash route and immediately removes invitation tokens / share secrets from the address bar and history entry. */14function takeRoute(): Route {15 const h = location.hash;16 const join = /^#\/join\/([A-Za-z0-9_-]{20,80})$/.exec(h), share = /^#\/s\/([0-9a-f-]{36})\/([A-Za-z0-9_-]{1,64})$/.exec(h);17 if (join || share || h.startsWith("#/join/") || h.startsWith("#/s/")) history.replaceState(null, "", location.pathname + location.search);18 if (join) return { k: "join", token: join[1]! };19 if (share) return { k: "share", id: share[1]!, secret: share[2]! };20 if (h.startsWith("#/s/")) return { k: "share", id: "", secret: "" };21 return { k: "app" };22}23const initialRoute = takeRoute();2425export function App() {26 const { version } = useEngine();27 const [phase, setPhase] = useState<Phase>({ k: "loading" });28 const loaded = useRef(false);29 const home = (): Phase => (engine.unlocked ? { k: "app" } : engine.identity ? { k: "locked" } : { k: "welcome" });3031 useEffect(() => {32 if (loaded.current) return; loaded.current = true;33 (async () => {34 try {35 await engine.load(); await loadPrefs();36 setPhase(initialRoute.k === "join" ? { k: "join", token: initialRoute.token } : initialRoute.k === "share" ? { k: "share", id: initialRoute.id, secret: initialRoute.secret } : home());37 } catch (e) { setPhase({ k: "error", message: `This browser's local storage could not be opened (${errText(e)}). Private browsing modes may block it.` }); }38 })();39 const onHash = () => { const r = takeRoute(); if (r.k === "join") setPhase({ k: "join", token: r.token }); else if (r.k === "share") setPhase({ k: "share", id: r.id, secret: r.secret }); };40 window.addEventListener("hashchange", onHash);41 return () => window.removeEventListener("hashchange", onHash);42 }, []);4344 // the app is only ever shown unlocked: any lock (button, timer, ended session) falls back to the locked screen45 useEffect(() => { if (phase.k === "app" && !engine.unlocked) setPhase(home()); }, [version, phase.k]);4647 // auto-lock after inactivity (checked against wall-clock time so that a sleeping laptop locks on wake)48 useEffect(() => {49 let last = Date.now();50 const touch = () => { last = Date.now(); };51 const check = () => {52 if (!engine.unlocked) { last = Date.now(); return; }53 if (Date.now() - last >= getPrefs().autoLockMin * 60_000) { notice.set("Locked automatically after inactivity."); engine.lock(); }54 };55 const events = ["pointerdown", "keydown", "wheel", "touchstart"] as const;56 events.forEach((e) => window.addEventListener(e, touch, { passive: true, capture: true }));57 const vis = () => check();58 document.addEventListener("visibilitychange", vis);59 const t = setInterval(check, 5000);60 return () => { events.forEach((e) => window.removeEventListener(e, touch, { capture: true })); document.removeEventListener("visibilitychange", vis); clearInterval(t); };61 }, []);6263 useEffect(() => { document.title = phase.k === "locked" ? "SP Vault — locked" : "SP Vault"; }, [phase.k]);6465 let screen;66 switch (phase.k) {67 case "loading": screen = <div className="boot" role="status" aria-label="Loading SP Vault"><Logo size={56} /><span className="spinner big" /></div>; break;68 case "error": screen = <AuthLayout><div className="stack"><h1 className="serif auth-title">SP Vault cannot start</h1><ErrorText text={phase.message} /><Btn onClick={() => location.reload()}>Try again</Btn></div></AuthLayout>; break;69 case "welcome": screen = <Welcome onPending={() => setPhase({ k: "enrol" })} onRecover={() => setPhase({ k: "recover" })} />; break;70 case "locked": screen = engine.unlocked ? null : <Locked onPending={() => setPhase({ k: "enrol" })} onForgotten={() => setPhase({ k: "welcome" })} />; break;71 case "join": screen = <Join invite={phase.token} onDone={() => setPhase({ k: "app" })} onExisting={() => setPhase(home())} />; break;72 case "enrol": screen = <AuthLayout wide><EnrolBrowser handle={engine.accountInfo?.handle ?? ""} onDone={() => setPhase({ k: "app" })} onUseRecovery={() => setPhase({ k: "recover", handle: engine.accountInfo?.handle })} onCancel={() => setPhase(home())} /></AuthLayout>; break;73 case "recover": screen = <Recover initialHandle={phase.handle} onDone={() => setPhase({ k: "app" })} onBack={() => setPhase(home())} />; break;74 case "share": screen = <PublicShare id={phase.id} secret={phase.secret} onLeave={() => setPhase(home())} />; break;75 case "app": screen = engine.unlocked ? <Shell onSignedOut={() => setPhase(home())} /> : null; break;76 }77 // a locked identity that unlocks from the locked screen moves on to the app78 useEffect(() => { if ((phase.k === "locked" || phase.k === "welcome") && engine.unlocked) setPhase({ k: "app" }); }, [version, phase.k]);7980 return <>{screen}<ToastHost /><ConfirmHost /></>;81}82