From fd33bcced9ed1346cc638cd001da3c42075aa182 Mon Sep 17 00:00:00 2001 From: Ryan Freeman Date: Thu, 27 Mar 2025 16:35:18 +0000 Subject: [PATCH] Comment form improvements --- components/ui/Comments.tsx | 69 ++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/components/ui/Comments.tsx b/components/ui/Comments.tsx index 5c5a201..cc372f8 100644 --- a/components/ui/Comments.tsx +++ b/components/ui/Comments.tsx @@ -1,6 +1,6 @@ 'use client' -import React, {useActionState} from 'react' +import React, {ReactNode, useActionState} from 'react' import {useSession} from 'next-auth/react' import Image from 'next/image' import useSWR from 'swr' @@ -27,36 +27,51 @@ type CommentsListProps = { } Comments.List = function List({comments}: CommentsListProps) { + if (!(comments?.length > 0)) return null + return (

- {comments?.length > 0 ? 'Comments' : 'No comments yet'} + Comments

- {comments && - <> - {comments.map((comment) => ( -
- {comment.user.name} -
-
-

{comment.user.name}

-

- -

-
-

{comment.content}

-
-
- ))} - - } + {comments.map((comment) => ( +
+ {comment.user.name} +
+
+

{comment.user.name}

+

+ +

+
+

{comment.content}

+
+
+ ))}
) } +type CommentsStatusProps = { + children: ReactNode +} + +Comments.Status = function Status({children}: CommentsStatusProps) { + const conditions = ['error', 'problem'] + const isError = conditions.some(condition => children?.toString().toLowerCase().includes(condition)) + + return ( +

+ {children} +

+ ) +} + type InitialState = { message: string } @@ -103,12 +118,10 @@ Comments.Form = function Form({slug}: CommentsProps) { /> -
-

+

+ {state?.message} -

+