Skip to content

Commit

Permalink
feat: add daily challenge problem pick (LeetCode-OpenSource#919)
Browse files Browse the repository at this point in the history
* feat: add daily chanllenge problem pick, both work on us and cn endpoint
  • Loading branch information
Icathian-Rain committed Oct 27, 2024
1 parent 586b3e4 commit f539057
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"onCommand:leetcode.manageSessions",
"onCommand:leetcode.refreshExplorer",
"onCommand:leetcode.pickOne",
"onCommand:leetcode.pickDaily",
"onCommand:leetcode.showProblem",
"onCommand:leetcode.previewProblem",
"onCommand:leetcode.searchProblem",
Expand Down Expand Up @@ -82,6 +83,11 @@
"title": "Pick One",
"category": "LeetCode"
},
{
"command": "leetcode.pickDaily",
"title": "Pick Daily Problem",
"category": "LeetCode"
},
{
"command": "leetcode.showProblem",
"title": "Show Problem",
Expand Down Expand Up @@ -193,9 +199,14 @@
"group": "overflow@2"
},
{
"command": "leetcode.problems.sort",
"command": "leetcode.pickDaily",
"when": "view == leetCodeExplorer",
"group": "overflow@3"
},
{
"command": "leetcode.problems.sort",
"when": "view == leetCodeExplorer",
"group": "overflow@4"
}
],
"view/item/context": [
Expand Down
11 changes: 11 additions & 0 deletions src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { leetCodeSolutionProvider } from "../webview/leetCodeSolutionProvider";
import * as list from "./list";
import { getLeetCodeEndpoint } from "./plugin";
import { globalState } from "../globalState";
import { queryDailyProblem } from "../request/query-daily-problem";

export async function previewProblem(input: IProblem | vscode.Uri, isSideMode: boolean = false): Promise<void> {
let node: IProblem;
Expand Down Expand Up @@ -70,6 +71,16 @@ export async function pickOne(): Promise<void> {
await showProblemInternal(randomProblem);
}

export async function pickDaily(): Promise<void> {
const dailyProblemID: string = await queryDailyProblem();
const node: IProblem | undefined = explorerNodeManager.getNodeById(dailyProblemID);
if (!node) {
vscode.window.showErrorMessage(`Failed to resolve the problem with id: ${dailyProblemID}.`);
return;
}
await showProblemInternal(node);
}

export async function showProblem(node?: LeetCodeNode): Promise<void> {
if (!node) {
return;
Expand Down
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
}),
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(node)),
vscode.commands.registerCommand("leetcode.pickOne", () => show.pickOne()),
vscode.commands.registerCommand("leetcode.pickDaily", () => show.pickDaily()),
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem()),
vscode.commands.registerCommand("leetcode.showSolution", (input: LeetCodeNode | vscode.Uri) => show.showSolution(input)),
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),
Expand Down
14 changes: 14 additions & 0 deletions src/request/query-daily-problem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getUrl, getDailyQueryStr, getDailyProblemID } from "../shared";
import { LcAxios } from "../utils/httpUtils";


export const queryDailyProblem = async (): Promise<string> => {
return LcAxios(getUrl("graphql"), {
method: "POST",
data: {
query: getDailyQueryStr(),
variables: {},
operationName: "questionOfToday"
},
}).then((res) => getDailyProblemID(res));
};
45 changes: 45 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import * as vscode from "vscode";
import { AxiosResponse } from "axios";

export interface IQuickItemEx<T> extends vscode.QuickPickItem {
value: T;
Expand Down Expand Up @@ -156,3 +157,47 @@ export const getUrl = (key: string) => {
return urls[key];
}
};

export const dailyQueryStr = `
query questionOfToday {
activeDailyCodingChallengeQuestion {
question {
frontendQuestionId: questionFrontendId
}
}
}
`;

export const dailyQueryStrCn = `
query questionOfToday {
todayRecord {
question {
questionId
}
}
}
`;

export const getDailyQueryStr = () => {
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
const point = leetCodeConfig.get<string>("endpoint", Endpoint.LeetCode);
switch (point) {
case Endpoint.LeetCodeCN:
return dailyQueryStrCn;
case Endpoint.LeetCode:
default:
return dailyQueryStr;
}
};

export const getDailyProblemID = (res : AxiosResponse<any, any>) => {
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
const point = leetCodeConfig.get<string>("endpoint", Endpoint.LeetCode);
switch (point) {
case Endpoint.LeetCodeCN:
return res.data.data.todayRecord[0].question.questionId;
case Endpoint.LeetCode:
default:
return res.data.data.activeDailyCodingChallengeQuestion.question.frontendQuestionId;
}
}

0 comments on commit f539057

Please sign in to comment.