11import React from "react"
22import Link from "next/link"
3- import { usePathname , useRouter } from "next/navigation"
3+ import { usePathname } from "next/navigation"
44import { useAccount } from "@/lib/providers/jazz-provider"
55import { cn } from "@/lib/utils"
66import { PersonalLinkLists } from "@/lib/schema/personal-link"
77import { useQueryState , parseAsStringLiteral } from "nuqs"
88import { LEARNING_STATES } from "@/lib/constants"
99
10- export const LinkSection : React . FC < { pathname : string } > = ( { pathname } ) => {
10+ const ALL_STATES = [ { label : "All" , value : "all" , icon : "List" , className : "text-foreground" } , ...LEARNING_STATES ]
11+ const ALL_STATES_STRING = ALL_STATES . map ( ls => ls . value )
12+
13+ interface LinkSectionProps {
14+ pathname : string
15+ }
16+
17+ export const LinkSection : React . FC < LinkSectionProps > = ( { pathname } ) => {
1118 const { me } = useAccount ( {
1219 root : {
1320 personalLinks : [ ]
1421 }
1522 } )
1623
17- const linkCount = me ?. root . personalLinks ?. length || 0
18- const isActive = pathname === "/links"
19-
2024 if ( ! me ) return null
2125
26+ const linkCount = me . root . personalLinks ?. length || 0
27+ const isActive = pathname === "/links"
28+
2229 return (
2330 < div className = "group/pages flex flex-col gap-px py-2" >
2431 < LinkSectionHeader linkCount = { linkCount } isActive = { isActive } />
@@ -34,20 +41,19 @@ interface LinkSectionHeaderProps {
3441
3542const LinkSectionHeader : React . FC < LinkSectionHeaderProps > = ( { linkCount } ) => {
3643 const pathname = usePathname ( )
37- const [ state ] = useQueryState ( "state" , parseAsStringLiteral ( LEARNING_STATES . map ( ls => ls . value ) ) )
38- const isLinksActive = pathname . startsWith ( "/links" ) && ! state
44+ const [ state ] = useQueryState ( "state" , parseAsStringLiteral ( ALL_STATES_STRING ) )
45+ const isLinksActive = pathname . startsWith ( "/links" ) && ( ! state || state === "all" )
3946
4047 return (
41- < div className = "flex gap-px rounded-md" >
48+ < div
49+ className = { cn (
50+ "flex min-h-[30px] items-center gap-px rounded-md" ,
51+ isLinksActive ? "bg-accent text-accent-foreground" : "hover:bg-accent hover:text-accent-foreground"
52+ ) }
53+ >
4254 < Link
4355 href = "/links"
44- className = { cn (
45- "flex size-6 flex-1 items-center justify-start rounded-md px-2" ,
46- "focus-visible:outline-none focus-visible:ring-0" ,
47- isLinksActive
48- ? "bg-accent text-accent-foreground items-center justify-center py-3"
49- : "hover:bg-accent hover:text-accent-foreground"
50- ) }
56+ className = "flex flex-1 items-center justify-start rounded-md px-2 py-1 focus-visible:outline-none focus-visible:ring-0"
5157 >
5258 < p className = "flex w-full items-center text-xs font-medium" >
5359 Links
@@ -66,24 +72,29 @@ const List: React.FC<ListProps> = ({ personalLinks }) => {
6672 const pathname = usePathname ( )
6773 const [ state ] = useQueryState ( "state" , parseAsStringLiteral ( LEARNING_STATES . map ( ls => ls . value ) ) )
6874
69- const toLearnCount = personalLinks . filter ( link => link ?. learningState === "wantToLearn" ) . length
70- const learningCount = personalLinks . filter ( link => link ?. learningState === "learning" ) . length
71- const learnedCount = personalLinks . filter ( link => link ?. learningState === "learned" ) . length
72-
73- const isActive = ( checkState : string ) => {
74- return pathname === "/links" && state === checkState
75+ const linkCounts = {
76+ wantToLearn : personalLinks . filter ( link => link ?. learningState === "wantToLearn" ) . length ,
77+ learning : personalLinks . filter ( link => link ?. learningState === "learning" ) . length ,
78+ learned : personalLinks . filter ( link => link ?. learningState === "learned" ) . length
7579 }
7680
81+ const isActive = ( checkState : string ) => pathname === "/links" && state === checkState
82+
7783 return (
7884 < div className = "flex flex-col gap-px" >
7985 < ListItem
8086 label = "To Learn"
8187 href = "/links?state=wantToLearn"
82- count = { toLearnCount }
88+ count = { linkCounts . wantToLearn }
8389 isActive = { isActive ( "wantToLearn" ) }
8490 />
85- < ListItem label = "Learning" href = "/links?state=learning" count = { learningCount } isActive = { isActive ( "learning" ) } />
86- < ListItem label = "Learned" href = "/links?state=learned" count = { learnedCount } isActive = { isActive ( "learned" ) } />
91+ < ListItem
92+ label = "Learning"
93+ href = "/links?state=learning"
94+ count = { linkCounts . learning }
95+ isActive = { isActive ( "learning" ) }
96+ />
97+ < ListItem label = "Learned" href = "/links?state=learned" count = { linkCounts . learned } isActive = { isActive ( "learned" ) } />
8798 </ div >
8899 )
89100}
@@ -95,26 +106,23 @@ interface ListItemProps {
95106 isActive : boolean
96107}
97108
98- const ListItem : React . FC < ListItemProps > = ( { label, href, count, isActive } ) => {
99- return (
100- < div className = "group/reorder-page relative" >
101- < div className = "group/topic-link relative flex min-w-0 flex-1" >
102- < Link
103- href = { href }
104- className = { cn (
105- "relative flex h-8 w-full items-center gap-2 rounded-md p-1.5 font-medium" ,
106- isActive ? "bg-accent text-accent-foreground" : "hover:bg-accent hover:text-accent-foreground"
107- ) }
108- >
109- < div className = "flex max-w-full flex-1 items-center gap-1.5 truncate text-sm" >
110- < p className = { cn ( "truncate opacity-95 group-hover/topic-link:opacity-100" ) } > { label } </ p >
111- </ div >
112- </ Link >
113-
114- { count > 0 && (
115- < span className = "absolute right-2 top-1/2 z-[1] -translate-y-1/2 rounded p-1 text-sm" > { count } </ span >
109+ const ListItem : React . FC < ListItemProps > = ( { label, href, count, isActive } ) => (
110+ < div className = "group/reorder-page relative" >
111+ < div className = "group/topic-link relative flex min-w-0 flex-1" >
112+ < Link
113+ href = { href }
114+ className = { cn (
115+ "relative flex h-8 w-full items-center gap-2 rounded-md p-1.5 font-medium" ,
116+ isActive ? "bg-accent text-accent-foreground" : "hover:bg-accent hover:text-accent-foreground"
116117 ) }
117- </ div >
118+ >
119+ < div className = "flex max-w-full flex-1 items-center gap-1.5 truncate text-sm" >
120+ < p className = { cn ( "truncate opacity-95 group-hover/topic-link:opacity-100" ) } > { label } </ p >
121+ </ div >
122+ </ Link >
123+ { count > 0 && (
124+ < span className = "absolute right-2 top-1/2 z-[1] -translate-y-1/2 rounded p-1 text-sm" > { count } </ span >
125+ ) }
118126 </ div >
119- )
120- }
127+ </ div >
128+ )
0 commit comments