import { notFound } from 'next/navigation' import { ArrowLeft, Eye, Clock } from 'lucide-react' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import Link from 'next/link' import Image from 'next/image' import { ITopic } from '@/models/topic' import { Metadata } from 'next' import { Header } from '@/components/header' import { Footer } from '@/components/footer' import { TopicViewTracker } from '@/components/topics/TopicClientComponents' import { TopicEditButton } from '@/components/topics/TopicEditButton' import { SimpleShareButtons } from '@/components/topics/SimpleShareButtons' // Force dynamic rendering for real-time updates export const dynamic = 'force-dynamic' // Server-side data fetching async function getTopicPost(slug: string): Promise { try { const baseUrl = process.env.NODE_ENV === 'production' ? `${process.env.NEXTAUTH_URL || 'https://siliconpin.com'}` : 'http://localhost:4024' const response = await fetch(`${baseUrl}/api/topics/${encodeURIComponent(slug)}`, { headers: { 'Content-Type': 'application/json', }, cache: 'no-store', }) if (!response.ok) { if (response.status === 404) { return null } throw new Error(`Failed to fetch topic post: ${response.status}`) } const result = await response.json() if (!result.success) { if (result.error?.code === 'POST_NOT_FOUND') { return null } throw new Error(result.error?.message || 'Failed to fetch topic post') } return result.data } catch (error) { console.error('Error fetching topic post:', error) return null } } async function getRelatedPosts(slug: string, limit: number = 2): Promise { try { const baseUrl = process.env.NODE_ENV === 'production' ? `${process.env.NEXTAUTH_URL || 'https://siliconpin.com'}` : 'http://localhost:4024' const response = await fetch( `${baseUrl}/api/topics/${encodeURIComponent(slug)}/related?limit=${limit}`, { headers: { 'Content-Type': 'application/json', }, cache: 'no-store', } ) if (!response.ok) { throw new Error(`Failed to fetch related posts: ${response.status}`) } const result = await response.json() if (!result.success) { throw new Error(result.error?.message || 'Failed to fetch related posts') } return result.data || [] } catch (error) { console.error('Error fetching related posts:', error) return [] } } // Generate metadata for the page export async function generateMetadata({ params, }: { params: Promise<{ slug: string }> }): Promise { const { slug } = await params const post = await getTopicPost(slug) if (!post) { return { title: 'Topic Not Found - SiliconPin', description: 'The requested topic could not be found.', } } const keywords = post.tags.map((tag) => tag.name).join(', ') return { title: `${post.title} - SiliconPin Topics`, description: post.excerpt, keywords: keywords, authors: [{ name: post.author }], openGraph: { title: post.title, description: post.excerpt, type: 'article', publishedTime: new Date(post.publishedAt).toISOString(), authors: [post.author], images: post.coverImage ? [post.coverImage] : undefined, tags: post.tags.map((tag) => tag.name), }, twitter: { card: 'summary_large_image', title: post.title, description: post.excerpt, images: post.coverImage ? [post.coverImage] : undefined, }, } } export default async function TopicPostPage({ params }: { params: Promise<{ slug: string }> }) { const { slug } = await params const post = await getTopicPost(slug) if (!post) { notFound() } const relatedPosts = await getRelatedPosts(slug) const publishedDate = new Date(post.publishedAt).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric', }) return (
{/* View tracking only - no visual component */}
{/* Back to topics and Edit button */}
{/* Header */}

{post.title}

{post.author.charAt(0)}
{post.author}
{post.readingTime?.text || `${post.readingTime?.minutes || 1} min read`} {(post.views || 0) > 0 && ( {post.views?.toLocaleString()} views )}
{post.tags.map((tag) => ( {tag.name} ))}
{/* Cover Image */}
{post.title}
{/* Content */}
{/* Tags */}

Tags

{post.tags.map((tag) => ( {tag.name} ))}
{/* Share Buttons */}
{/* Related posts */} {relatedPosts.length > 0 && (

Related Posts

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

{relatedPost.title}

{relatedPost.excerpt}

{relatedPost.tags.slice(0, 2).map((tag) => ( {tag.name} ))}
))}
)}
) }