"use client"; import { useState, useTransition } from "react"; import { getImageProps } from "next/image"; import { toast } from "sonner"; import { InfoIcon, Loader2Icon } from "lucide-react"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import Button from "@/components/ui/button"; import Textarea from "@/components/ui/textarea"; import Link from "@/components/link"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { MarkdownIcon } from "@/components/icons"; import { useSession } from "@/lib/auth-client"; import { createComment, updateComment } from "@/lib/server/comments"; const CommentForm = ({ slug, parentId, commentId, initialContent = "", onCancel, onSuccess, }: { slug: string; parentId?: string; commentId?: string; initialContent?: string; onCancel?: () => void; onSuccess?: () => void; }) => { const [content, setContent] = useState(initialContent); const [isPending, startTransition] = useTransition(); const isEditing = !!commentId; const isReplying = !!parentId; const { data: session } = useSession(); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!content.trim()) { toast.error("Comment cannot be empty."); return; } startTransition(async () => { try { if (isEditing) { await updateComment(commentId, content); toast.success("Comment updated!"); } else { await createComment({ content, parentId, pageSlug: slug, }); toast.success("Comment posted!"); } // Reset form if not editing if (!isEditing) { setContent(""); } // Call success callback if provided onSuccess?.(); } catch (error) { console.error("Error submitting comment:", error); toast.error("Failed to submit comment. Please try again."); } }); }; return (
{!isEditing && (
{session?.user.image && ( )} {session?.user.name.charAt(0).toUpperCase()}
)}