@@ -15,51 +15,33 @@ const config = generatePrettyBlocks({ layersCount: 10, connectionsPerLayer: 10,
1515const GraphApp = ( ) => {
1616 const [ transition , setTransition ] = useState ( "1000" ) ;
1717 const [ padding , setPadding ] = useState ( "50" ) ;
18- const [ selectedCount , setSelectedCount ] = useState ( 0 ) ;
18+ const [ selectedComponents , setSelectedComponents ] = useState < GraphComponent [ ] > ( [ ] ) ;
1919
2020 const graphRef = useRef < Graph | undefined > ( undefined ) ;
2121
2222 useEffect ( ( ) => {
2323 if ( ! graphRef . current ) return undefined ;
24- const selection = graphRef . current . selectionService . $selection . value ;
25- const count = Array . from ( selection . keys ( ) ) . reduce ( ( acc , key ) => {
26- return acc + ( selection . get ( key ) ?. size ?? 0 ) ;
27- } , 0 ) ;
28- setSelectedCount ( count ) ;
29- const unsubscribe = graphRef . current . selectionService . $selection . subscribe ( ( ) => {
30- const selection = graphRef . current . selectionService . $selection . value ;
31- const count = Array . from ( selection . keys ( ) ) . reduce ( ( acc , key ) => {
32- return acc + ( selection . get ( key ) ?. size ?? 0 ) ;
33- } , 0 ) ;
34- setSelectedCount ( count ) ;
24+
25+ // Get initial value
26+ setSelectedComponents ( graphRef . current . selectionService . $selectedComponents . value ) ;
27+
28+ // Subscribe to changes
29+ const unsubscribe = graphRef . current . selectionService . $selectedComponents . subscribe ( ( components ) => {
30+ setSelectedComponents ( components ) ;
3531 } ) ;
32+
3633 return ( ) => unsubscribe ( ) ;
3734 } , [ ] ) ;
3835
3936 const onClick : ButtonButtonProps [ "onClick" ] = useCallback ( ( ) => {
40- if ( ! graphRef . current ) return ;
41-
42- // Collect all selected components from all buckets
43- const allSelectedComponents : GraphComponent [ ] = [ ] ;
44- const selection = graphRef . current . selectionService . $selection . value ;
45-
46- // Iterate through all entity types in selection
47- for ( const entityType of selection . keys ( ) ) {
48- const bucket = graphRef . current . selectionService . getBucket ( entityType ) ;
49- if ( bucket ) {
50- const components = bucket . $selectedComponents . value ;
51- allSelectedComponents . push ( ...components ) ;
52- }
53- }
54-
55- if ( allSelectedComponents . length === 0 ) return ;
37+ if ( ! graphRef . current || selectedComponents . length === 0 ) return ;
5638
57- // Zoom to the calculated rect
58- graphRef . current . zoomTo ( allSelectedComponents , {
39+ // Zoom to the selected components
40+ graphRef . current . zoomTo ( selectedComponents , {
5941 transition : Number ( transition ) ,
6042 padding : Number ( padding ) ,
6143 } ) ;
62- } , [ transition , padding ] ) ;
44+ } , [ selectedComponents , transition , padding ] ) ;
6345
6446 return (
6547 < ThemeProvider theme = { "light" } >
@@ -68,12 +50,12 @@ const GraphApp = () => {
6850 Select elements by clicking on them (use Cmd/Ctrl + Click to select multiple elements), then click the button
6951 below to zoom to the selected elements.
7052 </ Text >
71- < Text color = "secondary" > Selected elements: { selectedCount } </ Text >
53+ < Text color = "secondary" > Selected elements: { selectedComponents . length } </ Text >
7254 < Flex direction = { "row" } gap = { 1 } >
7355 < TextInput type = "number" label = "transition (ms)" value = { transition } onUpdate = { setTransition } />
7456 < TextInput type = "number" label = "padding (px)" value = { padding } onUpdate = { setPadding } />
7557 </ Flex >
76- < Button onClick = { onClick } view = "action" disabled = { selectedCount === 0 } >
58+ < Button onClick = { onClick } view = "action" disabled = { selectedComponents . length === 0 } >
7759 Zoom to Selected Elements
7860 </ Button >
7961 </ Flex >
0 commit comments