refactor: fetching notification only when popover is open (#1706)

This commit is contained in:
Dakshesh Jain 2023-07-30 02:05:30 +05:30 committed by GitHub
parent e0affa21c4
commit 35bb71303e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 129 additions and 122 deletions

View File

@ -45,6 +45,7 @@ export const NotificationPopover = () => {
isLoadingMore, isLoadingMore,
hasMore, hasMore,
isRefreshing, isRefreshing,
setFetchNotifications,
} = useUserNotification(); } = useUserNotification();
// theme context // theme context
@ -66,7 +67,10 @@ export const NotificationPopover = () => {
}} }}
/> />
<Popover className="relative w-full"> <Popover className="relative w-full">
{({ open: isActive, close: closePopover }) => ( {({ open: isActive, close: closePopover }) => {
if (isActive) setFetchNotifications(true);
return (
<> <>
<Tooltip <Tooltip
tooltipContent="Notifications" tooltipContent="Notifications"
@ -189,7 +193,8 @@ export const NotificationPopover = () => {
</Popover.Panel> </Popover.Panel>
</Transition> </Transition>
</> </>
)} );
}}
</Popover> </Popover>
</> </>
); );

View File

@ -25,6 +25,7 @@ const useUserNotification = () => {
const [snoozed, setSnoozed] = useState<boolean>(false); const [snoozed, setSnoozed] = useState<boolean>(false);
const [archived, setArchived] = useState<boolean>(false); const [archived, setArchived] = useState<boolean>(false);
const [readNotification, setReadNotification] = useState<boolean>(false); const [readNotification, setReadNotification] = useState<boolean>(false);
const [fetchNotifications, setFetchNotifications] = useState<boolean>(false);
const [selectedNotificationForSnooze, setSelectedNotificationForSnooze] = useState<string | null>( const [selectedNotificationForSnooze, setSelectedNotificationForSnooze] = useState<string | null>(
null null
); );
@ -49,7 +50,7 @@ const useUserNotification = () => {
isValidating, isValidating,
mutate: notificationMutate, mutate: notificationMutate,
} = useSWRInfinite( } = useSWRInfinite(
workspaceSlug fetchNotifications && workspaceSlug
? (index, prevData) => ? (index, prevData) =>
getPaginatedNotificationKey(index, prevData, workspaceSlug.toString(), params) getPaginatedNotificationKey(index, prevData, workspaceSlug.toString(), params)
: () => null, : () => null,
@ -282,6 +283,7 @@ const useUserNotification = () => {
isLoadingMore, isLoadingMore,
hasMore, hasMore,
isRefreshing, isRefreshing,
setFetchNotifications,
}; };
}; };