2022-10-17 23:37:01 +08:00
|
|
|
import Link from '@/components/Link';
|
|
|
|
import Tag from '@/components/Tag';
|
|
|
|
import { ComponentProps, useState } from 'react';
|
|
|
|
import Pagination from '@/components/Pagination';
|
|
|
|
import formatDate from '@/lib/utils/formatDate';
|
|
|
|
import { PostFrontMatter } from 'types/PostFrontMatter';
|
2022-10-07 13:55:39 +08:00
|
|
|
interface Props {
|
2022-10-17 23:37:01 +08:00
|
|
|
posts: PostFrontMatter[];
|
|
|
|
title: string;
|
|
|
|
initialDisplayPosts?: PostFrontMatter[];
|
|
|
|
pagination?: ComponentProps<typeof Pagination>;
|
2022-10-07 13:55:39 +08:00
|
|
|
}
|
2022-07-17 21:40:41 +08:00
|
|
|
|
2022-10-17 23:37:01 +08:00
|
|
|
export default function ListLayout({
|
|
|
|
posts,
|
|
|
|
title,
|
|
|
|
initialDisplayPosts = [],
|
|
|
|
pagination,
|
|
|
|
}: Props) {
|
|
|
|
const [searchValue, setSearchValue] = useState('');
|
2022-07-17 21:40:41 +08:00
|
|
|
const filteredBlogPosts = posts.filter((frontMatter) => {
|
2022-10-17 23:37:01 +08:00
|
|
|
const searchContent =
|
|
|
|
frontMatter.title + frontMatter.summary + frontMatter.tags.join(' ');
|
|
|
|
return searchContent.toLowerCase().includes(searchValue.toLowerCase());
|
|
|
|
});
|
2022-07-17 21:40:41 +08:00
|
|
|
|
|
|
|
// If initialDisplayPosts exist, display it if no searchValue is specified
|
|
|
|
const displayPosts =
|
2022-10-17 23:37:01 +08:00
|
|
|
initialDisplayPosts.length > 0 && !searchValue
|
|
|
|
? initialDisplayPosts
|
|
|
|
: filteredBlogPosts;
|
2022-07-17 21:40:41 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-10-07 14:40:51 +08:00
|
|
|
<div className="divide-y divide-gray-200 dark:divide-gray-700">
|
2022-07-17 21:40:41 +08:00
|
|
|
<div className="space-y-2 pt-6 pb-8 md:space-y-5">
|
|
|
|
<h1 className="text-3xl font-extrabold leading-9 tracking-tight text-gray-900 dark:text-gray-100 sm:text-4xl sm:leading-10 md:text-6xl md:leading-14">
|
|
|
|
{title}
|
|
|
|
</h1>
|
|
|
|
<div className="relative max-w-lg">
|
|
|
|
<input
|
|
|
|
aria-label="Search articles"
|
|
|
|
type="text"
|
|
|
|
onChange={(e) => setSearchValue(e.target.value)}
|
|
|
|
placeholder="Search articles"
|
|
|
|
className="block w-full rounded-md border border-gray-300 bg-white px-4 py-2 text-gray-900 focus:border-primary-500 focus:ring-primary-500 dark:border-gray-900 dark:bg-gray-800 dark:text-gray-100"
|
|
|
|
/>
|
|
|
|
<svg
|
|
|
|
className="absolute right-3 top-3 h-5 w-5 text-gray-400 dark:text-gray-300"
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
fill="none"
|
|
|
|
viewBox="0 0 24 24"
|
2022-10-17 23:37:01 +08:00
|
|
|
stroke="currentColor">
|
2022-07-17 21:40:41 +08:00
|
|
|
<path
|
|
|
|
strokeLinecap="round"
|
|
|
|
strokeLinejoin="round"
|
|
|
|
strokeWidth={2}
|
|
|
|
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
|
|
|
|
/>
|
|
|
|
</svg>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<ul>
|
|
|
|
{!filteredBlogPosts.length && 'No posts found.'}
|
|
|
|
{displayPosts.map((frontMatter) => {
|
2022-10-17 23:37:01 +08:00
|
|
|
const { slug, date, title, summary, tags } = frontMatter;
|
2022-07-17 21:40:41 +08:00
|
|
|
return (
|
|
|
|
<li key={slug} className="py-4">
|
|
|
|
<article className="space-y-2 xl:grid xl:grid-cols-4 xl:items-baseline xl:space-y-0">
|
|
|
|
<dl>
|
|
|
|
<dt className="sr-only">Published on</dt>
|
|
|
|
<dd className="text-base font-medium leading-6 text-gray-500 dark:text-gray-400">
|
|
|
|
<time dateTime={date}>{formatDate(date)}</time>
|
|
|
|
</dd>
|
|
|
|
</dl>
|
|
|
|
<div className="space-y-3 xl:col-span-3">
|
|
|
|
<div>
|
|
|
|
<h3 className="text-2xl font-bold leading-8 tracking-tight">
|
2022-10-17 23:37:01 +08:00
|
|
|
<Link
|
|
|
|
href={`/blog/${slug}`}
|
|
|
|
className="text-gray-900 dark:text-gray-100">
|
2022-07-17 21:40:41 +08:00
|
|
|
{title}
|
|
|
|
</Link>
|
|
|
|
</h3>
|
|
|
|
<div className="flex flex-wrap">
|
|
|
|
{tags.map((tag) => (
|
|
|
|
<Tag key={tag} text={tag} />
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="prose max-w-none text-gray-500 dark:text-gray-400">
|
|
|
|
{summary}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</article>
|
|
|
|
</li>
|
2022-10-17 23:37:01 +08:00
|
|
|
);
|
2022-07-17 21:40:41 +08:00
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
{pagination && pagination.totalPages > 1 && !searchValue && (
|
2022-10-17 23:37:01 +08:00
|
|
|
<Pagination
|
|
|
|
currentPage={pagination.currentPage}
|
|
|
|
totalPages={pagination.totalPages}
|
|
|
|
/>
|
2022-07-17 21:40:41 +08:00
|
|
|
)}
|
|
|
|
</>
|
2022-10-17 23:37:01 +08:00
|
|
|
);
|
2022-07-17 21:40:41 +08:00
|
|
|
}
|