The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Fix admin > people pagination race conditions (#72888)
Nemanja Glumaceb80f4b1395b
diff --git a/e2e/test/scenarios/admin-2/people.cy.spec.js b/e2e/test/scenarios/admin-2/people.cy.spec.jsindex 700a920faa0..31b39390064 100644--- a/e2e/test/scenarios/admin-2/people.cy.spec.js+++ b/e2e/test/scenarios/admin-2/people.cy.spec.js@@ -549,41 +549,50 @@ describe("scenarios > admin > people", () => {beforeEach(() => {generateUsers(NEW_USERS);- cy.intercept("GET", "/api/user*").as("users");+ cy.intercept("GET", "/api/user?query*").as("users");cy.intercept("GET", "/api/permissions/membership").as("memberships");});it("should allow paginating people forward and backward", () => {const PAGE_SIZE = 25;- cy.visit("/admin/people");+ const footer = () =>+ cy+ .findByTestId("people-list-footer")+ .scrollIntoView()+ .should("be.visible");- // Total- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage- cy.findByText(`${NEW_TOTAL_USERS} people found`);+ cy.visit("/admin/people");+ cy.findByTestId("admin-panel")+ .findByText("Loading...")+ .should("not.exist");+ cy.wait("@users");- // Page 1- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage- cy.findByText(`1 - ${PAGE_SIZE}`);+ cy.log("Page 1");assertTableRowsCount(PAGE_SIZE);- cy.findByLabelText("Previous page").should("be.disabled");+ footer()+ .should("contain", `${NEW_TOTAL_USERS} people found`)+ .and("contain", `1 - ${PAGE_SIZE}`);- cy.findByTestId("next-page-btn").click();- cy.wait("@users");+ footer().within(() => {+ cy.findByLabelText("Previous page").should("be.disabled");- // Page 2- cy.findByTestId("people-list-footer")- .findByText(`${PAGE_SIZE + 1} - ${NEW_TOTAL_USERS}`)- .should("be.visible");+ cy.findByLabelText("Next page").click();+ cy.wait("@users");+ });++ cy.log("Page 2");assertTableRowsCount(NEW_TOTAL_USERS % PAGE_SIZE);- cy.findByLabelText("Next page").should("be.disabled");+ footer().should("contain", `${PAGE_SIZE + 1} - ${NEW_TOTAL_USERS}`);- cy.findByLabelText("Previous page").click();+ cy.log("Back to the Page 1");+ footer().within(() => {+ cy.findByLabelText("Next page").should("be.disabled");+ cy.findByLabelText("Previous page").click();+ });- // Page 1- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage- cy.findByText(`1 - ${PAGE_SIZE}`);assertTableRowsCount(PAGE_SIZE);+ footer().should("contain", `1 - ${PAGE_SIZE}`);});it("should allow paginating group members forward and backward", () => {diff --git a/frontend/src/metabase/admin/people/containers/PeopleListingApp/PeopleListingApp.tsx b/frontend/src/metabase/admin/people/containers/PeopleListingApp/PeopleListingApp.tsxindex 80f3a02c5e0..986d8cfaef9 100644--- a/frontend/src/metabase/admin/people/containers/PeopleListingApp/PeopleListingApp.tsx+++ b/frontend/src/metabase/admin/people/containers/PeopleListingApp/PeopleListingApp.tsx@@ -82,11 +82,14 @@ export function PeopleListingApp({}};+ // Only reset status if it actually needs changing. Calling `updateStatus`+ // unconditionally triggers `setPage(0)`, which races any in-flight+ // pagination clicks when this effect re-fires on query resolution.useEffect(() => {- if (!hasDeactivatedUsers) {- updateStatus("active");+ if (!hasDeactivatedUsers && status !== ACTIVE_STATUS.active) {+ updateStatus(ACTIVE_STATUS.active);}- }, [hasDeactivatedUsers, updateStatus]);+ }, [hasDeactivatedUsers, status, updateStatus]);const pageTitle = useMemo(() => {if (!isUsingTenants) {diff --git a/frontend/src/metabase/admin/people/hooks/use-people-query.ts b/frontend/src/metabase/admin/people/hooks/use-people-query.tsindex 3396565d3b0..f596f0d284b 100644--- a/frontend/src/metabase/admin/people/hooks/use-people-query.ts+++ b/frontend/src/metabase/admin/people/hooks/use-people-query.ts@@ -1,4 +1,4 @@-import { useCallback, useEffect, useMemo, useState } from "react";+import { useCallback, useEffect, useMemo, useRef, useState } from "react";import { usePagination } from "metabase/common/hooks/use-pagination";import { SEARCH_DEBOUNCE_DURATION } from "metabase/utils/constants";@@ -20,7 +20,17 @@ export const usePeopleQuery = (pageSize: number, tenancy: UserTenancy) => {const [searchText, setSearchText] = useState("");+ // Skip the mount-fire of this effect. Otherwise its trailing `setPage(0)`+ // races any pagination clicks that land within SEARCH_DEBOUNCE_DURATION+ // of mount and silently snaps the user back to page 0.+ const isInitialMountRef = useRef(true);+useEffect(() => {+ if (isInitialMountRef.current) {+ isInitialMountRef.current = false;+ return;+ }+const timerId = setTimeout(() => {const searchText =searchInputValue.length >= MIN_SEARCH_LENGTH ? searchInputValue : "";diff --git a/frontend/src/metabase/common/hooks/use-pagination.ts b/frontend/src/metabase/common/hooks/use-pagination.tsindex 242cf74f483..e697faad7ed 100644--- a/frontend/src/metabase/common/hooks/use-pagination.ts+++ b/frontend/src/metabase/common/hooks/use-pagination.ts@@ -8,7 +8,7 @@ export const usePagination = (initialPage = 0) => {[setPage],);const handlePreviousPage = useCallback(- () => setPage((prev) => prev - 1),+ () => setPage((prev) => Math.max(0, prev - 1)),[setPage],);diff --git a/frontend/src/metabase/common/hooks/use-pagination.unit.spec.ts b/frontend/src/metabase/common/hooks/use-pagination.unit.spec.tsindex 3280017c596..de5f39e86a3 100644--- a/frontend/src/metabase/common/hooks/use-pagination.unit.spec.ts+++ b/frontend/src/metabase/common/hooks/use-pagination.unit.spec.ts@@ -13,4 +13,19 @@ describe("usePagination", () => {act(() => result.current.resetPage());expect(result.current.page).toEqual(initialPage);});++ // Defensive floor guard: the UI normally disables "Previous" on page 0, but+ // fast/automated interactions can fire a click before the disabled state+ // lands, which previously sent offset=-pageSize to the backend. See DEV-1835.+ it("should not decrement 'page' below 0 when 'handlePreviousPage' is called on page 0", () => {+ const { result } = renderHook(() => usePagination());++ expect(result.current.page).toEqual(0);++ act(() => result.current.handlePreviousPage());+ expect(result.current.page).toEqual(0);++ act(() => result.current.handlePreviousPage());+ expect(result.current.page).toEqual(0);+ });});