/** * User Dropdown Health Check * This script diagnoses and attempts to fix issues with user dropdown population */ document.addEventListener('DOMContentLoaded', function() { console.log('🩺 Running user dropdown health check...'); // Create a debug panel const debugPanel = document.createElement('div'); debugPanel.id = 'userDropdownDebug'; debugPanel.style.cssText = 'position:fixed; bottom:10px; right:10px; background:#f5f5f5; border:1px solid #ddd; padding:10px; max-width:350px; max-height:400px; overflow:auto; font-size:12px; z-index:9999; display:none'; debugPanel.innerHTML = `
Running diagnostics...
'; // Check window.adminUsers let report = `✅ window.adminUsers exists with ${window.adminUsers.length} users
`; report += `${JSON.stringify(window.adminUsers.map(u => u.username), null, 2)}❌ window.adminUsers is undefined!
`; } // Check all user dropdowns report += `${status} ${dropdown.label}: ${optionCount - 1} users
`; } else { report += `❓ ${dropdown.label}: Not found in DOM
`; } }); // Check server connection report += `Testing connection to /getUsers endpoint...
`; fetch('/getUsers') .then(response => { if (response.ok) { return response.json(); } throw new Error(`Server returned ${response.status}`); }) .then(data => { report += `✅ Server returned ${data.length} users
`; debugContent.innerHTML = report; }) .catch(error => { report += `❌ Error fetching users: ${error.message}
`; debugContent.innerHTML = report; }); } // Validate all user dropdowns function validateUserDropdowns(autoFix = false) { console.log('🔍 Validating user dropdowns...'); // Get all user dropdown elements const dropdowns = [ document.getElementById('userDropdown'), document.getElementById('wordUserSelect'), document.getElementById('passwordUsername'), document.getElementById('analyticsUserSelect'), document.getElementById('reportUser') ].filter(Boolean); // Check if any dropdown is empty or has only the default option const needsRepopulation = dropdowns.some(dropdown => dropdown.options.length <= 1); if (needsRepopulation && autoFix) { console.log('🔧 Found empty dropdowns, attempting to fix...'); forceSyncUserData(); } return !needsRepopulation; } // Force refresh of all user data async function forceSyncUserData() { console.log('🔄 Forcing synchronization of user data...'); try { // Fetch fresh user data from server const response = await fetch('/getUsers'); if (!response.ok) { throw new Error(`Server error: ${response.status}`); } const users = await response.json(); // Update global user data if (typeof window.adminUsers === 'undefined') { window.adminUsers = []; } window.adminUsers = users; console.log(`✅ Updated global users list with ${users.length} users`); // Update all dropdowns if (typeof window.displayUserDropdown === 'function') { window.displayUserDropdown(); } else { console.warn('⚠️ window.displayUserDropdown function not found'); } // Also call analytics dropdown population if it exists if (typeof populateUserDropdown === 'function') { populateUserDropdown(); } // Run health check again if (document.getElementById('debugContent')) { runHealthCheck(); } return true; } catch (error) { console.error('❌ Error syncing user data:', error); return false; } } });