import { ArrowRight, Info, Search } from 'lucide-react'; import { useEffect, useMemo, useState } from 'react'; import { Link } from 'react-router-dom'; import { ApiError, getArticles } from '../api/client'; import { ArticleCard, getReadTime } from '../components/articles/ArticleCard'; import { AuthorAvatar } from '../components/articles/AuthorAvatar'; import { CategoryBadge } from '../components/articles/CategoryBadge'; import { sampleArticles } from '../data/sample'; import type { Article } from '../types'; type FeedSource = 'api' | 'sample'; /** * Homepage: featured (hero) article, search bar, article grid built from * ArticleCard components, and a pagination placeholder. * * Articles are loaded from `GET /api/v1/articles`; until that backend * endpoint exists the page falls back to sample data (clearly labelled). */ export function Home() { const [articles, setArticles] = useState([]); const [source, setSource] = useState('sample'); const [loading, setLoading] = useState(true); const [query, setQuery] = useState(''); useEffect(() => { let active = true; getArticles() .then((data) => { if (!active) return; if (data.length > 0) { setArticles(data); setSource('api'); } else { setArticles(sampleArticles); setSource('sample'); } }) .catch((error: unknown) => { if (!active) return; // 404 (endpoint not implemented yet) or network failure: fall back to // sample data so the UI stays useful without a running backend. if (!(error instanceof ApiError)) { console.error('Unexpected error while loading articles:', error); } setArticles(sampleArticles); setSource('sample'); }) .finally(() => { if (active) setLoading(false); }); return () => { active = false; }; }, []); const filtered = useMemo(() => { const q = query.trim().toLowerCase(); if (!q) return articles; return articles.filter( (article) => article.title.toLowerCase().includes(q) || article.content.toLowerCase().includes(q), ); }, [articles, query]); const featured = filtered[0]; const publishedAt = (article: Article): string => { const date = article.createdAt instanceof Date ? article.createdAt : new Date(article.createdAt); return Number.isNaN(date.getTime()) ? '' : date.toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric' }); }; return (
{/* Search / filter bar (client-side filter for now) */}
event.preventDefault()} >
{source === 'sample' && !loading && (

)} {/* Hero: featured article */} {featured && (
Featured {featured.category && }

{featured.title}

{featured.content}

{featured.author?.name ?? 'Unknown author'} {publishedAt(featured)} {getReadTime(featured.content)} min read
Read article
)} {/* Article grid */}

Latest articles

{loading ? (
{[0, 1, 2].map((index) => (
))}
) : filtered.length > 0 ? (
{filtered.map((article) => ( ))}
) : (

No articles match your search.

)}
{/* Pagination placeholder */}
); } export default Home;