connecton satus mobil profile

This commit is contained in:
larssand committed 2026-09-08 19:27:37 +02:00
1 parent 5559d74846
commit b3bec531de
11 files changed
+169 -7

No files matched your search

+52 -2
View File
@@ -10,6 +10,13 @@ let selectedClassId = new URLSearchParams(window.location.search).get("class") |
const messages = {
sv: {
connection_loading: "Hämtar statistik...",
connection_current: "Statistiken är uppdaterad.",
connection_failed: "Uppdateringen misslyckades. Visar senast hämtade data om de finns.",
connection_stale: "Statistiken kan vara inaktuell. Uppdatering väntar; avsluta eventuell inmatning.",
connection_last: "Senast hämtad {time}.",
connection_retry: "Försök igen",
connection_retrying: "Hämtar...",
loading: "Laddar träningsprofil...",
loadError: "Kunde inte läsa träningsprofilen. Länken kan vara avstängd eller ersatt.",
development: "Förarutveckling",
@@ -130,6 +137,13 @@ const messages = {
challengeProgress: "{current} / {target}",
},
en: {
connection_loading: "Loading statistics...",
connection_current: "Statistics are up to date.",
connection_failed: "Update failed. Showing previously loaded data if available.",
connection_stale: "Statistics may be outdated. Update pending; finish any active input.",
connection_last: "Last loaded {time}.",
connection_retry: "Retry",
connection_retrying: "Loading...",
loading: "Loading training profile...",
loadError: "The training profile could not be loaded. The link may be disabled or replaced.",
development: "Driver development",
@@ -788,8 +802,35 @@ function speak(text) {
window.speechSynthesis.speak(utterance);
}
const profileConnection = { lastSuccessAt: null, failed: false, loading: false };
function getProfileConnectionState(now = Date.now()) {
if (profileConnection.failed) return "failed";
if (profileConnection.lastSuccessAt !== null && now - profileConnection.lastSuccessAt >= 30000) return "stale";
return profileConnection.lastSuccessAt === null ? "loading" : "current";
}
function updateProfileConnectionStatus() {
const status = document.getElementById("publicTrainingConnection");
const label = document.getElementById("publicTrainingConnectionText");
const retry = document.getElementById("publicTrainingRetry");
if (!status || !label || !retry) return;
const state = getProfileConnectionState();
status.dataset.state = state;
const last = profileConnection.lastSuccessAt;
const text = t(`connection_${state}`) + (last === null ? "" : " " + t("connection_last", { time: new Date(last).toLocaleTimeString(language) }));
if (label.textContent !== text) label.textContent = text;
retry.textContent = t(profileConnection.loading ? "connection_retrying" : "connection_retry");
retry.hidden = state === "current" || (state === "loading" && !profileConnection.failed);
retry.disabled = profileConnection.loading;
}
async function loadProfile(background = false) {
if (background && profileIsBeingEdited()) return;
if (profileConnection.loading || (background && profileIsBeingEdited())) return;
profileConnection.loading = true;
updateProfileConnectionStatus();
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 15000);
try {
const timerState = readTimerState();
const apiUrl = new URL(`/public-training/api/${encodeURIComponent(shareToken)}`, window.location.origin);
@@ -801,18 +842,27 @@ async function loadProfile(background = false) {
apiUrl.searchParams.set("startedAgoMs", String(startedAgoMs));
apiUrl.searchParams.set("windowMs", String(Math.min(startedAgoMs, maximumWindowMs)));
}
const response = await fetch(apiUrl, { cache: "no-store" });
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();
if (background && profileIsBeingEdited()) return;
renderProfile(data);
profileConnection.lastSuccessAt = Date.now();
profileConnection.failed = false;
} catch {
profileConnection.failed = true;
if (background && profileData) return;
root.innerHTML = `<section class="public-training-error"><div class="profile-brand-mark">RC</div><h1>${escapeHtml(t("loadError"))}</h1></section>`;
} finally {
clearTimeout(timeout);
profileConnection.loading = false;
updateProfileConnectionStatus();
}
}
loadProfile();
document.getElementById("publicTrainingRetry")?.addEventListener("click", () => loadProfile(true));
setInterval(updateProfileConnectionStatus, 1000);
setInterval(() => {
if (!document.hidden) loadProfile(true);
}, 10000);