|
import { getGlobalData } from '@/lib/notion/getNotionData' |
|
import React from 'react' |
|
import BLOG from '@/blog.config' |
|
import { useRouter } from 'next/router' |
|
import { getLayoutByTheme } from '@/themes/theme' |
|
import { siteConfig } from '@/lib/config' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default function Category(props) { |
|
|
|
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() }) |
|
|
|
return <Layout {...props} /> |
|
} |
|
|
|
export async function getStaticProps({ params: { category, page } }) { |
|
const from = 'category-page-props' |
|
let props = await getGlobalData({ from }) |
|
|
|
|
|
props.posts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.category.includes(category)) |
|
|
|
props.postCount = props.posts.length |
|
|
|
props.posts = props.posts.slice(BLOG.POSTS_PER_PAGE * (page - 1), BLOG.POSTS_PER_PAGE * page) |
|
|
|
delete props.allPages |
|
props.page = page |
|
|
|
props = { ...props, category, page } |
|
|
|
return { |
|
props, |
|
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) |
|
} |
|
} |
|
|
|
export async function getStaticPaths() { |
|
const from = 'category-paths' |
|
const { categoryOptions, allPages } = await getGlobalData({ from }) |
|
const paths = [] |
|
|
|
categoryOptions?.forEach(category => { |
|
|
|
const categoryPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.category.includes(category.name)) |
|
|
|
const postCount = categoryPosts.length |
|
const totalPages = Math.ceil(postCount / BLOG.POSTS_PER_PAGE) |
|
if (totalPages > 1) { |
|
for (let i = 1; i <= totalPages; i++) { |
|
paths.push({ params: { category: category.name, page: '' + i } }) |
|
} |
|
} |
|
}) |
|
|
|
return { |
|
paths, |
|
fallback: true |
|
} |
|
} |
|
|