From 604f5d64d07b08209d5076195a264eccfa19f5b6 Mon Sep 17 00:00:00 2001 From: Max Leiter Date: Sun, 25 Dec 2022 20:04:56 -0800 Subject: [PATCH] post/[id]: move function to new file to avoid invalid-segment-export --- src/app/(posts)/post/[id]/get-post.tsx | 66 +++++++++++++++++++++++++ src/app/(posts)/post/[id]/layout.tsx | 2 +- src/app/(posts)/post/[id]/page.tsx | 67 +------------------------- 3 files changed, 68 insertions(+), 67 deletions(-) create mode 100644 src/app/(posts)/post/[id]/get-post.tsx diff --git a/src/app/(posts)/post/[id]/get-post.tsx b/src/app/(posts)/post/[id]/get-post.tsx new file mode 100644 index 00000000..aa9c0342 --- /dev/null +++ b/src/app/(posts)/post/[id]/get-post.tsx @@ -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 } +}) diff --git a/src/app/(posts)/post/[id]/layout.tsx b/src/app/(posts)/post/[id]/layout.tsx index 371428dd..4dff8783 100644 --- a/src/app/(posts)/post/[id]/layout.tsx +++ b/src/app/(posts)/post/[id]/layout.tsx @@ -4,7 +4,7 @@ import { title } from "process" import { PostButtons } from "./components/header/post-buttons" import styles from "./layout.module.css" import { PostTitle } from "./components/header/title" -import { getPost } from "./page" +import { getPost } from "./get-post" export default async function PostLayout({ children, diff --git a/src/app/(posts)/post/[id]/page.tsx b/src/app/(posts)/post/[id]/page.tsx index 0d31631e..04372e6d 100644 --- a/src/app/(posts)/post/[id]/page.tsx +++ b/src/app/(posts)/post/[id]/page.tsx @@ -1,71 +1,6 @@ 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" - -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 } -}) +import { getPost } from "./get-post" export default async function PostPage({ params