mirror of
https://github.com/r-freeman/portfolio.git
synced 2025-05-03 06:30:20 +00:00
Render comments in client
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m9s
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m9s
This commit is contained in:
parent
6a940d627a
commit
42c9dc2162
@ -1,8 +1,10 @@
|
|||||||
import {createClient} from '@/lib/supabase/client'
|
|
||||||
import {QueryData} from '@supabase/supabase-js'
|
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 {
|
try {
|
||||||
const supabase = await createClient()
|
const supabase = await createClient()
|
||||||
const commentsQuery = supabase
|
const commentsQuery = supabase
|
||||||
@ -30,7 +32,8 @@ export async function getComments(slug: string) {
|
|||||||
return acc
|
return acc
|
||||||
}, {})
|
}, {})
|
||||||
|
|
||||||
return comments?.reduce<Comment[]>((nested, comment) => {
|
// @ts-ignore
|
||||||
|
const nestedComments = comments?.reduce<Comment[]>((nested, comment) => {
|
||||||
if (typeof commentMap !== 'undefined') {
|
if (typeof commentMap !== 'undefined') {
|
||||||
if (comment.parent_id !== null) {
|
if (comment.parent_id !== null) {
|
||||||
const parent: Comment = commentMap[comment.parent_id]
|
const parent: Comment = commentMap[comment.parent_id]
|
||||||
@ -41,10 +44,13 @@ export async function getComments(slug: string) {
|
|||||||
} else {
|
} else {
|
||||||
nested.push(commentMap[comment.id])
|
nested.push(commentMap[comment.id])
|
||||||
}
|
}
|
||||||
|
return nested
|
||||||
}
|
}
|
||||||
return nested
|
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
return new Response(JSON.stringify(nestedComments), {status: 200})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
|
return new Response(JSON.stringify({status: 'Internal Server Error'}), {status: 500})
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,7 +8,6 @@ import ArticleNav from '@/components/ui/ArticleNav'
|
|||||||
import {Comments} from '@/components/ui/Comments'
|
import {Comments} from '@/components/ui/Comments'
|
||||||
import {Subscribe} from '@/components/ui/Subscribe'
|
import {Subscribe} from '@/components/ui/Subscribe'
|
||||||
import {getAllArticles} from '@/lib/getAllArticles'
|
import {getAllArticles} from '@/lib/getAllArticles'
|
||||||
import {getComments} from '@/lib/getComments'
|
|
||||||
import {format} from 'date-fns'
|
import {format} from 'date-fns'
|
||||||
|
|
||||||
type ArticleLayout = {
|
type ArticleLayout = {
|
||||||
@ -52,7 +51,6 @@ export async function ArticleLayout({
|
|||||||
slug,
|
slug,
|
||||||
children
|
children
|
||||||
}: ArticleLayout) {
|
}: ArticleLayout) {
|
||||||
const comments = await getComments(slug)
|
|
||||||
const articles = await getAllArticles(false)
|
const articles = await getAllArticles(false)
|
||||||
const {prev, next} = findAdjacentArticles(articles, slug)
|
const {prev, next} = findAdjacentArticles(articles, slug)
|
||||||
|
|
||||||
@ -85,7 +83,7 @@ export async function ArticleLayout({
|
|||||||
<Prose className="mt-8" data-mdx-content>{children}</Prose>
|
<Prose className="mt-8" data-mdx-content>{children}</Prose>
|
||||||
</article>
|
</article>
|
||||||
<Subscribe/>
|
<Subscribe/>
|
||||||
<Comments slug={slug} comments={comments}/>
|
<Comments slug={slug}/>
|
||||||
<ArticleNav prev={prev} next={next}/>
|
<ArticleNav prev={prev} next={next}/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -11,7 +11,9 @@ import {GitHubIcon} from '@/components/icons/SocialIcons'
|
|||||||
import {ArrowLeftIcon} from '@/components/icons/ArrowLeftIcon'
|
import {ArrowLeftIcon} from '@/components/icons/ArrowLeftIcon'
|
||||||
import {StatusMessage} from '@/components/ui/StatusMessage'
|
import {StatusMessage} from '@/components/ui/StatusMessage'
|
||||||
import {getShortDurationFromNow, truncateDatetime} from '@/lib/dateFns'
|
import {getShortDurationFromNow, truncateDatetime} from '@/lib/dateFns'
|
||||||
|
import fetcher from '@/lib/fetcher'
|
||||||
import CommentFormProvider, {useCommentFormContext} from '@/app/context/CommentFormProvider'
|
import CommentFormProvider, {useCommentFormContext} from '@/app/context/CommentFormProvider'
|
||||||
|
import useSWR from 'swr'
|
||||||
|
|
||||||
import type {Comment} from '@/types'
|
import type {Comment} from '@/types'
|
||||||
|
|
||||||
@ -19,7 +21,6 @@ type ReplyButton = {
|
|||||||
comment: Comment
|
comment: Comment
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Comments.ReplyButton = function ReplyButton({comment}: ReplyButton) {
|
Comments.ReplyButton = function ReplyButton({comment}: ReplyButton) {
|
||||||
const commentFormContext = useCommentFormContext()
|
const commentFormContext = useCommentFormContext()
|
||||||
const {data: session} = useSession()
|
const {data: session} = useSession()
|
||||||
@ -209,15 +210,21 @@ Comments.Form = function Form({slug}: { slug: string }) {
|
|||||||
|
|
||||||
type CommentsProps = {
|
type CommentsProps = {
|
||||||
slug: string
|
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 (
|
return (
|
||||||
<CommentFormProvider>
|
<CommentFormProvider>
|
||||||
<div className="mt-24">
|
<div className="mt-24">
|
||||||
{comments &&
|
{(comments && !error) ?
|
||||||
<Comments.List comments={comments}/>
|
<Comments.List comments={comments}/>
|
||||||
|
: null
|
||||||
}
|
}
|
||||||
<Comments.Form slug={slug}/>
|
<Comments.Form slug={slug}/>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user