|
2 | 2 |
|
3 | 3 | document.addEventListener('DOMContentLoaded', function() { |
4 | 4 | const summarizeButton = document.getElementById('summarizeButton'); |
| 5 | + const regenerateButton = document.getElementById('regenerateButton'); |
| 6 | + |
5 | 7 | if (summarizeButton) { |
6 | 8 | summarizeButton.addEventListener('click', summarizeNote); |
7 | 9 | } |
| 10 | + |
| 11 | + if (regenerateButton) { |
| 12 | + regenerateButton.addEventListener('click', summarizeNote); |
| 13 | + } |
| 14 | + |
| 15 | + // Handle toast close buttons |
| 16 | + document.addEventListener('click', function(e) { |
| 17 | + if (e.target.classList.contains('toast-close-btn')) { |
| 18 | + e.target.closest('.toast').remove(); |
| 19 | + } |
| 20 | + }); |
8 | 21 | }); |
9 | 22 |
|
10 | | -// Move summarizeNote function here if you want to fully decouple from inline, or keep it in EJS if it uses EJS variables. |
11 | | -// If you want to move it, copy the function from view-notes.ejs and replace <%= noteID %> with a data attribute or window.NOTE_ID. |
| 23 | +function showToast(message, type = 'info') { |
| 24 | + let toastContainer = document.getElementById('toast-container'); |
| 25 | + if (!toastContainer) { |
| 26 | + toastContainer = createToastContainer(); |
| 27 | + } |
| 28 | + |
| 29 | + const toast = document.createElement('div'); |
| 30 | + toast.className = `toast show bg-${type} text-white`; |
| 31 | + toast.setAttribute('role', 'alert'); |
| 32 | + toast.innerHTML = ` |
| 33 | + <div class="d-flex"> |
| 34 | + <div class="toast-body"> |
| 35 | + <i class="fas fa-${type === 'success' ? 'check-circle' : 'exclamation-circle'} me-2"></i> |
| 36 | + ${message} |
| 37 | + </div> |
| 38 | + <button type="button" class="btn-close btn-close-white me-2 m-auto toast-close-btn"></button> |
| 39 | + </div> |
| 40 | + `; |
| 41 | + |
| 42 | + toastContainer.appendChild(toast); |
| 43 | + |
| 44 | + // Auto remove after 4 seconds |
| 45 | + setTimeout(() => { |
| 46 | + if (toast.parentNode) { |
| 47 | + toast.classList.remove('show'); |
| 48 | + setTimeout(() => toast.remove(), 300); |
| 49 | + } |
| 50 | + }, 4000); |
| 51 | +} |
| 52 | + |
| 53 | +function createToastContainer() { |
| 54 | + const container = document.createElement('div'); |
| 55 | + container.id = 'toast-container'; |
| 56 | + container.className = 'toast-container position-fixed top-0 end-0 p-3'; |
| 57 | + container.style.zIndex = '1055'; |
| 58 | + document.body.appendChild(container); |
| 59 | + return container; |
| 60 | +} |
| 61 | + |
| 62 | +async function summarizeNote() { |
| 63 | + const noteBody = document.getElementById('body').value; |
| 64 | + const summarizeButton = document.getElementById('summarizeButton'); |
| 65 | + const summarySection = document.getElementById('summarySection'); |
| 66 | + const summaryContent = document.getElementById('summaryContent'); |
| 67 | + // Get noteId from global variable set by EJS template |
| 68 | + const noteId = window.NOTE_ID || document.body.dataset.noteId || window.location.pathname.split('/').pop(); |
| 69 | + |
| 70 | + if (!noteBody.trim()) { |
| 71 | + showToast('Please add some content to your note before summarizing.', 'warning'); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if (noteBody.trim().length < 50) { |
| 76 | + showToast('Note content is too short for meaningful summarization. Please add more content.', 'warning'); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // Update button state with loading animation |
| 81 | + const originalButtonContent = summarizeButton.innerHTML; |
| 82 | + summarizeButton.innerHTML = '<span class="spinner-border spinner-border-sm me-2"></span>Generating...'; |
| 83 | + summarizeButton.disabled = true; |
| 84 | + |
| 85 | + try { |
| 86 | + const response = await fetch('/dashboard/summarize', { |
| 87 | + method: 'POST', |
| 88 | + headers: { |
| 89 | + 'Content-Type': 'application/json', |
| 90 | + }, |
| 91 | + body: JSON.stringify({ |
| 92 | + text: noteBody, |
| 93 | + noteId: noteId |
| 94 | + }) |
| 95 | + }); |
| 96 | + |
| 97 | + if (!response.ok) { |
| 98 | + throw new Error(`HTTP ${response.status}: ${response.statusText}`); |
| 99 | + } |
| 100 | + |
| 101 | + const data = await response.json(); |
| 102 | + |
| 103 | + if (data.success && data.summary && data.summary.trim()) { |
| 104 | + // Update summary content with fade effect |
| 105 | + summaryContent.style.opacity = '0.5'; |
| 106 | + |
| 107 | + setTimeout(() => { |
| 108 | + summaryContent.textContent = data.summary; |
| 109 | + summaryContent.style.opacity = '1'; |
| 110 | + }, 200); |
| 111 | + |
| 112 | + // Show summary section with slide animation |
| 113 | + if (summarySection.classList.contains('d-none')) { |
| 114 | + summarySection.classList.remove('d-none'); |
| 115 | + summarySection.style.opacity = '0'; |
| 116 | + summarySection.style.transform = 'translateY(-20px)'; |
| 117 | + |
| 118 | + setTimeout(() => { |
| 119 | + summarySection.style.transition = 'all 0.3s ease'; |
| 120 | + summarySection.style.opacity = '1'; |
| 121 | + summarySection.style.transform = 'translateY(0)'; |
| 122 | + }, 10); |
| 123 | + } |
| 124 | + |
| 125 | + // Smooth scroll to summary |
| 126 | + setTimeout(() => { |
| 127 | + summarySection.scrollIntoView({ |
| 128 | + behavior: 'smooth', |
| 129 | + block: 'center', |
| 130 | + inline: 'nearest' |
| 131 | + }); |
| 132 | + }, 300); |
| 133 | + |
| 134 | + showToast('Summary generated and saved successfully!', 'success'); |
| 135 | + } else { |
| 136 | + showToast(data.message || 'Failed to generate summary. Please try again.', 'danger'); |
| 137 | + } |
| 138 | + } catch (error) { |
| 139 | + console.error('Summarization Error:', error); |
| 140 | + if (error.message.includes('HTML')) { |
| 141 | + showToast('Authentication error. Please refresh the page and try again.', 'danger'); |
| 142 | + } else { |
| 143 | + showToast('Network error occurred. Please check your connection and try again.', 'danger'); |
| 144 | + } |
| 145 | + } finally { |
| 146 | + // Restore button state |
| 147 | + summarizeButton.innerHTML = originalButtonContent; |
| 148 | + summarizeButton.disabled = false; |
| 149 | + } |
| 150 | +} |
0 commit comments