11import { useCallback } from 'react'
2- import { useReactFlow } from 'reactflow'
32import { createLogger } from '@/lib/logs/console/logger'
43import type { AutoLayoutOptions } from '@/app/workspace/[workspaceId]/w/[workflowId]/utils/auto-layout-utils'
54import { applyAutoLayoutAndUpdateStore as applyAutoLayoutStandalone } from '@/app/workspace/[workspaceId]/w/[workflowId]/utils/auto-layout-utils'
@@ -8,36 +7,45 @@ export type { AutoLayoutOptions }
87
98const logger = createLogger ( 'useAutoLayout' )
109
10+ interface UseAutoLayoutOptions {
11+ fitView ?: ( options ?: { padding ?: number ; duration ?: number } ) => void
12+ }
13+
1114/**
12- * Hook providing auto-layout functionality for workflows
13- * Binds workflowId context and provides memoized callback for React components
14- * Includes automatic fitView animation after successful layout
15+ * Hook providing auto-layout functionality for workflows.
16+ * Binds workflowId context and provides memoized callback for React components.
17+ * Optionally accepts a fitView function to animate after successful layout.
18+ *
19+ * @param workflowId - The workflow ID to apply layout to
20+ * @param options - Optional configuration including fitView function from useReactFlow
1521 */
16- export function useAutoLayout ( workflowId : string | null ) {
17- const { fitView } = useReactFlow ( )
22+ export function useAutoLayout ( workflowId : string | null , options : UseAutoLayoutOptions = { } ) {
23+ const { fitView } = options
1824
1925 const applyAutoLayoutAndUpdateStore = useCallback (
20- async ( options : AutoLayoutOptions = { } ) => {
26+ async ( layoutOptions : AutoLayoutOptions = { } ) => {
2127 if ( ! workflowId ) {
2228 return { success : false , error : 'No workflow ID provided' }
2329 }
24- return applyAutoLayoutStandalone ( workflowId , options )
30+ return applyAutoLayoutStandalone ( workflowId , layoutOptions )
2531 } ,
2632 [ workflowId ]
2733 )
2834
2935 /**
30- * Applies auto-layout and animates to fit all blocks in view
36+ * Applies auto-layout and optionally animates to fit all blocks in view
3137 */
3238 const handleAutoLayout = useCallback ( async ( ) => {
3339 try {
3440 const result = await applyAutoLayoutAndUpdateStore ( )
3541
3642 if ( result . success ) {
3743 logger . info ( 'Auto layout completed successfully' )
38- requestAnimationFrame ( ( ) => {
39- fitView ( { padding : 0.8 , duration : 600 } )
40- } )
44+ if ( fitView ) {
45+ requestAnimationFrame ( ( ) => {
46+ fitView ( { padding : 0.8 , duration : 600 } )
47+ } )
48+ }
4149 } else {
4250 logger . error ( 'Auto layout failed:' , result . error )
4351 }
0 commit comments