/* 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) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[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]) => `${t}`).join(""); $("top").innerHTML = `◆ProjectOS
TEST MODE
`; api("/api/admin/session").then((r) => { const authed = r.ok && r.body.authenticated; $("who").innerHTML = authed ? '' : 'Sign in'; 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 = `
SIGN IN
${note ? `

${esc(note)}

` : ""}
`; $("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();