File size: 6,127 Bytes
1444c0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | 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 ? '<span class="pill on">configured</span>' : '<span class="pill off">not set</span>';
const kept = s.persisted ? ' <span class="pill on">saved as Space secret</span>' : "";
let input;
if (s.key === "WALLET_STORE") input = `<select class="wide" id="f_${s.key}"><option value="memory"${s.value !== "supabase" ? " selected" : ""}>memory (resets on restart)</option><option value="supabase"${s.value === "supabase" ? " selected" : ""}>supabase (durable)</option></select>`;
else if (s.secret) input = `<input class="wide" id="f_${s.key}" type="password" autocomplete="new-password" spellcheck="false" placeholder="${s.configured ? "Current: " + esc(s.masked) + " — type to replace" : "Not set"}">`;
else input = `<input class="wide" id="f_${s.key}" type="url" spellcheck="false" value="${esc(s.value)}" placeholder="${s.key.startsWith("STRIPE") ? "Default: this app" : ""}">`;
const clr = s.configured && s.key !== "WALLET_STORE" ? `<label class="row" style="font-weight:400;margin:6px 0 0"><input type="checkbox" id="c_${s.key}" style="width:auto"> Remove this value</label>` : "";
return `<div class="field"><div class="row"><strong>${esc(s.label)}</strong>${pill}${kept}</div><div>${input}</div><p class="hint">${esc(s.hint)}</p><div class="msg err" id="e_${s.key}"></div>${clr}</div>`;
}
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 = `<div class="row"><span>Stripe:</span> ${by.STRIPE_SECRET_KEY.configured && by.STRIPE_WEBHOOK_SECRET.configured ? '<span class="pill on">ready</span>' : '<span class="pill off">incomplete</span>'}
<span>Storage:</span> <span class="pill ${st.storeMode === "supabase" ? "on" : "off"}">${esc(st.storeMode)}</span></div>
<p class="hint">${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."}</p>
<p class="hint">${esc(st.persistence.describe)}</p>`;
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 = '<div class="label">NOT AVAILABLE</div><p>The admin passcode is not configured on the server. Set <code>ADMIN_PASSCODE</code> as a Space secret, then reload.</p>'; 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();
|