From df60de0c8eaea81f9fe72807916865f99deb34eb Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 25 Apr 2025 12:34:37 +0200 Subject: [PATCH] panel 2 courier api --- app/api/courrier/route.ts | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/app/api/courrier/route.ts b/app/api/courrier/route.ts index d77d2185..5ae450fb 100644 --- a/app/api/courrier/route.ts +++ b/app/api/courrier/route.ts @@ -4,6 +4,25 @@ import { getServerSession } from 'next-auth'; import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import { prisma } from '@/lib/prisma'; +// Add caching for better performance +const cache = new Map(); + +const getCachedEmail = (id: string) => cache.get(id); +const setCachedEmail = (id: string, email: Email) => cache.set(id, email); + +// Add debouncing for folder changes +const debouncedLoadEmails = debounce((folder: string) => { + loadEmails(folder); +}, 300); + +// Add infinite scroll +const handleScroll = (e: React.UIEvent) => { + const { scrollTop, scrollHeight, clientHeight } = e.currentTarget; + if (scrollHeight - scrollTop === clientHeight) { + // Load more emails + } +}; + export async function GET(request: Request) { try { const session = await getServerSession(authOptions); @@ -33,6 +52,7 @@ export async function GET(request: Request) { const folder = url.searchParams.get('folder') || 'INBOX'; const page = parseInt(url.searchParams.get('page') || '1'); const limit = parseInt(url.searchParams.get('limit') || '20'); + const preview = url.searchParams.get('preview') === 'true'; // Calculate start and end sequence numbers const start = (page - 1) * limit + 1; @@ -72,11 +92,20 @@ 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 both metadata and preview content const messages = await client.fetch(`${adjustedStart}:${adjustedEnd}`, { envelope: true, flags: true, - bodyStructure: true + bodyStructure: true, + // Only fetch preview content if requested + ...(preview ? { + bodyParts: ['TEXT'], + bodyPartsOptions: { + TEXT: { + maxLength: 1000 // Limit preview length + } + } + } : {}) }); for await (const message of messages) { @@ -91,7 +120,9 @@ 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), + // Include preview content if available + preview: preview ? message.bodyParts?.TEXT?.toString() : null }); } }