diff --git a/app/mail/page.tsx b/app/mail/page.tsx index 0b53882..28fa8c0 100644 --- a/app/mail/page.tsx +++ b/app/mail/page.tsx @@ -427,23 +427,37 @@ export default function MailPage() { throw new Error('Failed to fetch emails'); } const data = await response.json(); - console.log('API Response:', data); // Debug log - // Ensure data is an array and has the expected structure + console.log('Raw API Response:', data); // Debug raw response + + // Handle different possible response formats + let emailData = []; if (Array.isArray(data)) { - const validEmails = data.filter(email => - email && - typeof email.id === 'string' && - typeof email.accountId === 'string' && - typeof email.from === 'string' && - typeof email.subject === 'string' && - email.date instanceof Date - ); - console.log('Valid emails:', validEmails); // Debug log - setEmails(validEmails); - } else { - console.warn('API returned non-array data:', data); - setEmails([]); + emailData = data; + } else if (data && typeof data === 'object') { + // If data is an object, try to extract emails from it + if (data.emails) { + emailData = data.emails; + } else if (data.messages) { + emailData = data.messages; + } else { + // Try to convert object values to array + emailData = Object.values(data); + } } + + console.log('Processed email data:', emailData); // Debug processed data + + // Ensure all dates are Date objects + const processedEmails = emailData.map(email => ({ + ...email, + date: email.date ? new Date(email.date) : new Date(), + read: email.read || false, + starred: email.starred || false, + category: email.category || 'inbox' + })); + + console.log('Final processed emails:', processedEmails); // Debug final data + setEmails(processedEmails); } catch (err) { console.error('Error fetching emails:', err); setError(err instanceof Error ? err.message : 'An error occurred');