Add class-specific driver training profiles and metadata
This commit is contained in:
1 parent
3aeb77a042
commit
6f02e955a5
14 files changed
+559
-128
No files matched your search
+34
-8
@@ -6,13 +6,14 @@ const language = requestedLanguage === "en" || requestedLanguage === "sv"
|
||||
: String(navigator.language || "sv").toLowerCase().startsWith("sv") ? "sv" : "en";
|
||||
const TIMER_DURATION_MS = 20 * 60 * 1000;
|
||||
const TIMER_PHASE_MS = 10 * 60 * 1000;
|
||||
const TIMER_STORAGE_KEY = `rc-training-timer:${shareToken}`;
|
||||
let selectedClassId = new URLSearchParams(window.location.search).get("class") || "";
|
||||
|
||||
const messages = {
|
||||
sv: {
|
||||
loading: "Laddar träningsprofil...",
|
||||
loadError: "Kunde inte läsa träningsprofilen. Länken kan vara avstängd eller ersatt.",
|
||||
development: "Förarutveckling",
|
||||
class: "Klass",
|
||||
rank: "Klubbrank {rank} av {count}",
|
||||
laps: "Totala varv",
|
||||
days: "Träningsdagar",
|
||||
@@ -62,6 +63,7 @@ const messages = {
|
||||
loading: "Loading training profile...",
|
||||
loadError: "The training profile could not be loaded. The link may be disabled or replaced.",
|
||||
development: "Driver development",
|
||||
class: "Class",
|
||||
rank: "Club rank {rank} of {count}",
|
||||
laps: "Total laps",
|
||||
days: "Practice days",
|
||||
@@ -160,9 +162,17 @@ function recommendationCopy(recommendation) {
|
||||
|
||||
function renderProfile(data) {
|
||||
profileData = data;
|
||||
const { profile, branding, stats, nextMilestone, recommendation, goals, currentSession } = data;
|
||||
const { profile, branding } = data;
|
||||
const classProfiles = Array.isArray(data.classProfiles) ? data.classProfiles : [];
|
||||
let activeClass = classProfiles.find((item) => String(item.classId || "") === selectedClassId) || classProfiles[0] || null;
|
||||
if (activeClass && selectedClassId !== activeClass.classId) selectedClassId = activeClass.classId;
|
||||
const stats = activeClass?.stats || data.stats;
|
||||
const nextMilestone = activeClass?.nextMilestone || data.nextMilestone;
|
||||
const recommendation = activeClass?.recommendation || data.recommendation;
|
||||
const goals = activeClass?.goals || data.goals || [];
|
||||
const currentSession = activeClass?.currentSession || data.currentSession;
|
||||
const recommendationText = recommendationCopy(recommendation);
|
||||
const rank = profile.clubRank && profile.rankedDrivers ? t("rank", { rank: profile.clubRank, count: profile.rankedDrivers }) : "";
|
||||
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="" />`
|
||||
: `<div class="profile-brand-mark">RC</div>`;
|
||||
@@ -189,6 +199,9 @@ function renderProfile(data) {
|
||||
</tr>
|
||||
`).join("")
|
||||
: `<tr><td colspan="4">-</td></tr>`;
|
||||
const classOptions = classProfiles.map((item) => (
|
||||
`<option value="${escapeHtml(item.classId)}" ${item.classId === selectedClassId ? "selected" : ""}>${escapeHtml(item.className || item.classId)}</option>`
|
||||
)).join("");
|
||||
|
||||
document.documentElement.lang = language;
|
||||
document.title = `${profile.name} - ${branding.clubName}`;
|
||||
@@ -205,8 +218,8 @@ function renderProfile(data) {
|
||||
<span class="profile-eyebrow">${escapeHtml(t("development"))}</span>
|
||||
<h1>${escapeHtml(profile.name)}</h1>
|
||||
<div class="profile-meta">
|
||||
${profile.className ? `<span class="profile-pill">${escapeHtml(profile.className)}</span>` : ""}
|
||||
${profile.brand ? `<span class="profile-pill">${escapeHtml(profile.brand)}</span>` : ""}
|
||||
${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>` : ""}
|
||||
${rank ? `<span class="profile-pill">${escapeHtml(rank)}</span>` : ""}
|
||||
</div>
|
||||
</section>
|
||||
@@ -282,6 +295,15 @@ function renderProfile(data) {
|
||||
</div>
|
||||
</details>
|
||||
`;
|
||||
document.getElementById("profileClass")?.addEventListener("change", (event) => {
|
||||
selectedClassId = String(event.currentTarget.value || "");
|
||||
const nextUrl = new URL(window.location.href);
|
||||
if (selectedClassId) nextUrl.searchParams.set("class", selectedClassId);
|
||||
else nextUrl.searchParams.delete("class");
|
||||
window.history.replaceState({}, "", nextUrl);
|
||||
lastTimerPhase = "";
|
||||
renderProfile(profileData);
|
||||
});
|
||||
bindTimerControls();
|
||||
updateTimerDisplay();
|
||||
}
|
||||
@@ -292,7 +314,7 @@ function renderStat(label, value) {
|
||||
|
||||
function readTimerState() {
|
||||
try {
|
||||
const parsed = JSON.parse(localStorage.getItem(TIMER_STORAGE_KEY) || "null");
|
||||
const parsed = JSON.parse(localStorage.getItem(getTimerStorageKey()) || "null");
|
||||
return parsed && typeof parsed === "object" ? parsed : null;
|
||||
} catch {
|
||||
return null;
|
||||
@@ -300,8 +322,12 @@ function readTimerState() {
|
||||
}
|
||||
|
||||
function writeTimerState(value) {
|
||||
if (value) localStorage.setItem(TIMER_STORAGE_KEY, JSON.stringify(value));
|
||||
else localStorage.removeItem(TIMER_STORAGE_KEY);
|
||||
if (value) localStorage.setItem(getTimerStorageKey(), JSON.stringify(value));
|
||||
else localStorage.removeItem(getTimerStorageKey());
|
||||
}
|
||||
|
||||
function getTimerStorageKey() {
|
||||
return `rc-training-timer:${shareToken}:${selectedClassId || "default"}`;
|
||||
}
|
||||
|
||||
function getTimerElapsed(state, now = Date.now()) {
|
||||
|
||||
Reference in new issue
Block a user