// ── Telegram Web App init ───────────────────────────────────────
const tg = window.Telegram.WebApp;
tg.ready();
tg.expand();
const API = window.location.origin + "/api";
const userId = tg.initDataUnsafe?.user?.id;
if (!userId) {
document.getElementById("app").innerHTML =
'
Superset
';
} else {
groupsHtml += '
';
}
group.forEach((ex) => {
const machine = ex.machine_id ? `
(${ex.machine_id})` : "";
const details = ex.sets_detail || [];
let detailStr;
if (details.length > 0 && !details.every(
(d) => d.reps === details[0].reps && d.weight_kg === details[0].weight_kg
)) {
detailStr = details.map((d) =>
d.weight_kg ? `${d.reps}x${fmtWeight(d.weight_kg)}kg` : `${d.reps}`
).join(", ");
} else {
const w = ex.weight_kg;
detailStr = w ? `${ex.sets}x${ex.reps}x${fmtWeight(w)}kg` : `${ex.sets}x${ex.reps}`;
}
groupsHtml += `
${ex.name}${machine}
— ${detailStr}
`;
});
groupsHtml += "
";
});
card.innerHTML = `
${groupsHtml}
`;
card.querySelector(".btn-history-edit").addEventListener("click", (e) => {
e.stopPropagation();
editSavedWorkout(w);
});
container.appendChild(card);
});
historyOffset += data.workouts.length;
loadMore.style.display = historyOffset < data.total ? "block" : "none";
} catch (e) {
console.error("Failed to load history", e);
}
}
document.getElementById("btn-load-more").addEventListener("click", () => {
loadHistory(true);
});
// ── Stats View ──────────────────────────────────────────────────
async function loadStats() {
try {
const data = await api("GET", "/stats");
const container = document.getElementById("stats-content");
if (data.total_workouts === 0) {
container.innerHTML = '
';
return;
}
container.innerHTML = `
${data.total_workouts}
Workouts
${data.unique_exercises}
Exercises
${data.total_sets.toLocaleString()}
Total Sets
${Math.round(data.total_volume).toLocaleString()}
Volume (kg)
`;
} catch (e) {
console.error("Failed to load stats", e);
}
}
// ── Helpers ─────────────────────────────────────────────────────
function fmtWeight(w) {
return w === Math.floor(w) ? Math.floor(w).toString() : w.toString();
}
// ── Init ────────────────────────────────────────────────────────
async function init() {
if (!userId) return;
try {
const data = await api("GET", "/exercises");
knownExercises = data.exercises || [];
} catch (e) {
console.error("Failed to load exercises", e);
}
restoreDraft();
}
init();