Skip to content

Commit

Permalink
refactor: use sortPostsByDate
Browse files Browse the repository at this point in the history
  • Loading branch information
junkisai committed Oct 15, 2024
1 parent b875552 commit 9dc6409
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type Lang, getTranslation } from '@/features/i18n'
import { MDXContent } from '@/libs/contentlayer'
import type { Post } from 'contentlayer/generated'
import { compareDesc, format, parseISO } from 'date-fns'
import { format, parseISO } from 'date-fns'
import Link from 'next/link'
import type { FC } from 'react'
import { filterPostsByLang } from '../../utils'
import { filterPostsByLang, sortPostsByDate } from '../../utils'

function PostCard(post: Post) {
return (
Expand All @@ -28,9 +28,7 @@ export const PostListPage: FC<Props> = ({ lang }) => {
const { t } = getTranslation(lang)

const posts = filterPostsByLang(lang)
const sortedPosts = posts.sort((a, b) =>
compareDesc(new Date(a.date), new Date(b.date)),
)
const sortedPosts = sortPostsByDate(posts)

return (
<div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './filterPostsByLang'
export * from './findPostByLangAndSlug'
export * from './sortPostsByDate'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Post } from 'contentlayer/generated'
import { compareDesc } from 'date-fns'

export function sortPostsByDate(posts: Post[]) {
return posts.sort((a, b) => compareDesc(new Date(a.date), new Date(b.date)))
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TopCards } from '@/components'
import type { Lang } from '@/features/i18n'
import { filterPostsByLang } from '@/features/posts'
import { compareDesc } from 'date-fns'
import { filterPostsByLang, sortPostsByDate } from '@/features/posts'
import type { FC } from 'react'

type Props = {
Expand All @@ -10,9 +9,7 @@ type Props = {

export const TopPage: FC<Props> = ({ lang }) => {
const posts = filterPostsByLang(lang)
const sortedPosts = posts.sort((a, b) =>
compareDesc(new Date(a.date), new Date(b.date)),
)
const sortedPosts = sortPostsByDate(posts)

return <TopCards posts={sortedPosts.slice(0, 14)} />
}

0 comments on commit 9dc6409

Please sign in to comment.