Files
NotionNext/themes/Medium/components/BlogPostListPage.js
tangly1024 c929d6fa02 Medium V2.0
2022-02-12 17:09:00 +08:00

52 lines
1.4 KiB
JavaScript

import BlogPostCard from './BlogPostCard'
import BLOG from '@/blog.config'
import BlogPostListEmpty from './BlogPostListEmpty'
import PaginationSimple from './PaginationSimple'
import { useRouter } from 'next/router'
/**
* 文章列表分页表格
* @param page 当前页
* @param posts 所有文章
* @param tags 所有标签
* @returns {JSX.Element}
* @constructor
*/
const BlogPostListPage = ({ page = 1, posts = [], postCount }) => {
let filteredPosts = Object.assign(posts)
const searchKey = getSearchKey()
if (searchKey) {
filteredPosts = posts.filter(post => {
const tagContent = post.tags ? post.tags.join(' ') : ''
const searchContent = post.title + post.summary + tagContent
return searchContent.toLowerCase().includes(searchKey.toLowerCase())
})
}
const filteredPostsCount = filteredPosts.length
const totalPage = Math.ceil(filteredPostsCount / BLOG.POSTS_PER_PAGE)
if (!posts || posts.length === 0) {
return <BlogPostListEmpty />
} else {
return (
<div id="container" className='w-full justify-center'>
{/* 文章列表 */}
{filteredPosts.map(post => (
<BlogPostCard key={post.id} post={post} />
))}
<PaginationSimple page={page} totalPage={totalPage} />
</div>
)
}
}
function getSearchKey () {
const router = useRouter()
if (router.query && router.query.s) {
return router.query.s
}
return null
}
export default BlogPostListPage