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

+23 -10
View File
@@ -236,7 +236,10 @@ export function syncTrainingLedger(training, state, { uid }) {
timestamp,
lapMs,
recordedAt: timestamp ? new Date(timestamp).toISOString() : new Date().toISOString(),
trackLayoutId: String(session?.trackLayoutId || "").trim(),
// The layout active when this lap was driven, not when the session started: a club
// can run one Open Practice all season and switch layout mid-session, and laps on
// either side of the switch must land on their own layout.
trackLayoutId: timestamp ? getTrackLayoutIdAt(state?.trackLayouts, timestamp) : String(session?.trackLayoutId || "").trim(),
});
});
});
@@ -278,9 +281,18 @@ export function getDriverTrainingLaps(state, driverId, classId = "") {
// layout-scoped subset instead. With no track layouts configured, every lap's
// trackLayoutId is "" and getTrackLayoutIdAt() also returns "" - so this returns exactly
// the same laps as getDriverTrainingLaps, and the feature stays fully inert until used.
// `layout` picks which laps count: "current" (default, the active layout), "all" (every
// layout blended - the overall view), "none" (laps driven before any layout existed), or
// a specific layout id.
export function getDriverTrainingLapsForLayout(state, driverId, classId = "", layout = "current") {
const laps = getDriverTrainingLaps(state, driverId, classId);
if (layout === "all") return laps;
const layoutId = layout === "current" || !layout ? getTrackLayoutIdAt(state?.trackLayouts) : layout === "none" ? "" : String(layout);
return laps.filter((lap) => lap.trackLayoutId === layoutId);
}
export function getDriverTrainingLapsForCurrentLayout(state, driverId, classId = "") {
const currentLayoutId = getTrackLayoutIdAt(state?.trackLayouts);
return getDriverTrainingLaps(state, driverId, classId).filter((lap) => lap.trackLayoutId === currentLayoutId);
return getDriverTrainingLapsForLayout(state, driverId, classId, "current");
}
function percentile(values, ratio) {
@@ -433,10 +445,10 @@ function getTrainingStintMetrics(stint) {
};
}
export function buildTrainingStintAnalysis(state, driverId, classId = "") {
// Scoped to the currently active track layout, same as buildDriverTrainingStats - a
// "best stint" pick spanning a track that no longer exists would not be comparable.
const stints = splitTrainingStints(getDriverTrainingLapsForCurrentLayout(state, driverId, classId));
export function buildTrainingStintAnalysis(state, driverId, classId = "", { layout = "current" } = {}) {
// Scoped to one track layout (the active one unless another is chosen), same as
// buildDriverTrainingStats - a "best stint" spanning two tracks would not be comparable.
const stints = splitTrainingStints(getDriverTrainingLapsForLayout(state, driverId, classId, layout));
const rows = stints.map((stint, index) => {
const { laps, values, outlierLaps, startPaceMs, endPaceMs, endChangeMs, strongFinish } = getTrainingStintMetrics(stint);
return {
@@ -655,14 +667,15 @@ export function getBestTrainingDurationResult(stints, targetMs = 5 * 60 * 1000)
return best;
}
export function buildDriverTrainingStats(state, driverId, classId = "") {
export function buildDriverTrainingStats(state, driverId, classId = "", { layout = "current" } = {}) {
// Lifetime commitment stats (how much a driver has trained, ever) read the full lap
// history - a track rebuild doesn't erase past practice. Every pace/record/consistency
// stat below instead reads layoutLaps, scoped to whichever track layout is active right
// now, so a lap from a track that no longer exists can't be reported as today's "record"
// or blend into today's consistency numbers. See getDriverTrainingLapsForCurrentLayout.
// or blend into today's consistency numbers. The `layout` option picks another layout,
// or "all" for the overall view - see getDriverTrainingLapsForLayout.
const laps = getDriverTrainingLaps(state, driverId, classId);
const layoutLaps = getDriverTrainingLapsForCurrentLayout(state, driverId, classId);
const layoutLaps = getDriverTrainingLapsForLayout(state, driverId, classId, layout);
const historyPaceLaps = laps.filter((lap) => !lap.excludedFromPace);
const paceLaps = layoutLaps.filter((lap) => !lap.excludedFromPace);
const lapTimes = paceLaps.map((lap) => lap.lapMs);