Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions src/main/frontend/app/routes/studio/canvas/flow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
type OnConnectEnd,
ReactFlow,
ReactFlowProvider,
useNodesInitialized,
useReactFlow,
useUpdateNodeInternals,
} from '@xyflow/react'
Expand Down Expand Up @@ -221,8 +222,10 @@
const reactFlow = useReactFlow()
const reactFlowRef = useRef(reactFlow)
reactFlowRef.current = reactFlow
const nodesInitialized = useNodesInitialized()
const canvasRef = useRef<HTMLDivElement>(null)
const fitAfterLayoutRef = useRef<{ id: string }[] | null>(null)
const pendingInitialRelayoutRef = useRef<{ pendingSelection: { subtype: string; name: string } | null } | null>(null)

const applySelectionToNodes = useCallback((pendingSelection: { subtype: string; name: string }) => {
const currentNodes = useFlowStore.getState().nodes
Expand Down Expand Up @@ -318,7 +321,7 @@
showErrorToast(`Failed to save XML: ${error instanceof Error ? error.message : error}`)
setIdle()
}
}, [project])

Check warning on line 324 in src/main/frontend/app/routes/studio/canvas/flow.tsx

View workflow job for this annotation

GitHub Actions / Build & Run All Tests

React Hook useCallback has missing dependencies: 'setIdle', 'setSaved', and 'setSaving'. Either include them or remove the dependency array

const autosaveEnabled = useSettingsStore((s) => s.general.autoSave.enabled)
const autosaveDelay = useSettingsStore((s) => s.general.autoSave.delayMs)
Expand Down Expand Up @@ -574,6 +577,36 @@
flowStore.setNodes(laidOut)
}, [layoutGraph])

useEffect(() => {
if (!nodesInitialized || !pendingInitialRelayoutRef.current) return

const { pendingSelection } = pendingInitialRelayoutRef.current
pendingInitialRelayoutRef.current = null

const flowStore = useFlowStore.getState()
const nodesWithResetPositions = flowStore.nodes.map((node) =>
node.type === 'frankNode' || node.type === 'exitNode' ? { ...node, position: { x: 0, y: 0 } } : node,
)
const laidOutNodes = layoutGraph(nodesWithResetPositions, flowStore.edges, 'LR')
flowStore.setNodes(laidOutNodes)

if (pendingSelection) {
applySelectionToNodes(pendingSelection)
} else {
waitForStableCanvasDimensions((canvasWidth, canvasHeight) => {
const freshViewport = computeAdapterCenteredViewport(laidOutNodes, canvasWidth, canvasHeight)
useFlowStore.getState().setViewport(freshViewport)
reactFlowRef.current?.setViewport(freshViewport)
})
}
}, [
nodesInitialized,
layoutGraph,
waitForStableCanvasDimensions,
computeAdapterCenteredViewport,
applySelectionToNodes,
])

const getFullySelectedGroupIds = useCallback(
(parentIds: (string | undefined)[], selectedNodes: FlowNode[]) => {
return parentIds.filter((parentId) => {
Expand Down Expand Up @@ -1285,20 +1318,10 @@
const adapterJson = await convertAdapterXmlToJson(adapter)

flowStore.setEdges(adapterJson.edges)
const laidOutNodes = layoutGraph(adapterJson.nodes, adapterJson.edges, 'LR')
flowStore.setNodes(laidOutNodes)
flowStore.setNodes(adapterJson.nodes)
flowStore.setHistory([])
flowStore.setFuture([])

if (pendingSelection) {
applySelectionToNodes(pendingSelection)
} else {
waitForStableCanvasDimensions((canvasWidth, canvasHeight) => {
const freshViewport = computeAdapterCenteredViewport(laidOutNodes, canvasWidth, canvasHeight)
useFlowStore.getState().setViewport(freshViewport)
reactFlowRef.current?.setViewport(freshViewport)
})
}
pendingInitialRelayoutRef.current = { pendingSelection }
}

async function loadFlowFromTab(tab: TabData) {
Expand Down
Loading