let state = null; const STRIPE = ["STRIPE_SECRET_KEY", "STRIPE_WEBHOOK_SECRET", "STRIPE_SUCCESS_URL", "STRIPE_CANCEL_URL"]; const SUPA = ["SUPABASE_URL", "SUPABASE_SERVICE_ROLE_KEY", "WALLET_STORE"]; function fieldHtml(s) { const pill = s.configured ? 'configured' : 'not set'; const kept = s.persisted ? ' saved as Space secret' : ""; let input; if (s.key === "WALLET_STORE") input = ``; else if (s.secret) input = ``; else input = ``; const clr = s.configured && s.key !== "WALLET_STORE" ? `` : ""; return `
${esc(s.label)}${pill}${kept}
${input}

${esc(s.hint)}

${clr}
`; } function render(st) { state = st; const by = Object.fromEntries(st.settings.map((s) => [s.key, s])); $("stripeFields").innerHTML = STRIPE.map((k) => fieldHtml(by[k])).join(""); $("supaFields").innerHTML = SUPA.map((k) => fieldHtml(by[k])).join(""); if (!st.canSwitchStore) $("f_WALLET_STORE").disabled = true; $("whUrl").textContent = st.webhookUrl; $("summary").innerHTML = `
Stripe: ${by.STRIPE_SECRET_KEY.configured && by.STRIPE_WEBHOOK_SECRET.configured ? 'ready' : 'incomplete'} Storage: ${esc(st.storeMode)}

${st.storeMode === "memory" ? "Running in memory: balances and devices reset when the Space restarts. Add Supabase and switch storage to supabase to keep them." : "Balances and devices are stored in Supabase."}

${esc(st.persistence.describe)}

`; const p = $("persist"); p.disabled = !st.persistence.available; if (!st.persistence.available) p.checked = false; else if (p.dataset.init !== "1") { p.checked = true; p.dataset.init = "1"; } $("persistLabel").textContent = st.persistence.available ? "Also save as Space secrets so they survive restarts (the Space restarts, which resets memory-mode data)" : "Space-secret persistence unavailable — values are kept in memory only until restart"; } async function load() { const s = await api("/api/admin/session"); if (s.ok && !s.body.configured) { $("content").hidden = true; $("gate").hidden = false; $("gate").innerHTML = '
NOT AVAILABLE

The admin passcode is not configured on the server. Set ADMIN_PASSCODE as a Space secret, then reload.

'; return; } if (!(s.ok && s.body.authenticated)) { $("content").hidden = true; $("gate").hidden = false; return signInForm($("gate"), load, "Enter the admin passcode to manage Stripe and Supabase keys."); } const r = await api("/api/admin/settings"); if (!r.ok) { $("gate").hidden = false; $("gate").textContent = r.body.error || "Could not load settings"; return; } $("gate").hidden = true; $("content").hidden = false; render(r.body); } $("setForm").addEventListener("submit", async (e) => { e.preventDefault(); const m = $("saveMsg"); document.querySelectorAll('[id^="e_"]').forEach((el) => (el.textContent = "")); const values = {}; const clear = []; for (const s of state.settings) { const el = $("f_" + s.key); if (!el) continue; const c = $("c_" + s.key); if (c && c.checked) { clear.push(s.key); continue; } const v = el.value.trim(); if (s.key === "WALLET_STORE") { if (v !== state.storeMode) values[s.key] = v; } else if (s.secret) { if (v) values[s.key] = v; } else if (v !== (s.value || "")) { if (v) values[s.key] = v; else if (s.configured) clear.push(s.key); } } if (!Object.keys(values).length && !clear.length) return msg(m, "warn", "Nothing to save — no fields changed."); const r = await api("/api/admin/settings", { method: "PUT", body: { values, clear, persist: $("persist").checked } }); for (const k of STRIPE.concat(SUPA)) if ($("f_" + k) && $("f_" + k).type === "password") $("f_" + k).value = ""; // never keep secrets in the page if (r.status === 401) return load(); if (!r.ok) { for (const [k, v] of Object.entries(r.body.fields || {})) if ($("e_" + k)) $("e_" + k).textContent = v; return msg(m, "err", r.body.error || `Save failed (${r.status})`); } render(r.body); const bits = ["Saved ✓"]; if (r.body.persisted) bits.push("Stored as Hugging Face Space secrets; the Space is restarting and this page will reconnect in about a minute."); else if ($("persist").checked) bits.push("Not persisted: " + (r.body.persistError || "unknown error") + " Active in memory until the next restart."); else bits.push("Active now (in memory only; lost on restart)."); msg(m, r.body.persistError ? "warn" : "ok", bits.join(" ")); }); async function runTest(path, el, btn) { btn.disabled = true; msg(el, "", "Testing…"); const r = await api(path, { method: "POST", body: {} }); btn.disabled = false; if (r.status === 401) return load(); msg(el, r.ok && r.body.ok ? "ok" : "err", r.body.message || r.body.error || `Test failed (${r.status})`); } $("testStripe").addEventListener("click", () => runTest("/api/admin/test/stripe", $("stripeMsg"), $("testStripe"))); $("testSupa").addEventListener("click", () => runTest("/api/admin/test/supabase", $("supaMsg"), $("testSupa"))); load();