diff --git a/components/email/EmailList.tsx b/components/email/EmailList.tsx index dbdcf015..3582fc4e 100644 --- a/components/email/EmailList.tsx +++ b/components/email/EmailList.tsx @@ -1,6 +1,6 @@ 'use client'; -import React, { useState } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import { Loader2, Mail, Search, X } from 'lucide-react'; import { Email } from '@/hooks/use-courrier'; import EmailListItem from './EmailListItem'; @@ -43,6 +43,15 @@ export default function EmailList({ }: EmailListProps) { const [scrollPosition, setScrollPosition] = useState(0); const [searchQuery, setSearchQuery] = useState(''); + const listRef = useRef(null); + const isLoadingMoreRef = useRef(false); + + // Reset loading state when isLoading changes + useEffect(() => { + if (!isLoading) { + isLoadingMoreRef.current = false; + } + }, [isLoading]); // Handle scroll to detect when user reaches the bottom const handleScroll = (event: React.UIEvent) => { @@ -54,13 +63,37 @@ export default function EmailList({ // Calculate how close to the bottom we are (in pixels) const distanceToBottom = scrollHeight - scrollTop - clientHeight; - // More aggressive threshold - load more when within 300px of bottom - // Only trigger if we have more emails and aren't already loading - if (distanceToBottom < 300 && hasMoreEmails && !isLoading) { - console.log(`[EMAIL_LIST] Near bottom (${distanceToBottom}px), loading more emails`); + // Debug logging to help diagnose scrolling issues + console.log(`[EMAIL_LIST] Scroll metrics - scrollHeight: ${scrollHeight}, scrollTop: ${scrollTop}, clientHeight: ${clientHeight}, distanceToBottom: ${distanceToBottom}`); + + // Only trigger if we have more emails, aren't already loading, and we're near the bottom + const LOAD_MORE_THRESHOLD = 400; // Increased threshold for more reliable loading + + if (distanceToBottom < LOAD_MORE_THRESHOLD && hasMoreEmails && !isLoading && !isLoadingMoreRef.current) { + console.log(`[EMAIL_LIST] Near bottom (${distanceToBottom}px), loading more emails. hasMoreEmails: ${hasMoreEmails}, isLoading: ${isLoading}`); + isLoadingMoreRef.current = true; // Prevent multiple load more triggers onLoadMore(); } }; + + // Also check on component mount and email changes if we need to load more + useEffect(() => { + if (!listRef.current) return; + + const checkScroll = () => { + const { scrollHeight, clientHeight } = listRef.current as HTMLDivElement; + + // If the content doesn't fill the container, and we have more to load + if (scrollHeight <= clientHeight && hasMoreEmails && !isLoading && !isLoadingMoreRef.current) { + console.log("[EMAIL_LIST] Content doesn't fill container, loading more emails"); + isLoadingMoreRef.current = true; + onLoadMore(); + } + }; + + // Check after render and when emails change + requestAnimationFrame(checkScroll); + }, [emails, hasMoreEmails, isLoading, onLoadMore]); // Handle search const handleSearch = (e: React.FormEvent) => { @@ -149,7 +182,8 @@ export default function EmailList({ )}
@@ -171,9 +205,18 @@ export default function EmailList({ /> ))} - {isLoading && emails.length > 0 && ( -
-
+ {/* Make loading indicator more visible */} + {isLoading && ( +
+ + Loading more emails... +
+ )} + + {/* Visual indicator when all emails are loaded */} + {!hasMoreEmails && emails.length > 0 && ( +
+ End of emails in this folder
)}
diff --git a/lib/reducers/emailReducer.ts b/lib/reducers/emailReducer.ts index 9deafebb..2466e7dc 100644 --- a/lib/reducers/emailReducer.ts +++ b/lib/reducers/emailReducer.ts @@ -227,6 +227,9 @@ export function emailReducer(state: EmailState, action: EmailAction): EmailState // Filter out any duplicates before appending const newEmails = action.payload.filter(email => !existingIds.has(email.id)); + // Log appending for debugging + console.log(`[EMAIL_REDUCER] Appending ${newEmails.length} new emails to existing ${state.emails.length} emails`); + // Combine and sort emails by date (newest first) const combinedEmails = [...state.emails, ...newEmails].sort( (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()