mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
104 lines
3.2 KiB
JavaScript
104 lines
3.2 KiB
JavaScript
import BlogPost from '@/components/BlogPost'
|
|
import PropTypes from 'prop-types'
|
|
import Pagination from '@/components/Pagination'
|
|
import BLOG from '@/blog.config'
|
|
import { useRouter } from 'next/router'
|
|
import { useTheme } from '@/lib/theme'
|
|
import CommonHead from '@/components/CommonHead'
|
|
import TopNav from '@/components/TopNav'
|
|
import Tags from '@/components/Tags'
|
|
import SideBarResponsive from '@/components/SideBarResponsive'
|
|
|
|
const DefaultLayout = ({ tags, posts, page, currentTag, ...customMeta }) => {
|
|
const meta = {
|
|
title: BLOG.title,
|
|
type: 'website',
|
|
...customMeta
|
|
}
|
|
page = page ?? 1
|
|
let postsToShow = []
|
|
let filteredBlogPosts = posts ?? []
|
|
let currentSearch = ''
|
|
if (posts) {
|
|
const router = useRouter()
|
|
if (router.query && router.query.s) {
|
|
currentSearch = router.query.s
|
|
filteredBlogPosts = posts.filter(post => {
|
|
const tagContent = post.tags ? post.tags.join(' ') : ''
|
|
const searchContent = post.title + post.summary + tagContent + post.slug
|
|
return searchContent.toLowerCase().includes(currentSearch.toLowerCase())
|
|
})
|
|
}
|
|
}
|
|
const totalPages = Math.ceil(filteredBlogPosts.length / BLOG.postsPerPage)
|
|
|
|
if (posts) {
|
|
postsToShow = filteredBlogPosts.slice(
|
|
BLOG.postsPerPage * (page - 1),
|
|
BLOG.postsPerPage * page
|
|
)
|
|
}
|
|
let showNext = false
|
|
if (filteredBlogPosts) {
|
|
const totalPosts = filteredBlogPosts.length
|
|
showNext = page * BLOG.postsPerPage < totalPosts
|
|
}
|
|
|
|
const { theme } = useTheme()
|
|
|
|
return (
|
|
<div id='wrapper' className={theme}>
|
|
<CommonHead meta={meta} />
|
|
<div className=' fixed w-full top-0 z-20'>
|
|
<TopNav />
|
|
</div>
|
|
|
|
<div className={`${BLOG.font} flex justify-between bg-gray-100 dark:bg-black min-h-screen`}>
|
|
{/* 侧边菜单 */}
|
|
<SideBarResponsive />
|
|
|
|
<div className='flex-grow'>
|
|
<div className='fixed top-12 z-10 w-full border-b dark:border-gray-600'>
|
|
<Tags tags={tags} currentTag={currentTag} />
|
|
</div>
|
|
|
|
<main id='post-list-wrapper' className='py-24 px-8 md:px-20'>
|
|
{(!page || page === 1) && (<div className='py-5' />)}
|
|
|
|
{/* 当前搜索 */}
|
|
{(currentSearch || (page && page !== 1)) && (
|
|
<div className='pb-5'>
|
|
<div className='dark:text-gray-200 flex justify-between py-1'>
|
|
{page && page !== 1 && (<span>页 {page} / {totalPages}</span>)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<div className=''>
|
|
{/* 文章列表 */}
|
|
<div className='grid xl:grid-cols-3 lg:grid-cols-2 grid-cols-1 gap-3'>
|
|
{!postsToShow.length && (
|
|
<p className='text-gray-500 dark:text-gray-300'>No posts found.</p>
|
|
)}
|
|
{postsToShow.map(post => (
|
|
<BlogPost key={post.id} post={post} tags={tags} />
|
|
))}
|
|
</div>
|
|
|
|
<Pagination page={page} showNext={showNext} />
|
|
</div>
|
|
</main>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
DefaultLayout.propTypes = {
|
|
posts: PropTypes.array.isRequired,
|
|
tags: PropTypes.object.isRequired,
|
|
currentTag: PropTypes.string
|
|
}
|
|
export default DefaultLayout
|