Skip to content
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

feat: add daily challenge problem pick (#919) #981

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
11 changes: 11 additions & 0 deletions src/commands/show.ts
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -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()),
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
@@ -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;
@@ -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<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.frontendQuestionId;
case Endpoint.LeetCode:
default:
return res.data.data.activeDailyCodingChallengeQuestion.question.frontendQuestionId;
}
}