diff --git a/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect.ts b/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect.ts index 0aa4c51e9d2c..80c880094a54 100644 --- a/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect.ts +++ b/packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect.ts @@ -1,13 +1,11 @@ import { ApolloCache, StoreObject } from '@apollo/client'; import { triggerUpdateRelationsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerUpdateRelationsOptimisticEffect'; -import { CachedObjectRecordQueryVariables } from '@/apollo/types/CachedObjectRecordQueryVariables'; import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { RecordGqlRefEdge } from '@/object-record/cache/types/RecordGqlRefEdge'; import { isObjectRecordConnectionWithRefs } from '@/object-record/cache/utils/isObjectRecordConnectionWithRefs'; import { RecordGqlNode } from '@/object-record/graphql/types/RecordGqlNode'; import { isDefined } from '~/utils/isDefined'; -import { parseApolloStoreFieldName } from '~/utils/parseApolloStoreFieldName'; export const triggerDeleteRecordsOptimisticEffect = ({ cache, @@ -24,7 +22,7 @@ export const triggerDeleteRecordsOptimisticEffect = ({ fields: { [objectMetadataItem.namePlural]: ( rootQueryCachedResponse, - { DELETE, readField, storeFieldName }, + { readField }, ) => { const rootQueryCachedResponseIsNotACachedObjectRecordConnection = !isObjectRecordConnectionWithRefs( @@ -38,11 +36,6 @@ export const triggerDeleteRecordsOptimisticEffect = ({ const rootQueryCachedObjectRecordConnection = rootQueryCachedResponse; - const { fieldVariables: rootQueryVariables } = - parseApolloStoreFieldName( - storeFieldName, - ); - const recordIdsToDelete = recordsToDelete.map(({ id }) => id); const cachedEdges = readField( @@ -65,14 +58,6 @@ export const triggerDeleteRecordsOptimisticEffect = ({ if (nextCachedEdges.length === cachedEdges?.length) return rootQueryCachedObjectRecordConnection; - // TODO: same as in update, should we trigger DELETE ? - if ( - isDefined(rootQueryVariables?.first) && - cachedEdges?.length === rootQueryVariables.first - ) { - return DELETE; - } - return { ...rootQueryCachedObjectRecordConnection, edges: nextCachedEdges, diff --git a/packages/twenty-front/src/modules/workflow/components/RecordShowPageWorkflowHeader.tsx b/packages/twenty-front/src/modules/workflow/components/RecordShowPageWorkflowHeader.tsx index cfea2f3b543e..c2000974e5f9 100644 --- a/packages/twenty-front/src/modules/workflow/components/RecordShowPageWorkflowHeader.tsx +++ b/packages/twenty-front/src/modules/workflow/components/RecordShowPageWorkflowHeader.tsx @@ -50,7 +50,6 @@ export const RecordShowPageWorkflowHeader = ({ ); return deleteOneWorkflowVersion({ - workflowId: workflowWithCurrentVersion.id, workflowVersionId: workflowWithCurrentVersion.currentVersion.id, }); }} @@ -69,9 +68,10 @@ export const RecordShowPageWorkflowHeader = ({ workflowWithCurrentVersion, ); - return activateWorkflowVersion( - workflowWithCurrentVersion.currentVersion.id, - ); + return activateWorkflowVersion({ + workflowVersionId: workflowWithCurrentVersion.currentVersion.id, + workflowId: workflowWithCurrentVersion.id, + }); }} /> ) : workflowWithCurrentVersion?.currentVersion?.status === 'ACTIVE' ? ( diff --git a/packages/twenty-front/src/modules/workflow/components/RecordShowPageWorkflowVersionHeader.tsx b/packages/twenty-front/src/modules/workflow/components/RecordShowPageWorkflowVersionHeader.tsx index d7d66c2dc878..79cbe7c27f77 100644 --- a/packages/twenty-front/src/modules/workflow/components/RecordShowPageWorkflowVersionHeader.tsx +++ b/packages/twenty-front/src/modules/workflow/components/RecordShowPageWorkflowVersionHeader.tsx @@ -108,7 +108,10 @@ export const RecordShowPageWorkflowVersionHeader = ({ Icon={IconPower} disabled={isWaitingForWorkflowVersion} onClick={() => { - return activateWorkflowVersion(workflowVersion.id); + return activateWorkflowVersion({ + workflowVersionId: workflowVersion.id, + workflowId: workflowVersion.workflowId, + }); }} /> ) : workflowVersion?.status === 'ACTIVE' ? ( diff --git a/packages/twenty-front/src/modules/workflow/hooks/useActivateWorkflowVersion.tsx b/packages/twenty-front/src/modules/workflow/hooks/useActivateWorkflowVersion.tsx index 230b7be35b29..23bdf15a4138 100644 --- a/packages/twenty-front/src/modules/workflow/hooks/useActivateWorkflowVersion.tsx +++ b/packages/twenty-front/src/modules/workflow/hooks/useActivateWorkflowVersion.tsx @@ -1,18 +1,19 @@ import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient'; import { ApolloClient, useApolloClient, useMutation } from '@apollo/client'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; -import { useFindOneRecordQuery } from '@/object-record/hooks/useFindOneRecordQuery'; +import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache'; import { ACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/activateWorkflowVersion'; +import { WorkflowVersion } from '@/workflow/types/Workflow'; import { ActivateWorkflowVersionMutation, ActivateWorkflowVersionMutationVariables, } from '~/generated/graphql'; export const useActivateWorkflowVersion = () => { - const apolloClient = useApolloClient(); - const apolloMetadataClient = useApolloMetadataClient(); + const apolloClient = useApolloClient(); const [mutate] = useMutation< ActivateWorkflowVersionMutation, ActivateWorkflowVersionMutationVariables @@ -20,24 +21,55 @@ export const useActivateWorkflowVersion = () => { client: apolloMetadataClient ?? ({} as ApolloClient), }); - const { findOneRecordQuery: findOneWorkflowVersionQuery } = - useFindOneRecordQuery({ + const { objectMetadataItem: objectMetadataItemWorkflowVersion } = + useObjectMetadataItem({ objectNameSingular: CoreObjectNameSingular.WorkflowVersion, }); - const activateWorkflowVersion = async (workflowVersionId: string) => { + 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', + }, + }); - await apolloClient.query({ - query: findOneWorkflowVersionQuery, - variables: { - objectRecordId: workflowVersionId, + const cacheSnapshot = apolloClient.cache.extract(); + const allWorkflowVersions: Array = Object.values( + cacheSnapshot, + ).filter( + (item) => + item.__typename === 'WorkflowVersion' && + item.workflowId === workflowId, + ); + + for (const workflowVersion of allWorkflowVersions) { + apolloClient.cache.modify({ + id: apolloClient.cache.identify(workflowVersion), + fields: { + status: () => { + return workflowVersion.id !== workflowVersionId && + workflowVersion.status === 'ACTIVE' + ? 'ARCHIVED' + : workflowVersion.status; + }, + }, + }); + } }, - fetchPolicy: 'network-only', }); }; diff --git a/packages/twenty-front/src/modules/workflow/hooks/useDeactivateWorkflowVersion.tsx b/packages/twenty-front/src/modules/workflow/hooks/useDeactivateWorkflowVersion.tsx index 2ebd34f67453..ec71956e69dc 100644 --- a/packages/twenty-front/src/modules/workflow/hooks/useDeactivateWorkflowVersion.tsx +++ b/packages/twenty-front/src/modules/workflow/hooks/useDeactivateWorkflowVersion.tsx @@ -1,8 +1,9 @@ import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient'; import { ApolloClient, useApolloClient, useMutation } from '@apollo/client'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; -import { useFindOneRecordQuery } from '@/object-record/hooks/useFindOneRecordQuery'; +import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache'; import { DEACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/deactivateWorkflowVersion'; import { ActivateWorkflowVersionMutation, @@ -10,9 +11,8 @@ import { } from '~/generated/graphql'; export const useDeactivateWorkflowVersion = () => { - const apolloClient = useApolloClient(); - const apolloMetadataClient = useApolloMetadataClient(); + const apolloClient = useApolloClient(); const [mutate] = useMutation< ActivateWorkflowVersionMutation, ActivateWorkflowVersionMutationVariables @@ -20,8 +20,8 @@ export const useDeactivateWorkflowVersion = () => { client: apolloMetadataClient ?? ({} as ApolloClient), }); - const { findOneRecordQuery: findOneWorkflowVersionQuery } = - useFindOneRecordQuery({ + const { objectMetadataItem: objectMetadataItemWorkflowVersion } = + useObjectMetadataItem({ objectNameSingular: CoreObjectNameSingular.WorkflowVersion, }); @@ -30,14 +30,16 @@ export const useDeactivateWorkflowVersion = () => { variables: { workflowVersionId, }, - }); - - await apolloClient.query({ - query: findOneWorkflowVersionQuery, - variables: { - objectRecordId: workflowVersionId, + update: () => { + modifyRecordFromCache({ + cache: apolloClient.cache, + recordId: workflowVersionId, + objectMetadataItem: objectMetadataItemWorkflowVersion, + fieldModifiers: { + status: () => 'DEACTIVATED', + }, + }); }, - fetchPolicy: 'network-only', }); }; diff --git a/packages/twenty-front/src/modules/workflow/hooks/useDeleteOneWorkflowVersion.tsx b/packages/twenty-front/src/modules/workflow/hooks/useDeleteOneWorkflowVersion.tsx index 7997b14e3afe..bd3cf0305664 100644 --- a/packages/twenty-front/src/modules/workflow/hooks/useDeleteOneWorkflowVersion.tsx +++ b/packages/twenty-front/src/modules/workflow/hooks/useDeleteOneWorkflowVersion.tsx @@ -1,36 +1,17 @@ import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; import { useDeleteOneRecord } from '@/object-record/hooks/useDeleteOneRecord'; -import { useFindOneRecordQuery } from '@/object-record/hooks/useFindOneRecordQuery'; -import { useApolloClient } from '@apollo/client'; export const useDeleteOneWorkflowVersion = () => { - const apolloClient = useApolloClient(); - - const { findOneRecordQuery: findOneWorkflowRecordQuery } = - useFindOneRecordQuery({ - objectNameSingular: CoreObjectNameSingular.Workflow, - }); - const { deleteOneRecord } = useDeleteOneRecord({ objectNameSingular: CoreObjectNameSingular.WorkflowVersion, }); const deleteOneWorkflowVersion = async ({ - workflowId, workflowVersionId, }: { - workflowId: string; workflowVersionId: string; }) => { await deleteOneRecord(workflowVersionId); - - await apolloClient.query({ - query: findOneWorkflowRecordQuery, - variables: { - objectRecordId: workflowId, - }, - fetchPolicy: 'network-only', - }); }; return { deleteOneWorkflowVersion }; diff --git a/packages/twenty-front/src/modules/workflow/hooks/useWorkflowWithCurrentVersion.tsx b/packages/twenty-front/src/modules/workflow/hooks/useWorkflowWithCurrentVersion.tsx index fc87a6a6ec2d..d24da7d10f95 100644 --- a/packages/twenty-front/src/modules/workflow/hooks/useWorkflowWithCurrentVersion.tsx +++ b/packages/twenty-front/src/modules/workflow/hooks/useWorkflowWithCurrentVersion.tsx @@ -41,7 +41,6 @@ export const useWorkflowWithCurrentVersion = ( createdAt: 'DescNullsLast', }, ], - limit: 1, skip: !isDefined(workflowId), }); @@ -50,7 +49,16 @@ export const useWorkflowWithCurrentVersion = ( return undefined; } - const currentVersion = mostRecentWorkflowVersions?.[0]; + const draftVersion = mostRecentWorkflowVersions.find( + (workflowVersion) => workflowVersion.status === 'DRAFT', + ); + const latestVersion = mostRecentWorkflowVersions[0]; + + const currentVersion = draftVersion ?? latestVersion; + + if (!isDefined(currentVersion)) { + return undefined; + } return { ...workflow,