CoastalCommitsPastes/client/app/(posts)/post/[id]/page.tsx

141 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-12-04 04:31:51 -05:00
import PostPage from "./components/post-page"
import { notFound, redirect } from "next/navigation"
2022-12-04 04:31:51 -05:00
import { getPostById, Post, PostWithFilesAndAuthor } from "@lib/server/prisma"
import { getCurrentUser } from "@lib/server/session"
2022-12-04 04:31:51 -05:00
import ScrollToTop from "@components/scroll-to-top"
import { title } from "process"
import { PostButtons } from "./components/header/post-buttons"
import styles from "./styles.module.css"
import { PostTitle } from "./components/header/title"
import VisibilityControl from "@components/badges/visibility-control"
export type PostProps = {
post: Post
isProtected?: boolean
}
2022-12-04 04:31:51 -05:00
// export async function generateStaticParams() {
// const posts = await getAllPosts({
// where: {
// visibility: {
// equals: "public"
// }
// }
// })
// return posts.map((post) => ({
// id: post.id
// }))
// }
const fetchOptions = {
withFiles: true,
withAuthor: true
}
2022-11-12 21:39:03 -05:00
const getPost = async (id: string) => {
2022-12-04 04:31:51 -05:00
const post = (await getPostById(id, fetchOptions)) as PostWithFilesAndAuthor
2022-11-12 21:39:03 -05:00
if (!post) {
return notFound()
}
2022-11-30 01:22:17 -05:00
const user = await getCurrentUser()
const isAuthorOrAdmin = user?.id === post?.authorId || user?.role === "admin"
if (post.visibility === "public") {
return { post, isAuthor: isAuthorOrAdmin }
}
2022-11-18 01:36:53 -05:00
if (post.visibility === "private" && !isAuthorOrAdmin) {
return notFound()
}
if (post.visibility === "private" && !isAuthorOrAdmin) {
return notFound()
}
if (post.visibility === "protected" && !isAuthorOrAdmin) {
return {
// TODO: remove this. It's temporary to appease typescript
post: {
visibility: "protected",
2022-12-04 04:31:51 -05:00
id: post.id,
files: [],
parentId: "",
title: "",
createdAt: new Date("1970-01-01"),
expiresAt: new Date("1970-01-01"),
2022-12-04 04:31:51 -05:00
author: {
2022-12-04 17:26:14 -05:00
displayName: ""
2022-12-04 04:31:51 -05:00
},
2022-12-04 04:55:20 -05:00
description: "",
2022-12-04 17:26:14 -05:00
authorId: ""
},
isProtected: true,
isAuthor: isAuthorOrAdmin
}
}
// if expired
if (post.expiresAt && !isAuthorOrAdmin) {
const expirationDate = new Date(post.expiresAt)
if (expirationDate < new Date()) {
return redirect("/expired")
}
}
return { post, isAuthor: isAuthorOrAdmin }
}
const PostView = async ({
params
}: {
params: {
2022-11-12 04:28:06 -05:00
id: string
}
}) => {
const { post, isProtected, isAuthor } = await getPost(params.id)
// TODO: serialize dates in prisma middleware instead of passing as JSON
const stringifiedPost = JSON.stringify(post)
2022-12-04 04:31:51 -05:00
return (
2022-12-04 17:49:18 -05:00
<div className={styles.root}>
2022-12-04 04:31:51 -05:00
<div className={styles.header}>
<PostButtons
parentId={post.parentId || undefined}
postId={post.id}
files={post.files}
title={title}
/>
<PostTitle
title={post.title}
createdAt={post.createdAt.toString()}
expiresAt={post.expiresAt?.toString()}
2022-12-04 04:31:51 -05:00
displayName={post.author?.displayName || ""}
visibility={post.visibility}
2022-12-04 04:55:20 -05:00
authorId={post.authorId}
2022-12-04 04:31:51 -05:00
/>
</div>
{post.description && (
<div>
<p>{post.description}</p>
</div>
)}
<PostPage
isAuthor={isAuthor}
isProtected={isProtected}
post={stringifiedPost}
/>
{isAuthor && (
<span className={styles.controls}>
<VisibilityControl postId={post.id} visibility={post.visibility} />
</span>
)}
<ScrollToTop />
2022-12-04 17:49:18 -05:00
</div>
)
}
export default PostView