diff --git a/hooks/use-email-state.ts b/hooks/use-email-state.ts index 8c8d7f46..128dd6d3 100644 --- a/hooks/use-email-state.ts +++ b/hooks/use-email-state.ts @@ -138,6 +138,19 @@ export const useEmailState = () => { // Ensure all emails have proper account ID and folder format if (Array.isArray(data.emails)) { + // Log email dates for debugging + if (data.emails.length > 0) { + logEmailOp('EMAIL_DATES', `First few email dates before processing:`, + data.emails.slice(0, 5).map((e: any) => ({ + id: e.id.substring(0, 8), + subject: e.subject?.substring(0, 20), + date: e.date, + dateObj: new Date(e.date), + timestamp: new Date(e.date).getTime() + })) + ); + } + data.emails.forEach((email: Email) => { // If email doesn't have an accountId, set it to the effective one if (!email.accountId) { @@ -148,6 +161,22 @@ export const useEmailState = () => { if (email.folder && !email.folder.includes(':')) { email.folder = `${email.accountId}:${email.folder}`; } + + // Ensure date is a valid Date object (handle strings or timestamps) + if (email.date && !(email.date instanceof Date)) { + try { + // Convert to a proper Date object if it's a string or number + const dateObj = new Date(email.date); + // Verify it's a valid date + if (!isNaN(dateObj.getTime())) { + email.date = dateObj; + } + } catch (err) { + // If conversion fails, log and use current date as fallback + console.error(`Invalid date format for email ${email.id}: ${email.date}`); + email.date = new Date(); + } + } }); } diff --git a/lib/reducers/emailReducer.ts b/lib/reducers/emailReducer.ts index e8bbf802..e575f757 100644 --- a/lib/reducers/emailReducer.ts +++ b/lib/reducers/emailReducer.ts @@ -165,9 +165,53 @@ export function emailReducer(state: EmailState, action: EmailAction): EmailState } case 'SET_EMAILS': + // Sort emails by date (newest first) to ensure consistent sorting + // First make a copy to avoid mutating the input + const unsortedEmails = [...action.payload]; + + // For debugging - log a few emails before sorting + if (unsortedEmails.length > 0) { + console.log(`[EMAIL_REDUCER] Sorting ${unsortedEmails.length} emails`); + + // Log a sample of emails before sorting + console.log('[EMAIL_REDUCER] Sample emails before sorting:', + unsortedEmails.slice(0, 3).map(e => ({ + id: e.id.substring(0, 8), + subject: e.subject?.substring(0, 20), + date: e.date, + timestamp: new Date(e.date).getTime() + })) + ); + } + + // Sort by date, newest first + const sortedEmails = unsortedEmails.sort((a, b) => { + const dateA = new Date(a.date).getTime(); + const dateB = new Date(b.date).getTime(); + + // Handle invalid dates + if (isNaN(dateA) && isNaN(dateB)) return 0; + if (isNaN(dateA)) return 1; // Put invalid dates at the end + if (isNaN(dateB)) return -1; + + return dateB - dateA; // Newest first + }); + + // For debugging - log a few emails after sorting + if (sortedEmails.length > 0) { + console.log('[EMAIL_REDUCER] Sample emails after sorting:', + sortedEmails.slice(0, 3).map(e => ({ + id: e.id.substring(0, 8), + subject: e.subject?.substring(0, 20), + date: e.date, + timestamp: new Date(e.date).getTime() + })) + ); + } + return { ...state, - emails: action.payload, + emails: sortedEmails, isLoading: false };