Skip to content

Commit

Permalink
Wait for live notifications to stop fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni K committed Oct 31, 2024
1 parent dca9aac commit d2032ec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
11 changes: 7 additions & 4 deletions apps/web/src/components/NotificationList/NotificationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ const NotificationList = ({
const [searchParams] = useSearchParams();
const onlyUnread =
searchParams.has("unread") && searchParams.get("unread") !== null;
const [{ data: maxPagesData, fetching }] = useQuery({
const [{ data: maxPagesData, fetching: fetchingMaxPages }] = useQuery({
query: MaxNotificationPages_Query,
variables: {
where: { onlyUnread },
},
});
const [{ data }] = usePollingQuery(
const [{ data, fetching: fetchingLiveNotifications }] = usePollingQuery(
{
query: NotificationPolling_Query,
pause: !live || fetching,
pause: !live || fetchingMaxPages,
variables: {
where: {
createdAt: {
Expand Down Expand Up @@ -119,14 +119,17 @@ const NotificationList = ({
<NotificationListPage
key={`notification-page-${currentPage}`}
page={currentPage}
exclude={liveIds}
excludeIds={liveIds}
isLastPage={currentPage === pagesToLoad}
onlyUnread={onlyUnread}
inDialog={inDialog}
onRead={onRead}
{...((!paginate || limit) && {
first: limit ?? 100,
})}
fetchingLiveNotifications={
fetchingLiveNotifications || fetchingMaxPages
}
/>
);
})}
Expand Down
48 changes: 30 additions & 18 deletions apps/web/src/components/NotificationList/NotificationListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ interface NotificationPageProps {
page: number;
onlyUnread?: boolean;
isLastPage?: boolean;
exclude?: Scalars["UUID"]["input"][];
excludeIds?: Scalars["UUID"]["input"][];
first?: number;
inDialog?: boolean;
onRead?: () => void;
fetchingLiveNotifications?: boolean;
}

const NotificationListPage = ({
Expand All @@ -56,15 +57,17 @@ const NotificationListPage = ({
isLastPage,
inDialog,
onRead,
exclude = [],
excludeIds = [],
fetchingLiveNotifications,
}: NotificationPageProps) => {
const intl = useIntl();
const [searchParams] = useSearchParams();
searchParams.set("page", `${page + 1}`);
const [{ data, fetching }] = useQuery({
query: Notifications_Query,
pause: fetchingLiveNotifications,
variables: {
excludeIds: exclude,
excludeIds,
first: first ?? PER_PAGE,
page,
where: {
Expand All @@ -75,34 +78,43 @@ const NotificationListPage = ({

const notifications = unpackMaybes(data?.notifications?.data);

const isLoading =
(fetching || fetchingLiveNotifications) && excludeIds.length === 0;

const showNullMessage =
notifications.length === 0 &&
page === 1 &&
!fetching &&
exclude.length === 0;
!fetchingLiveNotifications &&
excludeIds.length === 0;

const firstNewNotification = useRef<HTMLAnchorElement & HTMLButtonElement>(
null,
);

return (
<>
{notifications.length > 0 ? (
{isLoading ? (
<Loading inline />
) : (
<>
{notifications.map((notification, index) => (
<NotificationItem
key={notification.id}
focusRef={
index === 0 && page !== 1 ? firstNewNotification : undefined
}
notification={notification}
inDialog={inDialog}
onRead={onRead}
/>
))}
{notifications.length > 0 ? (
<>
{notifications.map((notification, index) => (
<NotificationItem
key={notification.id}
focusRef={
index === 0 && page !== 1 ? firstNewNotification : undefined
}
notification={notification}
inDialog={inDialog}
onRead={onRead}
/>
))}
</>
) : null}
</>
) : null}
{fetching && exclude.length === 0 && <Loading inline />}
)}
{showNullMessage && (
<NotificationPortal.Portal
containerId={NULL_MESSAGE_ROOT_ID}
Expand Down

0 comments on commit d2032ec

Please sign in to comment.