/
bytevalue
/
optimal
Обзор
Документация
Войти
/
bytevalue
/
optimal
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/resources/static/js/app.js
153 строки
5 KB
bytevalue
Nexus -> optimal switch
08 дек 2025, 14:17
08 дек 2025, 14:17
a892ce4
Код
Авторство
О чём код?
/** * Main Application Entry Point */ class ChatApplication { constructor() { this.isInitialized = false; } /** * Initialize application */ async initialize() { if (this.isInitialized) { console.warn('⚠️ Application already initialized'); return; } try { console.log('🚀 Initializing Optimal...'); uiManager.showLoading(true); // 1. Check API health const isHealthy = await apiService.checkHealth(); if (!isHealthy) { console.warn('⚠️ API not responding, continuing in offline mode'); } else { stateService.setState('isConnected', true); } // 2. Load initial data await this.loadInitialData(); // 3. Connect WebSocket if API is healthy if (stateService.getState('isConnected')) { webSocketService.connect(); } uiManager.showLoading(false); this.isInitialized = true; // 4. Важное изменение: СНАЧАЛА сбрасываем активный чат stateService.setState('activeChatId', null); stateService.setWelcomeScreen(true); // 5. Показываем приветственный экран uiManager.showWelcomeScreen(); eventBus.publish(CONFIG.EVENTS.APP_READY); console.log('✅ Application initialized successfully'); } catch (error) { console.error('❌ Failed to initialize application:', error); uiManager.showError( `Initialization failed: ${error.message}` ); } } /** * Load initial data */ async loadInitialData() { try { console.log('📥 Loading initial data...'); const response = await apiService.getActiveChats(); if (response && response.chats) { stateService.setActiveChats(response.chats); console.log(`✅ Loaded ${response.chats.length} active chats`); // Обновляем список чатов в UI uiManager.renderChatList(); // Обновляем список недавних чатов в приветственном экране this.updateRecentChats(); } else { console.log('📥 No active chats found'); stateService.setActiveChats([]); } } catch (error) { console.warn('⚠️ Failed to load initial data:', error.message); stateService.setActiveChats([]); } } /** * Update recent chats in welcome screen */ updateRecentChats() { const activeChats = stateService.getState('activeChats'); const recentChatsContainer = document.getElementById('recentChats'); if (!recentChatsContainer) return; recentChatsContainer.innerHTML = ''; // Показываем последние 3 чата (только активные) const recentChats = activeChats .filter(chat => chat.status !== CONFIG.CHAT_STATUS.DELETED) .slice(0, 3); if (recentChats.length === 0) { recentChatsContainer.innerHTML = ` <div class="empty-recent-chats"> <p>У вас пока нет чатов</p> </div> `; return; } recentChats.forEach(chat => { const chatItem = document.createElement('div'); chatItem.className = 'recent-chat-item'; chatItem.innerHTML = ` <div class="recent-chat-icon"> <i class="fas fa-comment"></i> </div> <div class="recent-chat-info"> <div class="recent-chat-title">${chat.title || 'Новый чат'}</div> <div class="recent-chat-time">${uiManager.formatTime(new Date(chat.updatedAt || chat.createdAt))}</div> </div> `; chatItem.addEventListener('click', () => { uiManager.selectChat(chat.id); }); recentChatsContainer.appendChild(chatItem); }); } } const app = new ChatApplication(); // Initialize on document ready if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', () => { console.log('📄 DOM loaded, initializing app...'); app.initialize(); }); } else { console.log('📄 DOM already loaded, initializing app...'); app.initialize(); } // Handle visibility changes document.addEventListener('visibilitychange', () => { if (!document.hidden) { console.log('👁️ Application became visible, checking connection...'); if (!stateService.getState('isConnected')) { webSocketService.connect(); } } });