@@ -34,6 +34,22 @@ function SearchBox(props: UseSearchBoxProps) {
3434 }
3535 } , [ ] ) ;
3636
37+ useEffect ( ( ) => {
38+ const params = new URLSearchParams ( window . location . search ) ;
39+
40+ if ( query ) {
41+ params . set ( "q" , query ) ;
42+ } else {
43+ params . delete ( "q" ) ;
44+ }
45+
46+ history . pushState (
47+ null ,
48+ "" ,
49+ `${ window . location . pathname } ?${ params . toString ( ) } ` ,
50+ ) ;
51+ } , [ query ] ) ;
52+
3753 return (
3854 < div className = "border-cl1-gray-8 dark:border-cl1-gray-2 flex items-center rounded-sm border p-2" >
3955 < input
@@ -90,12 +106,18 @@ function InfiniteHits(props: UseInfiniteHitsProps) {
90106function FilterDropdown ( {
91107 attribute,
92108 label,
109+ limit = 1000 ,
93110} : {
94111 attribute : string ;
95112 label : string ;
113+ limit ?: number ;
96114} ) {
97115 const [ isOpen , setIsOpen ] = useState ( false ) ;
98- const { items, refine } = useRefinementList ( { attribute } ) ;
116+ const { items, refine } = useRefinementList ( {
117+ attribute,
118+ limit,
119+ sortBy : [ "count:desc" ] ,
120+ } ) ;
99121
100122 useEffect ( ( ) => {
101123 const params = new URLSearchParams ( window . location . search ) ;
@@ -112,12 +134,19 @@ function FilterDropdown({
112134 const refined = items
113135 . filter ( ( item ) => item . isRefined )
114136 . map ( ( item ) => item . value ) ;
115- if ( refined . length === 0 ) return ;
137+
138+ const params = new URLSearchParams ( window . location . search ) ;
139+
140+ if ( refined . length === 0 ) {
141+ params . delete ( attribute ) ;
142+ } else {
143+ params . set ( attribute , refined . join ( "," ) ) ;
144+ }
116145
117146 history . pushState (
118147 null ,
119148 "" ,
120- `${ window . location . pathname } ?${ attribute } = ${ refined . join ( "," ) } ` ,
149+ `${ window . location . pathname } ?${ params . toString ( ) } ` ,
121150 ) ;
122151 } , [ items ] ) ;
123152
@@ -160,21 +189,28 @@ function FilterDropdown({
160189 className = "border-cl1-gray-8 bg-cl1-white dark:border-cl1-gray-1 dark:bg-cl1-gray-0 rounded-sm border p-4 shadow-md"
161190 >
162191 < div className = "max-h-60 space-y-2 overflow-y-auto" >
163- { items . map ( ( item ) => (
164- < label
165- key = { item . value }
166- className = "flex items-center gap-2 text-sm"
167- >
168- < input
169- type = "checkbox"
170- checked = { item . isRefined }
171- onChange = { ( ) => refine ( item . value ) }
172- />
173- < span >
174- { item . label } ({ item . count } )
175- </ span >
176- </ label >
177- ) ) }
192+ { items
193+ . sort ( ( a , b ) => {
194+ if ( a . isRefined && ! b . isRefined ) return - 1 ;
195+ if ( ! a . isRefined && b . isRefined ) return 1 ;
196+ return b . count - a . count ;
197+ } )
198+ . map ( ( item ) => (
199+ < label
200+ key = { item . value }
201+ className = "flex items-center gap-2 text-sm"
202+ >
203+ < input
204+ type = "checkbox"
205+ className = "bg-transparent"
206+ checked = { item . isRefined }
207+ onChange = { ( ) => refine ( item . value ) }
208+ />
209+ < span >
210+ { item . label } ({ item . count } )
211+ </ span >
212+ </ label >
213+ ) ) }
178214 </ div >
179215 </ div >
180216 </ FloatingPortal >
@@ -198,8 +234,9 @@ export default function InstantSearchComponent() {
198234 < Configure filters = "type:content" />
199235 < div className = "space-y-4" >
200236 < SearchBox />
201- < div className = "flex gap-2" >
237+ < div className = "not-content flex gap-2" >
202238 < FilterDropdown attribute = "product" label = "Products" />
239+ < FilterDropdown attribute = "tags" label = "Tags" />
203240 </ div >
204241 < InfiniteHits />
205242 </ div >
0 commit comments