From dc919c58a99cf2397182128421661444679156eb Mon Sep 17 00:00:00 2001 From: alma Date: Wed, 30 Apr 2025 17:34:57 +0200 Subject: [PATCH] courrier formatting --- hooks/use-email-state.ts | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/hooks/use-email-state.ts b/hooks/use-email-state.ts index fdcb6413..9e9aed07 100644 --- a/hooks/use-email-state.ts +++ b/hooks/use-email-state.ts @@ -645,7 +645,7 @@ export const useEmailState = () => { // Handle loading more emails const handleLoadMore = useCallback(() => { - logEmailOp('LOAD_MORE', `Current state - page: ${state.page}, totalPages: ${state.totalPages}, isLoading: ${state.isLoading}`); + logEmailOp('LOAD_MORE', `Current state - page: ${state.page}, totalPages: ${state.totalPages}, isLoading: ${state.isLoading}, current emails: ${state.emails.length}`); // Skip if we're already at the last page if (state.page >= state.totalPages) { @@ -666,10 +666,19 @@ export const useEmailState = () => { return; } - // Update the trigger time and load more + // Update the trigger time loadMoreTriggerTimeRef.current = now; - dispatch({ type: 'SET_PAGE', payload: state.page + 1 }); - }, [state.page, state.totalPages, state.isLoading, logEmailOp, dispatch]); + + // CRITICAL FIX: Set nextPage value and log for debugging + const nextPage = state.page + 1; + console.log(`[DEBUG-LOAD_MORE] Incrementing page from ${state.page} to ${nextPage}, current emails count: ${state.emails.length}`); + + // Increment the page to trigger the useEffect + dispatch({ type: 'SET_PAGE', payload: nextPage }); + + // CRITICAL FIX: Force loading state to true immediately + dispatch({ type: 'SET_LOADING', payload: true }); + }, [state.page, state.totalPages, state.isLoading, state.emails.length, logEmailOp, dispatch]); // Effect to load emails when folder changes useEffect(() => { @@ -679,10 +688,15 @@ export const useEmailState = () => { logEmailOp('FOLDER_CHANGE', `Loading emails for folder ${state.currentFolder} with account ${effectiveAccountId}`); - // Load emails with the correct account ID + // Reset page to 1 when changing folders + if (state.page !== 1) { + dispatch({ type: 'SET_PAGE', payload: 1 }); + } + + // Load emails with the correct account ID (not appending since this is a folder change) loadEmails(false, effectiveAccountId); } - }, [session?.user?.id, state.currentFolder, loadEmails, logEmailOp]); + }, [session?.user?.id, state.currentFolder, state.page, loadEmails, logEmailOp, dispatch]); // Effect to load more emails when page changes useEffect(() => { @@ -699,7 +713,7 @@ export const useEmailState = () => { } // Debug log - console.log(`[DEBUG-PAGE_EFFECT] Page effect running for page ${state.page}, lastLoaded=${lastPageLoadedRef.current}`); + console.log(`[DEBUG-PAGE_EFFECT] Page effect running for page ${state.page}, lastLoaded=${lastPageLoadedRef.current}, current emails count: ${state.emails.length}`); // Skip if this page was already loaded if (lastPageLoadedRef.current === state.page) { @@ -713,9 +727,9 @@ export const useEmailState = () => { // Extract account ID for consistency const { effectiveAccountId } = normalizeFolderAndAccount(state.currentFolder); - logEmailOp('PAGINATION', `Loading page ${state.page} for ${state.currentFolder} with account ${effectiveAccountId}`); + logEmailOp('PAGINATION', `Loading more emails for page ${state.page} (total emails so far: ${state.emails.length})`); - // Load more emails with the correct account ID + // CRITICAL FIX: Always use isLoadMore=true when page > 1 loadEmails(true, effectiveAccountId); // Reset the lastPageLoadedRef when folder changes or component unmounts @@ -726,8 +740,8 @@ export const useEmailState = () => { prevFolderRef.current = state.currentFolder; } }; - // Changed dependency array to exclude state.isLoading to prevent loop - }, [session?.user?.id, state.page, state.currentFolder, loadEmails, logEmailOp]); + // Do NOT include state.emails.length here to prevent infinite loops + }, [session?.user?.id, state.page, state.currentFolder, state.isLoading, loadEmails, logEmailOp]); // Fetch unread counts from API const fetchUnreadCounts = useCallback(async () => {