From 42c9dc2162a22ed647458de4e4f2cfc1f736ba0a Mon Sep 17 00:00:00 2001 From: Ryan Freeman Date: Tue, 29 Apr 2025 22:30:19 +0100 Subject: [PATCH] Render comments in client --- .../api/comments/[slug]/route.ts | 16 +++++++++++----- components/layouts/ArticleLayout.tsx | 4 +--- components/ui/Comments.tsx | 15 +++++++++++---- 3 files changed, 23 insertions(+), 12 deletions(-) rename lib/getComments.ts => app/api/comments/[slug]/route.ts (74%) diff --git a/lib/getComments.ts b/app/api/comments/[slug]/route.ts similarity index 74% rename from lib/getComments.ts rename to app/api/comments/[slug]/route.ts index 4b69600..9382766 100644 --- a/lib/getComments.ts +++ b/app/api/comments/[slug]/route.ts @@ -1,8 +1,10 @@ -import {createClient} from '@/lib/supabase/client' import {QueryData} from '@supabase/supabase-js' -import type {Comment} from '@/types' +import {createClient} from '@/lib/supabase/server' +import {Comment} from '@/types' + +export async function GET(request: Request, {params}: { params: Promise<{ slug: string }> }) { + const {slug} = await params -export async function getComments(slug: string) { try { const supabase = await createClient() const commentsQuery = supabase @@ -30,7 +32,8 @@ export async function getComments(slug: string) { return acc }, {}) - return comments?.reduce((nested, comment) => { + // @ts-ignore + const nestedComments = comments?.reduce((nested, comment) => { if (typeof commentMap !== 'undefined') { if (comment.parent_id !== null) { const parent: Comment = commentMap[comment.parent_id] @@ -41,10 +44,13 @@ export async function getComments(slug: string) { } else { nested.push(commentMap[comment.id]) } + return nested } - return nested }, []) + + return new Response(JSON.stringify(nestedComments), {status: 200}) } catch (error) { console.error(error) + return new Response(JSON.stringify({status: 'Internal Server Error'}), {status: 500}) } } \ No newline at end of file diff --git a/components/layouts/ArticleLayout.tsx b/components/layouts/ArticleLayout.tsx index d18c0b2..fa87437 100644 --- a/components/layouts/ArticleLayout.tsx +++ b/components/layouts/ArticleLayout.tsx @@ -8,7 +8,6 @@ import ArticleNav from '@/components/ui/ArticleNav' import {Comments} from '@/components/ui/Comments' import {Subscribe} from '@/components/ui/Subscribe' import {getAllArticles} from '@/lib/getAllArticles' -import {getComments} from '@/lib/getComments' import {format} from 'date-fns' type ArticleLayout = { @@ -52,7 +51,6 @@ export async function ArticleLayout({ slug, children }: ArticleLayout) { - const comments = await getComments(slug) const articles = await getAllArticles(false) const {prev, next} = findAdjacentArticles(articles, slug) @@ -85,7 +83,7 @@ export async function ArticleLayout({ {children} - + diff --git a/components/ui/Comments.tsx b/components/ui/Comments.tsx index 59bcbe4..da9dae5 100644 --- a/components/ui/Comments.tsx +++ b/components/ui/Comments.tsx @@ -11,7 +11,9 @@ import {GitHubIcon} from '@/components/icons/SocialIcons' import {ArrowLeftIcon} from '@/components/icons/ArrowLeftIcon' import {StatusMessage} from '@/components/ui/StatusMessage' import {getShortDurationFromNow, truncateDatetime} from '@/lib/dateFns' +import fetcher from '@/lib/fetcher' import CommentFormProvider, {useCommentFormContext} from '@/app/context/CommentFormProvider' +import useSWR from 'swr' import type {Comment} from '@/types' @@ -19,7 +21,6 @@ type ReplyButton = { comment: Comment } - Comments.ReplyButton = function ReplyButton({comment}: ReplyButton) { const commentFormContext = useCommentFormContext() const {data: session} = useSession() @@ -209,15 +210,21 @@ Comments.Form = function Form({slug}: { slug: string }) { type CommentsProps = { slug: string - comments?: any } -export function Comments({slug, comments}: CommentsProps) { +export function Comments({slug}: CommentsProps) { + const {data: comments, error, isLoading} = useSWR(`/api/comments/${slug}`, fetcher) as { + data: Comment[], + error: Error, + isLoading: boolean + } + return (
- {comments && + {(comments && !error) ? + : null }