@@ -8,6 +8,7 @@ import { displayError } from "components/GlobalSnackbar/utils";
8
8
import { type FC , useEffect , useRef } from "react" ;
9
9
import { useMutation , useQuery , useQueryClient } from "react-query" ;
10
10
import { InboxPopover } from "./InboxPopover" ;
11
+ import { useEffectEvent } from "hooks/hookPolyfills" ;
11
12
12
13
const NOTIFICATIONS_QUERY_KEY = [ "notifications" ] ;
13
14
@@ -37,10 +38,29 @@ export const NotificationsInbox: FC<NotificationsInboxProps> = ({
37
38
queryFn : fetchNotifications ,
38
39
} ) ;
39
40
41
+ const updateNotificationsCache = useEffectEvent (
42
+ async (
43
+ callback : (
44
+ res : ListInboxNotificationsResponse ,
45
+ ) => ListInboxNotificationsResponse ,
46
+ ) => {
47
+ await queryClient . cancelQueries ( NOTIFICATIONS_QUERY_KEY ) ;
48
+ queryClient . setQueryData < ListInboxNotificationsResponse > (
49
+ NOTIFICATIONS_QUERY_KEY ,
50
+ ( prev ) => {
51
+ if ( ! prev ) {
52
+ return { notifications : [ ] , unread_count : 0 } ;
53
+ }
54
+ return callback ( prev ) ;
55
+ } ,
56
+ ) ;
57
+ } ,
58
+ ) ;
59
+
40
60
useEffect ( ( ) => {
41
61
const socket = watchInboxNotifications (
42
62
( res ) => {
43
- safeUpdateNotificationsCache ( ( prev ) => {
63
+ updateNotificationsCache ( ( prev ) => {
44
64
return {
45
65
unread_count : res . unread_count ,
46
66
notifications : [ res . notification , ...prev . notifications ] ,
@@ -53,17 +73,18 @@ export const NotificationsInbox: FC<NotificationsInboxProps> = ({
53
73
return ( ) => {
54
74
socket . close ( ) ;
55
75
} ;
56
- } , [ ] ) ;
76
+ } , [ updateNotificationsCache ] ) ;
57
77
58
78
const markAllAsReadMutation = useMutation ( {
59
79
mutationFn : markAllAsRead ,
60
80
onSuccess : ( ) => {
61
- safeUpdateNotificationsCache ( ( prev ) => {
81
+ updateNotificationsCache ( ( prev ) => {
82
+ console . log ( "PREV" , prev ) ;
62
83
return {
63
84
unread_count : 0 ,
64
85
notifications : prev . notifications . map ( ( n ) => ( {
65
86
...n ,
66
- read_status : "read" ,
87
+ read_at : new Date ( ) . toISOString ( ) ,
67
88
} ) ) ,
68
89
} ;
69
90
} ) ;
@@ -79,7 +100,7 @@ export const NotificationsInbox: FC<NotificationsInboxProps> = ({
79
100
const markNotificationAsReadMutation = useMutation ( {
80
101
mutationFn : markNotificationAsRead ,
81
102
onSuccess : ( res ) => {
82
- safeUpdateNotificationsCache ( ( prev ) => {
103
+ updateNotificationsCache ( ( prev ) => {
83
104
return {
84
105
unread_count : res . unread_count ,
85
106
notifications : prev . notifications . map ( ( n ) => {
@@ -99,23 +120,6 @@ export const NotificationsInbox: FC<NotificationsInboxProps> = ({
99
120
} ,
100
121
} ) ;
101
122
102
- async function safeUpdateNotificationsCache (
103
- callback : (
104
- res : ListInboxNotificationsResponse ,
105
- ) => ListInboxNotificationsResponse ,
106
- ) {
107
- await queryClient . cancelQueries ( NOTIFICATIONS_QUERY_KEY ) ;
108
- queryClient . setQueryData < ListInboxNotificationsResponse > (
109
- NOTIFICATIONS_QUERY_KEY ,
110
- ( prev ) => {
111
- if ( ! prev ) {
112
- return { notifications : [ ] , unread_count : 0 } ;
113
- }
114
- return callback ( prev ) ;
115
- } ,
116
- ) ;
117
- }
118
-
119
123
return (
120
124
< InboxPopover
121
125
defaultOpen = { defaultOpen }
0 commit comments