news widget design 6

This commit is contained in:
alma 2025-04-14 20:58:44 +02:00
parent 81ead6d121
commit f5ff070d1f

View File

@ -1,7 +1,7 @@
import { NextResponse } from 'next/server'; import { NextResponse } from 'next/server';
// FastAPI server configuration - use localhost since both servers are on the same machine // FastAPI server configuration
const API_HOST = process.env.API_HOST || 'http://localhost:8000'; const API_HOST = process.env.API_HOST || 'http://172.16.0.104:8000';
// Helper function to clean HTML content // Helper function to clean HTML content
function cleanHtmlContent(content: string): string { function cleanHtmlContent(content: string): string {
@ -23,14 +23,14 @@ function formatDateTime(dateStr: string): { displayDate: string, timestamp: stri
const month = date.toLocaleString('fr-FR', { month: 'short' }).toLowerCase(); const month = date.toLocaleString('fr-FR', { month: 'short' }).toLowerCase();
return { return {
displayDate: `${day} ${month}`, displayDate: `${day} ${month}.`, // Added dot for better styling
timestamp: date.toLocaleString('fr-FR', { timestamp: date.toLocaleString('fr-FR', {
day: '2-digit', day: '2-digit',
month: 'short', month: 'short',
hour: '2-digit', hour: '2-digit',
minute: '2-digit', minute: '2-digit',
hour12: false hour12: false
}) }).replace(',', ' à') // Format: "14 avr. à 15:30"
}; };
} catch (error) { } catch (error) {
return { displayDate: 'N/A', timestamp: 'N/A' }; return { displayDate: 'N/A', timestamp: 'N/A' };
@ -40,18 +40,24 @@ function formatDateTime(dateStr: string): { displayDate: string, timestamp: stri
// Helper function to truncate text // Helper function to truncate text
function truncateText(text: string, maxLength: number): string { function truncateText(text: string, maxLength: number): string {
if (!text || text.length <= maxLength) return text; if (!text || text.length <= maxLength) return text;
const truncated = text.substring(0, maxLength).trim(); // Find the last space before maxLength to avoid cutting words
const lastSpace = text.lastIndexOf(' ', maxLength);
const truncated = text.substring(0, lastSpace > 0 ? lastSpace : maxLength).trim();
return truncated.replace(/[.,!?]$/, '') + '...'; return truncated.replace(/[.,!?]$/, '') + '...';
} }
// Helper function to format category // Helper function to format category
function formatCategory(category: string): string { function formatCategory(category: string): string {
if (!category) return 'GENERAL'; if (!category) return 'GENERAL';
// Simplify long category names // Make category names shorter and more readable
return category const categoryMap: { [key: string]: string } = {
.split('-') 'GLOBAL ISSUES - WORLD AFFAIRS': 'WORLD',
.map(part => part.trim().toUpperCase()) 'UN NEWS - GLOBAL NEWS': 'UN NEWS',
.join(' - '); 'GLOBAL NEWS': 'WORLD',
};
const normalizedCategory = category.toUpperCase();
return categoryMap[normalizedCategory] || normalizedCategory;
} }
// Helper function to format source // Helper function to format source
@ -69,10 +75,7 @@ function formatSource(source: string): string {
export async function GET() { export async function GET() {
try { try {
console.log(`Attempting to connect to FastAPI server at ${API_HOST}...`); console.log(`Fetching news from FastAPI server at ${API_HOST}...`);
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
const response = await fetch(`${API_HOST}/news?limit=10`, { const response = await fetch(`${API_HOST}/news?limit=10`, {
method: 'GET', method: 'GET',
@ -81,12 +84,8 @@ export async function GET() {
'Accept': 'application/json', 'Accept': 'application/json',
}, },
cache: 'no-store', cache: 'no-store',
signal: controller.signal,
keepalive: true,
}); });
clearTimeout(timeoutId);
if (!response.ok) { if (!response.ok) {
const errorText = await response.text(); const errorText = await response.text();
throw new Error(`HTTP error! status: ${response.status}, body: ${errorText}`); throw new Error(`HTTP error! status: ${response.status}, body: ${errorText}`);