news widget design 2
This commit is contained in:
parent
67896ef072
commit
fbb2059f5c
@ -16,36 +16,35 @@ function cleanHtmlContent(content: string): string {
|
|||||||
.trim();
|
.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to format relative time
|
// Helper function to format time
|
||||||
function formatRelativeTime(dateStr: string): string {
|
function formatDateTime(dateStr: string): string {
|
||||||
const date = new Date(dateStr);
|
const date = new Date(dateStr);
|
||||||
const now = new Date();
|
return date.toLocaleString('fr-FR', {
|
||||||
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',
|
month: 'short',
|
||||||
day: 'numeric',
|
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() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
console.log(`Fetching news from FastAPI server at ${API_HOST}...`);
|
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',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
},
|
},
|
||||||
// Add timeout and other fetch options
|
|
||||||
cache: 'no-store',
|
cache: 'no-store',
|
||||||
next: { revalidate: 0 },
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@ -58,17 +57,18 @@ export async function GET() {
|
|||||||
// Format and clean the news data
|
// Format and clean the news data
|
||||||
const formattedNews = rawNews.map((article: any) => ({
|
const formattedNews = rawNews.map((article: any) => ({
|
||||||
...article,
|
...article,
|
||||||
title: cleanHtmlContent(article.title),
|
title: truncateText(cleanHtmlContent(article.title), 100),
|
||||||
description: cleanHtmlContent(article.description),
|
description: truncateText(cleanHtmlContent(article.description), 150),
|
||||||
relativeTime: formatRelativeTime(article.date),
|
formattedDate: formatDateTime(article.date),
|
||||||
|
source: article.source?.toLowerCase() || 'unknown',
|
||||||
category: article.category?.toUpperCase() || 'GENERAL',
|
category: article.category?.toUpperCase() || 'GENERAL',
|
||||||
|
url: article.url || '#',
|
||||||
}));
|
}));
|
||||||
|
|
||||||
console.log(`Successfully fetched and formatted ${formattedNews.length} news articles`);
|
console.log(`Successfully fetched and formatted ${formattedNews.length} news articles`);
|
||||||
|
|
||||||
return NextResponse.json(formattedNews);
|
return NextResponse.json(formattedNews);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Enhanced error logging
|
|
||||||
console.error('Error in news API:', {
|
console.error('Error in news API:', {
|
||||||
error: error instanceof Error ? error.message : 'Unknown error',
|
error: error instanceof Error ? error.message : 'Unknown error',
|
||||||
apiHost: API_HOST,
|
apiHost: API_HOST,
|
||||||
@ -76,7 +76,6 @@ export async function GET() {
|
|||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// More detailed error response
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
error: 'Failed to fetch news',
|
error: 'Failed to fetch news',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user