cannot fix issue with elapsed timer on admin dashboard. fixing issues with QR code editing.

Grey = cant click
Red = current
Green = complete
blue = skipped?
This commit is contained in:
bobbert committed 2025-03-15 20:01:51 +00:00
1 parent 7d6b73ffb4
commit 0afa6817a8
12 files changed
+1529 -211

No files matched your search

+112 -30
View File
@@ -4,26 +4,41 @@
// Store the hunt timer information in localStorage for persistence
export const setHuntTimer = (startTime, endTime = null, isActive = true) => {
const timerData = {
startTime,
endTime,
isActive,
lastUpdated: new Date().toISOString(),
};
localStorage.setItem("huntTimerData", JSON.stringify(timerData));
};
if (!startTime) {
console.error("Cannot set timer without a start time");
return false;
}
// Clear the hunt timer completely
export const clearHuntTimer = () => {
localStorage.removeItem("huntTimerData");
localStorage.removeItem("teamHuntStartTime"); // Remove legacy timer as well
try {
const timerData = {
startTime,
endTime,
isActive,
lastUpdated: new Date().toISOString(),
};
localStorage.setItem("huntTimerData", JSON.stringify(timerData));
// Also save to sessionStorage as a fallback
// This can survive page refreshes even if localStorage is cleared
sessionStorage.setItem("huntTimerData", JSON.stringify(timerData));
return true;
} catch (error) {
console.error("Error setting hunt timer data:", error);
return false;
}
};
// Get the current hunt timer data with improved error handling
export const getHuntTimer = () => {
try {
// First try to get timer data from localStorage
const timerData = localStorage.getItem("huntTimerData");
let timerData = localStorage.getItem("huntTimerData");
// If not in localStorage, try sessionStorage as fallback
if (!timerData) {
timerData = sessionStorage.getItem("huntTimerData");
}
if (!timerData) {
// Try to migrate from old format if exists
@@ -39,9 +54,9 @@ export const getHuntTimer = () => {
// Save the migrated data for future use
localStorage.setItem("huntTimerData", JSON.stringify(migratedData));
sessionStorage.setItem("huntTimerData", JSON.stringify(migratedData));
return migratedData;
}
console.log("No hunt timer data found");
return null;
}
@@ -61,6 +76,7 @@ export const getHuntTimer = () => {
// Try to recover by clearing corrupt data
try {
localStorage.removeItem("huntTimerData");
sessionStorage.removeItem("huntTimerData");
} catch (e) {
// Ignore errors when trying to clean up
}
@@ -69,28 +85,94 @@ export const getHuntTimer = () => {
}
};
// Calculate and format elapsed time from a given start time
// Clear the hunt timer completely
export const clearHuntTimer = () => {
try {
localStorage.removeItem("huntTimerData");
sessionStorage.removeItem("huntTimerData");
localStorage.removeItem("teamHuntStartTime"); // Remove legacy timer as well
return true;
} catch (error) {
console.error("Error clearing hunt timer data:", error);
return false;
}
};
// Calculate and format elapsed time from a given start time with high accuracy
export const formatElapsedTime = (startTimeStr) => {
if (!startTimeStr) return "00:00:00";
const startTime = new Date(startTimeStr).getTime();
const now = Date.now();
const elapsed = Math.floor((now - startTime) / 1000);
try {
const startTime = new Date(startTimeStr).getTime();
// Handle negative time (shouldn't happen in normal usage)
if (elapsed < 0) return "00:00:00";
// Validate that we have a valid timestamp
if (isNaN(startTime)) {
console.error("Invalid start time:", startTimeStr);
return "00:00:00";
}
const hours = Math.floor(elapsed / 3600)
.toString()
.padStart(2, "0");
const minutes = Math.floor((elapsed % 3600) / 60)
.toString()
.padStart(2, "0");
const seconds = Math.floor(elapsed % 60)
.toString()
.padStart(2, "0");
const now = Date.now();
const elapsed = Math.floor((now - startTime) / 1000);
return `${hours}:${minutes}:${seconds}`;
// Handle negative time (shouldn't happen in normal usage)
if (elapsed < 0) {
console.warn("Negative elapsed time detected:", elapsed);
return "00:00:00";
}
const hours = Math.floor(elapsed / 3600)
.toString()
.padStart(2, "0");
const minutes = Math.floor((elapsed % 3600) / 60)
.toString()
.padStart(2, "0");
const seconds = Math.floor(elapsed % 60)
.toString()
.padStart(2, "0");
return `${hours}:${minutes}:${seconds}`;
} catch (error) {
console.error("Error formatting elapsed time:", error);
return "00:00:00";
}
};
// Function to fetch current timer state from server and update local storage
export const syncTimerWithServer = async () => {
try {
const response = await fetch("/api/hunt/timer", {
credentials: "include",
headers: {
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache",
},
});
if (!response.ok) {
console.error("Server responded with status:", response.status);
return { success: false, error: `Server error: ${response.status}` };
}
const data = await response.json();
if (data.active && data.startTime) {
setHuntTimer(data.startTime, data.endTime);
return {
success: true,
data: {
startTime: data.startTime,
endTime: data.endTime,
active: data.active,
},
};
} else {
clearHuntTimer();
return { success: true, data: null };
}
} catch (error) {
console.error("Error syncing timer with server:", error);
return { success: false, error: error.message };
}
};
// Hook function to use in components to get formatted elapsed time