feat: add activity heatmap by day

Aggregate last-played dates via GET /api/activity and render a calendar heatmap with a year selector in the library view.
This commit is contained in:
strsv committed 2026-11-07 00:55:11 +03:00
1 parent 54a7b36aae
commit 4566066f2b
10 files changed
+171 -1

No files matched your search

+49
View File
@@ -820,6 +820,55 @@ async function restoreBulk(items, clearHideUndo = false) {
}
}
function renderActivity(data) {
const byDate = new Map(data.days.map((day) => [day.date, day]));
const year = Number(data.year);
const start = new Date(Date.UTC(year, 0, 1));
const end = new Date(Date.UTC(year + 1, 0, 1));
const step = Math.max(1, Math.ceil(data.max_games / 4));
const cells = [];
const offset = (start.getUTCDay() + 6) % 7;
for (let i = 0; i < offset; i++) {
cells.push('<i class="activity-cell blank"></i>');
}
for (const day = new Date(start); day < end; day.setUTCDate(day.getUTCDate() + 1)) {
const date = day.toISOString().slice(0, 10);
const item = byDate.get(date);
const games = item ? item.games : 0;
const level = games ? Math.min(4, 1 + Math.floor((games - 1) / step)) : 0;
const title = games
? `${date}: игр ${games}, ${item.hours} ч`
: `${date}: запусков нет`;
cells.push(`<i class="activity-cell l${level}" title="${title}" aria-label="${title}"></i>`);
}
$("#activityHeatmap").innerHTML = cells.join("");
$("#activitySummary").textContent = `${data.total_games} игр · ${data.total_hours} ч`;
}
async function loadActivity(year = "") {
const params = new URLSearchParams();
if (year) params.set("year", year);
try {
const data = await api(`/api/activity${params.toString() ? `?${params}` : ""}`);
const select = $("#activityYear");
select.innerHTML = data.years.map((value) => `<option value="${value}">${value}</option>`).join("");
if (!data.years.includes(data.year)) {
select.insertAdjacentHTML("afterbegin", `<option value="${data.year}">${data.year}</option>`);
}
select.value = data.year;
renderActivity(data);
} catch (err) {
toast(err.message);
}
}
$("#activityToggle").addEventListener("click", () => {
const body = $("#activityBody");
body.hidden = !body.hidden;
if (!body.hidden) loadActivity($("#activityYear").value || "");
});
$("#activityYear").addEventListener("change", () => loadActivity($("#activityYear").value));
/* ---------- library ---------- */
function filterParams() {