11import styles from "./styles.module.css"
2- import { useState , useEffect , useCallback } from "react"
2+ import { useState , useEffect } from "react"
33import type { ChangelogItem } from "~/components/ChangelogSnippet/types"
44import { matchesFilters } from "~/utils/changelogFilters"
5+ import { parseURLParams , updateFilterURL , toggleItemInArray } from "~/utils/changelogFilterUtils"
56import { DesktopFilters } from "./DesktopFilters"
67import { MobileFilters } from "./MobileFilters"
78
@@ -21,58 +22,20 @@ export const ChangelogFilters = ({ products, networks, types, items }: Changelog
2122
2223 // Read URL parameters on mount
2324 useEffect ( ( ) => {
24- if ( typeof window === "undefined" ) return
25-
26- const params = new URLSearchParams ( window . location . search )
27- const productParam = params . get ( "product" )
28- const networkParam = params . get ( "network" )
29- const typeParam = params . get ( "type" )
30- const searchParam = params . get ( "*" )
25+ const urlParams = parseURLParams ( )
3126
32- if ( productParam ) {
33- setSelectedProducts ( productParam . split ( "," ) )
34- }
35- if ( networkParam ) {
36- setSelectedNetworks ( networkParam . split ( "," ) )
37- }
38- if ( typeParam ) {
39- setSelectedTypes ( typeParam . split ( "," ) )
40- }
41- if ( searchParam ) {
42- setSearchTerm ( searchParam )
43- setSearchExpanded ( true )
44- }
27+ setSelectedProducts ( urlParams . products )
28+ setSelectedNetworks ( urlParams . networks )
29+ setSelectedTypes ( urlParams . types )
30+ setSearchTerm ( urlParams . searchTerm )
31+ setSearchExpanded ( urlParams . searchExpanded )
4532 } , [ ] )
4633
4734
48- // Update URL whenever filters change
49- const updateURL = useCallback ( ( products : string [ ] , networks : string [ ] , types : string [ ] , search : string ) => {
50- if ( typeof window === "undefined" ) return
51-
52- const params = new URLSearchParams ( )
53-
54- if ( search ) {
55- params . set ( "*" , search )
56- } else {
57- if ( products . length > 0 ) {
58- params . set ( "product" , products . join ( "," ) )
59- }
60- if ( networks . length > 0 ) {
61- params . set ( "network" , networks . join ( "," ) )
62- }
63- if ( types . length > 0 ) {
64- params . set ( "type" , types . join ( "," ) )
65- }
66- }
67-
68- const newURL = params . toString ( ) ? `?${ params . toString ( ) } ` : window . location . pathname
69- window . history . replaceState ( { } , "" , newURL )
70- } , [ ] )
71-
7235 // Update URL when filters change
7336 useEffect ( ( ) => {
74- updateURL ( selectedProducts , selectedNetworks , selectedTypes , searchTerm )
75- } , [ selectedProducts , selectedNetworks , selectedTypes , searchTerm , updateURL ] )
37+ updateFilterURL ( selectedProducts , selectedNetworks , selectedTypes , searchTerm )
38+ } , [ selectedProducts , selectedNetworks , selectedTypes , searchTerm ] )
7639
7740 // Filter items and update the display
7841 useEffect ( ( ) => {
@@ -186,13 +149,13 @@ export const ChangelogFilters = ({ products, networks, types, items }: Changelog
186149 const toggleSelection = ( type : "product" | "network" | "type" , value : string ) => {
187150 switch ( type ) {
188151 case "product" :
189- setSelectedProducts ( ( prev ) => ( prev . includes ( value ) ? prev . filter ( ( p ) => p !== value ) : [ ... prev , value ] ) )
152+ setSelectedProducts ( ( prev ) => toggleItemInArray ( prev , value ) )
190153 break
191154 case "network" :
192- setSelectedNetworks ( ( prev ) => ( prev . includes ( value ) ? prev . filter ( ( n ) => n !== value ) : [ ... prev , value ] ) )
155+ setSelectedNetworks ( ( prev ) => toggleItemInArray ( prev , value ) )
193156 break
194157 case "type" :
195- setSelectedTypes ( ( prev ) => ( prev . includes ( value ) ? prev . filter ( ( t ) => t !== value ) : [ ... prev , value ] ) )
158+ setSelectedTypes ( ( prev ) => toggleItemInArray ( prev , value ) )
196159 break
197160 }
198161 }
0 commit comments