Files
twenty/packages/twenty-front/src/modules/workflow/utils/getStepDefinitionOrThrow.ts
T
Thomas TrompetteandGitHub 6ea7aea92b Migrate update step changes to diff (#14663)
This PR should fix optimistic rendering issues on step updates:
- compute a diff for trigger and steps on mutations
- build an util that applies that diff (built a more robust version from
https://github.com/AsyncBanana/micropatch)
- apply diff in cache
2025-09-23 16:41:26 +00:00

51 lines
1.1 KiB
TypeScript

import {
type WorkflowAction,
type WorkflowTrigger,
} from '@/workflow/types/Workflow';
import { findStepPosition } from '@/workflow/utils/findStepPosition';
import { isDefined } from 'twenty-shared/utils';
import { TRIGGER_STEP_ID } from 'twenty-shared/workflow';
export const getStepDefinitionOrThrow = ({
stepId,
trigger,
steps,
}: {
stepId: string;
trigger: WorkflowTrigger | null;
steps: Array<WorkflowAction> | null;
}) => {
if (stepId === TRIGGER_STEP_ID) {
if (!isDefined(trigger)) {
return {
type: 'trigger',
definition: undefined,
} as const;
}
return {
type: 'trigger',
definition: trigger,
} as const;
}
if (!isDefined(steps)) {
throw new Error(
'Malformed workflow version: missing steps information; be sure to create at least one step before trying to edit one',
);
}
const selectedNodePosition = findStepPosition({
steps,
stepId,
});
if (!isDefined(selectedNodePosition)) {
return undefined;
}
return {
type: 'action',
definition: selectedNodePosition.steps[selectedNodePosition.index],
} as const;
};