File size: 4,175 Bytes
ed9f15f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
/**
* 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 };
|