mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-18 07:26:48 +00:00
78 lines
2.0 KiB
JavaScript
78 lines
2.0 KiB
JavaScript
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
|
|
import React, { Suspense, useEffect, useState } from 'react'
|
|
import { useGlobal } from '@/lib/global'
|
|
import dynamic from 'next/dynamic'
|
|
import BLOG from '@/blog.config'
|
|
import Loading from '@/components/Loading'
|
|
|
|
const layout = 'LayoutArchive'
|
|
|
|
/**
|
|
* 加载默认主题
|
|
*/
|
|
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
|
|
|
|
const ArchiveIndex = props => {
|
|
const { siteInfo } = props
|
|
const { theme, locale } = useGlobal()
|
|
const [Layout, setLayout] = useState(DefaultLayout)
|
|
// 切换主题
|
|
useEffect(() => {
|
|
const loadLayout = async () => {
|
|
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
|
|
setLayout(newLayout)
|
|
}
|
|
loadLayout()
|
|
}, [theme])
|
|
|
|
const meta = {
|
|
title: `${locale.NAV.ARCHIVE} | ${siteInfo?.title}`,
|
|
description: siteInfo?.description,
|
|
image: siteInfo?.pageCover,
|
|
slug: 'archive',
|
|
type: 'website'
|
|
}
|
|
|
|
props = { ...props, meta }
|
|
|
|
return <Suspense fallback={<Loading/>}>
|
|
<Layout {...props} />
|
|
</Suspense>
|
|
}
|
|
|
|
export async function getStaticProps() {
|
|
const props = await getGlobalNotionData({ from: 'archive-index' })
|
|
// 处理分页
|
|
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published')
|
|
delete props.allPages
|
|
|
|
const postsSortByDate = Object.create(props.posts)
|
|
|
|
postsSortByDate.sort((a, b) => {
|
|
const dateA = new Date(a?.date?.start_date || a.createdTime)
|
|
const dateB = new Date(b?.date?.start_date || b.createdTime)
|
|
return dateB - dateA
|
|
})
|
|
|
|
const archivePosts = {}
|
|
|
|
postsSortByDate.forEach(post => {
|
|
const date = post.date?.start_date?.slice(0, 7) || post.createdTime
|
|
if (archivePosts[date]) {
|
|
archivePosts[date].push(post)
|
|
} else {
|
|
archivePosts[date] = [post]
|
|
}
|
|
})
|
|
|
|
props.archivePosts = archivePosts
|
|
delete props.allPages
|
|
|
|
return {
|
|
props,
|
|
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND)
|
|
}
|
|
}
|
|
|
|
export default ArchiveIndex
|