www/components/recent-posts.tsx

51 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2022-04-27 21:55:18 -04:00
import Link from "next/link";
import { toRelativeDate } from "../lib/date";
2022-04-27 21:55:18 -04:00
import style from '../styles/recent-posts.module.css';
import PostsInfo from '../public/posts.json';
2022-04-27 21:55:18 -04:00
function PostBlock({ slug, otime, title }: { slug: string, otime: string, title: string }) {
return (
<div className={style.block}>
<span className={style.postDate}>
{toRelativeDate(new Date(otime))}
</span>
<div className={style.postTitle}>
<Link href={`/posts/${slug}`}>
{title}
</Link>
</div>
</div>
);
}
function RecentPosts() {
const posts = Object.entries(PostsInfo).reverse();
if (!posts.length)
return <></>;
return (
<div className='block'>
<h2>Recent Posts</h2>
<div className={style.container}>
{posts?.slice(0, 10)
.map(([slug, post]: any, i: number) => {
return (
<PostBlock
key={slug}
slug={slug}
title={post.title}
otime={post.otime} />
);
})}
</div>
{
posts.length > 10 &&
<div className={style.more}>
<Link href='/posts' >More...</Link>
</div>
}
</div>
);
2022-04-27 21:55:18 -04:00
}
export default RecentPosts;