Skip to content

Commit

Permalink
Effects moved to callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
StaNov committed Jan 28, 2025
1 parent 348413d commit c57086f
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions webapp/src/component/layout/Notifications/NotificationsPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import { BoxLoading } from 'tg.component/common/BoxLoading';
import { PopoverProps } from '@mui/material/Popover';
import { notificationComponents } from 'tg.component/layout/Notifications/NotificationTypeMap';
import { NotificationsChanged } from 'tg.websocket-client/WebsocketClient';
import { components } from 'tg.service/apiSchema.generated';
import { InfiniteData } from 'react-query';

type PagedModelNotificationModel =
components['schemas']['PagedModelNotificationModel'];

const FETCH_NEXT_PAGE_SCROLL_THRESHOLD_IN_PIXELS = 100;

Expand All @@ -25,6 +30,14 @@ const StyledListItemHeader = styled(ListItem)`
font-weight: bold;
`;

function getNotifications(
data: InfiniteData<PagedModelNotificationModel> | undefined
) {
return data?.pages
.flatMap((it) => it?._embedded?.notificationModelList)
.filter((it) => it !== undefined);
}

type NotificationsPopupProps = {
onClose: () => void;
onNotificationsChanged: (event: NotificationsChanged) => void;
Expand All @@ -45,7 +58,10 @@ export const NotificationsPopup: React.FC<NotificationsPopupProps> = ({
method: 'get',
query: query,
options: {
enabled: false,
enabled: !!anchorEl,
refetchOnMount: false,
staleTime: Infinity,
cacheTime: Infinity,
getNextPageParam: (lastPage) => {
if (
lastPage.page &&
Expand All @@ -61,6 +77,18 @@ export const NotificationsPopup: React.FC<NotificationsPopupProps> = ({
return null;
}
},
onSuccess(data) {
const markAsSeenIds = getNotifications(data)?.map((it) => it.id);
if (!markAsSeenIds) return;

markSeenMutation.mutate({
content: {
'application/json': {
notificationIds: markAsSeenIds,
},
},
});
},
},
});

Expand All @@ -69,33 +97,6 @@ export const NotificationsPopup: React.FC<NotificationsPopupProps> = ({
method: 'put',
});

const notifications = notificationsLoadable.data?.pages
.flatMap((it) => it?._embedded?.notificationModelList)
.filter((it) => it !== undefined);

useEffect(() => {
if (!anchorEl) return;

const data = notificationsLoadable.data;
if (!data) {
if (!notificationsLoadable.isFetching) {
notificationsLoadable.refetch();
}
return;
}

const markAsSeenIds = notifications?.map((it) => it.id);
if (!markAsSeenIds) return;

markSeenMutation.mutate({
content: {
'application/json': {
notificationIds: markAsSeenIds,
},
},
});
}, [notificationsLoadable.data, anchorEl]);

useEffect(() => {
if (client && user) {
return client.subscribe(
Expand All @@ -110,6 +111,8 @@ export const NotificationsPopup: React.FC<NotificationsPopupProps> = ({
}
}, [user, client]);

const notifications = getNotifications(notificationsLoadable.data);

return (
<StyledMenu
keepMounted
Expand Down Expand Up @@ -148,7 +151,7 @@ export const NotificationsPopup: React.FC<NotificationsPopupProps> = ({
<StyledListItemHeader divider>
<T keyName="notifications-header" />
</StyledListItemHeader>
{notifications?.map((notification, i) => {
{notifications?.map((notification) => {
const Component = notificationComponents[notification.type]!;
return (
<Component
Expand Down

0 comments on commit c57086f

Please sign in to comment.