document.addEventListener('DOMContentLoaded', () => {
const leaderboardBody = document.getElementById('leaderboard-body');
const loadingIndicator = document.getElementById('loading-indicator');
const lastUpdatedElement = document.getElementById('last-updated');
const modal = document.getElementById('code-modal');
const modalUsername = document.getElementById('modal-username');
const modalCode = document.getElementById('modal-code');
const closeModalButton = document.querySelector('.close-button');
// Fetch from the FastAPI backend endpoint
const API_URL = '/api/leaderboard';
const REFRESH_INTERVAL_MS = 60 * 1000; // 60 seconds
async function fetchData() {
try {
// Fetch directly from our backend API
const response = await fetch(API_URL);
if (!response.ok) {
// Try to get error message from backend if available
let errorMsg = `HTTP error! status: ${response.status}`;
try {
const errorData = await response.json();
errorMsg = errorData.detail || errorMsg;
} catch (e) { /* Ignore if response is not JSON */ }
throw new Error(errorMsg);
}
const data = await response.json();
// The data is expected to be the already sorted list of objects
return data;
} catch (error) {
console.error("Failed to fetch leaderboard data:", error);
lastUpdatedElement.textContent = `Error: ${error.message}`; // Display error
return null; // Return null to indicate failure
}
}
// No sorting needed here - backend does it
// function sortData(data) { ... } // REMOVED
function formatTimestamp(isoString) {
// Keep this helper function
try {
const date = new Date(isoString);
return date.toISOString().slice(0, 19).replace('T', ' ');
} catch (e) {
return 'Invalid Date';
}
}
function renderLeaderboard(leaderboardData) {
// Clear previous content
leaderboardBody.innerHTML = '';
if (!leaderboardData || leaderboardData.length === 0) {
// Check if the loading indicator is still displayed, means fetch failed on load
if (loadingIndicator.style.display !== 'none') {
leaderboardBody.innerHTML = '
No data available or failed to load. Check logs.
';
} else {
// If fetch failed during refresh, keep old data and show error in footer? Or clear?
// Let's clear for now and rely on error message in footer.
leaderboardBody.innerHTML = '