diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 7ef80ff7..e1ea1135 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -525,8 +525,7 @@ export default function CourrierPage() { bcc: email.bcc, flags: email.flags || [], raw: email.body || '' - })) - .sort((a: Email, b: Email) => new Date(b.date).getTime() - new Date(a.date).getTime()); + })); // Only update unread count if we're in the Inbox folder if (currentView === 'INBOX') { @@ -536,13 +535,27 @@ export default function CourrierPage() { setUnreadCount(unreadInboxEmails); } + // Sort emails by date, ensuring most recent first + const sortedEmails = processedEmails.sort((a: Email, b: Email) => { + const dateA = new Date(a.date).getTime(); + const dateB = new Date(b.date).getTime(); + return dateB - dateA; // Most recent first + }); + if (isLoadMore) { - setEmails(prev => [...prev, ...processedEmails].sort((a: Email, b: Email) => - new Date(b.date).getTime() - new Date(a.date).getTime() - )); + // When loading more, merge with existing emails and re-sort + setEmails(prev => { + const combined = [...prev, ...sortedEmails]; + return combined.sort((a: Email, b: Email) => { + const dateA = new Date(a.date).getTime(); + const dateB = new Date(b.date).getTime(); + return dateB - dateA; // Most recent first + }); + }); setPage(prev => prev + 1); } else { - setEmails(processedEmails); + // For initial load or refresh, just use the sorted emails + setEmails(sortedEmails); setPage(1); } @@ -1157,7 +1170,7 @@ export default function CourrierPage() { ); // Add back the handleReply function - const handleReply = (type: 'reply' | 'reply-all' | 'forward') => { + const handleReply = async (type: 'reply' | 'reply-all' | 'forward') => { if (!selectedEmail) return; const getReplyTo = () => { @@ -1178,45 +1191,51 @@ export default function CourrierPage() { return subject.startsWith('Re:') ? subject : `Re: ${subject}`; }; - // Get the formatted original email content - const originalContent = getReplyBody(selectedEmail, type); - - // Create a clean structure with clear separation - const formattedContent = ` -
-- ${originalContent} --
+ ${decoded.html || decoded.text || ''} ++ `} +