'use client'; import { Search, Settings } from 'lucide-react'; import { useEffect, useState } from 'react'; import Link from 'next/link'; import { toast } from 'sonner'; interface Discover { title: string; content: string; url: string; thumbnail: string; } // List of available categories const categories = [ 'For You', 'AI', 'Technology', 'Current News', 'Sports', 'Money', 'Gaming', 'Weather', 'Entertainment', 'Science', 'Health', 'Travel' ]; const Page = () => { const [discover, setDiscover] = useState(null); const [loading, setLoading] = useState(true); const [activeCategory, setActiveCategory] = useState('For You'); const [showPreferences, setShowPreferences] = useState(false); const [userPreferences, setUserPreferences] = useState(['AI', 'Technology']); // Load user preferences on component mount useEffect(() => { const loadUserPreferences = async () => { try { const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/discover/preferences`, { method: 'GET', headers: { 'Content-Type': 'application/json', }, }); if (res.ok) { const data = await res.json(); setUserPreferences(data.categories || ['AI', 'Technology']); } } catch (err: any) { console.error('Error loading preferences:', err.message); // Use default preferences if loading fails } }; loadUserPreferences(); }, []); // Save user preferences const saveUserPreferences = async (preferences: string[]) => { try { const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/discover/preferences`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ categories: preferences }), }); if (res.ok) { toast.success('Preferences saved successfully'); } else { const data = await res.json(); throw new Error(data.message); } } catch (err: any) { console.error('Error saving preferences:', err.message); toast.error('Error saving preferences'); } }; // Fetch data based on active category or user preferences useEffect(() => { const fetchData = async () => { setLoading(true); try { let endpoint = `${process.env.NEXT_PUBLIC_API_URL}/discover`; if (activeCategory !== 'For You') { endpoint += `?category=${encodeURIComponent(activeCategory)}`; } else if (userPreferences.length > 0) { endpoint += `?preferences=${encodeURIComponent(JSON.stringify(userPreferences))}`; } const res = await fetch(endpoint, { 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); } }; fetchData(); }, [activeCategory, userPreferences]); return loading ? (
) : ( <>

Discover

{/* Category Navigation */}
{categories.map((category) => ( ))}

{/* Personalization Modal */} {showPreferences && (

Personalize Your Feed

Select categories you're interested in:

{categories.filter(c => c !== 'For You').map((category) => ( ))}
)}
{discover && discover?.map((item, i) => ( {/* Using img tag instead of Next.js Image for external URLs */} {item.title}
{item.title.slice(0, 100)}...

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

))}
); }; export default Page;