From f8d668e4f32c59a25cf641ba84bfeec48cba2b59 Mon Sep 17 00:00:00 2001 From: Ryan Freeman Date: Thu, 27 Mar 2025 20:53:49 +0000 Subject: [PATCH] Render comments on server --- app/api/comments/[slug]/route.ts | 32 ---------------------------- components/layouts/ArticleLayout.tsx | 4 +++- components/ui/Comments.tsx | 19 +++++------------ lib/getComments.ts | 24 +++++++++++++++++++++ 4 files changed, 32 insertions(+), 47 deletions(-) delete mode 100644 app/api/comments/[slug]/route.ts create mode 100644 lib/getComments.ts diff --git a/app/api/comments/[slug]/route.ts b/app/api/comments/[slug]/route.ts deleted file mode 100644 index 3d93fcb..0000000 --- a/app/api/comments/[slug]/route.ts +++ /dev/null @@ -1,32 +0,0 @@ -import {createClient} from '@/lib/supabase/server' -import {NextResponse} from 'next/server' - -export async function GET(request: Request, {params}: { params: Promise<{ slug: string }> }) { - const {slug} = await params - if (typeof slug !== 'undefined') { - try { - const supabase = await createClient() - const {data: comments, error} = await supabase - .from('comments') - .select(` - id, - content, - published, - created_at, - user:users!inner(id, name, image), - article:articles!inner(id, title, slug) - `) - .eq('article.slug', slug) - .eq('published', true) - .order('created_at', {ascending: false}) - - if (comments !== null && comments?.length > 0) { - return NextResponse.json({comments: comments}) - } - return NextResponse.json([]) - } catch (e) { - return new Response(JSON.stringify({status: 'Internal Server Error'}), {status: 500}) - } - } - return new Response(JSON.stringify({status: 'Not Found'}), {status: 404}) -} diff --git a/components/layouts/ArticleLayout.tsx b/components/layouts/ArticleLayout.tsx index e4d5a6e..b92e9fd 100644 --- a/components/layouts/ArticleLayout.tsx +++ b/components/layouts/ArticleLayout.tsx @@ -8,6 +8,7 @@ import {formatDate} from '@/lib/formatDate' import ArticleNav from '@/components/ui/ArticleNav' import Comments from '@/components/ui/Comments' import {getAllArticles} from '@/lib/getAllArticles' +import {getComments} from '@/lib/getComments' type ArticleLayout = { title: string @@ -50,6 +51,7 @@ export async function ArticleLayout({ slug, children }: ArticleLayout) { + const comments = await getComments(slug) const articles = await getAllArticles(false) const {prev, next} = findAdjacentArticles(articles, slug) @@ -81,7 +83,7 @@ export async function ArticleLayout({ {children} - + diff --git a/components/ui/Comments.tsx b/components/ui/Comments.tsx index cc372f8..4c6ac6f 100644 --- a/components/ui/Comments.tsx +++ b/components/ui/Comments.tsx @@ -3,9 +3,7 @@ import React, {ReactNode, useActionState} from 'react' import {useSession} from 'next-auth/react' import Image from 'next/image' -import useSWR from 'swr' import clsx from 'clsx' -import fetcher from '@/lib/fetcher' import {formatDate} from '@/lib/formatDate' import {addComment, loginWithGitHub} from '@/app/actions/comments' import {Button} from '@/components/ui/Button' @@ -65,7 +63,7 @@ Comments.Status = function Status({children}: CommentsStatusProps) { return (

{children}

@@ -134,21 +132,14 @@ Comments.Form = function Form({slug}: CommentsProps) { type CommentsProps = { slug: string + comments?: any } -export default function Comments({slug}: CommentsProps) { - const {data, isLoading, error} = useSWR(`/api/comments/${slug}`, fetcher) as { - data: { comments: Comment[] }, - isLoading: boolean, - error: string - } - - if (error) return null - +export default function Comments({slug, comments}: CommentsProps) { return (
- {!isLoading && - + {comments && + }
diff --git a/lib/getComments.ts b/lib/getComments.ts new file mode 100644 index 0000000..e889823 --- /dev/null +++ b/lib/getComments.ts @@ -0,0 +1,24 @@ +import {createClient} from '@/lib/supabase/client' + +export async function getComments(slug: string) { + try { + const supabase = await createClient() + const {data: comments, error} = await supabase + .from('comments') + .select(` + id, + content, + published, + created_at, + user:users!inner(id, name, image), + article:articles!inner(id, title, slug) + `) + .eq('article.slug', slug) + .eq('published', true) + .order('created_at', {ascending: false}) + + return comments + } catch (error) { + console.error(error) + } +} \ No newline at end of file