'use client'; import { useState, useEffect } from 'react'; import Image from 'next/image'; import Link from 'next/link'; import { useParams, useRouter } from 'next/navigation'; import { ChevronRight, Clock, Calendar, Share2, ArrowLeft, Facebook, Twitter, Linkedin } from 'lucide-react'; import { blogService, Blog } from '../../services/blogService'; // Adjust path as needed const BlogDetail: React.FC = () => { const params = useParams(); const router = useRouter(); const blogId = params.id as string; const [blogData, setBlogData] = useState(null); const [relatedPosts, setRelatedPosts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [mounted, setMounted] = useState(false); useEffect(() => { const fetchBlogData = async () => { if (!blogId) return; setLoading(true); setError(null); try { // Convert string ID to number for API call const numericId = parseInt(blogId, 10); if (isNaN(numericId)) { throw new Error('Invalid blog ID'); } console.log('Fetching blog with ID:', numericId); // Fetch the specific blog and related posts const [blog, allBlogs] = await Promise.all([ blogService.getBlogById(numericId), blogService.getPostedBlogs() ]); if (!blog) { setError('Blog not found'); return; } setBlogData(blog); // Get related posts (same tags, exclude current blog) const related = allBlogs .filter(b => b.id !== blog.id && b.tags.some(tag => blog.tags.includes(tag))) .slice(0, 3); // If not enough related posts with same tags, fill with other recent posts if (related.length < 3) { const otherPosts = allBlogs .filter(b => b.id !== blog.id && !related.some(r => r.id === b.id)) .slice(0, 3 - related.length); related.push(...otherPosts); } setRelatedPosts(related); } catch (err) { console.error('Error fetching blog:', err); setError('Failed to load blog. Please try again later.'); } finally { setLoading(false); } }; if (mounted) { fetchBlogData(); } }, [blogId, mounted]); useEffect(() => { setMounted(true); }, []); const handleGoBack = () => { router.back(); }; const handleShare = async (platform?: string) => { if (!blogData || typeof window === 'undefined') return; const url = window.location.href; const title = blogData.title; try { if (platform === 'facebook') { window.open(`https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(url)}`, '_blank'); } else if (platform === 'twitter') { window.open(`https://twitter.com/intent/tweet?url=${encodeURIComponent(url)}&text=${encodeURIComponent(title)}`, '_blank'); } else if (platform === 'linkedin') { window.open(`https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(url)}`, '_blank'); } else { // Generic share or copy link if (navigator.share) { await navigator.share({ title: title, url: url, }); } else { await navigator.clipboard.writeText(url); alert('Blog link copied to clipboard!'); } } } catch (error) { console.error('Share failed:', error); } }; const handleImageError = (event: React.SyntheticEvent) => { const target = event.target as HTMLImageElement; target.src = '/images/default-blog-image.jpg'; }; const getAuthorName = (blog: Blog) => { if (blog.professors && blog.professors.length > 0) { return blog.professors.map(prof => prof.firstName || prof.name).join(', '); } return 'Medical Team'; }; const getAuthorBio = (blog: Blog) => { if (blog.professors && blog.professors.length > 0) { return `Medical professional${blog.professors.length > 1 ? 's' : ''} specializing in trauma care and mental health support.`; } return 'Our medical team consists of experienced professionals dedicated to trauma care and mental health support.'; }; if (!mounted) { return null; } if (loading) { return (

Loading blog...

); } if (error || !blogData) { return (
{/* Breadcrumb Section */}

{error || 'Blog Not Found'}

); } return (
{/* Breadcrumb Section */}
{/* Back Button */}
{/* Article Content */}
{/* Article Header */}
{/* Tags */}
{blogData.tags.map((tag, index) => ( {tag} ))}
{/* Title */}

{blogData.title}

{/* Meta Information */}
{new Date(blogData.publishDate).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })}
{blogData.readTime}
{/* Author Info */}
{getAuthorName(blogData).charAt(0)}

{getAuthorName(blogData)}

{getAuthorBio(blogData)}

{/* Share Buttons */}
Share:
{/* Article Content */}
{/* Related Posts */} {relatedPosts.length > 0 && (

Related Articles

{relatedPosts.map((post) => (
{post.title}

{post.title}

{post.readTime}
))}
)} {/* CTA Section */}

Need Professional Support?

If you or someone you know is struggling with trauma, don't hesitate to reach out for professional help. Our team is here to support you on your healing journey.

Get Support Today
); }; export default BlogDetail;