feat(notification): optimistically clear unread badge on mark all as read

This commit is contained in:
yiling
2026-05-29 23:35:26 +08:00
parent 3ba107bfa4
commit 1a865eac88
2 changed files with 29 additions and 5 deletions
@@ -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)
+22 -4
View File
@@ -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()
}