Results share link: read-only /r page with a revocable key, the admin ticks which closed questions it shows (sharing locks reopen like publish), refreshes every 15 s

This commit is contained in:
2026-11-19 01:11:05 +01:00
parent 41b4ac1a78
commit 98d8199ffd
7 changed files with 146 additions and 4 deletions
+43
View File
@@ -65,6 +65,7 @@ function render() {
renderSettings(p);
renderQuestions();
renderShare(p);
}
function renderSettings(p) {
@@ -134,6 +135,7 @@ function questionCard(q) {
const badges = [h("span", { class: `badge ${q.state}` }, STATE_LABEL[q.state])];
if (q.results_public) badges.push(h("span", { class: "badge public" }, "Publiés"));
if (q.shared && state.poll.share_key) badges.push(h("span", { class: "badge public" }, "Partagés"));
if (focused) badges.push(h("span", { class: "focus-mark" }, icon("screen", 14), "À l'écran"));
if (q.kind === "multi") badges.push(h("span", { class: "badge" }, `${q.max_choices} choix max`));
@@ -306,6 +308,47 @@ $("#export").addEventListener("click", async () => {
} catch (e) { toast(errMsg(e), "bad"); }
});
// read-only results link, the admin ticks which closed questions it shows
const shareUrl = (k) => `${location.origin}/r/${pid}#${k}`;
function renderShare(p) {
const btn = (label, onclick, cls = "small") => h("button", { class: cls, type: "button", onclick }, label);
const rotate = async (msg) => {
if (msg && !confirm(msg)) return;
try { const r = await call("POST", "/share-key"); await copy(shareUrl(r.share_key), "Lien des résultats"); }
catch (e) { toast(errMsg(e), "bad"); }
};
const off = async () => {
if (!confirm("Désactiver le lien des résultats ?")) return;
try { await call("DELETE", "/share-key"); } catch (e) { toast(errMsg(e), "bad"); }
};
const closed = state.questions.filter((q) => q.state === "closed").sort((a, b) => a.id - b.id);
const n = closed.filter((q) => q.shared).length;
$("#share-summary").textContent = p.share_key ? `${n} question${n > 1 ? "s" : ""} visible${n > 1 ? "s" : ""}` : "Lecture seule";
$("#share-actions").replaceChildren(...(p.share_key
? [btn("Copier", () => copy(shareUrl(p.share_key), "Lien des résultats")),
btn("Nouveau lien", () => rotate("Créer un nouveau lien ? L'ancien cessera de fonctionner.")),
btn("Désactiver", off, "small danger")]
: [btn("Créer", () => rotate())]));
if (!p.share_key) return $("#share-qs").replaceChildren();
$("#share-qs").replaceChildren(...(closed.length
? closed.map((q) => h("label", { class: "share-q" },
h("input", {
type: "checkbox", checked: q.shared,
onchange: async (e) => {
const on = e.target.checked;
if (on && !q.shown && !confirm("Partager ces résultats ? Le vote ne pourra plus être rouvert.")) {
e.target.checked = false;
return;
}
try { await call("POST", `/questions/${q.id}/${on ? "share" : "unshare"}`); }
catch (err) { e.target.checked = !on; toast(errMsg(err), "bad"); }
},
}), h("span", {}, q.text)))
: [h("p", { class: "muted share-none" }, "Aucune question close.")]));
}
// text export, built in the browser from the closed tallies the socket already sends
const exDlg = $("#export-dlg");