11import type {
2+ IAction ,
3+ IApp ,
24 IDataOutMetadata ,
35 IDataOutMetadatum ,
46 IExecutionStep ,
7+ ITrigger ,
58 TDataOutMetadatumType ,
69} from '@plumber/types'
710
811import get from 'lodash.get'
912
1013import { RawColumn , RawRow } from '@/components/VariablesList/utils'
14+ import { TOOLBOX_ACTIONS , TOOLBOX_APP_KEY } from '@/helpers/toolbox'
1115
1216// these are the variable types to display on the frontend (make visible)
1317export const VISIBLE_VARIABLE_TYPES : TDataOutMetadatumType [ ] = [
@@ -44,6 +48,40 @@ export interface Variable {
4448 isHidden ?: boolean
4549}
4650
51+ function getStepName ( executionStep : IExecutionStep , allApps : IApp [ ] ) {
52+ const { appKey, key, step } = executionStep
53+
54+ const isTrigger = [ 'formsg' , 'webhook' , 'scheduler' ] . includes ( appKey )
55+ const isIfThen = appKey === TOOLBOX_APP_KEY && key === TOOLBOX_ACTIONS . IfThen
56+ const app = allApps ?. find ( ( a : IApp ) => a . key === appKey )
57+ const actionsOrTriggers : Array < ITrigger | IAction > =
58+ ( isTrigger ? app ?. triggers : app ?. actions ) || [ ]
59+
60+ const selectedActionOrTrigger = actionsOrTriggers . find (
61+ ( actionOrTrigger : IAction | ITrigger ) => actionOrTrigger . key === key ,
62+ )
63+
64+ let caption = ''
65+ const defaultCaption = selectedActionOrTrigger ?. name
66+ if ( step ?. config ?. stepName ) {
67+ caption = `${ step . config . stepName } `
68+ } else if ( defaultCaption ) {
69+ caption = defaultCaption
70+
71+ if ( isIfThen ) {
72+ caption = 'Condition'
73+ }
74+ } else if ( app ?. name ) {
75+ caption = app . name
76+ }
77+
78+ return (
79+ step ?. config ?. stepName ||
80+ caption ||
81+ ( appKey || '' ) . charAt ( 0 ) ?. toUpperCase ( ) + appKey ?. slice ( 1 )
82+ )
83+ }
84+
4785function sortVariables ( variables : Variable [ ] ) : void {
4886 variables . sort ( ( a , b ) => {
4987 // Put vars with null order last, but preserve ordering (via `sort`'s
@@ -198,6 +236,7 @@ const process = (
198236
199237export function extractVariables (
200238 executionSteps : IExecutionStep [ ] ,
239+ allApps ?: IApp [ ] ,
201240) : StepWithVariables [ ] {
202241 if ( ! executionSteps ) {
203242 return [ ]
@@ -218,15 +257,12 @@ export function extractVariables(
218257 metadata ,
219258 '' ,
220259 )
260+ const stepName = getStepName ( executionStep , allApps || [ ] )
221261 // sort variable by order key in-place
222262 sortVariables ( variables )
223263 return {
224264 id : executionStep . stepId ,
225- name : `${ executionStep . step . position } . ${
226- executionStep . step ?. config ?. stepName ||
227- ( executionStep . appKey || '' ) . charAt ( 0 ) ?. toUpperCase ( ) +
228- executionStep . appKey ?. slice ( 1 )
229- } `,
265+ name : `${ executionStep . step . position } . ${ stepName } ` ,
230266 output : variables ,
231267 }
232268 } )
0 commit comments