/** * Email Agent - Webhook Integration * Connects to your n8n email analysis workflow */ class EmailAgent { constructor() { this.apiEndpoint = 'https://thecyberlearn.app.n8n.cloud/webhook/email-agent'; this.currentEmails = []; this.currentFilter = 'all'; this.totalActions = 0; this.init(); } init() { this.updateLastSync(); this.initializeEventListeners(); this.loadEmails(); // Auto-load emails on page load } initializeEventListeners() { // Search on Enter key const searchInput = document.getElementById('searchQuery'); if (searchInput) { searchInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') { this.searchEmails(); } }); } } updateLastSync() { const element = document.getElementById('lastSync'); if (element) { element.textContent = new Date().toLocaleTimeString(); } } // Main function to load emails from webhook async loadEmails(action = 'get_inbox', params = {}) { const loadingState = document.getElementById('loadingState'); const emailList = document.getElementById('emailList'); const emptyState = document.getElementById('emptyState'); try { // Show loading state this.showLoading(true); // Build API URL const url = new URL(this.apiEndpoint); url.searchParams.append('action', action); // Add additional parameters Object.keys(params).forEach(key => { if (params[key]) { url.searchParams.append(key, params[key]); } }); console.log('Loading emails from:', url.toString()); const response = await fetch(url.toString()); if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); } const data = await response.json(); console.log('Email data received:', data); if (data.success && data.emails) { this.currentEmails = data.emails; this.displayEmails(data.emails); this.updateStats(data); this.addToRecentActivity(`Loaded ${data.emails.length} emails`, 'load'); // Hide empty state, show email list if (emptyState) emptyState.classList.add('hidden'); if (emailList) emailList.classList.remove('hidden'); } else { this.showEmptyState(); console.log('No emails found or API error'); } } catch (error) { console.error('Error loading emails:', error); this.showError('Failed to load emails: ' + error.message); } finally { this.showLoading(false); this.updateLastSync(); } } // Display emails in the UI displayEmails(emails) { const emailList = document.getElementById('emailList'); if (!emailList) return; if (emails.length === 0) { this.showEmptyState(); return; } const emailHTML = emails.map((email, index) => { return this.generateEmailCard(email, index); }).join(''); emailList.innerHTML = emailHTML; emailList.classList.remove('hidden'); // Add click handlers this.attachEmailClickHandlers(); } // Generate individual email card HTML generateEmailCard(email, index) { const priorityClass = `priority-${email.priority.toLowerCase()}`; const unreadClass = email.unread ? 'email-unread' : 'email-read'; const typeIcon = this.getTypeIcon(email.type); const typeColor = this.getTypeColor(email.type); return `
${email.summary}
From: ${email.sender} (${email.senderEmail})
${email.timestamp}
${email.summary}
${email.fullSummary}
Priority: ${email.priority}
Attachments: ${email.attachments}
Auto-processed: Yes
${message}
${time}