diff --git a/app/api/courrier/route.ts b/app/api/courrier/route.ts index ca640004..a34dc20a 100644 --- a/app/api/courrier/route.ts +++ b/app/api/courrier/route.ts @@ -61,8 +61,7 @@ export async function GET(request: Request) { folder, page, perPage, - searchQuery, - accountId + accountId || undefined ); // The result is already cached in the getEmails function diff --git a/hooks/use-courrier.ts b/hooks/use-courrier.ts index f258dfc3..fc51f6f7 100644 --- a/hooks/use-courrier.ts +++ b/hooks/use-courrier.ts @@ -81,23 +81,14 @@ 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); - // Keep reference to the current page for this request - const currentRequestPage = page; - try { - // Build query params + // Construct query parameters const queryParams = new URLSearchParams({ folder: currentFolder, - page: currentRequestPage.toString(), + page: page.toString(), perPage: perPage.toString() }); @@ -105,21 +96,22 @@ export const useCourrier = () => { queryParams.set('search', searchQuery); } - // Add accountId if provided and not 'loading-account' - if (accountId && accountId !== 'all-accounts' && accountId !== 'loading-account') { + // Add accountId to query params if provided + if (accountId) { queryParams.set('accountId', accountId); - console.log(`Loading emails for account ${accountId} in folder ${currentFolder}`); } - // First try Redis cache with low timeout + // Try to get cached emails first + const currentRequestPage = page; const cachedEmails = await getCachedEmailsWithTimeout( - session.user.id, - currentFolder, - currentRequestPage, - perPage, - 100, + session.user.id, + accountId || 'default', + currentFolder, + currentRequestPage, + perPage, accountId && accountId !== 'all-accounts' && accountId !== 'loading-account' ? accountId : undefined ); + if (cachedEmails) { // Ensure cached data has emails array property if (Array.isArray(cachedEmails.emails)) { @@ -238,7 +230,7 @@ export const useCourrier = () => { setSelectedEmailIds([]); } } catch (err) { - console.error(`Error loading emails for page ${currentRequestPage}:`, err); + console.error(`Error loading emails for page ${page}:`, err); // Set emails to empty array on error to prevent runtime issues if (!isLoadMore) { setEmails([]);