"use client"; import { useEffect, useState } from "react"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { formatDistanceToNow } from 'date-fns'; import { fr } from 'date-fns/locale'; import Link from 'next/link'; import { Button } from '@/components/ui/button'; import { RefreshCw } from 'lucide-react'; interface NewsItem { id: number; title: string; url: string; date: string; source: string; description: string; category: string; sentiment: { score: number | null; label: string | null; }; symbols: string[] | null; symbol: string | null; } // Mock data for testing - remove this in production const MOCK_NEWS = [ { id: 1, title: "New Project Management Features Released", url: "#", date: "2024-03-20", source: "Internal", description: "New features added to improve project management workflow", category: "Update", sentiment: { score: null, label: null }, symbols: null, symbol: null }, { id: 2, title: "Team Meeting Schedule Changes", url: "#", date: "2024-03-19", source: "Internal", description: "Updates to the team meeting schedule", category: "Announcement", sentiment: { score: null, label: null }, symbols: null, symbol: null }, { id: 3, title: "Upcoming Training Sessions", url: "#", date: "2024-03-18", source: "Internal", description: "Schedule for upcoming training sessions", category: "Training", sentiment: { score: null, label: null }, symbols: null, symbol: null } ]; export function News() { console.log('News component mounted'); const [news, setNews] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [refreshing, setRefreshing] = useState(false); const [dbStatus, setDbStatus] = useState<'connecting' | 'connected' | 'error'>('connecting'); const fetchNews = async (isRefresh = false) => { console.log('Fetching news...'); if (isRefresh) setRefreshing(true); setLoading(true); setDbStatus('connecting'); try { console.log('Making API request to /api/news'); const response = await fetch('/api/news'); console.log('API response status:', response.status); if (!response.ok) { console.log('API request failed'); throw new Error('Failed to fetch news'); } const data = await response.json(); console.log('API response data:', data); if (data.error) { console.log('API returned error:', data.error); throw new Error(data.error); } // If no news items returned, use mock data for testing if (!data.news || data.news.length === 0) { console.log('No news items returned, using mock data'); setNews(MOCK_NEWS); } else { console.log('Setting news items from API'); setNews(data.news); } setError(null); setDbStatus('connected'); } catch (err) { console.error('Error details:', err); setError('Failed to load news. Please try again later.'); setDbStatus('error'); console.log('Using mock data due to error'); setNews(MOCK_NEWS); } finally { setLoading(false); setRefreshing(false); } }; useEffect(() => { fetchNews(); }, []); if (loading && !refreshing) { return ( News

{dbStatus === 'connecting' ? 'Connecting to database...' : 'Loading news...'}

); } if (error) { return ( News

{error}

{dbStatus === 'error' ? 'Database connection error' : 'Failed to fetch news'}

); } return ( News
{news.length === 0 ? (
No news available
) : ( news.map((item) => (
{item.title}

{item.source} • {formatDistanceToNow(new Date(item.date), { addSuffix: true, locale: fr })}

{item.description && (

{item.description.replace(/<[^>]*>/g, '')}

)}
{item.sentiment.score !== null && (
0 ? 'bg-green-100 text-green-800' : item.sentiment.score < 0 ? 'bg-red-100 text-red-800' : 'bg-gray-100 text-gray-800' }`}> {item.sentiment.label || 'Neutral'}
)}
{(item.symbols?.length || item.symbol) && (
{item.symbols?.map((symbol, index) => ( {symbol} ))} {item.symbol && !item.symbols?.includes(item.symbol) && ( {item.symbol} )}
)}
)) )}
); }