@@ -15,6 +15,7 @@ import dynamic from 'next/dynamic';
1515import { Position } from "./graphView" ;
1616import { prepareArg } from '../utils' ;
1717import { NodeObject , ForceGraphMethods } from "react-force-graph-2d" ;
18+ import { handleZoomToFit } from "@/lib/utils" ;
1819
1920const GraphView = dynamic ( ( ) => import ( './graphView' ) ) ;
2021
@@ -68,6 +69,7 @@ export function CodeGraph({
6869 const [ cooldownTicks , setCooldownTicks ] = useState < number | undefined > ( 0 )
6970 const [ cooldownTime , setCooldownTime ] = useState < number > ( 0 )
7071 const containerRef = useRef < HTMLDivElement > ( null ) ;
72+ const [ zoomedNodes , setZoomedNodes ] = useState < Node [ ] > ( [ ] ) ;
7173
7274 useEffect ( ( ) => {
7375 setData ( { ...graph . Elements } )
@@ -167,17 +169,17 @@ export function CodeGraph({
167169 }
168170
169171 const deleteNeighbors = ( nodes : Node [ ] ) => {
170-
172+
171173 if ( nodes . length === 0 ) return ;
172-
174+
173175 const expandedNodes : Node [ ] = [ ]
174-
176+
175177 graph . Elements = {
176178 nodes : graph . Elements . nodes . filter ( node => {
177179 if ( ! node . collapsed ) return true
178-
180+
179181 const isTarget = graph . Elements . links . some ( link => link . target . id === node . id && nodes . some ( n => n . id === link . source . id ) ) ;
180-
182+
181183 if ( ! isTarget ) return true
182184
183185 const deleted = graph . NodesMap . delete ( Number ( node . id ) )
@@ -190,7 +192,7 @@ export function CodeGraph({
190192 } ) ,
191193 links : graph . Elements . links
192194 }
193-
195+
194196 deleteNeighbors ( expandedNodes )
195197
196198 graph . removeLinks ( )
@@ -239,12 +241,12 @@ export function CodeGraph({
239241 }
240242 graph . visibleLinks ( true , [ chartNode ! . id ] )
241243 setData ( { ...graph . Elements } )
244+ setZoomedNodes ( [ node ] )
245+ return
242246 }
243-
247+
248+ handleZoomToFit ( chartRef , 4 , ( n : NodeObject < Node > ) => n . id === node . id )
244249 setSearchNode ( chartNode )
245- setTimeout ( ( ) => {
246- chart . zoomToFit ( 1000 , 150 , ( n : NodeObject < Node > ) => n . id === chartNode ! . id ) ;
247- } , 0 )
248250 }
249251 }
250252
@@ -307,7 +309,9 @@ export function CodeGraph({
307309 graph = { graph }
308310 onValueChange = { ( node ) => setSearchNode ( node ) }
309311 icon = { < Search /> }
310- handleSubmit = { handleSearchSubmit }
312+ handleSubmit = { ( node ) => {
313+ handleSearchSubmit ( node )
314+ } }
311315 node = { searchNode }
312316 />
313317 < Labels categories = { graph . Categories } onClick = { onCategoryClick } />
@@ -421,6 +425,8 @@ export function CodeGraph({
421425 setCooldownTicks = { setCooldownTicks }
422426 cooldownTime = { cooldownTime }
423427 setCooldownTime = { setCooldownTime }
428+ setZoomedNodes = { setZoomedNodes }
429+ zoomedNodes = { zoomedNodes }
424430 />
425431 </ div >
426432 : < div className = "flex flex-col items-center justify-center h-full text-gray-400" >
0 commit comments