@@ -11,6 +11,7 @@ import ReactFlow, {
1111} from 'reactflow'
1212import dagre from 'dagre'
1313import 'reactflow/dist/style.css'
14+ import { useDependencyGraph } from '../hooks/useCachedQuery'
1415
1516interface DependencyGraphProps {
1617 repoId : string
@@ -47,17 +48,25 @@ const getLayoutedElements = (nodes: Node[], edges: Edge[]) => {
4748export function DependencyGraph ( { repoId, apiUrl, apiKey } : DependencyGraphProps ) {
4849 const [ nodes , setNodes , onNodesChange ] = useNodesState ( [ ] )
4950 const [ edges , setEdges , onEdgesChange ] = useEdgesState ( [ ] )
50- const [ loading , setLoading ] = useState ( true )
5151 const [ metrics , setMetrics ] = useState < any > ( null )
5252 const [ filterCritical , setFilterCritical ] = useState ( false )
5353 const [ minDeps , setMinDeps ] = useState ( 0 )
5454 const [ highlightedNode , setHighlightedNode ] = useState < string | null > ( null )
5555 const [ allNodes , setAllNodes ] = useState < Node [ ] > ( [ ] )
5656 const [ allEdges , setAllEdges ] = useState < Edge [ ] > ( [ ] )
5757
58+ // Use cached query for dependencies
59+ const { data, isLoading : loading , isFetching } = useDependencyGraph ( {
60+ repoId,
61+ apiKey
62+ } )
63+
64+ // Process data when it arrives
5865 useEffect ( ( ) => {
59- loadGraph ( )
60- } , [ repoId ] )
66+ if ( data ) {
67+ processGraphData ( data )
68+ }
69+ } , [ data ] )
6170
6271 useEffect ( ( ) => {
6372 if ( allNodes . length > 0 ) {
@@ -89,83 +98,67 @@ export function DependencyGraph({ repoId, apiUrl, apiKey }: DependencyGraphProps
8998 setEdges ( layoutedEdges )
9099 }
91100
92- const loadGraph = async ( ) => {
93- setLoading ( true )
94- try {
95- const response = await fetch ( `${ apiUrl } /api/repos/${ repoId } /dependencies` , {
96- headers : {
97- 'Authorization' : `Bearer ${ apiKey } `
98- }
99- } )
101+ const processGraphData = ( data : any ) => {
102+ const flowNodes : Node [ ] = data . nodes . map ( ( node : any ) => {
103+ const fileName = node . label || node . id . split ( '/' ) . pop ( )
104+ const fullPath = node . id
105+ const importCount = node . import_count || node . imports || 0
100106
101- const data = await response . json ( )
102-
103- const flowNodes : Node [ ] = data . nodes . map ( ( node : any ) => {
104- const fileName = node . label || node . id . split ( '/' ) . pop ( )
105- const fullPath = node . id
106- const importCount = node . import_count || node . imports || 0
107-
108- return {
109- id : node . id ,
110- type : 'default' ,
111- data : {
112- label : (
113- < div title = { fullPath } style = { { cursor : 'pointer' } } >
114- < div style = { { fontWeight : 600 , fontSize : '11px' , marginBottom : '4px' } } >
115- { fileName }
116- </ div >
117- { importCount > 0 && (
118- < div style = { { fontSize : '9px' , opacity : 0.8 } } >
119- { importCount } imports
120- </ div >
121- ) }
107+ return {
108+ id : node . id ,
109+ type : 'default' ,
110+ data : {
111+ label : (
112+ < div title = { fullPath } style = { { cursor : 'pointer' } } >
113+ < div style = { { fontWeight : 600 , fontSize : '11px' , marginBottom : '4px' } } >
114+ { fileName }
122115 </ div >
123- ) ,
124- language : node . language ,
125- imports : importCount
126- } ,
127- position : { x : 0 , y : 0 } ,
128- style : {
129- background : getLanguageColor ( node . language ) ,
130- color : 'white' ,
131- border : '2px solid #3b82f6' ,
132- borderRadius : '8px' ,
133- padding : '8px 12px' ,
134- fontSize : '11px' ,
135- fontFamily : 'monospace' ,
136- width : 180 ,
137- height : 60
138- }
139- }
140- } )
141-
142- const flowEdges : Edge [ ] = data . edges . map ( ( edge : any ) => ( {
143- id : `${ edge . source } -${ edge . target } ` ,
144- source : edge . source ,
145- target : edge . target ,
146- animated : false ,
147- style : { stroke : '#4b5563' , strokeWidth : 1.5 } ,
148- markerEnd : {
149- type : MarkerType . ArrowClosed ,
150- color : '#4b5563' ,
116+ { importCount > 0 && (
117+ < div style = { { fontSize : '9px' , opacity : 0.8 } } >
118+ { importCount } imports
119+ </ div >
120+ ) }
121+ </ div >
122+ ) ,
123+ language : node . language ,
124+ imports : importCount
151125 } ,
152- } ) )
153-
154- setAllNodes ( flowNodes )
155- setAllEdges ( flowEdges )
156- setMetrics ( data . metrics )
157-
158- const { nodes : layoutedNodes , edges : layoutedEdges } =
159- getLayoutedElements ( flowNodes , flowEdges )
160-
161- setNodes ( layoutedNodes )
162- setEdges ( layoutedEdges )
163-
164- } catch ( error ) {
165- console . error ( 'Error loading graph:' , error )
166- } finally {
167- setLoading ( false )
168- }
126+ position : { x : 0 , y : 0 } ,
127+ style : {
128+ background : getLanguageColor ( node . language ) ,
129+ color : 'white' ,
130+ border : '2px solid #3b82f6' ,
131+ borderRadius : '8px' ,
132+ padding : '8px 12px' ,
133+ fontSize : '11px' ,
134+ fontFamily : 'monospace' ,
135+ width : 180 ,
136+ height : 60
137+ }
138+ }
139+ } )
140+
141+ const flowEdges : Edge [ ] = data . edges . map ( ( edge : any ) => ( {
142+ id : `${ edge . source } -${ edge . target } ` ,
143+ source : edge . source ,
144+ target : edge . target ,
145+ animated : false ,
146+ style : { stroke : '#4b5563' , strokeWidth : 1.5 } ,
147+ markerEnd : {
148+ type : MarkerType . ArrowClosed ,
149+ color : '#4b5563' ,
150+ } ,
151+ } ) )
152+
153+ setAllNodes ( flowNodes )
154+ setAllEdges ( flowEdges )
155+ setMetrics ( data . metrics )
156+
157+ const { nodes : layoutedNodes , edges : layoutedEdges } =
158+ getLayoutedElements ( flowNodes , flowEdges )
159+
160+ setNodes ( layoutedNodes )
161+ setEdges ( layoutedEdges )
169162 }
170163
171164 const handleNodeClick = useCallback ( ( event : any , node : Node ) => {
0 commit comments