Skip to content

Commit 281dadf

Browse files
committed
Apply review comments and fix storybook tests
1 parent 328854d commit 281dadf

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

site/src/api/api.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,14 @@ export const watchInboxNotifications = (
139139
);
140140

141141
socket.addEventListener("message", (event) => {
142-
const res = JSON.parse(event.data) as TypesGen.GetInboxNotificationResponse;
143-
onNewNotification(res);
142+
try {
143+
const res = JSON.parse(
144+
event.data,
145+
) as TypesGen.GetInboxNotificationResponse;
146+
onNewNotification(res);
147+
} catch (error) {
148+
console.warn("Error parsing inbox notification: ", error);
149+
}
144150
});
145151

146152
socket.addEventListener("error", (event) => {

site/src/modules/dashboard/Navbar/NavbarView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const NavbarView: FC<NavbarViewProps> = ({
6969

7070
<NotificationsInbox
7171
fetchNotifications={API.getInboxNotifications}
72-
markAllAsRead={(): Promise<void> => {
72+
markAllAsRead={() => {
7373
throw new Error("Function not implemented.");
7474
}}
7575
markNotificationAsRead={(notificationId) =>

site/src/modules/notifications/NotificationsInbox/NotificationsInbox.stories.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,13 @@ export const MarkNotificationAsRead: Story = {
134134
notifications: MockNotifications,
135135
unread_count: 2,
136136
})),
137-
markNotificationAsRead: fn(),
137+
markNotificationAsRead: fn(async () => ({
138+
unread_count: 1,
139+
notification: {
140+
...MockNotifications[1],
141+
read_at: new Date().toISOString(),
142+
},
143+
})),
138144
},
139145
play: async ({ canvasElement }) => {
140146
const body = within(canvasElement.ownerDocument.body);

site/src/modules/notifications/NotificationsInbox/NotificationsInbox.tsx

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { displayError } from "components/GlobalSnackbar/utils";
88
import { type FC, useEffect, useRef } from "react";
99
import { useMutation, useQuery, useQueryClient } from "react-query";
1010
import { InboxPopover } from "./InboxPopover";
11+
import { useEffectEvent } from "hooks/hookPolyfills";
1112

1213
const NOTIFICATIONS_QUERY_KEY = ["notifications"];
1314

@@ -37,10 +38,29 @@ export const NotificationsInbox: FC<NotificationsInboxProps> = ({
3738
queryFn: fetchNotifications,
3839
});
3940

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+
4060
useEffect(() => {
4161
const socket = watchInboxNotifications(
4262
(res) => {
43-
safeUpdateNotificationsCache((prev) => {
63+
updateNotificationsCache((prev) => {
4464
return {
4565
unread_count: res.unread_count,
4666
notifications: [res.notification, ...prev.notifications],
@@ -53,17 +73,18 @@ export const NotificationsInbox: FC<NotificationsInboxProps> = ({
5373
return () => {
5474
socket.close();
5575
};
56-
}, []);
76+
}, [updateNotificationsCache]);
5777

5878
const markAllAsReadMutation = useMutation({
5979
mutationFn: markAllAsRead,
6080
onSuccess: () => {
61-
safeUpdateNotificationsCache((prev) => {
81+
updateNotificationsCache((prev) => {
82+
console.log("PREV", prev);
6283
return {
6384
unread_count: 0,
6485
notifications: prev.notifications.map((n) => ({
6586
...n,
66-
read_status: "read",
87+
read_at: new Date().toISOString(),
6788
})),
6889
};
6990
});
@@ -79,7 +100,7 @@ export const NotificationsInbox: FC<NotificationsInboxProps> = ({
79100
const markNotificationAsReadMutation = useMutation({
80101
mutationFn: markNotificationAsRead,
81102
onSuccess: (res) => {
82-
safeUpdateNotificationsCache((prev) => {
103+
updateNotificationsCache((prev) => {
83104
return {
84105
unread_count: res.unread_count,
85106
notifications: prev.notifications.map((n) => {
@@ -99,23 +120,6 @@ export const NotificationsInbox: FC<NotificationsInboxProps> = ({
99120
},
100121
});
101122

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-
119123
return (
120124
<InboxPopover
121125
defaultOpen={defaultOpen}

0 commit comments

Comments
 (0)