@@ -21,10 +21,15 @@ interface DependencyLink extends d3.SimulationLinkDatum<DependencyNode> {
2121export interface DependencyGraphProps {
2222 crateName : string ;
2323 currentVersion : string ;
24- dependencies ?: GraphDependency ;
24+ // dependencies?: GraphDependency;
2525}
2626
27- const DependencyGraph : React . FC < DependencyGraphProps > = ( { crateName, currentVersion, dependencies } ) => {
27+ interface SubDep {
28+ name : string ;
29+ version : string ;
30+ }
31+
32+ const DependencyGraph : React . FC < DependencyGraphProps > = ( { crateName, currentVersion } ) => {
2833 const [ graphDependencies , setGraphDependencies ] = useState < GraphDependency | null > ( null ) ;
2934 const d3Container = useRef < HTMLDivElement | null > ( null ) ;
3035 const router = useRouter ( ) ;
@@ -33,10 +38,10 @@ const DependencyGraph: React.FC<DependencyGraphProps> = ({ crateName, currentVer
3338 async function fetchDependencyTree ( name : string , version : string ) : Promise < GraphDependency > {
3439 const response = await fetch ( `/api/crates/${ name } /${ version } ` ) ;
3540 const versionData = await response . json ( ) ;
36-
41+
3742 const dependencies = versionData . dependencies || [ ] ;
38-
39- const dependenciesDetails = await Promise . all ( dependencies . map ( async ( subDep : any ) => {
43+
44+ const dependenciesDetails = await Promise . all ( dependencies . map ( async ( subDep : SubDep ) => {
4045 return fetchDependencyTree ( subDep . name , subDep . version ) ;
4146 } ) ) ;
4247
@@ -65,25 +70,25 @@ const DependencyGraph: React.FC<DependencyGraphProps> = ({ crateName, currentVer
6570 d3 . select ( d3Container . current ) . select ( 'svg' ) . remove ( ) ;
6671
6772 const svg = d3 . select ( d3Container . current ) . append ( 'svg' )
68- . attr ( 'width' , '100%' )
69- . attr ( 'height' , '100%' )
70- . attr ( 'viewBox' , `0 0 ${ width } ${ height } ` )
71- . attr ( 'preserveAspectRatio' , 'xMidYMid meet' ) ;
73+ . attr ( 'width' , '100%' )
74+ . attr ( 'height' , '100%' )
75+ . attr ( 'viewBox' , `0 0 ${ width } ${ height } ` )
76+ . attr ( 'preserveAspectRatio' , 'xMidYMid meet' ) ;
7277
7378 svg . append ( 'defs' ) . append ( 'marker' )
74- . attr ( 'id' , 'arrowhead' )
75- . attr ( 'viewBox' , '0 -5 10 10' )
76- . attr ( 'refX' , 20 ) // 增加 refX 以使箭头远离节点
77- . attr ( 'refY' , 0 )
78- . attr ( 'orient' , 'auto' )
79- . attr ( 'markerWidth' , 7 )
80- . attr ( 'markerHeight' , 7 )
81- . append ( 'path' )
82- . attr ( 'd' , 'M 0,-5 L 10,0 L 0,5' )
83- . attr ( 'fill' , '#333' )
84- . style ( 'stroke' , 'none' ) ;
85-
86-
79+ . attr ( 'id' , 'arrowhead' )
80+ . attr ( 'viewBox' , '0 -5 10 10' )
81+ . attr ( 'refX' , 20 ) // 增加 refX 以使箭头远离节点
82+ . attr ( 'refY' , 0 )
83+ . attr ( 'orient' , 'auto' )
84+ . attr ( 'markerWidth' , 7 )
85+ . attr ( 'markerHeight' , 7 )
86+ . append ( 'path' )
87+ . attr ( 'd' , 'M 0,-5 L 10,0 L 0,5' )
88+ . attr ( 'fill' , '#333' )
89+ . style ( 'stroke' , 'none' ) ;
90+
91+
8792
8893 const nodesMap = new Map < string , DependencyNode > ( ) ;
8994 const links : DependencyLink [ ] = [ ] ;
@@ -108,12 +113,12 @@ const DependencyGraph: React.FC<DependencyGraphProps> = ({ crateName, currentVer
108113 const nodes = Array . from ( nodesMap . values ( ) ) ;
109114
110115 const simulation = d3 . forceSimulation < DependencyNode > ( nodes )
111- . force ( 'link' , d3 . forceLink < DependencyNode , DependencyLink > ( links ) . id ( d => d . id ) . distance ( 100 ) ) // 增加距离
112- . force ( 'charge' , d3 . forceManyBody ( ) . strength ( - 500 ) ) // 增加排斥力
113- . force ( 'center' , d3 . forceCenter ( width / 2 , height / 2 ) )
114- . force ( 'collide' , d3 . forceCollide ( ) . radius ( 50 ) ) ; // 增加碰撞半径
116+ . force ( 'link' , d3 . forceLink < DependencyNode , DependencyLink > ( links ) . id ( d => d . id ) . distance ( 100 ) ) // 增加距离
117+ . force ( 'charge' , d3 . forceManyBody ( ) . strength ( - 500 ) ) // 增加排斥力
118+ . force ( 'center' , d3 . forceCenter ( width / 2 , height / 2 ) )
119+ . force ( 'collide' , d3 . forceCollide ( ) . radius ( 50 ) ) ; // 增加碰撞半径
115120
116- const link = svg . append ( 'g' )
121+ const link = svg . append ( 'g' )
117122 . selectAll ( 'line' )
118123 . data ( links )
119124 . enter ( ) . append ( 'line' )
0 commit comments