From 64c9b1509753ba8d89d9278d96502ef31b4b143d Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 25 Apr 2025 11:09:24 +0200 Subject: [PATCH] panel 2 courier --- app/api/courrier/route.ts | 20 +++++++++++-- app/courrier/page.tsx | 61 +++++++++++++++++++++------------------ 2 files changed, 50 insertions(+), 31 deletions(-) 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) => (
handleEmailSelect(email.id)} @@ -1070,31 +1077,29 @@ export default function CourrierPage() { />
-
-
- - {email.fromName || email.from} - - {!email.read && ( - - )} -
- +
+

+ {email.subject || '(No subject)'} +

+ {formatDate(new Date(email.date))}
-

- {email.subject || '(No subject)'} -

- -
-
- {email.starred && ( - - )} - {email.attachments && email.attachments.length > 0 && ( - - )} +
+ + {email.fromName || email.from} + + {!email.read && ( + + )} +
+
+ {email.content ? ( + + ) : ( + 'No content available' + )} +
); @@ -1354,13 +1359,13 @@ export default function CourrierPage() { ); }; + // Update searchEmails to just set the search query since filteredEmails is computed by useMemo const searchEmails = (query: string) => { - setSearchQuery(query.trim()); + setSearchQuery(query); }; const handleSearchChange = (e: React.ChangeEvent) => { - const query = e.target.value; - setSearchQuery(query); + searchEmails(e.target.value); }; const renderEmailPreview = (email: Email) => {