diff --git a/app/mail/page.tsx b/app/mail/page.tsx index ff09bd0..c874f02 100644 --- a/app/mail/page.tsx +++ b/app/mail/page.tsx @@ -427,10 +427,8 @@ export default function MailPage() { throw new Error('Failed to fetch emails'); } const data = await response.json(); - console.log('Fetched emails:', data); // Debug log setEmails(data); } catch (err) { - console.error('Error fetching emails:', err); // Debug log setError(err instanceof Error ? err.message : 'An error occurred'); } finally { setLoading(false); @@ -453,7 +451,6 @@ export default function MailPage() { // Filter emails based on current view const filteredEmails = emails.filter(email => { - console.log('Filtering email:', email); // Debug log if (selectedAccount === 0) return true; return email.accountId === selectedAccount.toString(); }).filter(email => { @@ -575,54 +572,22 @@ export default function MailPage() { return emails.find(email => email.id === selectedEmail?.id); }; + // Add useEffect to load emails on mount + useEffect(() => { + loadEmails(); + }, []); + // Add account management functions const handleAddAccount = () => { // Implementation for adding a new account }; - const handleRemoveAccount = (accountId: number) => { - setAccounts(accounts.filter(acc => acc.id !== accountId)); - if (selectedAccount === accountId) { - setSelectedAccount(accounts[0].id); - } - }; - const handleEditAccount = (accountId: number, newName: string) => { setAccounts(accounts.map(acc => acc.id === accountId ? { ...acc, name: newName } : acc )); }; - // Handle reply - const handleReply = async (type: 'reply' | 'replyAll' | 'forward') => { - const selectedEmailData = getSelectedEmail(); - if (!selectedEmailData) return; - - setComposeOpen(true); - const subject = `${type === 'forward' ? 'Fwd: ' : 'Re: '}${selectedEmailData.subject}`; - let to = ''; - let content = ''; - - switch (type) { - case 'reply': - to = selectedEmailData.from; - content = `\n\nOn ${new Date(selectedEmailData.date).toLocaleString()}, ${selectedEmailData.fromName} wrote:\n> ${selectedEmailData.body.split('\n').join('\n> ')}`; - break; - case 'replyAll': - to = selectedEmailData.from; // You would also need to add CC recipients here - content = `\n\nOn ${new Date(selectedEmailData.date).toLocaleString()}, ${selectedEmailData.fromName} wrote:\n> ${selectedEmailData.body.split('\n').join('\n> ')}`; - break; - case 'forward': - content = `\n\n---------- Forwarded message ----------\nFrom: ${selectedEmailData.fromName} <${selectedEmailData.from}>\nDate: ${new Date(selectedEmailData.date).toLocaleString()}\nSubject: ${selectedEmailData.subject}\n\n${selectedEmailData.body}`; - break; - } - - // Update compose form state (you'll need to add these state variables) - setComposeSubject(subject); - setComposeRecipient(to); - setComposeContent(content); - }; - const handleEmailCheckbox = (e: React.ChangeEvent, emailId: string) => { e.stopPropagation(); if (e.target.checked) { @@ -632,75 +597,6 @@ export default function MailPage() { } }; - // Update the mock data with all required properties - const mockEmails: Email[] = [ - { - id: "1", - accountId: "1", - from: "john@example.com", - fromName: "John Doe", - to: "me@example.com", - subject: "Hello", - body: "This is a test email", - preview: "This is a test email", - category: "inbox", - date: new Date(), - read: false, - starred: false - } - ]; - - // Update the email action handlers to use string IDs - const handleMarkAsRead = (emailId: string, isRead: boolean) => { - setEmails(emails.map(email => - email.id === emailId ? { ...email, read: isRead } : email - )); - }; - - const handleDeleteEmail = (emailId: string) => { - setEmails(emails.filter(email => email.id !== emailId)); - setSelectedEmails(selectedEmails.filter(id => id !== emailId)); - }; - - // Update the bulk action handlers - const handleBulkAction = (action: 'delete' | 'mark-read' | 'mark-unread') => { - selectedEmails.forEach(emailId => { - const email = emails.find(e => e.id === emailId); - if (email) { - switch (action) { - case 'delete': - handleDeleteEmail(emailId); - break; - case 'mark-read': - handleMarkAsRead(emailId, true); - break; - case 'mark-unread': - handleMarkAsRead(emailId, false); - break; - } - } - }); - setSelectedEmails([]); - }; - - // Update the email selection handlers to use string types - const handleEmailSelection = (emailId: string) => { - setSelectedEmail(emails.find(e => e.id === emailId) || null); - }; - - const handleBulkSelection = (emailId: string) => { - if (selectedEmails.includes(emailId)) { - setSelectedEmails(selectedEmails.filter(id => id !== emailId)); - } else { - setSelectedEmails([...selectedEmails, emailId]); - } - }; - - // Add useEffect to load emails on mount - useEffect(() => { - loadEmails(); - }, []); // Empty dependency array means this runs once on mount - if (loading) { return (