diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 9bec341a..c0efc0be 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -686,56 +686,50 @@ export default function CourrierPage() { // Set the selected email first to show preview immediately setSelectedEmail(email); + // Fetch the full email content + const response = await fetch(`/api/mail/${emailId}`); + if (!response.ok) { + throw new Error('Failed to fetch full email content'); + } + + const fullEmail = await response.json(); + + // Update the email in the list and selected email with full content + setEmails(prevEmails => prevEmails.map(email => + email.id === emailId + ? { ...email, body: fullEmail.body } + : email + )); + + setSelectedEmail(prev => prev ? { ...prev, body: fullEmail.body } : prev); + + // Try to mark as read in the background try { - // Fetch the full email content - const response = await fetch(`/api/courrier/email/${emailId}`); - if (!response.ok) { - throw new Error('Failed to fetch full email content'); - } - - const fullEmail = await response.json(); - - // Update the email in the list and selected email with full content - setEmails(prevEmails => prevEmails.map(email => - email.id === emailId - ? { ...email, body: fullEmail.body } - : email - )); - - setSelectedEmail(prev => prev ? { ...prev, body: fullEmail.body } : prev); + const markReadResponse = await fetch(`/api/mail/mark-read`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + emailId, + isRead: true, + }), + }); - // Try to mark as read in the background - try { - const markReadResponse = await fetch(`/api/courrier/mark-read`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - emailId, - isRead: true, - }), - }); - - if (markReadResponse.ok) { - // Only update the emails list if the API call was successful - setEmails((prevEmails: Email[]) => - prevEmails.map((email: Email): Email => - email.id === emailId - ? { ...email, read: true } - : email - ) - ); - } else { - console.error('Failed to mark email as read:', await markReadResponse.text()); - } - } catch (error) { - console.error('Error marking email as read:', error); + if (markReadResponse.ok) { + // Only update the emails list if the API call was successful + setEmails((prevEmails: Email[]) => + prevEmails.map((email: Email): Email => + email.id === emailId + ? { ...email, read: true } + : email + ) + ); + } else { + console.error('Failed to mark email as read:', await markReadResponse.text()); } } catch (error) { - console.error('Error fetching full email content:', error); - // If we can't fetch the full content, at least show what we have - setSelectedEmail(email); + console.error('Error marking email as read:', error); } }; @@ -1419,12 +1413,6 @@ export default function CourrierPage() { const handleReply = (type: 'reply' | 'reply-all' | 'forward') => { if (!selectedEmail) return; - // Ensure we have the email content - if (!selectedEmail.body) { - console.error('No email content available for reply/forward'); - return; - } - const getReplyTo = () => { if (type === 'forward') return ''; return selectedEmail.from;