Skip to content

Commit

Permalink
Created custom hook to handle fetch on scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
lokanandaprabhu committed Oct 24, 2024
1 parent 203a42a commit 5bbdaf4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 35 deletions.
23 changes: 4 additions & 19 deletions src/components/pipelineRuns-list/PipelineRunsList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as React from 'react';
import { useTranslation } from 'react-i18next';
import { useParams } from 'react-router-dom-v5-compat';
import { SortByDirection } from '@patternfly/react-table';
import {
ListPageBody,
Expand All @@ -11,8 +13,7 @@ import { usePipelineRunsFilters } from './usePipelineRunsFilters';
import { PipelineRunKind } from '../../types';
import { useGetPipelineRuns } from '../hooks/useTektonResult';
import PipelineRunsRow from './PipelineRunsRow';
import { useTranslation } from 'react-i18next';
import { useParams } from 'react-router-dom-v5-compat';
import { useLoadMoreOnScroll } from '../utils/tekton-results';

import './PipelineRunsList.scss';

Expand Down Expand Up @@ -56,23 +57,7 @@ const PipelineRunsList: React.FC<PipelineRunsListProps> = ({
filters,
);

// Once OCPBUGS-43538 ticket is closed, use onRowsRendered prop in VirtualizedTable for load more on scroll
React.useEffect(() => {
if (!loadMoreRef.current || !pipelineRunsLoaded) return;

const observer = new IntersectionObserver((entries) => {
const entry = entries[0];
if (entry.isIntersecting && nextPageToken) {
nextPageToken();
}
});

observer.observe(loadMoreRef.current);

return () => {
observer.disconnect();
};
}, [nextPageToken, pipelineRunsLoaded]);
useLoadMoreOnScroll(loadMoreRef, nextPageToken, pipelineRunsLoaded);

return (
<ListPageBody>
Expand Down
18 changes: 2 additions & 16 deletions src/components/pipelines-tasks/TaskRunsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { TaskRunModel } from '../../models';
import { ALL_NAMESPACES_KEY, TektonResourceLabel } from '../../consts';
import { getReferenceForModel } from '../pipelines-overview/utils';
import { useTaskRunsFilters } from './useTaskRunsFilters';
import { useLoadMoreOnScroll } from '../utils/tekton-results';

interface TaskRunsListPageProps {
showTitle?: boolean;
Expand Down Expand Up @@ -123,23 +124,8 @@ const TaskRunsList: React.FC<TaskRunsListPageProps> = ({
useTaskRunsFilters(),
);

// Once OCPBUGS-43538 ticket is closed, use onRowsRendered prop in VirtualizedTable for load more on scroll
React.useEffect(() => {
if (!loadMoreRef.current || !loaded) return;
useLoadMoreOnScroll(loadMoreRef, nextPageToken, loaded);

const observer = new IntersectionObserver((entries) => {
const entry = entries[0];
if (entry.isIntersecting && nextPageToken) {
nextPageToken();
}
});

observer.observe(loadMoreRef.current);

return () => {
observer.disconnect();
};
}, [nextPageToken, loaded]);
return (
<>
<ListPageBody>
Expand Down
22 changes: 22 additions & 0 deletions src/components/utils/tekton-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
k8sGet,
} from '@openshift-console/dynamic-plugin-sdk';
import _ from 'lodash';
import { RefObject, useEffect } from 'react';
import {
ALL_NAMESPACES_KEY,
DELETED_RESOURCE_IN_K8S_ANNOTATION,
Expand Down Expand Up @@ -491,3 +492,24 @@ export const getTaskRunLog = async (taskRunPath: string): Promise<string> => {
)}`;
return consoleProxyFetchLog({ url, method: 'GET', allowInsecure: true });
};

export const useLoadMoreOnScroll = (
loadMoreRef: RefObject<HTMLElement>,
nextPageToken: (() => void) | null,
loaded: boolean,
) => {
useEffect(() => {
if (!loadMoreRef.current || !loaded) return;

const observer = new IntersectionObserver((entries) => {
const entry = entries[0];
if (entry.isIntersecting && nextPageToken) {
nextPageToken();
}
});
observer.observe(loadMoreRef.current);
return () => {
observer.disconnect();
};
}, [nextPageToken, loaded, loadMoreRef]);
};

0 comments on commit 5bbdaf4

Please sign in to comment.