diff --git a/app/api/courrier/[id]/route.ts b/app/api/courrier/[id]/route.ts index 4c348a9f..8b2e6301 100644 --- a/app/api/courrier/[id]/route.ts +++ b/app/api/courrier/[id]/route.ts @@ -1,3 +1,11 @@ +/* + * NOTE: This endpoint is now mostly for backward compatibility. + * The main email list API (/api/courrier) now fetches full email content, + * so individual email fetching is typically not needed. + * This is kept for cases where individual email access is still required + * or when using older client code. + */ + import { NextResponse } from 'next/server'; import { ImapFlow } from 'imapflow'; import { getServerSession } from 'next-auth'; diff --git a/app/api/courrier/route.ts b/app/api/courrier/route.ts index 08ad5eff..103881ea 100644 --- a/app/api/courrier/route.ts +++ b/app/api/courrier/route.ts @@ -98,12 +98,11 @@ 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'; const skipCache = url.searchParams.get('skipCache') === 'true'; - console.log('Request parameters:', { folder, page, limit, preview, skipCache }); + console.log('Request parameters:', { folder, page, limit, skipCache }); // Generate cache key based on request parameters - const cacheKey = `${session.user.id}:${folder}:${page}:${limit}:${preview}`; + const cacheKey = `${session.user.id}:${folder}:${page}:${limit}:full`; // Check cache first if not explicitly skipped if (!skipCache && emailListCache[cacheKey]) { @@ -176,18 +175,14 @@ export async function GET(request: Request) { const adjustedEnd = Math.min(end, mailbox.exists); console.log('Adjusted fetch range:', { adjustedStart, adjustedEnd }); - // Fetch both metadata and preview content + // Fetch both metadata AND full content const fetchOptions: any = { envelope: true, flags: true, - bodyStructure: true + bodyStructure: true, + source: true // Include full email source }; - // Only fetch preview content if requested - if (preview) { - fetchOptions.bodyParts = ['TEXT']; - } - console.log('Fetching messages with options:', fetchOptions); const fetchPromises = []; for (let i = adjustedStart; i <= adjustedEnd; i++) { @@ -213,14 +208,10 @@ 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: message.source?.toString() || '' // Include full email content }; - // Include preview content if available - if (preview && message.bodyParts && message.bodyParts.has('TEXT')) { - emailData.preview = message.bodyParts.get('TEXT')?.toString() || null; - } - result.push(emailData); } } catch (fetchError) { diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 445ded7d..7ab20117 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -688,44 +688,20 @@ export default function CourrierPage() { return account ? account.color : 'bg-gray-500'; }; - // Update handleEmailSelect to set selectedEmail correctly and ensure content is loaded + // Update handleEmailSelect to use the full content that's already in the emails data const handleEmailSelect = async (emailId: string) => { try { setContentLoading(true); - // Find the email in the current list first - const emailInList = emails.find(email => email.id === emailId); + // Find the email in the current list + const selectedEmail = emails.find(email => email.id === emailId); - // Set selected email immediately with what we have - if (emailInList) { - setSelectedEmail(emailInList); + if (!selectedEmail) { + throw new Error('Email not found in list'); } - // Fetch the full email content - const response = await fetch(`/api/courrier/${emailId}`); - - if (!response.ok) { - throw new Error('Failed to fetch full email content'); - } - - const fullEmail = await response.json(); - console.log('Fetched email content:', fullEmail); - - // Create a complete email object by combining what we have - const completeEmail = { - ...(emailInList || {}), - ...fullEmail, - id: emailId, - content: fullEmail.content || '', - }; - - // Update the email in the list - setEmails(prevEmails => prevEmails.map(email => - email.id === emailId ? completeEmail : email - )); - - // Update the selected email - setSelectedEmail(completeEmail); + // Set selected email from our existing data (which now includes full content) + setSelectedEmail(selectedEmail); // Try to mark as read in the background try { @@ -745,8 +721,8 @@ export default function CourrierPage() { console.error('Error marking email as read:', error); } } catch (error) { - console.error('Error fetching email:', error); - setError('Failed to load email content. Please try again.'); + console.error('Error selecting email:', error); + setError('Failed to select email. Please try again.'); } finally { setContentLoading(false); } @@ -1506,7 +1482,7 @@ export default function CourrierPage() { ); }; - // Keep the loadEmails function for other parts of the code to use + // Update loadEmails to store the full content from the API response const loadEmails = async (isLoadMore = false) => { try { // Skip if already loading @@ -1567,7 +1543,7 @@ export default function CourrierPage() { fromName: email.fromName || email.from?.split('@')[0] || '', to: email.to || '', subject: email.subject || '(No subject)', - content: email.preview || '', + content: email.content || '', // Full content is now included from the API date: email.date || new Date().toISOString(), read: email.read || false, starred: email.starred || false,