Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node: Fix job list pagination #74

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 deletions src/components/JobList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import { ReactNode } from "react";
import DescriptionIcon from "@mui/icons-material/Description";
import Typography from "@mui/material/Typography";
import Box from "@mui/material/Box";
import type {
DecodedValueMap,
QueryParamConfigMap,
SetQuery,
} from "use-query-params";
import {
useMaterialReactTable,
MaterialReactTable,
type MRT_ColumnDef,
type MRT_Row,
type MRT_PaginationState,
type MRT_Updater,
} from 'material-react-table';
import type { UseQueryResult } from "@tanstack/react-query";
import { type Theme } from "@mui/material/styles";
Expand All @@ -21,6 +28,8 @@ import useDefaultTableOptions from "../../lib/table";
import sentryIcon from "./assets/sentry.svg";


const DEFAULT_PAGE_SIZE = 25;

const columns: MRT_ColumnDef<Job>[] = [
{
header: "status",
Expand Down Expand Up @@ -196,18 +205,53 @@ function JobDetailPanel(props: JobDetailPanelProps): ReactNode {
};

type JobListProps = {
params: DecodedValueMap<QueryParamConfigMap>;
setter: SetQuery<QueryParamConfigMap>;
manualPagination: Boolean;
query: UseQueryResult<Run> | UseQueryResult<NodeJobs>;
sortMode?: "time" | "id";
}

export default function JobList({ query, sortMode }: JobListProps) {
export default function JobList({
query,
params,
setter,
manualPagination,
sortMode,
}: JobListProps) {
const options = useDefaultTableOptions<Job>();
const data = query.data?.jobs || [];
const onPaginationChange = (updater: MRT_Updater<MRT_PaginationState>) => {
if (!(updater instanceof Function)) return;
const newParams = updater({
pageSize: params.pageSize,
pageIndex: params.page,
});
setter({
page: newParams.pageIndex || 0,
pageSize: newParams.pageSize || DEFAULT_PAGE_SIZE,
});
};
options.state = {};
if (manualPagination === true) {
options.manualPagination = true;
options.rowCount = Infinity;
options.onPaginationChange = onPaginationChange;
options.state.pagination = {
pageIndex: params.page || 0,
pageSize: params.pageSize || DEFAULT_PAGE_SIZE,
};
} else {
options.initialState = {
pagination: { pageIndex: 0, pageSize: DEFAULT_PAGE_SIZE },
};
}
options.state.isLoading = query.isLoading || query.isFetching;
const table = useMaterialReactTable({
rowCount: data.length,
...options,
columns,
data: data,
rowCount: data.length,
enableFacetedValues: true,
initialState: {
...options.initialState,
Expand All @@ -217,20 +261,13 @@ export default function JobList({ query, sortMode }: JobListProps) {
duration: false,
waiting: false,
},
pagination: {
pageIndex: 0,
pageSize: 25,
},
sorting: [
{
id: sortMode === "time"? "started" : "job_id",
desc: true,
},
],
},
state: {
isLoading: query.isLoading || query.isFetching,
},
renderDetailPanel: JobDetailPanel,
muiTableBodyRowProps: ({row, isDetailPanel}) => {
if ( isDetailPanel ) {
Expand Down
12 changes: 9 additions & 3 deletions src/pages/Node/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { NodeParams } from "../../lib/paddles.d";
import { useNode, useNodeJobs } from "../../lib/paddles";

export default function Node() {
const [params, _] = useQueryParams({
const [params, setParams] = useQueryParams({
page: NumberParam,
pageSize: NumberParam,
});
Expand All @@ -23,7 +23,7 @@ export default function Node() {

if (jobsQuery === null) return <Typography>404</Typography>;
if (jobsQuery.isError) return null;

return (
<div>
<Helmet>
Expand All @@ -36,7 +36,13 @@ export default function Node() {
<div>
<div>
<NodeList query={detailsQuery} />
<JobList query={jobsQuery} sortMode={"time"} />
<JobList
query={jobsQuery}
params={params}
setter={setParams}
manualPagination={true}
sortMode={"time"}
/>
</div>
</div>
</div>
Expand Down
Loading