Pages corrections widget
This commit is contained in:
parent
8f5a63042c
commit
430940cc12
@ -48,6 +48,7 @@ export function Parole() {
|
|||||||
const { data: session, status } = useSession();
|
const { data: session, status } = useSession();
|
||||||
const { triggerNotification } = useWidgetNotification();
|
const { triggerNotification } = useWidgetNotification();
|
||||||
const lastUnreadCountRef = useRef<number>(-1); // Initialize to -1 to detect first load
|
const lastUnreadCountRef = useRef<number>(-1); // Initialize to -1 to detect first load
|
||||||
|
const lastMessageIdsRef = useRef<Set<string>>(new Set());
|
||||||
const isInitializedRef = useRef<boolean>(false);
|
const isInitializedRef = useRef<boolean>(false);
|
||||||
|
|
||||||
const fetchMessages = async (forceRefresh = false) => {
|
const fetchMessages = async (forceRefresh = false) => {
|
||||||
@ -74,19 +75,29 @@ export function Parole() {
|
|||||||
if (Array.isArray(data.messages)) {
|
if (Array.isArray(data.messages)) {
|
||||||
// Utiliser le totalUnreadCount de l'API (plus fiable)
|
// Utiliser le totalUnreadCount de l'API (plus fiable)
|
||||||
const currentUnreadCount = data.totalUnreadCount || 0;
|
const currentUnreadCount = data.totalUnreadCount || 0;
|
||||||
|
const currentMessageIds = new Set(data.messages.map((m: any) => m.id));
|
||||||
|
|
||||||
// Update unread count state for badge display
|
// Update unread count state for badge display
|
||||||
setUnreadCount(currentUnreadCount);
|
setUnreadCount(currentUnreadCount);
|
||||||
|
|
||||||
// On initialise le count au premier chargement
|
// Detect new messages by comparing IDs (more reliable than count)
|
||||||
if (!isInitializedRef.current) {
|
const newMessageIds = new Set(
|
||||||
isInitializedRef.current = true;
|
Array.from(currentMessageIds).filter(id => !lastMessageIdsRef.current.has(id))
|
||||||
lastUnreadCountRef.current = -1; // Set to -1 to trigger on first load
|
);
|
||||||
console.log('[Parole] Initial unread count:', currentUnreadCount);
|
const hasNewMessages = newMessageIds.size > 0;
|
||||||
}
|
|
||||||
|
|
||||||
// Si le count a changé (ou premier chargement), déclencher notification
|
// On initialise au premier chargement
|
||||||
if (currentUnreadCount !== lastUnreadCountRef.current) {
|
if (!isInitializedRef.current) {
|
||||||
|
console.log('[Parole Widget] 💬 Initializing - storing existing message IDs without notifications', {
|
||||||
|
messageCount: data.messages.length,
|
||||||
|
unreadCount: currentUnreadCount,
|
||||||
|
});
|
||||||
|
lastMessageIdsRef.current = currentMessageIds;
|
||||||
|
lastUnreadCountRef.current = currentUnreadCount;
|
||||||
|
isInitializedRef.current = true;
|
||||||
|
} else {
|
||||||
|
// Si le count a changé ou nouveaux messages détectés, déclencher notification
|
||||||
|
if (currentUnreadCount !== lastUnreadCountRef.current || hasNewMessages) {
|
||||||
const previousCount = lastUnreadCountRef.current;
|
const previousCount = lastUnreadCountRef.current;
|
||||||
lastUnreadCountRef.current = currentUnreadCount;
|
lastUnreadCountRef.current = currentUnreadCount;
|
||||||
|
|
||||||
@ -112,18 +123,14 @@ export function Parole() {
|
|||||||
items: notificationItems,
|
items: notificationItems,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Dispatch event for Outlook-style notifications (only for new messages)
|
// Dispatch event for Outlook-style notifications (for new messages detected by ID)
|
||||||
// We detect new messages by comparing message count, not unread count
|
if (hasNewMessages) {
|
||||||
// because new messages might be read immediately
|
console.log('[Parole Widget] 💬 Dispatching new messages event', {
|
||||||
if (previousCount >= 0 && data.messages.length > 0) {
|
newMessagesCount: newMessageIds.size,
|
||||||
// Get the most recent messages (first ones in the array, sorted by date desc)
|
newMessageIds: Array.from(newMessageIds),
|
||||||
// We'll let the hook determine which are truly new
|
previousCount,
|
||||||
const totalMessages = data.messages.length;
|
currentCount: currentUnreadCount,
|
||||||
|
previousMessageIds: Array.from(lastMessageIdsRef.current),
|
||||||
console.log('[Parole Widget] 💬 Dispatching messages event', {
|
|
||||||
totalMessages,
|
|
||||||
previousUnreadCount: previousCount,
|
|
||||||
currentUnreadCount,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
window.dispatchEvent(new CustomEvent('new-messages-detected', {
|
window.dispatchEvent(new CustomEvent('new-messages-detected', {
|
||||||
@ -135,7 +142,10 @@ export function Parole() {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always update lastMessageIdsRef to track current state
|
||||||
|
lastMessageIdsRef.current = currentMessageIds;
|
||||||
setMessages(data.messages);
|
setMessages(data.messages);
|
||||||
} else {
|
} else {
|
||||||
console.warn('Unexpected data format:', data);
|
console.warn('Unexpected data format:', data);
|
||||||
@ -160,12 +170,14 @@ export function Parole() {
|
|||||||
}, [status]);
|
}, [status]);
|
||||||
|
|
||||||
// Integrate unified refresh for automatic polling
|
// Integrate unified refresh for automatic polling
|
||||||
|
// Use forceRefresh=true to ensure we get the latest messages immediately
|
||||||
const { refresh } = useUnifiedRefresh({
|
const { refresh } = useUnifiedRefresh({
|
||||||
resource: 'parole',
|
resource: 'parole',
|
||||||
interval: REFRESH_INTERVALS.PAROLE, // 30 seconds
|
interval: REFRESH_INTERVALS.PAROLE, // 30 seconds
|
||||||
enabled: status === 'authenticated',
|
enabled: status === 'authenticated',
|
||||||
onRefresh: async () => {
|
onRefresh: async () => {
|
||||||
await fetchMessages(false); // Use cache for auto-refresh
|
// Use forceRefresh to bypass cache and get latest messages immediately
|
||||||
|
await fetchMessages(true); // Force refresh to get new messages immediately
|
||||||
},
|
},
|
||||||
priority: 'high',
|
priority: 'high',
|
||||||
});
|
});
|
||||||
|
|||||||
@ -29,10 +29,8 @@ export function useRocketChatMessageNotifications() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only show notifications if the count increased (new messages arrived)
|
// Find new messages 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 messages (not in lastMessageIdsRef) - these are the ones that just arrived
|
|
||||||
const newMessages = messages
|
const newMessages = messages
|
||||||
.filter((message: any) => {
|
.filter((message: any) => {
|
||||||
const messageId = message.id;
|
const messageId = message.id;
|
||||||
@ -44,8 +42,14 @@ export function useRocketChatMessageNotifications() {
|
|||||||
lastMessageIdsRef.current = new Set(messages.map((m: any) => m.id));
|
lastMessageIdsRef.current = new Set(messages.map((m: any) => m.id));
|
||||||
|
|
||||||
// If there are new messages, queue them for notification
|
// If there are new messages, queue them for notification
|
||||||
|
// This works regardless of previousCount value
|
||||||
if (newMessages.length > 0) {
|
if (newMessages.length > 0) {
|
||||||
console.log('[useRocketChatMessageNotifications] 💬 New messages detected:', newMessages.length);
|
console.log('[useRocketChatMessageNotifications] 💬 New messages detected:', {
|
||||||
|
newMessagesCount: newMessages.length,
|
||||||
|
previousCount,
|
||||||
|
currentCount,
|
||||||
|
newMessageIds: newMessages.map((m: any) => m.id),
|
||||||
|
});
|
||||||
|
|
||||||
newMessages.forEach((message: any) => {
|
newMessages.forEach((message: any) => {
|
||||||
const senderName = message.sender?.name || message.sender?.username || 'Inconnu';
|
const senderName = message.sender?.name || message.sender?.username || 'Inconnu';
|
||||||
@ -84,10 +88,13 @@ export function useRocketChatMessageNotifications() {
|
|||||||
if (!isShowingRef.current && notificationQueueRef.current.length > 0) {
|
if (!isShowingRef.current && notificationQueueRef.current.length > 0) {
|
||||||
showNextNotification();
|
showNextNotification();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Just update the message IDs without showing notifications
|
console.log('[useRocketChatMessageNotifications] ⏭️ No new messages detected', {
|
||||||
lastMessageIdsRef.current = new Set(messages.map((m: any) => m.id));
|
previousCount,
|
||||||
|
currentCount,
|
||||||
|
totalMessages: messages.length,
|
||||||
|
lastMessageIdsCount: lastMessageIdsRef.current.size,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user