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