courrier formatting
This commit is contained in:
parent
e00f9e44f5
commit
af18853d30
@ -30,6 +30,15 @@ export const useEmailState = () => {
|
||||
const { data: session } = useSession();
|
||||
const { toast } = useToast();
|
||||
|
||||
// Refs to track state
|
||||
const updateUnreadTimerRef = useRef<number | null>(null);
|
||||
const lastEmailViewedRef = useRef<number | null>(null);
|
||||
const failedFetchCountRef = useRef<number>(0);
|
||||
const lastFolderRef = useRef<string | null>(null);
|
||||
const lastPageLoadedRef = useRef<number>(0);
|
||||
const prevFolderRef = useRef<string | null>(null);
|
||||
const loadMoreTriggerTimeRef = useRef<number>(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<number>(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 () => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user