Watchtower
Deploy GitHub main 67e6ec2 (PR #2: wallet UI, devices, settings)
1444c0a
Raw History Blame Contribute Delete
3.21 kB
/* Shared helpers + header/nav for every ProjectOS wallet page. */
const $ = (id) => document.getElementById(id);
const fmt = (minor, cur = "usd") => new Intl.NumberFormat(undefined, { style: "currency", currency: cur.toUpperCase() }).format(minor / 100);
const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c]));
async function api(path, opts = {}) {
const o = { credentials: "same-origin", ...opts };
if (o.body && typeof o.body !== "string") { o.body = JSON.stringify(o.body); o.headers = { "content-type": "application/json", ...(o.headers || {}) }; }
let r; try { r = await fetch(path, o); } catch { return { ok: false, status: 0, body: { error: "Network error: could not reach the server." } }; }
let body = {}; try { body = await r.json(); } catch {}
return { ok: r.ok, status: r.status, body };
}
function msg(el, cls, text) { el.className = "msg " + cls; el.textContent = text; }
const NAV = [["/", "Wallet"], ["/devices", "Devices"], ["/settings", "Settings"]];
function renderHeader() {
const here = location.pathname.replace(/\/$/, "") || "/";
const links = NAV.map(([h, t]) => `<a href="${h}"${(here === h || here === h + ".html") ? ' class="active" aria-current="page"' : ""}>${t}</a>`).join("");
$("top").innerHTML = `<a class="brand" href="/"><span class="mark">◆</span>Project<span class="light">OS</span></a>
<nav class="nav" aria-label="Main">${links}</nav>
<div class="right"><span class="badge">TEST MODE</span><span id="who"></span></div>`;
api("/api/admin/session").then((r) => {
const authed = r.ok && r.body.authenticated;
$("who").innerHTML = authed ? '<button class="link" id="logout" type="button">Sign out</button>' : '<a class="signin" href="/settings">Sign in</a>';
if (authed) $("logout").addEventListener("click", async () => { await api("/api/admin/logout", { method: "POST", body: {} }); location.href = "/"; });
});
}
async function loadStatus() {
const r = await api("/healthz"); const el = $("status"); if (!el) return;
el.textContent = r.ok ? `● Service OK · TEST mode · Stripe: ${r.body.stripe ? "configured" : "not configured"} · storage: ${r.body.store} · ${new Date().toLocaleTimeString()}` : "Service unreachable";
}
/** Shows an inline sign-in form inside `host`; calls onDone() after success. */
function signInForm(host, onDone, note) {
host.innerHTML = `<form id="loginForm" class="stack"><div class="label">SIGN IN</div>${note ? `<p class="muted">${esc(note)}</p>` : ""}
<label for="passcode">Admin passcode</label><input id="passcode" type="password" autocomplete="current-password" required style="width:100%;max-width:340px">
<div><button class="primary" type="submit">Sign in</button></div><div id="loginMsg" class="msg" role="status"></div></form>`;
$("loginForm").addEventListener("submit", async (e) => {
e.preventDefault();
const r = await api("/api/admin/login", { method: "POST", body: { passcode: $("passcode").value } });
if (r.ok) { $("passcode").value = ""; onDone(); } else msg($("loginMsg"), "err", r.body.error || `Sign-in failed (${r.status})`);
});
}
renderHeader(); loadStatus();