diff --git a/src/services/notification/notification.test.ts b/src/services/notification/notification.test.ts index a4a58154..3cffd566 100644 --- a/src/services/notification/notification.test.ts +++ b/src/services/notification/notification.test.ts @@ -332,7 +332,13 @@ describe('NotificationService', () => { data: { id: '101', teamId: tm.id, type: NotificationType.comment_created, creatorId: u2.id }, }) const n2 = await prisma.notification.create({ - data: { id: '102', teamId: tm.id, type: NotificationType.reply_created, userId: u1.id, creatorId: u2.id }, + data: { + id: '102', + teamId: tm.id, + type: NotificationType.reply_created, + userId: u1.id, + creatorId: u2.id, + }, }) count = await notificationService.getUnreadCount(tm.id, u1.id) diff --git a/src/ui/components/notification-list.tsx b/src/ui/components/notification-list.tsx index e4b5b984..e8aeee86 100644 --- a/src/ui/components/notification-list.tsx +++ b/src/ui/components/notification-list.tsx @@ -8,6 +8,7 @@ import { CheckCheckIcon, Loader2 } from 'lucide-react' import { useEffect, useMemo, useState } from 'react' import { useInView } from 'react-intersection-observer' import type { InferResponseType } from 'hono/client' +import type { GetMeResponse } from '@/dtos/team' export const NotificationList = () => { const { teamId } = useTeamContextStore() @@ -83,14 +84,31 @@ export const NotificationList = () => { if (notifications.length > 0 && teamId) { const topId = notifications[0].id if (topId) { - await markReadMutation.mutateAsync({ - teamId, - notificationId: topId, + // Optimistically clear the unread count in React Query cache instantly + queryClient.setQueryData(['teams', teamId, 'me'], (oldData: GetMeResponse | undefined) => { + if (!oldData) return oldData + return { + ...oldData, + unreadNotificationCount: 0, + } }) - // Invalidate queries + + try { + await markReadMutation.mutateAsync({ + teamId, + notificationId: topId, + }) + } catch (err) { + console.error('Failed to mark all as read:', err) + } + + // Invalidate queries to trigger sync refetches queryClient.invalidateQueries({ queryKey: ['notifications', teamId], }) + queryClient.invalidateQueries({ + queryKey: ['teams', teamId, 'me'], + }) // Refetch to update UI state immediately refetch() }