33import React , { useMemo } from "react" ;
44import styles from "./library-hero.module.scss" ;
55import { 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" ;
811import { clsx } from "clsx" ;
912import { getLocalizedSecondaryFont } from "@/libs/theme/fonts" ;
1013import { LibrariesDictionaryModel } from "@/features/localization/models/libraries-dictionary.model" ;
1114import { 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" ;
1217
1318interface LibraryHeroComponentProps {
1419 languageCode : string ;
1520 query : string ;
1621 categoryOptions : { id : string ; name : string } [ ] ;
22+ algorithmOptions : { value : string ; label : string } [ ] ;
1723 dictionary : LibrariesDictionaryModel ;
1824}
1925
2026export const LibraryHeroComponent : React . FC < LibraryHeroComponentProps > = ( {
2127 languageCode,
2228 query,
2329 categoryOptions,
30+ algorithmOptions,
2431 dictionary,
2532} ) => {
26- const searchParams = useSearchParams ( ) ;
2733 const pathname = usePathname ( ) ;
2834 const { replace } = useRouter ( ) ;
2935
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 ;
3753 }
38-
3954 replace ( `${ pathname } ?${ params . toString ( ) } ` ) ;
4055 } ;
4156
4257 const options = useMemo ( ( ) => {
4358 return [
4459 {
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 ] ,
4777 } ,
48- ...categoryOptions . map ( ( categoryOption ) => {
49- return {
50- value : categoryOption . id ,
51- label : categoryOption . name ,
52- } ;
53- } ) ,
54- ] ;
78+ ] as DebuggerPickerOptionModel [ ] ;
5579 } , [
5680 categoryOptions ,
5781 dictionary . filterPicker . defaultValue . label ,
5882 dictionary . filterPicker . defaultValue . value ,
83+ algorithmOptions
5984 ] ) ;
6085
6186 return (
@@ -67,7 +92,7 @@ export const LibraryHeroComponent: React.FC<LibraryHeroComponentProps> = ({
6792 < h1
6893 className = { clsx (
6994 styles . heroTitle ,
70- getLocalizedSecondaryFont ( languageCode ) ,
95+ getLocalizedSecondaryFont ( languageCode )
7196 ) }
7297 >
7398 { dictionary . title }
@@ -80,7 +105,9 @@ export const LibraryHeroComponent: React.FC<LibraryHeroComponentProps> = ({
80105 languageCode = { languageCode }
81106 options = { options }
82107 selectedOptionCode = {
83- options . filter ( ( option ) => option . value === query ) [ 0 ]
108+ options
109+ . flatMap ( ( group ) => group . options )
110+ . filter ( ( option ) => option . value === query ) [ 0 ]
84111 }
85112 handleSelection = { handleSelection }
86113 placeholder = { null }
0 commit comments