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