diff --git a/hooks/use-courrier.ts b/hooks/use-courrier.ts index 0eb86f84..e5601ef0 100644 --- a/hooks/use-courrier.ts +++ b/hooks/use-courrier.ts @@ -80,6 +80,12 @@ export const useCourrier = () => { const loadEmails = useCallback(async (isLoadMore = false, accountId?: string) => { if (!session?.user?.id) return; + // Skip loading if accountId is 'loading-account' + if (accountId === 'loading-account') { + console.log('Skipping email load for loading account'); + return; + } + setIsLoading(true); setError(null); @@ -98,8 +104,8 @@ export const useCourrier = () => { queryParams.set('search', searchQuery); } - // Add accountId if provided - if (accountId && accountId !== 'all-accounts') { + // Add accountId if provided and not 'loading-account' + if (accountId && accountId !== 'all-accounts' && accountId !== 'loading-account') { queryParams.set('accountId', accountId); } @@ -110,7 +116,7 @@ export const useCourrier = () => { currentRequestPage, perPage, 100, - accountId && accountId !== 'all-accounts' ? accountId : undefined + accountId && accountId !== 'all-accounts' && accountId !== 'loading-account' ? accountId : undefined ); if (cachedEmails) { // Ensure cached data has emails array property diff --git a/lib/services/prefetch-service.ts b/lib/services/prefetch-service.ts index 172a1933..3ea4381f 100644 --- a/lib/services/prefetch-service.ts +++ b/lib/services/prefetch-service.ts @@ -62,6 +62,12 @@ export async function getCachedEmailsWithTimeout( timeoutMs: number = 100, accountId?: string ): Promise { + // Skip cache if accountId is 'loading-account' + if (accountId === 'loading-account') { + console.log(`Skipping cache for loading account`); + return null; + } + return new Promise((resolve) => { const timeoutId = setTimeout(() => { console.log(`Cache access timeout for ${userId}:${folder}:${page}:${perPage}${accountId ? ` for account ${accountId}` : ''}`); @@ -73,25 +79,7 @@ export async function getCachedEmailsWithTimeout( clearTimeout(timeoutId); if (result) { console.log(`Using cached data for ${userId}:${folder}:${page}:${perPage}${accountId ? ` for account ${accountId}` : ''}`); - - // Validate and normalize the data structure - if (typeof result === 'object') { - // Make sure we have an emails array - if (!result.emails && Array.isArray(result)) { - // If result is an array, convert to proper structure - resolve({ emails: result }); - } else if (!result.emails) { - // If no emails property, add empty array - resolve({ ...result, emails: [] }); - } else { - // Normal case, return as is - resolve(result); - } - } else { - // Invalid data, return null - console.warn('Invalid cached data format:', result); - resolve(null); - } + resolve(result); } else { resolve(null); }