www/components/recent-posts.tsx

51 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-04-28 01:55:18 +00:00
import Link from "next/link";
import { toRelativeDate } from "../lib/date";
2022-04-28 01:55:18 +00:00
import style from '../styles/recent-posts.module.css';
import PostsInfo from '../public/posts.json';
2022-04-28 01:55:18 +00: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 <></>;
2022-05-15 13:56:45 +00:00
return (
<div className='block'>
<h2>Recent Posts</h2>
2022-05-15 13:56:45 +00:00
<div className={style.container}>
{posts?.slice(0, 10)
.map(([slug, post]: any, i: number) => {
return (
<PostBlock
key={i}
slug={slug}
title={post.title}
otime={post.otime} />
);
2022-05-15 13:56:45 +00:00
})}
2022-04-28 01:55:18 +00:00
</div>
2022-05-15 13:56:45 +00:00
{
posts.length > 10 &&
2022-05-15 13:56:45 +00:00
<div className={style.more}>
<Link href='/posts' >More...</Link>
2022-05-15 13:56:45 +00:00
</div>
}
</div>
2022-04-28 01:55:18 +00:00
);
}
export default RecentPosts;