feat: tags in archive page

This commit is contained in:
DIYgod
2022-11-02 01:55:39 +00:00
parent d0b079886c
commit a4e51f7b6e
2 changed files with 66 additions and 25 deletions
+65 -24
View File
@@ -3,11 +3,13 @@ import { useMemo } from "react"
import { formatDate } from "~/lib/date"
import { EmptyState } from "../ui/EmptyState"
import { Notes, Note } from "~/lib/types"
import { UniLink } from "../ui/UniLink"
export const SiteArchives: React.FC<{
posts?: Notes
title?: string
}> = ({ posts, title }) => {
showTags?: boolean
}> = ({ posts, title, showTags }) => {
const groupedByYear = useMemo<Map<string, Note[]>>(() => {
const map = new Map()
@@ -23,6 +25,26 @@ export const SiteArchives: React.FC<{
return map
}, [posts])
const tags = useMemo<Map<string, number>>(() => {
const result = new Map()
if (posts) {
for (const post of posts.list) {
post.tags?.forEach((tag) => {
if (tag !== "post" && tag !== "page") {
if (result.has(tag)) {
result.set(tag, result.get(tag) + 1)
} else {
result.set(tag, 1)
}
}
})
}
}
return result
}, [posts])
if (!posts) return null
return (
@@ -34,30 +56,49 @@ export const SiteArchives: React.FC<{
</div>
)}
{posts.list.length > 0 && (
<div className="mt-5 space-y-5">
{[...groupedByYear.keys()].map((year) => {
const posts = groupedByYear.get(year)!
return (
<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 || post.id}`}
className="flex justify-between items-center p-2 rounded-lg -mx-2 hover:bg-zinc-100"
>
<span className="text-zinc-700">{post.title}</span>
<span className="text-zinc-400 mr-3 whitespace-nowrap">
{formatDate(post.date_published, "MMM D")}
</span>
</Link>
)
})}
<>
{showTags && tags.size > 0 && (
<div className="mt-5">
<h3 className="text-lg font-bold mb-1 text-zinc-700">Tags</h3>
<div className="pt-2">
{[...tags.keys()].map((tag) => (
<UniLink key={tag} href={`/tag/${tag}`} className="mr-6">
<span className="align-middle">{tag}</span>
<span className="text-gray-400 text-sm ml-1 align-middle">
({tags.get(tag)})
</span>
</UniLink>
))}
</div>
)
})}
</div>
</div>
)}
<div className="mt-5 space-y-5">
{[...groupedByYear.keys()].map((year) => {
const posts = groupedByYear.get(year)!
return (
<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 || post.id}`}
className="flex justify-between items-center p-2 rounded-lg -mx-2 hover:bg-zinc-100"
>
<span className="text-zinc-700">{post.title}</span>
<span className="text-zinc-400 mr-3 whitespace-nowrap">
{formatDate(post.date_published, "MMM D")}
</span>
</Link>
)
})}
</div>
)
})}
</div>
</>
)}
</>
)
+1 -1
View File
@@ -39,7 +39,7 @@ function SiteArchivesPage({
render: true,
})
return <SiteArchives posts={posts.data} />
return <SiteArchives posts={posts.data} showTags={true} />
}
SiteArchivesPage.getLayout = (page: ReactElement) => {