11import classNames from 'classnames' ;
22import { NextPage } from 'next' ;
33
4- import useTagHandling from '@/hooks/useTagHandling ' ;
4+ import useFilterHandling from '@/hooks/useFilterHandling ' ;
55
66import { RepoModal } from '@/components/dialog/RepoModal' ;
77import { NoPrefetchLink } from '@/components/links/CustomLink' ;
8+ import RankLink from '@/components/links/rankLink' ;
89import TagLink from '@/components/links/TagLink' ;
910
11+ import { indexRankBy } from '@/utils/constants' ;
12+
1013type Props = {
1114 t : ( key : string ) => string ;
1215 i18n_lang : string ;
13- tid : string ;
14- sort_by : string ;
16+ tid ?: string ;
17+ sortBy : string ;
18+ rankBy : string ;
19+ year ?: number ;
20+ month ?: number ;
1521} ;
1622
17- const IndexBar : NextPage < Props > = ( { t, i18n_lang, tid, sort_by } ) => {
18- const { labelStatus, tagItems, featuredURL, allURL, handleTagButton } =
19- useTagHandling ( tid , sort_by , i18n_lang ) ;
23+ const IndexBar : NextPage < Props > = ( {
24+ t,
25+ i18n_lang,
26+ tid,
27+ sortBy,
28+ rankBy,
29+ year,
30+ month,
31+ } ) => {
32+ const defaultURL = '/?sort_by=featured' ;
33+
34+ const {
35+ tagLabelStatus,
36+ tagItems,
37+ featuredURL,
38+ allURL,
39+ handleTagButton,
40+ monthlyURL,
41+ yearlyURL,
42+ rankItems,
43+ } = useFilterHandling ( tid , sortBy , rankBy , i18n_lang , year , month ) ;
44+
45+ const getSortLinkClassName = ( sortName : string , isMobile = false ) => {
46+ const isActive = sortBy === sortName ;
2047
21- const linkClassName = ( sortName : string ) =>
48+ if ( isMobile ) {
49+ return classNames (
50+ 'flex h-8 items-center rounded-lg pl-3 pr-3 text-sm font-bold' ,
51+ 'hover:bg-gray-100 hover:text-blue-500 dark:hover:bg-gray-700' ,
52+ {
53+ 'text-gray-500 dark:text-gray-200' : ! isActive ,
54+ 'bg-gray-100 dark:bg-gray-700 text-blue-500' : isActive ,
55+ 'lg:hidden' : sortName === 'label' ,
56+ }
57+ ) ;
58+ } else {
59+ return isActive ? 'text-blue-500' : 'text-gray-500 dark:text-gray-300' ;
60+ }
61+ } ;
62+
63+ const rankLinkClassName = ( rankName : string ) =>
2264 classNames (
23- 'flex h-8 items-center whitespace-nowrap rounded-lg pl-3 pr-3 text-sm font-bold hover:bg-gray-100 hover:text-blue-500 dark:hover:bg-gray-700' ,
65+ 'flex h-8 items-center rounded-lg pl-3 pr-3 hover:bg-gray-100 hover:text-blue-500 dark:hover:bg-gray-700' ,
2466 {
25- 'text-gray-500 dark:text-gray-200' : sort_by !== sortName ,
26- 'bg-gray-100 dark:bg-gray-700 text-blue-500' : sort_by === sortName ,
27- 'lg:hidden' : sortName === 'label' ,
67+ 'text-gray-500 dark:text-gray-200' : rankBy !== rankName ,
68+ 'bg-gray-100 dark:bg-gray-700 text-blue-500' : rankBy === rankName ,
2869 }
2970 ) ;
3071
3172 return (
32- < div className = 'relative my-2 overflow-hidden bg-white dark:bg-gray-800 md:rounded-lg' >
33- < div className = 'flex h-12 shrink grow items-center justify-start space-x-1 py-2 px-4 md:space-x-2' >
34- < NoPrefetchLink href = { featuredURL } >
35- < a className = { linkClassName ( 'featured' ) } > { t ( 'nav.featured' ) } </ a >
36- </ NoPrefetchLink >
37- < NoPrefetchLink href = { allURL } >
38- < a className = { linkClassName ( 'all' ) } > { t ( 'nav.all' ) } </ a >
39- </ NoPrefetchLink >
40- < span onClick = { handleTagButton } className = { linkClassName ( 'label' ) } >
41- { t ( 'nav.tag' ) }
42- </ span >
43- < div className = 'shrink grow' />
73+ < div className = 'relative my-2 overflow-hidden bg-white dark:bg-gray-800' >
74+ < div className = 'flex h-12 items-center space-x-1 py-2 px-2 md:space-x-2 md:rounded-lg lg:px-4' >
75+ < div className = 'hidden space-x-2 text-sm font-bold md:flex' >
76+ { indexRankBy . map ( ( rank ) => (
77+ < NoPrefetchLink
78+ key = { rank }
79+ href = {
80+ rank === 'newest'
81+ ? defaultURL
82+ : rank === 'monthly'
83+ ? monthlyURL
84+ : yearlyURL
85+ }
86+ >
87+ < a className = { rankLinkClassName ( rank ) } > { t ( `nav.${ rank } ` ) } </ a >
88+ </ NoPrefetchLink >
89+ ) ) }
90+ </ div >
91+ < div className = 'hidden shrink grow md:block' />
92+ < div className = 'hidden gap-2.5 text-[13px] font-medium md:flex' >
93+ < NoPrefetchLink href = { featuredURL } >
94+ < a className = { getSortLinkClassName ( 'featured' ) } >
95+ { t ( 'nav.featured' ) }
96+ </ a >
97+ </ NoPrefetchLink >
98+ < span className = 'border-r border-gray-100 dark:border-gray-700' > </ span >
99+ < NoPrefetchLink href = { allURL } >
100+ < a className = { getSortLinkClassName ( 'all' ) } > { t ( 'nav.all' ) } </ a >
101+ </ NoPrefetchLink >
102+ </ div >
103+ { /* 移动端 */ }
104+ < div className = 'flex md:hidden' >
105+ < NoPrefetchLink href = { featuredURL } >
106+ < a className = { getSortLinkClassName ( 'featured' , true ) } >
107+ { t ( 'nav.featured' ) }
108+ </ a >
109+ </ NoPrefetchLink >
110+ < NoPrefetchLink href = { allURL } >
111+ < a className = { getSortLinkClassName ( 'all' , true ) } > { t ( 'nav.all' ) } </ a >
112+ </ NoPrefetchLink >
113+ < span
114+ onClick = { handleTagButton }
115+ className = { getSortLinkClassName ( 'label' , true ) }
116+ >
117+ { t ( 'nav.tag' ) }
118+ </ span >
119+ </ div >
120+ < div className = 'shrink grow md:hidden' />
44121 < div className = 'md:hidden' >
45122 < RepoModal >
46123 < div className = 'flex h-8 items-center rounded-lg bg-blue-500 px-3 text-xs text-white active:bg-blue-600 dark:bg-gray-700 dark:text-gray-300 dark:active:bg-gray-900 sm:px-4' >
@@ -49,9 +126,26 @@ const IndexBar: NextPage<Props> = ({ t, i18n_lang, tid, sort_by }) => {
49126 </ RepoModal >
50127 </ div >
51128 </ div >
52- < div className = { labelStatus ? 'flex px-4 pb-2.5 lg:hidden' : 'hidden' } >
53- < TagLink items = { tagItems } tid = { tid } sort_by = { sort_by } />
54- </ div >
129+
130+ { /* 移动端标签 */ }
131+ { tagLabelStatus && (
132+ < div className = 'flex px-4 pb-2.5 lg:hidden' >
133+ < TagLink items = { tagItems } tid = { tid } sort_by = { sortBy } />
134+ </ div >
135+ ) }
136+ { /* 排行榜 */ }
137+ { rankItems . length > 0 && (
138+ < div className = 'hidden px-4 pb-2 md:flex' >
139+ < RankLink
140+ items = { rankItems }
141+ tid = { tid }
142+ sort_by = { sortBy }
143+ rank_by = { rankBy }
144+ year = { year }
145+ month = { month }
146+ />
147+ </ div >
148+ ) }
55149 </ div >
56150 ) ;
57151} ;
0 commit comments