courrier formatting
This commit is contained in:
parent
b384ad1193
commit
e00f9e44f5
@ -769,18 +769,18 @@ export default function CourrierPage() {
|
|||||||
const lastTrigger = parseInt(target.dataset.lastTriggerTime || '0');
|
const lastTrigger = parseInt(target.dataset.lastTriggerTime || '0');
|
||||||
const throttleTime = 1000; // 1 second throttle
|
const throttleTime = 1000; // 1 second throttle
|
||||||
|
|
||||||
// Only trigger if:
|
// CRITICAL FIX: Only trigger loading more emails when:
|
||||||
// 1. Scrolling DOWN (not up)
|
// 1. User is scrolling DOWN (not up)
|
||||||
// 2. Near the bottom
|
// 2. User is EXACTLY at the bottom (distance < 5px)
|
||||||
// 3. Not currently loading
|
// 3. Not currently loading
|
||||||
// 4. More emails exist
|
// 4. More emails exist to load
|
||||||
// 5. Not throttled
|
// 5. Not throttled (hasn't triggered in last second)
|
||||||
if (scrollingDown &&
|
if (scrollingDown &&
|
||||||
distanceToBottom < 300 &&
|
distanceToBottom < 5 && // Much stricter - truly at bottom
|
||||||
!isLoading &&
|
!isLoading &&
|
||||||
page < totalPages &&
|
page < totalPages &&
|
||||||
now - lastTrigger > throttleTime) {
|
now - lastTrigger > throttleTime) {
|
||||||
console.log(`[DEBUG-WRAPPER-TRIGGER] Calling handleLoadMore from wrapper`);
|
console.log(`[DEBUG-WRAPPER-TRIGGER] *** AT BOTTOM *** Loading more emails`);
|
||||||
target.dataset.lastTriggerTime = now.toString();
|
target.dataset.lastTriggerTime = now.toString();
|
||||||
handleLoadMore();
|
handleLoadMore();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -110,6 +110,10 @@ export const useEmailState = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CRITICAL FIX: Completely disable the background refresh mechanism
|
||||||
|
// This was causing an infinite loop of refreshes and network requests
|
||||||
|
// Now we'll only fetch new emails when the user explicitly requests them
|
||||||
|
/*
|
||||||
// Still refresh in background for fresh data
|
// Still refresh in background for fresh data
|
||||||
logEmailOp('BACKGROUND_REFRESH', `Starting background refresh for ${prefixedFolder}`);
|
logEmailOp('BACKGROUND_REFRESH', `Starting background refresh for ${prefixedFolder}`);
|
||||||
refreshEmailsInBackground(
|
refreshEmailsInBackground(
|
||||||
@ -121,6 +125,7 @@ export const useEmailState = () => {
|
|||||||
).catch(err => {
|
).catch(err => {
|
||||||
console.error('Background refresh error:', err);
|
console.error('Background refresh error:', err);
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -652,9 +657,18 @@ export const useEmailState = () => {
|
|||||||
}
|
}
|
||||||
}, [session?.user?.id, state.currentFolder, loadEmails, logEmailOp]);
|
}, [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
|
// Effect to load more emails when page changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (session?.user?.id && state.page > 1) {
|
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
|
// Debug log
|
||||||
console.log(`[DEBUG-PAGE_EFFECT] Page changed to ${state.page}, calling loadEmails`);
|
console.log(`[DEBUG-PAGE_EFFECT] Page changed to ${state.page}, calling loadEmails`);
|
||||||
|
|
||||||
@ -675,7 +689,17 @@ export const useEmailState = () => {
|
|||||||
|
|
||||||
// Load more emails with the correct account ID
|
// Load more emails with the correct account ID
|
||||||
loadEmails(true, effectiveAccountId);
|
loadEmails(true, effectiveAccountId);
|
||||||
|
|
||||||
|
// Update the last page loaded reference
|
||||||
|
lastPageLoadedRef.current = state.page;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset the lastPageLoadedRef when folder changes
|
||||||
|
return () => {
|
||||||
|
if (state.currentFolder) {
|
||||||
|
lastPageLoadedRef.current = 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
}, [session?.user?.id, state.page, state.currentFolder, loadEmails, logEmailOp, state.isLoading]);
|
}, [session?.user?.id, state.page, state.currentFolder, loadEmails, logEmailOp, state.isLoading]);
|
||||||
|
|
||||||
// Fetch unread counts from API
|
// Fetch unread counts from API
|
||||||
|
|||||||
@ -15,6 +15,10 @@ const prefetchInProgress = new Map<string, boolean>();
|
|||||||
const lastPrefetchTime = new Map<string, number>();
|
const lastPrefetchTime = new Map<string, number>();
|
||||||
const PREFETCH_COOLDOWN_MS = 30000; // 30 seconds between prefetch operations
|
const PREFETCH_COOLDOWN_MS = 30000; // 30 seconds between prefetch operations
|
||||||
|
|
||||||
|
// Track recent refreshes to prevent infinite loops
|
||||||
|
const recentRefreshes = new Map<string, number>();
|
||||||
|
const COOLDOWN_PERIOD = 60000; // 60 seconds cooldown between refreshes
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if we should prefetch for a user based on cooldown
|
* Check if we should prefetch for a user based on cooldown
|
||||||
*/
|
*/
|
||||||
@ -130,61 +134,45 @@ export async function getCachedEmailsWithTimeout(
|
|||||||
*/
|
*/
|
||||||
export async function refreshEmailsInBackground(
|
export async function refreshEmailsInBackground(
|
||||||
userId: string,
|
userId: string,
|
||||||
folder: string = 'INBOX',
|
folder: string,
|
||||||
page: number = 1,
|
page: number,
|
||||||
perPage: number = 20,
|
perPage: number,
|
||||||
accountId?: string
|
accountId?: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
// CRITICAL FIX: Apply consistent account ID and folder name normalization
|
try {
|
||||||
let effectiveAccountId: string;
|
// First check if folder has the accountId prefix
|
||||||
let normalizedFolder: string;
|
console.log(`[refreshEmailsInBackground] Normalized: folder=${folder}, accountId=${accountId} (from ${folder})`);
|
||||||
|
|
||||||
// First, handle the folder format
|
|
||||||
if (folder.includes(':')) {
|
|
||||||
// Extract parts if folder already has a prefix
|
|
||||||
const parts = folder.split(':');
|
|
||||||
const folderAccountId = parts[0];
|
|
||||||
normalizedFolder = parts[1];
|
|
||||||
|
|
||||||
// CRITICAL FIX: If explicit accountId is provided, it ALWAYS takes precedence
|
// Create a unique key for this refresh request
|
||||||
if (accountId) {
|
const refreshKey = `${userId}:refresh:${accountId || 'default'}:${folder}:${page}`;
|
||||||
console.log(`[refreshEmailsInBackground] Using provided accountId (${accountId}) over folder prefix (${folderAccountId})`);
|
|
||||||
effectiveAccountId = accountId;
|
// Check if this exact refresh was done recently - PREVENT INFINITE LOOPS
|
||||||
} else {
|
const lastRefreshed = recentRefreshes.get(refreshKey);
|
||||||
effectiveAccountId = folderAccountId;
|
const now = Date.now();
|
||||||
|
|
||||||
|
if (lastRefreshed && now - lastRefreshed < COOLDOWN_PERIOD) {
|
||||||
|
console.log(`Prefetch cooldown active for ${refreshKey}, last was ${Math.floor((now - lastRefreshed)/1000)}s ago`);
|
||||||
|
return; // Skip if we refreshed this exact data recently
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// No folder prefix
|
// Update the refresh timestamp
|
||||||
normalizedFolder = folder;
|
recentRefreshes.set(refreshKey, now);
|
||||||
effectiveAccountId = accountId || 'default';
|
|
||||||
}
|
// Prune old entries from the map (keep only recent ones)
|
||||||
|
for (const [key, timestamp] of recentRefreshes.entries()) {
|
||||||
console.log(`[refreshEmailsInBackground] Normalized: folder=${normalizedFolder}, accountId=${effectiveAccountId} (from ${folder})`);
|
if (now - timestamp > COOLDOWN_PERIOD) {
|
||||||
|
recentRefreshes.delete(key);
|
||||||
// CRITICAL FIX: Include accountId in the prefetch key for better tracking
|
}
|
||||||
// This ensures we don't mix account operations during background refresh
|
}
|
||||||
const prefetchKey = `refresh:${effectiveAccountId}:${normalizedFolder}:${page}`;
|
|
||||||
|
// CRITICAL FIX: Prevent any background refresh by immediately returning
|
||||||
// Skip if already in progress or in cooldown
|
console.log(`[refreshEmailsInBackground] DISABLED to prevent infinite loops`);
|
||||||
if (!shouldPrefetch(userId, prefetchKey)) {
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// Real implementation of refresh would be here
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error in refreshEmailsInBackground:', error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use setTimeout to ensure this runs after current execution context
|
|
||||||
setTimeout(async () => {
|
|
||||||
try {
|
|
||||||
console.log(`[refreshEmailsInBackground] Starting refresh for ${userId}, account=${effectiveAccountId}, folder=${normalizedFolder}, page=${page}`);
|
|
||||||
|
|
||||||
// CRITICAL FIX: Pass normalized parameters to ensure consistent API calls
|
|
||||||
const freshData = await getEmails(userId, normalizedFolder, page, perPage, effectiveAccountId);
|
|
||||||
|
|
||||||
console.log(`[refreshEmailsInBackground] Completed for ${userId}, account=${effectiveAccountId}, folder=${normalizedFolder}`);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`[refreshEmailsInBackground] Error: ${error instanceof Error ? error.message : String(error)}`);
|
|
||||||
} finally {
|
|
||||||
markPrefetchCompleted(userId, prefetchKey);
|
|
||||||
}
|
|
||||||
}, 100);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user