@@ -215,19 +215,28 @@ function titleToGitHubFormat(title: string, frontend_id: number): string {
215
215
return `${ idStr } -${ formattedTitle } ` ;
216
216
}
217
217
218
+ // Define the language map type
219
+ type SupportedLanguage = 'python' | 'java' | 'javascript' | 'cpp' ;
220
+
218
221
// Fetches the solution code from Neetcode's github repo
219
- async function getCodeSolution ( title : string , frontend_id : number , language : string , ) {
222
+ async function getCodeSolution ( title : string , frontend_id : number , language : string ) : Promise < string | null > {
220
223
// map the language names to their extensions
221
- const languageMap = {
224
+ const languageMap : Record < SupportedLanguage , string > = {
222
225
'python' : 'py' ,
223
226
'java' : 'java' ,
224
227
'javascript' : 'js' ,
225
228
'cpp' : 'cpp' ,
229
+ } ;
230
+
231
+ // Type guard to check if the language is supported
232
+ if ( ! isLanguageSupported ( language ) ) {
233
+ console . error ( 'Unsupported language:' , language ) ;
234
+ return null ;
226
235
}
227
236
228
237
// Convert frontend_id and title to the GitHub-compatible format
229
238
const formattedTitle = titleToGitHubFormat ( title , frontend_id ) ;
230
- const filePath = `${ language } /${ formattedTitle } .${ languageMap [ language ] } ` ; // Change 'other_extension' accordingly
239
+ const filePath = `${ language } /${ formattedTitle } .${ languageMap [ language as SupportedLanguage ] } ` ;
231
240
232
241
// Construct the URL to fetch the file content from GitHub
233
242
const url = `https://api.github.com/repos/neetcode-gh/leetcode/contents/${ filePath } ` ;
@@ -242,9 +251,15 @@ async function getCodeSolution(title: string, frontend_id: number, language: str
242
251
return code ;
243
252
} catch ( error ) {
244
253
console . error ( 'Failed to fetch code:' , error ) ;
254
+ return null ;
245
255
}
246
256
}
247
257
258
+ // Type guard function to check if a language is supported
259
+ function isLanguageSupported ( language : string ) : language is SupportedLanguage {
260
+ return [ 'python' , 'java' , 'javascript' , 'cpp' ] . includes ( language ) ;
261
+ }
262
+
248
263
function createLanguageButtons ( problem : any ) {
249
264
const container = createStyledElement ( 'div' , {
250
265
paddingTop : '20px' ,
@@ -282,20 +297,22 @@ function createLanguageButtons(problem: any) {
282
297
langName . style . paddingLeft = '15px' ;
283
298
langButton . appendChild ( langName ) ;
284
299
285
- langButton . addEventListener ( 'click' , ( ) => {
286
- let code = getCodeSolution ( problem . title , problem . frontend_id , language ) ;
287
- code . then ( ( code ) => {
288
- let codeContainer = document . getElementsByClassName ( 'code-container' ) [ 0 ] as HTMLDivElement ;
289
- if ( codeContainer ) {
290
- codeContainer . style . display = 'flex' ;
291
- codeContainer . textContent = code ;
292
- addCopyIconToElement ( codeContainer ) ;
293
- }
294
- } ) ;
300
+ langButton . addEventListener ( 'click' , async ( ) => {
301
+ const code = await getCodeSolution ( problem . title , problem . frontend_id , language ) ;
302
+ let codeContainer = document . getElementsByClassName ( 'code-container' ) [ 0 ] as HTMLDivElement ;
303
+ if ( codeContainer && code ) {
304
+ codeContainer . style . display = 'flex' ;
305
+ codeContainer . textContent = code ;
306
+ addCopyIconToElement ( codeContainer ) ;
307
+ } else if ( codeContainer ) {
308
+ codeContainer . style . display = 'flex' ;
309
+ codeContainer . textContent = 'Code not available' ;
310
+ }
295
311
} ) ;
296
312
container . append ( langButton ) ;
297
313
} ) ;
298
314
return container ;
315
+
299
316
}
300
317
301
318
function addCopyIconToElement ( element : HTMLElement ) {
@@ -370,14 +387,6 @@ chrome.runtime.onMessage.addListener((request) => {
370
387
if ( ! document . querySelector ( '.code-container' ) && problem . languages . length > 0 ) {
371
388
let codeContainer = createCodeContainer ( ) ;
372
389
if ( searchBar ) searchBar . insertBefore ( codeContainer , searchBar . children [ 1 ] ) ;
373
- // let code = getCodeSolution(problem.title, problem.frontend_id, 'python');
374
- // code.then((code) => {
375
- // let codeContainer = document.getElementsByClassName('code-container')[0] as HTMLDivElement;
376
- // if (codeContainer) {
377
- // codeContainer.textContent = code;
378
- // addCopyIconToElement(codeContainer);
379
- // }
380
- // });
381
390
}
382
391
383
392
// Check if the language buttons container already exists before adding
0 commit comments