Skip to content

Commit b56bd55

Browse files
committed
Pipelines: Migrate to page-based pagination
1 parent 680f437 commit b56bd55

File tree

3 files changed

+42
-59
lines changed

3 files changed

+42
-59
lines changed

frontend/src/components/pipeline/PipelinesTable.tsx

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ import {
1616
import { SkeletonTable } from "@patternfly/react-component-groups";
1717
import { LabelGroup } from "@patternfly/react-core";
1818
import { ExternalLinkAltIcon } from "@patternfly/react-icons";
19-
import { useInfiniteQuery } from "@tanstack/react-query";
19+
import { useQuery } from "@tanstack/react-query";
2020
import { Link } from "@tanstack/react-router";
2121
import { PipelineRun } from "../../apiDefinitions";
2222
import { pipelinesQueryOptions } from "../../queries/pipeline/pipelinesQuery";
2323
import coprLogo from "../../static/copr.ico";
2424
import kojiLogo from "../../static/koji.ico";
2525
import { ErrorConnection } from "../errors/ErrorConnection";
2626
import { ForgeIcon, ForgeIconByForge } from "../icons/ForgeIcon";
27-
import { LoadMore } from "../shared/LoadMore";
27+
import { PackitPagination } from "../shared/PackitPagination";
28+
import { PackitPaginationContext } from "../shared/PackitPaginationContext";
2829
import { Timestamp } from "../shared/Timestamp";
2930
import { StatusLabel } from "../statusLabels/StatusLabel";
3031
import { SyncReleaseTargetStatusLabel } from "../statusLabels/SyncReleaseTargetStatusLabel";
@@ -104,24 +105,20 @@ const columnNames = {
104105
external: "External",
105106
};
106107
export function PipelinesTable() {
107-
const {
108-
isLoading,
109-
isError,
110-
fetchNextPage,
111-
data,
112-
isFetchingNextPage,
113-
hasNextPage,
114-
} = useInfiniteQuery(pipelinesQueryOptions());
115-
116-
// Create a memoization of all the data when we flatten it out. Ideally one should render all the pages separately so that rendering will be done faster
117-
const rows = useMemo(() => (data ? data.pages.flat() : []), [data]);
108+
const [page, setPage] = useState(1);
109+
const [perPage, setPerPage] = useState(10);
110+
const value = { page, setPage, perPage, setPerPage };
111+
112+
const { isLoading, isError, data } = useQuery(
113+
pipelinesQueryOptions(page, perPage),
114+
);
118115

119116
const mappedRows = useMemo(
120117
() =>
121-
rows
122-
.filter((run) => run.trigger)
118+
data
119+
?.filter((run) => run.trigger)
123120
.map((run) => <PipelinesTableTr key={run.merged_run_id} run={run} />),
124-
[columnNames, rows],
121+
[columnNames, data],
125122
);
126123

127124
const TableHeads = [
@@ -142,30 +139,24 @@ export function PipelinesTable() {
142139
return <ErrorConnection />;
143140
}
144141

145-
if (isLoading) {
146-
return (
147-
<SkeletonTable
148-
variant={TableVariant.compact}
149-
rows={10}
150-
columns={TableHeads}
151-
/>
152-
);
153-
}
154-
155142
return (
156-
<>
157-
<Table aria-label="Pipeline runs" variant={TableVariant.compact}>
158-
<Thead>
159-
<Tr>{TableHeads}</Tr>
160-
</Thead>
161-
<Tbody>{mappedRows}</Tbody>
162-
</Table>
163-
<LoadMore
164-
isFetchingNextPage={isFetchingNextPage}
165-
hasNextPage={hasNextPage}
166-
fetchNextPage={() => void fetchNextPage()}
167-
/>
168-
</>
143+
<PackitPaginationContext.Provider value={value}>
144+
<PackitPagination />
145+
{isLoading ? (
146+
<SkeletonTable
147+
variant={TableVariant.compact}
148+
rows={perPage}
149+
columns={TableHeads}
150+
/>
151+
) : (
152+
<Table aria-label="Pipeline runs" variant={TableVariant.compact}>
153+
<Thead>
154+
<Tr>{TableHeads}</Tr>
155+
</Thead>
156+
<Tbody>{mappedRows}</Tbody>
157+
</Table>
158+
)}
159+
</PackitPaginationContext.Provider>
169160
);
170161
}
171162

frontend/src/queries/pipeline/pipelines.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ import { PipelineRun } from "../../apiDefinitions";
55

66
export interface fetchPipelinesProps {
77
pageParam: number;
8+
perPage: number;
89
signal?: AbortSignal;
910
}
1011

1112
// Fetch data from dashboard backend (or if we want, directly from the API)
1213
export const fetchPipelines = async ({
1314
pageParam = 1,
15+
perPage,
1416
signal,
1517
}: fetchPipelinesProps): Promise<PipelineRun[]> => {
1618
const data = await fetch(
17-
`${import.meta.env.VITE_API_URL}/runs?page=${pageParam}`,
19+
`${import.meta.env.VITE_API_URL}/runs?page=${pageParam}&per_page=${perPage}`,
1820
{ signal },
1921
)
2022
.then((response) => response.json())
Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
// Copyright Contributors to the Packit project.
22
// SPDX-License-Identifier: MIT
33

4-
import { infiniteQueryOptions } from "@tanstack/react-query";
4+
import { queryOptions } from "@tanstack/react-query";
55
import { fetchPipelines } from "./pipelines";
66

7-
export const pipelinesQueryOptions = () =>
8-
infiniteQueryOptions({
9-
queryKey: ["pipeline"],
10-
queryFn: async ({ pageParam, signal }) =>
11-
await fetchPipelines({ pageParam, signal }),
12-
initialPageParam: 1,
13-
getNextPageParam: (lastPage, _allPages, lastPageParam) => {
14-
if (lastPage.length === 0) {
15-
return undefined;
16-
}
17-
return lastPageParam + 1;
18-
},
19-
getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => {
20-
if (firstPageParam <= 1) {
21-
return undefined;
22-
}
23-
return firstPageParam - 1;
24-
},
7+
export const pipelinesQueryOptions = (
8+
pageParam: number,
9+
perPage: number = 20,
10+
) =>
11+
queryOptions({
12+
queryKey: ["pipeline", { pageParam, perPage }],
13+
queryFn: async ({ signal }) =>
14+
await fetchPipelines({ pageParam, perPage, signal }),
2515
});

0 commit comments

Comments
 (0)