Skip to content

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

Improve ActivityLog perf

François Zaninottof2c879d8c250
diff --git a/src/activity/ActivityLogCompanyCreated.tsx b/src/activity/ActivityLogCompanyCreated.tsx
index 477097e2..d4f463d1 100644
--- a/src/activity/ActivityLogCompanyCreated.tsx
+++ b/src/activity/ActivityLogCompanyCreated.tsx
@@ -12,9 +12,10 @@ type ActivityLogCompanyCreatedProps = {
};
export function ActivityLogCompanyCreated({
- activity: { sale, company },
+ activity,
}: ActivityLogCompanyCreatedProps) {
const context = useActivityLogContext();
+ const { sale, company } = activity;
return (
<ListItem disableGutters>
<Stack direction="row" spacing={1} alignItems="center" width="100%">
@@ -32,7 +33,7 @@ export function ActivityLogCompanyCreated({
{context === 'all' && (
<>
{' '}
- <RelativeDate date={company.created_at} />
+ <RelativeDate date={activity.date} />
</>
)}
</Typography>
@@ -42,7 +43,7 @@ export function ActivityLogCompanyCreated({
variant="body2"
component="span"
>
- <RelativeDate date={company.created_at} />
+ <RelativeDate date={activity.date} />
</Typography>
)}
</Stack>
diff --git a/src/activity/ActivityLogContactCreated.tsx b/src/activity/ActivityLogContactCreated.tsx
index 7b8592b9..95e449be 100644
--- a/src/activity/ActivityLogContactCreated.tsx
+++ b/src/activity/ActivityLogContactCreated.tsx
@@ -1,5 +1,5 @@
import { ListItem, Stack, Typography } from '@mui/material';
-import { Link } from 'react-admin';
+import { Link, ReferenceField } from 'react-admin';
import { Avatar } from '../contacts/Avatar';
import type { ActivityContactCreated } from '../types';
@@ -12,9 +12,10 @@ type ActivityLogContactCreatedProps = {
};
export function ActivityLogContactCreated({
- activity: { sale, contact, company },
+ activity,
}: ActivityLogContactCreatedProps) {
const context = useActivityLogContext();
+ const { sale, contact } = activity;
return (
<ListItem disableGutters>
<Stack direction="row" spacing={1} alignItems="center" width="100%">
@@ -32,10 +33,13 @@ export function ActivityLogContactCreated({
{context !== 'company' && (
<>
to{' '}
- <Link to={`/companies/${contact.company_id}/show`}>
- {company.name}
- </Link>{' '}
- <RelativeDate date={contact.first_seen} />
+ <ReferenceField
+ source="company_id"
+ reference="companies"
+ record={activity}
+ link="show"
+ />{' '}
+ <RelativeDate date={activity.date} />
</>
)}
</Typography>
@@ -45,7 +49,7 @@ export function ActivityLogContactCreated({
variant="body2"
component="span"
>
- <RelativeDate date={contact.first_seen} />
+ <RelativeDate date={activity.date} />
</Typography>
)}
</Stack>
diff --git a/src/activity/ActivityLogContactNoteCreated.tsx b/src/activity/ActivityLogContactNoteCreated.tsx
index 65b3190d..31f9af5f 100644
--- a/src/activity/ActivityLogContactNoteCreated.tsx
+++ b/src/activity/ActivityLogContactNoteCreated.tsx
@@ -1,5 +1,5 @@
import { Typography } from '@mui/material';
-import { Link } from 'react-admin';
+import { Link, ReferenceField } from 'react-admin';
import { Avatar } from '../contacts/Avatar';
import type { ActivityContactNoteCreated } from '../types';
@@ -13,9 +13,10 @@ type ActivityLogContactNoteCreatedProps = {
};
export function ActivityLogContactNoteCreated({
- activity: { sale, contact, contactNote, company },
+ activity,
}: ActivityLogContactNoteCreatedProps) {
const context = useActivityLogContext();
+ const { sale, contact, contactNote } = activity;
return (
<ActivityLogNote
header={
@@ -34,10 +35,13 @@ export function ActivityLogContactNoteCreated({
{context !== 'company' && (
<>
{' from '}
- <Link to={`/companies/${company.id}/show`}>
- {company.name}
- </Link>{' '}
- <RelativeDate date={contactNote.date} />
+ <ReferenceField
+ source="company_id"
+ reference="companies"
+ record={activity}
+ link="show"
+ />{' '}
+ <RelativeDate date={activity.date} />
</>
)}
</Typography>
@@ -47,7 +51,7 @@ export function ActivityLogContactNoteCreated({
variant="body2"
component="span"
>
- <RelativeDate date={contactNote.date} />
+ <RelativeDate date={activity.date} />
</Typography>
)}
</>
diff --git a/src/activity/ActivityLogDealCreated.tsx b/src/activity/ActivityLogDealCreated.tsx
index ef8157fe..c7d7ea72 100644
--- a/src/activity/ActivityLogDealCreated.tsx
+++ b/src/activity/ActivityLogDealCreated.tsx
@@ -1,5 +1,5 @@
import { ListItem, Stack, Typography } from '@mui/material';
-import { Link } from 'react-admin';
+import { Link, ReferenceField } from 'react-admin';
import { CompanyAvatar } from '../companies/CompanyAvatar';
import type { ActivityDealCreated } from '../types';
@@ -12,13 +12,21 @@ type ActivityLogDealCreatedProps = {
};
export function ActivityLogDealCreated({
- activity: { sale, deal, company },
+ activity,
}: ActivityLogDealCreatedProps) {
const context = useActivityLogContext();
+ const { sale, deal } = activity;
return (
<ListItem disableGutters>
<Stack direction="row" spacing={1} alignItems="center" width="100%">
- <CompanyAvatar width={20} height={20} record={company} />
+ <ReferenceField
+ source="company_id"
+ reference="companies"
+ record={activity}
+ link={false}
+ >
+ <CompanyAvatar width={20} height={20} />
+ </ReferenceField>
<Typography
component="p"
variant="body2"
@@ -30,10 +38,13 @@ export function ActivityLogDealCreated({
{context !== 'company' && (
<>
to company{' '}
- <Link to={`/companies/${company.id}/show`}>
- {company.name}
- </Link>{' '}
- <RelativeDate date={deal.created_at} />
+ <ReferenceField
+ source="company_id"
+ reference="companies"
+ record={activity}
+ link="show"
+ />{' '}
+ <RelativeDate date={activity.date} />
</>
)}
</Typography>
@@ -43,7 +54,7 @@ export function ActivityLogDealCreated({
variant="body2"
component="span"
>
- <RelativeDate date={deal.created_at} />
+ <RelativeDate date={activity.date} />
</Typography>
)}
</Stack>
diff --git a/src/activity/ActivityLogDealNoteCreated.tsx b/src/activity/ActivityLogDealNoteCreated.tsx
index 8d8db5a3..53827452 100644
--- a/src/activity/ActivityLogDealNoteCreated.tsx
+++ b/src/activity/ActivityLogDealNoteCreated.tsx
@@ -1,5 +1,5 @@
import Typography from '@mui/material/Typography';
-import { Link } from 'react-admin';
+import { Link, ReferenceField } from 'react-admin';
import { CompanyAvatar } from '../companies/CompanyAvatar';
import type { ActivityDealNoteCreated } from '../types';
@@ -13,14 +13,22 @@ type ActivityLogDealNoteCreatedProps = {
};
export function ActivityLogDealNoteCreated({
- activity: { company, sale, deal, dealNote },
+ activity,
}: ActivityLogDealNoteCreatedProps) {
const context = useActivityLogContext();
+ const { sale, deal, dealNote } = activity;
return (
<ActivityLogNote
header={
<>
- <CompanyAvatar width={20} height={20} record={company} />
+ <ReferenceField
+ source="company_id"
+ reference="companies"
+ record={activity}
+ link={false}
+ >
+ <CompanyAvatar width={20} height={20} />
+ </ReferenceField>
<Typography
component="p"
variant="body2"
@@ -32,10 +40,13 @@ export function ActivityLogDealNoteCreated({
{context !== 'company' && (
<>
{' at '}
- <Link to={`/companies/${deal.company_id}/show`}>
- {company.name}
- </Link>{' '}
- <RelativeDate date={dealNote.date} />
+ <ReferenceField
+ source="company_id"
+ reference="companies"
+ record={activity}
+ link="show"
+ />{' '}
+ <RelativeDate date={activity.date} />
</>
)}
</Typography>
@@ -45,7 +56,7 @@ export function ActivityLogDealNoteCreated({
variant="body2"
component="span"
>
- <RelativeDate date={dealNote.date} />
+ <RelativeDate date={activity.date} />
</Typography>
)}
</>
diff --git a/src/providers/commons/activity.ts b/src/providers/commons/activity.ts
index 4b63147e..b0646c7f 100644
--- a/src/providers/commons/activity.ts
+++ b/src/providers/commons/activity.ts
@@ -22,13 +22,21 @@ export async function getActivityLog(
companyId?: Identifier
) {
const sales = await getSales(dataProvider);
- const companies = await getCompaniesLog(dataProvider, sales, companyId);
+ const { companiesIds, companiesLog } = await getCompaniesLog(
+ dataProvider,
+ sales,
+ companyId
+ );
const [contactsLog, dealsLog] = await Promise.all([
- getContactsLog(dataProvider, sales, companies),
- getDealsLog(dataProvider, sales, companies),
+ getContactsLog(
+ dataProvider,
+ sales,
+ companyId != null ? companiesIds : undefined
+ ),
+ getDealsLog(dataProvider, sales, companiesIds),
]);
- return companies.companiesLog.concat(contactsLog, dealsLog).sort(
+ return companiesLog.concat(contactsLog, dealsLog).sort(
(a, b) => (a.date && b.date ? a.date.localeCompare(b.date) * -1 : 0) // sort by date desc
);
}
@@ -72,29 +80,30 @@ async function getCompaniesLog(
companiesLog: companies.map<Activity>(company => ({
id: `company.${company.id}.created`,
type: COMPANY_CREATED,
+ company_id: company.id,
company,
sale: salesById.get(company.sales_id) as Sale,
date: company.created_at,
})),
companiesIds: companies.map(({ id }) => id),
- companiesById: companies.reduce((acc, company) => {
- acc.set(company.id, company);
- return acc;
- }, new Map<Identifier, Company>()),
};
}
async function getContactsLog(
dataProvider: DataProvider,
{ salesById: salesDict, salesIds }: Awaited<ReturnType<typeof getSales>>,
- { companiesIds, companiesById }: Awaited<ReturnType<typeof getCompaniesLog>>
+ companiesIds?: Identifier[]
) {
const contacts = await dataProvider
.getList<Contact>('contacts', {
- filter: {
- 'company_id@in': `(${companiesIds})`,
- 'sales_id@in': `(${salesIds})`,
- },
+ filter: companiesIds
+ ? {
+ 'company_id@in': `(${companiesIds})`,
+ 'sales_id@in': `(${salesIds})`,
+ }
+ : {
+ 'sales_id@in': `(${salesIds})`,
+ },
pagination: { page: 1, perPage: 10_000 },
sort: { field: 'first_seen', order: 'DESC' },
})
@@ -107,10 +116,14 @@ async function getContactsLog(
const contactNotes = await dataProvider
.getList<ContactNote>('contactNotes', {
- filter: {
- 'contact_id@in': `(${contacts.map(({ id }) => id)})`,
- 'sales_id@in': `(${salesIds})`,
- },
+ filter: companiesIds
+ ? {
+ 'contact_id@in': `(${contacts.map(({ id }) => id)})`,
+ 'sales_id@in': `(${salesIds})`,
+ }
+ : {
+ 'sales_id@in': `(${salesIds})`,
+ },
pagination: { page: 1, perPage: 10_000 },
sort: { field: 'date', order: 'DESC' },
})
@@ -120,7 +133,7 @@ async function getContactsLog(
.map<Activity>(contact => ({
id: `contact.${contact.id}.created`,
type: CONTACT_CREATED,
- company: companiesById.get(contact.company_id) as Company,
+ company_id: contact.company_id,
sale: salesDict.get(contact.sales_id) as Sale,
contact,
date: contact.first_seen,
@@ -133,7 +146,7 @@ async function getContactsLog(
return {
id: `contactNote.${contactNote.id}.created`,
type: CONTACT_NOTE_CREATED,
- company: companiesById.get(contact.company_id) as Company,
+ company_id: contact.company_id,
sale: salesDict.get(contactNote.sales_id) as Sale,
contact,
contactNote,
@@ -146,14 +159,18 @@ async function getContactsLog(
async function getDealsLog(
dataProvider: DataProvider,
{ salesById: salesDict, salesIds }: Awaited<ReturnType<typeof getSales>>,
- { companiesIds, companiesById }: Awaited<ReturnType<typeof getCompaniesLog>>
+ companiesIds?: Identifier[]
) {
const deals = await dataProvider
.getList<Deal>('deals', {
- filter: {
- 'company_id@in': `(${companiesIds})`,
- 'sales_id@in': `(${salesIds})`,
- },
+ filter: companiesIds
+ ? {
+ 'company_id@in': `(${companiesIds})`,
+ 'sales_id@in': `(${salesIds})`,
+ }
+ : {
+ 'sales_id@in': `(${salesIds})`,
+ },
pagination: { page: 1, perPage: 10_000 },
sort: { field: 'created_at', order: 'DESC' },
})
@@ -179,7 +196,7 @@ async function getDealsLog(
.map<Activity>(deal => ({
id: `deal.${deal.id}.created`,
type: DEAL_CREATED,
- company: companiesById.get(deal.company_id) as Company,
+ company_id: deal.company_id,
sale: salesDict.get(deal.sales_id) as Sale,
deal,
date: deal.created_at,
@@ -190,7 +207,7 @@ async function getDealsLog(
return {
id: `dealNote.${dealNote.id}.created`,
type: DEAL_NOTE_CREATED,
- company: companiesById.get(deal.company_id) as Company,
+ company_id: deal.company_id,
sale: salesDict.get(dealNote.sales_id) as Sale,
deal,
dealNote,
diff --git a/src/types.ts b/src/types.ts
index 5bf968b0..d5d7c48b 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -146,41 +146,48 @@ export type Task = {
export type ActivityCompanyCreated = {
type: typeof COMPANY_CREATED;
+ company_id: Identifier;
company: Company;
sale: Sale;
+ date: string;
};
export type ActivityContactCreated = {
type: typeof CONTACT_CREATED;
- company: Company;
+ company_id: Identifier;
sale: Sale;
contact: Contact;
+ date: string;
};
export type ActivityContactNoteCreated = {
type: typeof CONTACT_NOTE_CREATED;
sale: Sale;
- company: Company;
+ company_id: Identifier;
contact: Contact;
contactNote: ContactNote;
+ date: string;
};
export type ActivityDealCreated = {
type: typeof DEAL_CREATED;
- company: Company;
+ company_id: Identifier;
sale: Sale;
deal: Deal;
+ date: string;
};
export type ActivityDealNoteCreated = {
type: typeof DEAL_NOTE_CREATED;
- company: Company;
+ company_id: Identifier;
sale: Sale;
deal: Deal;
dealNote: DealNote;
+ date: string;
};
-export type Activity = RaRecord & { date: string } & (
+export type Activity = RaRecord &
+ (
| ActivityCompanyCreated
| ActivityContactCreated
| ActivityContactNoteCreated