diff --git a/app/api/mail/route.ts b/app/api/mail/route.ts index 9ce893d..5966b90 100644 --- a/app/api/mail/route.ts +++ b/app/api/mail/route.ts @@ -198,12 +198,14 @@ export async function GET() { }); f.once('end', () => { - console.log('Fetch completed, found emails:', emails.length); - imap.end(); - resolve(NextResponse.json({ - emails: emails.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()), + console.log('Emails before sending:', emails); + const response = { + emails: emails, mailUrl: process.env.NEXTCLOUD_URL ? `${process.env.NEXTCLOUD_URL}/apps/mail/` : null - })); + }; + console.log('Final response:', response); + imap.end(); + resolve(NextResponse.json(response)); }); }); }); diff --git a/components/email.tsx b/components/email.tsx index 90763a8..68ce6ef 100644 --- a/components/email.tsx +++ b/components/email.tsx @@ -41,6 +41,11 @@ export function Email() { const response = await fetch('/api/mail'); const data = await response.json(); + // Add debug logs + console.log('Raw API response:', data); + console.log('Type of data:', typeof data); + console.log('Type of data.emails:', data.emails ? typeof data.emails : 'undefined'); + if (!response.ok) { if (response.status === 401) { signIn(); @@ -49,17 +54,26 @@ export function Email() { throw new Error(data.error || 'Failed to fetch emails'); } + // Ensure data.emails is an array + const emailsArray = Array.isArray(data.emails) ? data.emails : []; + console.log('Emails array:', emailsArray); + // Transform the email data to match the expected format - const transformedEmails = (data.emails || []).map((email: any) => ({ - id: email.id, - subject: email.subject, - sender: { - name: email.from.split('<')[0].trim().replace(/"/g, '') || email.from, - email: (email.from.match(/<(.+)>/) || [])[1] || email.from - }, - date: email.date, - isUnread: !email.read - })); + const transformedEmails = emailsArray.map((email: any) => { + console.log('Processing email:', email); + return { + id: email.id || String(Math.random()), // Fallback ID if none exists + subject: email.subject || '(No subject)', + sender: { + name: email.from ? (email.from.split('<')[0].trim().replace(/"/g, '') || email.from) : 'Unknown', + email: email.from ? ((email.from.match(/<(.+)>/) || [])[1] || email.from) : 'unknown@example.com' + }, + date: email.date || new Date().toISOString(), + isUnread: !email.read + }; + }); + + console.log('Transformed emails:', transformedEmails); setEmails(transformedEmails); setMailUrl(data.mailUrl);