diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 1d6aa97a..e2671363 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -582,6 +582,33 @@ export default function CourrierPage() { return emails.find(email => email.id === selectedEmail?.id); }; + // Add an effect to detect and fix stuck loading states + useEffect(() => { + let timeoutId: NodeJS.Timeout | null = null; + + // If we have emails but loading state is still true after emails load, it's likely stuck + if (emails.length > 0 && (loading || isLoadingInitial)) { + console.log('[DEBUG] Detected potential stuck loading state, setting recovery timeout'); + + // Set a timeout to automatically reset the loading state + timeoutId = setTimeout(() => { + // Double check if still stuck + if (emails.length > 0 && (loading || isLoadingInitial)) { + console.log('[DEBUG] Confirmed stuck loading state, auto-resetting'); + setLoading(false); + setIsLoadingInitial(false); + } + }, 3000); // 3 second timeout + } + + // Cleanup + return () => { + if (timeoutId) { + clearTimeout(timeoutId); + } + }; + }, [emails.length, loading, isLoadingInitial]); + // Single initialization effect that loads emails correctly on first page load useEffect(() => { const loadInitialData = async () => { @@ -817,8 +844,11 @@ export default function CourrierPage() { setError(err instanceof Error ? err.message : 'Failed to load data'); } finally { console.log('[DEBUG] Setting loading state to false in finally block'); - setLoading(false); - setIsLoadingInitial(false); + // Ensure we reset the loading state after a short delay to make sure React has processed state updates + setTimeout(() => { + setLoading(false); + setIsLoadingInitial(false); + }, 100); } }; @@ -1049,19 +1079,25 @@ export default function CourrierPage() { className="flex-1 overflow-y-auto" onScroll={handleScroll} > - {/* Only show loading if we don't have emails yet - this prevents stuck loading states */} - {(loading || isLoadingInitial) && filteredEmails.length === 0 ? ( + {/* Always show emails when available, regardless of loading state */} + {filteredEmails.length > 0 ? ( +
Loading emails...
-- {loading ? 'Loading state: true' : ''} - {isLoadingInitial ? 'Loading initial: true' : ''} -
@@ -1085,22 +1121,6 @@ export default function CourrierPage() {