A full-featured CRM built with React, shadcn/ui, and Supabase.
add infinite paginatio to notes iterator
ThieryMichela37b91c949b7
diff --git a/src/components/atomic-crm/contacts/ContactAside.tsx b/src/components/atomic-crm/contacts/ContactAside.tsxindex f9542387..6ec7a0ee 100644--- a/src/components/atomic-crm/contacts/ContactAside.tsx+++ b/src/components/atomic-crm/contacts/ContactAside.tsx@@ -45,6 +45,7 @@ export const ContactAside = ({ link = "edit" }: { link?: "edit" | "show" }) => {target="contact_id"reference="tasks"sort={{ field: "due_date", order: "ASC" }}+ perPage={1000}><TasksIterator /></ReferenceManyField>diff --git a/src/components/atomic-crm/contacts/ContactShow.tsx b/src/components/atomic-crm/contacts/ContactShow.tsxindex 4cc43d7c..100a6c44 100644--- a/src/components/atomic-crm/contacts/ContactShow.tsx+++ b/src/components/atomic-crm/contacts/ContactShow.tsx@@ -249,6 +249,12 @@ const ContactShowContent = () => {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>diff --git a/src/components/atomic-crm/notes/NotesIterator.tsx b/src/components/atomic-crm/notes/NotesIterator.tsxindex 695fb3ac..6d778c9e 100644--- a/src/components/atomic-crm/notes/NotesIterator.tsx+++ b/src/components/atomic-crm/notes/NotesIterator.tsx@@ -1,9 +1,10 @@-import { useListContext } from "ra-core";-import { Fragment } from "react";+import { InfinitePaginationContext, useListContext } from "ra-core";+import { Fragment, useEffect, useMemo, useState } from "react";import { Separator } from "@/components/ui/separator";import { Note } from "./Note";import { NoteCreate } from "./NoteCreate";+import { InfinitePagination } from "../misc/InfinitePagination";export const NotesIterator = ({reference,@@ -12,26 +13,58 @@ export const NotesIterator = ({reference: "contacts" | "deals";showStatus?: boolean;}) => {- const { data, error, isPending } = useListContext();+ const { data, error, isPending, page, setPage, total } = useListContext();++ const [allPages, setAllPages] = useState<Record<number, any[]>>({});++ useEffect(() => {+ if (!data) {+ return;+ }+ setAllPages((prev) => ({ ...prev, [page]: data }));+ }, [page, setAllPages, data]);++ const allLoadedData = useMemo(() => {+ return Object.entries(allPages)+ .sort(([pageA], [pageB]) => Number(pageA) - Number(pageB))+ .flatMap(([, pageData]) => pageData);+ }, [allPages]);+if (isPending || error) return null;return (- <div className="mt-4">- <NoteCreate reference={reference} showStatus={showStatus} />- {data && (- <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>- )}- </div>+ <InfinitePaginationContext.Provider+ value={{+ fetchNextPage: async (): Promise<any> => {+ setPage(page + 1);+ },+ hasNextPage: allLoadedData.length < total,+ isFetchingNextPage: isPending,+ fetchPreviousPage: async (): Promise<any> => {+ setPage(page - 1);+ },+ hasPreviousPage: page > 1,+ isFetchingPreviousPage: false,+ }}+ >+ <div className="mt-4">+ <NoteCreate reference={reference} showStatus={showStatus} />+ {allLoadedData.length && (+ <div className="mt-4 space-y-4">+ {allLoadedData.map((note, index) => (+ <Fragment key={index}>+ <Note+ note={note}+ isLast={index === allLoadedData.length - 1}+ key={index}+ showStatus={showStatus}+ />+ {index < allLoadedData.length - 1 && <Separator />}+ </Fragment>+ ))}+ </div>+ )}+ <InfinitePagination />+ </div>+ </InfinitePaginationContext.Provider>);};diff --git a/src/components/ui/progress.tsx b/src/components/ui/progress.tsxindex 10af7e63..42ac3160 100644--- a/src/components/ui/progress.tsx+++ b/src/components/ui/progress.tsx@@ -1,5 +1,5 @@import * as React from "react"-import * as ProgressPrimitive from "@radix-ui/react-progress"+import { Progress as ProgressPrimitive } from "radix-ui"import { cn } from "@/lib/utils"@@ -26,4 +26,21 @@ function Progress({)}-export { Progress }+// @TODO: Replace me by shadcn-admin-kit progressAuto component when it will be released+function ProgressAuto(+ props: React.ComponentProps<typeof ProgressPrimitive.Root>+) {+ const [progress, setProgress] = React.useState(20)++ React.useEffect(() => {+ const timer = setTimeout(+ () => setProgress((prev) => (prev + 20) % 100),+ 1500+ )+ return () => clearTimeout(timer)+ }, [progress])++ return <Progress {...props} value={progress} />+}++export { Progress, ProgressAuto }