1- import { IAction , IApp , IStep , ISubstep , ITrigger } from '@plumber/types'
1+ import {
2+ IAction ,
3+ IApp ,
4+ IStep ,
5+ IStepApprovalBranch ,
6+ ISubstep ,
7+ ITrigger ,
8+ } from '@plumber/types'
29
310import { useContext , useMemo } from 'react'
4- import get from 'lodash/get'
511
612import { EditorContext } from '@/contexts/Editor'
7- import { FORMSG_APP_KEY , MRF_ACTION_KEY } from '@/helpers/formsg '
13+ import { MrfContext } from '@/contexts/MrfContext '
814import getStepName from '@/helpers/getStepName'
915import {
1016 isIfThenStep as checkIfThenStep ,
@@ -27,6 +33,7 @@ interface UseStepMetadataResult {
2733 isDeletable : boolean
2834 isMrfStep : boolean
2935 isApprovalStep : boolean
36+ approvalBranch : IStepApprovalBranch | null
3037}
3138
3239export function useStepMetadata (
@@ -35,6 +42,7 @@ export function useStepMetadata(
3542 allowReorder ?: boolean ,
3643) : UseStepMetadataResult {
3744 const { readOnly, isMobile, isDrawerOpen } = useContext ( EditorContext )
45+ const { mrfSteps, approvalBranches } = useContext ( MrfContext )
3846
3947 const isCompleted = step ?. status === 'completed'
4048 const isTrigger = step ?. type === 'trigger'
@@ -75,11 +83,59 @@ export function useStepMetadata(
7583 [ readOnly , selectedActionOrTrigger , step ?. key ] ,
7684 )
7785
78- const isMrfStep =
79- step ?. appKey === FORMSG_APP_KEY && step ?. key === MRF_ACTION_KEY
86+ const isMrfStep = useMemo ( ( ) => {
87+ return mrfSteps . some ( ( mrfStep ) => mrfStep . id === step ?. id )
88+ } , [ mrfSteps , step ?. id ] )
89+
90+ const isApprovalStep = useMemo ( ( ) => {
91+ if ( ! step ?. id ) {
92+ return false
93+ }
94+ return approvalBranches [ step . id ] !== undefined
95+ } , [ approvalBranches , step ?. id ] )
8096
81- const isApprovalStep =
82- isMrfStep && ! ! get ( step ?. parameters , 'mrf.approvalField' , false )
97+ const approvalBranch = useMemo ( ( ) => {
98+ // If step not instantiated, return null
99+ if ( ! step ) {
100+ return null
101+ }
102+ // If not MRF workflow, return null
103+ if ( ! mrfSteps . length || ! step ) {
104+ return null
105+ }
106+ // If step is an MRF step, return null
107+ if ( isMrfStep ) {
108+ return null
109+ }
110+ if ( step . config . approval ?. branch === 'reject' ) {
111+ return 'reject'
112+ }
113+ // Find the most immediate prior mrfStep by iterating from the last to the first.
114+ let immediatePriorMrfStep = null
115+ for ( let i = mrfSteps . length - 1 ; i >= 0 ; i -- ) {
116+ const mrfStep = mrfSteps [ i ]
117+ if ( mrfStep . position < step . position ) {
118+ immediatePriorMrfStep = mrfStep
119+ break
120+ }
121+ }
122+ // if is approval step, return whether
123+ if ( ! immediatePriorMrfStep ) {
124+ console . warn (
125+ 'No immediate prior mrf step found... this should not happen tho' ,
126+ )
127+ return null
128+ }
129+ /**
130+ * If no approval step config exists, but is part of an mrf branch,
131+ * it's part of the approve branch by default
132+ */
133+ if ( approvalBranches [ immediatePriorMrfStep . id ] ) {
134+ return 'approve'
135+ }
136+ // If follows a non-approval mrf step, return null
137+ return null
138+ } , [ step , mrfSteps , isMrfStep , approvalBranches ] )
83139
84140 /**
85141 * NOTE: there are various conditions that determine whether the drag handle
@@ -130,5 +186,6 @@ export function useStepMetadata(
130186 isDeletable,
131187 isMrfStep,
132188 isApprovalStep,
189+ approvalBranch,
133190 }
134191}
0 commit comments