@@ -17,15 +17,17 @@ function maybeStringifyChildren(children) {
17
17
return Array . isArray ( children ) ? children . join ( "" ) : children ;
18
18
}
19
19
20
+
20
21
export default function CodeBlock ( { children : rawChildren , ...props } ) {
21
22
const [ codes , setCodes ] = useState ( [ ] ) ; // 코드를 담을 상태
22
23
const [ names , setNames ] = useState ( [ ] ) ;
23
24
const [ members , setMembers ] = useState ( [ ] ) ;
24
25
25
26
const axios = require ( 'axios' ) ;
26
27
27
- const fetchMembers = async ( org = "Code-Study" , token = null ) => {
28
+ const fetchMembers = async ( org = "Code-Study" ) => {
28
29
try {
30
+ const token = localStorage . getItem ( 'githubToken' ) ;
29
31
const options = token
30
32
? {
31
33
headers : { Authorization : `Bearer ${ token } ` } ,
@@ -40,63 +42,51 @@ export default function CodeBlock({ children: rawChildren, ...props }) {
40
42
setMembers ( members )
41
43
return members ;
42
44
} 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 ) ;
45
+ if ( error . response && error . response . status === 403 ) {
46
+ console . error ( 'Error: 403 Forbidden. A token is required.' ) ;
47
+
48
+ const userToken = prompt ( 'Access denied. Please provide a valid GitHub personal access token:' ) ;
49
+
50
+ if ( userToken ) {
51
+ localStorage . setItem ( 'githubToken' , userToken ) ;
52
+ return await fetchMembers ( org ) ;
53
+ } else {
54
+ console . error ( 'No token provided. Unable to access the file.' ) ;
55
+ return null ;
56
+ }
57
+ } else {
58
+ console . error ( 'Error fetching file content:' , error ) ;
59
+ return null ;
69
60
}
70
- }
71
- return files ;
61
+ }
72
62
} ;
73
63
74
64
const getRepositoryFileContent = async ( owner , repo , path ) => {
75
65
try {
76
- const response = await axios . get ( `https://api.github.com/repos/${ owner } /${ repo } /contents/${ path } ` ) ;
66
+ const token = localStorage . getItem ( 'githubToken' ) ;
67
+ const options = token
68
+ ? {
69
+ headers : { Authorization : `Bearer ${ token } ` } ,
70
+ params : { filter : 'all' } ,
71
+ }
72
+ : { } ;
73
+ const response = await axios . get ( `https://api.github.com/repos/${ owner } /${ repo } /contents/${ path } ` , options ) ;
77
74
const content = decodeURIComponent ( escape ( window . atob ( response . data . content ) ) ) ; // 디코딩
78
75
return content ;
79
76
} catch ( error ) {
80
- console . error ( 'Error fetching file content:' , error ) ;
81
- return null ;
77
+ if ( error . response && error . response . status === 403 ) {
78
+ console . error ( 'Error: 403 Forbidden. A token is required.' ) ;
79
+ } else {
80
+ console . error ( 'Error fetching file content:' , error ) ;
81
+ return null ;
82
+ }
82
83
}
83
84
} ;
84
85
85
86
const fetchCodes = async ( ) => {
86
87
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 ) \/ .+ \/ .+ \/ .+ $ / ;
88
+ const pattern = / ^ \d { 4 } - / ;
90
89
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
90
const memberContents = await fetchMembers ( 'Code-Study' ) ;
101
91
const fileName = props . metastring + '/' + props . metastring + '.py'
102
92
memberContents . map ( async member => {
0 commit comments