feat(layouts): switch track layout mid-practice and view stats per layout

A club can run one Open Practice all season and re-lay the track now and then.
Previously a session's layout was frozen at start and every training lap inherited
it, so a season-long practice stayed on its first layout forever, and changing layout
was hidden under Settings -> Appearance.

- Each practice lap now counts toward the layout active when it was driven; a running
  practice session moves onto a new layout when one is added
- "New layout" strip on Training stats, Race Day and the Dashboard, offered only around
  practice; a running race session blocks layout changes everywhere
- "Stats for" selector (current / older layout / before layouts / all layouts) on
  Training stats and in the driver's mobile profile (?layout= on the public API)
- A layout that has practice laps on it can no longer be deleted
- Fix: the buildDriverTrainingStats wrapper in app.js dropped extra arguments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
larssandandClaude Opus 5.5 committed 2026-09-27 21:24:44 +02:00
1 parent 196437d25b
commit 627179b95a
22 files changed
+454 -44

No files matched your search

+34
View File
@@ -7,6 +7,7 @@ const language = requestedLanguage === "en" || requestedLanguage === "sv"
const DEFAULT_TIMER_CONFIG = Object.freeze({ pushMinutes: 10, controlMinutes: 10 });
const MAX_TIMER_BLOCK_MINUTES = 120;
let selectedClassId = new URLSearchParams(window.location.search).get("class") || "";
let selectedLayout = new URLSearchParams(window.location.search).get("layout") || "current";
const messages = {
sv: {
@@ -21,6 +22,10 @@ const messages = {
loadError: "Kunde inte läsa träningsprofilen. Länken kan vara avstängd eller ersatt.",
development: "Förarutveckling",
class: "Klass",
layoutStats: "Banlayout",
layoutCurrent: "Aktiv layout",
layoutNone: "Utan layout (före första layouten)",
layoutAll: "Alla layouter",
laps: "Totala varv",
days: "Träningsdagar",
best: "Bästa varv",
@@ -187,6 +192,10 @@ const messages = {
loadError: "The training profile could not be loaded. The link may be disabled or replaced.",
development: "Driver development",
class: "Class",
layoutStats: "Track layout",
layoutCurrent: "Current layout",
layoutNone: "No layout (before the first layout)",
layoutAll: "All layouts",
laps: "Total laps",
days: "Practice days",
best: "Best lap",
@@ -637,6 +646,7 @@ function renderProfile(data) {
<div class="profile-meta">
${classProfiles.length > 1 ? `<label class="profile-class-select"><span>${escapeHtml(t("class"))}</span><select id="profileClass">${classOptions}</select></label>` : activeClass?.className ? `<span class="profile-pill">${escapeHtml(activeClass.className)}</span>` : ""}
${activeClass?.brand ? `<span class="profile-pill">${escapeHtml(activeClass.brand)}</span>` : ""}
${renderLayoutSelect(data.trackLayouts)}
</div>
</section>
@@ -924,6 +934,14 @@ function renderProfile(data) {
if (button) button.disabled = false;
}
});
document.getElementById("profileLayout")?.addEventListener("change", (event) => {
selectedLayout = String(event.currentTarget.value || "current");
const nextUrl = new URL(window.location.href);
if (selectedLayout !== "current") nextUrl.searchParams.set("layout", selectedLayout);
else nextUrl.searchParams.delete("layout");
window.history.replaceState({}, "", nextUrl);
loadProfile();
});
document.getElementById("profileClass")?.addEventListener("change", (event) => {
selectedClassId = String(event.currentTarget.value || "");
const nextUrl = new URL(window.location.href);
@@ -1275,6 +1293,21 @@ function updateProfileConnectionStatus() {
retry.disabled = profileConnection.loading;
}
// Only shown once the club has set up track layouts. Pace/best/consistency follow the
// choice; lifetime totals always cover every lap.
function renderLayoutSelect(trackLayouts) {
const options = Array.isArray(trackLayouts?.options) ? trackLayouts.options : [];
if (!options.length) return "";
const selected = trackLayouts.selected || "current";
const option = (value, label) => `<option value="${escapeHtml(value)}" ${selected === value ? "selected" : ""}>${escapeHtml(label)}</option>`;
return `<label class="profile-class-select"><span>${escapeHtml(t("layoutStats"))}</span><select id="profileLayout">
${option("current", t("layoutCurrent"))}
${options.slice().reverse().map((item) => option(item.id, item.label)).join("")}
${trackLayouts.hasUntaggedLaps ? option("none", t("layoutNone")) : ""}
${option("all", t("layoutAll"))}
</select></label>`;
}
async function loadProfile(background = false) {
if (profileConnection.loading || (background && profileIsBeingEdited())) return;
profileConnection.loading = true;
@@ -1296,6 +1329,7 @@ async function loadProfile(background = false) {
if (evenLapsRun) apiUrl.searchParams.set("evenLapsStartedAgoMs", String(Math.max(0, Date.now() - evenLapsRun.startedAt)));
const stepTrainingRun = readExerciseRunState("steptraining");
if (stepTrainingRun) apiUrl.searchParams.set("stepTrainingStartedAgoMs", String(Math.max(0, Date.now() - stepTrainingRun.startedAt)));
if (selectedLayout !== "current") apiUrl.searchParams.set("layout", selectedLayout);
const response = await fetch(apiUrl, { cache: "no-store", signal: controller.signal });
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();