/** * Embryo Grading System - Main Application Entry Point * Modular structure for better debugging and maintenance */ import { initONNXRuntime, loadLabelMappings, loadAllModels } from './models/modelLoader.js'; import { setupEventListeners } from './ui/eventHandlers.js'; import { updateLoadingStatus, hideLoading } from './ui/loading.js'; import { showToast } from './ui/toast.js'; import { initializeStepper } from './ui/stepper.js'; import { initializeCropEditor } from './ui/cropEditor.js'; import { onAuthChange, signOutUser, getCurrentUser } from './utils/firebaseService.js'; /** * Check authentication status */ function checkAuthentication() { return new Promise((resolve) => { onAuthChange((user) => { if (user) { // User is logged in const userEmail = document.getElementById('userEmail'); const userMenu = document.getElementById('userMenu'); if (userEmail) { userEmail.textContent = user.email; } if (userMenu) { userMenu.style.display = 'flex'; } resolve(true); } else { // User is not logged in, redirect to login if (window.location.pathname !== '/login.html' && !window.location.pathname.endsWith('login.html')) { window.location.href = '/login.html'; } resolve(false); } }); }); } /** * Setup logout functionality */ function setupLogout() { const logoutBtn = document.getElementById('logoutBtn'); if (logoutBtn) { logoutBtn.addEventListener('click', async () => { try { await signOutUser(); window.location.href = '/'; // Redirect to root which serves login.html } catch (error) { console.error('Logout error:', error); showToast('Failed to logout', 'error'); } }); } } /** * Initialize the application */ async function init() { try { // Auto-logout on page refresh/reload // Check if this is a page reload (not initial load from login) const isPageReload = performance.navigation.type === 1 || performance.getEntriesByType('navigation')[0]?.type === 'reload'; if (isPageReload) { // Page was reloaded, logout and redirect to login try { await signOutUser(); } catch (error) { console.error('Auto-logout error:', error); } window.location.href = '/'; return; } // Check authentication first const isAuthenticated = await checkAuthentication(); if (!isAuthenticated) { return; // Will redirect to login } // Setup logout setupLogout(); updateLoadingStatus('Initializing ONNX Runtime...', 10); // Configure ONNX Runtime await initONNXRuntime(); // Load label mappings await loadLabelMappings(); // Load models await loadAllModels(); // Setup UI event listeners setupEventListeners(); // Initialize stepper initializeStepper(); // Initialize crop editor initializeCropEditor(); // Hide loading overlay hideLoading(); showToast('AI models loaded successfully!', 'success'); } catch (error) { console.error('Initialization error:', error); showToast(`Initialization failed: ${error.message}`, 'error'); updateLoadingStatus(`Error: ${error.message}`, 100); } } // Initialize app when DOM is loaded if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } export { init };