@@ -20,32 +20,32 @@ import { Text, useToken } from "@chakra-ui/react";
2020import { Group } from "@visx/group" ;
2121import { LinePath } from "@visx/shape" ;
2222import type { Edge as EdgeType } from "@xyflow/react" ;
23+ import { useNodesData } from "@xyflow/react" ;
2324import type { ElkPoint } from "elkjs" ;
2425
2526import type { EdgeData } from "./reactflowUtils" ;
2627
2728type Props = EdgeType < EdgeData > ;
2829
29- const CustomEdge = ( { data } : Props ) => {
30+ const CustomEdge = ( { data, source , target } : Props ) => {
3031 const [ strokeColor , blueColor , dataEdgeColor ] = useToken ( "colors" , [
3132 "border.inverted" ,
3233 "blue.500" ,
3334 "purple.500" ,
3435 ] ) ;
3536
37+ // Read isSelected directly from the node store so that selection changes
38+ // don't require the parent to rebuild and pass down a new edges array.
39+ // useNodesData subscribes to data changes for these specific node IDs only.
40+ const nodesData = useNodesData ( [ source , target ] ) ;
41+ const isSelected = nodesData . some ( ( node ) => Boolean ( node . data . isSelected ) ) ;
42+
3643 if ( data === undefined ) {
3744 return undefined ;
3845 }
3946 const { rest } = data ;
4047
41- // Determine edge color based on type
42- const getEdgeColor = ( ) => {
43- if ( rest . isSelected ) {
44- return rest . edgeType === "data" ? dataEdgeColor : blueColor ;
45- }
46-
47- return strokeColor ;
48- } ;
48+ const edgeStrokeColor = isSelected ? ( rest . edgeType === "data" ? dataEdgeColor : blueColor ) : strokeColor ;
4949
5050 return (
5151 < >
@@ -73,9 +73,9 @@ const CustomEdge = ({ data }: Props) => {
7373 < LinePath
7474 data = { [ section . startPoint , ...( section . bendPoints ?? [ ] ) , section . endPoint ] }
7575 key = { section . id }
76- stroke = { getEdgeColor ( ) }
76+ stroke = { edgeStrokeColor }
7777 strokeDasharray = { rest . isSetupTeardown ? "10,5" : undefined }
78- strokeWidth = { rest . isSelected ? 3 : 1 }
78+ strokeWidth = { isSelected ? 3 : 1 }
7979 x = { ( point : ElkPoint ) => point . x }
8080 y = { ( point : ElkPoint ) => point . y }
8181 />
0 commit comments