"use client"; import { useActionState, useId, useState } from "react"; import { Button, ErrorText, HelpText, Input, Label, Textarea } from "@/components/ui"; import { type FormState, firstFieldError, initialFormState } from "@/lib/forms"; import { formatDate, isoDate } from "@/lib/format"; import type { PublicComment } from "@/lib/services/comments"; type CommentAction = (prev: FormState, formData: FormData) => Promise; function countComments(list: PublicComment[]): number { return list.reduce((sum, c) => sum + 1 + countComments(c.replies), 0); } function CommentForm({ postId, parentId, action, onCancel, }: { postId: number; parentId: number | null; action: CommentAction; onCancel?: () => void; }) { const [state, formAction] = useActionState(action, initialFormState); const ids = useId(); const err = (field: string) => firstFieldError(state, field); if (state.status === "success") { return (

Thanks! Your comment is awaiting moderation and will appear once approved.

); } return (
{state.formError && (

{state.formError}

)} {/* Honeypot — hidden from people, tempting to bots. */}
{err("authorName")}
{err("authorEmail")}