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
This commit is contained in:
Thomas Trompette
2025-01-10 18:33:10 +01:00
committed by GitHub
parent 11a47264a7
commit 4efa959401

View File

@ -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],
});
}
},
});