Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Fix mobile notes pagination race

Anibal Svarcas89da105e6edc
diff --git a/src/components/atomic-crm/notes/NotesIteratorMobile.pagination.integration.test.tsx b/src/components/atomic-crm/notes/NotesIteratorMobile.pagination.integration.test.tsx
new file mode 100644
index 00000000..c01208ee
--- /dev/null
+++ b/src/components/atomic-crm/notes/NotesIteratorMobile.pagination.integration.test.tsx
@@ -0,0 +1,126 @@
+import { describe, expect, it, vi } from "vitest";
+import { render } from "vitest-browser-react";
+import { ListContextProvider } from "ra-core";
+import {
+ createCrmDb,
+ createCrmScenario,
+ CrmTestProvider,
+} from "@/test/browser/atomic-crm/crmUiHarness";
+import { NotesIteratorMobile } from "./NotesIteratorMobile";
+
+const singleNote = {
+ contact_id: 3,
+ date: "2026-03-17T13:50:00.000Z",
+ id: 9,
+ sales_id: 0,
+ status: "warm",
+ text: "Only note in the list",
+};
+
+// Trigger the infinite-pagination sentinel immediately in tests so we can
+// assert whether mounting the list would request another page.
+class ImmediateIntersectionObserver implements IntersectionObserver {
+ readonly root = null;
+ readonly rootMargin = "0px";
+ readonly thresholds = [0];
+
+ constructor(private readonly callback: IntersectionObserverCallback) {}
+
+ disconnect() {}
+
+ observe(target: Element) {
+ this.callback(
+ [
+ {
+ boundingClientRect: target.getBoundingClientRect(),
+ intersectionRatio: 1,
+ intersectionRect: target.getBoundingClientRect(),
+ isIntersecting: true,
+ rootBounds: null,
+ target,
+ time: Date.now(),
+ },
+ ],
+ this,
+ );
+ }
+
+ takeRecords() {
+ return [];
+ }
+
+ unobserve() {}
+}
+
+const NotesIteratorHarness = ({
+ setPage,
+}: {
+ setPage: ReturnType<typeof vi.fn>;
+}) => {
+ const listContext = {
+ data: [singleNote],
+ defaultTitle: undefined,
+ displayedFilters: {},
+ error: null,
+ exporter: false,
+ filterValues: {},
+ hasNextPage: false,
+ hasPreviousPage: false,
+ hideFilter: vi.fn(),
+ isFetching: false,
+ isLoading: false,
+ isPaused: false,
+ isPending: false,
+ onSelect: vi.fn(),
+ onSelectAll: vi.fn(),
+ onToggleItem: vi.fn(),
+ onUnselectItems: vi.fn(),
+ page: 1,
+ perPage: 25,
+ refetch: vi.fn(),
+ resource: "contact_notes",
+ selectedIds: [],
+ setFilters: vi.fn(),
+ setPage,
+ setPerPage: vi.fn(),
+ setSort: vi.fn(),
+ showFilter: vi.fn(),
+ sort: { field: "date", order: "DESC" },
+ total: 1,
+ } as any;
+
+ return (
+ <ListContextProvider value={listContext}>
+ <NotesIteratorMobile contactId={3} showStatus />
+ </ListContextProvider>
+ );
+};
+
+describe("NotesIteratorMobile pagination integration", () => {
+ it("does not request the next page on mount when the first page already contains the full result set", async () => {
+ const originalIntersectionObserver = window.IntersectionObserver;
+ window.IntersectionObserver =
+ ImmediateIntersectionObserver as unknown as typeof IntersectionObserver;
+
+ try {
+ const setPage = vi.fn();
+ const scenario = createCrmScenario({
+ db: createCrmDb(),
+ latency: 0,
+ });
+
+ const screen = await render(
+ <CrmTestProvider scenario={scenario}>
+ <NotesIteratorHarness setPage={setPage} />
+ </CrmTestProvider>,
+ );
+
+ await expect
+ .element(screen.getByText("Only note in the list"))
+ .toBeInTheDocument();
+ expect(setPage).not.toHaveBeenCalled();
+ } finally {
+ window.IntersectionObserver = originalIntersectionObserver;
+ }
+ });
+});
diff --git a/src/components/atomic-crm/notes/useAddInfinitePagination.integration.test.tsx b/src/components/atomic-crm/notes/useAddInfinitePagination.integration.test.tsx
new file mode 100644
index 00000000..cf883668
--- /dev/null
+++ b/src/components/atomic-crm/notes/useAddInfinitePagination.integration.test.tsx
@@ -0,0 +1,79 @@
+import { describe, expect, it, vi } from "vitest";
+import { render } from "vitest-browser-react";
+import { ListContextProvider } from "ra-core";
+import { useAddInfinitePagination } from "./useAddInfinitePagination";
+
+const firstPageNotes = [
+ { contact_id: 4, date: "2026-03-17T13:50:00.000Z", id: 9, sales_id: 0 },
+];
+
+type HookSnapshot = {
+ hasNextPage: boolean;
+ noteIds: number[];
+};
+
+// Keep the list context fixed on the initial first page so the test can
+// inspect what the hook exposes before that page is persisted into allPages.
+const HookStateHarness = ({ snapshots }: { snapshots: HookSnapshot[] }) => {
+ const listContext = {
+ data: firstPageNotes,
+ defaultTitle: undefined,
+ displayedFilters: {},
+ error: null,
+ exporter: false,
+ filterValues: {},
+ hasNextPage: false,
+ hasPreviousPage: false,
+ hideFilter: vi.fn(),
+ isFetching: false,
+ isLoading: false,
+ isPending: false,
+ isPaused: false,
+ onSelect: vi.fn(),
+ onSelectAll: vi.fn(),
+ onToggleItem: vi.fn(),
+ onUnselectItems: vi.fn(),
+ page: 1,
+ perPage: 25,
+ refetch: vi.fn(),
+ resource: "contact_notes",
+ selectedIds: [],
+ setFilters: vi.fn(),
+ setPage: vi.fn(),
+ setPerPage: vi.fn(),
+ setSort: vi.fn(),
+ showFilter: vi.fn(),
+ sort: { field: "date", order: "DESC" },
+ total: 1,
+ } as any;
+
+ return (
+ <ListContextProvider value={listContext}>
+ <HookStateHarnessBody snapshots={snapshots} />
+ </ListContextProvider>
+ );
+};
+
+const HookStateHarnessBody = ({ snapshots }: { snapshots: HookSnapshot[] }) => {
+ const { data, infinitePaginationContextValue } = useAddInfinitePagination();
+
+ snapshots.push({
+ hasNextPage: infinitePaginationContextValue.hasNextPage,
+ noteIds: data.map((note) => note.id),
+ });
+
+ return null;
+};
+
+describe("useAddInfinitePagination integration", () => {
+ it("exposes the current first-page data before allPages is populated", async () => {
+ const snapshots: HookSnapshot[] = [];
+
+ await render(<HookStateHarness snapshots={snapshots} />);
+
+ expect(snapshots[0]).toEqual({
+ hasNextPage: false,
+ noteIds: [9],
+ });
+ });
+});
diff --git a/src/components/atomic-crm/notes/useAddInfinitePagination.ts b/src/components/atomic-crm/notes/useAddInfinitePagination.ts
index 4e4f5004..8197e970 100644
--- a/src/components/atomic-crm/notes/useAddInfinitePagination.ts
+++ b/src/components/atomic-crm/notes/useAddInfinitePagination.ts
@@ -25,16 +25,27 @@ export const useAddInfinitePagination = () => {
.flatMap(([, pageData]) => pageData);
}, [allPages]);
+ const visibleData = useMemo(() => {
+ if (allLoadedData.length > 0) {
+ return allLoadedData;
+ }
+
+ // Before the first page is persisted into allPages, still expose the
+ // current list data so pagination logic doesn't behave as if the list were
+ // empty.
+ return page === 1 && data ? data : allLoadedData;
+ }, [allLoadedData, data, page]);
+
return {
isPending,
error,
- data: allLoadedData,
+ data: visibleData,
refetch,
infinitePaginationContextValue: {
fetchNextPage: async (): Promise<any> => {
setPage(page + 1);
},
- hasNextPage: allLoadedData.length < (total ?? 0),
+ hasNextPage: visibleData.length < (total ?? 0),
isFetchingNextPage: isPending,
fetchPreviousPage: async (): Promise<any> => {
setPage(page - 1);