3
3
import React , { useMemo } from "react" ;
4
4
import styles from "./library-hero.module.scss" ;
5
5
import { BoxComponent } from "@/features/common/components/box/box.component" ;
6
- import { usePathname , useRouter , useSearchParams } from "next/navigation" ;
7
- import { LIBRARIES_FILTER_QUERY_PARAM_KEY } from "@/libs/config/project.constants" ;
6
+ import { usePathname , useRouter } from "next/navigation" ;
7
+ import {
8
+ LIBRARIES_FILTER_ALGORITHM_KEY ,
9
+ LIBRARIES_FILTER_PROGRAMMING_LANGUAGE_KEY ,
10
+ } from "@/libs/config/project.constants" ;
8
11
import { clsx } from "clsx" ;
9
12
import { getLocalizedSecondaryFont } from "@/libs/theme/fonts" ;
10
13
import { LibrariesDictionaryModel } from "@/features/localization/models/libraries-dictionary.model" ;
11
14
import { DebuggerPickerComponent } from "@/features/common/components/debugger-picker/debugger-picker.component" ;
15
+ import { LibraryFilterLabel } from "../../models/library-filters.model" ;
16
+ import { DebuggerPickerOptionModel } from "@/features/common/models/debugger-picker-option.model" ;
12
17
13
18
interface LibraryHeroComponentProps {
14
19
languageCode : string ;
15
20
query : string ;
16
21
categoryOptions : { id : string ; name : string } [ ] ;
22
+ algorithmOptions : { value : string ; label : string } [ ] ;
17
23
dictionary : LibrariesDictionaryModel ;
18
24
}
19
25
20
26
export const LibraryHeroComponent : React . FC < LibraryHeroComponentProps > = ( {
21
27
languageCode,
22
28
query,
23
29
categoryOptions,
30
+ algorithmOptions,
24
31
dictionary,
25
32
} ) => {
26
- const searchParams = useSearchParams ( ) ;
27
33
const pathname = usePathname ( ) ;
28
34
const { replace } = useRouter ( ) ;
29
35
30
- const handleSelection = ( selection : string ) => {
31
- const params = new URLSearchParams ( searchParams ) ;
32
-
33
- if ( selection ) {
34
- params . set ( LIBRARIES_FILTER_QUERY_PARAM_KEY , selection ) ;
35
- } else {
36
- params . delete ( LIBRARIES_FILTER_QUERY_PARAM_KEY ) ;
36
+ const handleSelection = (
37
+ selection : string ,
38
+ parentLabel ?: LibraryFilterLabel
39
+ ) => {
40
+ if ( ! parentLabel ) {
41
+ return ;
42
+ }
43
+ const params = new URLSearchParams ( "" ) ;
44
+ switch ( parentLabel ) {
45
+ case "ProgrammingLanguage" :
46
+ params . set ( LIBRARIES_FILTER_PROGRAMMING_LANGUAGE_KEY , selection ) ;
47
+ break ;
48
+ case "Algorithm" :
49
+ params . set ( LIBRARIES_FILTER_ALGORITHM_KEY , selection ) ;
50
+ break ;
51
+ default :
52
+ break ;
37
53
}
38
-
39
54
replace ( `${ pathname } ?${ params . toString ( ) } ` ) ;
40
55
} ;
41
56
42
57
const options = useMemo ( ( ) => {
43
58
return [
44
59
{
45
- value : dictionary . filterPicker . defaultValue . value ,
46
- label : dictionary . filterPicker . defaultValue . label ,
60
+ label : "ProgrammingLanguage" ,
61
+ options : [
62
+ {
63
+ value : dictionary . filterPicker . defaultValue . value ,
64
+ label : dictionary . filterPicker . defaultValue . label ,
65
+ } ,
66
+ ...categoryOptions . map ( ( categoryOption ) => {
67
+ return {
68
+ value : categoryOption . id ,
69
+ label : categoryOption . name ,
70
+ } ;
71
+ } ) ,
72
+ ] ,
73
+ } ,
74
+ {
75
+ label : "Algorithm" ,
76
+ options : [ ...algorithmOptions ] ,
47
77
} ,
48
- ...categoryOptions . map ( ( categoryOption ) => {
49
- return {
50
- value : categoryOption . id ,
51
- label : categoryOption . name ,
52
- } ;
53
- } ) ,
54
- ] ;
78
+ ] as DebuggerPickerOptionModel [ ] ;
55
79
} , [
56
80
categoryOptions ,
57
81
dictionary . filterPicker . defaultValue . label ,
58
82
dictionary . filterPicker . defaultValue . value ,
83
+ algorithmOptions
59
84
] ) ;
60
85
61
86
return (
@@ -67,7 +92,7 @@ export const LibraryHeroComponent: React.FC<LibraryHeroComponentProps> = ({
67
92
< h1
68
93
className = { clsx (
69
94
styles . heroTitle ,
70
- getLocalizedSecondaryFont ( languageCode ) ,
95
+ getLocalizedSecondaryFont ( languageCode )
71
96
) }
72
97
>
73
98
{ dictionary . title }
@@ -80,7 +105,9 @@ export const LibraryHeroComponent: React.FC<LibraryHeroComponentProps> = ({
80
105
languageCode = { languageCode }
81
106
options = { options }
82
107
selectedOptionCode = {
83
- options . filter ( ( option ) => option . value === query ) [ 0 ]
108
+ options
109
+ . flatMap ( ( group ) => group . options )
110
+ . filter ( ( option ) => option . value === query ) [ 0 ]
84
111
}
85
112
handleSelection = { handleSelection }
86
113
placeholder = { null }
0 commit comments