@@ -23,7 +23,7 @@ const ChallengeListPage = () => {
23
23
const { t } = useTranslation ( ) ;
24
24
const isMobile = useMediaQuery ( { maxWidth : 768 } ) ;
25
25
const [ filters , setFilters ] = useState ( {
26
- difficulty : 'all' ,
26
+ difficulty : [ ] as string [ ] ,
27
27
tags : [ ] as string [ ] ,
28
28
platform : 'all'
29
29
} ) ;
@@ -64,8 +64,10 @@ const ChallengeListPage = () => {
64
64
const newSearchParams = new URLSearchParams ( searchParams ) ;
65
65
66
66
// 设置难度
67
- if ( parsedFilters . difficulty && parsedFilters . difficulty !== 'all' ) {
68
- newSearchParams . set ( 'difficulty' , parsedFilters . difficulty ) ;
67
+ if ( parsedFilters . difficulty && parsedFilters . difficulty . length > 0 ) {
68
+ parsedFilters . difficulty . forEach ( ( difficulty : string ) => {
69
+ newSearchParams . append ( 'difficulty' , difficulty ) ;
70
+ } ) ;
69
71
}
70
72
71
73
// 设置平台
@@ -120,11 +122,33 @@ const ChallengeListPage = () => {
120
122
// 处理难度点击
121
123
const handleDifficultyClick = ( difficulty : string ) => {
122
124
const newSearchParams = new URLSearchParams ( searchParams ) ;
123
- newSearchParams . set ( 'difficulty' , difficulty ) ;
125
+ const currentDifficulties = filters . difficulty ;
126
+
127
+ // 检查是否已选中该难度
128
+ const isSelected = currentDifficulties . includes ( difficulty ) ;
129
+
130
+ if ( difficulty === 'all' ) {
131
+ // 如果选择"全部",则清除所有难度筛选
132
+ newSearchParams . delete ( 'difficulty' ) ;
133
+ filters . difficulty = [ ] ;
134
+ } else if ( isSelected ) {
135
+ // 如果已选中,则取消选中
136
+ const newDifficulties = currentDifficulties . filter ( d => d !== difficulty ) ;
137
+ newSearchParams . delete ( 'difficulty' ) ;
138
+ newDifficulties . forEach ( d => newSearchParams . append ( 'difficulty' , d ) ) ;
139
+ filters . difficulty = newDifficulties ;
140
+ } else {
141
+ // 如果未选中,则添加选中
142
+ const newDifficulties = [ ...currentDifficulties , difficulty ] ;
143
+ newSearchParams . delete ( 'difficulty' ) ;
144
+ newDifficulties . forEach ( d => newSearchParams . append ( 'difficulty' , d ) ) ;
145
+ filters . difficulty = newDifficulties ;
146
+ }
147
+
124
148
navigate ( `/challenges?${ newSearchParams . toString ( ) } ` ) ;
125
149
126
150
// 保存筛选设置到本地存储
127
- saveFilterPreferences ( { ...filters , difficulty } ) ;
151
+ saveFilterPreferences ( { ...filters , difficulty : filters . difficulty } ) ;
128
152
} ;
129
153
130
154
// 处理平台筛选
@@ -183,10 +207,22 @@ const ChallengeListPage = () => {
183
207
// 从URL同步过滤器状态
184
208
useEffect ( ( ) => {
185
209
const tags = searchParams . getAll ( 'tags' ) ;
186
- const difficulty = searchParams . get ( 'difficulty' ) || 'all' ;
210
+ let difficultyParams = searchParams . getAll ( 'difficulty' ) ;
187
211
const platform = searchParams . get ( 'platform' ) || 'all' ;
188
212
189
- const newFilters = { tags, difficulty, platform } ;
213
+ // 处理特殊难度字符串转换
214
+ if ( difficultyParams . length === 1 ) {
215
+ const diffParam = difficultyParams [ 0 ] ;
216
+ if ( diffParam === 'easy' ) {
217
+ difficultyParams = [ '1' , '2' ] ;
218
+ } else if ( diffParam === 'medium' ) {
219
+ difficultyParams = [ '3' , '4' ] ;
220
+ } else if ( diffParam === 'hard' ) {
221
+ difficultyParams = [ '5' ] ;
222
+ }
223
+ }
224
+
225
+ const newFilters = { tags, difficulty : difficultyParams , platform } ;
190
226
setFilters ( newFilters ) ;
191
227
192
228
// 当URL发生变化时,同步到本地存储
@@ -218,7 +254,7 @@ const ChallengeListPage = () => {
218
254
// 使用搜索服务过滤
219
255
const filtered = searchService . filterChallenges ( visibleChallenges , {
220
256
tags : filters . tags ,
221
- difficulty : filters . difficulty ,
257
+ difficulty : filters . difficulty . join ( ',' ) ,
222
258
platform : filters . platform ,
223
259
query : searchQuery
224
260
} ) ;
@@ -263,8 +299,17 @@ const ChallengeListPage = () => {
263
299
newTags . forEach ( t => newSearchParams . append ( 'tags' , t ) ) ;
264
300
newFilters = { ...newFilters , tags : newTags } ;
265
301
} else if ( type === 'difficulty' ) {
266
- newSearchParams . delete ( 'difficulty' ) ;
267
- newFilters = { ...newFilters , difficulty : 'all' } ;
302
+ if ( value ) {
303
+ // 仅移除单个难度
304
+ const newDifficulties = filters . difficulty . filter ( d => d !== value ) ;
305
+ newSearchParams . delete ( 'difficulty' ) ;
306
+ newDifficulties . forEach ( d => newSearchParams . append ( 'difficulty' , d ) ) ;
307
+ newFilters = { ...newFilters , difficulty : newDifficulties } ;
308
+ } else {
309
+ // 移除所有难度
310
+ newSearchParams . delete ( 'difficulty' ) ;
311
+ newFilters = { ...newFilters , difficulty : [ ] } ;
312
+ }
268
313
} else if ( type === 'platform' ) {
269
314
newSearchParams . delete ( 'platform' ) ;
270
315
newFilters = { ...newFilters , platform : 'all' } ;
@@ -278,7 +323,7 @@ const ChallengeListPage = () => {
278
323
const handleClearAllFilters = ( ) => {
279
324
const newSearchParams = new URLSearchParams ( ) ;
280
325
setSearchParams ( newSearchParams ) ;
281
- const newFilters = { tags : [ ] , difficulty : 'all' , platform : 'all' } ;
326
+ const newFilters = { tags : [ ] , difficulty : [ ] , platform : 'all' } ;
282
327
setFilters ( newFilters ) ;
283
328
saveFilterPreferences ( newFilters ) ;
284
329
} ;
@@ -295,7 +340,7 @@ const ChallengeListPage = () => {
295
340
// 获取应用的过滤器数量
296
341
const getAppliedFiltersCount = ( ) => {
297
342
let count = 0 ;
298
- if ( filters . difficulty !== 'all' ) count ++ ;
343
+ if ( filters . difficulty . length > 0 ) count ++ ;
299
344
if ( filters . platform !== 'all' ) count ++ ;
300
345
count += filters . tags . length ;
301
346
return count ;
@@ -325,7 +370,7 @@ const ChallengeListPage = () => {
325
370
selectedTags = { filters . tags }
326
371
selectedDifficulty = { filters . difficulty }
327
372
selectedPlatform = { filters . platform }
328
- hasFilters = { filters . tags . length > 0 || filters . difficulty !== 'all' || filters . platform !== 'all' }
373
+ hasFilters = { filters . tags . length > 0 || filters . difficulty . length > 0 || filters . platform !== 'all' }
329
374
onRemoveTag = { ( tag ) => handleFilterRemove ( 'tag' , tag ) }
330
375
onRemoveDifficulty = { ( ) => handleFilterRemove ( 'difficulty' ) }
331
376
onRemovePlatform = { ( ) => handleFilterRemove ( 'platform' ) }
0 commit comments