shortstats/src/shortstats/web/static/js/videos.js
ploush 81ef12a9cd
Some checks failed
build / test (push) Failing after 2m48s
build / images (collector) (push) Has been skipped
build / images (web) (push) Has been skipped
upgrade 1.4.0
2026-11-29 20:43:50 +01:00

70 lines
3.4 KiB
JavaScript

// Page Vidéos : filtres appliqués dès le choix, détail des étapes (relevées ou
// complétées) chargé à l'ouverture (tableau construit avec textContent uniquement).
import { NF } from "./util.js";
const form = document.querySelector(".video-filters");
if (form) {
form.querySelectorAll("select").forEach((s) => s.addEventListener("change", () => form.submit()));
}
const PF = new Intl.NumberFormat("fr-FR", { style: "percent", maximumFractionDigits: 1 });
const fmt = (v) => (v === null || v === undefined ? "" : NF.format(v));
const age = (h) => (h < 48 ? `${Math.round(h * 10) / 10} h` : h < 24 * 14 ? `${Math.round(h / 24 * 10) / 10} j` : `${Math.round(h / 24)} j`);
async function load(details) {
const body = details.querySelector(".snapshots-body");
if (details.dataset.loaded) return;
details.dataset.loaded = "1";
body.textContent = "Chargement…";
try {
const res = await fetch(`/api/videos/${encodeURIComponent(details.dataset.video)}/snapshots`,
{ credentials: "same-origin", headers: { Accept: "application/json" } });
if (res.status === 401) { window.location.href = "/login"; return; }
if (!res.ok) throw new Error(`Erreur ${res.status}`);
const rows = await res.json();
const table = document.createElement("table");
table.className = "table table-compact snapshots-table";
const head = table.createTHead().insertRow();
for (const [text, num] of [["Étape", false], ["Relevé le", false], ["Âge réel", true], ["Vues", true],
["Likes", true], ["Comm.", true], ["Relevés", true], ["Thèmes", false]]) {
const th = document.createElement("th");
th.scope = "col";
th.textContent = text;
if (num) th.className = "num";
head.append(th);
}
const tbody = table.createTBody();
for (const r of rows) {
const tr = tbody.insertRow();
if (r.estimated) tr.className = "is-estimated";
const themes = r.themes.map(([name, n]) => `${name} ${fmt(n)}${r.comments_read ? ` (${PF.format(n / r.comments_read)})` : ""}`);
const when = r.estimated ? `complétée (${r.taken_at})` : r.taken_at;
for (const [value, num] of [[r.step, false], [when, false], [age(r.age_hours), true],
[fmt(r.views), true], [fmt(r.likes), true], [fmt(r.comments), true],
[r.comments_read === null ? "à relever" : fmt(r.comments_read), true],
[themes.join(" · "), false]]) {
const td = tr.insertCell();
td.textContent = value;
if (num) td.className = "num";
}
}
body.replaceChildren(table);
if (rows.some((r) => r.estimated)) {
const note = document.createElement("p");
note.className = "hint";
note.textContent = "Étape complétée : passée sans relevé (vues et likes inconnus) ; ses commentaires sont "
+ "comptés d'après leur date de publication, à l'heure théorique de l'étape (sortie + âge).";
body.append(note);
}
} catch (err) {
delete details.dataset.loaded;
body.textContent = `Impossible de charger les relevés : ${err.message}`;
}
}
// Délégation : la zone est remplacée lors des mises à jour en direct.
document.addEventListener("toggle", (event) => {
const d = event.target;
if (d instanceof HTMLDetailsElement && d.matches("details.snapshots") && d.open) load(d);
}, true);