diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index e0820d22..f7c7b628 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -246,15 +246,28 @@ export default function CourrierPage() { const nextPage = page + 1; setPage(nextPage); + console.log(`Requesting page ${nextPage} for folder ${currentFolder}`); + + // Immediately trigger a load for this page rather than relying on the useEffect + // This helps ensure we get the data faster + loadEmails(true).catch(error => { + console.error(`Error loading page ${nextPage}:`, error); + }); + // Also prefetch additional pages to make scrolling smoother if (session?.user?.id) { - // Prefetch next 2 pages beyond the current next page - prefetchFolderEmails(session.user.id, currentFolder, 2, nextPage + 1).catch(err => { - console.error(`Error prefetching additional pages for ${currentFolder}:`, err); - }); + // Prefetch next page beyond the one we're loading + const pagesToPrefetch = 2; // Prefetch 2 pages ahead + + // Small delay to let the current request start first + setTimeout(() => { + console.log(`Prefetching pages ${nextPage + 1} to ${nextPage + pagesToPrefetch}`); + prefetchFolderEmails(session.user.id, currentFolder, pagesToPrefetch, nextPage + 1) + .catch(err => { + console.error(`Error prefetching additional pages for ${currentFolder}:`, err); + }); + }, 200); } - - // Note: loadEmails will be called automatically due to the page dependency in useEffect } }; diff --git a/components/email/EmailList.tsx b/components/email/EmailList.tsx index 94480b2c..4ecf373f 100644 --- a/components/email/EmailList.tsx +++ b/components/email/EmailList.tsx @@ -63,7 +63,7 @@ export default function EmailList({ // If near bottom (within 200px) and more emails are available, load more // Added additional checks to prevent loading loop - const isNearBottom = scrollHeight - scrollTop - clientHeight < 200; + const isNearBottom = scrollHeight - scrollTop - clientHeight < 300; // Increased detection area if (isNearBottom && hasMoreEmails && !isLoading && !isLoadingMore) { setIsLoadingMore(true); @@ -71,13 +71,16 @@ export default function EmailList({ scrollTimeoutRef.current = setTimeout(() => { // Clear the timeout reference before loading scrollTimeoutRef.current = null; + + console.log('Loading more emails from scroll trigger'); onLoadMore(); // Reset loading state after a delay setTimeout(() => { + console.log('Resetting loading more state after timeout'); setIsLoadingMore(false); - }, 1500); // Increased from 1000ms to 1500ms to prevent quick re-triggering - }, 200); // Increased from 100ms to 200ms for better debouncing + }, 3000); // Increased from 1500ms to 3000ms to allow more time for loading + }, 300); // Increased from 200ms to 300ms for better debouncing } }, [hasMoreEmails, isLoading, isLoadingMore, onLoadMore]); @@ -105,6 +108,25 @@ export default function EmailList({ prevEmailsLengthRef.current = emails.length; }, [emails.length, scrollPosition, isLoading]); + // Add safety mechanism to reset loading state if we get stuck + useEffect(() => { + // If we have more emails now but still in loading state, reset it + if (emails.length > prevEmailsLengthRef.current && isLoadingMore) { + console.log('Safety reset: Clearing loading state after emails updated'); + setIsLoadingMore(false); + } + + // Add a timeout-based safety mechanism + const safetyTimeout = setTimeout(() => { + if (isLoadingMore) { + console.log('Safety timeout: Resetting stuck loading state'); + setIsLoadingMore(false); + } + }, 5000); + + return () => clearTimeout(safetyTimeout); + }, [emails.length, isLoadingMore]); + // Add listener for custom reset scroll event useEffect(() => { const handleResetScroll = () => { @@ -258,14 +280,18 @@ export default function EmailList({ {/* Loading indicator */} {(isLoading || isLoadingMore) && (
- + + Loading more emails...
)} {/* Load more button - only show when near bottom but not auto-loading */} {hasMoreEmails && !isLoading && !isLoadingMore && (