Fix Uncaught Error: Workflow is not enabled. If you want to use it, please enable it in the lab. <img width="505" alt="erroractionmenu" src="https://github.com/user-attachments/assets/66e60219-20fb-4b00-90e4-d6bd640be774" /> This error was due to using the hook `useWorkflowWithCurrentVersion` outside of a workflow object. Adding a skip parameter wasn't enough because the `useFindOneRecord` uses `useObjectMetadataItem` which throws if workflows aren't enabled.
53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
|
import { useFindOneRecord } from '@/object-record/hooks/useFindOneRecord';
|
|
import {
|
|
Workflow,
|
|
WorkflowWithCurrentVersion,
|
|
} from '@/workflow/types/Workflow';
|
|
import { useMemo } from 'react';
|
|
import { isDefined } from 'twenty-shared/utils';
|
|
|
|
export const useWorkflowWithCurrentVersion = (
|
|
workflowId: string | undefined,
|
|
): WorkflowWithCurrentVersion | undefined => {
|
|
const { record: workflow } = useFindOneRecord<Workflow>({
|
|
objectNameSingular: CoreObjectNameSingular.Workflow,
|
|
objectRecordId: workflowId,
|
|
recordGqlFields: {
|
|
id: true,
|
|
name: true,
|
|
statuses: true,
|
|
lastPublishedVersionId: true,
|
|
versions: true,
|
|
},
|
|
skip: !isDefined(workflowId),
|
|
});
|
|
|
|
return useMemo(() => {
|
|
if (!isDefined(workflow)) {
|
|
return undefined;
|
|
}
|
|
|
|
const draftVersion = workflow.versions.find(
|
|
(workflowVersion) => workflowVersion.status === 'DRAFT',
|
|
);
|
|
|
|
const workflowVersions = [...workflow.versions];
|
|
|
|
workflowVersions.sort((a, b) => (a.createdAt > b.createdAt ? -1 : 1));
|
|
|
|
const latestVersion = workflowVersions[0];
|
|
|
|
const currentVersion = draftVersion ?? latestVersion;
|
|
|
|
if (!isDefined(currentVersion)) {
|
|
return undefined;
|
|
}
|
|
|
|
return {
|
|
...workflow,
|
|
currentVersion,
|
|
};
|
|
}, [workflow]);
|
|
};
|