diff --git a/app/api/courrier/route.ts b/app/api/courrier/route.ts index d77d2185..e5ab7b8b 100644 --- a/app/api/courrier/route.ts +++ b/app/api/courrier/route.ts @@ -72,14 +72,23 @@ export async function GET(request: Request) { const adjustedStart = Math.min(start, mailbox.exists); const adjustedEnd = Math.min(end, mailbox.exists); - // Fetch messages from the current folder + // Fetch messages from the current folder with content const messages = await client.fetch(`${adjustedStart}:${adjustedEnd}`, { envelope: true, flags: true, - bodyStructure: true + bodyStructure: true, + source: true, // Get the full email source + bodyParts: ['text/plain', 'text/html'] // Get both text and HTML content }); for await (const message of messages) { + // Get the email content + let content = ''; + if (message.bodyParts) { + // Prefer HTML content if available + content = message.bodyParts.get('text/html')?.toString() || message.bodyParts.get('text/plain')?.toString() || ''; + } + result.push({ id: message.uid, from: message.envelope.from?.[0]?.address || '', @@ -91,9 +100,14 @@ export async function GET(request: Request) { starred: message.flags.has('\\Flagged'), folder: mailbox.path, hasAttachments: message.bodyStructure?.type === 'multipart', - flags: Array.from(message.flags) + flags: Array.from(message.flags), + content: content, + source: message.source?.toString() || '' // Include full email source for parsing }); } + + // Sort results by date, most recent first + result.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()); } return NextResponse.json({ diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 75beecb4..5ecad122 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -798,7 +798,14 @@ export default function CourrierPage() { // Sort emails by date (most recent first) const sortedEmails = useMemo(() => { return [...emails].sort((a, b) => { - return new Date(b.date).getTime() - new Date(a.date).getTime(); + const dateA = new Date(a.date); + const dateB = new Date(b.date); + // First sort by date + const dateDiff = dateB.getTime() - dateA.getTime(); + if (dateDiff !== 0) return dateDiff; + + // If dates are equal, maintain stable order using email ID + return Number(b.id) - Number(a.id); }); }, [emails]); @@ -819,7 +826,7 @@ export default function CourrierPage() { email.subject.toLowerCase().includes(query) || email.from.toLowerCase().includes(query) || email.to.toLowerCase().includes(query) || - email.content.toLowerCase().includes(query) + (email.content || '').toLowerCase().includes(query) ); }, [sortedEmails, searchQuery]); @@ -1056,7 +1063,7 @@ export default function CourrierPage() { const renderEmailListItem = (email: Email) => (