import { sql } from '@/lib/db' export const dynamic = "force-dynamic" import { notFound } from 'next/navigation' import Link from 'next/link' import Image from 'next/image' import AdminButton from '@/components/AdminButton' async function getArticleWithTags(slug: string) { try { const { rows } = await sql` SELECT a.*, c.name as category_name, c.name_burmese as category_name_burmese, c.slug as category_slug, COALESCE( array_agg(t.name_burmese) FILTER (WHERE t.id IS NOT NULL), ARRAY[]::VARCHAR[] ) as tags_burmese, COALESCE( array_agg(t.slug) FILTER (WHERE t.id IS NOT NULL), ARRAY[]::VARCHAR[] ) as tag_slugs FROM articles a JOIN categories c ON a.category_id = c.id LEFT JOIN article_tags at ON a.id = at.article_id LEFT JOIN tags t ON at.tag_id = t.id WHERE a.slug = ${slug} AND a.status = 'published' GROUP BY a.id, c.id ` if (rows.length === 0) return null // Increment view count await sql`SELECT increment_view_count(${slug})` return rows[0] } catch (error) { console.error('Error fetching article:', error) return null } } async function getRelatedArticles(articleId: number) { try { const { rows } = await sql`SELECT * FROM get_related_articles(${articleId}, 6)` return rows } catch (error) { return [] } } export default async function ImprovedArticlePage({ params }: { params: { slug: string } }) { const article = await getArticleWithTags(params.slug) if (!article) { notFound() } const relatedArticles = await getRelatedArticles(article.id) const publishedDate = new Date(article.published_at).toLocaleDateString('my-MM', { year: 'numeric', month: 'long', day: 'numeric' }) return (
{/* Hero Cover Image */} {article.featured_image && (
{article.title_burmese}
{/* Category */} {article.category_name_burmese} {/* Title */}

{article.title_burmese}

{/* Meta */}
{publishedDate} {article.reading_time} မိနစ် {article.view_count} views
)} {/* Article Content */}
{/* Tags */} {article.tags_burmese && article.tags_burmese.length > 0 && (
{article.tags_burmese.map((tag: string, idx: number) => ( #{tag} ))}
)} {/* Article Body */}
{/* Additional Images Gallery */} {article.images && article.images.length > 1 && (

ဓာတ်ပုံများ

{article.images.slice(1).map((img: string, idx: number) => (
{`${article.title_burmese}
))}
)} {/* Videos */} {article.videos && article.videos.length > 0 && (

ဗီဒီယိုများ

{article.videos.map((video: string, idx: number) => (
{renderVideo(video)}
))}
)}
{/* Source Attribution */} {article.source_articles && article.source_articles.length > 0 && (

မူရင်းသတင်းရင်းမြစ်များ

ဤဆောင်းပါးကို အောက်ပါမူရင်းသတင်းများမှ စုစည်း၍ မြန်မာဘာသာသို့ ပြန်ဆိုထားခြင်း ဖြစ်ပါသည်။ အားလုံးသော အကြွေးအရ မူရင်းစာရေးသူများနှင့် ထုတ်ပြန်သူများကို သက်ဆိုင်ပါသည်။

{article.source_articles.map((source: any, index: number) => (
{index + 1}
{source.title} {source.author && source.author !== 'Unknown' && (

စာရေးသူ: {source.author}

)}
))}
)} {/* Share Section */}

မျှဝေပါ:

{/* Related Articles */} {relatedArticles.length > 0 && (

ဆက်စပ်ဆောင်းပါးများ

{relatedArticles.map((related: any) => ( {related.featured_image && (
{related.title_burmese}
)}

{related.title_burmese}

{related.excerpt_burmese}

))}
)} {/* Admin Button (hidden, press Alt+Shift+A to show) */}
) } function formatContent(content: string): string { let formatted = content .replace(/\n\n/g, '

') .replace(/## (.*?)\n/g, '

$1

') .replace(/### (.*?)\n/g, '

$1

') .replace(/\*\*(.*?)\*\*/g, '$1') .replace(/\*(.*?)\*/g, '$1') return `

${formatted}

` } function renderVideo(videoUrl: string) { let videoId = null if (videoUrl.includes('youtube.com/watch')) { const match = videoUrl.match(/v=([^&]+)/) videoId = match ? match[1] : null } else if (videoUrl.includes('youtu.be/')) { const match = videoUrl.match(/youtu\.be\/([^?]+)/) videoId = match ? match[1] : null } else if (videoUrl.includes('youtube.com/embed/')) { const match = videoUrl.match(/embed\/([^?]+)/) videoId = match ? match[1] : null } if (videoId) { return (