post/[id]: move function to new file to avoid invalid-segment-export

This commit is contained in:
Max Leiter 2022-12-25 20:04:56 -08:00
parent b848aa9e40
commit 604f5d64d0
3 changed files with 68 additions and 67 deletions

View file

@ -0,0 +1,66 @@
import { getPostById } from "@lib/server/prisma"
import { getCurrentUser } from "@lib/server/session"
import { notFound, redirect } from "next/navigation"
import { cache } from "react"
export const getPost = cache(async (id: string) => {
const post = await getPostById(id, {
select: {
visibility: true,
authorId: true,
title: true,
description: true,
id: true,
createdAt: true,
expiresAt: true,
parentId: true,
author: {
select: {
displayName: true,
image: true
}
},
files: {
select: {
id: true,
content: true,
updatedAt: true,
title: true,
html: true
}
}
}
})
if (!post) {
return notFound()
}
if (post.expiresAt && new Date(post.expiresAt) < new Date()) {
return redirect("/expired")
}
if (post.visibility === "public" || post.visibility === "unlisted") {
return { post }
}
if (post.visibility === "private") {
const user = await getCurrentUser()
if (user?.id === post.authorId || user?.role === "admin") {
return { post }
}
return redirect("/new")
}
if (post.visibility === "protected") {
return {
post: {
visibility: "protected",
authorId: post.authorId,
id: post.id
}
}
}
return { post }
})

View file

@ -4,7 +4,7 @@ import { title } from "process"
import { PostButtons } from "./components/header/post-buttons" import { PostButtons } from "./components/header/post-buttons"
import styles from "./layout.module.css" import styles from "./layout.module.css"
import { PostTitle } from "./components/header/title" import { PostTitle } from "./components/header/title"
import { getPost } from "./page" import { getPost } from "./get-post"
export default async function PostLayout({ export default async function PostLayout({
children, children,

View file

@ -1,71 +1,6 @@
import VisibilityControl from "@components/badges/visibility-control" import VisibilityControl from "@components/badges/visibility-control"
import { getPostById } from "@lib/server/prisma"
import { getCurrentUser } from "@lib/server/session"
import { notFound, redirect } from "next/navigation"
import { cache } from "react"
import PostFiles from "./components/post-files" import PostFiles from "./components/post-files"
import { getPost } from "./get-post"
export const getPost = cache(async (id: string) => {
const post = await getPostById(id, {
select: {
visibility: true,
authorId: true,
title: true,
description: true,
id: true,
createdAt: true,
expiresAt: true,
parentId: true,
author: {
select: {
displayName: true,
image: true
}
},
files: {
select: {
id: true,
content: true,
updatedAt: true,
title: true,
html: true
}
}
}
})
if (!post) {
return notFound()
}
if (post.expiresAt && new Date(post.expiresAt) < new Date()) {
return redirect("/expired")
}
if (post.visibility === "public" || post.visibility === "unlisted") {
return { post }
}
if (post.visibility === "private") {
const user = await getCurrentUser()
if (user?.id === post.authorId || user.role === "admin") {
return { post }
}
return redirect("/new")
}
if (post.visibility === "protected") {
return {
post: {
visibility: "protected",
authorId: post.authorId,
id: post.id
}
}
}
return { post }
})
export default async function PostPage({ export default async function PostPage({
params params