Fix manual trigger output schema (#8150)

- add schema for manual trigger
- split into sub functions
- handle case with no variables
This commit is contained in:
Thomas Trompette
2024-10-28 18:42:09 +01:00
committed by GitHub
parent 69c24968c1
commit 409def8431
13 changed files with 305 additions and 83 deletions

View File

@ -1,67 +0,0 @@
import { capitalize } from '~/utils/string/capitalize';
import { useRecoilValue } from 'recoil';
import { workflowIdState } from '@/workflow/states/workflowIdState';
import { useWorkflowWithCurrentVersion } from '@/workflow/hooks/useWorkflowWithCurrentVersion';
import { workflowSelectedNodeState } from '@/workflow/states/workflowSelectedNodeState';
import { getStepDefinitionOrThrow } from '@/workflow/utils/getStepDefinitionOrThrow';
import { isDefined } from 'twenty-ui';
import { StepOutputSchema } from '@/workflow/search-variables/types/StepOutputSchema';
export const useAvailableVariablesInWorkflowStep = (): StepOutputSchema[] => {
const workflowId = useRecoilValue(workflowIdState);
const workflow = useWorkflowWithCurrentVersion(workflowId);
const workflowSelectedNode = useRecoilValue(workflowSelectedNodeState);
if (!isDefined(workflowSelectedNode) || !isDefined(workflow)) {
return [];
}
const stepDefinition = getStepDefinitionOrThrow({
stepId: workflowSelectedNode,
workflowVersion: workflow.currentVersion,
});
if (
!isDefined(stepDefinition) ||
stepDefinition.type === 'trigger' ||
!isDefined(workflow.currentVersion.steps)
) {
return [];
}
const previousSteps = [];
for (const step of workflow.currentVersion.steps) {
if (step.id === workflowSelectedNode) {
break;
}
previousSteps.push(step);
}
const result = [];
if (
workflow.currentVersion.trigger?.type === 'DATABASE_EVENT' &&
isDefined(workflow.currentVersion.trigger?.settings?.outputSchema)
) {
const [object, action] =
workflow.currentVersion.trigger.settings.eventName.split('.');
result.push({
id: 'trigger',
name: `${capitalize(object)} is ${capitalize(action)}`,
outputSchema: workflow.currentVersion.trigger.settings.outputSchema,
});
}
previousSteps.forEach((previousStep) => {
if (isDefined(previousStep.settings.outputSchema)) {
result.push({
id: previousStep.id,
name: previousStep.name,
outputSchema: previousStep.settings.outputSchema,
});
}
});
return result;
};