Skip to content

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

Add infinite pagination to the activity log

François Zaninotto42dd3a3dd1dd
diff --git a/src/components/atomic-crm/activity/ActivityLog.tsx b/src/components/atomic-crm/activity/ActivityLog.tsx
index b6538d30..71a5a563 100644
--- a/src/components/atomic-crm/activity/ActivityLog.tsx
+++ b/src/components/atomic-crm/activity/ActivityLog.tsx
@@ -1,12 +1,6 @@
-import { Button } from "@/components/ui/button";
-import { Separator } from "@/components/ui/separator";
-import { Skeleton } from "@/components/ui/skeleton";
-import { useQuery } from "@tanstack/react-query";
+import { InfiniteListBase } from "ra-core";
import type { Identifier } from "ra-core";
-import { useDataProvider, useTranslate } from "ra-core";
-import { RotateCcw } from "lucide-react";
-import type { CrmDataProvider } from "../providers/types";
import { ActivityLogContext } from "./ActivityLogContext";
import { ActivityLogIterator } from "./ActivityLogIterator";
@@ -21,55 +15,17 @@ export function ActivityLog({
pageSize = 20,
context = "all",
}: ActivityLogProps) {
- const dataProvider = useDataProvider<CrmDataProvider>();
- const translate = useTranslate();
- const { data, isPending, error, refetch } = useQuery({
- queryKey: ["activityLog", companyId],
- queryFn: () => dataProvider.getActivityLog(companyId),
- });
-
- if (isPending) {
- return (
- <div className="mt-1">
- {Array.from({ length: 5 }).map((_, index) => (
- <div className="space-y-2 mt-1" key={index}>
- <div className="flex flex-row space-x-2 items-center">
- <Skeleton className="w-5 h-5 rounded-full" />
- <Skeleton className="w-full h-4" />
- </div>
- <Skeleton className="w-full h-12" />
- <Separator />
- </div>
- ))}
- </div>
- );
- }
-
- if (error && !data) {
- return (
- <div className="p-4">
- <div className="text-center text-muted-foreground mb-4">
- {translate("crm.dashboard.latest_activity_error", {
- _: "Error loading latest activity",
- })}
- </div>
- <div className="text-center mt-2">
- <Button
- onClick={() => {
- refetch();
- }}
- >
- <RotateCcw />
- {translate("crm.common.retry")}
- </Button>
- </div>
- </div>
- );
- }
-
return (
<ActivityLogContext.Provider value={context}>
- <ActivityLogIterator activities={data} pageSize={pageSize} />
+ <InfiniteListBase
+ resource="activity_log"
+ filter={companyId ? { company_id: companyId } : {}}
+ sort={{ field: "date", order: "DESC" }}
+ perPage={pageSize}
+ disableSyncWithLocation
+ >
+ <ActivityLogIterator />
+ </InfiniteListBase>
</ActivityLogContext.Provider>
);
}
diff --git a/src/components/atomic-crm/activity/ActivityLogIterator.tsx b/src/components/atomic-crm/activity/ActivityLogIterator.tsx
index 441b0391..ca87bf32 100644
--- a/src/components/atomic-crm/activity/ActivityLogIterator.tsx
+++ b/src/components/atomic-crm/activity/ActivityLogIterator.tsx
@@ -1,7 +1,15 @@
-import { Fragment, useState } from "react";
-import { useTranslate } from "ra-core";
+import { Fragment } from "react";
+import {
+ useListContext,
+ useInfinitePaginationContext,
+ useTranslate,
+} from "ra-core";
+import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
+import { Skeleton } from "@/components/ui/skeleton";
+import { Spinner } from "@/components/admin/spinner";
+import { RotateCcw } from "lucide-react";
import {
COMPANY_CREATED,
CONTACT_CREATED,
@@ -15,44 +23,84 @@ import { ActivityLogContactCreated } from "./ActivityLogContactCreated";
import { ActivityLogContactNoteCreated } from "./ActivityLogContactNoteCreated";
import { ActivityLogDealCreated } from "./ActivityLogDealCreated";
import { ActivityLogDealNoteCreated } from "./ActivityLogDealNoteCreated";
+import { InfinitePagination } from "../misc/InfinitePagination";
+import { useIsMobile } from "@/hooks/use-mobile";
-type ActivityLogIteratorProps = {
- activities: Activity[];
- pageSize: number;
-};
-
-export function ActivityLogIterator({
- activities,
- pageSize,
-}: ActivityLogIteratorProps) {
- const [activitiesDisplayed, setActivityDisplayed] = useState(pageSize);
+export function ActivityLogIterator() {
+ const isMobile = useIsMobile();
+ const { data, isPending, error, refetch } = useListContext<Activity>();
+ const { hasNextPage, fetchNextPage, isFetchingNextPage } =
+ useInfinitePaginationContext();
const translate = useTranslate();
- const filteredActivities = activities.slice(0, activitiesDisplayed);
+ if (isPending) {
+ return (
+ <div className="mt-1">
+ {Array.from({ length: 5 }).map((_, index) => (
+ <div className="space-y-2 mt-1" key={index}>
+ <div className="flex flex-row space-x-2 items-center">
+ <Skeleton className="w-5 h-5 rounded-full" />
+ <Skeleton className="w-full h-4" />
+ </div>
+ <Skeleton className="w-full h-12" />
+ <Separator />
+ </div>
+ ))}
+ </div>
+ );
+ }
+
+ if (error && !data?.length) {
+ return (
+ <div className="p-4">
+ <div className="text-center text-muted-foreground mb-4">
+ {translate("crm.dashboard.latest_activity_error", {
+ _: "Error loading latest activity",
+ })}
+ </div>
+ <div className="text-center mt-2">
+ <Button onClick={() => refetch()}>
+ <RotateCcw />
+ {translate("crm.common.retry")}
+ </Button>
+ </div>
+ </div>
+ );
+ }
return (
<div className="space-y-4">
- {filteredActivities.map((activity, index) => (
+ {data?.map((activity, index) => (
<Fragment key={index}>
- <ActivityItem key={activity.id} activity={activity} />
- <Separator />
+ <ActivityItem activity={activity} />
+ {index < data.length - 1 && <Separator />}
</Fragment>
))}
- {activitiesDisplayed < activities.length && (
+ {/* Desktop: explicit Load More button */}
+ {!isMobile && hasNextPage && (
<a
href="#"
onClick={(e) => {
e.preventDefault();
- setActivityDisplayed(
- (activitiesDisplayed) => activitiesDisplayed + pageSize,
- );
+ fetchNextPage();
}}
className="flex w-full justify-center text-sm underline hover:no-underline"
>
- {translate("crm.activity.load_more")}
+ {isFetchingNextPage ? (
+ <Spinner />
+ ) : (
+ translate("crm.activity.load_more")
+ )}
</a>
)}
+
+ {/* Mobile: auto-load on scroll via IntersectionObserver */}
+ {isMobile && (
+ <div className="flex justify-center">
+ <InfinitePagination />
+ </div>
+ )}
</div>
);
}
diff --git a/src/components/atomic-crm/providers/fakerest/dataProvider.ts b/src/components/atomic-crm/providers/fakerest/dataProvider.ts
index 8d24d1bc..92b19b06 100644
--- a/src/components/atomic-crm/providers/fakerest/dataProvider.ts
+++ b/src/components/atomic-crm/providers/fakerest/dataProvider.ts
@@ -167,6 +167,20 @@ export const createDataProvider = ({
const dataProviderWithCustomMethod: CrmDataProvider = {
...baseDataProvider,
+ async getList(resource: string, params: any) {
+ if (resource === "activity_log") {
+ const { filter = {}, pagination } = params;
+ const all = await getActivityLog(
+ withSupabaseFilterAdapter(baseDataProvider),
+ filter.company_id,
+ filter.sales_id,
+ );
+ const { page, perPage } = pagination;
+ const start = (page - 1) * perPage;
+ return { data: all.slice(start, start + perPage), total: all.length };
+ }
+ return baseDataProvider.getList(resource, params);
+ },
unarchiveDeal: async (deal: Deal) => {
// get all deals where stage is the same as the deal to unarchive
const { data: deals } = await baseDataProvider.getList<Deal>("deals", {
@@ -192,10 +206,6 @@ export const createDataProvider = ({
),
);
},
- // We simulate a remote endpoint that is in charge of returning activity log
- getActivityLog: async (companyId?: Identifier) => {
- return getActivityLog(dataProvider, companyId);
- },
signUp: async ({
email,
password,
diff --git a/src/components/atomic-crm/providers/supabase/dataProvider.ts b/src/components/atomic-crm/providers/supabase/dataProvider.ts
index 17be17c1..0fc6ba7d 100644
--- a/src/components/atomic-crm/providers/supabase/dataProvider.ts
+++ b/src/components/atomic-crm/providers/supabase/dataProvider.ts
@@ -16,7 +16,6 @@ import type {
SignUpData,
} from "../../types";
import type { ConfigurationContextValue } from "../../root/ConfigurationContext";
-import { getActivityLog } from "../commons/activity";
import { ATTACHMENTS_BUCKET } from "../commons/attachments";
import { getIsInitialized } from "./authProvider";
import { supabase } from "./supabase";
@@ -62,6 +61,23 @@ const dataProviderWithCustomMethods = {
if (resource === "contacts") {
return baseDataProvider.getList("contacts_summary", params);
}
+ if (resource === "activity_log") {
+ const { data, total } = await baseDataProvider.getList(
+ "activity_log",
+ params,
+ );
+ // Rename snake_case view columns to camelCase to match Activity type
+ return {
+ data: data.map((row: any) => ({
+ ...row,
+ contactNote: row.contact_note ?? undefined,
+ dealNote: row.deal_note ?? undefined,
+ contact_note: undefined,
+ deal_note: undefined,
+ })),
+ total,
+ };
+ }
return baseDataProvider.getList(resource, params);
},
@@ -195,9 +211,6 @@ const dataProviderWithCustomMethods = {
),
);
},
- async getActivityLog(companyId?: Identifier) {
- return getActivityLog(baseDataProvider, companyId);
- },
async isInitialized() {
return getIsInitialized();
},
diff --git a/supabase/migrations/20260314120000_activity_log_view.sql b/supabase/migrations/20260314120000_activity_log_view.sql
new file mode 100644
index 00000000..a80f7eda
--- /dev/null
+++ b/supabase/migrations/20260314120000_activity_log_view.sql
@@ -0,0 +1,79 @@
+-- Create a view that aggregates all activity events into a single, pageable list.
+-- Each row represents one event (company created, contact created, note created, etc.)
+-- sorted by date so PostgREST can paginate with Range headers.
+
+create view "public"."activity_log"
+ with (security_invoker=on)
+ as
+SELECT
+ 'company.' || c.id || '.created' as id,
+ 'company.created' as type,
+ c.created_at as date,
+ c.id as company_id,
+ c.sales_id,
+ to_json(c.*) as company,
+ NULL::json as contact,
+ NULL::json as deal,
+ NULL::json as contact_note,
+ NULL::json as deal_note
+FROM companies c
+UNION ALL
+SELECT
+ 'contact.' || co.id || '.created',
+ 'contact.created',
+ co.first_seen,
+ co.company_id,
+ co.sales_id,
+ NULL::json,
+ to_json(co.*),
+ NULL::json,
+ NULL::json,
+ NULL::json
+FROM contacts co
+UNION ALL
+SELECT
+ 'contactNote.' || cn.id || '.created',
+ 'contactNote.created',
+ cn.date,
+ co.company_id,
+ cn.sales_id,
+ NULL::json,
+ NULL::json,
+ NULL::json,
+ to_json(cn.*),
+ NULL::json
+FROM contact_notes cn
+LEFT JOIN contacts co ON co.id = cn.contact_id
+UNION ALL
+SELECT
+ 'deal.' || d.id || '.created',
+ 'deal.created',
+ d.created_at,
+ d.company_id,
+ d.sales_id,
+ NULL::json,
+ NULL::json,
+ to_json(d.*),
+ NULL::json,
+ NULL::json
+FROM deals d
+UNION ALL
+SELECT
+ 'dealNote.' || dn.id || '.created',
+ 'dealNote.created',
+ dn.date,
+ d.company_id,
+ dn.sales_id,
+ NULL::json,
+ NULL::json,
+ NULL::json,
+ NULL::json,
+ to_json(dn.*)
+FROM deal_notes dn
+LEFT JOIN deals d ON d.id = dn.deal_id;
+
+-- Indexes to support filtering the view by company_id efficiently
+CREATE INDEX ON contacts (company_id);
+CREATE INDEX ON deals (company_id);
+CREATE INDEX ON contact_notes (contact_id);
+CREATE INDEX ON deal_notes (deal_id);