11import type { Metadata } from 'next' ;
22import Link from 'next/link' ;
3- import { format , isSameYear } from 'date-fns' ;
43
4+ import ArrowCard from '@/components/arrow-card' ;
55import client from '@/tina/__generated__/client' ;
6+ import type { PostConnectionQuery } from '@/tina/__generated__/types' ;
67
78export const revalidate = 3600 ; // invalidate every hour
89
910export const metadata : Metadata = {
10- title : '博客 ' ,
11+ title : '文章 ' ,
1112} ;
1213
1314export default async function Posts ( ) {
@@ -17,40 +18,40 @@ export default async function Posts() {
1718 filter : { draft : { eq : false } } ,
1819 } ) ;
1920
21+ const posts = data . postConnection . edges ?. reduce ( ( acc , post ) => {
22+ const year = new Date ( post ?. node ?. date ! ) . getFullYear ( ) . toString ( ) ;
23+
24+ acc [ year ] ??= [ ] ;
25+ acc [ year ] ?. push ( post ) ;
26+
27+ return acc ;
28+ } , { } as Record < string , PostConnectionQuery [ 'postConnection' ] [ 'edges' ] > ) ;
29+
30+ const years = Object . keys ( posts ?? { } ) . sort ( ( a , b ) => parseInt ( b ) - parseInt ( a ) ) ;
31+
2032 return (
21- < div >
22- < ul className = "my-6 ml-6 list-disc [&>li]:mt-2" >
23- { data . postConnection . edges ?. map ( ( post ) => {
24- const date = new Date ( post ?. node ?. date ! ) ;
25-
26- return (
27- < li key = { post ?. node ?. id } >
28- < div className = "flex flex-col sm:flex-row sm:gap-2" >
29- < div className = "flex-1" >
30- < Link
31- href = { `/posts/${ post ?. node ?. _sys . breadcrumbs . join ( '/' ) } ` }
32- className = "transition-[background-size] duration-300
33- bg-gradient-to-r bg-left-bottom bg-no-repeat
34- bg-[length:0%_55%] hover:bg-[length:100%_55%] dark:bg-[length:0%_2px] hover:dark:bg-[length:100%_2px]
35- from-primary-blue to-primary-blue dark:from-primary-blue dark:to-primary-blue"
36- >
37- { post ?. node ?. title }
38- </ Link >
39- </ div >
40-
41- < div className = "pt-1 italic text-sm text-text-muted" >
42- < time dateTime = { post ?. node ?. date } >
43- { format (
44- date ,
45- isSameYear ( new Date ( ) , date ) ? 'MM-dd' : 'yyyy-MM-dd'
46- ) }
47- </ time >
48- </ div >
49- </ div >
50- </ li >
51- ) ;
52- } ) }
53- </ ul >
33+ < div className = "mx-auto max-w-screen-md px-5" >
34+ < div className = "space-y-10" >
35+ < div className = "font-semibold text-black dark:text-white" > 文章</ div >
36+
37+ < div className = "space-y-4" >
38+ { years . map ( ( year ) => (
39+ < section className = "space-y-4" key = { year } >
40+ < div className = "font-semibold text-black dark:text-white" > { year } </ div >
41+
42+ < ul className = "flex flex-col gap-4" >
43+ { posts ?. [ year ] ?. map ( ( post ) => (
44+ < li key = { post ?. node ?. id } >
45+ < Link href = { `/posts/${ post ?. node ?. _sys . breadcrumbs . join ( '/' ) } ` } >
46+ < ArrowCard title = { post ?. node ?. title } />
47+ </ Link >
48+ </ li >
49+ ) ) }
50+ </ ul >
51+ </ section >
52+ ) ) }
53+ </ div >
54+ </ div >
5455 </ div >
5556 ) ;
5657}
0 commit comments