Skip to content

fix tsc errors #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
const url = tab.url;
let problemUrl = /^https:\/\/leetcode\.com\/problems\/.*\/?/;
if (tab.url.match(problemUrl)) {
if (url.match(problemUrl)) {
chrome.storage.local.get(['currentLeetCodeProblemTitle', 'descriptionTabUpdated', 'solutionsTabUpdated'], (result) => {
let lastTitle = result.currentLeetCodeProblemTitle || '';
let descriptionTabUpdated = result.descriptionTabUpdated || false;
Expand All @@ -88,18 +89,18 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
}

let descriptionUrl = /^https:\/\/leetcode\.com\/problems\/.*\/(description\/)?/;
if (!descriptionTabUpdated && tab.url.match(descriptionUrl)) {
if (!descriptionTabUpdated && url.match(descriptionUrl)) {
chrome.storage.local.set({ 'descriptionTabUpdated': true });
chrome.tabs.sendMessage(tabId, { action: 'updateDescription', title: tab.title || 'title' });
}

let solutionsUrl = /^https:\/\/leetcode\.com\/problems\/.*\/solutions\/?/;
if (tab.url.match(solutionsUrl)) {
if (url.match(solutionsUrl)) {
chrome.storage.local.set({ 'solutionsTabUpdated': true });
// No need for a timeout if the tab is already loaded completely.
chrome.tabs.sendMessage(tabId, { action: 'updateSolutions', title: tab.title || 'title' });
}
});
}
}
});

20 changes: 20 additions & 0 deletions src/content-script/get-user-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ function getProblem() {
return collectedData;
}

function getCodeComplexity() {
const codeEditor = document.querySelector('[data-track-load="code_editor"]');
if (!codeEditor) {
return {
code: '',
language: '',
error: 'Code editor not found'
};
}

const code = (codeEditor as HTMLElement).innerText;
const languageSelect = document.querySelector('[data-cy="lang-select"]') as HTMLElement;
const language = languageSelect ? languageSelect.innerText : '';

return {
code: code,
language: language
};
}

