Watchtower
Deploy GitHub main 67e6ec2 (PR #2: wallet UI, devices, settings)
1444c0a
Raw History Blame Contribute Delete
4.11 kB
async function loadBalance() { const r = await api("/wallet/balance?currency=usd"); $("balance").textContent = r.ok ? fmt(r.body.balance, r.body.currency) : "Unavailable"; }
async function loadTxs() {
const r = await api("/wallet/transactions"); const ul = $("txs");
if (!r.ok) { ul.innerHTML = '<li class="err">Could not load transactions</li>'; return; }
if (!r.body.length) { ul.innerHTML = '<li class="muted">No transactions yet — add funds or pay an invoice.</li>'; return; }
ul.innerHTML = r.body.slice(0, 10).map((t) => {
const amt = Math.abs(t.postings[0]?.amount ?? 0); const pos = t.kind === "funding";
return `<li><div><strong>${esc(t.memo || t.kind)}</strong><small>${esc(t.kind)} · ${new Date(t.created_at).toLocaleString()}</small></div><span class="amt ${pos ? "pos" : "neg"}">${pos ? "+" : "−"}${fmt(amt)}</span></li>`;
}).join("");
}
$("example").addEventListener("click", () => { $("qr").value = JSON.stringify({ v: 1, invoice_id: "INV-" + Date.now().toString(36), merchant_id: "demo-merchant", merchant_name: "Demo Merchant", amount: 500, currency: "usd" }); });
$("payForm").addEventListener("submit", async (e) => {
e.preventDefault(); const qr = $("qr").value.trim(); const m = $("payMsg");
if (!qr) return msg(m, "err", "Paste an invoice QR payload first (or click “Fill example”).");
const r = await api("/invoices/pay", { method: "POST", body: { qr } });
if (r.ok) { msg(m, "ok", r.body.status === "already_paid" ? "This invoice was already paid." : `Paid ✓ — new balance ${fmt(r.body.balance)}`); $("qr").value = ""; refresh(); }
else msg(m, "err", r.body.error || `Payment failed (${r.status})`);
});
$("fund").addEventListener("click", async () => {
const m = $("fundMsg"); const amount = Math.round(Number($("fundAmt").value) * 100);
if (!(amount >= 50)) return msg(m, "err", "Enter an amount of at least $0.50.");
$("fund").disabled = true;
const r = await api("/funding/checkout", { method: "POST", body: { amount, currency: "usd" } });
$("fund").disabled = false;
if (r.status === 503) { $("testCredit").hidden = false; return msg(m, "warn", "Stripe isn't configured yet."); }
if (r.ok && r.body.url) { location.href = r.body.url; return; }
msg(m, "err", r.body.error || `Could not start checkout (${r.status})`);
});
$("testCreditBtn").addEventListener("click", async () => {
const m = $("fundMsg"); const amount = Math.round(Number($("fundAmt").value) * 100);
const r = await api("/wallet/test-credit", { method: "POST", body: { amount } });
if (r.status === 401 || r.status === 503) return msg(m, "warn", "Sign in first (Settings page) to add simulated credit.");
if (r.ok) { msg(m, "ok", `Added simulated credit ✓ — balance ${fmt(r.body.balance)}`); refresh(); } else msg(m, "err", r.body.error || `Failed (${r.status})`);
});
async function loadApps() {
const r = await api("/links.json"); const el = $("apps"); if (!r.ok) { el.innerHTML = '<p class="muted">No apps configured.</p>'; return; }
el.innerHTML = r.body.apps.map((a) => {
const ic = esc(a.name.slice(0, 1));
if (!a.url) return `<div class="app disabled" aria-disabled="true"><div class="ic">${ic}</div><strong>${esc(a.name)}</strong><small>Coming soon</small></div>`;
const priv = (u) => (u.private ? '<span class="priv" title="Private — opens only when you are signed in to the owning account">private</span>' : "");
const sec = a.secondary ? `<a class="sec" href="${esc(a.secondary.url)}" target="_blank" rel="noopener">${esc(a.secondary.label)} ↗</a>${priv(a.secondary)}` : "";
return `<div class="app"><a href="${esc(a.url)}" target="_blank" rel="noopener" style="text-decoration:none;color:inherit;display:block"><div class="ic">${ic}</div><strong>${esc(a.name)} ↗${priv(a)}</strong><small>${esc(a.description || "")}</small></a>${sec}</div>`;
}).join("");
}
async function loadTestCreditHint() { const r = await api("/healthz"); if (r.ok && !r.body.stripe) $("testCredit").hidden = false; }
function refresh() { loadBalance(); loadTxs(); loadStatus(); }
refresh(); loadApps(); loadTestCreditHint();