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';
// FastAPI server configuration - use localhost since both servers are on the same machine
const API_HOST = process.env.API_HOST || 'http://localhost:8000';
// FastAPI server configuration
const API_HOST = process.env.API_HOST || 'http://172.16.0.104:8000';
// Helper function to clean HTML content
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();
return {
displayDate: `${day} ${month}`,
displayDate: `${day} ${month}.`, // Added dot for better styling
timestamp: date.toLocaleString('fr-FR', {
day: '2-digit',
month: 'short',
hour: '2-digit',
minute: '2-digit',
hour12: false
})
}).replace(',', ' à') // Format: "14 avr. à 15:30"
};
} catch (error) {
return { displayDate: 'N/A', timestamp: 'N/A' };
@ -40,18 +40,24 @@ function formatDateTime(dateStr: string): { displayDate: string, timestamp: stri
// Helper function to truncate text
function truncateText(text: string, maxLength: number): string {
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(/[.,!?]$/, '') + '...';
}
// Helper function to format category
function formatCategory(category: string): string {
if (!category) return 'GENERAL';
// Simplify long category names
return category
.split('-')
.map(part => part.trim().toUpperCase())
.join(' - ');
// Make category names shorter and more readable
const categoryMap: { [key: string]: string } = {
'GLOBAL ISSUES - WORLD AFFAIRS': 'WORLD',
'UN NEWS - GLOBAL NEWS': 'UN NEWS',
'GLOBAL NEWS': 'WORLD',
};
const normalizedCategory = category.toUpperCase();
return categoryMap[normalizedCategory] || normalizedCategory;
}
// Helper function to format source
@ -69,11 +75,8 @@ function formatSource(source: string): string {
export async function GET() {
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`, {
method: 'GET',
headers: {
@ -81,12 +84,8 @@ export async function GET() {
'Accept': 'application/json',
},
cache: 'no-store',
signal: controller.signal,
keepalive: true,
});
clearTimeout(timeoutId);
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP error! status: ${response.status}, body: ${errorText}`);