"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); const [isBrowser, setIsBrowser] = useState(false); // Check if we're in the browser useEffect(() => { setIsBrowser(true); }, []); // Fetch news data const fetchNews = async () => { setLoading(true); try { const response = await fetch('/api/news?limit=100'); 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', 'Sudan', 'New York', 'United Nations', 'Ukraine', 'Egypt', 'Mexico', 'South Africa', 'Nigeria', 'Argentina', 'Pakistan', 'Indonesia', 'Saudi Arabia', 'Iran', 'Turkey', 'South Korea', 'Thailand', 'Vietnam', 'Philippines', 'Malaysia', 'Singapore', 'Israel', 'Palestine', 'Syria', 'Iraq', 'Afghanistan', 'Morocco', 'Algeria', 'Tunisia', 'Kenya', 'Ethiopia', 'Greece', 'Poland', 'Sweden', 'Norway', 'Denmark', 'Finland', 'Netherlands', 'Belgium', 'Portugal', 'Switzerland', 'Austria' ]; // Sort countries by length (to prioritize longer names) const sortedCountries = [...countries].sort((a, b) => b.length - a.length); const result: Record = {}; newsItems.forEach(item => { // For title and description const titleAndDesc = [ item.title || '', item.description || '' ].join(' ').toLowerCase(); // Check each country sortedCountries.forEach(country => { if (titleAndDesc.includes(country.toLowerCase())) { if (!result[country]) { result[country] = []; } // Only add once per item if (!result[country].some(existingItem => existingItem.id === item.id)) { 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); return (
{/* Main Content */}
{/* News Feed Section */}

Latest News ({filteredNews.length} articles)

{filteredNews.length === 0 ? (

No news available

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

{item.title}

{item.description}

)) )}
{/* Map Section */}

World Map

{!loading && isBrowser && ( ({ name, count: items.length }))} onCountrySelect={handleCountrySelect} selectedCountry={selectedCountry} /> )}
); }