Skip to content

Commit

Permalink
✨ Use InfiniteScroller pattern for the Task Drawer (konveyor#2049)
Browse files Browse the repository at this point in the history
Implement InfiniteScroller based on oVirt VmPortal implementation and
use it to fetch additional tasks in the Task Drawer.

Key features:
1. fetch data using useInfiniteQuery from React Query
2. track sentinel visibility using IntersectionObserver API
3. monitor item count to prevent extra fetchMore calls
4. ensure that state setter is not called on unmounted component (from 
    IntersectionObserver callback)

Resolves: konveyor#1973
Reference-Url: https://github.com/oVirt/ovirt-web-ui/blob/dfe0c4b8c92638f6e41b9fe0b09e0d07509618ae/src/components/VmsList/index.js#L50

---------

Signed-off-by: Radoslaw Szwajkowski <rszwajko@redhat.com>
Co-authored-by: Scott Dickerson <sdickers@redhat.com>
  • Loading branch information
rszwajko and sjd78 committed Sep 9, 2024
1 parent 16f166f commit 7cd3ebb
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 65 deletions.
1 change: 1 addition & 0 deletions client/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
"issuesEffortTooltip": "This column shows the effort weight for a single issue incident.",
"dependentQuestionTooltip": "This question is conditionally included or excluded based on tags:",
"jiraInstanceNotConnected": "Jira instance {{name}} is not connected.",
"loadingTripleDot": "Loading...",
"manageDependenciesInstructions": "Add northbound and southbound dependencies for the selected application here. Note that any selections made will be saved automatically. To undo any changes, you must manually delete the applications from the dropdowns.",
"noDataAvailableBody": "No data available to be shown here.",
"noDataAvailableTitle": "No data available",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.infinite-scroll-sentinel {
width: 100%;
text-align: center;
font-style: italic;
font-weight: bold;
}
61 changes: 61 additions & 0 deletions client/src/app/components/InfiniteScroller/InfiniteScroller.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React, { ReactNode, useEffect, useRef } from "react";
import { useTranslation } from "react-i18next";
import { useVisibilityTracker } from "./useVisibilityTracker";
import "./InfiniteScroller.css";

export interface InfiniteScrollerProps {
// content to display
children: ReactNode;
// function triggered if sentinel node is visible to load more items
// returns false if call was rejected by the scheduler
fetchMore: () => boolean;
hasMore: boolean;
// number of items currently displayed/known
itemCount: number;
}

export const InfiniteScroller = ({
children,
fetchMore,
hasMore,
itemCount,
}: InfiniteScrollerProps) => {
const { t } = useTranslation();
// Track how many items were known at time of triggering the fetch.
// This allows to detect edge case when second(or more) fetchMore() is triggered before
// IntersectionObserver is able to detect out-of-view event.
// Initializing with zero ensures that the effect will be triggered immediately
// (parent is expected to display empty state until some items are available).
const itemCountRef = useRef(0);
const { visible: isSentinelVisible, nodeRef: sentinelRef } =
useVisibilityTracker({
enable: hasMore,
});

useEffect(
() => {
if (
isSentinelVisible &&
itemCountRef.current !== itemCount &&
fetchMore() // fetch may be blocked if background refresh is in progress (or other manual fetch)
) {
// fetchMore call was triggered (it may fail but will be subject to React Query retry policy)
itemCountRef.current = itemCount;
}
},
// reference to fetchMore() changes based on query state and ensures that the effect is triggered in the right moment
// i.e. after fetch triggered by the previous fetchMore() call finished
[isSentinelVisible, fetchMore, itemCount]
);

return (
<div>
{children}
{hasMore && (
<div ref={sentinelRef} className="infinite-scroll-sentinel">
{t("message.loadingTripleDot")}
</div>
)}
</div>
);
};
1 change: 1 addition & 0 deletions client/src/app/components/InfiniteScroller/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./InfiniteScroller";
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { useEffect, useRef, useState, useCallback } from "react";

export function useVisibilityTracker({ enable }: { enable: boolean }) {
const nodeRef = useRef<HTMLDivElement>(null);
const [visible, setVisible] = useState(false);
const node = nodeRef.current;

// state is set from IntersectionObserver callbacks which may not align with React lifecycle
// we can add extra safety by using the same approach as Console's useSafetyFirst() hook
// https://github.com/openshift/console/blob/9d4a9b0a01b2de64b308f8423a325f1fae5f8726/frontend/packages/console-dynamic-plugin-sdk/src/app/components/safety-first.tsx#L10
const mounted = useRef(true);
useEffect(
() => () => {
mounted.current = false;
},
[]
);
const setVisibleSafe = useCallback((newValue) => {
if (mounted.current) {
setVisible(newValue);
}
}, []);

useEffect(() => {
if (!enable || !node) {
return undefined;
}

// Observer with default options - the whole view port used.
// Note that if root element is used then it needs to be the ancestor of the target.
// In case of infinite scroller the target is always within the (scrollable!)parent
// even if the node is technically hidden from the user.
// https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#root
const observer = new IntersectionObserver(
(entries: IntersectionObserverEntry[]) =>
entries.forEach(({ isIntersecting }) => {
if (isIntersecting) {
setVisibleSafe(true);
} else {
setVisibleSafe(false);
}
})
);
observer.observe(node);

return () => {
observer.disconnect();
setVisibleSafe(false);
};
}, [enable, node, setVisibleSafe]);

return {
/**
* Is the node referenced via `nodeRef` currently visible on the page?
*/
visible,
/**
* A ref to a node whose visibility will be tracked. This should be set as a ref to a
* relevant dom element by the component using this hook.
*/
nodeRef,
};
}
148 changes: 84 additions & 64 deletions client/src/app/components/task-manager/TaskManagerDrawer.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import React, { forwardRef, useMemo, useState } from "react";
import React, { forwardRef, useCallback, useMemo, useState } from "react";
import { Link } from "react-router-dom";
import dayjs from "dayjs";
import {
Dropdown,
DropdownItem,
DropdownList,
EmptyState,
EmptyStateBody,
EmptyStateHeader,
EmptyStateIcon,
EmptyStateVariant,
MenuToggle,
MenuToggleElement,
NotificationDrawer,
Expand All @@ -20,17 +15,23 @@ import {
NotificationDrawerListItemBody,
NotificationDrawerListItemHeader,
Tooltip,
EmptyState,
EmptyStateHeader,
EmptyStateIcon,
EmptyStateBody,
EmptyStateVariant,
} from "@patternfly/react-core";
import { CubesIcon, EllipsisVIcon } from "@patternfly/react-icons";
import { EllipsisVIcon, CubesIcon } from "@patternfly/react-icons";
import { css } from "@patternfly/react-styles";

import { Task, TaskState } from "@app/api/models";
import { useTaskManagerContext } from "./TaskManagerContext";
import { useServerTasks } from "@app/queries/tasks";
import { useInfiniteServerTasks } from "@app/queries/tasks";

import "./TaskManagerDrawer.css";
import { TaskStateIcon } from "../Icons";
import { useTaskActions } from "@app/pages/tasks/useTaskActions";
import { InfiniteScroller } from "../InfiniteScroller";

/** A version of `Task` specific for the task manager drawer components */
interface TaskManagerTask {
Expand Down Expand Up @@ -65,7 +66,7 @@ interface TaskManagerDrawerProps {
export const TaskManagerDrawer: React.FC<TaskManagerDrawerProps> = forwardRef(
(_props, ref) => {
const { isExpanded, setIsExpanded, queuedCount } = useTaskManagerContext();
const { tasks } = useTaskManagerData();
const { tasks, hasNextPage, fetchNextPage } = useTaskManagerData();

const [expandedItems, setExpandedItems] = useState<number[]>([]);
const [taskWithExpandedActions, setTaskWithExpandedAction] = useState<
Expand Down Expand Up @@ -101,26 +102,32 @@ export const TaskManagerDrawer: React.FC<TaskManagerDrawerProps> = forwardRef(
</EmptyStateBody>
</EmptyState>
) : (
<NotificationDrawerList>
{tasks.map((task) => (
<TaskItem
key={task.id}
task={task}
expanded={expandedItems.includes(task.id)}
onExpandToggle={(expand) => {
setExpandedItems(
expand
? [...expandedItems, task.id]
: expandedItems.filter((i) => i !== task.id)
);
}}
actionsExpanded={task.id === taskWithExpandedActions}
onActionsExpandToggle={(flag: boolean) =>
setTaskWithExpandedAction(flag && task.id)
}
/>
))}
</NotificationDrawerList>
<InfiniteScroller
fetchMore={fetchNextPage}
hasMore={hasNextPage}
itemCount={tasks?.length ?? 0}
>
<NotificationDrawerList>
{tasks.map((task) => (
<TaskItem
key={task.id}
task={task}
expanded={expandedItems.includes(task.id)}
onExpandToggle={(expand) => {
setExpandedItems(
expand
? [...expandedItems, task.id]
: expandedItems.filter((i) => i !== task.id)
);
}}
actionsExpanded={task.id === taskWithExpandedActions}
onActionsExpandToggle={(flag: boolean) =>
setTaskWithExpandedAction(flag && task.id)
}
/>
))}
</NotificationDrawerList>
</InfiniteScroller>
)}
</NotificationDrawerBody>
</NotificationDrawer>
Expand Down Expand Up @@ -221,12 +228,13 @@ const TaskItem: React.FC<{
};

const useTaskManagerData = () => {
const [pageSize, setPageSize] = useState(PAGE_SIZE);
const increasePageSize = () => {
setPageSize(pageSize + PAGE_SIZE);
};

const { result, isFetching } = useServerTasks(
const {
data,
fetchNextPage,
hasNextPage = false,
isFetching,
isFetchingNextPage,
} = useInfiniteServerTasks(
{
filters: [{ field: "state", operator: "=", value: "queued" }],
sort: {
Expand All @@ -235,48 +243,60 @@ const useTaskManagerData = () => {
},
page: {
pageNumber: 1,
itemsPerPage: pageSize,
itemsPerPage: PAGE_SIZE,
},
},
5000
);

const tasks: TaskManagerTask[] = useMemo(
() =>
!result.data
? []
: result.data.map(
(task) =>
({
id: task.id ?? -1,
createUser: task.createUser ?? "",
updateUser: task.updateUser ?? "",
createTime: task.createTime ?? "",
started: task.started ?? "",
terminated: task.terminated ?? "",
name: task.name,
kind: task.kind,
addon: task.addon,
extensions: task.extensions,
state: task.state ?? "",
priority: task.priority ?? 0,
applicationId: task.application.id,
applicationName: task.application.name,
preemptEnabled: task?.policy?.preemptEnabled ?? false,
data?.pages
?.flatMap((data) => data?.data ?? [])
?.map(
(task) =>
({
id: task.id ?? -1,
createUser: task.createUser ?? "",
updateUser: task.updateUser ?? "",
createTime: task.createTime ?? "",
started: task.started ?? "",
terminated: task.terminated ?? "",
name: task.name,
kind: task.kind,
addon: task.addon,
extensions: task.extensions,
state: task.state ?? "",
priority: task.priority ?? 0,
applicationId: task.application.id,
applicationName: task.application.name,
preemptEnabled: task?.policy?.preemptEnabled ?? false,

_: task,
_: task,

// TODO: Add any checks that could be needed later...
// - isCancelable (does the current user own the task? other things to check?)
// - isPreemptionToggleAllowed
}) as TaskManagerTask
),
[result.data]
// TODO: Add any checks that could be needed later...
// - isCancelable (does the current user own the task? other things to check?)
// - isPreemptionToggleAllowed
}) as TaskManagerTask
) ?? [],
[data]
);

const fetchMore = useCallback(() => {
// forced fetch is not allowed when background fetch or other forced fetch is in progress
if (!isFetching && !isFetchingNextPage) {
fetchNextPage();
return true;
} else {
return false;
}
}, [isFetching, isFetchingNextPage, fetchNextPage]);

return {
tasks,
increasePageSize,
isFetching,
hasNextPage,
fetchNextPage: fetchMore,
isReadyToFetch: !isFetching && !isFetchingNextPage,
};
};
Loading

0 comments on commit 7cd3ebb

Please sign in to comment.