import { useEffect, useState } from 'react'; import LazyImage from '../lazy-image'; import { PiNewspaper } from 'react-icons/pi'; import { getDevPost, getMediumPost } from '@arifszn/blog-js'; import { formatDistance } from 'date-fns'; import { SanitizedBlog } from '../../interfaces/sanitized-config'; import { ga, skeleton } from '../../utils'; import { Article } from '../../interfaces/article'; const BlogCard = ({ loading, blog, googleAnalyticsId, }: { loading: boolean; blog: SanitizedBlog; googleAnalyticsId?: string; }) => { const [articles, setArticles] = useState([]); useEffect(() => { if (blog.source === 'medium') { getMediumPost({ user: blog.username, }).then((res) => { setArticles(res); }); } else if (blog.source === 'dev') { getDevPost({ user: blog.username, }).then((res) => { setArticles(res); }); } }, [blog.source, blog.username]); const renderSkeleton = () => { const array = []; for (let index = 0; index < blog.limit; index++) { array.push(
{skeleton({ widthCls: 'w-full', heightCls: 'h-full', shape: '', })}

{skeleton({ widthCls: 'w-full', heightCls: 'h-8', className: 'mb-2 mx-auto md:mx-0', })}

{skeleton({ widthCls: 'w-24', heightCls: 'h-3', className: 'mx-auto md:mx-0', })}
{skeleton({ widthCls: 'w-full', heightCls: 'h-4', className: 'mx-auto md:mx-0', })}
{skeleton({ widthCls: 'w-32', heightCls: 'h-4', className: 'md:mr-2 mx-auto md:mx-0', })}
, ); } return array; }; const renderArticles = () => { return articles && articles.length ? ( articles.slice(0, blog.limit).map((article, index) => ( { e.preventDefault(); try { if (googleAnalyticsId) { ga.event('Click Blog Post', { post: article.title, }); } } catch (error) { console.error(error); } window?.open(article.link, '_blank'); }} >

{article.title}

{formatDistance(article.publishedAt, new Date(), { addSuffix: true, })}

{article.description}

{article.categories.map((category, index2) => (
#{category}
))}
)) ) : (

No recent post

); }; return (
{loading ? ( skeleton({ widthCls: 'w-12', heightCls: 'h-12', className: 'rounded-xl', }) ) : (
)}

{loading ? skeleton({ widthCls: 'w-28', heightCls: 'h-8' }) : 'My Articles'}

{loading ? skeleton({ widthCls: 'w-32', heightCls: 'h-4' }) : 'Recent posts'}
{loading || !articles ? renderSkeleton() : renderArticles()}
); }; export default BlogCard;