Comment form improvements
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m6s

This commit is contained in:
Ryan Freeman 2025-03-27 16:35:18 +00:00
parent a6e8c373b8
commit fd33bcced9

View File

@ -1,6 +1,6 @@
'use client' 'use client'
import React, {useActionState} from 'react' import React, {ReactNode, useActionState} from 'react'
import {useSession} from 'next-auth/react' import {useSession} from 'next-auth/react'
import Image from 'next/image' import Image from 'next/image'
import useSWR from 'swr' import useSWR from 'swr'
@ -27,19 +27,19 @@ type CommentsListProps = {
} }
Comments.List = function List({comments}: CommentsListProps) { Comments.List = function List({comments}: CommentsListProps) {
if (!(comments?.length > 0)) return null
return ( return (
<section> <section>
<h3 className="text-base font-semibold tracking-tight text-zinc-800 dark:text-zinc-100 mb-4"> <h3 className="text-base font-semibold tracking-tight text-zinc-800 dark:text-zinc-100 mb-4">
{comments?.length > 0 ? 'Comments' : 'No comments yet'} Comments
</h3> </h3>
{comments &&
<>
{comments.map((comment) => ( {comments.map((comment) => (
<article key={comment.id} className="flex gap-x-4 py-5"> <article key={comment.id} className="flex gap-x-4 py-5">
<Image src={comment.user.image} alt={comment.user.name} width={64} height={64} <Image src={comment.user.image} alt={comment.user.name} width={64} height={64}
className="size-12 rounded-full"/> className="size-12 rounded-full"/>
<div className="flex-auto"> <div className="flex-auto">
<div className="flex items-baseline gap-x-2"> <div className="flex items-baseline gap-x-1">
<p className="font-semibold text-sm text-zinc-800 dark:text-zinc-100">{comment.user.name}</p> <p className="font-semibold text-sm text-zinc-800 dark:text-zinc-100">{comment.user.name}</p>
<p className="text-sm text-zinc-500 dark:text-zinc-400"> <p className="text-sm text-zinc-500 dark:text-zinc-400">
<time dateTime={comment.created_at}> <time dateTime={comment.created_at}>
@ -51,12 +51,27 @@ Comments.List = function List({comments}: CommentsListProps) {
</div> </div>
</article> </article>
))} ))}
</>
}
</section> </section>
) )
} }
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 (
<p aria-live="polite" role="status"
className={clsx('text-base font-semibold',
!isError ? 'text-green-800 dark:text-green-600' : 'text-red-800 dark:text-red-600')}>
{children}
</p>
)
}
type InitialState = { type InitialState = {
message: string message: string
} }
@ -103,12 +118,10 @@ Comments.Form = function Form({slug}: CommentsProps) {
/> />
</div> </div>
<input type="hidden" name="slug" value={slug}/> <input type="hidden" name="slug" value={slug}/>
<div className="mt-2 flex justify-between items-center"> <div className="mt-2 flex justify-between items-start gap-x-4">
<p aria-live="polite" role="status" className={clsx('text-base font-semibold', <Comments.Status>
(state?.message !== null && !state?.message.toLowerCase().includes('error')
? 'text-green-800 dark:text-green-600' : 'text-red-800 dark:text-red-600'))}>
{state?.message} {state?.message}
</p> </Comments.Status>
<Button variant="secondary" disabled={pending}> <Button variant="secondary" disabled={pending}>
Comment Comment
</Button> </Button>