diff --git a/package.json b/package.json index 53552b7..433e9d8 100644 --- a/package.json +++ b/package.json @@ -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", @@ -82,6 +83,11 @@ "title": "Pick One", "category": "LeetCode" }, + { + "command": "leetcode.pickDaily", + "title": "Pick Daily Problem", + "category": "LeetCode" + }, { "command": "leetcode.showProblem", "title": "Show Problem", @@ -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": [ diff --git a/src/commands/show.ts b/src/commands/show.ts index eccf557..7e2c62c 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -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 { let node: IProblem; @@ -70,6 +71,16 @@ export async function pickOne(): Promise { await showProblemInternal(randomProblem); } +export async function pickDaily(): Promise { + 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 { if (!node) { return; diff --git a/src/extension.ts b/src/extension.ts index 439673f..7599418 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -71,6 +71,7 @@ export async function activate(context: vscode.ExtensionContext): Promise }), 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()), diff --git a/src/request/query-daily-problem.ts b/src/request/query-daily-problem.ts new file mode 100644 index 0000000..da3cbb9 --- /dev/null +++ b/src/request/query-daily-problem.ts @@ -0,0 +1,14 @@ +import { getUrl, getDailyQueryStr, getDailyProblemID } from "../shared"; +import { LcAxios } from "../utils/httpUtils"; + + +export const queryDailyProblem = async (): Promise => { + return LcAxios(getUrl("graphql"), { + method: "POST", + data: { + query: getDailyQueryStr(), + variables: {}, + operationName: "questionOfToday" + }, + }).then((res) => getDailyProblemID(res)); +}; diff --git a/src/shared.ts b/src/shared.ts index e8b59d8..f8542e6 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -2,6 +2,7 @@ // Licensed under the MIT license. import * as vscode from "vscode"; +import { AxiosResponse } from "axios"; export interface IQuickItemEx extends vscode.QuickPickItem { value: T; @@ -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 { + frontendQuestionId: questionFrontendId + } + } + } +`; + +export const getDailyQueryStr = () => { + const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode"); + const point = leetCodeConfig.get("endpoint", Endpoint.LeetCode); + switch (point) { + case Endpoint.LeetCodeCN: + return dailyQueryStrCn; + case Endpoint.LeetCode: + default: + return dailyQueryStr; + } +}; + +export const getDailyProblemID = (res : AxiosResponse) => { + const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode"); + const point = leetCodeConfig.get("endpoint", Endpoint.LeetCode); + switch (point) { + case Endpoint.LeetCodeCN: + return res.data.data.todayRecord[0].question.frontendQuestionId; + case Endpoint.LeetCode: + default: + return res.data.data.activeDailyCodingChallengeQuestion.question.frontendQuestionId; + } +}