Pages corrections pages health
This commit is contained in:
parent
e23965b0fd
commit
d1e34c5081
@ -40,6 +40,8 @@ export function Email() {
|
|||||||
const [accountErrors, setAccountErrors] = useState<Record<string, string>>({});
|
const [accountErrors, setAccountErrors] = useState<Record<string, string>>({});
|
||||||
const { triggerNotification } = useWidgetNotification();
|
const { triggerNotification } = useWidgetNotification();
|
||||||
const lastUnreadCountRef = useRef<number>(-1);
|
const lastUnreadCountRef = useRef<number>(-1);
|
||||||
|
const lastEmailIdsRef = useRef<Set<string>>(new Set());
|
||||||
|
const isInitializedRef = useRef(false);
|
||||||
|
|
||||||
// Create a map for quick account lookup by ID (recalculated when accounts change)
|
// Create a map for quick account lookup by ID (recalculated when accounts change)
|
||||||
const accountMap = useMemo(() => {
|
const accountMap = useMemo(() => {
|
||||||
@ -173,9 +175,26 @@ export function Email() {
|
|||||||
|
|
||||||
// Calculate unread count
|
// Calculate unread count
|
||||||
const currentUnreadCount = transformedEmails.filter(e => !e.read).length;
|
const currentUnreadCount = transformedEmails.filter(e => !e.read).length;
|
||||||
|
const currentEmailIds = new Set(transformedEmails.map(e => e.id));
|
||||||
|
|
||||||
// Trigger notification if count changed
|
// Detect new emails by comparing IDs (more reliable than count)
|
||||||
if (currentUnreadCount !== lastUnreadCountRef.current) {
|
const newEmailIds = new Set(
|
||||||
|
Array.from(currentEmailIds).filter(id => !lastEmailIdsRef.current.has(id))
|
||||||
|
);
|
||||||
|
const hasNewEmails = newEmailIds.size > 0;
|
||||||
|
|
||||||
|
// On first load, just store email IDs without triggering notifications
|
||||||
|
if (!isInitializedRef.current) {
|
||||||
|
console.log('[Email Widget] 📧 Initializing - storing existing email IDs without notifications', {
|
||||||
|
emailCount: transformedEmails.length,
|
||||||
|
unreadCount: currentUnreadCount,
|
||||||
|
});
|
||||||
|
lastEmailIdsRef.current = currentEmailIds;
|
||||||
|
lastUnreadCountRef.current = currentUnreadCount;
|
||||||
|
isInitializedRef.current = true;
|
||||||
|
} else {
|
||||||
|
// Trigger notification if count changed or new emails detected
|
||||||
|
if (currentUnreadCount !== lastUnreadCountRef.current || hasNewEmails) {
|
||||||
const previousCount = lastUnreadCountRef.current;
|
const previousCount = lastUnreadCountRef.current;
|
||||||
lastUnreadCountRef.current = currentUnreadCount;
|
lastUnreadCountRef.current = currentUnreadCount;
|
||||||
|
|
||||||
@ -205,18 +224,20 @@ export function Email() {
|
|||||||
items: notificationItems,
|
items: notificationItems,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Dispatch event for Outlook-style notifications (only for new emails)
|
// Dispatch event for Outlook-style notifications (for new emails detected by ID)
|
||||||
if (previousCount >= 0 && currentUnreadCount > previousCount) {
|
if (hasNewEmails) {
|
||||||
// Get only the newly arrived emails (the difference)
|
// Get only the newly arrived emails (by ID comparison)
|
||||||
const newEmails = transformedEmails
|
const newEmails = transformedEmails
|
||||||
.filter(e => !e.read)
|
.filter(e => newEmailIds.has(e.id) && !e.read)
|
||||||
.slice(0, currentUnreadCount - previousCount); // Only the new ones
|
.slice(0, 5); // Limit to 5 most recent new emails
|
||||||
|
|
||||||
if (newEmails.length > 0) {
|
if (newEmails.length > 0) {
|
||||||
console.log('[Email Widget] 📧 Dispatching new emails event', {
|
console.log('[Email Widget] 📧 Dispatching new emails event', {
|
||||||
newEmailsCount: newEmails.length,
|
newEmailsCount: newEmails.length,
|
||||||
|
newEmailIds: Array.from(newEmailIds),
|
||||||
previousCount,
|
previousCount,
|
||||||
currentCount: currentUnreadCount,
|
currentCount: currentUnreadCount,
|
||||||
|
previousEmailIds: Array.from(lastEmailIdsRef.current),
|
||||||
});
|
});
|
||||||
|
|
||||||
window.dispatchEvent(new CustomEvent('new-emails-detected', {
|
window.dispatchEvent(new CustomEvent('new-emails-detected', {
|
||||||
@ -230,6 +251,10 @@ export function Email() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always update lastEmailIdsRef to track current state
|
||||||
|
lastEmailIdsRef.current = currentEmailIds;
|
||||||
|
|
||||||
// Show error only if all accounts failed
|
// Show error only if all accounts failed
|
||||||
if (allEmails.length === 0 && accounts.length > 0 && Object.keys(accountErrors).length === accounts.length) {
|
if (allEmails.length === 0 && accounts.length > 0 && Object.keys(accountErrors).length === accounts.length) {
|
||||||
@ -272,12 +297,14 @@ export function Email() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Integrate unified refresh for automatic polling
|
// Integrate unified refresh for automatic polling
|
||||||
|
// Use forceRefresh=true to ensure we get the latest emails immediately
|
||||||
const { refresh } = useUnifiedRefresh({
|
const { refresh } = useUnifiedRefresh({
|
||||||
resource: 'email',
|
resource: 'email',
|
||||||
interval: REFRESH_INTERVALS.EMAIL, // 30 seconds (harmonized)
|
interval: REFRESH_INTERVALS.EMAIL, // 30 seconds (harmonized)
|
||||||
enabled: status === 'authenticated',
|
enabled: status === 'authenticated',
|
||||||
onRefresh: async () => {
|
onRefresh: async () => {
|
||||||
await fetchEmails(false); // Use cache for auto-refresh
|
// Use forceRefresh to bypass cache and get latest emails immediately
|
||||||
|
await fetchEmails(true); // Force refresh to get new emails immediately
|
||||||
await fetchUnreadCount();
|
await fetchUnreadCount();
|
||||||
},
|
},
|
||||||
priority: 'high',
|
priority: 'high',
|
||||||
|
|||||||
@ -30,10 +30,8 @@ export function useEmailNotifications() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only show notifications if the count increased (new emails arrived)
|
// Find new emails by comparing IDs (more reliable than count comparison)
|
||||||
// AND we have a previous count to compare with
|
// This works even if previousCount is -1 (first load after initialization)
|
||||||
if (previousCount >= 0 && currentCount > previousCount) {
|
|
||||||
// Find new emails (not in lastEmailIdsRef) - these are the ones that just arrived
|
|
||||||
const newEmails = emails
|
const newEmails = emails
|
||||||
.filter((email: any) => {
|
.filter((email: any) => {
|
||||||
const emailId = email.id;
|
const emailId = email.id;
|
||||||
@ -45,8 +43,14 @@ export function useEmailNotifications() {
|
|||||||
lastEmailIdsRef.current = new Set(emails.map((e: any) => e.id));
|
lastEmailIdsRef.current = new Set(emails.map((e: any) => e.id));
|
||||||
|
|
||||||
// If there are new unread emails, queue them for notification
|
// If there are new unread emails, queue them for notification
|
||||||
|
// This works regardless of previousCount value
|
||||||
if (newEmails.length > 0) {
|
if (newEmails.length > 0) {
|
||||||
console.log('[useEmailNotifications] 📧 New emails detected:', newEmails.length);
|
console.log('[useEmailNotifications] 📧 New emails detected:', {
|
||||||
|
newEmailsCount: newEmails.length,
|
||||||
|
previousCount,
|
||||||
|
currentCount,
|
||||||
|
newEmailIds: newEmails.map((e: any) => e.id),
|
||||||
|
});
|
||||||
|
|
||||||
newEmails.forEach((email: any) => {
|
newEmails.forEach((email: any) => {
|
||||||
const account = accountMap.get(email.accountId);
|
const account = accountMap.get(email.accountId);
|
||||||
@ -82,10 +86,13 @@ export function useEmailNotifications() {
|
|||||||
if (!isShowingRef.current && notificationQueueRef.current.length > 0) {
|
if (!isShowingRef.current && notificationQueueRef.current.length > 0) {
|
||||||
showNextNotification();
|
showNextNotification();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Just update the email IDs without showing notifications
|
console.log('[useEmailNotifications] ⏭️ No new emails detected', {
|
||||||
lastEmailIdsRef.current = new Set(emails.map((e: any) => e.id));
|
previousCount,
|
||||||
|
currentCount,
|
||||||
|
totalEmails: emails.length,
|
||||||
|
lastEmailIdsCount: lastEmailIdsRef.current.size,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user