Skip to content

Commit

Permalink
post title
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasp committed Jan 6, 2025
1 parent 7fa2366 commit b562e2e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
41 changes: 33 additions & 8 deletions app/post/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
import { Suspense } from "react"
import type { Metadata, ResolvingMetadata } from "next"
import { getPostBySlug } from "@/data/dbPosts"

import { deslugify } from "@/lib/slug"

import { SinglePostContent } from "./SinglePostContent"
import { SinglePostSkeleton } from "./SinglePostSkeleton"
import { removeSocialMediaEmbeds } from "./util"

type Props = {
params: { slug: string }
}

export async function generateMetadata({ params }: Props): Promise<Metadata> {
// read route params
const slug = params.slug

// fetch data
const post = await getPostBySlug(slug)

if (!post) {
return {
title: "Post not found",
description: "Post not found",
}
}

let description = removeSocialMediaEmbeds(post.content)
if (description.length > 150) {
description = description.slice(0, 150) + "..."
}

export const metadata = {
title: "News Categories",
description: "Explore all Polkadot News Categories",
return {
title: `${post.title} | ${post.categories.map((c) => c.name).join(", ")}`,
description,
}
}

export default async function PostPage({
Expand All @@ -15,11 +44,7 @@ export default async function PostPage({
}) {
const { slug } = params

let calculatedTitle = slug
.split("-")
.slice(0, -1)
.map((s) => s.charAt(0).toUpperCase() + s.slice(1))
.join(" ")
let calculatedTitle = deslugify(slug)

return (
<Suspense fallback={<SinglePostSkeleton title={calculatedTitle} />}>
Expand Down
12 changes: 12 additions & 0 deletions data/dbPosts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cache } from "react"
import { prisma } from "@/prisma/prisma"
import { totalEarnings } from "@/utils/totalPostEarnings"
import orderBy from "lodash"
Expand Down Expand Up @@ -292,3 +293,14 @@ export async function getAuthorPostCount(authorName: string) {
})
return count
}

export const getPostBySlug = cache(async (slug: string) => {
const post = await prisma.post.findUnique({
where: { slug },
include: {
tags: true,
categories: true,
},
})
return post
})

0 comments on commit b562e2e

Please sign in to comment.