@@ -20,68 +20,103 @@ function maybeStringifyChildren(children) {
2020export default function CodeBlock ( { children : rawChildren , ...props } ) {
2121 const [ codes , setCodes ] = useState ( [ ] ) ; // 코드를 담을 상태
2222 const [ names , setNames ] = useState ( [ ] ) ;
23+ const [ members , setMembers ] = useState ( [ ] ) ;
2324
24- useEffect ( ( ) => {
25- const fetchCodes = async ( ) => {
26- try {
27- const axios = require ( 'axios' ) ;
25+ const axios = require ( 'axios' ) ;
2826
29- const getRepositoryContents = async ( owner , repo , path = '' ) => {
30- try {
31- const response = await axios . get ( `https://api.github.com/repos/ ${ owner } / ${ repo } /contents/ ${ path } ` ) ;
32- return response . data ;
33- } catch ( error ) {
34- console . error ( 'Error fetching repository contents:' , error ) ;
35- return [ ] ;
36- }
37- } ;
27+ const fetchMembers = async ( org = "Code-Study" , token = null ) => {
28+ try {
29+ const options = token
30+ ? {
31+ headers : { Authorization : `Bearer ${ token } ` } ,
32+ params : { filter : 'all' } ,
33+ }
34+ : { } ;
35+ const response = await axios . get ( `https://api.github.com/orgs/ ${ org } /members` , options ) ;
3836
39- const getFilesRecursively = async ( owner , repo , path = '' ) => {
40- let files = [ ] ;
41- const contents = await getRepositoryContents ( owner , repo , path ) ;
42-
43- for ( const content of contents ) {
44- if ( content . type === 'file' ) {
45- // 파일인 경우 목록에 추가
46- files . push ( content . path ) ;
47- } else if ( content . type === 'dir' ) {
48- // 폴더인 경우 재귀적으로 해당 폴더의 파일을 가져옴
49- const subFiles = await getFilesRecursively ( owner , repo , content . path ) ;
50- files = files . concat ( subFiles ) ;
51- }
52- }
53- return files ;
54- } ;
55-
56- const getRepositoryFileContent = async ( owner , repo , path ) => {
57- try {
58- const response = await axios . get ( `https://api.github.com/repos/${ owner } /${ repo } /contents/${ path } ` ) ;
59- const content = decodeURIComponent ( escape ( window . atob ( response . data . content ) ) ) ; // 디코딩
60- return content ;
61- } catch ( error ) {
62- console . error ( 'Error fetching file content:' , error ) ;
63- return null ;
64- }
65- } ;
37+ const members = response . data . map ( ( member ) => (
38+ member . login
39+ ) ) ;
40+ setMembers ( members )
41+ return members ;
42+ } catch ( error ) {
43+ console . error ( 'Error fetching organization members:' , error ) ;
44+ }
45+ } ;
46+
47+ const getRepositoryContents = async ( owner , repo , path = '' ) => {
48+ try {
49+ const response = await axios . get ( `https://api.github.com/repos/${ owner } /${ repo } /contents/${ path } ` ) ;
50+ return response . data ;
51+ } catch ( error ) {
52+ console . error ( 'Error fetching repository contents:' , error ) ;
53+ return [ ] ;
54+ }
55+ } ;
56+
57+ const getFilesRecursively = async ( owner , repo , path = '' ) => {
58+ let files = [ ] ;
59+ const contents = await getRepositoryContents ( owner , repo , path ) ;
60+
61+ for ( const content of contents ) {
62+ if ( content . type === 'file' ) {
63+ // 파일인 경우 목록에 추가
64+ files . push ( content . path ) ;
65+ } else if ( content . type === 'dir' ) {
66+ // 폴더인 경우 재귀적으로 해당 폴더의 파일을 가져옴
67+ const subFiles = await getFilesRecursively ( owner , repo , content . path ) ;
68+ files = files . concat ( subFiles ) ;
69+ }
70+ }
71+ return files ;
72+ } ;
6673
67- // const pattern = /^[a-zA-Z]+-[a-zA-Z]+\/\d+\/\d+\/\d+$/;
68- // const pattern = /^[a-zA-Z]+-[a-zA-Z]+\/[a-zA-Z]+-\d+\/\d+-[a-zA-Z]+-[a-zA-Z]+\/\d+$/;
69- const pattern = / ^ ( l e e t - c o d e | n o v i c e - h i g h ) \/ .+ \/ .+ \/ .+ $ / ;
70- if ( ( pattern . test ( props . metastring ) === true ) && ( props . className === "language-python" ) ) {
71- const contents = await getFilesRecursively ( 'Code-Study' , 'Code' , props . metastring ) ;
72- const codePromises = contents . map ( element =>
73- getRepositoryFileContent ( 'Code-Study' , 'Code' , element )
74+ const getRepositoryFileContent = async ( owner , repo , path ) => {
75+ try {
76+ const response = await axios . get ( `https://api.github.com/repos/${ owner } /${ repo } /contents/${ path } ` ) ;
77+ const content = decodeURIComponent ( escape ( window . atob ( response . data . content ) ) ) ; // 디코딩
78+ return content ;
79+ } catch ( error ) {
80+ console . error ( 'Error fetching file content:' , error ) ;
81+ return null ;
82+ }
83+ } ;
84+
85+ const fetchCodes = async ( ) => {
86+ try {
87+ // const pattern = /^[a-zA-Z]+-[a-zA-Z]+\/\d+\/\d+\/\d+$/;
88+ // const pattern = /^[a-zA-Z]+-[a-zA-Z]+\/[a-zA-Z]+-\d+\/\d+-[a-zA-Z]+-[a-zA-Z]+\/\d+$/;
89+ const pattern = / ^ ( l e e t - c o d e | n o v i c e - h i g h ) \/ .+ \/ .+ \/ .+ $ / ;
90+ if ( ( pattern . test ( props . metastring ) === true ) && ( props . className === "language-python" ) ) {
91+ const contents = await getFilesRecursively ( 'Code-Study' , 'Code' , props . metastring ) ;
92+ const codePromises = contents . map ( element =>
93+ getRepositoryFileContent ( 'Code-Study' , 'Code' , element )
94+ ) ;
95+ const codeContents = await Promise . all ( codePromises ) ;
96+ setCodes ( codeContents ) ;
97+ //console.log(contents.map(elem => elem.split('/').pop().split('.')[0]));
98+ setNames ( contents . map ( elem => elem . split ( '/' ) . pop ( ) . split ( '.' ) [ 0 ] ) ) ;
99+ } else {
100+ const memberContents = await fetchMembers ( 'Code-Study' ) ;
101+
102+ setNames ( memberContents ) ;
103+ const contents = memberContents . map ( async member => {
104+ const contents = await getFilesRecursively ( member , 'LeetCode' , props . metastring ) ;
105+ const codePromises = contents
106+ . filter ( element => element . endsWith ( '.py' ) )
107+ . map ( element =>
108+ getRepositoryFileContent ( member , 'LeetCode' , element )
74109 ) ;
75110 const codeContents = await Promise . all ( codePromises ) ;
76- setCodes ( codeContents ) ;
77- //console.log(contents.map(elem => elem.split('/').pop().split('.')[0]));
78- setNames ( contents . map ( elem => elem . split ( '/' ) . pop ( ) . split ( '.' ) [ 0 ] ) ) ;
79- }
80- } catch ( error ) {
81- console . error ( 'Error fetching codes:' , error ) ;
111+ setCodes ( ( prevCodes ) => [ ...prevCodes , ...codeContents ] ) ;
112+ } ) ;
82113 }
83- } ;
84-
114+ } catch ( error ) {
115+ console . error ( 'Error fetching codes:' , error ) ;
116+ }
117+ } ;
118+
119+ useEffect ( ( ) => {
85120 fetchCodes ( ) ;
86121 } , [ props . metastring ] ) ;
87122
0 commit comments