'use client'; import { Search } from 'lucide-react'; import { useEffect, useState } from 'react'; import Link from 'next/link'; import { toast } from 'sonner'; import { cn } from '@/lib/utils'; interface Discover { title: string; content: string; url: string; thumbnail: string; } const topics: { key: string; display: string }[] = [ { display: 'Tech & Science', key: 'tech', }, { display: 'Finance', key: 'finance', }, { display: 'Art & Culture', key: 'art', }, { display: 'Sports', key: 'sports', }, { display: 'Entertainment', key: 'entertainment', }, ]; const Page = () => { const [discover, setDiscover] = useState(null); const [loading, setLoading] = useState(true); const [activeTopic, setActiveTopic] = useState(topics[0].key); const fetchArticles = async (topic: string) => { setLoading(true); try { const res = await fetch(`/api/discover?topic=${topic}`, { method: 'GET', headers: { 'Content-Type': 'application/json', }, }); const data = await res.json(); if (!res.ok) { throw new Error(data.message); } data.blogs = data.blogs.filter((blog: Discover) => blog.thumbnail); setDiscover(data.blogs); } catch (err: any) { console.error('Error fetching data:', err.message); toast.error('Error fetching data'); } finally { setLoading(false); } }; useEffect(() => { fetchArticles(activeTopic); }, [activeTopic]); return ( <>

Discover


{topics.map((t, i) => (
setActiveTopic(t.key)} > {t.display}
))}
{loading ? (
) : (
{discover && discover?.map((item, i) => ( {item.title}
{item.title.slice(0, 100)}...

{item.content.slice(0, 100)}...

))}
)}
); }; export default Page;