From af18853d30db41b866541b514142a8cf79b8a8db Mon Sep 17 00:00:00 2001 From: alma Date: Wed, 30 Apr 2025 17:24:07 +0200 Subject: [PATCH] courrier formatting --- hooks/use-email-state.ts | 116 +++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 52 deletions(-) diff --git a/hooks/use-email-state.ts b/hooks/use-email-state.ts index 32e90b0f..84d6f6a1 100644 --- a/hooks/use-email-state.ts +++ b/hooks/use-email-state.ts @@ -30,6 +30,15 @@ export const useEmailState = () => { const { data: session } = useSession(); const { toast } = useToast(); + // Refs to track state + const updateUnreadTimerRef = useRef(null); + const lastEmailViewedRef = useRef(null); + const failedFetchCountRef = useRef(0); + const lastFolderRef = useRef(null); + const lastPageLoadedRef = useRef(0); + const prevFolderRef = useRef(null); + const loadMoreTriggerTimeRef = useRef(0); + // Expose dispatch function to window for external components useEffect(() => { // Make dispatch available globally for older code @@ -621,28 +630,31 @@ export const useEmailState = () => { // Handle loading more emails const handleLoadMore = useCallback(() => { - // DEBUG: Log current state - console.log(`[DEBUG-LOAD_MORE] Current state - page: ${state.page}, totalPages: ${state.totalPages}, isLoading: ${state.isLoading}`); - - // CRITICAL: Don't try to load more if we're already at the last page - // This prevents unnecessary API calls when scrolling up and down at the end of the list - if (state.page >= state.totalPages && state.totalPages > 0) { - console.log(`[DEBUG-LOAD_MORE] Already at last page (${state.page}/${state.totalPages}), not loading more`); + logEmailOp('LOAD_MORE', `Current state - page: ${state.page}, totalPages: ${state.totalPages}, isLoading: ${state.isLoading}`); + + // Skip if we're already at the last page + if (state.page >= state.totalPages) { + logEmailOp('LOAD_MORE', 'Already at the last page, skipping load more'); return; } - + + // Skip if we're already loading if (state.isLoading) { - logEmailOp('LOAD_MORE', `Skipping load more request - already loading`); - console.log(`[DEBUG-LOAD_MORE] Skipping because isLoading is true`); + logEmailOp('LOAD_MORE', 'Already loading, skipping load more request'); + return; + } + + // Debounce: prevent multiple triggers within 1 second + const now = Date.now(); + if (now - loadMoreTriggerTimeRef.current < 1000) { + logEmailOp('LOAD_MORE', 'Debouncing load more request'); return; } - // Normal case - load the next page - console.log(`[DEBUG-LOAD_MORE] Incrementing page from ${state.page} to ${state.page + 1}`); - logEmailOp('LOAD_MORE', `Loading more emails: page ${state.page + 1}/${state.totalPages || '?'}`); - dispatch({ type: 'INCREMENT_PAGE' }); - // The actual loading will be handled by the useEffect that watches page changes - }, [state.page, state.totalPages, state.isLoading, logEmailOp]); + // Update the trigger time and load more + loadMoreTriggerTimeRef.current = now; + dispatch({ type: 'SET_PAGE', payload: state.page + 1 }); + }, [state.page, state.totalPages, state.isLoading, logEmailOp, dispatch]); // Effect to load emails when folder changes useEffect(() => { @@ -657,50 +669,50 @@ export const useEmailState = () => { } }, [session?.user?.id, state.currentFolder, loadEmails, logEmailOp]); - // Reference to track the last page loaded to prevent redundant loads - const lastPageLoadedRef = useRef(0); - // Effect to load more emails when page changes useEffect(() => { - if (session?.user?.id && state.page > 1) { - // Skip if this page was already loaded - if (lastPageLoadedRef.current === state.page) { - console.log(`[DEBUG-PAGE_EFFECT] Skipping reload of already loaded page ${state.page}`); - return; - } - - // Debug log - console.log(`[DEBUG-PAGE_EFFECT] Page changed to ${state.page}, calling loadEmails`); - - // Prevent duplicate effect triggers with ref tracking - const currentEffectPage = state.page; - - // CRITICAL: We need to avoid triggering duplicate loads - // Check if we're already loading to prevent duplicate calls - if (state.isLoading) { - console.log(`[DEBUG-PAGE_EFFECT] Skipping loadEmails - already loading`); - return; - } - - // Extract account ID for consistency - const { effectiveAccountId } = normalizeFolderAndAccount(state.currentFolder); - - logEmailOp('PAGINATION', `Loading page ${state.page} for ${state.currentFolder} with account ${effectiveAccountId}`); - - // Load more emails with the correct account ID - loadEmails(true, effectiveAccountId); - - // Update the last page loaded reference - lastPageLoadedRef.current = state.page; + // Skip if no user ID + if (!session?.user?.id) return; + + // Skip if still on page 1 or the current folder is empty + if (state.page <= 1 || !state.currentFolder) return; + + // CRITICAL FIX: Don't run this effect at all if we're already loading + if (state.isLoading) { + console.log(`[DEBUG-PAGE_EFFECT] Skipping effect execution entirely - already loading`); + return; } - // Reset the lastPageLoadedRef when folder changes + // Debug log + console.log(`[DEBUG-PAGE_EFFECT] Page effect running for page ${state.page}, lastLoaded=${lastPageLoadedRef.current}`); + + // Skip if this page was already loaded + if (lastPageLoadedRef.current === state.page) { + console.log(`[DEBUG-PAGE_EFFECT] Skipping reload of already loaded page ${state.page}`); + return; + } + + // CRITICAL FIX: Set the ref immediately to prevent further executions + lastPageLoadedRef.current = state.page; + + // Extract account ID for consistency + const { effectiveAccountId } = normalizeFolderAndAccount(state.currentFolder); + + logEmailOp('PAGINATION', `Loading page ${state.page} for ${state.currentFolder} with account ${effectiveAccountId}`); + + // Load more emails with the correct account ID + loadEmails(true, effectiveAccountId); + + // Reset the lastPageLoadedRef when folder changes or component unmounts return () => { - if (state.currentFolder) { + if (state.currentFolder !== prevFolderRef.current) { + console.log(`[DEBUG-PAGE_EFFECT] Folder changed from ${prevFolderRef.current} to ${state.currentFolder}, resetting lastPageLoaded`); lastPageLoadedRef.current = 0; + prevFolderRef.current = state.currentFolder; } }; - }, [session?.user?.id, state.page, state.currentFolder, loadEmails, logEmailOp, state.isLoading]); + // Changed dependency array to exclude state.isLoading to prevent loop + }, [session?.user?.id, state.page, state.currentFolder, loadEmails, logEmailOp]); // Fetch unread counts from API const fetchUnreadCounts = useCallback(async () => {