"use client"; import { useEffect, useState } from "react"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { RefreshCw, Globe } from "lucide-react"; import { Button } from "@/components/ui/button"; import { ObservatoryMap } from "./observatory-map"; // News item interface matching the API response interface NewsItem { id: number; title: string; displayDate: string; timestamp: string; source: string; description: string | null; category: string | null; url: string; } export function ObservatoryView() { const [news, setNews] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [selectedCountry, setSelectedCountry] = useState(null); // Fetch news data const fetchNews = async () => { setLoading(true); try { const response = await fetch('/api/news?limit=50'); if (!response.ok) { throw new Error('Failed to fetch news'); } const data = await response.json(); setNews(data); setError(null); } catch (err) { setError('Failed to fetch news'); console.error('Error fetching news:', err); } finally { setLoading(false); } }; // Fetch news on component mount useEffect(() => { fetchNews(); }, []); // Extract countries from news data (simplified version) const extractCountries = (newsItems: NewsItem[]) => { // This is a simplified implementation // In a real app, we would use NLP or a more sophisticated technique const countries = [ 'France', 'USA', 'Canada', 'UK', 'Germany', 'Japan', 'China', 'India', 'Brazil', 'Australia', 'Russia', 'Italy', 'Spain' ]; const result: Record = {}; newsItems.forEach(item => { countries.forEach(country => { if ( (item.title && item.title.includes(country)) || (item.description && item.description.includes(country)) ) { if (!result[country]) { result[country] = []; } result[country].push(item); } }); }); return result; }; // Handle country selection on the map const handleCountrySelect = (country: string) => { setSelectedCountry(selectedCountry === country ? null : country); }; // Get news filtered by selected country const getFilteredNews = () => { if (!selectedCountry) return news; const countriesMap = extractCountries(news); return countriesMap[selectedCountry] || []; }; // Loading state if (loading) { return (
); } // Error state if (error) { return (

{error}

); } const filteredNews = getFilteredNews(); const countriesMap = extractCountries(news); const countries = Object.keys(countriesMap); return (
News Observatory
{/* News Feed Section */}
{selectedCountry ? `News about ${selectedCountry}` : 'Latest News'} ({filteredNews.length} articles)
{filteredNews.length === 0 ? (

No news available

) : ( filteredNews.map(item => (
window.open(item.url, '_blank')} >
{item.displayDate} {item.source}

{item.title}

{item.description}

)) )}
{/* Map Section */}
World Map {/* Map Placeholder */}
({ name, count: items.length }))} onCountrySelect={handleCountrySelect} selectedCountry={selectedCountry} />
{/* Countries List */}

Mentioned Countries:

{countries.length === 0 ? (

No countries detected

) : (
{countries.map(country => ( ))}
)}
); }