Files
twenty/packages/twenty-front/src/modules/workflow/hooks/useActivateWorkflowVersion.ts
Thomas Trompette b5736a28fd Fix workflow activation optimistic rendering in Cmd+K (#10109)
Close
https://discord.com/channels/1130383047699738754/1334441759484149793

Using refetch queries was not working for certain use cases.
To find manual active workflows in cmd+k, we use a query with specific
filters that was complicated to refetch.

Finally I will update the cache manually. It was not properly updated
before because the json value of the version trigger was stringified
without spaces. So the entity was not found in apollo cache.
2025-02-10 16:46:36 +00:00

111 lines
3.7 KiB
TypeScript

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';
import { ACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/mutations/activateWorkflowVersion';
import { WorkflowVersion } from '@/workflow/types/Workflow';
import { isDefined } from 'twenty-shared';
import {
ActivateWorkflowVersionMutation,
ActivateWorkflowVersionMutationVariables,
} from '~/generated/graphql';
export const useActivateWorkflowVersion = () => {
const apolloClient = useApolloClient();
const [mutate] = useMutation<
ActivateWorkflowVersionMutation,
ActivateWorkflowVersionMutationVariables
>(ACTIVATE_WORKFLOW_VERSION, {
client: apolloClient,
});
const { objectMetadataItem: objectMetadataItemWorkflowVersion } =
useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
});
const activateWorkflowVersion = async ({
workflowVersionId,
workflowId,
}: {
workflowVersionId: string;
workflowId: string;
}) => {
await mutate({
variables: {
workflowVersionId,
},
update: () => {
modifyRecordFromCache({
cache: apolloClient.cache,
recordId: workflowVersionId,
objectMetadataItem: objectMetadataItemWorkflowVersion,
fieldModifiers: {
status: () => 'ACTIVE',
},
});
const cacheSnapshot = apolloClient.cache.extract();
const allWorkflowVersions: Array<WorkflowVersion> = Object.values(
cacheSnapshot,
).filter(
(item) =>
item.__typename === 'WorkflowVersion' &&
item.workflowId === workflowId,
);
const previousActiveWorkflowVersions = allWorkflowVersions.filter(
(version) =>
version.status === 'ACTIVE' && version.id !== workflowVersionId,
);
const newlyActiveWorkflowVersion = allWorkflowVersions.find(
(version) => version.id === workflowVersionId,
);
if (isDefined(newlyActiveWorkflowVersion)) {
triggerUpdateRecordOptimisticEffect({
cache: apolloClient.cache,
objectMetadataItem: objectMetadataItemWorkflowVersion,
currentRecord: newlyActiveWorkflowVersion,
updatedRecord: {
...newlyActiveWorkflowVersion,
status: 'ACTIVE',
},
objectMetadataItems: [objectMetadataItemWorkflowVersion],
});
}
for (const workflowVersion of previousActiveWorkflowVersions) {
apolloClient.cache.modify({
id: apolloClient.cache.identify(workflowVersion),
fields: {
status: () => {
return workflowVersion.id !== workflowVersionId &&
workflowVersion.status === 'ACTIVE'
? 'ARCHIVED'
: workflowVersion.status;
},
},
});
triggerUpdateRecordOptimisticEffect({
cache: apolloClient.cache,
objectMetadataItem: objectMetadataItemWorkflowVersion,
currentRecord: workflowVersion,
updatedRecord: {
...workflowVersion,
status: 'ARCHIVED',
},
objectMetadataItems: [objectMetadataItemWorkflowVersion],
});
}
},
});
};
return { activateWorkflowVersion };
};