1- import type { IApp , IStep } from '@plumber/types'
2-
31import { useContext , useMemo } from 'react'
4- import { Center , Flex } from '@chakra-ui/react'
2+ import { Flex } from '@chakra-ui/react'
53
64import EditorRightDrawer from '@/components/EditorRightDrawer'
7- import FlowStepGroup from '@/components/FlowStepGroup'
8- import { SortableList } from '@/components/SortableList'
95import { EditorContext } from '@/contexts/Editor'
106import { MrfContextProvider } from '@/contexts/MrfContext'
117import { StepExecutionsToIncludeProvider } from '@/contexts/StepExecutionsToInclude'
12- import { StepEnumType } from '@/graphql/__generated__/graphql'
13- import { extractBranchesWithSteps , TOOLBOX_ACTIONS } from '@/helpers/toolbox'
14- import useReorderSteps from '@/hooks/useReorderSteps'
15-
16- import PrimarySpinner from '../PrimarySpinner'
178
9+ import { StepsList } from './components/StepsList'
1810import { EDITOR_RIGHT_DRAWER_WIDTH } from './constants'
19- import FlowStepWithAddButton from './FlowStepWithAddButton'
2011import { editorStyles } from './styles'
2112
2213type EditorProps = {
@@ -25,108 +16,13 @@ type EditorProps = {
2516
2617export default function Editor ( props : EditorProps ) : React . ReactElement {
2718 const { isNested } = props
28-
29- const { allApps, isDrawerOpen, isMobile, flow, readOnly, currentStepId } =
30- useContext ( EditorContext )
31-
32- const { handleReorderUpdate } = useReorderSteps ( flow . id )
33- const steps = flow . steps
34-
19+ const { currentStepId, flow } = useContext ( EditorContext )
3520 const currentStep = useMemo ( ( ) => {
36- return steps . find ( ( step ) => step . id === currentStepId )
37- } , [ currentStepId , steps ] )
38-
39- const appsWithActions : IApp [ ] = allApps . filter (
40- ( app : IApp ) => ! ! app . actions ?. length ,
41- )
42-
43- const groupingActions = useMemo ( ( ) => {
44- if ( ! appsWithActions ) {
45- return null
46- }
47-
48- return new Set (
49- appsWithActions ?. flatMap ( ( app ) =>
50- app . actions
51- ?. filter ( ( action ) => action . groupsLaterSteps )
52- ?. map ( ( action ) => `${ app . key } -${ action . key } ` ) ,
53- ) ?? [ ] ,
54- )
55- } , [ appsWithActions ] )
56-
57- const [ triggerStep , actionStepsBeforeGroup , groupedSteps ] = useMemo ( ( ) => {
58- if ( ! groupingActions ) {
59- return [ null , [ ] , [ ] ]
60- }
61-
62- const groupStepIdx = steps . findIndex ( ( step , index ) => {
63- if (
64- // We ignore the 1st step because it's either a trigger, or a
65- // step-grouping action that is using a nested Editor to edit steps in
66- // its group.
67- index === 0 ||
68- ! step . appKey ||
69- ! step . key
70- ) {
71- return false
72- }
73- return groupingActions . has ( `${ step . appKey } -${ step . key } ` )
74- } )
75-
76- let branchesWithSteps : IStep [ ] [ ] = [ ]
77- if ( groupStepIdx !== - 1 ) {
78- branchesWithSteps = extractBranchesWithSteps ( steps . slice ( groupStepIdx ) , 0 )
79- }
21+ return flow . steps . find ( ( step ) => step . id === currentStepId )
22+ } , [ currentStepId , flow ] )
8023
81- const triggerStep = steps [ 0 ]
24+ const { isDrawerOpen , isMobile } = useContext ( EditorContext )
8225
83- return groupStepIdx === - 1
84- ? [ triggerStep , steps . slice ( 1 ) , [ ] ]
85- : [ triggerStep , steps . slice ( 1 , groupStepIdx ) , branchesWithSteps ]
86- } , [ groupingActions , steps ] )
87-
88- const handleReorderSteps = async ( reorderedSteps : IStep [ ] ) => {
89- const stepPositions = reorderedSteps . map ( ( step , index ) => ( {
90- id : step . id ,
91- position : index + 2 , // trigger position is 1
92- type : step . type as StepEnumType ,
93- } ) )
94-
95- try {
96- await handleReorderUpdate ( stepPositions )
97- } catch ( error ) {
98- console . error (
99- 'Error updating step positions: ' ,
100- error ,
101- JSON . stringify ( stepPositions ) ,
102- )
103- }
104- }
105-
106- const nonIfThenActionSteps = actionStepsBeforeGroup . filter (
107- ( step ) => step . key !== TOOLBOX_ACTIONS . IfThen ,
108- )
109-
110- // Disables last add step and hide in-between add step buttons
111- const hasExactlyOneEmptyActionStep =
112- nonIfThenActionSteps . length === 1 && ! nonIfThenActionSteps [ 0 ] . appKey
113-
114- // Disables last add step button but show empty action instead
115- const hasNoActionSteps = nonIfThenActionSteps . length === 0
116- const shouldShowEmptyAction = hasNoActionSteps && ! groupedSteps . length
117- // for backwards compatibility where empty step is created
118- const shouldDisableAddButton =
119- ( hasExactlyOneEmptyActionStep || hasNoActionSteps ) && ! groupedSteps . length
120-
121- if ( ! appsWithActions || ! groupingActions ) {
122- return (
123- < Center height = "100vh" position = "fixed" width = "full" top = { 0 } left = { 0 } >
124- < PrimarySpinner fontSize = "4xl" />
125- </ Center >
126- )
127- }
128-
129- const leftStepPadding = isDrawerOpen ? ( isMobile ? 0 : '5rem' ) : 0
13026 const rightDrawerTransform = isDrawerOpen
13127 ? 'translateX(0)'
13228 : 'translateX(100%)'
@@ -144,75 +40,9 @@ export default function Editor(props: EditorProps): React.ReactElement {
14440 backgroundSize : '30px 30px' ,
14541 } }
14642 >
147- < MrfContextProvider steps = { steps } >
148- < StepExecutionsToIncludeProvider
149- groupedSteps = { groupedSteps }
150- triggerStep = { triggerStep }
151- actionStepsBeforeGroup = { actionStepsBeforeGroup }
152- >
153- < Flex
154- { ...editorStyles . stepHeaderContainer }
155- flex = { isDrawerOpen ? ( isMobile ? 0 : 1 ) : undefined }
156- px = { leftStepPadding }
157- maxWidth = { `calc(100% - ${
158- isDrawerOpen ? EDITOR_RIGHT_DRAWER_WIDTH : '0px'
159- } )`}
160- >
161- { triggerStep && (
162- < FlowStepWithAddButton
163- step = { triggerStep }
164- isLastStep = {
165- actionStepsBeforeGroup . length === 0 &&
166- groupedSteps . length === 0
167- }
168- isNested = { isNested }
169- stepsBeforeGroup = { [ ] } // no reason to pass in for this
170- groupedSteps = { groupedSteps }
171- addButtonProps = { {
172- isHidden : readOnly ,
173- isDisabled : shouldDisableAddButton ,
174- showEmptyAction : shouldShowEmptyAction ,
175- } }
176- />
177- ) }
178-
179- < SortableList
180- items = { actionStepsBeforeGroup }
181- onChange = { handleReorderSteps }
182- renderItem = { ( step , isOverlay ) => {
183- const { id, position } = step
184- return (
185- < SortableList . Item id = { id } >
186- < Flex
187- key = { `${ id } -${ position } ` }
188- width = { isDrawerOpen || isMobile ? '100%' : 'auto' }
189- flexDir = "column"
190- position = "relative"
191- >
192- < FlowStepWithAddButton
193- step = { step }
194- isLastStep = { position === steps . length }
195- isNested = { isNested }
196- stepsBeforeGroup = { actionStepsBeforeGroup }
197- groupedSteps = { groupedSteps }
198- addButtonProps = { {
199- isHidden : readOnly || ! ! isOverlay ,
200- isDisabled : shouldDisableAddButton ,
201- showEmptyAction : shouldShowEmptyAction ,
202- } }
203- />
204- </ Flex >
205- </ SortableList . Item >
206- )
207- } }
208- />
209- { groupedSteps . length > 0 && (
210- < FlowStepGroup
211- stepsBeforeGroup = { actionStepsBeforeGroup }
212- groupedSteps = { groupedSteps }
213- />
214- ) }
215- </ Flex >
43+ < MrfContextProvider >
44+ < StepExecutionsToIncludeProvider >
45+ < StepsList isNested = { isNested } />
21646 { /** HACKFIX (kevinkim-ogp): to ensure that the transitions are smooth */ }
21747 < Flex
21848 { ...editorStyles . dummyRightContainer }
0 commit comments