Skip to content

Commit

Permalink
feat(editor): clean editor when new flow is created
Browse files Browse the repository at this point in the history
  • Loading branch information
anteqkois committed May 23, 2024
1 parent b544686 commit 631fc73
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
5 changes: 4 additions & 1 deletion apps/web/app/app/flows/editor/[[...id]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,17 @@ const renderFlow = (flowVersion: FlowVersion, connectorsMetadata: ConnectorMetad

export default function Page({ params }: { params: { id: string } }) {
const { data: connectorsMetadata, status } = useClientQuery(connectorsMetadataQueryConfig.getSummaryMany())
const { loadFlow, setFlow, setEdges, setNodes } = useEditor()
const { loadFlow, setFlow, setEdges, setNodes, clearEditorState } = useEditor()
const { toast } = useToast()
const { showDialogBasedOnErrorCode } = useReachLimitDialog()
const { currentPlan, subscriptionsStatus, subscriptionsError } = useSubscriptions()
const { push } = useRouter()

const initEditor = useCallback(async () => {
if (!connectorsMetadata?.length) throw new Error('Can not retrive connectors metadata')
clearEditorState()
// Clear editor state, becouse wehn user click to create new flow when they edit other flow, there are still old data

const id = params?.id?.[0]

// Fetch and render if exists
Expand Down
4 changes: 2 additions & 2 deletions apps/web/modules/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const Editor = ({ mode, limits, useLocalStorage = false }: EditorProps) =
setShowRightDrawer,
rightDrawer,
leftDrawer,
loaded,
flowLoaded,
showLeftDrawer,
setShowLeftDrawer,
setLimits,
Expand All @@ -75,7 +75,7 @@ export const Editor = ({ mode, limits, useLocalStorage = false }: EditorProps) =

return (
<ReactFlowProvider>
{loaded ? <EditorFlowMenu /> : null}
{flowLoaded ? <EditorFlowMenu /> : null}
<div style={{ width: '100vw', height: '100vh' }} ref={reactFlowWrapper}>
<ReactFlow
nodes={nodes}
Expand Down
17 changes: 17 additions & 0 deletions apps/web/modules/editor/store/editorSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,21 @@ export const createEditorSlice: CreateSlice<EditorSlice> = (set, get) => ({
if (newDrawer?.name === get().leftDrawer.name) return
set(() => ({ leftDrawer: newDrawer }))
},
clearEditorState: () => {
const { flowLoaded } = get()

// this mean that flow is first time loaded, so there is no need to clear
if (!flowLoaded) return

set({
showLeftDrawer: false,
leftDrawer: editorDrawers[0],
showRightDrawer: false,
rightDrawer: editorDrawers[0],
editedTrigger: null,
editStepMetadata: null,
editedAction: null,
editedConnectorMetadata: null,
})
},
})
4 changes: 2 additions & 2 deletions apps/web/modules/editor/store/flowAndConnectorsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const emptyFlow: FlowPopulated = {

export const createFlowAndConnectorsSlice: CreateSlice<FlowAndConnectorsSlice> = (set, get) => ({
// FLOW
loaded: false,
flowLoaded: false,
flowOperationRunning: null,
flow: emptyFlow,
loadFlow: async (id: Id) => {
Expand All @@ -68,7 +68,7 @@ export const createFlowAndConnectorsSlice: CreateSlice<FlowAndConnectorsSlice> =

assertNotNullOrUndefined(flow, 'flow')

set({ flow, loaded: true })
set({ flow, flowLoaded: true })
return flow
},
setFlow: (flow: FlowPopulated) => {
Expand Down
3 changes: 2 additions & 1 deletion apps/web/modules/editor/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface EditorSlice {
setLeftDrawer: (name: EditorDrawer['name']) => void
showLeftDrawer: boolean
setShowLeftDrawer: Dispatch<SetStateAction<boolean>>
clearEditorState: () => void
}

export enum FlowOperationRunnType {
Expand All @@ -68,7 +69,7 @@ export enum FlowOperationRunnType {

export interface FlowAndConnectorsSlice {
// FLOW
loaded: boolean
flowLoaded: boolean
flowOperationRunning: null | FlowOperationRunnType
flow: FlowPopulated
loadFlow: (id: Id) => Promise<FlowPopulated | null>
Expand Down

0 comments on commit 631fc73

Please sign in to comment.