@@ -1275,39 +1275,51 @@ export function InteractiveGraphVisualization({ onResetLayout }: InteractiveGrap
12751275 }
12761276 } ;
12771277
1278- // Selective data update function - updates simulation data without DOM rebuilds
1278+ // Smart data update function - only reinitializes when necessary, preserves camera position
12791279 const updateVisualizationData = useCallback ( ( ) => {
12801280 if ( ! simulationRef . current || ! svgRef . current ) return ;
12811281
1282- console . log ( '[Graph Debug] Performing selective data update ...' ) ;
1282+ console . log ( '[Graph Debug] Checking for data changes ...' ) ;
12831283
1284- // For relationship creation/deletion, we need full reinitialization to properly handle
1285- // edge labels and complex DOM synchronization
1286- const currentEdgeCount = d3 . select ( svgRef . current ) . select ( '.edges-group' ) . selectAll ( '.edge' ) . size ( ) ;
1284+ const svg = d3 . select ( svgRef . current ) ;
1285+ const simulation = simulationRef . current ;
1286+
1287+ // Get current data counts from DOM
1288+ const currentNodeCount = svg . select ( '.nodes-group' ) . selectAll ( '.node' ) . size ( ) ;
1289+ const currentEdgeCount = svg . select ( '.edges-group' ) . selectAll ( '.edge' ) . size ( ) ;
1290+ const newNodeCount = nodes . length ;
12871291 const newEdgeCount = validatedEdges . length ;
12881292
1289- if ( currentEdgeCount !== newEdgeCount ) {
1290- console . log ( '[Graph Debug] Edge count changed, forcing full reinit for proper label sync' ) ;
1293+ console . log ( '[Graph Debug] Data counts:' , {
1294+ currentNodes : currentNodeCount ,
1295+ newNodes : newNodeCount ,
1296+ currentEdges : currentEdgeCount ,
1297+ newEdges : newEdgeCount
1298+ } ) ;
1299+
1300+ // Check if we need full reinitialization (node/edge count changed)
1301+ const needsReinit = ( currentNodeCount !== newNodeCount ) || ( currentEdgeCount !== newEdgeCount ) ;
1302+
1303+ if ( needsReinit ) {
1304+ console . log ( '[Graph Debug] Data structure changed - triggering reinitialization with preserved camera' ) ;
12911305 setReinitTrigger ( prev => prev + 1 ) ;
12921306 return ;
12931307 }
1308+
1309+ // If counts are the same, just update simulation data (for property changes)
1310+ console . log ( '[Graph Debug] Data counts unchanged - updating simulation data only' ) ;
12941311
1295- // Update simulation with new data
1296- const simulation = simulationRef . current ;
1297-
1298- // Update nodes data
1312+ // Update simulation with current data (handles property changes)
12991313 simulation . nodes ( nodes as any ) ;
1300-
1301- // Update links data
13021314 const linkForce = simulation . force ( 'link' ) as d3 . ForceLink < any , any > ;
13031315 if ( linkForce ) {
13041316 linkForce . links ( validatedEdges ) ;
13051317 }
1306-
1307- // Restart simulation with low alpha to settle new positions
1318+
1319+ // Gentle restart to settle any property changes
13081320 simulation . alpha ( 0.1 ) . restart ( ) ;
13091321
1310- console . log ( '[Graph Debug] Selective update completed ' ) ;
1322+ console . log ( '[Graph Debug] Simulation data updated ' ) ;
13111323 } , [ nodes , validatedEdges ] ) ;
13121324
13131325 // Define initializeVisualization function with access to nodes data
0 commit comments