// Settings → System → House of RC: save the club's API key on the server and test it. // The key is never read back; the server only tells whether one is set (and its last 4 chars). async function postJson(path, body) { try { const response = await fetch(path, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body || {}) }); return { status: response.status, body: await response.json().catch(() => ({})) }; } catch { return { status: 0, body: { error: "unreachable" } }; } } export function describeHouseOfRcError(t, code, body = {}) { if (code === 401 && body.error === "auth_required") return t("auth.login_required"); const key = { not_configured: "houseofrc.err_not_configured", unauthorized: "houseofrc.err_unauthorized", rate_limited: "houseofrc.err_rate_limited", timeout: "houseofrc.err_unreachable", unreachable: "houseofrc.err_unreachable", invalid_key: "houseofrc.err_invalid_key", }[body.error]; return key ? t(key, { seconds: body.retryAfter || 60 }) : t("houseofrc.err_other", { status: body.status || code }); } export async function mountHouseOfRcSettings(container, { t, escapeHtml }) { if (!container) return; const status = await fetch("/api/houseofrc/status").then((res) => res.json()).catch(() => null); if (!status) { container.innerHTML = `
${escapeHtml(t("houseofrc.err_unreachable"))}
`; return; } const remount = () => mountHouseOfRcSettings(container, { t, escapeHtml }); const stateText = !status.configured ? t("houseofrc.state_none") : status.source === "env" ? t("houseofrc.state_env", { hint: status.keyHint }) : t("houseofrc.state_saved", { hint: status.keyHint }); container.innerHTML = `${escapeHtml(stateText)}
${escapeHtml(t("houseofrc.hint"))}
`; const result = container.querySelector("#houseOfRcResult"); const show = (text, isError = false) => { result.textContent = text; result.className = isError ? "error" : "hint"; result.hidden = false; }; container.querySelector("#houseOfRcKeyForm").addEventListener("submit", async (event) => { event.preventDefault(); const apiKey = String(new FormData(event.currentTarget).get("apiKey") || "").trim(); if (!apiKey) return; const { status: code, body } = await postJson("/api/houseofrc/key", { apiKey }); if (code !== 200) return show(describeHouseOfRcError(t, code, body), true); await remount(); container.querySelector("#houseOfRcTest")?.click(); }); container.querySelector("#houseOfRcTest")?.addEventListener("click", async () => { show(t("houseofrc.testing")); const { status: code, body } = await postJson("/api/houseofrc/test"); if (code === 200 && body.ok) show(t("houseofrc.connected", { name: body.user?.name || "?", id: body.user?.id ?? "?", limit: body.rateLimit ?? "?" })); else show(describeHouseOfRcError(t, code, body), true); }); container.querySelector("#houseOfRcRemove")?.addEventListener("click", async () => { if (!window.confirm(t("houseofrc.remove_confirm"))) return; const { status: code, body } = await postJson("/api/houseofrc/key", { apiKey: "" }); if (code !== 200) return show(describeHouseOfRcError(t, code, body), true); remount(); }); }