Skip to content

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

Make activity log more scalable

François Zaninottob21075efad5f
diff --git a/src/activity/ActivityLogCompanyCreated.tsx b/src/activity/ActivityLogCompanyCreated.tsx
index d4f463d1..57a51a2e 100644
--- a/src/activity/ActivityLogCompanyCreated.tsx
+++ b/src/activity/ActivityLogCompanyCreated.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 { SaleName } from '../sales/SaleName';
@@ -15,7 +15,7 @@ export function ActivityLogCompanyCreated({
activity,
}: ActivityLogCompanyCreatedProps) {
const context = useActivityLogContext();
- const { sale, company } = activity;
+ const { company } = activity;
return (
<ListItem disableGutters>
<Stack direction="row" spacing={1} alignItems="center" width="100%">
@@ -26,7 +26,15 @@ export function ActivityLogCompanyCreated({
color="text.secondary"
flexGrow={1}
>
- <SaleName sale={sale} /> added company{' '}
+ <ReferenceField
+ source="sales_id"
+ reference="sales"
+ record={activity}
+ link={false}
+ >
+ <SaleName />
+ </ReferenceField>{' '}
+ added company{' '}
<Link to={`/companies/${company.id}/show`}>
{company.name}
</Link>
diff --git a/src/activity/ActivityLogContactCreated.tsx b/src/activity/ActivityLogContactCreated.tsx
index 95e449be..f60a02b0 100644
--- a/src/activity/ActivityLogContactCreated.tsx
+++ b/src/activity/ActivityLogContactCreated.tsx
@@ -15,7 +15,7 @@ export function ActivityLogContactCreated({
activity,
}: ActivityLogContactCreatedProps) {
const context = useActivityLogContext();
- const { sale, contact } = activity;
+ const { contact } = activity;
return (
<ListItem disableGutters>
<Stack direction="row" spacing={1} alignItems="center" width="100%">
@@ -26,7 +26,15 @@ export function ActivityLogContactCreated({
color="text.secondary"
flexGrow={1}
>
- <SaleName sale={sale} /> added{' '}
+ <ReferenceField
+ source="sales_id"
+ reference="sales"
+ record={activity}
+ link={false}
+ >
+ <SaleName />
+ </ReferenceField>{' '}
+ added{' '}
<Link to={`/contacts/${contact.id}/show`}>
{contact.first_name} {contact.last_name}
</Link>{' '}
diff --git a/src/activity/ActivityLogContactNoteCreated.tsx b/src/activity/ActivityLogContactNoteCreated.tsx
index 31f9af5f..325f8a1c 100644
--- a/src/activity/ActivityLogContactNoteCreated.tsx
+++ b/src/activity/ActivityLogContactNoteCreated.tsx
@@ -1,5 +1,5 @@
import { Typography } from '@mui/material';
-import { Link, ReferenceField } from 'react-admin';
+import { ReferenceField } from 'react-admin';
import { Avatar } from '../contacts/Avatar';
import type { ActivityContactNoteCreated } from '../types';
@@ -16,31 +16,54 @@ export function ActivityLogContactNoteCreated({
activity,
}: ActivityLogContactNoteCreatedProps) {
const context = useActivityLogContext();
- const { sale, contact, contactNote } = activity;
+ const { contactNote } = activity;
return (
<ActivityLogNote
header={
<>
- <Avatar width={20} height={20} record={contact} />
+ <ReferenceField
+ source="contact_id"
+ reference="contacts"
+ record={contactNote}
+ link={false}
+ >
+ <Avatar width={20} height={20} />
+ </ReferenceField>
<Typography
component="p"
variant="body2"
color="text.secondary"
flexGrow={1}
>
- <SaleName sale={sale} /> added a note about{' '}
- <Link to={`/contacts/${contact.id}/show`}>
- {contact.first_name} {contact.last_name}
- </Link>
+ <ReferenceField
+ source="sales_id"
+ reference="sales"
+ record={activity}
+ link={false}
+ >
+ <SaleName />
+ </ReferenceField>{' '}
+ added a note about{' '}
+ <ReferenceField
+ source="contact_id"
+ reference="contacts"
+ record={contactNote}
+ />
{context !== 'company' && (
<>
{' from '}
<ReferenceField
- source="company_id"
- reference="companies"
- record={activity}
- link="show"
- />{' '}
+ source="contact_id"
+ reference="contacts"
+ record={contactNote}
+ link={false}
+ >
+ <ReferenceField
+ source="company_id"
+ reference="companies"
+ link="show"
+ />
+ </ReferenceField>{' '}
<RelativeDate date={activity.date} />
</>
)}
diff --git a/src/activity/ActivityLogDealCreated.tsx b/src/activity/ActivityLogDealCreated.tsx
index c7d7ea72..e5fc9cba 100644
--- a/src/activity/ActivityLogDealCreated.tsx
+++ b/src/activity/ActivityLogDealCreated.tsx
@@ -15,7 +15,7 @@ export function ActivityLogDealCreated({
activity,
}: ActivityLogDealCreatedProps) {
const context = useActivityLogContext();
- const { sale, deal } = activity;
+ const { deal } = activity;
return (
<ListItem disableGutters>
<Stack direction="row" spacing={1} alignItems="center" width="100%">
@@ -33,7 +33,15 @@ export function ActivityLogDealCreated({
color="text.secondary"
flexGrow={1}
>
- <SaleName sale={sale} /> added deal{' '}
+ <ReferenceField
+ source="sales_id"
+ reference="sales"
+ record={activity}
+ link={false}
+ >
+ <SaleName />
+ </ReferenceField>{' '}
+ added deal{' '}
<Link to={`/deals/${deal.id}/show`}>{deal.name}</Link>{' '}
{context !== 'company' && (
<>
diff --git a/src/activity/ActivityLogDealNoteCreated.tsx b/src/activity/ActivityLogDealNoteCreated.tsx
index 53827452..3c567379 100644
--- a/src/activity/ActivityLogDealNoteCreated.tsx
+++ b/src/activity/ActivityLogDealNoteCreated.tsx
@@ -1,5 +1,5 @@
import Typography from '@mui/material/Typography';
-import { Link, ReferenceField } from 'react-admin';
+import { ReferenceField } from 'react-admin';
import { CompanyAvatar } from '../companies/CompanyAvatar';
import type { ActivityDealNoteCreated } from '../types';
@@ -16,18 +16,24 @@ export function ActivityLogDealNoteCreated({
activity,
}: ActivityLogDealNoteCreatedProps) {
const context = useActivityLogContext();
- const { sale, deal, dealNote } = activity;
+ const { dealNote } = activity;
return (
<ActivityLogNote
header={
<>
<ReferenceField
- source="company_id"
- reference="companies"
- record={activity}
+ source="deal_id"
+ reference="deals"
+ record={dealNote}
link={false}
>
- <CompanyAvatar width={20} height={20} />
+ <ReferenceField
+ source="company_id"
+ reference="companies"
+ link={false}
+ >
+ <CompanyAvatar width={20} height={20} />
+ </ReferenceField>
</ReferenceField>
<Typography
component="p"
@@ -35,17 +41,36 @@ export function ActivityLogDealNoteCreated({
color="text.secondary"
flexGrow={1}
>
- <SaleName sale={sale} /> added a note about deal{' '}
- <Link to={`/deals/${deal.id}/show`}>{deal.name}</Link>
+ <ReferenceField
+ source="sales_id"
+ reference="sales"
+ record={activity}
+ link={false}
+ >
+ <SaleName />
+ </ReferenceField>{' '}
+ added a note about deal{' '}
+ <ReferenceField
+ source="deal_id"
+ reference="deals"
+ record={dealNote}
+ link="show"
+ />
{context !== 'company' && (
<>
{' at '}
<ReferenceField
- source="company_id"
- reference="companies"
- record={activity}
- link="show"
- />{' '}
+ source="deal_id"
+ reference="deals"
+ record={dealNote}
+ link={false}
+ >
+ <ReferenceField
+ source="company_id"
+ reference="companies"
+ link="show"
+ />
+ </ReferenceField>{' '}
<RelativeDate date={activity.date} />
</>
)}
diff --git a/src/providers/commons/activity.ts b/src/providers/commons/activity.ts
index b0646c7f..9c868120 100644
--- a/src/providers/commons/activity.ts
+++ b/src/providers/commons/activity.ts
@@ -13,206 +13,164 @@ import {
ContactNote,
Deal,
DealNote,
- Sale,
} from '../../types';
-// FIXME: Super inefficient implementation, will not scale for a large dataset
+// FIXME: Requires 5 large queries to get the latest activities.
+// Replace with a server-side view or a custom API endpoint.
export async function getActivityLog(
dataProvider: DataProvider,
- companyId?: Identifier
+ companyId?: Identifier,
+ salesId?: Identifier
) {
- const sales = await getSales(dataProvider);
- const { companiesIds, companiesLog } = await getCompaniesLog(
- dataProvider,
- sales,
- companyId
- );
+ let companyFilter = {} as any;
+ if (companyId) {
+ companyFilter.id = companyId;
+ } else if (salesId) {
+ companyFilter['sales_id@in'] = `(${salesId})`;
+ }
- const [contactsLog, dealsLog] = await Promise.all([
- getContactsLog(
- dataProvider,
- sales,
- companyId != null ? companiesIds : undefined
- ),
- getDealsLog(dataProvider, sales, companiesIds),
- ]);
- return companiesLog.concat(contactsLog, dealsLog).sort(
- (a, b) => (a.date && b.date ? a.date.localeCompare(b.date) * -1 : 0) // sort by date desc
- );
-}
+ let filter = {} as any;
+ if (companyId) {
+ filter.company_id = companyId;
+ } else if (salesId) {
+ filter['sales_id@in'] = `(${salesId})`;
+ }
-async function getSales(dataProvider: DataProvider) {
- const salesById = await dataProvider
- .getList<Sale>('sales', {
- pagination: { page: 1, perPage: 200 },
- sort: { field: 'id', order: 'ASC' },
- filter: {},
- })
- .then(({ data }) =>
- data.reduce((acc, sale) => {
- acc.set(sale.id, sale);
- return acc;
- }, new Map<Identifier, Sale>())
- );
-
- return {
- salesById,
- salesIds: [...salesById.keys()],
- };
+ const [newCompanies, newContactsAndNotes, newDealsAndNotes] =
+ await Promise.all([
+ getNewCompanies(dataProvider, companyFilter),
+ getNewContactsAndNotes(dataProvider, filter),
+ getNewDealsAndNotes(dataProvider, filter),
+ ]);
+ return (
+ [...newCompanies, ...newContactsAndNotes, ...newDealsAndNotes]
+ // sort by date desc
+ .sort((a, b) =>
+ a.date && b.date ? a.date.localeCompare(b.date) * -1 : 0
+ )
+ // limit to 250 activities
+ .slice(0, 250)
+ );
}
-async function getCompaniesLog(
+const getNewCompanies = async (
dataProvider: DataProvider,
- { salesById, salesIds }: Awaited<ReturnType<typeof getSales>>,
- companyId?: Identifier
-) {
- const companies = await dataProvider
- .getList<Company>('companies', {
- filter: companyId
- ? { id: companyId, 'sales_id@in': `(${salesIds})` }
- : { 'sales_id@in': `(${salesIds})` },
- pagination: { page: 1, perPage: 10_000 },
+ filter: any
+): Promise<Activity[]> => {
+ const { data: companies } = await dataProvider.getList<Company>(
+ 'companies',
+ {
+ filter,
+ pagination: { page: 1, perPage: 250 },
sort: { field: 'created_at', order: 'DESC' },
- })
- .then(({ data }) => data);
-
- return {
- 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),
- };
-}
+ }
+ );
+ return companies.map(company => ({
+ id: `company.${company.id}.created`,
+ type: COMPANY_CREATED,
+ company_id: company.id,
+ company,
+ sales_id: company.sales_id,
+ date: company.created_at,
+ }));
+};
-async function getContactsLog(
+async function getNewContactsAndNotes(
dataProvider: DataProvider,
- { salesById: salesDict, salesIds }: Awaited<ReturnType<typeof getSales>>,
- companiesIds?: Identifier[]
-) {
- const contacts = await dataProvider
- .getList<Contact>('contacts', {
- 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' },
- })
- .then(({ data }) => data);
-
- const contactsDict = contacts.reduce((acc, contact) => {
- acc.set(contact.id, contact);
- return acc;
- }, new Map<Identifier, Contact>());
-
- const contactNotes = await dataProvider
- .getList<ContactNote>('contactNotes', {
- filter: companiesIds
- ? {
- 'contact_id@in': `(${contacts.map(({ id }) => id)})`,
- 'sales_id@in': `(${salesIds})`,
- }
- : {
- 'sales_id@in': `(${salesIds})`,
- },
- pagination: { page: 1, perPage: 10_000 },
+ filter: any
+): Promise<Activity[]> {
+ const { data: contacts } = await dataProvider.getList<Contact>('contacts', {
+ filter,
+ pagination: { page: 1, perPage: 250 },
+ sort: { field: 'first_seen', order: 'DESC' },
+ });
+
+ let recentContactNotesFilter = {} as any;
+ if (filter.sales_id) {
+ recentContactNotesFilter.sales_id = filter.sales_id;
+ }
+ if (filter.company_id) {
+ // No company_id field in contactNote, filtering by related contacts instead.
+ // This filter is only valid if a company has less than 250 contact.
+ const contactIds = contacts.map(contact => contact.id).join(',');
+ recentContactNotesFilter['contact_id@in'] = `(${contactIds})`;
+ }
+
+ const { data: contactNotes } = await dataProvider.getList<ContactNote>(
+ 'contactNotes',
+ {
+ filter: recentContactNotesFilter,
+ pagination: { page: 1, perPage: 250 },
sort: { field: 'date', order: 'DESC' },
- })
- .then(({ data }) => data);
-
- return contacts
- .map<Activity>(contact => ({
- id: `contact.${contact.id}.created`,
- type: CONTACT_CREATED,
- company_id: contact.company_id,
- sale: salesDict.get(contact.sales_id) as Sale,
- contact,
- date: contact.first_seen,
- }))
- .concat(
- contactNotes.map<Activity>(contactNote => {
- const contact = contactsDict.get(
- contactNote.contact_id
- ) as Contact;
- return {
- id: `contactNote.${contactNote.id}.created`,
- type: CONTACT_NOTE_CREATED,
- company_id: contact.company_id,
- sale: salesDict.get(contactNote.sales_id) as Sale,
- contact,
- contactNote,
- date: contactNote.date,
- };
- })
- );
+ }
+ );
+
+ const newContacts = contacts.map(contact => ({
+ id: `contact.${contact.id}.created`,
+ type: CONTACT_CREATED,
+ company_id: contact.company_id,
+ sales_id: contact.sales_id,
+ contact,
+ date: contact.first_seen,
+ }));
+
+ const newContactNotes = contactNotes.map(contactNote => ({
+ id: `contactNote.${contactNote.id}.created`,
+ type: CONTACT_NOTE_CREATED,
+ sales_id: contactNote.sales_id,
+ contactNote,
+ date: contactNote.date,
+ }));
+
+ return [...newContacts, ...newContactNotes];
}
-async function getDealsLog(
+async function getNewDealsAndNotes(
dataProvider: DataProvider,
- { salesById: salesDict, salesIds }: Awaited<ReturnType<typeof getSales>>,
- companiesIds?: Identifier[]
-) {
- const deals = await dataProvider
- .getList<Deal>('deals', {
- 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' },
- })
- .then(({ data }) => data);
-
- const dealsDict = deals.reduce((acc, deals) => {
- acc.set(deals.id, deals);
- return acc;
- }, new Map<Identifier, Deal>());
-
- const dealsNotes = await dataProvider
- .getList<DealNote>('dealNotes', {
- filter: {
- 'deal_id@in': `(${deals.map(({ id }) => id)})`,
- 'sales_id@in': `(${salesIds})`,
- },
- pagination: { page: 1, perPage: 10_000 },
+ filter: any
+): Promise<Activity[]> {
+ const { data: deals } = await dataProvider.getList<Deal>('deals', {
+ filter,
+ pagination: { page: 1, perPage: 250 },
+ sort: { field: 'created_at', order: 'DESC' },
+ });
+
+ let recentDealNotesFilter = {} as any;
+ if (filter.sales_id) {
+ recentDealNotesFilter.sales_id = filter.sales_id;
+ }
+ if (filter.company_id) {
+ // No company_id field in dealNote, filtering by related deals instead.
+ // This filter is only valid if a deal has less than 250 notes.
+ const dealIds = deals.map(deal => deal.id).join(',');
+ recentDealNotesFilter['deal_id@in'] = `(${dealIds})`;
+ }
+
+ const { data: dealNotes } = await dataProvider.getList<DealNote>(
+ 'dealNotes',
+ {
+ filter: recentDealNotesFilter,
+ pagination: { page: 1, perPage: 250 },
sort: { field: 'date', order: 'DESC' },
- })
- .then(({ data }) => data);
-
- return deals
- .map<Activity>(deal => ({
- id: `deal.${deal.id}.created`,
- type: DEAL_CREATED,
- company_id: deal.company_id,
- sale: salesDict.get(deal.sales_id) as Sale,
- deal,
- date: deal.created_at,
- }))
- .concat(
- dealsNotes.map<Activity>(dealNote => {
- const deal = dealsDict.get(dealNote.deal_id) as Deal;
- return {
- id: `dealNote.${dealNote.id}.created`,
- type: DEAL_NOTE_CREATED,
- company_id: deal.company_id,
- sale: salesDict.get(dealNote.sales_id) as Sale,
- deal,
- dealNote,
- date: dealNote.date,
- };
- })
- );
+ }
+ );
+
+ const newDeals = deals.map(deal => ({
+ id: `deal.${deal.id}.created`,
+ type: DEAL_CREATED,
+ company_id: deal.company_id,
+ sales_id: deal.sales_id,
+ deal,
+ date: deal.created_at,
+ }));
+
+ const newDealNotes = dealNotes.map(dealNote => ({
+ id: `dealNote.${dealNote.id}.created`,
+ type: DEAL_NOTE_CREATED,
+ sales_id: dealNote.sales_id,
+ dealNote,
+ date: dealNote.date,
+ }));
+
+ return [...newDeals, ...newDealNotes];
}
diff --git a/src/sales/SaleName.tsx b/src/sales/SaleName.tsx
index 9bef5237..fa3a2751 100644
--- a/src/sales/SaleName.tsx
+++ b/src/sales/SaleName.tsx
@@ -1,10 +1,12 @@
-import { useGetIdentity } from 'react-admin';
+import { useGetIdentity, useRecordContext } from 'react-admin';
import { Sale } from '../types';
-export const SaleName = ({ sale }: { sale: Sale }) => {
+export const SaleName = ({ sale }: { sale?: Sale }) => {
const { identity, isPending } = useGetIdentity();
- if (isPending) return null;
- return sale.id === identity?.id
+ const saleFromContext = useRecordContext<Sale>();
+ const finalSale = sale || saleFromContext;
+ if (isPending || !finalSale) return null;
+ return finalSale.id === identity?.id
? 'You'
- : `${sale.first_name} ${sale.last_name}`;
+ : `${finalSale.first_name} ${finalSale.last_name}`;
};
diff --git a/src/types.ts b/src/types.ts
index d5d7c48b..70191d1e 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -148,23 +148,21 @@ export type ActivityCompanyCreated = {
type: typeof COMPANY_CREATED;
company_id: Identifier;
company: Company;
- sale: Sale;
+ sales_id: Identifier;
date: string;
};
export type ActivityContactCreated = {
type: typeof CONTACT_CREATED;
company_id: Identifier;
- sale: Sale;
+ sales_id?: Identifier;
contact: Contact;
date: string;
};
export type ActivityContactNoteCreated = {
type: typeof CONTACT_NOTE_CREATED;
- sale: Sale;
- company_id: Identifier;
- contact: Contact;
+ sales_id?: Identifier;
contactNote: ContactNote;
date: string;
};
@@ -172,16 +170,14 @@ export type ActivityContactNoteCreated = {
export type ActivityDealCreated = {
type: typeof DEAL_CREATED;
company_id: Identifier;
- sale: Sale;
+ sales_id?: Identifier;
deal: Deal;
date: string;
};
export type ActivityDealNoteCreated = {
type: typeof DEAL_NOTE_CREATED;
- company_id: Identifier;
- sale: Sale;
- deal: Deal;
+ sales_id?: Identifier;
dealNote: DealNote;
date: string;
};