This commit is contained in:
EGOIST
2022-05-14 00:11:14 +08:00
parent 00df250ff5
commit 2383f2f455
3 changed files with 49 additions and 19 deletions
+40 -13
View File
@@ -1,26 +1,53 @@
import Link from "next/link"
import { useMemo } from "react"
import { formatDate } from "~/lib/date"
type Post = { id: string; publishedAt: string; slug: string; title: string }
export const SiteArchives: React.FC<{
posts:
| { id: string; publishedAt: string; slug: string; title: string }[]
| undefined
posts: Post[] | undefined
}> = ({ posts }) => {
const groupedByYear = useMemo<Map<string, Post[]>>(() => {
const map = new Map()
if (posts) {
for (const post of posts) {
const year = formatDate(post.publishedAt, "YYYY")
const items = map.get(year) || []
items.push(post)
map.set(year, items)
}
}
return map
}, [posts])
if (!posts) return null
return (
<>
<h2 className="text-xl font-bold page-title">Archives</h2>
<div className="mt-5">
{posts.map((post) => {
<div className="mt-5 space-y-5">
{[...groupedByYear.keys()].map((year) => {
const posts = groupedByYear.get(year)!
return (
<Link key={post.id} href={`/${post.slug}`}>
<a className="flex justify-between items-center p-1 px-2 rounded-lg -mx-2 hover:bg-zinc-50">
<span className="text-accent">{post.title}</span>
<span className="text-zinc-400 mr-3 text-sm whitespace-nowrap">
{formatDate(post.publishedAt)}
</span>
</a>
</Link>
<div key={year}>
<h3 className="text-lg font-bold mb-1 text-zinc-700">{year}</h3>
{posts.map((post) => {
return (
<Link key={post.id} href={`/${post.slug}`}>
<a className="flex justify-between items-center p-1 px-2 rounded-lg -mx-2 hover:bg-zinc-50">
<span className="text-accent font-medium">
{post.title}
</span>
<span className="text-zinc-400 mr-3 font-medium whitespace-nowrap">
{formatDate(post.publishedAt, "MMM D")}
</span>
</a>
</Link>
)
})}
</div>
)
})}
</div>
+5 -2
View File
@@ -1,5 +1,8 @@
import dayjs from "dayjs"
export const formatDate = (date: string | Date) => {
return dayjs(date).format("MMM D, YYYY")
export const formatDate = (
date: string | Date,
format: "MMM D" | "MMM D, YYYY" | "YYYY" = "MMM D, YYYY"
) => {
return dayjs(date).format(format)
}
+4 -4
View File
@@ -154,17 +154,17 @@ export async function getPagesBySite(
deletedAt: null,
type: input.type === "post" ? "POST" : "PAGE",
}
if (input.visibility === PageVisibilityEnum.Published) {
if (visibility === PageVisibilityEnum.Published) {
where.published = true
where.publishedAt = {
lte: now,
}
} else if (input.visibility === PageVisibilityEnum.Scheduled) {
} else if (visibility === PageVisibilityEnum.Scheduled) {
where.published = true
where.publishedAt = {
gt: now,
}
} else if (input.visibility === PageVisibilityEnum.Draft) {
} else if (visibility === PageVisibilityEnum.Draft) {
where.published = false
}
@@ -173,7 +173,7 @@ export async function getPagesBySite(
prisma.page.findMany({
where,
orderBy: {
createdAt: "desc",
publishedAt: "desc",
},
take: take + 1,
cursor: input.cursor