// Chat functionality let conversations = []; let messages = {}; let pollingInterval; // Mobile sidebar functionality function toggleMobileSidebar() { const sidebar = document.getElementById('sidebar'); const overlay = document.getElementById('sidebarOverlay'); if (sidebar && overlay) { sidebar.classList.toggle('show'); overlay.classList.toggle('show'); // Prevent body scroll when sidebar is open if (sidebar.classList.contains('show')) { document.body.style.overflow = 'hidden'; } else { document.body.style.overflow = ''; } } } // Close mobile sidebar when clicking outside function closeMobileSidebar() { const sidebar = document.getElementById('sidebar'); const overlay = document.getElementById('sidebarOverlay'); if (sidebar && overlay) { sidebar.classList.remove('show'); overlay.classList.remove('show'); document.body.style.overflow = ''; } } // Initialize chat functionality document.addEventListener('DOMContentLoaded', () => { if (!document.body.classList.contains('chat-page')) { return; } initializeChat(); }); async function initializeChat() { try { console.log('Initializing chat...'); // Clear any existing data conversations = []; messages = {}; window.currentConversation = null; // Load fresh data await loadConversations(); startPolling(); setupEventListeners(); console.log('Chat initialized successfully'); } catch (error) { console.error('Failed to initialize chat:', error); MainJS.showError('Failed to initialize chat'); } } function setupEventListeners() { // Message form const messageForm = document.getElementById('messageForm'); if (messageForm) { messageForm.addEventListener('submit', handleSendMessage); } // Private chat form const privateChatForm = document.getElementById('privateChatForm'); if (privateChatForm) { privateChatForm.addEventListener('submit', handleStartPrivateChat); } // Group chat form const groupChatForm = document.getElementById('groupChatForm'); if (groupChatForm) { groupChatForm.addEventListener('submit', handleCreateGroup); } } async function loadConversations() { try { const response = await MainJS.apiRequest('/api/conversations'); if (response.success) { conversations = response.conversations || []; renderConversations(); } else { console.warn('Failed to load conversations:', response.message); // Show empty state instead of error for unauthenticated users conversations = []; renderConversations(); } } catch (error) { console.error('Failed to load conversations:', error); conversations = []; renderConversations(); } } function renderConversations() { const conversationsList = document.getElementById('conversationsList'); if (!conversationsList) { console.error('Conversations list element not found'); return; } console.log('Rendering conversations:', conversations); // FORCE CLEAR the conversations list first conversationsList.innerHTML = ''; if (conversations.length === 0) { conversationsList.innerHTML = `
No conversations yet
Start a new chat to begin messagingNo messages yet
Send the first message to start the conversation