mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-01 13:34:33 +00:00
feat(notification): optimistically clear unread badge on mark all as read
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user