import * as React from 'react'; import Box from '@mui/material/Box'; import Grid from '@mui/material/Grid'; import Typography from '@mui/material/Typography'; import NavigateNextRoundedIcon from '@mui/icons-material/NavigateNextRounded'; import CircularProgress from '@mui/material/CircularProgress'; import { LatestProps } from "../types/props"; import { StyledTypography, TitleTypography } from "../types/styles"; import { ArticleMeta } from "./ArticleMeta"; import Fade from '@mui/material/Fade'; export default function Latest({ articles, onSelectArticle, onLoadMore }: LatestProps) { const [visibleCount, setVisibleCount] = React.useState(2); const [loadingMore, setLoadingMore] = React.useState(false); const [animating, setAnimating] = React.useState(false); const loaderRef = React.useRef(null); const displayedArticles = articles.slice(0, visibleCount); React.useEffect(() => { if (!loaderRef.current) return; const observer = new IntersectionObserver( async (entries) => { const first = entries[0]; if (first.isIntersecting && !loadingMore && visibleCount < articles.length) { console.log('๐ŸŸก Intersection triggered โ€” loading more blogs...'); setLoadingMore(true); // simulate API load delay await new Promise((resolve) => setTimeout(resolve, 1000)); if (onLoadMore) { console.log(`๐Ÿ“ก Calling onLoadMore(offset=${visibleCount}, limit=2)`); await onLoadMore(visibleCount, 2); } setAnimating(true); setVisibleCount((prev) => { const newCount = prev + 2; console.log(`โœ… Increasing visibleCount from ${prev} โ†’ ${newCount}`); return newCount; }); setTimeout(() => setAnimating(false), 600); setLoadingMore(false); } }, { threshold: 0.5 } ); const current = loaderRef.current; observer.observe(current); console.log('๐Ÿ‘€ IntersectionObserver attached to loaderRef:', loaderRef.current); return () => { if (current) observer.unobserve(current); console.log('๐Ÿงน IntersectionObserver detached'); }; }, [loadingMore, visibleCount, articles.length, onLoadMore]); return ( Latest {displayedArticles.map((article, index) => ( {article.tag} onSelectArticle?.(index)} > {article.title} {article.description} ))} {loadingMore ? ( ) : ( Scroll to load more... )} ); }