88735f6788
Workflow, workflow version and workflow runs should not be passed through props or context. These are set in recoil component states and that's where all hooks should look for these. This PR stop propagating workflow with version through all components.
38 lines
1.5 KiB
TypeScript
38 lines
1.5 KiB
TypeScript
import { useRecoilComponentValue } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValue';
|
|
import { useCreateDraftFromWorkflowVersion } from '@/workflow/hooks/useCreateDraftFromWorkflowVersion';
|
|
import { useWorkflowWithCurrentVersion } from '@/workflow/hooks/useWorkflowWithCurrentVersion';
|
|
import { workflowVisualizerWorkflowIdComponentState } from '@/workflow/states/workflowVisualizerWorkflowIdComponentState';
|
|
import { isDefined } from 'twenty-shared/utils';
|
|
|
|
export const useGetUpdatableWorkflowVersionOrThrow = () => {
|
|
const { createDraftFromWorkflowVersion } =
|
|
useCreateDraftFromWorkflowVersion();
|
|
const workflowVisualizerWorkflowId = useRecoilComponentValue(
|
|
workflowVisualizerWorkflowIdComponentState,
|
|
);
|
|
const workflow = useWorkflowWithCurrentVersion(workflowVisualizerWorkflowId);
|
|
|
|
const getUpdatableWorkflowVersion = async (): Promise<string> => {
|
|
if (!isDefined(workflowVisualizerWorkflowId) || !isDefined(workflow)) {
|
|
throw new Error('Failed to get updatable workflow version');
|
|
}
|
|
|
|
if (workflow.currentVersion.status === 'DRAFT') {
|
|
return workflow.currentVersion.id;
|
|
}
|
|
|
|
const draftVersionId = await createDraftFromWorkflowVersion({
|
|
workflowId: workflowVisualizerWorkflowId,
|
|
workflowVersionIdToCopy: workflow.currentVersion.id,
|
|
});
|
|
|
|
if (!isDefined(draftVersionId)) {
|
|
throw new Error('Failed to create draft version');
|
|
}
|
|
|
|
return draftVersionId;
|
|
};
|
|
|
|
return { getUpdatableWorkflowVersion };
|
|
};
|