'use client';
import { useEffect } from 'react';
import { useParams, useRouter } from 'next/navigation';
import Link from 'next/link';
import { motion } from 'framer-motion';
import { useBlogPost } from '../../../services/blog';
import { assetUrl } from '../../../services/api';
import Loader from '../../../components/Loader';
function renderContent(content: string) {
return content.split('\n').map((line, i) => {
if (line.startsWith('# '))
return
{line.slice(2)}
;
if (line.startsWith('## '))
return {line.slice(3)}
;
if (line.startsWith('- '))
return {line.slice(2)};
if (line.trim() === '')
return
;
return {line}
;
});
}
export default function ArticlePage() {
const params = useParams();
const router = useRouter();
const slug = typeof params.slug === 'string' ? params.slug : params.slug?.[0] ?? '';
const { data: post, isLoading, isError } = useBlogPost(slug);
useEffect(() => {
if (post) document.title = `${post.title} | Blog`;
}, [post]);
if (isLoading) return ;
if (isError || !post) {
return (
ARTÍCULO NO ENCONTRADO
);
}
const imageUrl = assetUrl(post.imageUrl);
const publishDate = post.publishedAt
? new Date(post.publishedAt).toLocaleDateString('es-MX', { day: '2-digit', month: 'long', year: 'numeric' })
: null;
return (
{/* Hero */}
{/* Breadcrumb */}
BLOG
/
{post.category.toUpperCase()}
{/* Categoría */}
{post.category.toUpperCase()}
{/* Título */}
{post.title}
{/* Resumen */}
{post.summary && (
{post.summary}
)}
{/* Meta */}
{post.authorName && (
{post.authorName.charAt(0).toUpperCase()}
{post.authorName}
)}
{publishDate && (
<>
{post.authorName &&
·}
{publishDate.toUpperCase()}
>
)}
{/* Imagen de portada */}
{imageUrl && (
)}
{/* Contenido */}
{renderContent(post.content)}
{/* Volver */}
← VOLVER AL BLOG
);
}