Workflow statuses update on record table - use cache instead of web sockets (#16391)
Updated the 3 hooks - activate, deactivate and discard draft (deleteVersion) - so those updates the cache. Removed the update listeners. Also wrapped a few actions to unsure workflowId is not undefined when received by useWorkflowWithCurrentVersion hook. Otherwise, useFindRecord with by performed with skip true, disconnecting tmp from the cache and making it miss a statuses update. https://github.com/user-attachments/assets/5fc355e8-cf18-4881-855b-744eb253b79e
This commit is contained in:
@@ -5,11 +5,16 @@ import { useApolloCoreClient } from '@/object-metadata/hooks/useApolloCoreClient
|
||||
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
|
||||
import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems';
|
||||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
||||
import { getRecordFromCache } from '@/object-record/cache/utils/getRecordFromCache';
|
||||
import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache';
|
||||
import { useObjectPermissions } from '@/object-record/hooks/useObjectPermissions';
|
||||
import { useUpsertRecordsInStore } from '@/object-record/record-store/hooks/useUpsertRecordsInStore';
|
||||
import { ACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/mutations/activateWorkflowVersion';
|
||||
import { type WorkflowVersion } from '@/workflow/types/Workflow';
|
||||
import {
|
||||
type Workflow,
|
||||
type WorkflowStatus,
|
||||
type WorkflowVersion,
|
||||
} from '@/workflow/types/Workflow';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
import {
|
||||
type ActivateWorkflowVersionMutation,
|
||||
@@ -30,6 +35,10 @@ export const useActivateWorkflowVersion = () => {
|
||||
useObjectMetadataItem({
|
||||
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
|
||||
});
|
||||
const { objectMetadataItem: objectMetadataItemWorkflow } =
|
||||
useObjectMetadataItem({
|
||||
objectNameSingular: CoreObjectNameSingular.Workflow,
|
||||
});
|
||||
const { objectMetadataItems } = useObjectMetadataItems();
|
||||
|
||||
const { objectPermissionsByObjectMetadataId } = useObjectPermissions();
|
||||
@@ -56,6 +65,7 @@ export const useActivateWorkflowVersion = () => {
|
||||
});
|
||||
|
||||
const cacheSnapshot = apolloCoreClient.cache.extract();
|
||||
|
||||
const allWorkflowVersions: Array<WorkflowVersion> = Object.values(
|
||||
cacheSnapshot,
|
||||
).filter(
|
||||
@@ -114,6 +124,37 @@ export const useActivateWorkflowVersion = () => {
|
||||
upsertRecordsInStore,
|
||||
});
|
||||
}
|
||||
|
||||
const cachedWorkflow = getRecordFromCache<Workflow>({
|
||||
objectMetadataItem: objectMetadataItemWorkflow,
|
||||
cache: apolloCoreClient.cache,
|
||||
objectMetadataItems,
|
||||
objectPermissionsByObjectMetadataId,
|
||||
recordId: workflowId,
|
||||
});
|
||||
|
||||
const newStatuses = new Set(
|
||||
[...(cachedWorkflow?.statuses ?? []), 'ACTIVE'].filter(
|
||||
(status) => status !== 'DEACTIVATED',
|
||||
),
|
||||
);
|
||||
|
||||
if (isDefined(cachedWorkflow)) {
|
||||
modifyRecordFromCache({
|
||||
cache: apolloCoreClient.cache,
|
||||
recordId: workflowId,
|
||||
objectMetadataItem: objectMetadataItemWorkflow,
|
||||
fieldModifiers: {
|
||||
statuses: () => Array.from(newStatuses),
|
||||
},
|
||||
});
|
||||
upsertRecordsInStore([
|
||||
{
|
||||
...cachedWorkflow,
|
||||
statuses: Array.from(newStatuses) as WorkflowStatus[],
|
||||
},
|
||||
]);
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user