@@ -4,53 +4,104 @@ import { getRepositoryContracts as getRepositoryContractsUrl } from "../urls"
44
55type GetRepositoryContractsResponse = string [ ]
66
7- export type TreeValue = Tree | string
8- export interface Tree extends Map < string , TreeValue > { }
9-
10- export const getAllTreePaths = ( parentPath : string , tree : TreeValue ) => {
11- if ( typeof tree === "string" ) {
12- return [ `${ parentPath } ` ]
13- }
7+ type File = {
8+ filepath : string
9+ nsloc ?: number
10+ }
1411
15- let paths : string [ ] = [ ]
12+ interface BaseEntry {
13+ name : string
14+ }
1615
17- tree . forEach ( ( value , key ) => {
18- paths = [ ...paths , ...getAllTreePaths ( `${ parentPath } /${ key } ` , value ) ]
19- } )
16+ export interface FileEntry extends BaseEntry {
17+ type : "file"
18+ filepath : string
19+ nsloc ?: number
20+ }
2021
21- return paths
22+ interface DirectoryEntry extends BaseEntry {
23+ type : "directory"
24+ entries : Array < Entry >
2225}
2326
24- type Files = {
25- tree : Tree
26- rawPaths : string [ ]
27+ export type Entry = FileEntry | DirectoryEntry
28+
29+ type AnyEntry = RootDirectory | DirectoryEntry
30+
31+ export interface RootDirectory {
32+ type : "root"
33+ entries : Array < Entry >
2734}
2835
29- export const convertToTree = ( paths : string [ ] ) => {
30- const tree : Tree = new Map ( )
36+ export const convertToTree2 = ( files : File [ ] ) => {
37+ const root : RootDirectory = {
38+ type : "root" ,
39+ entries : [ ] ,
40+ }
41+
42+ files . forEach ( ( { filepath, nsloc } ) => {
43+ const parts = filepath . split ( "/" )
44+
45+ let current : AnyEntry = root
3146
32- paths . forEach ( ( d ) => {
33- const parts = d . split ( "/" )
34- let current : Tree = tree
3547 parts . forEach ( ( p ) => {
3648 if ( p . endsWith ( ".sol" ) || p . endsWith ( ".vy" ) ) {
37- current . set ( p , p )
49+ current . entries . push ( {
50+ type : "file" ,
51+ name : p ,
52+ filepath,
53+ nsloc,
54+ } )
3855 } else {
39- if ( ! current . get ( p ) ) current . set ( p , new Map ( ) )
40- current = current . get ( p ) as Tree
56+ let directory : DirectoryEntry | undefined = current . entries . find (
57+ ( e ) => e . type === "directory" && e . name === p
58+ ) as DirectoryEntry
59+
60+ if ( ! directory ) {
61+ directory = {
62+ type : "directory" ,
63+ name : p ,
64+ entries : [ ] ,
65+ }
66+ current . entries . push ( directory )
67+ }
68+
69+ current = directory
4170 }
4271 } )
4372 } )
4473
45- return tree
74+ return root
75+ }
76+
77+ export type TreeValue = Tree | string
78+ export interface Tree extends Map < string , TreeValue > { }
79+
80+ export const getAllTreePaths = ( parentPath : string , tree : Entry ) => {
81+ if ( tree . type === "file" ) {
82+ return [ `${ parentPath } ` ]
83+ }
84+
85+ let paths : string [ ] = [ ]
86+
87+ tree . entries . forEach ( ( value , key ) => {
88+ paths = [ ...paths , ...getAllTreePaths ( `${ parentPath } /${ value . name } ` , value ) ]
89+ } )
90+
91+ return paths
92+ }
93+
94+ type RepositoryContracts = {
95+ tree : RootDirectory
96+ rawPaths : string [ ]
4697}
4798
4899export const repositoryContractsQuery = ( repo : string , commit : string ) => [ "repository-contracts" , repo , commit ]
49100export const useRepositoryContracts = ( repo : string , commit : string ) =>
50- useQuery < Files , Error > ( repositoryContractsQuery ( repo , commit ) , async ( ) => {
101+ useQuery < RepositoryContracts , Error > ( repositoryContractsQuery ( repo , commit ) , async ( ) => {
51102 const { data } = await contestsAPI . get < GetRepositoryContractsResponse > ( getRepositoryContractsUrl ( repo , commit ) )
52103
53- const tree = convertToTree ( data )
104+ const tree = convertToTree2 ( data . map ( ( f ) => ( { filepath : f } ) ) )
54105
55106 return {
56107 tree,
0 commit comments