Files
NotionNext/components/LatestPosts.js
tangly 46495f9fe6 bugFix:
最新文章排序;
输入框底边样式;
目录背景色高度;
夜间模式按钮颜色;
移动端隐藏悬浮Darkmode按钮;
2021-11-06 11:37:20 +08:00

52 lines
1.6 KiB
JavaScript

import Link from 'next/link'
import BLOG from '@/blog.config'
import { formatDateFmt } from '@/lib/formatDate'
import { useRouter } from 'next/router'
/**
* 最新文章列表
* @param posts
* @constructor
*/
const LatestPosts = ({ posts }) => {
// 深拷贝
let postsSortByDate = Object.create(posts)
// 时间排序
postsSortByDate.sort((a, b) => {
const dateA = new Date(a?.lastEditedTime || a.createdTime)
const dateB = new Date(b?.lastEditedTime || b.createdTime)
return dateB - dateA
})
// 只取前五
postsSortByDate = postsSortByDate.slice(0, 5)
// 获取当前路径
const currentPath = useRouter().asPath
return <>
<section
className='text-sm font-bold py-3 px-5 text-gray-600 dark:text-gray-400 duration-100 flex flex-nowrap align-middle'>
<div className='w-32'>最近更新</div>
</section>
<div>
{postsSortByDate.map(post => {
return (
<Link key={post.id} title={post.title} href={`${BLOG.path}/article/${post.slug}`} >
<div className={(currentPath === `${BLOG.path}/article/${post.slug}` ? 'bg-gray-200 dark:bg-black' : '') + ' text-xs leading-5 py-1.5 px-5 flex'}>
<div className='mr-2 text-gray-500'>
{formatDateFmt(post.lastEditedTime, 'yyyy/MM/dd')}
</div>
<div className='text-sm flex w-50 overflow-x-hidden whitespace-nowrap dark:text-gray-300 cursor-pointer hover:underline'>
{post.title}
</div>
</div>
</Link>
)
})}
</div>
</>
}
export default LatestPosts