From 4efa959401de4ac9c84d21a09ee7e102daa917f7 Mon Sep 17 00:00:00 2001 From: Thomas Trompette Date: Fri, 10 Jan 2025 18:33:10 +0100 Subject: [PATCH] Fix apollo cache for workflow cmd+k (#9549) When a workflow was activate, the previous activated version was still available in cmd+k This is because we were only updating entities in cache after activation. We also need to update the queries stored in cache, because this is where the cmd+k look to know which workflow is active. https://github.com/user-attachments/assets/750cc24a-7583-4641-856c-0f9892d26331 --- .../hooks/useActivateWorkflowVersion.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/twenty-front/src/modules/workflow/hooks/useActivateWorkflowVersion.ts b/packages/twenty-front/src/modules/workflow/hooks/useActivateWorkflowVersion.ts index 5797c9707..542563599 100644 --- a/packages/twenty-front/src/modules/workflow/hooks/useActivateWorkflowVersion.ts +++ b/packages/twenty-front/src/modules/workflow/hooks/useActivateWorkflowVersion.ts @@ -1,6 +1,7 @@ import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient'; import { useApolloClient, useMutation } from '@apollo/client'; +import { triggerUpdateRecordOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerUpdateRecordOptimisticEffect'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache'; @@ -56,7 +57,12 @@ export const useActivateWorkflowVersion = () => { item.workflowId === workflowId, ); - for (const workflowVersion of allWorkflowVersions) { + const previousActiveWorkflowVersions = allWorkflowVersions.filter( + (version) => + version.status === 'ACTIVE' && version.id !== workflowVersionId, + ); + + for (const workflowVersion of previousActiveWorkflowVersions) { apolloClient.cache.modify({ id: apolloClient.cache.identify(workflowVersion), fields: { @@ -68,6 +74,17 @@ export const useActivateWorkflowVersion = () => { }, }, }); + + triggerUpdateRecordOptimisticEffect({ + cache: apolloClient.cache, + objectMetadataItem: objectMetadataItemWorkflowVersion, + currentRecord: workflowVersion, + updatedRecord: { + ...workflowVersion, + status: 'ARCHIVED', + }, + objectMetadataItems: [objectMetadataItemWorkflowVersion], + }); } }, });