generated from warmachine028/github-super-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3a3031
commit b8fe3c7
Showing
6 changed files
with
121 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { validateRequest } from '@/auth' | ||
import { prisma } from '@/lib' | ||
import { getPostDataInclude, UserData } from '@/types' | ||
import { Metadata } from 'next' | ||
import { notFound } from 'next/navigation' | ||
import { cache, Suspense } from 'react' | ||
import { Avatar, FollowButton, Linkify, Menubar, Post } from '@/components' | ||
import { UserTooltip } from '@/components/users' | ||
import Link from 'next/link' | ||
import { Loader2 } from 'lucide-react' | ||
|
||
interface PageProps { | ||
params: Promise<{ postId: string }> | ||
} | ||
|
||
const getPost = cache(async (postId: string, loggedInUserId: string) => { | ||
const post = await prisma.post.findUnique({ | ||
where: { | ||
id: postId | ||
}, | ||
include: getPostDataInclude(loggedInUserId) | ||
}) | ||
|
||
if (!post) { | ||
notFound() | ||
} | ||
return post | ||
}) | ||
|
||
export const generateMetadata = async ({ params }: PageProps): Promise<Metadata> => { | ||
const { postId } = await params | ||
const { user: currentUser } = await validateRequest() | ||
|
||
if (!currentUser) { | ||
return {} | ||
} | ||
|
||
const post = await getPost(postId, currentUser.id) | ||
|
||
return { | ||
title: `${post.author.userName}: ${post.content.slice(0, 50)}...` | ||
} | ||
} | ||
|
||
const PostPage = async ({ params }: PageProps) => { | ||
const { postId } = await params | ||
const { user } = await validateRequest() | ||
|
||
if (!user) { | ||
return <p className="text-destructive">You're not authorized to view this page.</p> | ||
} | ||
const post = await getPost(postId, user.id) | ||
|
||
return ( | ||
<main className="container mx-auto flex min-h-[calc(100vh-125px)] w-full grow gap-5 p-5"> | ||
<Menubar className="sticky top-[5.25rem] hidden h-fit flex-none space-y-3 bg-card px-3 py-5 shadow-sm sm:block lg:px-5 xl:w-80" /> | ||
<div className="w-full min-w-0 space-y-5"> | ||
<Post post={post} /> | ||
</div> | ||
<div className="sticky top-[5.25rem] hidden h-fit w-80 flex-none lg:block"> | ||
<Suspense fallback={<Loader2 className="mx-auto animate-spin" />}> | ||
<AuthorInfoSidebar user={post.author} /> | ||
</Suspense> | ||
</div> | ||
</main> | ||
) | ||
} | ||
|
||
interface AuthorInfoSidebarProps { | ||
user: UserData | ||
} | ||
|
||
const AuthorInfoSidebar = async ({ user }: AuthorInfoSidebarProps) => { | ||
const { user: currentUser } = await validateRequest() | ||
// await new Promise((resolve) => setTimeout(resolve, 5000)) | ||
if (!currentUser) { | ||
return null | ||
} | ||
|
||
return ( | ||
<div className="space-y-5 rounded-xl bg-card p-5 shadow-sm"> | ||
<div className="text-xl font-bold">About the author</div> | ||
<UserTooltip user={user}> | ||
<Link href={`/users/${user.userName}`} className="flex items-center gap-3"> | ||
<Avatar url={user.avatarUrl} className="flex-none" /> | ||
<div className=""> | ||
<p className="line-clamp-1 break-all font-semibold hover:underline">{user.displayName}</p> | ||
<p className="line-clamp-1 break-all text-muted-foreground">@{user.userName}</p> | ||
</div> | ||
</Link> | ||
</UserTooltip> | ||
<Linkify> | ||
<div className="line-clamp-6 whitespace-pre-line break-words text-muted-foreground">{user.bio}</div> | ||
</Linkify> | ||
{user.id !== currentUser.id && ( | ||
<FollowButton | ||
userId={user.id} | ||
initialState={{ | ||
followers: user._count.followers, | ||
isFollowedByUser: user.followers.some(({ followerId }) => followerId === currentUser.id) | ||
}} | ||
/> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default PostPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters