Skip to content

Commit f539057

Browse files
committed
feat: add daily challenge problem pick (LeetCode-OpenSource#919)
* feat: add daily chanllenge problem pick, both work on us and cn endpoint
1 parent 586b3e4 commit f539057

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

package.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"onCommand:leetcode.manageSessions",
3333
"onCommand:leetcode.refreshExplorer",
3434
"onCommand:leetcode.pickOne",
35+
"onCommand:leetcode.pickDaily",
3536
"onCommand:leetcode.showProblem",
3637
"onCommand:leetcode.previewProblem",
3738
"onCommand:leetcode.searchProblem",
@@ -82,6 +83,11 @@
8283
"title": "Pick One",
8384
"category": "LeetCode"
8485
},
86+
{
87+
"command": "leetcode.pickDaily",
88+
"title": "Pick Daily Problem",
89+
"category": "LeetCode"
90+
},
8591
{
8692
"command": "leetcode.showProblem",
8793
"title": "Show Problem",
@@ -193,9 +199,14 @@
193199
"group": "overflow@2"
194200
},
195201
{
196-
"command": "leetcode.problems.sort",
202+
"command": "leetcode.pickDaily",
197203
"when": "view == leetCodeExplorer",
198204
"group": "overflow@3"
205+
},
206+
{
207+
"command": "leetcode.problems.sort",
208+
"when": "view == leetCodeExplorer",
209+
"group": "overflow@4"
199210
}
200211
],
201212
"view/item/context": [

src/commands/show.ts

+11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { leetCodeSolutionProvider } from "../webview/leetCodeSolutionProvider";
3030
import * as list from "./list";
3131
import { getLeetCodeEndpoint } from "./plugin";
3232
import { globalState } from "../globalState";
33+
import { queryDailyProblem } from "../request/query-daily-problem";
3334

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

74+
export async function pickDaily(): Promise<void> {
75+
const dailyProblemID: string = await queryDailyProblem();
76+
const node: IProblem | undefined = explorerNodeManager.getNodeById(dailyProblemID);
77+
if (!node) {
78+
vscode.window.showErrorMessage(`Failed to resolve the problem with id: ${dailyProblemID}.`);
79+
return;
80+
}
81+
await showProblemInternal(node);
82+
}
83+
7384
export async function showProblem(node?: LeetCodeNode): Promise<void> {
7485
if (!node) {
7586
return;

src/extension.ts

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
7171
}),
7272
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(node)),
7373
vscode.commands.registerCommand("leetcode.pickOne", () => show.pickOne()),
74+
vscode.commands.registerCommand("leetcode.pickDaily", () => show.pickDaily()),
7475
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem()),
7576
vscode.commands.registerCommand("leetcode.showSolution", (input: LeetCodeNode | vscode.Uri) => show.showSolution(input)),
7677
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),

src/request/query-daily-problem.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { getUrl, getDailyQueryStr, getDailyProblemID } from "../shared";
2+
import { LcAxios } from "../utils/httpUtils";
3+
4+
5+
export const queryDailyProblem = async (): Promise<string> => {
6+
return LcAxios(getUrl("graphql"), {
7+
method: "POST",
8+
data: {
9+
query: getDailyQueryStr(),
10+
variables: {},
11+
operationName: "questionOfToday"
12+
},
13+
}).then((res) => getDailyProblemID(res));
14+
};

src/shared.ts

+45
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
import * as vscode from "vscode";
5+
import { AxiosResponse } from "axios";
56

67
export interface IQuickItemEx<T> extends vscode.QuickPickItem {
78
value: T;
@@ -156,3 +157,47 @@ export const getUrl = (key: string) => {
156157
return urls[key];
157158
}
158159
};
160+
161+
export const dailyQueryStr = `
162+
query questionOfToday {
163+
activeDailyCodingChallengeQuestion {
164+
question {
165+
frontendQuestionId: questionFrontendId
166+
}
167+
}
168+
}
169+
`;
170+
171+
export const dailyQueryStrCn = `
172+
query questionOfToday {
173+
todayRecord {
174+
question {
175+
questionId
176+
}
177+
}
178+
}
179+
`;
180+
181+
export const getDailyQueryStr = () => {
182+
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
183+
const point = leetCodeConfig.get<string>("endpoint", Endpoint.LeetCode);
184+
switch (point) {
185+
case Endpoint.LeetCodeCN:
186+
return dailyQueryStrCn;
187+
case Endpoint.LeetCode:
188+
default:
189+
return dailyQueryStr;
190+
}
191+
};
192+
193+
export const getDailyProblemID = (res : AxiosResponse<any, any>) => {
194+
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
195+
const point = leetCodeConfig.get<string>("endpoint", Endpoint.LeetCode);
196+
switch (point) {
197+
case Endpoint.LeetCodeCN:
198+
return res.data.data.todayRecord[0].question.questionId;
199+
case Endpoint.LeetCode:
200+
default:
201+
return res.data.data.activeDailyCodingChallengeQuestion.question.frontendQuestionId;
202+
}
203+
}

0 commit comments

Comments
 (0)