Skip to content

Image Pipeline Playground

feat(frontend/canvas): fit view and select first node after load

Mohammad Rajaeic0c91876a77a
diff --git a/frontend/src/features/canvas/PipelineCanvas.tsx b/frontend/src/features/canvas/PipelineCanvas.tsx
index 7877396..d821ec6 100644
--- a/frontend/src/features/canvas/PipelineCanvas.tsx
+++ b/frontend/src/features/canvas/PipelineCanvas.tsx
@@ -1,10 +1,11 @@
-import { useCallback, useMemo } from 'react'
+import { useCallback, useEffect, useMemo } from 'react'
import {
Background,
Controls,
MiniMap,
ReactFlow,
reconnectEdge,
+ useReactFlow,
type Connection,
type Edge,
type EdgeTypes,
@@ -17,6 +18,22 @@ import { PipelineNodeView } from './PipelineNode'
import { EditableEdge } from './EditableEdge'
import type { NodeMetadata } from '../../types'
+function FitViewOnGraphChange() {
+ const { fitView } = useReactFlow()
+ const graphRevision = useGraphStore((s) => s.graphRevision)
+ const nodeCount = useGraphStore((s) => s.nodes.length)
+
+ useEffect(() => {
+ if (graphRevision === 0 || nodeCount === 0) return
+ const timer = window.setTimeout(() => {
+ void fitView({ padding: 0.2, duration: 220 })
+ }, 30)
+ return () => window.clearTimeout(timer)
+ }, [fitView, graphRevision, nodeCount])
+
+ return null
+}
+
export function PipelineCanvas() {
const nodes = useGraphStore((s) => s.nodes)
const edges = useGraphStore((s) => s.edges)
@@ -121,6 +138,7 @@ export function PipelineCanvas() {
data: { waypoints: [] },
}}
>
+ <FitViewOnGraphChange />
<MiniMap
pannable
zoomable
diff --git a/frontend/src/store/graphStore.ts b/frontend/src/store/graphStore.ts
index 5ff1984..be2595b 100644
--- a/frontend/src/store/graphStore.ts
+++ b/frontend/src/store/graphStore.ts
@@ -39,6 +39,8 @@ interface GraphState {
isExecuting: boolean
seed: number
sampleCount: number
+ /** Bumped on loadWorkflow so the canvas can refit the viewport. */
+ graphRevision: number
setNodeCatalog: (catalog: NodeMetadata[]) => void
onNodesChange: (changes: NodeChange<PipelineNode>[]) => void
onEdgesChange: (changes: EdgeChange[]) => void
@@ -127,6 +129,7 @@ export const useGraphStore = create<GraphState>((set, get) => ({
isExecuting: false,
seed: 0,
sampleCount: 1,
+ graphRevision: 0,
setNodeCatalog: (catalog) => set({ nodeCatalog: catalog }),
@@ -519,10 +522,16 @@ export const useGraphStore = create<GraphState>((set, get) => ({
syncNodeCounter(nodes.map((node) => node.id))
+ const preferred =
+ nodes.find((node) => node.data.type === 'load_image')?.id ?? nodes[0]?.id ?? null
+
set({
- nodes,
+ nodes: nodes.map((node) => ({
+ ...node,
+ selected: node.id === preferred,
+ })),
edges,
- selectedNodeId: null,
+ selectedNodeId: preferred,
activeNodeId: null,
previews: [],
nodeImages: {},
@@ -531,6 +540,7 @@ export const useGraphStore = create<GraphState>((set, get) => ({
generatedCode: '# Run codegen to export a Python script\n',
seed: doc.seed,
sampleCount: Math.max(1, doc.sampleCount),
+ graphRevision: get().graphRevision + 1,
})
return { skippedTypes: [...skipped].sort() }