|
| 1 | +import BLOG from '@/blog.config' |
| 2 | +import { useGlobal } from '@/lib/global' |
| 3 | +import { useEffect } from 'react' |
| 4 | +import BlogArchiveItem from './components/BlogPostArchive' |
1 | 5 | import LayoutBase from './LayoutBase'
|
2 | 6 |
|
3 | 7 | export const LayoutArchive = (props) => {
|
4 |
| - return <LayoutBase {...props}> |
5 |
| - Archive Page |
| 8 | + const { locale } = useGlobal() |
| 9 | + const { posts } = props |
| 10 | + // 深拷贝 |
| 11 | + const postsSortByDate = Object.create(posts) |
| 12 | + |
| 13 | + // 时间排序 |
| 14 | + postsSortByDate.sort((a, b) => { |
| 15 | + const dateA = new Date(a?.date.start_date || a.createdTime) |
| 16 | + const dateB = new Date(b?.date.start_date || b.createdTime) |
| 17 | + return dateB - dateA |
| 18 | + }) |
| 19 | + |
| 20 | + const meta = { |
| 21 | + title: `${locale.NAV.ARCHIVE} | ${BLOG.title}`, |
| 22 | + description: BLOG.description, |
| 23 | + type: 'website' |
| 24 | + } |
| 25 | + |
| 26 | + const archivePosts = {} |
| 27 | + |
| 28 | + postsSortByDate.forEach(post => { |
| 29 | + const date = post.date.start_date.slice(0, 7) |
| 30 | + if (archivePosts[date]) { |
| 31 | + archivePosts[date].push(post) |
| 32 | + } else { |
| 33 | + archivePosts[date] = [post] |
| 34 | + } |
| 35 | + }) |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + if (window) { |
| 39 | + const anchor = window.location.hash |
| 40 | + if (anchor) { |
| 41 | + setTimeout(() => { |
| 42 | + const anchorElement = document.getElementById(anchor.substring(1)) |
| 43 | + if (anchorElement) { |
| 44 | + anchorElement.scrollIntoView({ block: 'start', behavior: 'smooth' }) |
| 45 | + } |
| 46 | + }, 300) |
| 47 | + } |
| 48 | + } |
| 49 | + }, []) |
| 50 | + return <LayoutBase {...props} meta={meta}> |
| 51 | + <div className="mb-10 pb-20 bg-white md:p-12 p-3 dark:bg-gray-800 shadow-md min-h-full"> |
| 52 | + {Object.keys(archivePosts).map(archiveTitle => ( |
| 53 | + <BlogArchiveItem |
| 54 | + key={archiveTitle} |
| 55 | + posts={archivePosts[archiveTitle]} |
| 56 | + archiveTitle={archiveTitle} |
| 57 | + /> |
| 58 | + ))} |
| 59 | + </div> |
6 | 60 | </LayoutBase>
|
7 | 61 | }
|
0 commit comments