"use client"; import { FormEvent, useState } from "react"; import { MessageSquare, Send, UserRound } from "lucide-react"; import { useAuth } from "@/components/auth-provider"; import { CUSTOMER_URL, MESHKEE_API_BASE, WEBSITE_DOMAIN, type PublicComment, } from "@/lib/meshkee"; export function CommentsPanel({ entityType, entityId, comments, }: { entityType: "product" | "blog"; entityId: string; comments: PublicComment[]; }) { const { user, loading } = useAuth(); const [status, setStatus] = useState<"idle" | "sending" | "sent" | "error">("idle"); async function submit(event: FormEvent) { event.preventDefault(); if (!user) return; const form = event.currentTarget; const data = new FormData(form); const text = String(data.get("text") ?? "").trim(); if (!text) return; setStatus("sending"); try { const authorName = [user.firstName, user.lastName].filter(Boolean).join(" ") || user.name || "کاربر گلسموند"; const response = await fetch( `${MESHKEE_API_BASE}/tenants/${WEBSITE_DOMAIN}/comments`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ entityType, entityId, authorName, authorEmail: user.email || undefined, text, }), }, ); if (!response.ok) throw new Error("Comment failed"); setStatus("sent"); form.reset(); } catch { setStatus("error"); } } return (
{comments.length > 0 ? ( comments.map((comment) => (

{comment.authorName}

{comment.text}

)) ) : (

هنوز دیدگاهی ثبت نشده است. اولین نفر باشید.

)}
{!loading && ( user ? (