|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function checkAuthentication() {
|
|
|
return new Promise((resolve) => {
|
|
|
onAuthChange((user) => {
|
|
|
if (user) {
|
|
|
|
|
|
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 {
|
|
|
|
|
|
if (window.location.pathname !== '/login.html' && !window.location.pathname.endsWith('login.html')) {
|
|
|
window.location.href = '/login.html';
|
|
|
}
|
|
|
resolve(false);
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function setupLogout() {
|
|
|
const logoutBtn = document.getElementById('logoutBtn');
|
|
|
if (logoutBtn) {
|
|
|
logoutBtn.addEventListener('click', async () => {
|
|
|
try {
|
|
|
await signOutUser();
|
|
|
window.location.href = '/';
|
|
|
} catch (error) {
|
|
|
console.error('Logout error:', error);
|
|
|
showToast('Failed to logout', 'error');
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function init() {
|
|
|
try {
|
|
|
|
|
|
|
|
|
const isPageReload = performance.navigation.type === 1 ||
|
|
|
performance.getEntriesByType('navigation')[0]?.type === 'reload';
|
|
|
|
|
|
if (isPageReload) {
|
|
|
|
|
|
try {
|
|
|
await signOutUser();
|
|
|
} catch (error) {
|
|
|
console.error('Auto-logout error:', error);
|
|
|
}
|
|
|
window.location.href = '/';
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
const isAuthenticated = await checkAuthentication();
|
|
|
if (!isAuthenticated) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
setupLogout();
|
|
|
|
|
|
updateLoadingStatus('Initializing ONNX Runtime...', 10);
|
|
|
|
|
|
|
|
|
await initONNXRuntime();
|
|
|
|
|
|
|
|
|
await loadLabelMappings();
|
|
|
|
|
|
|
|
|
await loadAllModels();
|
|
|
|
|
|
|
|
|
setupEventListeners();
|
|
|
|
|
|
|
|
|
initializeStepper();
|
|
|
|
|
|
|
|
|
initializeCropEditor();
|
|
|
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
if (document.readyState === 'loading') {
|
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
|
} else {
|
|
|
init();
|
|
|
}
|
|
|
|
|
|
export { init };
|
|
|
|