/** * Listmonk Status Manager * Handles real-time status monitoring and user notifications for email list sync */ class ListmonkStatus { constructor() { this.statusElement = null; this.checkInterval = null; this.lastErrorShown = null; this.currentStatus = { enabled: false, connected: false, lastError: null }; this.init(); } init() { // Only initialize if user is authenticated if (window.currentUser) { this.createStatusIndicator(); this.startStatusCheck(); } } createStatusIndicator() { // Create status indicator element const indicator = document.createElement('div'); indicator.id = 'listmonk-status'; indicator.className = 'listmonk-status-indicator checking'; indicator.innerHTML = ` Email Sync `; // Find a good place to put it - try header actions first let container = document.querySelector('.header-actions'); if (!container) { // Fallback to header nav container = document.querySelector('.header-nav'); } if (!container) { // Fallback to header itself container = document.querySelector('header'); } if (container) { container.appendChild(indicator); this.statusElement = indicator; } } async checkStatus() { try { const response = await fetch('/api/listmonk/status', { credentials: 'include' }); if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); } const status = await response.json(); this.updateIndicator(status); } catch (error) { console.error('Failed to check Listmonk status:', error); this.updateIndicator({ enabled: false, connected: false, lastError: error.message }); } } updateIndicator(status) { if (!this.statusElement) return; const icon = this.statusElement.querySelector('.status-icon'); const text = this.statusElement.querySelector('.status-text'); // Update current status const wasConnected = this.currentStatus.connected; this.currentStatus = status; if (!status.enabled) { this.statusElement.className = 'listmonk-status-indicator disabled'; icon.innerHTML = '⭕'; text.textContent = 'Sync Off'; this.statusElement.title = 'Email list synchronization is disabled'; } else if (status.connected) { this.statusElement.className = 'listmonk-status-indicator connected'; icon.innerHTML = '✅'; text.textContent = 'Sync On'; this.statusElement.title = 'Email lists are synchronizing automatically'; // If we just reconnected, show a brief success message if (!wasConnected && this.lastErrorShown) { this.showReconnectedNotification(); } } else { this.statusElement.className = 'listmonk-status-indicator error'; icon.innerHTML = '❌'; text.textContent = 'Sync Error'; this.statusElement.title = status.lastError || 'Email list sync failed - check configuration'; // Show popup warning if this is a new error or first time seeing this error const errorKey = status.lastError + (status.lastErrorTime || ''); if ((!this.lastErrorShown || this.lastErrorShown !== errorKey) && status.lastError) { this.showSyncError(status.lastError); this.lastErrorShown = errorKey; } } } showSyncError(error) { // Don't spam notifications if (document.querySelector('.sync-error-notification')) { return; } // Create and show error notification const notification = document.createElement('div'); notification.className = 'sync-error-notification'; notification.innerHTML = `
The email list synchronization is not working:
${this.escapeHtml(error || 'Connection failed')}
New contacts will be saved locally but won't sync to email lists until this is resolved.
Please contact your administrator if this persists.
Email list synchronization is working again. New contacts will now sync automatically.