Skip to content

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

Use InfiniteListBase for contact and deal notes

Anibal Svarcase71808382195
diff --git a/src/components/atomic-crm/contacts/ContactShow.tsx b/src/components/atomic-crm/contacts/ContactShow.tsx
index 9436437e..9cf7629e 100644
--- a/src/components/atomic-crm/contacts/ContactShow.tsx
+++ b/src/components/atomic-crm/contacts/ContactShow.tsx
@@ -1,5 +1,6 @@
import { useState } from "react";
import {
+ InfiniteListBase,
RecordRepresentation,
ShowBase,
useShowContext,
@@ -8,7 +9,6 @@ import {
import type { ShowBaseProps } from "ra-core";
import { useIsMobile } from "@/hooks/use-mobile";
import { ReferenceField } from "@/components/admin/reference-field";
-import { ReferenceManyField } from "@/components/admin/reference-many-field";
import { TextField } from "@/components/admin/text-field";
import { Card, CardContent } from "@/components/ui/card";
import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/components/ui/tabs";
@@ -148,10 +148,13 @@ const ContactShowContentMobile = () => {
</TabsList>
<TabsContent value="notes" className="mt-2">
- <ReferenceManyField
- target="contact_id"
- reference="contact_notes"
+ <InfiniteListBase
+ resource="contact_notes"
+ filter={{ contact_id: record.id }}
sort={{ field: "date", order: "DESC" }}
+ perPage={25}
+ disableSyncWithLocation
+ storeKey={false}
empty={
<div className="flex flex-col items-center justify-center py-8 text-center">
<p className="text-muted-foreground mb-4">
@@ -167,18 +170,14 @@ const ContactShowContentMobile = () => {
}
loading={false}
error={false}
- queryOptions={
- {
- onError: () => {
- /** override to hide notification as error case is handled by NotesIteratorMobile */
- },
- // We want infinite pagination so we need to disable placeHolder data to avoid flicker duplicating previous page before showing the new one
- placeholderData: null,
- } as any // fixme: remove once https://github.com/marmelab/react-admin/pull/11166 is released
- }
+ queryOptions={{
+ onError: () => {
+ /** override to hide notification as error case is handled by NotesIteratorMobile */
+ },
+ }}
>
<NotesIteratorMobile contactId={record.id} showStatus />
- </ReferenceManyField>
+ </InfiniteListBase>
</TabsContent>
<TabsContent value="tasks" className="mt-4">
@@ -271,22 +270,19 @@ const ContactShowContent = () => {
</ReferenceField>
</div>
</div>
- <ReferenceManyField
- target="contact_id"
- reference="contact_notes"
+ <InfiniteListBase
+ resource="contact_notes"
+ filter={{ contact_id: record.id }}
sort={{ field: "date", order: "DESC" }}
+ perPage={25}
+ disableSyncWithLocation
+ storeKey={false}
empty={
<NoteCreate reference="contacts" showStatus className="mt-4" />
}
- queryOptions={{
- // We want infinite pagination so we need to disable placeHolder data to avoid flicker duplicating previous page before showing the new one
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- //@ts-expect-error
- placeholderData: null,
- }}
>
<NotesIterator reference="contacts" showStatus />
- </ReferenceManyField>
+ </InfiniteListBase>
</CardContent>
</Card>
</div>
diff --git a/src/components/atomic-crm/deals/DealShow.tsx b/src/components/atomic-crm/deals/DealShow.tsx
index 61248b97..8e422c38 100644
--- a/src/components/atomic-crm/deals/DealShow.tsx
+++ b/src/components/atomic-crm/deals/DealShow.tsx
@@ -2,6 +2,7 @@ import { useMutation } from "@tanstack/react-query";
import { isValid } from "date-fns";
import { Archive, ArchiveRestore } from "lucide-react";
import {
+ InfiniteListBase,
ShowBase,
useDataProvider,
useNotify,
@@ -15,7 +16,6 @@ import { DeleteButton } from "@/components/admin/delete-button";
import { EditButton } from "@/components/admin/edit-button";
import { ReferenceArrayField } from "@/components/admin/reference-array-field";
import { ReferenceField } from "@/components/admin/reference-field";
-import { ReferenceManyField } from "@/components/admin/reference-many-field";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent } from "@/components/ui/dialog";
@@ -168,14 +168,17 @@ const DealShowContent = () => {
<div className="m-4">
<Separator className="mb-4" />
- <ReferenceManyField
- target="deal_id"
- reference="deal_notes"
+ <InfiniteListBase
+ resource="deal_notes"
+ filter={{ deal_id: record.id }}
sort={{ field: "date", order: "DESC" }}
+ perPage={25}
+ disableSyncWithLocation
+ storeKey={false}
empty={<NoteCreate reference={"deals"} />}
>
<NotesIterator reference="deals" />
- </ReferenceManyField>
+ </InfiniteListBase>
</div>
</div>
</div>
diff --git a/src/components/atomic-crm/notes/NotesIterator.tsx b/src/components/atomic-crm/notes/NotesIterator.tsx
index 3f905638..a5647d68 100644
--- a/src/components/atomic-crm/notes/NotesIterator.tsx
+++ b/src/components/atomic-crm/notes/NotesIterator.tsx
@@ -1,11 +1,10 @@
-import { InfinitePaginationContext } from "ra-core";
+import { useListContext } from "ra-core";
import { Fragment } from "react";
import { Separator } from "@/components/ui/separator";
import { Note } from "./Note";
import { NoteCreate } from "./NoteCreate";
import { InfinitePagination } from "../misc/InfinitePagination";
-import { useAddInfinitePagination } from "./useAddInfinitePagination";
export const NotesIterator = ({
reference,
@@ -14,32 +13,28 @@ export const NotesIterator = ({
reference: "contacts" | "deals";
showStatus?: boolean;
}) => {
- const { infinitePaginationContextValue, isPending, error, data } =
- useAddInfinitePagination();
+ const { isPending, error, data = [] } = useListContext();
if (isPending || error) return null;
return (
- <InfinitePaginationContext.Provider value={infinitePaginationContextValue}>
- <div className="mt-4">
- <NoteCreate reference={reference} showStatus={showStatus} />
- {data.length && (
- <div className="mt-4 space-y-4">
- {data.map((note, index) => (
- <Fragment key={index}>
- <Note
- note={note}
- isLast={index === data.length - 1}
- key={index}
- showStatus={showStatus}
- />
- {index < data.length - 1 && <Separator />}
- </Fragment>
- ))}
- </div>
- )}
- <InfinitePagination />
- </div>
- </InfinitePaginationContext.Provider>
+ <div className="mt-4">
+ <NoteCreate reference={reference} showStatus={showStatus} />
+ {data.length > 0 && (
+ <div className="mt-4 space-y-4">
+ {data.map((note, index) => (
+ <Fragment key={note.id}>
+ <Note
+ note={note}
+ isLast={index === data.length - 1}
+ showStatus={showStatus}
+ />
+ {index < data.length - 1 && <Separator />}
+ </Fragment>
+ ))}
+ </div>
+ )}
+ <InfinitePagination />
+ </div>
);
};
diff --git a/src/components/atomic-crm/notes/NotesIteratorMobile.tsx b/src/components/atomic-crm/notes/NotesIteratorMobile.tsx
index a9261be1..f8ac3d50 100644
--- a/src/components/atomic-crm/notes/NotesIteratorMobile.tsx
+++ b/src/components/atomic-crm/notes/NotesIteratorMobile.tsx
@@ -1,7 +1,7 @@
import type { Identifier } from "ra-core";
import {
- InfinitePaginationContext,
useGetIdentity,
+ useListContext,
useTimeout,
useTranslate,
} from "ra-core";
@@ -16,7 +16,6 @@ import { Status } from "../misc/Status";
import { useGetSalesName } from "../sales/useGetSalesName";
import type { ContactNote } from "../types";
import { InfinitePagination } from "../misc/InfinitePagination";
-import { useAddInfinitePagination } from "./useAddInfinitePagination";
export const NotesIteratorMobile = ({
contactId,
@@ -25,8 +24,8 @@ export const NotesIteratorMobile = ({
contactId: Identifier;
showStatus?: boolean;
}) => {
- const { data, error, isPending, infinitePaginationContextValue, refetch } =
- useAddInfinitePagination();
+ const { data = [], error, isPending, refetch } =
+ useListContext<ContactNote>();
const translate = useTranslate();
const oneSecondHasPassed = useTimeout(1000);
if (isPending) {
@@ -70,9 +69,9 @@ export const NotesIteratorMobile = ({
}
return (
- <InfinitePaginationContext.Provider value={infinitePaginationContextValue}>
+ <>
<div className="divide-y">
- {data?.map((note) => (
+ {data.map((note) => (
<NoteMobile
key={note.id}
note={note}
@@ -82,7 +81,7 @@ export const NotesIteratorMobile = ({
))}
</div>
<InfinitePagination />
- </InfinitePaginationContext.Provider>
+ </>
);
};
diff --git a/src/components/atomic-crm/notes/useAddInfinitePagination.test.tsx b/src/components/atomic-crm/notes/useAddInfinitePagination.test.tsx
deleted file mode 100644
index cf883668..00000000
--- a/src/components/atomic-crm/notes/useAddInfinitePagination.test.tsx
+++ /dev/null
@@ -1,79 +0,0 @@
-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
deleted file mode 100644
index 8197e970..00000000
--- a/src/components/atomic-crm/notes/useAddInfinitePagination.ts
+++ /dev/null
@@ -1,57 +0,0 @@
-import { useListContext } from "ra-core";
-import { useEffect, useMemo, useState } from "react";
-
-/**
- * Turn a standard list context into an infinite pagination context by keeping track of all loaded pages and their data.
- * This allows to use the InfinitePagination component inside a standard List component.
- */
-export const useAddInfinitePagination = () => {
- const { data, error, isPending, page, setPage, total, refetch } =
- useListContext();
-
- const [allPages, setAllPages] = useState<Record<number, any[]>>({});
-
- useEffect(() => {
- if (!data) {
- return;
- }
- setAllPages((prev) => ({ ...prev, [page]: data }));
- }, [page, setAllPages, data]);
-
- const allLoadedData = useMemo(() => {
- // Sort pages by page number and flatten the data
- return Object.entries(allPages)
- .sort(([pageA], [pageB]) => Number(pageA) - Number(pageB))
- .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: visibleData,
- refetch,
- infinitePaginationContextValue: {
- fetchNextPage: async (): Promise<any> => {
- setPage(page + 1);
- },
- hasNextPage: visibleData.length < (total ?? 0),
- isFetchingNextPage: isPending,
- fetchPreviousPage: async (): Promise<any> => {
- setPage(page - 1);
- },
- hasPreviousPage: page > 1,
- isFetchingPreviousPage: false,
- },
- };
-};