"use client"; import React, { useState, useEffect } from "react"; import DOMPurify from 'dompurify'; import { ChevronRight, Calendar, User, Clock, Tag, Share2, MessageSquare, Sparkles, ShoppingBag, ArrowLeft, Send, Eye, CheckCircle2, } from "lucide-react"; import Link from 'next/link'; import SafeImage from "./SafeImage"; import api from "../lib/services/api"; interface BlogPostClientProps { blog: any; publishedDate: string; authorName: string; } export default function BlogPostClient({ blog, publishedDate, authorName }: BlogPostClientProps) { const [copied, setCopied] = useState(false); const [viewCount, setViewCount] = useState(blog.viewCount || 0); const [commentName, setCommentName] = useState(""); const [commentEmail, setCommentEmail] = useState(""); const [commentText, setCommentText] = useState(""); const [commentSent, setCommentSent] = useState(false); const [isSubmittingComment, setIsSubmittingComment] = useState(false); const [commentError, setCommentError] = useState(""); const relatedProducts = Array.isArray(blog.relatedProducts) ? blog.relatedProducts : []; const relatedBlogs = Array.isArray(blog.relatedBlogs) ? blog.relatedBlogs : []; const comments = Array.isArray(blog.comments) ? blog.comments : []; useEffect(() => { if (!blog?.slug) return; const viewKey = `viewed_blog_${blog.slug}`; const hasViewed = sessionStorage.getItem(viewKey); api.post(`/blogs/${encodeURIComponent(blog.slug)}/view`) .then((res) => { if (res.data?.viewCount !== undefined) { setViewCount(res.data.viewCount); } else { setViewCount((prev) => prev + (hasViewed ? 0 : 1)); } sessionStorage.setItem(viewKey, '1'); }) .catch((err) => { console.error('Failed to register view count:', err); }); }, [blog?.slug]); const handleShare = () => { if (typeof window !== 'undefined') { if (navigator.share) { navigator.share({ title: blog.title, url: window.location.href }).catch(() => null); } else { navigator.clipboard.writeText(window.location.href); setCopied(true); setTimeout(() => setCopied(false), 3000); } } }; const handleCommentSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!commentText.trim() || !commentName.trim()) return; try { setIsSubmittingComment(true); setCommentError(""); await api.post(`/blogs/${encodeURIComponent(blog.slug || blog.id)}/comments`, { userName: commentName.trim(), email: commentEmail.trim() || undefined, content: commentText.trim(), }); setCommentSent(true); setCommentName(""); setCommentEmail(""); setCommentText(""); } catch (err: any) { console.error('Failed to submit comment:', err); setCommentError(err.response?.data?.message || "خطا در ارسال دیدگاه. لطفاً دوباره تلاش کنید."); } finally { setIsSubmittingComment(false); } }; return (
{/* Breadcrumbs */}
خانه مجله سلامت {blog.category && ( <> {blog.category.name} )} {blog.title}
{/* Main Article Content Column */}
{/* Featured Image */} {blog.imageUrl && (
{blog.imageCaption && (
{blog.imageCaption}
)}
)} {/* Meta details bar */}
{blog.category && ( {blog.category.name} )} {publishedDate && (
{publishedDate}
)}
{blog.readingTime || 4} دقیقه مطالعه
{Number(viewCount).toLocaleString('fa-IR')} بازدید
{/* Title */}

{blog.title}

{/* Excerpt / Summary Callout */} {blog.excerpt && (
{blog.excerpt}
)} {/* Main Rich Content HTML Renderer */}
{/* Tags Chip Footer */} {Array.isArray(blog.tags) && blog.tags.length > 0 && (
برچسب‌های مرتبط: {blog.tags.map((tag: any, idx: number) => { const tagName = typeof tag === 'string' ? tag : tag?.name || ''; const tagId = tag?.id || idx; if (!tagName) return null; return ( #{tagName} ); })}
)} {/* Author Box */}
{authorName}
تیم تحقیق و توسعه علمی کنینا آلمان (Canina Pharma)
{/* Comments Section */} {blog.allowComments !== false && (

دیدگاه‌ها و پرسش‌های کاربران ({comments.length})

{commentSent ? (
دیدگاه شما با موفقیت ثبت شد و پس از بررسی و تایید تیم علمی، در سایت نمایش داده می‌شود.
) : (
پرسش یا تجربه خود درباره این موضوع را با ما در میان بگذارید:
setCommentName(e.target.value)} placeholder="نام و نام خانوادگی..." className="px-4 py-2.5 bg-white rounded-xl border border-medical-gray-200 text-xs font-bold outline-none" /> setCommentEmail(e.target.value)} placeholder="آدرس ایمیل..." className="px-4 py-2.5 bg-white rounded-xl border border-medical-gray-200 text-xs font-bold outline-none" />