Improve decoder setup UI and add configurable training timers
This commit is contained in:
1 parent
32acc0e9c7
commit
a87c64a47c
8 files changed
+493
-57
No files matched your search
+105
-18
@@ -4,8 +4,8 @@ const requestedLanguage = new URLSearchParams(window.location.search).get("lang"
|
||||
const language = requestedLanguage === "en" || requestedLanguage === "sv"
|
||||
? requestedLanguage
|
||||
: String(navigator.language || "sv").toLowerCase().startsWith("sv") ? "sv" : "en";
|
||||
const TIMER_DURATION_MS = 20 * 60 * 1000;
|
||||
const TIMER_PHASE_MS = 10 * 60 * 1000;
|
||||
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") || "";
|
||||
|
||||
const messages = {
|
||||
@@ -22,15 +22,20 @@ const messages = {
|
||||
nextLevel: "Nästa nivå",
|
||||
reachLaps: "Nå {laps} varv",
|
||||
lapsRemaining: "{laps} varv kvar",
|
||||
plan: "Dagens 20-minuterspass",
|
||||
plan: "Dagens träningspass",
|
||||
timerSetup: "Ställ in passet",
|
||||
pushMinutes: "Maxfokus (min)",
|
||||
controlMinutes: "Kontrollerad (min)",
|
||||
timerSetupHint: "Minst ett block måste vara 1 minut. Tiderna sparas för den här föraren och klassen.",
|
||||
timerLockedHint: "Nollställ eller slutför passet för att ändra tiderna.",
|
||||
sessionStats: "Det här passet",
|
||||
sessionLaps: "Varv",
|
||||
baselineTitle: "Bygg en baslinje",
|
||||
baselineBody: "Kör lugnt och jämnt tills du har minst 10 rena varv. Upprepa samma linje och bromspunkt.",
|
||||
consistencyTitle: "Höj din lägstanivå",
|
||||
consistencyBody: "Kör 10 minuter nära max, sedan 10 minuter på 80-90 procent med fokus på rena, repeterbara varv.",
|
||||
consistencyBody: "Kör första blocket nära max, sedan ett kontrollerat block på 80-90 procent med fokus på rena, repeterbara varv.",
|
||||
progressionTitle: "Balansera fart och kontroll",
|
||||
progressionBody: "Kör 10 minuter med maxfokus och avsluta med 10 kontrollerade minuter utan misstag.",
|
||||
progressionBody: "Kör ett block med maxfokus och avsluta med ett kontrollerat block utan misstag.",
|
||||
target: "Kontrollmål {lap}",
|
||||
ready: "Redo att börja",
|
||||
push: "Maxfokus: hitta gränsen",
|
||||
@@ -76,15 +81,20 @@ const messages = {
|
||||
nextLevel: "Next level",
|
||||
reachLaps: "Reach {laps} laps",
|
||||
lapsRemaining: "{laps} laps remaining",
|
||||
plan: "Today's 20-minute session",
|
||||
plan: "Today's practice session",
|
||||
timerSetup: "Set session duration",
|
||||
pushMinutes: "Maximum focus (min)",
|
||||
controlMinutes: "Controlled (min)",
|
||||
timerSetupHint: "At least one block must be 1 minute. Times are saved for this driver and class.",
|
||||
timerLockedHint: "Reset or finish the session to change the times.",
|
||||
sessionStats: "This session",
|
||||
sessionLaps: "Laps",
|
||||
baselineTitle: "Build a baseline",
|
||||
baselineBody: "Drive calmly and consistently until at least 10 clean laps are recorded. Repeat the same line and braking point.",
|
||||
consistencyTitle: "Raise your minimum level",
|
||||
consistencyBody: "Run 10 minutes near maximum pace, then 10 minutes at 80-90 percent with clean, repeatable laps.",
|
||||
consistencyBody: "Run the first block near maximum pace, then a controlled block at 80-90 percent with clean, repeatable laps.",
|
||||
progressionTitle: "Balance pace and control",
|
||||
progressionBody: "Run 10 minutes at maximum focus and finish with 10 controlled minutes without mistakes.",
|
||||
progressionBody: "Run one block at maximum focus and finish with a controlled block without mistakes.",
|
||||
target: "Control target {lap}",
|
||||
ready: "Ready to start",
|
||||
push: "Maximum focus: find the limit",
|
||||
@@ -181,6 +191,10 @@ function renderProfile(data) {
|
||||
const goals = activeClass?.goals || data.goals || [];
|
||||
const currentSession = activeClass?.currentSession || data.currentSession;
|
||||
const recommendationText = recommendationCopy(recommendation);
|
||||
const timerState = readTimerState();
|
||||
const timerConfig = getTimerConfig(timerState);
|
||||
const timerDurationMs = getTimerDurationMs(timerConfig);
|
||||
const timerLocked = Boolean(timerState?.startedAt && getTimerElapsed(timerState) < timerDurationMs);
|
||||
const rank = activeClass?.clubRank && activeClass?.rankedDrivers ? t("rank", { rank: activeClass.clubRank, count: activeClass.rankedDrivers }) : "";
|
||||
const logo = /^data:image\/(?:png|jpeg|webp);base64,/i.test(branding.logoDataUrl || "")
|
||||
? `<img src="${escapeHtml(branding.logoDataUrl)}" alt="" />`
|
||||
@@ -258,7 +272,13 @@ function renderProfile(data) {
|
||||
<span class="profile-mini-label">${recommendation.targetLapMs ? escapeHtml(t("target", { lap: formatLap(recommendation.targetLapMs) })) : ""}</span>
|
||||
<h3>${escapeHtml(recommendationText.title)}</h3>
|
||||
<p class="profile-plan-copy">${escapeHtml(recommendationText.body)}</p>
|
||||
<div id="profileTimer" class="profile-timer">20:00</div>
|
||||
<fieldset id="profileTimerSetup" class="profile-timer-setup" ${timerLocked ? "disabled" : ""}>
|
||||
<legend>${escapeHtml(t("timerSetup"))}</legend>
|
||||
<label><span>${escapeHtml(t("pushMinutes"))}</span><input id="profilePushMinutes" type="number" min="0" max="${MAX_TIMER_BLOCK_MINUTES}" step="1" value="${timerConfig.pushMinutes}" /></label>
|
||||
<label><span>${escapeHtml(t("controlMinutes"))}</span><input id="profileControlMinutes" type="number" min="0" max="${MAX_TIMER_BLOCK_MINUTES}" step="1" value="${timerConfig.controlMinutes}" /></label>
|
||||
<small id="profileTimerSetupHint">${escapeHtml(t(timerLocked ? "timerLockedHint" : "timerSetupHint"))}</small>
|
||||
</fieldset>
|
||||
<div id="profileTimer" class="profile-timer">${formatTimer(timerDurationMs)}</div>
|
||||
<div id="profileTimerPhase" class="profile-timer-phase">${escapeHtml(t("ready"))}</div>
|
||||
${currentSession ? `
|
||||
<div class="profile-session-heading">${escapeHtml(t("sessionStats"))}</div>
|
||||
@@ -334,6 +354,38 @@ function readTimerState() {
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeTimerConfig(value) {
|
||||
const clampMinutes = (minutes) => Math.max(0, Math.min(MAX_TIMER_BLOCK_MINUTES, Math.round(Number(minutes) || 0)));
|
||||
const config = {
|
||||
pushMinutes: clampMinutes(value?.pushMinutes),
|
||||
controlMinutes: clampMinutes(value?.controlMinutes),
|
||||
};
|
||||
return config.pushMinutes + config.controlMinutes > 0 ? config : { ...DEFAULT_TIMER_CONFIG };
|
||||
}
|
||||
|
||||
function readTimerConfig() {
|
||||
try {
|
||||
return normalizeTimerConfig(JSON.parse(localStorage.getItem(getTimerConfigStorageKey()) || "null") || DEFAULT_TIMER_CONFIG);
|
||||
} catch {
|
||||
return { ...DEFAULT_TIMER_CONFIG };
|
||||
}
|
||||
}
|
||||
|
||||
function writeTimerConfig(value) {
|
||||
const config = normalizeTimerConfig(value);
|
||||
localStorage.setItem(getTimerConfigStorageKey(), JSON.stringify(config));
|
||||
return config;
|
||||
}
|
||||
|
||||
function getTimerConfig(state = null) {
|
||||
if (state && (state.pushMinutes != null || state.controlMinutes != null)) return normalizeTimerConfig(state);
|
||||
return readTimerConfig();
|
||||
}
|
||||
|
||||
function getTimerDurationMs(config) {
|
||||
return (Number(config.pushMinutes || 0) + Number(config.controlMinutes || 0)) * 60 * 1000;
|
||||
}
|
||||
|
||||
function writeTimerState(value) {
|
||||
if (value) localStorage.setItem(getTimerStorageKey(), JSON.stringify(value));
|
||||
else localStorage.removeItem(getTimerStorageKey());
|
||||
@@ -343,6 +395,10 @@ function getTimerStorageKey() {
|
||||
return `rc-training-timer:${shareToken}:${selectedClassId || "default"}`;
|
||||
}
|
||||
|
||||
function getTimerConfigStorageKey() {
|
||||
return `rc-training-timer-config:${shareToken}:${selectedClassId || "default"}`;
|
||||
}
|
||||
|
||||
function getTimerElapsed(state, now = Date.now()) {
|
||||
if (!state?.startedAt) return 0;
|
||||
const endpoint = state.pausedAt || now;
|
||||
@@ -350,15 +406,33 @@ function getTimerElapsed(state, now = Date.now()) {
|
||||
}
|
||||
|
||||
function bindTimerControls() {
|
||||
const saveTimerSetup = () => {
|
||||
const current = readTimerState();
|
||||
if (current?.startedAt && getTimerElapsed(current) < getTimerDurationMs(getTimerConfig(current))) return;
|
||||
if (current?.startedAt) writeTimerState(null);
|
||||
const config = writeTimerConfig({
|
||||
pushMinutes: document.getElementById("profilePushMinutes")?.value,
|
||||
controlMinutes: document.getElementById("profileControlMinutes")?.value,
|
||||
});
|
||||
const pushInput = document.getElementById("profilePushMinutes");
|
||||
const controlInput = document.getElementById("profileControlMinutes");
|
||||
if (pushInput) pushInput.value = String(config.pushMinutes);
|
||||
if (controlInput) controlInput.value = String(config.controlMinutes);
|
||||
updateTimerDisplay();
|
||||
};
|
||||
document.getElementById("profilePushMinutes")?.addEventListener("change", saveTimerSetup);
|
||||
document.getElementById("profileControlMinutes")?.addEventListener("change", saveTimerSetup);
|
||||
document.getElementById("profileTimerStart")?.addEventListener("click", () => {
|
||||
const current = readTimerState();
|
||||
const currentDurationMs = getTimerDurationMs(getTimerConfig(current));
|
||||
if (current?.pausedAt) {
|
||||
current.pausedTotalMs = Number(current.pausedTotalMs || 0) + Date.now() - current.pausedAt;
|
||||
current.pausedAt = 0;
|
||||
writeTimerState(current);
|
||||
} else if (!current || getTimerElapsed(current) >= TIMER_DURATION_MS) {
|
||||
writeTimerState({ startedAt: Date.now(), pausedAt: 0, pausedTotalMs: 0 });
|
||||
lastTimerPhase = "push";
|
||||
} else if (!current || getTimerElapsed(current) >= currentDurationMs) {
|
||||
const config = readTimerConfig();
|
||||
writeTimerState({ startedAt: Date.now(), pausedAt: 0, pausedTotalMs: 0, ...config });
|
||||
lastTimerPhase = config.pushMinutes > 0 ? "push" : "control";
|
||||
void loadProfile();
|
||||
}
|
||||
startTimerLoop();
|
||||
@@ -366,7 +440,7 @@ function bindTimerControls() {
|
||||
});
|
||||
document.getElementById("profileTimerPause")?.addEventListener("click", () => {
|
||||
const current = readTimerState();
|
||||
if (current?.startedAt && !current.pausedAt && getTimerElapsed(current) < TIMER_DURATION_MS) {
|
||||
if (current?.startedAt && !current.pausedAt && getTimerElapsed(current) < getTimerDurationMs(getTimerConfig(current))) {
|
||||
current.pausedAt = Date.now();
|
||||
writeTimerState(current);
|
||||
}
|
||||
@@ -391,18 +465,31 @@ function updateTimerDisplay() {
|
||||
const phase = document.getElementById("profileTimerPhase");
|
||||
const startButton = document.getElementById("profileTimerStart");
|
||||
const pauseButton = document.getElementById("profileTimerPause");
|
||||
const timerSetup = document.getElementById("profileTimerSetup");
|
||||
const timerSetupHint = document.getElementById("profileTimerSetupHint");
|
||||
if (!timer || !phase || !startButton || !pauseButton) return;
|
||||
const state = readTimerState();
|
||||
const config = getTimerConfig(state);
|
||||
const timerDurationMs = getTimerDurationMs(config);
|
||||
const pushDurationMs = Number(config.pushMinutes || 0) * 60 * 1000;
|
||||
const elapsed = getTimerElapsed(state);
|
||||
const remaining = Math.max(0, TIMER_DURATION_MS - elapsed);
|
||||
const phaseName = !state?.startedAt ? "ready" : remaining <= 0 ? "finished" : elapsed < TIMER_PHASE_MS ? "push" : "control";
|
||||
timer.textContent = formatTimer(remaining || (phaseName === "ready" ? TIMER_DURATION_MS : 0));
|
||||
const remaining = Math.max(0, timerDurationMs - elapsed);
|
||||
const phaseName = !state?.startedAt
|
||||
? "ready"
|
||||
: remaining <= 0
|
||||
? "finished"
|
||||
: config.pushMinutes > 0 && elapsed < pushDurationMs
|
||||
? "push"
|
||||
: "control";
|
||||
timer.textContent = formatTimer(remaining || (phaseName === "ready" ? timerDurationMs : 0));
|
||||
phase.textContent = t(phaseName);
|
||||
startButton.textContent = state?.pausedAt ? t("resume") : !state?.startedAt || remaining <= 0 ? t("start") : t("running");
|
||||
startButton.disabled = Boolean(state?.startedAt && !state.pausedAt && remaining > 0);
|
||||
pauseButton.disabled = !state?.startedAt || Boolean(state.pausedAt) || remaining <= 0;
|
||||
if (timerSetup) timerSetup.disabled = Boolean(state?.startedAt && remaining > 0);
|
||||
if (timerSetupHint) timerSetupHint.textContent = t(state?.startedAt && remaining > 0 ? "timerLockedHint" : "timerSetupHint");
|
||||
if (state?.startedAt && !state.pausedAt && phaseName !== lastTimerPhase) {
|
||||
if (lastTimerPhase === "push" && phaseName === "control") speak(t("switchCue"));
|
||||
if (lastTimerPhase === "push" && phaseName === "control" && config.controlMinutes > 0) speak(t("switchCue"));
|
||||
if (phaseName === "finished") speak(t("finishCue"));
|
||||
lastTimerPhase = phaseName;
|
||||
}
|
||||
@@ -424,7 +511,7 @@ async function loadProfile() {
|
||||
const now = Date.now();
|
||||
const startedAgoMs = Math.max(0, now - timerState.startedAt);
|
||||
const currentPauseMs = timerState.pausedAt ? Math.max(0, now - timerState.pausedAt) : 0;
|
||||
const maximumWindowMs = TIMER_DURATION_MS + Number(timerState.pausedTotalMs || 0) + currentPauseMs;
|
||||
const maximumWindowMs = getTimerDurationMs(getTimerConfig(timerState)) + Number(timerState.pausedTotalMs || 0) + currentPauseMs;
|
||||
apiUrl.searchParams.set("startedAgoMs", String(startedAgoMs));
|
||||
apiUrl.searchParams.set("windowMs", String(Math.min(startedAgoMs, maximumWindowMs)));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user