jonbio/layouts/ListLayout.js

87 lines
3.4 KiB
JavaScript
Raw Normal View History

import Link from '@/components/Link'
2021-01-09 17:50:45 +08:00
import Tag from '@/components/Tag'
2021-04-26 10:11:03 -06:00
import siteMetadata from '@/data/siteMetadata'
2021-02-25 00:04:02 +01:00
import { useState } from 'react'
2021-01-09 17:50:45 +08:00
2021-02-25 00:04:02 +01:00
const postDateTemplate = { year: 'numeric', month: 'long', day: 'numeric' }
2021-01-09 17:50:45 +08:00
export default function ListLayout({ posts, title }) {
const [searchValue, setSearchValue] = useState('')
const filteredBlogPosts = posts.filter((frontMatter) => {
const searchContent = frontMatter.title + frontMatter.summary + frontMatter.tags.join(' ')
return searchContent.toLowerCase().includes(searchValue.toLowerCase())
})
2021-01-09 17:50:45 +08:00
return (
<>
<div className="divide-y">
<div className="pt-6 pb-8 space-y-2 md:space-y-5">
2021-01-12 23:35:36 +08:00
<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">
2021-01-09 17:50:45 +08:00
{title}
</h1>
<div className="relative max-w-lg">
<input
aria-label="Search articles"
type="text"
onChange={(e) => setSearchValue(e.target.value)}
placeholder="Search articles"
2021-01-12 23:35:36 +08:00
className="block w-full px-4 py-2 text-gray-900 bg-white border border-gray-300 rounded-md dark:border-gray-900 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-800 dark:text-gray-100"
2021-01-09 17:50:45 +08:00
/>
<svg
2021-01-12 23:35:36 +08:00
className="absolute w-5 h-5 text-gray-400 right-3 top-3 dark:text-gray-300"
2021-01-09 17:50:45 +08:00
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<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.'}
{filteredBlogPosts.map((frontMatter) => {
const { slug, date, title, summary, tags } = frontMatter
return (
<li key={slug} className="py-4">
<article className="space-y-2 xl:grid xl:grid-cols-4 xl:space-y-0 xl:items-baseline">
<dl>
<dt className="sr-only">Published on</dt>
2021-01-12 23:35:36 +08:00
<dd className="text-base font-medium leading-6 text-gray-500 dark:text-gray-400">
2021-02-25 00:04:02 +01:00
<time dateTime={date}>
2021-04-26 10:11:03 -06:00
{new Date(date).toLocaleDateString(siteMetadata.locale, postDateTemplate)}
2021-02-25 00:04:02 +01:00
</time>
2021-01-09 17:50:45 +08:00
</dd>
</dl>
<div className="space-y-3 xl:col-span-3">
<div>
2021-01-12 23:35:36 +08:00
<h3 className="text-2xl font-bold leading-8 tracking-tight">
<Link href={`/blog/${slug}`} className="text-gray-900 dark:text-gray-100">
{title}
2021-01-09 17:50:45 +08:00
</Link>
</h3>
2021-02-06 16:54:20 +08:00
<div className="flex flex-wrap">
2021-01-09 17:50:45 +08:00
{tags.map((tag) => (
<Tag key={tag} text={tag} />
))}
</div>
</div>
2021-01-12 23:35:36 +08:00
<div className="prose text-gray-500 max-w-none dark:text-gray-400">
2021-01-09 17:50:45 +08:00
{summary}
</div>
</div>
</article>
</li>
)
})}
</ul>
</div>
</>
)
}