diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 9dfcbd8b..8c5b15bd 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -767,6 +767,13 @@ export default function MailPage() { // Update handleEmailSelect to set selectedEmail correctly const handleEmailSelect = async (emailId: number) => { try { + // Find the email in the existing emails list + const emailToSelect = emails.find(email => email.id === emailId); + if (!emailToSelect) { + console.error('Email not found in list'); + return; + } + // Mark email as read const markReadResponse = await fetch(`/api/mail/mark-read`, { method: 'POST', @@ -783,25 +790,17 @@ export default function MailPage() { console.error('Failed to mark email as read'); } - // Fetch full email content - const response = await fetch(`/api/mail/${emailId}?folder=${currentView}`); - if (!response.ok) { - throw new Error('Failed to fetch email content'); - } - - const fullEmail = await response.json(); - - // Update the email in the list with full content + // Update the email in the list to mark it as read setEmails(prevEmails => prevEmails.map(email => email.id === emailId - ? { ...email, ...fullEmail, read: true } + ? { ...email, read: true } : email ) ); - // Update selected email - setSelectedEmail(fullEmail); + // Set the selected email from the existing list + setSelectedEmail(emailToSelect); } catch (error) { console.error('Error selecting email:', error);