The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Update CommandMenuContextChipRecordSetterEffect to use the cache (#11043)
Raphaël Bosi7fe81f28b7d8
diff --git a/packages/twenty-front/src/modules/command-menu/components/CommandMenuContextChipRecordSetterEffect.tsx b/packages/twenty-front/src/modules/command-menu/components/CommandMenuContextChipRecordSetterEffect.tsxindex 46286a42977..0cf86645f04 100644--- a/packages/twenty-front/src/modules/command-menu/components/CommandMenuContextChipRecordSetterEffect.tsx+++ b/packages/twenty-front/src/modules/command-menu/components/CommandMenuContextChipRecordSetterEffect.tsx@@ -2,13 +2,11 @@ import { commandMenuNavigationMorphItemByPageState } from '@/command-menu/statesimport { commandMenuNavigationRecordsState } from '@/command-menu/states/commandMenuNavigationRecordsState';import { commandMenuNavigationStackState } from '@/command-menu/states/commandMenuNavigationStackState';import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState';-import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';-import { RecordGqlNode } from '@/object-record/graphql/types/RecordGqlNode';-import { usePerformCombinedFindManyRecords } from '@/object-record/multiple-objects/hooks/usePerformCombinedFindManyRecords';-import { isNonEmptyArray } from '@sniptt/guards';-import { useCallback, useEffect } from 'react';+import { getRecordFromCache } from '@/object-record/cache/utils/getRecordFromCache';+import { useApolloClient } from '@apollo/client';+import { useEffect } from 'react';import { useRecoilValue, useSetRecoilState } from 'recoil';-import { capitalize, isDefined } from 'twenty-shared';+import { isDefined } from 'twenty-shared';export const CommandMenuContextChipRecordSetterEffect = () => {const commandMenuNavigationMorphItemByPage = useRecoilValue(@@ -21,97 +19,56 @@ export const CommandMenuContextChipRecordSetterEffect = () => {const objectMetadataItems = useRecoilValue(objectMetadataItemsState);- const { performCombinedFindManyRecords } =- usePerformCombinedFindManyRecords();-- const objectMetadataIdsUsedInCommandMenuNavigation = Array.from(- commandMenuNavigationMorphItemByPage.values(),- ).map(({ objectMetadataId }) => objectMetadataId);-- const searchableObjectMetadataItems = objectMetadataItems.filter(({ id }) =>- objectMetadataIdsUsedInCommandMenuNavigation.includes(id),- );-const commandMenuNavigationStack = useRecoilValue(commandMenuNavigationStackState,);- const fetchRecords = useCallback(async () => {- const filterPerMetadataItemFilteredOnRecordId = Object.fromEntries(- searchableObjectMetadataItems- .map(({ id, nameSingular }) => {- const recordIdsForMetadataItem = Array.from(- commandMenuNavigationMorphItemByPage.values(),- )- .filter(({ objectMetadataId }) => objectMetadataId === id)- .map(({ recordId }) => recordId);+ const apolloClient = useApolloClient();- if (!isNonEmptyArray(recordIdsForMetadataItem)) {- return null;- }+ useEffect(() => {+ if (commandMenuNavigationStack.length > 1) {+ const morphItems = Array.from(+ commandMenuNavigationMorphItemByPage.values(),+ );- return [- `filter${capitalize(nameSingular)}`,- {- id: {- in: recordIdsForMetadataItem,- },- },- ];- })- .filter(isDefined),- );+ const records = morphItems+ .map((morphItem) => {+ const objectMetadataItem = objectMetadataItems.find(+ ({ id }) => id === morphItem.objectMetadataId,+ );- const operationSignatures = searchableObjectMetadataItems- .filter(({ nameSingular }) =>- isDefined(- filterPerMetadataItemFilteredOnRecordId[- `filter${capitalize(nameSingular)}`- ],- ),- )- .map((objectMetadataItem) => ({- objectNameSingular: objectMetadataItem.nameSingular,- variables: {- filter:- filterPerMetadataItemFilteredOnRecordId[- `filter${capitalize(objectMetadataItem.nameSingular)}`- ],- },- }));+ if (!objectMetadataItem) {+ return null;+ }- if (operationSignatures.length === 0) {- setCommandMenuNavigationRecords([]);- return;- }+ const record = getRecordFromCache({+ recordId: morphItem.recordId,+ cache: apolloClient.cache,+ objectMetadataItems,+ objectMetadataItem,+ });- const { result } = await performCombinedFindManyRecords({- operationSignatures,- });+ if (!record) {+ return null;+ }- const formattedRecords = Object.entries(result).flatMap(- ([objectNamePlural, records]) =>- records.map((record) => ({- objectMetadataItem: searchableObjectMetadataItems.find(- ({ namePlural }) => namePlural === objectNamePlural,- ) as ObjectMetadataItem,- record: record as RecordGqlNode,- })),- );+ return {+ objectMetadataItem,+ record,+ };+ })+ .filter(isDefined);- setCommandMenuNavigationRecords(formattedRecords);+ setCommandMenuNavigationRecords(records);+ }}, [+ apolloClient.cache,commandMenuNavigationMorphItemByPage,- performCombinedFindManyRecords,- searchableObjectMetadataItems,+ commandMenuNavigationStack,+ commandMenuNavigationStack.length,+ objectMetadataItems,setCommandMenuNavigationRecords,]);- useEffect(() => {- if (commandMenuNavigationStack.length > 1) {- fetchRecords();- }- }, [commandMenuNavigationStack.length, fetchRecords]);-return null;};