Skip to content

Commit 186c5c8

Browse files
committed
fix(problemset): 修复本地题目过滤失效
官方添加了一个分类 `all-code-essentials`,现在所有默认选中的是这个分类,表示选择所有题目
1 parent 7159f00 commit 186c5c8

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

src/content/pages/problemset/questionsSlice.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const questionsAdapter = createEntityAdapter<
3030
})
3131

3232
export const fetchAllQuestions = createAsyncThunk<
33-
ProblemsetQuestion[] | null,
33+
Map<CategorySlugType, ProblemsetQuestion[]> | null,
3434
undefined,
3535
{ state: RootState }
3636
>('questions/fetchAllQuestions', async (_, { getState, dispatch }) => {
@@ -53,7 +53,7 @@ export const fetchAllQuestions = createAsyncThunk<
5353
}
5454
}
5555

56-
return api.getProblemsetQuestionListAll({}, total)
56+
return api.getAllQuestion()
5757
})
5858

5959
export const fetchLastACQuestions = createAsyncThunk<
@@ -70,10 +70,15 @@ export const fetchAllQuestionIds = createAsyncThunk<
7070
{ state: RootState }
7171
>('questions/fetchAllQuestionIds', async () => api.getAllQuestions())
7272

73-
const initialState = questionsAdapter.getInitialState({
73+
const initData = {
7474
total: 0,
7575
update: 0,
76-
})
76+
} as {
77+
total: number
78+
update: number
79+
} & { [key in CategorySlugType]: string[] }
80+
81+
const initialState = questionsAdapter.getInitialState(initData)
7782

7883
const questionsSlice = createSlice({
7984
name: 'posts',
@@ -82,10 +87,16 @@ const questionsSlice = createSlice({
8287
extraReducers(builder) {
8388
builder
8489
.addCase(fetchAllQuestions.fulfilled, (state, action) => {
85-
if (action.payload) {
86-
questionsAdapter.upsertMany(state, action.payload)
87-
state.total = action.payload.length
90+
const map = action.payload
91+
if (map) {
92+
const allQuestions = map.get('all-code-essentials')!
93+
questionsAdapter.upsertMany(state, allQuestions)
94+
state.total = allQuestions.length
8895
state.update = new Date().valueOf()
96+
for (const [category, questions] of map) {
97+
if (category === 'all-code-essentials') continue
98+
state[category] = questions.map(q => q.frontendQuestionId)
99+
}
89100
}
90101
})
91102
.addCase(fetchAllQuestionIds.fulfilled, (state, action) => {
@@ -154,26 +165,12 @@ export const selectQuestonsByOption = (
154165
.filter(Boolean) as EntityId[]) ?? []
155166
}
156167

157-
if (option.categorySlug) {
158-
const categorySlugs = new Set(['database', 'shell', 'concurrency'])
168+
if (option.categorySlug && option.categorySlug !== 'all-code-essentials') {
169+
const set = new Set(questions[option.categorySlug])
159170
for (const id of ids) {
160171
const question = questions.entities[id]
161-
if (!question) continue
162-
if (option.categorySlug === 'algorithms') {
163-
// 力扣没有将 「剑指」和「面试题」 的题目归为 algorithms 这个分类,
164-
// 为了保持跟力扣的一致,如果分类是 algorithms 也过滤掉这些题目
165-
if (
166-
!question.frontendQuestionId.startsWith('剑指') &&
167-
!question.frontendQuestionId.startsWith('面试题') &&
168-
question.topicTags.every(a => !categorySlugs.has(a.slug))
169-
) {
170-
res.push(question)
171-
}
172-
} else {
173-
if (question.topicTags.some(a => a.slug === option.categorySlug)) {
174-
res.push(question)
175-
}
176-
}
172+
if (!question || !set.has(question.frontendQuestionId)) continue
173+
res.push(question)
177174
}
178175
} else {
179176
res = ids.map(id => questions.entities[id]!)

src/content/utils/leetcode-api.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,14 @@ export type ProblemsetPageProps = {
532532

533533
export type CategorySlugType =
534534
| ''
535+
| 'all-code-essentials'
535536
| 'algorithms'
536537
| 'database'
537538
| 'shell'
538539
| 'concurrency'
540+
| 'pandas'
541+
| 'javascript'
542+
539543
export type ProblemsetQuestionListFilterType = {
540544
listId?: string
541545
difficulty?: 'EASY' | 'MEDIUM' | 'HARD'
@@ -1792,6 +1796,37 @@ class LeetCodeApi {
17921796
return res
17931797
}
17941798

1799+
public async getAllQuestion(): Promise<
1800+
Map<CategorySlugType, ProblemsetQuestion[]>
1801+
> {
1802+
const CategorySlugs = [
1803+
'algorithms',
1804+
'database',
1805+
'shell',
1806+
'concurrency',
1807+
'pandas',
1808+
'javascript',
1809+
] as const
1810+
const res = new Map<CategorySlugType, ProblemsetQuestion[]>([
1811+
['all-code-essentials', []],
1812+
]),
1813+
set = new Set<string>()
1814+
for (const categorySlug of CategorySlugs) {
1815+
if (!res.has(categorySlug)) res.set(categorySlug, [])
1816+
const questions = await this.getProblemsetQuestionListAll({
1817+
categorySlug,
1818+
})
1819+
for (const question of questions) {
1820+
if (!set.has(question.titleSlug)) {
1821+
res.get('all-code-essentials')!.push(question)
1822+
set.add(question.titleSlug)
1823+
}
1824+
res.get(categorySlug)!.push(question)
1825+
}
1826+
}
1827+
return res
1828+
}
1829+
17951830
/** 获取题单(收藏夹)列表,包括自己创建的和官方题单
17961831
*
17971832
*/

src/content/utils/predict.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const getERank = (seeds: number[], x: number): number => {
9090
/** 获取某个分数 x 的期望胜率
9191
*
9292
* @param seeds 种子数据
93-
* @param x
93+
* @param x 真实分数*ACCURACY
9494
* @returns
9595
*/
9696
export const getSeed = (seeds: number[], x: number): number => {

0 commit comments

Comments
 (0)