@@ -11,16 +11,34 @@ import { cn } from "@/lib/utils";
1111type TagProps = {
1212 tag : string ;
1313 isActive ?: boolean ;
14+ activeTags ?: string [ ] ;
1415} & Omit < LinkProps , "href" > &
1516 ComponentPropsWithoutRef < "a" > ;
1617
17- export function Tag ( { tag, isActive, className, ...props } : TagProps ) {
18+ export function Tag ( {
19+ tag,
20+ isActive,
21+ activeTags = [ ] ,
22+ className,
23+ ...props
24+ } : TagProps ) {
1825 const Icon = isActive ? IconRemove : IconTag ;
26+
27+ // Update URL based on tag state
28+ const remainingTags = isActive
29+ ? activeTags . filter ( ( t ) => t !== tag )
30+ : [ ...activeTags , tag ] ;
31+
32+ const searchParams = new URLSearchParams (
33+ remainingTags . map ( ( tag ) => [ "tag" , encodeURIComponent ( tag ) ] ) ,
34+ ) ;
35+ const href = searchParams . toString ( ) ? `/?${ searchParams } ` : "/" ;
36+
1937 return (
2038 < Link
2139 { ...props }
2240 className = { cn ( styles . tag , className , isActive && styles . active ) }
23- href = { isActive ? "/" : `/?tag= ${ encodeURIComponent ( tag ) } ` }
41+ href = { href }
2442 >
2543 < Icon className = { cn ( styles . icon ) } />
2644 < span className = { styles . label } > { tag } </ span >
@@ -30,17 +48,23 @@ export function Tag({ tag, isActive, className, ...props }: TagProps) {
3048
3149interface TagsProps {
3250 tags : string [ ] ;
33- activeTag ? : string ;
51+ activeTags : string [ ] ;
3452 className ?: string ;
3553}
3654
37- export function Tags ( { tags, activeTag , className } : TagsProps ) {
55+ export function Tags ( { tags, activeTags , className } : TagsProps ) {
3856 const label = getLabel ( "filterByTag" ) ;
3957 return (
4058 < div className = { cn ( styles . tags , className ) } >
4159 { ! ! label && < h3 > { label } </ h3 > }
4260 { tags . map ( ( tag ) => (
43- < Tag key = { tag } tag = { tag } isActive = { activeTag == tag } scroll = { false } />
61+ < Tag
62+ key = { tag }
63+ tag = { tag }
64+ isActive = { activeTags . includes ( tag ) }
65+ activeTags = { activeTags }
66+ scroll = { false }
67+ />
4468 ) ) }
4569 </ div >
4670 ) ;
0 commit comments