From 33c6fe709a59a7fa802213198cb8847a8aee69e3 Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 25 Apr 2025 20:57:49 +0200 Subject: [PATCH] panel 2 courier api restore --- app/courrier/page.tsx | 105 +++++++++++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 28 deletions(-) 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 ? ( +
+ {filteredEmails.map((email) => renderEmailListItem(email))} + {(isLoadingMore || loading) && ( +
+
+ Refreshing... +
+ )} +
+ ) : isLoadingInitial || (loading && emails.length === 0) ? (

Loading emails...

-

- {loading ? 'Loading state: true' : ''} - {isLoadingInitial ? 'Loading initial: true' : ''} -

- ) : filteredEmails.length === 0 ? ( + ) : (

@@ -1085,22 +1121,6 @@ export default function CourrierPage() { Refresh Folder

- ) : ( -
- {filteredEmails.map((email) => renderEmailListItem(email))} - {isLoadingMore && ( -
-
-
- )} - {/* Show loading indicator at the bottom if we're still loading but have emails */} - {(loading || isLoadingInitial) && filteredEmails.length > 0 && ( -
-
- Refreshing email list... -
- )} -
)} @@ -1857,9 +1877,12 @@ export default function CourrierPage() { setError('Failed to load emails'); } finally { console.log('[DEBUG] Setting loading states to false in loadEmails finally block'); - setLoading(false); - setIsLoadingMore(false); - setIsLoadingInitial(false); + // Ensure we reset the loading state after a short delay to make sure React has processed state updates + setTimeout(() => { + setLoading(false); + setIsLoadingMore(false); + setIsLoadingInitial(false); + }, 100); } }; @@ -1969,6 +1992,32 @@ export default function CourrierPage() { fetchImapCredentials(); }, []); + // Add ref for tracking component mount status + const isComponentMounted = useRef(true); + + // Add a cleanup effect to reset states on unmount/remount + useEffect(() => { + // Reset isComponentMounted value on mount + isComponentMounted.current = true; + + // Reset loading states when component mounts + const timeoutId = setTimeout(() => { + if (isComponentMounted.current && (loading || isLoadingInitial)) { + console.log('[DEBUG] Reset loading states on mount'); + setLoading(false); + setIsLoadingInitial(false); + } + }, 5000); // Longer timeout to avoid race conditions + + // Return cleanup function + return () => { + console.log('[DEBUG] Component unmounting, clearing states'); + isComponentMounted.current = false; + clearTimeout(timeoutId); + // No need to set states during unmount as component is gone + }; + }, []); // Empty dependency array + if (error) { return (