// Main Application File for SnowWorld Admin Dashboard // Application configuration const AppConfig = { API_BASE_URL: 'http://localhost:3000/api', WS_URL: 'http://localhost:3000', REFRESH_INTERVAL: 30000, // 30 seconds MAX_FILE_SIZE: 50 * 1024 * 1024, // 50MB SUPPORTED_FILE_TYPES: { 'image': ['image/jpeg', 'image/png', 'image/gif', 'image/webp'], 'video': ['video/mp4', 'video/webm', 'video/ogg'] } }; // Main Application Class class SnowWorldAdminApp { constructor() { this.config = AppConfig; this.isInitialized = false; this.refreshTimer = null; this.init(); } async init() { try { console.log('Initializing SnowWorld Admin Dashboard...'); // Wait for dependencies to load await this.waitForDependencies(); // Initialize application components this.setupGlobalErrorHandling(); this.setupKeyboardShortcuts(); this.setupAutoRefresh(); // Initialize UI and WebSocket connections if (window.ui) { console.log('UI Manager loaded successfully'); } if (window.wsManager) { console.log('WebSocket Manager loaded successfully'); } if (window.api) { console.log('API Service loaded successfully'); } this.isInitialized = true; console.log('SnowWorld Admin Dashboard initialized successfully'); // Show welcome message this.showWelcomeMessage(); } catch (error) { console.error('Failed to initialize application:', error); this.handleInitializationError(error); } } async waitForDependencies() { const maxWaitTime = 10000; // 10 seconds const checkInterval = 100; // 100ms let elapsedTime = 0; return new Promise((resolve, reject) => { const checkDependencies = () => { if (window.ui && window.wsManager && window.api) { resolve(); } else if (elapsedTime >= maxWaitTime) { reject(new Error('Dependencies timeout - required services not loaded')); } else { elapsedTime += checkInterval; setTimeout(checkDependencies, checkInterval); } }; checkDependencies(); }); } setupGlobalErrorHandling() { // Handle JavaScript errors window.addEventListener('error', (event) => { console.error('Global error:', event.error); this.handleError(event.error); }); // Handle unhandled promise rejections window.addEventListener('unhandledrejection', (event) => { console.error('Unhandled promise rejection:', event.reason); this.handleError(event.reason); }); } setupKeyboardShortcuts() { document.addEventListener('keydown', (e) => { // Ctrl/Cmd + R: Refresh data if ((e.ctrlKey || e.metaKey) && e.key === 'r') { e.preventDefault(); this.refreshData(); } // Ctrl/Cmd + N: New content (if on content tab) if ((e.ctrlKey || e.metaKey) && e.key === 'n') { e.preventDefault(); if (window.ui && window.ui.currentTab === 'content') { window.ui.openContentModal(); } } // Escape: Close modals if (e.key === 'Escape') { window.ui?.closeModals(); } // F5: Refresh (prevent default and use our refresh) if (e.key === 'F5') { e.preventDefault(); this.refreshData(); } }); } setupAutoRefresh() { // Clear any existing timer if (this.refreshTimer) { clearInterval(this.refreshTimer); } // Set up new timer this.refreshTimer = setInterval(() => { this.autoRefresh(); }, this.config.REFRESH_INTERVAL); console.log(`Auto-refresh enabled with interval: ${this.config.REFRESH_INTERVAL}ms`); } autoRefresh() { // Only refresh if connected and not in modal if (window.wsManager?.getConnectionStatus().connected && !document.querySelector('.modal.active')) { console.log('Performing auto-refresh...'); // Refresh current tab data if (window.ui) { window.ui.refreshData(); } } } refreshData() { if (window.ui) { window.ui.refreshData(); } if (window.wsManager) { const status = window.wsManager.getConnectionStatus(); console.log('Connection status:', status); } } showWelcomeMessage() { const messages = [ 'Welkom bij SnowWorld Narrowcasting Admin!', 'Systeem succesvol geladen.', 'Klaar om content te beheren.' ]; messages.forEach((message, index) => { setTimeout(() => { window.ui?.showToast(message, 'info'); }, index * 1000); }); } handleError(error) { console.error('Application error:', error); // Show user-friendly error message const userMessage = this.getUserFriendlyErrorMessage(error); window.ui?.showToast(userMessage, 'error'); // Log to server if connected if (window.wsManager?.getConnectionStatus().connected) { window.wsManager.sendMessage('clientError', { message: error.message, stack: error.stack, timestamp: new Date().toISOString(), userAgent: navigator.userAgent }); } } handleInitializationError(error) { console.error('Initialization error:', error); // Create emergency error display const errorDiv = document.createElement('div'); errorDiv.className = 'emergency-error'; errorDiv.innerHTML = `
Er is een fout opgetreden bij het laden van het systeem.
${error.message}\n${error.stack}