@@ -11,16 +11,39 @@ 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
+ // Generate URL based on tag state:
28
+ // - If active: remove this tag from URL
29
+ // - If inactive: add this tag to existing active tags
30
+ const remainingTags = isActive
31
+ ? activeTags . filter ( ( t ) => t !== tag )
32
+ : [ ...activeTags , tag ] ;
33
+
34
+ const searchParams = new URLSearchParams ( ) ;
35
+ if ( remainingTags . length ) {
36
+ remainingTags . forEach ( ( tag ) => {
37
+ searchParams . append ( "tag" , encodeURIComponent ( tag ) ) ;
38
+ } ) ;
39
+ }
40
+ const href = remainingTags . length ? `/?${ searchParams . toString ( ) } ` : "/" ;
41
+
19
42
return (
20
43
< Link
21
44
{ ...props }
22
45
className = { cn ( styles . tag , className , isActive && styles . active ) }
23
- href = { isActive ? "/" : `/?tag= ${ encodeURIComponent ( tag ) } ` }
46
+ href = { href }
24
47
>
25
48
< Icon className = { cn ( styles . icon ) } />
26
49
< span className = { styles . label } > { tag } </ span >
@@ -30,17 +53,23 @@ export function Tag({ tag, isActive, className, ...props }: TagProps) {
30
53
31
54
interface TagsProps {
32
55
tags : string [ ] ;
33
- activeTag ? : string ;
56
+ activeTags : string [ ] ;
34
57
className ?: string ;
35
58
}
36
59
37
- export function Tags ( { tags, activeTag , className } : TagsProps ) {
60
+ export function Tags ( { tags, activeTags , className } : TagsProps ) {
38
61
const label = getLabel ( "filterByTag" ) ;
39
62
return (
40
63
< div className = { cn ( styles . tags , className ) } >
41
64
{ ! ! label && < h3 > { label } </ h3 > }
42
65
{ tags . map ( ( tag ) => (
43
- < Tag key = { tag } tag = { tag } isActive = { activeTag == tag } scroll = { false } />
66
+ < Tag
67
+ key = { tag }
68
+ tag = { tag }
69
+ isActive = { activeTags . includes ( tag ) }
70
+ activeTags = { activeTags }
71
+ scroll = { false }
72
+ />
44
73
) ) }
45
74
</ div >
46
75
) ;
0 commit comments