// On get user code request, read & send the code as a response
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'getProblem') {
Expand Down
14 changes: 10 additions & 4 deletions src/content-script/update-description-tab.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// shows the examples if the user has enabled it in the settings
function showExamples() {
chrome.storage.local.get(['showExamples'], (result) => {
Expand All @@ -14,11 +13,18 @@ function showExamples() {
});
}

// Define the Problem interface
interface Problem {
title: string;
rating?: string;
// Add other properties as needed
}

// show the leetcode difficulty if the user has enabled it in the settings
function showDifficulty() {
chrome.storage.local.get(['showDifficulty'], (result) => {
const showDifficulty = result.showDifficulty;
const difficultyContainer = document.querySelectorAll('div.relative.inline-flex')[0];
const difficultyContainer = document.querySelectorAll('div.relative.inline-flex')[0] as HTMLDivElement;
if (!difficultyContainer) return;
if (showDifficulty) {
// hide the first child of the difficulty container
Expand All @@ -36,7 +42,7 @@ function showRating(problemTitle: string) {
const showRating = result.showRating;
if (showRating) {
chrome.storage.local.get(['leetcodeProblems'], (result) => {
const problem = result.leetcodeProblems.questions.find((problem: problem) => problem.title === problemTitle);
const problem = result.leetcodeProblems.questions.find((problem: Problem) => problem.title === problemTitle);

let ratingElement = document.getElementById('rating');

Expand Down Expand Up @@ -65,7 +71,7 @@ function showRating(problemTitle: string) {
ratingElement.style.color = 'lightcyan';
}

const difficultyContainer = document.querySelectorAll('div.relative.inline-flex')[0];
const difficultyContainer = document.querySelectorAll('div.relative.inline-flex')[0] as HTMLDivElement;
if (difficultyContainer) {
// insert the rating element after the first child of the difficulty container
let parent = difficultyContainer.parentElement;
Expand Down
51 changes: 30 additions & 21 deletions src/content-script/update-solutions-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,28 @@ function titleToGitHubFormat(title: string, frontend_id: number): string {
return `${idStr}-${formattedTitle}`;
}

// Define the language map type
type SupportedLanguage = 'python' | 'java' | 'javascript' | 'cpp';

// Fetches the solution code from Neetcode's github repo
async function getCodeSolution(title: string, frontend_id: number, language: string,) {
async function getCodeSolution(title: string, frontend_id: number, language: string): Promise<string | null> {
// map the language names to their extensions
const languageMap = {
const languageMap: Record<SupportedLanguage, string> = {
'python': 'py',
'java': 'java',
'javascript': 'js',
'cpp': 'cpp',
};

// Type guard to check if the language is supported
if (!isLanguageSupported(language)) {
console.error('Unsupported language:', language);
return null;
}

// Convert frontend_id and title to the GitHub-compatible format
const formattedTitle = titleToGitHubFormat(title, frontend_id);
const filePath = `${language}/${formattedTitle}.${languageMap[language]}`; // Change 'other_extension' accordingly
const filePath = `${language}/${formattedTitle}.${languageMap[language as SupportedLanguage]}`;

// Construct the URL to fetch the file content from GitHub
const url = `https://api.github.com/repos/neetcode-gh/leetcode/contents/${filePath}`;
Expand All @@ -242,9 +251,15 @@ async function getCodeSolution(title: string, frontend_id: number, language: str
return code;
} catch (error) {
console.error('Failed to fetch code:', error);
return null;
}
}

// Type guard function to check if a language is supported
function isLanguageSupported(language: string): language is SupportedLanguage {
return ['python', 'java', 'javascript', 'cpp'].includes(language);
}

function createLanguageButtons(problem: any) {
const container = createStyledElement('div', {
paddingTop: '20px',
Expand Down Expand Up @@ -282,20 +297,22 @@ function createLanguageButtons(problem: any) {
langName.style.paddingLeft = '15px';
langButton.appendChild(langName);

langButton.addEventListener('click', () => {
let code = getCodeSolution(problem.title, problem.frontend_id, language);
code.then((code) => {
let codeContainer = document.getElementsByClassName('code-container')[0] as HTMLDivElement;
if (codeContainer) {
codeContainer.style.display = 'flex';
codeContainer.textContent = code;
addCopyIconToElement(codeContainer);
}
});
langButton.addEventListener('click', async () => {
const code = await getCodeSolution(problem.title, problem.frontend_id, language);
let codeContainer = document.getElementsByClassName('code-container')[0] as HTMLDivElement;
if (codeContainer && code) {
codeContainer.style.display = 'flex';
codeContainer.textContent = code;
addCopyIconToElement(codeContainer);
} else if (codeContainer) {
codeContainer.style.display = 'flex';
codeContainer.textContent = 'Code not available';
}
});
container.append(langButton);
});
return container;

}

function addCopyIconToElement(element: HTMLElement) {
Expand Down Expand Up @@ -370,14 +387,6 @@ chrome.runtime.onMessage.addListener((request) => {
if (!document.querySelector('.code-container') && problem.languages.length > 0) {
let codeContainer = createCodeContainer();
if (searchBar) searchBar.insertBefore(codeContainer, searchBar.children[1]);
// let code = getCodeSolution(problem.title, problem.frontend_id, 'python');
// code.then((code) => {
// let codeContainer = document.getElementsByClassName('code-container')[0] as HTMLDivElement;
// if (codeContainer) {
// codeContainer.textContent = code;
// addCopyIconToElement(codeContainer);
// }
// });
}

// Check if the language buttons container already exists before adding
Expand Down
4 changes: 2 additions & 2 deletions src/popup/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ const fixCodeContainer = elements['fixCodeContainer'];

/* Helper functions */
function disableAllButtons(disabled: boolean): void {
let fixCodeButton = elements['fixCodeBtn'];
let getComplexityButton = elements['getComplexityBtn'];
let fixCodeButton = elements['fixCodeBtn'] as HTMLButtonElement;
let getComplexityButton = elements['getComplexityBtn'] as HTMLButtonElement;

// Use the arguments to determine if a specific button should be disabled
fixCodeButton && (fixCodeButton.disabled = disabled);
Expand Down
Loading