Skip to content

fix: fix workspaces pagination #19448

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
Aug 21, 2025
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: 2 additions & 2 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1187,9 +1187,9 @@ class ApiMethods {
};

getWorkspaces = async (
options: TypesGen.WorkspacesRequest,
req: TypesGen.WorkspacesRequest,
): Promise<TypesGen.WorkspacesResponse> => {
const url = getURLWithSearchParams("/api/v2/workspaces", options);
const url = getURLWithSearchParams("/api/v2/workspaces", req);
const response = await this.axios.get<TypesGen.WorkspacesResponse>(url);
return response.data;
};
Expand Down
11 changes: 5 additions & 6 deletions site/src/api/queries/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,14 @@ async function findMatchWorkspace(q: string): Promise<Workspace | undefined> {
}
}

function workspacesKey(config: WorkspacesRequest = {}) {
const { q, limit } = config;
return ["workspaces", { q, limit }] as const;
function workspacesKey(req: WorkspacesRequest = {}) {
return ["workspaces", req] as const;
}

export function workspaces(config: WorkspacesRequest = {}) {
export function workspaces(req: WorkspacesRequest = {}) {
return {
queryKey: workspacesKey(config),
queryFn: () => API.getWorkspaces(config),
queryKey: workspacesKey(req),
queryFn: () => API.getWorkspaces(req),
} as const satisfies QueryOptions<WorkspacesResponse>;
}

Expand Down
61 changes: 61 additions & 0 deletions site/src/pages/WorkspacesPage/WorkspacesPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,67 @@ describe("WorkspacesPage", () => {
MockStoppedWorkspace.latest_build.template_version_id,
);
});

it("correctly handles pagination by including pagination parameters in query key", async () => {
const totalWorkspaces = 50;
const workspacesPage1 = Array.from({ length: 25 }, (_, i) => ({
...MockWorkspace,
id: `page1-workspace-${i}`,
name: `page1-workspace-${i}`,
}));
const workspacesPage2 = Array.from({ length: 25 }, (_, i) => ({
...MockWorkspace,
id: `page2-workspace-${i}`,
name: `page2-workspace-${i}`,
}));

const getWorkspacesSpy = jest.spyOn(API, "getWorkspaces");

getWorkspacesSpy.mockImplementation(({ offset }) => {
switch (offset) {
case 0:
return Promise.resolve({
workspaces: workspacesPage1,
count: totalWorkspaces,
});
case 25:
return Promise.resolve({
workspaces: workspacesPage2,
count: totalWorkspaces,
});
default:
return Promise.reject(new Error("Unexpected offset"));
}
});

const user = userEvent.setup();
renderWithAuth(<WorkspacesPage />);

await waitFor(() => {
expect(screen.getByText("page1-workspace-0")).toBeInTheDocument();
});

expect(getWorkspacesSpy).toHaveBeenLastCalledWith({
q: "owner:me",
offset: 0,
limit: 25,
});

const nextPageButton = screen.getByRole("button", { name: /next page/i });
await user.click(nextPageButton);

await waitFor(() => {
expect(screen.getByText("page2-workspace-0")).toBeInTheDocument();
});

expect(getWorkspacesSpy).toHaveBeenLastCalledWith({
q: "owner:me",
offset: 25,
limit: 25,
});

expect(screen.queryByText("page1-workspace-0")).not.toBeInTheDocument();
});
});

const getWorkspaceCheckbox = (workspace: Workspace) => {
Expand Down
3 changes: 2 additions & 1 deletion site/src/pages/WorkspacesPage/WorkspacesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ const WorkspacesPage: FC = () => {
});

const workspacesQueryOptions = workspaces({
...pagination,
limit: pagination.limit,
offset: pagination.offset,
q: filterState.filter.query,
});
const { data, error, refetch } = useQuery({
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy