-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
652 lines (558 loc) · 22.1 KB
/
app.js
File metadata and controls
652 lines (558 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
// API Configuration
const API_BASE_URL = 'http://localhost:3002/api';
// Email data cache
let emails = {
inbox: [],
sent: [],
drafts: [],
trash: []
};
let currentFolder = 'inbox';
let selectedEmails = new Set();
let attachments = [];
// Initialize the app
document.addEventListener('DOMContentLoaded', function() {
loadEmails(currentFolder);
setupEventListeners();
checkAPIConnection();
});
function setupEventListeners() {
// Search functionality
document.getElementById('searchInput').addEventListener('keyup', function(e) {
if (e.key === 'Enter') {
searchEmails(this.value);
}
});
// File drag and drop
const attachmentArea = document.querySelector('.attachment-area');
if (attachmentArea) {
attachmentArea.addEventListener('dragover', function(e) {
e.preventDefault();
this.style.borderColor = 'var(--primary-color)';
this.style.backgroundColor = 'var(--hover-color)';
});
attachmentArea.addEventListener('dragleave', function(e) {
e.preventDefault();
this.style.borderColor = 'var(--border-color)';
this.style.backgroundColor = 'transparent';
});
attachmentArea.addEventListener('drop', function(e) {
e.preventDefault();
this.style.borderColor = 'var(--border-color)';
this.style.backgroundColor = 'transparent';
const files = Array.from(e.dataTransfer.files);
handleFiles(files);
});
}
}
function showFolder(folder) {
// Update active sidebar item
document.querySelectorAll('.sidebar-item').forEach(item => {
item.classList.remove('active');
});
event.target.closest('.sidebar-item').classList.add('active');
currentFolder = folder;
loadEmails(folder);
}
async function loadEmails(folder) {
const emailList = document.getElementById('emailList');
try {
// Show loading state
emailList.innerHTML = `
<div style="text-align: center; padding: 40px; color: #5f6368;">
<div style="display: inline-block; width: 32px; height: 32px; border: 3px solid #f3f3f3; border-top: 3px solid var(--primary-color); border-radius: 50%; animation: spin 1s linear infinite;"></div>
<p>Loading emails...</p>
</div>
`;
const response = await fetch(`${API_BASE_URL}/emails?folder=${folder}`);
const data = await response.json();
if (!response.ok) {
throw new Error(data.error || 'Failed to load emails');
}
const folderEmails = data.emails || [];
emails[folder] = folderEmails; // Cache the emails
if (folderEmails.length === 0) {
emailList.innerHTML = `
<div style="text-align: center; padding: 40px; color: #5f6368;">
<i class="material-icons" style="font-size: 48px; margin-bottom: 16px;">inbox</i>
<p>No emails in ${folder}</p>
</div>
`;
return;
}
emailList.innerHTML = folderEmails.map(email => `
<div class="email-item ${!email.read ? 'unread' : ''}" onclick="openEmail(${email.id})">
<input type="checkbox" class="email-checkbox" onclick="event.stopPropagation(); toggleEmailSelection(${email.id})">
<i class="material-icons email-star ${email.starred ? 'starred' : ''}" onclick="event.stopPropagation(); toggleStar(${email.id})">
${email.starred ? 'star' : 'star_border'}
</i>
<div class="email-sender">${email.from}</div>
<div class="email-subject">
<strong>${email.subject}</strong>
<span style="color: #5f6368; font-weight: normal;"> - ${email.body.substring(0, 50)}...</span>
</div>
<div class="email-date">${formatDate(new Date(email.date))}</div>
</div>
`).join('');
} catch (error) {
console.error('Error loading emails:', error);
emailList.innerHTML = `
<div style="text-align: center; padding: 40px; color: var(--danger-color);">
<i class="material-icons" style="font-size: 48px; margin-bottom: 16px;">error</i>
<p>Error loading emails: ${error.message}</p>
<button class="btn btn-primary" onclick="loadEmails('${folder}')">Retry</button>
</div>
`;
}
}
function formatDate(date) {
const now = new Date();
const diff = now - date;
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
if (days === 0) {
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
} else if (days === 1) {
return 'Yesterday';
} else if (days < 7) {
return date.toLocaleDateString([], { weekday: 'short' });
} else {
return date.toLocaleDateString([], { month: 'short', day: 'numeric' });
}
}
function openEmail(emailId) {
const email = findEmailById(emailId);
if (!email) return;
// Mark as read
email.read = true;
loadEmails(currentFolder);
// Create email view modal
const modal = document.createElement('div');
modal.className = 'modal show';
modal.innerHTML = `
<div class="modal-content">
<div class="modal-header">
<div class="modal-title">${email.subject}</div>
<button class="close-button" onclick="this.closest('.modal').remove()">×</button>
</div>
<div class="modal-body">
<div style="margin-bottom: 16px; padding-bottom: 16px; border-bottom: 1px solid var(--border-color);">
<div><strong>From:</strong> ${email.from}</div>
<div><strong>To:</strong> ${email.to}</div>
<div><strong>Date:</strong> ${email.date.toLocaleString()}</div>
</div>
<div style="white-space: pre-wrap; line-height: 1.6;">${email.body}</div>
${email.attachments && email.attachments.length > 0 ? `
<div style="margin-top: 20px;">
<strong>Attachments:</strong>
${email.attachments.map(att => `
<div style="margin-top: 8px;">
<i class="material-icons" style="vertical-align: middle;">attach_file</i>
<span>${att.name}</span>
</div>
`).join('')}
</div>
` : ''}
</div>
<div class="modal-footer">
<div>
<button class="btn btn-secondary" onclick="replyToEmail(${emailId})">Reply</button>
<button class="btn btn-secondary" onclick="forwardEmail(${emailId})">Forward</button>
</div>
<div>
<button class="btn btn-secondary" onclick="deleteEmail(${emailId}); this.closest('.modal').remove()">Delete</button>
</div>
</div>
</div>
`;
document.body.appendChild(modal);
}
function findEmailById(id) {
for (const folder in emails) {
const email = emails[folder].find(e => e.id === id);
if (email) return email;
}
return null;
}
// toggleStar function moved to async version below
function toggleEmailSelection(emailId) {
if (selectedEmails.has(emailId)) {
selectedEmails.delete(emailId);
} else {
selectedEmails.add(emailId);
}
}
// deleteEmail function moved to async version below
function openComposeModal() {
document.getElementById('composeModal').classList.add('show');
document.getElementById('toField').focus();
}
function closeComposeModal() {
document.getElementById('composeModal').classList.remove('show');
clearComposeForm();
}
function clearComposeForm() {
document.getElementById('composeForm').reset();
attachments = [];
updateAttachmentList();
}
function handleFileSelect(event) {
const files = Array.from(event.target.files);
handleFiles(files);
}
function handleFiles(files) {
files.forEach(file => {
if (file.size > 25 * 1024 * 1024) { // 25MB limit
showNotification('File too large: ' + file.name, 'error');
return;
}
attachments.push({
name: file.name,
size: file.size,
type: file.type,
file: file
});
});
updateAttachmentList();
}
function updateAttachmentList() {
const attachmentList = document.getElementById('attachmentList');
if (attachments.length === 0) {
attachmentList.innerHTML = '';
return;
}
attachmentList.innerHTML = `
<div style="margin-top: 12px;">
<strong>Attached files:</strong>
${attachments.map((att, index) => `
<div style="display: flex; align-items: center; justify-content: space-between; padding: 8px; background-color: var(--secondary-color); border-radius: 4px; margin-top: 4px;">
<div>
<i class="material-icons" style="vertical-align: middle; margin-right: 8px;">attach_file</i>
<span>${att.name}</span>
<span style="color: #5f6368; font-size: 12px;"> (${formatFileSize(att.size)})</span>
</div>
<button type="button" onclick="removeAttachment(${index})" style="background: none; border: none; color: #5f6368; cursor: pointer;">
<i class="material-icons">close</i>
</button>
</div>
`).join('')}
</div>
`;
}
function removeAttachment(index) {
attachments.splice(index, 1);
updateAttachmentList();
}
function formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
async function sendEmail() {
const to = document.getElementById('toField').value;
const cc = document.getElementById('ccField').value;
const subject = document.getElementById('subjectField').value;
const body = document.getElementById('bodyField').value;
if (!to || !subject || !body) {
showNotification('Please fill in all required fields', 'error');
return;
}
try {
// Show sending state
const sendButton = document.querySelector('.btn-primary');
const originalText = sendButton.textContent;
sendButton.textContent = 'Sending...';
sendButton.disabled = true;
const emailData = {
to: to,
cc: cc || undefined,
subject: subject,
body: body,
attachments: attachments.map(att => ({
name: att.name,
size: att.size,
type: att.type,
content: att.content || '' // Base64 content
}))
};
const response = await fetch(`${API_BASE_URL}/emails/send`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(emailData)
});
const result = await response.json();
if (!response.ok) {
throw new Error(result.error || 'Failed to send email');
}
showNotification('Email sent successfully!', 'success');
closeComposeModal();
// If currently viewing sent folder, refresh the list
if (currentFolder === 'sent') {
loadEmails('sent');
}
} catch (error) {
console.error('Error sending email:', error);
showNotification('Failed to send email: ' + error.message, 'error');
// Reset button
const sendButton = document.querySelector('.btn-primary');
sendButton.textContent = 'Send';
sendButton.disabled = false;
}
}
// saveDraft function moved to async version below
function replyToEmail(emailId) {
const email = findEmailById(emailId);
if (!email) return;
// Close email view modal
document.querySelector('.modal').remove();
// Open compose modal with reply data
openComposeModal();
document.getElementById('toField').value = email.from;
document.getElementById('subjectField').value = email.subject.startsWith('Re: ') ? email.subject : 'Re: ' + email.subject;
document.getElementById('bodyField').value = `\n\n--- Original Message ---\nFrom: ${email.from}\nDate: ${email.date.toLocaleString()}\nSubject: ${email.subject}\n\n${email.body}`;
}
function forwardEmail(emailId) {
const email = findEmailById(emailId);
if (!email) return;
// Close email view modal
document.querySelector('.modal').remove();
// Open compose modal with forward data
openComposeModal();
document.getElementById('subjectField').value = email.subject.startsWith('Fwd: ') ? email.subject : 'Fwd: ' + email.subject;
document.getElementById('bodyField').value = `\n\n--- Forwarded Message ---\nFrom: ${email.from}\nTo: ${email.to}\nDate: ${email.date.toLocaleString()}\nSubject: ${email.subject}\n\n${email.body}`;
}
function searchEmails(query) {
if (!query.trim()) {
loadEmails(currentFolder);
return;
}
const emailList = document.getElementById('emailList');
const allEmails = Object.values(emails).flat();
const searchResults = allEmails.filter(email =>
email.subject.toLowerCase().includes(query.toLowerCase()) ||
email.body.toLowerCase().includes(query.toLowerCase()) ||
email.from.toLowerCase().includes(query.toLowerCase())
);
if (searchResults.length === 0) {
emailList.innerHTML = `
<div style="text-align: center; padding: 40px; color: #5f6368;">
<i class="material-icons" style="font-size: 48px; margin-bottom: 16px;">search</i>
<p>No emails found for "${query}"</p>
</div>
`;
return;
}
emailList.innerHTML = searchResults.map(email => `
<div class="email-item ${!email.read ? 'unread' : ''}" onclick="openEmail(${email.id})">
<input type="checkbox" class="email-checkbox" onclick="event.stopPropagation(); toggleEmailSelection(${email.id})">
<i class="material-icons email-star ${email.starred ? 'starred' : ''}" onclick="event.stopPropagation(); toggleStar(${email.id})">
${email.starred ? 'star' : 'star_border'}
</i>
<div class="email-sender">${email.from}</div>
<div class="email-subject">
<strong>${email.subject}</strong>
<span style="color: #5f6368; font-weight: normal;"> - ${email.body.substring(0, 50)}...</span>
</div>
<div class="email-date">${formatDate(email.date)}</div>
</div>
`).join('');
}
function showNotification(message, type = 'info') {
// Remove existing notifications
const existingNotification = document.querySelector('.notification');
if (existingNotification) {
existingNotification.remove();
}
const notification = document.createElement('div');
notification.className = 'notification';
notification.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
padding: 12px 20px;
border-radius: 4px;
color: white;
font-weight: 500;
z-index: 10000;
animation: slideIn 0.3s ease-out;
background-color: ${type === 'success' ? 'var(--success-color)' : type === 'error' ? 'var(--danger-color)' : 'var(--primary-color)'};
`;
notification.textContent = message;
document.body.appendChild(notification);
// Auto remove after 3 seconds
setTimeout(() => {
notification.style.animation = 'slideOut 0.3s ease-in';
setTimeout(() => notification.remove(), 300);
}, 3000);
}
async function checkAPIConnection() {
try {
const response = await fetch(`${API_BASE_URL}/health`);
const data = await response.json();
if (response.ok) {
console.log('✅ API connection successful');
showNotification('Connected to email server', 'success');
} else {
throw new Error('API health check failed');
}
} catch (error) {
console.error('❌ API connection failed:', error);
showNotification('Failed to connect to email server. Using offline mode.', 'error');
}
}
async function saveDraft() {
const to = document.getElementById('toField').value;
const cc = document.getElementById('ccField').value;
const subject = document.getElementById('subjectField').value;
const body = document.getElementById('bodyField').value;
if (!to && !subject && !body) {
showNotification('Nothing to save', 'error');
return;
}
try {
const draftData = {
to: to || '',
cc: cc || '',
subject: subject || '(No subject)',
body: body || '',
attachments: attachments.map(att => ({
name: att.name,
size: att.size,
type: att.type,
content: att.content || ''
}))
};
const response = await fetch(`${API_BASE_URL}/emails/draft`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(draftData)
});
const result = await response.json();
if (!response.ok) {
throw new Error(result.error || 'Failed to save draft');
}
showNotification('Draft saved', 'success');
closeComposeModal();
if (currentFolder === 'drafts') {
loadEmails('drafts');
}
} catch (error) {
console.error('Error saving draft:', error);
showNotification('Failed to save draft: ' + error.message, 'error');
}
}
async function toggleStar(emailId) {
try {
const email = findEmailById(emailId);
if (!email) return;
const response = await fetch(`${API_BASE_URL}/emails/${emailId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ starred: !email.starred })
});
if (response.ok) {
email.starred = !email.starred;
loadEmails(currentFolder);
}
} catch (error) {
console.error('Error toggling star:', error);
showNotification('Failed to update email', 'error');
}
}
async function deleteEmail(emailId) {
try {
const response = await fetch(`${API_BASE_URL}/emails/${emailId}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ folder: currentFolder })
});
if (response.ok) {
loadEmails(currentFolder);
showNotification('Email moved to trash');
} else {
throw new Error('Failed to delete email');
}
} catch (error) {
console.error('Error deleting email:', error);
showNotification('Failed to delete email', 'error');
}
}
async function searchEmails(query) {
if (!query.trim()) {
loadEmails(currentFolder);
return;
}
try {
const response = await fetch(`${API_BASE_URL}/emails/search?q=${encodeURIComponent(query)}`);
const data = await response.json();
if (!response.ok) {
throw new Error(data.error || 'Search failed');
}
const emailList = document.getElementById('emailList');
const searchResults = data.results || [];
if (searchResults.length === 0) {
emailList.innerHTML = `
<div style="text-align: center; padding: 40px; color: #5f6368;">
<i class="material-icons" style="font-size: 48px; margin-bottom: 16px;">search</i>
<p>No emails found for "${query}"</p>
</div>
`;
return;
}
emailList.innerHTML = searchResults.map(email => `
<div class="email-item ${!email.read ? 'unread' : ''}" onclick="openEmail(${email.id})">
<input type="checkbox" class="email-checkbox" onclick="event.stopPropagation(); toggleEmailSelection(${email.id})">
<i class="material-icons email-star ${email.starred ? 'starred' : ''}" onclick="event.stopPropagation(); toggleStar(${email.id})">
${email.starred ? 'star' : 'star_border'}
</i>
<div class="email-sender">${email.from}</div>
<div class="email-subject">
<strong>${email.subject}</strong>
<span style="color: #5f6368; font-weight: normal;"> - ${email.body.substring(0, 50)}...</span>
</div>
<div class="email-date">${formatDate(new Date(email.date))}</div>
</div>
`).join('');
} catch (error) {
console.error('Error searching emails:', error);
showNotification('Search failed: ' + error.message, 'error');
}
}
// Add CSS animations
const style = document.createElement('style');
style.textContent = `
@keyframes slideIn {
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
@keyframes slideOut {
from {
transform: translateX(0);
opacity: 1;
}
to {
transform: translateX(100%);
opacity: 0;
}
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
`;
document.head.appendChild(style);