@@ -11,16 +11,34 @@ import { cn } from "@/lib/utils";
11
11
type TagProps = {
12
12
tag : string ;
13
13
isActive ?: boolean ;
14
+ activeTags ?: string [ ] ;
14
15
} & Omit < LinkProps , "href" > &
15
16
ComponentPropsWithoutRef < "a" > ;
16
17
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 ) {
18
25
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
+
19
37
return (
20
38
< Link
21
39
{ ...props }
22
40
className = { cn ( styles . tag , className , isActive && styles . active ) }
23
- href = { isActive ? "/" : `/?tag= ${ encodeURIComponent ( tag ) } ` }
41
+ href = { href }
24
42
>
25
43
< Icon className = { cn ( styles . icon ) } />
26
44
< span className = { styles . label } > { tag } </ span >
@@ -30,17 +48,23 @@ export function Tag({ tag, isActive, className, ...props }: TagProps) {
30
48
31
49
interface TagsProps {
32
50
tags : string [ ] ;
33
- activeTag ? : string ;
51
+ activeTags : string [ ] ;
34
52
className ?: string ;
35
53
}
36
54
37
- export function Tags ( { tags, activeTag , className } : TagsProps ) {
55
+ export function Tags ( { tags, activeTags , className } : TagsProps ) {
38
56
const label = getLabel ( "filterByTag" ) ;
39
57
return (
40
58
< div className = { cn ( styles . tags , className ) } >
41
59
{ ! ! label && < h3 > { label } </ h3 > }
42
60
{ 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
+ />
44
68
) ) }
45
69
</ div >
46
70
) ;
0 commit comments