import { Fragment, useEffect, useState } from 'react'; import { ga, skeleton } from '../../helpers/utils'; import LazyImage from '../lazy-image'; import PropTypes from 'prop-types'; import { AiOutlineContainer } from 'react-icons/ai'; import { getDevPost, getMediumPost } from '@arifszn/blog-js'; import { formatDistance } from 'date-fns'; const displaySection = (blog) => { if (blog?.source && blog?.username) { return true; } else { return false; } }; const Blog = ({ loading, blog, googleAnalytics }) => { const [articles, setArticles] = useState(null); useEffect(() => { if (displaySection(blog)) { 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); }); } } }, []); const renderSkeleton = () => { let array = []; for (let index = 0; index < blog.limit; index++) { array.push(
{skeleton({ width: 'w-full', height: 'h-full', shape: '', })}

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

{skeleton({ width: 'w-24', height: 'h-3', className: 'mx-auto md:mx-0', })}
{skeleton({ width: 'w-full', height: 'h-4', className: 'mx-auto md:mx-0', })}
{skeleton({ width: 'w-32', height: '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 (googleAnalytics?.id) { ga.event({ action: 'Click Blog Post', params: { 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 ( {displaySection(blog) && (
{loading ? ( skeleton({ width: 'w-28', height: 'h-8' }) ) : ( Recent Posts )}
{loading || !articles ? renderSkeleton() : renderArticles()}
)}
); }; Blog.propTypes = { loading: PropTypes.bool.isRequired, blog: PropTypes.object.isRequired, googleAnalytics: PropTypes.object.isRequired, }; export default Blog;