diff --git a/apps/app/components/notifications/notification-card.tsx b/apps/app/components/notifications/notification-card.tsx index 71c2d676a..3a0d2f9b8 100644 --- a/apps/app/components/notifications/notification-card.tsx +++ b/apps/app/components/notifications/notification-card.tsx @@ -27,6 +27,7 @@ import { snoozeOptions } from "constants/notification"; type NotificationCardProps = { notification: IUserNotification; markNotificationReadStatus: (notificationId: string) => Promise; + markNotificationReadStatusToggle: (notificationId: string) => Promise; markNotificationArchivedStatus: (notificationId: string) => Promise; setSelectedNotificationForSnooze: (notificationId: string) => void; markSnoozeNotification: (notificationId: string, dateTime?: Date | undefined) => Promise; @@ -36,6 +37,7 @@ export const NotificationCard: React.FC = (props) => { const { notification, markNotificationReadStatus, + markNotificationReadStatusToggle, markNotificationArchivedStatus, setSelectedNotificationForSnooze, markSnoozeNotification, @@ -159,7 +161,7 @@ export const NotificationCard: React.FC = (props) => { name: notification.read_at ? "Mark as unread" : "Mark as read", icon: "chat_bubble", onClick: () => { - markNotificationReadStatus(notification.id).then(() => { + markNotificationReadStatusToggle(notification.id).then(() => { setToastAlert({ title: notification.read_at ? "Notification marked as unread" diff --git a/apps/app/components/notifications/notification-popover.tsx b/apps/app/components/notifications/notification-popover.tsx index 0e57c472c..cd8c50bd2 100644 --- a/apps/app/components/notifications/notification-popover.tsx +++ b/apps/app/components/notifications/notification-popover.tsx @@ -38,6 +38,7 @@ export const NotificationPopover = () => { notificationMutate, markNotificationArchivedStatus, markNotificationReadStatus, + markNotificationAsRead, markSnoozeNotification, notificationCount, totalNotificationCount, @@ -128,7 +129,8 @@ export const NotificationPopover = () => { key={notification.id} notification={notification} markNotificationArchivedStatus={markNotificationArchivedStatus} - markNotificationReadStatus={markNotificationReadStatus} + markNotificationReadStatus={markNotificationAsRead} + markNotificationReadStatusToggle={markNotificationReadStatus} setSelectedNotificationForSnooze={setSelectedNotificationForSnooze} markSnoozeNotification={markSnoozeNotification} /> diff --git a/apps/app/hooks/use-user-notifications.tsx b/apps/app/hooks/use-user-notifications.tsx index 33c2d4e65..868633db0 100644 --- a/apps/app/hooks/use-user-notifications.tsx +++ b/apps/app/hooks/use-user-notifications.tsx @@ -185,6 +185,26 @@ const useUserNotification = () => { } }; + const markNotificationAsRead = async (notificationId: string) => { + if (!workspaceSlug) return; + + const isRead = + notifications?.find((notification) => notification.id === notificationId)?.read_at !== null; + + if (isRead) return; + + mutateNotification(notificationId, { read_at: new Date() }); + handleReadMutation("read"); + + await userNotificationServices + .markUserNotificationAsRead(workspaceSlug.toString(), notificationId) + .catch(() => { + throw new Error("Something went wrong"); + }); + + mutateNotificationCount(); + }; + const markNotificationArchivedStatus = async (notificationId: string) => { if (!workspaceSlug) return; const isArchived = @@ -283,6 +303,7 @@ const useUserNotification = () => { hasMore, isRefreshing, setFetchNotifications, + markNotificationAsRead, }; };