Files
Live_RC/src/houseofrc_view.js
T

78 lines
4.1 KiB
JavaScript

// 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 = `<p class="error">${escapeHtml(t("houseofrc.err_unreachable"))}</p>`;
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 = `
<p><strong id="houseOfRcState">${escapeHtml(stateText)}</strong></p>
<p class="hint">${escapeHtml(t("houseofrc.hint"))}</p>
<form id="houseOfRcKeyForm" class="form-grid cols-2">
<label>${escapeHtml(t("houseofrc.key"))}
<input type="password" name="apiKey" id="houseOfRcKey" autocomplete="off" spellcheck="false" placeholder="hrc_…" ${status.source === "env" ? "disabled" : ""} />
</label>
<div class="actions">
<button class="btn btn-primary" type="submit" ${status.source === "env" ? "disabled" : ""}>${escapeHtml(t("houseofrc.save"))}</button>
<button class="btn" type="button" id="houseOfRcTest" ${status.configured ? "" : "disabled"}>${escapeHtml(t("houseofrc.test"))}</button>
${status.source === "file" ? `<button class="btn btn-danger" type="button" id="houseOfRcRemove">${escapeHtml(t("houseofrc.remove"))}</button>` : ""}
</div>
</form>
<p class="hint" id="houseOfRcResult" role="status" hidden></p>`;
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();
});
}