From fbb2059f5cfe4dc65d702d2b2e597167b5214771 Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 14 Apr 2025 19:27:52 +0200 Subject: [PATCH] news widget design 2 --- app/api/news/route.ts | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/app/api/news/route.ts b/app/api/news/route.ts index f6a9b00..31c9caa 100644 --- a/app/api/news/route.ts +++ b/app/api/news/route.ts @@ -16,36 +16,35 @@ function cleanHtmlContent(content: string): string { .trim(); } -// Helper function to format relative time -function formatRelativeTime(dateStr: string): string { +// Helper function to format time +function formatDateTime(dateStr: string): string { const date = new Date(dateStr); - const now = new Date(); - const diffInHours = Math.floor((now.getTime() - date.getTime()) / (1000 * 60 * 60)); - - if (diffInHours < 1) return 'Just now'; - if (diffInHours === 1) return '1 hour ago'; - if (diffInHours < 24) return `${diffInHours} hours ago`; - if (diffInHours < 48) return 'Yesterday'; - return date.toLocaleDateString('en-US', { - month: 'short', + return date.toLocaleString('fr-FR', { + month: 'short', day: 'numeric', - year: 'numeric' - }); + hour: '2-digit', + minute: '2-digit', + hour12: false + }).replace(',', ' à'); // Format: "17 avr. à 15:30" +} + +// Helper function to truncate text +function truncateText(text: string, maxLength: number): string { + if (!text || text.length <= maxLength) return text; + return text.substring(0, maxLength).trim() + '...'; } export async function GET() { try { console.log(`Fetching news from FastAPI server at ${API_HOST}...`); - const response = await fetch(`${API_HOST}/news`, { + const response = await fetch(`${API_HOST}/news?limit=10`, { // Increased limit to 10 articles method: 'GET', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', }, - // Add timeout and other fetch options cache: 'no-store', - next: { revalidate: 0 }, }); if (!response.ok) { @@ -58,17 +57,18 @@ export async function GET() { // Format and clean the news data const formattedNews = rawNews.map((article: any) => ({ ...article, - title: cleanHtmlContent(article.title), - description: cleanHtmlContent(article.description), - relativeTime: formatRelativeTime(article.date), + title: truncateText(cleanHtmlContent(article.title), 100), + description: truncateText(cleanHtmlContent(article.description), 150), + formattedDate: formatDateTime(article.date), + source: article.source?.toLowerCase() || 'unknown', category: article.category?.toUpperCase() || 'GENERAL', + url: article.url || '#', })); console.log(`Successfully fetched and formatted ${formattedNews.length} news articles`); return NextResponse.json(formattedNews); } catch (error) { - // Enhanced error logging console.error('Error in news API:', { error: error instanceof Error ? error.message : 'Unknown error', apiHost: API_HOST, @@ -76,7 +76,6 @@ export async function GET() { timestamp: new Date().toISOString(), }); - // More detailed error response return NextResponse.json( { error: 'Failed to fetch news',