Skip to content

fix(site): display not found page when pagination page is invalid #12611

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

Merged
merged 5 commits into from
Mar 18, 2024
Merged
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
4 changes: 4 additions & 0 deletions site/src/components/PaginationWidget/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ describe(buildPagedList.name, () => {
expect(uniqueCount).toEqual(result.length);
}
});

it("works for invalid active page number", () => {
expect(buildPagedList(2, 4)).toEqual([1, 2]);
});
});

describe(getOffset.name, () => {
Expand Down
9 changes: 7 additions & 2 deletions site/src/components/PaginationWidget/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ export const buildPagedList = (
return range(1, numPages);
}

const isInvalidActivePage = activePage > numPages || activePage < 1;
const pageBeforeLast = numPages - 1;
const startPage = Math.max(activePage - PAGE_NEIGHBORS, 2);
const endPage = Math.min(activePage + PAGE_NEIGHBORS, pageBeforeLast);
const startPage = isInvalidActivePage
? 1 + PAGE_NEIGHBORS
: Math.max(activePage - PAGE_NEIGHBORS, 2);
const endPage = isInvalidActivePage
? numPages - PAGE_NEIGHBORS
: Math.min(activePage + PAGE_NEIGHBORS, pageBeforeLast);

let pages: ReturnType<typeof buildPagedList> = range(startPage, endPage);

Expand Down
9 changes: 9 additions & 0 deletions site/src/pages/WorkspacesPage/WorkspacesPageView.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,12 @@ export const Error: Story = {
error: mockApiError({ message: "Something went wrong" }),
},
};

export const InvalidPageNumber: Story = {
args: {
workspaces: [],
count: 200,
limit: 25,
page: 1000,
},
};
63 changes: 46 additions & 17 deletions site/src/pages/WorkspacesPage/WorkspacesPageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import KeyboardArrowDownOutlined from "@mui/icons-material/KeyboardArrowDownOutl
import PlayArrowOutlined from "@mui/icons-material/PlayArrowOutlined";
import StopOutlined from "@mui/icons-material/StopOutlined";
import LoadingButton from "@mui/lab/LoadingButton";
import Button from "@mui/material/Button";
import Divider from "@mui/material/Divider";
import type { ComponentProps } from "react";
import type { UseQueryResult } from "react-query";
import { hasError, isApiValidationError } from "api/errors";
import type { Template, Workspace } from "api/typesGenerated";
import { ErrorAlert } from "components/Alert/ErrorAlert";
import { EmptyState } from "components/EmptyState/EmptyState";
import { Margins } from "components/Margins/Margins";
import {
MoreMenu,
Expand Down Expand Up @@ -85,6 +87,11 @@ export const WorkspacesPageView = ({
canCreateTemplate,
canChangeVersions,
}: WorkspacesPageViewProps) => {
// Let's say the user has 5 workspaces, but tried to hit page 100, which does
// not exist. In this case, the page is not valid and we want to show a better
// error message.
const invalidPageNumber = page !== 1 && workspaces?.length === 0;

return (
<Margins>
<PageHeader
Expand Down Expand Up @@ -168,26 +175,48 @@ export const WorkspacesPageView = ({
</MoreMenu>
</>
) : (
<PaginationHeader
paginationUnitLabel="workspaces"
limit={limit}
totalRecords={count}
currentOffsetStart={(page - 1) * limit + 1}
css={{ paddingBottom: "0" }}
/>
!invalidPageNumber && (
<PaginationHeader
paginationUnitLabel="workspaces"
limit={limit}
totalRecords={count}
currentOffsetStart={(page - 1) * limit + 1}
css={{ paddingBottom: "0" }}
/>
)
)}
</TableToolbar>

<WorkspacesTable
canCreateTemplate={canCreateTemplate}
workspaces={workspaces}
isUsingFilter={filterProps.filter.used}
onUpdateWorkspace={onUpdateWorkspace}
checkedWorkspaces={checkedWorkspaces}
onCheckChange={onCheckChange}
canCheckWorkspaces={canCheckWorkspaces}
templates={templates}
/>
{invalidPageNumber ? (
<EmptyState
css={(theme) => ({
border: `1px solid ${theme.palette.divider}`,
borderRadius: theme.shape.borderRadius,
})}
message="Page not found"
description="The page you are trying to access does not exist."
cta={
<Button
onClick={() => {
onPageChange(1);
}}
>
Back to the first page
</Button>
}
/>
) : (
<WorkspacesTable
canCreateTemplate={canCreateTemplate}
workspaces={workspaces}
isUsingFilter={filterProps.filter.used}
onUpdateWorkspace={onUpdateWorkspace}
checkedWorkspaces={checkedWorkspaces}
onCheckChange={onCheckChange}
canCheckWorkspaces={canCheckWorkspaces}
templates={templates}
/>
)}

{count !== undefined && (
// Temporary styling stopgap before component is migrated to using
Expand Down