1- import { IFlow , IStep } from '@plumber/types'
1+ import { IStep } from '@plumber/types'
22
33import { useContext } from 'react'
44
55import { BranchContext } from '@/components/FlowStepGroup/Content/IfThen/BranchContext'
66import { NESTED_IFTHEN_FEATURE_FLAG } from '@/config/flags'
7- import { EditorContext } from '@/contexts/Editor'
87import { LaunchDarklyContext } from '@/contexts/LaunchDarkly'
8+ import { StepsToDisplayContext } from '@/contexts/StepsToDisplay'
99import { TOOLBOX_ACTIONS } from '@/helpers/toolbox'
1010
1111/**
@@ -16,68 +16,26 @@ import { TOOLBOX_ACTIONS } from '@/helpers/toolbox'
1616 *
1717 */
1818function isDelaySelectable ( {
19- flow,
19+ hasForEach,
20+ groupedSteps,
2021 step,
2122 prevStepId,
2223} : {
23- flow : IFlow
24+ hasForEach : boolean
25+ groupedSteps : IStep [ ] [ ]
2426 step ?: IStep
2527 prevStepId ?: string
2628} ) {
27- const forEachStepPosition = flow ?. steps . find (
28- ( s ) => s . key === TOOLBOX_ACTIONS . ForEach ,
29- ) ?. position
30-
31- let prevStepPosition = null
32- if ( step ) {
33- prevStepPosition = step . position
34- } else if ( prevStepId ) {
35- prevStepPosition = flow ?. steps ?. find ( ( s ) => s . id === prevStepId ) ?. position
36- }
37-
38- let isDelaySelectable : boolean
39- if ( ! forEachStepPosition ) {
40- isDelaySelectable = true
41- } else if ( forEachStepPosition && prevStepPosition ) {
42- isDelaySelectable = prevStepPosition < forEachStepPosition
43- } else {
44- isDelaySelectable = false
29+ if ( ! hasForEach ) {
30+ return true
4531 }
46-
47- return isDelaySelectable
48- }
49-
50- /**
51- * Helper function to check if For-each action should be selectable; supports edge
52- * case in ChooseEvent component.
53- *
54- * For-each should only be selectable if:
55- * - We're the last step.
56- * - We are not inside a for-each action.
57- * - We are not inside an if-then action.
58- * - There is no if-then action in the flow,
59- *
60- * Using many consts as purpose of the conditions may not be immediately
61- * apparent.
62- */
63- function isForEachSelectable ( {
64- flow,
65- hasIfThen,
66- isLastStep,
67- } : {
68- flow : IFlow
69- hasIfThen : boolean
70- isLastStep : boolean
71- } ) {
72- const hasForEach = flow ?. steps ?. some (
73- ( step : IStep ) => step . key === TOOLBOX_ACTIONS . ForEach ,
74- )
75-
76- if ( hasForEach || hasIfThen || ! isLastStep ) {
32+ const isStepWithinForEach = groupedSteps . flat ( ) . some ( ( groupedStep ) => {
33+ if ( step ?. id === groupedStep . id || prevStepId === groupedStep . id ) {
34+ return true
35+ }
7736 return false
78- }
79-
80- return true
37+ } )
38+ return ! isStepWithinForEach
8139}
8240
8341/**
@@ -116,6 +74,7 @@ function isIfThenSelectable({
11674 }
11775
11876 const isNestedBranch = depth > 0
77+
11978 return ! isNestedBranch
12079}
12180
@@ -129,21 +88,35 @@ export const useIsAppSelectable = ({
12988 prevStepId ?: string
13089} ) : Record < string , boolean > => {
13190 const { depth } = useContext ( BranchContext )
132- const { flow , hasIfThen } = useContext ( EditorContext )
91+ const { groupedSteps } = useContext ( StepsToDisplayContext )
13392 const { getFlagValue } = useContext ( LaunchDarklyContext )
13493
94+ const hasIfThen = groupedSteps . some ( ( group ) =>
95+ group . some ( ( step ) => step . key === TOOLBOX_ACTIONS . IfThen ) ,
96+ )
97+
98+ const hasForEach = groupedSteps . some ( ( group ) =>
99+ group . some ( ( step ) => step . key === TOOLBOX_ACTIONS . ForEach ) ,
100+ )
101+ console . log ( {
102+ step,
103+ prevStepId,
104+ } )
105+
135106 return {
136107 [ TOOLBOX_ACTIONS . IfThen ] : isIfThenSelectable ( {
137108 depth,
138109 hasIfThen,
139110 isLastStep,
140111 getFlagValue,
141112 } ) ,
142- [ TOOLBOX_ACTIONS . ForEach ] : isForEachSelectable ( {
143- flow,
144- hasIfThen,
145- isLastStep,
146- } ) ,
147- delay : isDelaySelectable ( { flow, step, prevStepId } ) ,
113+ /**
114+ * For-each should only be selectable if:
115+ * - We're the last step.
116+ * - There's no other for-each action
117+ * - There's no other if-then action
118+ */
119+ [ TOOLBOX_ACTIONS . ForEach ] : isLastStep && ! hasIfThen && ! hasForEach ,
120+ delay : isDelaySelectable ( { hasForEach, groupedSteps, step, prevStepId } ) ,
148121 }
149122}
0 commit comments