news widget design 4
This commit is contained in:
parent
fbb2059f5c
commit
0496f90d32
@ -6,7 +6,6 @@ const API_HOST = process.env.API_HOST || 'http://172.16.0.104:8000';
|
||||
// Helper function to clean HTML content
|
||||
function cleanHtmlContent(content: string): string {
|
||||
if (!content) return '';
|
||||
// Remove HTML tags
|
||||
return content
|
||||
.replace(/<[^>]*>/g, '')
|
||||
.replace(/ /g, ' ')
|
||||
@ -17,28 +16,43 @@ function cleanHtmlContent(content: string): string {
|
||||
}
|
||||
|
||||
// Helper function to format time
|
||||
function formatDateTime(dateStr: string): string {
|
||||
const date = new Date(dateStr);
|
||||
return date.toLocaleString('fr-FR', {
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
hour12: false
|
||||
}).replace(',', ' à'); // Format: "17 avr. à 15:30"
|
||||
function formatDateTime(dateStr: string): { date: string, time: string } {
|
||||
try {
|
||||
const date = new Date(dateStr);
|
||||
return {
|
||||
date: date.toLocaleDateString('fr-FR', {
|
||||
day: 'numeric',
|
||||
month: 'short'
|
||||
}),
|
||||
time: date.toLocaleTimeString('fr-FR', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
hour12: false
|
||||
})
|
||||
};
|
||||
} catch (error) {
|
||||
return { date: 'N/A', time: 'N/A' };
|
||||
}
|
||||
}
|
||||
|
||||
// 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() + '...';
|
||||
const truncated = text.substring(0, maxLength).trim();
|
||||
return truncated.replace(/[.,!?]$/, '') + '...';
|
||||
}
|
||||
|
||||
// Helper function to format source
|
||||
function formatSource(source: string): string {
|
||||
if (!source) return '';
|
||||
return source.replace(/^(https?:\/\/)?(www\.)?/i, '').split('.')[0].toLowerCase();
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
console.log(`Fetching news from FastAPI server at ${API_HOST}...`);
|
||||
|
||||
const response = await fetch(`${API_HOST}/news?limit=10`, { // Increased limit to 10 articles
|
||||
const response = await fetch(`${API_HOST}/news?limit=10`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@ -55,15 +69,19 @@ export async function GET() {
|
||||
const rawNews = await response.json();
|
||||
|
||||
// Format and clean the news data
|
||||
const formattedNews = rawNews.map((article: any) => ({
|
||||
...article,
|
||||
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 || '#',
|
||||
}));
|
||||
const formattedNews = rawNews.map((article: any) => {
|
||||
const { date, time } = formatDateTime(article.date);
|
||||
return {
|
||||
id: article.id,
|
||||
title: truncateText(cleanHtmlContent(article.title), 100),
|
||||
description: truncateText(cleanHtmlContent(article.description), 150),
|
||||
date,
|
||||
time,
|
||||
source: formatSource(article.source),
|
||||
category: (article.category || 'GENERAL').toUpperCase(),
|
||||
url: article.url || '#',
|
||||
};
|
||||
});
|
||||
|
||||
console.log(`Successfully fetched and formatted ${formattedNews.length} news articles`);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user