mirror of
https://github.com/lingble/twenty.git
synced 2025-11-20 07:54:52 +00:00
- Improve the design of the right drawer - Allow to update the trigger of the workflow: the object and the event listened to - Allow to update the selected serverless function that a code action should execute - Change how we determine which workflow version to display in the visualizer. We fetch the selected workflow's data, including whether it has a draft or a published version. If the workflow has a draft version, it gets displayed; otherwise, we display the last published version. - I used the type `WorkflowWithCurrentVersion` to forward the currently edited workflow with its _current_ version embedded across the app. - I created single-responsibility hooks like `useFindWorkflowWithCurrentVersion`, `useFindShowPageWorkflow`, `useUpdateWorkflowVersionTrigger` or `useUpdateWorkflowVersionStep`. - I updated the types for workflow related objects, like `Workflow` and `WorkflowVersion`. See `packages/twenty-front/src/modules/workflow/types/Workflow.ts`. - This introduced the possibility to have `null` values for triggers and steps. I made the according changes in the codebase and in the tests. - I created a utility function to extract both parts of object-event format (`company.created`): `packages/twenty-front/src/modules/workflow/utils/splitWorkflowTriggerEventName.ts`
84 lines
2.6 KiB
TypeScript
84 lines
2.6 KiB
TypeScript
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
|
import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord';
|
|
import { workflowCreateStepFromParentStepIdState } from '@/workflow/states/workflowCreateStepFromParentStepIdState';
|
|
import { workflowDiagramTriggerNodeSelectionState } from '@/workflow/states/workflowDiagramTriggerNodeSelectionState';
|
|
import {
|
|
WorkflowStep,
|
|
WorkflowStepType,
|
|
WorkflowVersion,
|
|
WorkflowWithCurrentVersion,
|
|
} from '@/workflow/types/Workflow';
|
|
import { getStepDefaultDefinition } from '@/workflow/utils/getStepDefaultDefinition';
|
|
import { insertStep } from '@/workflow/utils/insertStep';
|
|
import { useRecoilValue, useSetRecoilState } from 'recoil';
|
|
import { isDefined } from 'twenty-ui';
|
|
|
|
export const useCreateStep = ({
|
|
workflow,
|
|
}: {
|
|
workflow: WorkflowWithCurrentVersion;
|
|
}) => {
|
|
const workflowCreateStepFromParentStepId = useRecoilValue(
|
|
workflowCreateStepFromParentStepIdState,
|
|
);
|
|
|
|
const setWorkflowDiagramTriggerNodeSelection = useSetRecoilState(
|
|
workflowDiagramTriggerNodeSelectionState,
|
|
);
|
|
|
|
const { updateOneRecord: updateOneWorkflowVersion } =
|
|
useUpdateOneRecord<WorkflowVersion>({
|
|
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
|
|
});
|
|
|
|
const insertNodeAndSave = ({
|
|
parentNodeId,
|
|
nodeToAdd,
|
|
}: {
|
|
parentNodeId: string;
|
|
nodeToAdd: WorkflowStep;
|
|
}) => {
|
|
const currentVersion = workflow.currentVersion;
|
|
if (!isDefined(currentVersion)) {
|
|
throw new Error("Can't add a node when there is no current version.");
|
|
}
|
|
|
|
return updateOneWorkflowVersion({
|
|
idToUpdate: currentVersion.id,
|
|
updateOneRecordInput: {
|
|
steps: insertStep({
|
|
steps: currentVersion.steps ?? [],
|
|
parentStepId: parentNodeId,
|
|
stepToAdd: nodeToAdd,
|
|
}),
|
|
},
|
|
});
|
|
};
|
|
|
|
const createStep = async (newStepType: WorkflowStepType) => {
|
|
if (!isDefined(workflowCreateStepFromParentStepId)) {
|
|
throw new Error('Select a step to create a new step from first.');
|
|
}
|
|
|
|
const newStep = getStepDefaultDefinition(newStepType);
|
|
|
|
await insertNodeAndSave({
|
|
parentNodeId: workflowCreateStepFromParentStepId,
|
|
nodeToAdd: newStep,
|
|
});
|
|
|
|
/**
|
|
* After the step has been created, select it.
|
|
* As the `insertNodeAndSave` function mutates the cached workflow before resolving,
|
|
* we are sure that the new node will have been created at this stage.
|
|
*
|
|
* Selecting the node will cause a right drawer to open in order to edit the step.
|
|
*/
|
|
setWorkflowDiagramTriggerNodeSelection(newStep.id);
|
|
};
|
|
|
|
return {
|
|
createStep,
|
|
};
|
|
};
|