From ec79de798943e9943aced33e78c9179388aca5ac Mon Sep 17 00:00:00 2001 From: Charles Bochet Date: Fri, 11 Oct 2024 19:50:44 +0200 Subject: [PATCH] Fix --- .../triggerDestroyRecordsOptimisticEffect.ts | 12 +-- .../triggerUpdateRecordOptimisticEffect.ts | 73 +++++++++---------- .../triggerUpdateRelationsOptimisticEffect.ts | 2 +- .../cache/hooks/useDeleteRecordFromCache.ts | 4 +- .../cache/utils/deleteRecordFromCache.ts | 8 +- .../hooks/useCreateManyRecords.ts | 6 +- .../object-record/hooks/useCreateOneRecord.ts | 4 +- .../hooks/useDestroyManyRecords.ts | 2 +- .../hooks/useDestroyOneRecord.ts | 2 +- .../internal/usePersistViewFilterRecords.ts | 4 +- .../internal/usePersistViewSortRecords.ts | 4 +- 11 files changed, 58 insertions(+), 63 deletions(-) diff --git a/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerDestroyRecordsOptimisticEffect.ts b/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerDestroyRecordsOptimisticEffect.ts index 0c98c63c3694..5f62ff3bdbf3 100644 --- a/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerDestroyRecordsOptimisticEffect.ts +++ b/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerDestroyRecordsOptimisticEffect.ts @@ -10,12 +10,12 @@ import { isDefined } from '~/utils/isDefined'; export const triggerDestroyRecordsOptimisticEffect = ({ cache, objectMetadataItem, - recordsToDelete, + recordsToDestroy, objectMetadataItems, }: { cache: ApolloCache; objectMetadataItem: ObjectMetadataItem; - recordsToDelete: RecordGqlNode[]; + recordsToDestroy: RecordGqlNode[]; objectMetadataItems: ObjectMetadataItem[]; }) => { cache.modify({ @@ -36,7 +36,7 @@ export const triggerDestroyRecordsOptimisticEffect = ({ const rootQueryCachedObjectRecordConnection = rootQueryCachedResponse; - const recordIdsToDelete = recordsToDelete.map(({ id }) => id); + const recordIdsToDelete = recordsToDestroy.map(({ id }) => id); const cachedEdges = readField( 'edges', @@ -69,15 +69,15 @@ export const triggerDestroyRecordsOptimisticEffect = ({ }, }); - recordsToDelete.forEach((recordToDelete) => { + recordsToDestroy.forEach((recordToDestroy) => { triggerUpdateRelationsOptimisticEffect({ cache, sourceObjectMetadataItem: objectMetadataItem, - currentSourceRecord: recordToDelete, + currentSourceRecord: recordToDestroy, updatedSourceRecord: null, objectMetadataItems, }); - cache.evict({ id: cache.identify(recordToDelete) }); + cache.evict({ id: cache.identify(recordToDestroy) }); }); }; diff --git a/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerUpdateRecordOptimisticEffect.ts b/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerUpdateRecordOptimisticEffect.ts index 3c09c213f68b..15878796f5bd 100644 --- a/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerUpdateRecordOptimisticEffect.ts +++ b/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerUpdateRecordOptimisticEffect.ts @@ -65,50 +65,45 @@ export const triggerUpdateRecordOptimisticEffect = ({ const rootQueryFilter = rootQueryVariables?.filter; const rootQueryOrderBy = rootQueryVariables?.orderBy; - const shouldTryToMatchFilter = isDefined(rootQueryFilter); - - if (shouldTryToMatchFilter) { - const updatedRecordMatchesThisRootQueryFilter = - isRecordMatchingFilter({ - record: updatedRecord, - filter: rootQueryFilter, - objectMetadataItem, - }); + const updatedRecordMatchesThisRootQueryFilter = isRecordMatchingFilter({ + record: updatedRecord, + filter: rootQueryFilter ?? {}, + objectMetadataItem, + }); + + const updatedRecordIndexInRootQueryEdges = + rootQueryCurrentEdges.findIndex( + (cachedEdge) => + readField('id', cachedEdge.node) === updatedRecord.id, + ); - const updatedRecordIndexInRootQueryEdges = - rootQueryCurrentEdges.findIndex( - (cachedEdge) => - readField('id', cachedEdge.node) === updatedRecord.id, - ); - - const updatedRecordFoundInRootQueryEdges = - updatedRecordIndexInRootQueryEdges > -1; - - const updatedRecordShouldBeAddedToRootQueryEdges = - updatedRecordMatchesThisRootQueryFilter && - !updatedRecordFoundInRootQueryEdges; - - const updatedRecordShouldBeRemovedFromRootQueryEdges = - !updatedRecordMatchesThisRootQueryFilter && - updatedRecordFoundInRootQueryEdges; - - if (updatedRecordShouldBeAddedToRootQueryEdges) { - const updatedRecordNodeReference = toReference(updatedRecord); - - if (isDefined(updatedRecordNodeReference)) { - rootQueryNextEdges.push({ - __typename: getEdgeTypename(objectMetadataItem.nameSingular), - node: updatedRecordNodeReference, - cursor: '', - }); - } - } + const updatedRecordFoundInRootQueryEdges = + updatedRecordIndexInRootQueryEdges > -1; + + const updatedRecordShouldBeAddedToRootQueryEdges = + updatedRecordMatchesThisRootQueryFilter && + !updatedRecordFoundInRootQueryEdges; + + const updatedRecordShouldBeRemovedFromRootQueryEdges = + !updatedRecordMatchesThisRootQueryFilter && + updatedRecordFoundInRootQueryEdges; - if (updatedRecordShouldBeRemovedFromRootQueryEdges) { - rootQueryNextEdges.splice(updatedRecordIndexInRootQueryEdges, 1); + if (updatedRecordShouldBeAddedToRootQueryEdges) { + const updatedRecordNodeReference = toReference(updatedRecord); + + if (isDefined(updatedRecordNodeReference)) { + rootQueryNextEdges.push({ + __typename: getEdgeTypename(objectMetadataItem.nameSingular), + node: updatedRecordNodeReference, + cursor: '', + }); } } + if (updatedRecordShouldBeRemovedFromRootQueryEdges) { + rootQueryNextEdges.splice(updatedRecordIndexInRootQueryEdges, 1); + } + const rootQueryNextEdgesShouldBeSorted = isDefined(rootQueryOrderBy); if ( diff --git a/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerUpdateRelationsOptimisticEffect.ts b/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerUpdateRelationsOptimisticEffect.ts index f4e47612a0fa..4e44e098adcb 100644 --- a/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerUpdateRelationsOptimisticEffect.ts +++ b/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerUpdateRelationsOptimisticEffect.ts @@ -125,7 +125,7 @@ export const triggerUpdateRelationsOptimisticEffect = ({ triggerDestroyRecordsOptimisticEffect({ cache, objectMetadataItem: fullTargetObjectMetadataItem, - recordsToDelete: targetRecordsToDetachFrom, + recordsToDestroy: targetRecordsToDetachFrom, objectMetadataItems, }); } else { diff --git a/packages/twenty-front/src/modules/object-record/cache/hooks/useDeleteRecordFromCache.ts b/packages/twenty-front/src/modules/object-record/cache/hooks/useDeleteRecordFromCache.ts index 693236975e51..90105c700526 100644 --- a/packages/twenty-front/src/modules/object-record/cache/hooks/useDeleteRecordFromCache.ts +++ b/packages/twenty-front/src/modules/object-record/cache/hooks/useDeleteRecordFromCache.ts @@ -18,11 +18,11 @@ export const useDeleteRecordFromCache = ({ const { objectMetadataItems } = useObjectMetadataItems(); - return (recordToDelete: ObjectRecord) => { + return (recordToDestroy: ObjectRecord) => { deleteRecordFromCache({ objectMetadataItem, objectMetadataItems, - recordToDelete, + recordToDestroy, cache: apolloClient.cache, }); }; diff --git a/packages/twenty-front/src/modules/object-record/cache/utils/deleteRecordFromCache.ts b/packages/twenty-front/src/modules/object-record/cache/utils/deleteRecordFromCache.ts index d84c3d764e36..10bc657ca3d7 100644 --- a/packages/twenty-front/src/modules/object-record/cache/utils/deleteRecordFromCache.ts +++ b/packages/twenty-front/src/modules/object-record/cache/utils/deleteRecordFromCache.ts @@ -8,21 +8,21 @@ import { ObjectRecord } from '@/object-record/types/ObjectRecord'; export const deleteRecordFromCache = ({ objectMetadataItem, objectMetadataItems, - recordToDelete, + recordToDestroy, cache, }: { objectMetadataItem: ObjectMetadataItem; objectMetadataItems: ObjectMetadataItem[]; - recordToDelete: ObjectRecord; + recordToDestroy: ObjectRecord; cache: ApolloCache; }) => { triggerDestroyRecordsOptimisticEffect({ cache, objectMetadataItem, objectMetadataItems, - recordsToDelete: [ + recordsToDestroy: [ { - ...recordToDelete, + ...recordToDestroy, __typename: getObjectTypename(objectMetadataItem.nameSingular), }, ], diff --git a/packages/twenty-front/src/modules/object-record/hooks/useCreateManyRecords.ts b/packages/twenty-front/src/modules/object-record/hooks/useCreateManyRecords.ts index d5b823df101f..c236baede835 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useCreateManyRecords.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useCreateManyRecords.ts @@ -122,19 +122,19 @@ export const useCreateManyRecords = < }, }) .catch((error: Error) => { - recordsCreatedInCache.forEach((recordToDelete) => { + recordsCreatedInCache.forEach((recordToDestroy) => { deleteRecordFromCache({ objectMetadataItems, objectMetadataItem, cache: apolloClient.cache, - recordToDelete, + recordToDestroy, }); }); triggerDestroyRecordsOptimisticEffect({ cache: apolloClient.cache, objectMetadataItem, - recordsToDelete: recordsCreatedInCache, + recordsToDestroy: recordsCreatedInCache, objectMetadataItems, }); diff --git a/packages/twenty-front/src/modules/object-record/hooks/useCreateOneRecord.ts b/packages/twenty-front/src/modules/object-record/hooks/useCreateOneRecord.ts index fe1852766a94..73c9cd9897d6 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useCreateOneRecord.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useCreateOneRecord.ts @@ -118,13 +118,13 @@ export const useCreateOneRecord = < objectMetadataItems, objectMetadataItem, cache: apolloClient.cache, - recordToDelete: recordCreatedInCache, + recordToDestroy: recordCreatedInCache, }); triggerDestroyRecordsOptimisticEffect({ cache: apolloClient.cache, objectMetadataItem, - recordsToDelete: [recordCreatedInCache], + recordsToDestroy: [recordCreatedInCache], objectMetadataItems, }); diff --git a/packages/twenty-front/src/modules/object-record/hooks/useDestroyManyRecords.ts b/packages/twenty-front/src/modules/object-record/hooks/useDestroyManyRecords.ts index 5e726513693d..66e19922d5e4 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useDestroyManyRecords.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useDestroyManyRecords.ts @@ -98,7 +98,7 @@ export const useDestroyManyRecords = ({ triggerDestroyRecordsOptimisticEffect({ cache, objectMetadataItem, - recordsToDelete: cachedRecords, + recordsToDestroy: cachedRecords, objectMetadataItems, }); }, diff --git a/packages/twenty-front/src/modules/object-record/hooks/useDestroyOneRecord.ts b/packages/twenty-front/src/modules/object-record/hooks/useDestroyOneRecord.ts index 166bb2432573..1ea6aacea173 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useDestroyOneRecord.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useDestroyOneRecord.ts @@ -68,7 +68,7 @@ export const useDestroyOneRecord = ({ triggerDestroyRecordsOptimisticEffect({ cache, objectMetadataItem, - recordsToDelete: [cachedRecord], + recordsToDestroy: [cachedRecord], objectMetadataItems, }); }, diff --git a/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewFilterRecords.ts b/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewFilterRecords.ts index 9ec7f9c03b59..488b33c31bd9 100644 --- a/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewFilterRecords.ts +++ b/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewFilterRecords.ts @@ -134,7 +134,7 @@ export const usePersistViewFilterRecords = () => { idToDestroy: viewFilterId, }, update: (cache, { data }) => { - const record = data?.['deleteViewFilter']; + const record = data?.['destroyViewFilter']; if (!record) return; @@ -145,7 +145,7 @@ export const usePersistViewFilterRecords = () => { triggerDestroyRecordsOptimisticEffect({ cache, objectMetadataItem, - recordsToDelete: [cachedRecord], + recordsToDestroy: [cachedRecord], objectMetadataItems, }); }, diff --git a/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewSortRecords.ts b/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewSortRecords.ts index 59c26c080ecb..1dae1bf39d62 100644 --- a/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewSortRecords.ts +++ b/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewSortRecords.ts @@ -129,7 +129,7 @@ export const usePersistViewSortRecords = () => { idToDestroy: viewSortId, }, update: (cache, { data }) => { - const record = data?.['deleteViewSort']; + const record = data?.['destroyViewSort']; if (!record) return; @@ -140,7 +140,7 @@ export const usePersistViewSortRecords = () => { triggerDestroyRecordsOptimisticEffect({ cache, objectMetadataItem, - recordsToDelete: [cachedRecord], + recordsToDestroy: [cachedRecord], objectMetadataItems, }); },