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
|
// Helper function to clean HTML content
|
||||||
function cleanHtmlContent(content: string): string {
|
function cleanHtmlContent(content: string): string {
|
||||||
if (!content) return '';
|
if (!content) return '';
|
||||||
// Remove HTML tags
|
|
||||||
return content
|
return content
|
||||||
.replace(/<[^>]*>/g, '')
|
.replace(/<[^>]*>/g, '')
|
||||||
.replace(/ /g, ' ')
|
.replace(/ /g, ' ')
|
||||||
@ -17,28 +16,43 @@ function cleanHtmlContent(content: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to format time
|
// Helper function to format time
|
||||||
function formatDateTime(dateStr: string): string {
|
function formatDateTime(dateStr: string): { date: string, time: string } {
|
||||||
const date = new Date(dateStr);
|
try {
|
||||||
return date.toLocaleString('fr-FR', {
|
const date = new Date(dateStr);
|
||||||
month: 'short',
|
return {
|
||||||
day: 'numeric',
|
date: date.toLocaleDateString('fr-FR', {
|
||||||
hour: '2-digit',
|
day: 'numeric',
|
||||||
minute: '2-digit',
|
month: 'short'
|
||||||
hour12: false
|
}),
|
||||||
}).replace(',', ' à'); // Format: "17 avr. à 15:30"
|
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
|
// 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;
|
||||||
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() {
|
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?limit=10`, { // Increased limit to 10 articles
|
const response = await fetch(`${API_HOST}/news?limit=10`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
@ -55,15 +69,19 @@ export async function GET() {
|
|||||||
const rawNews = await response.json();
|
const rawNews = await response.json();
|
||||||
|
|
||||||
// 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,
|
const { date, time } = formatDateTime(article.date);
|
||||||
title: truncateText(cleanHtmlContent(article.title), 100),
|
return {
|
||||||
description: truncateText(cleanHtmlContent(article.description), 150),
|
id: article.id,
|
||||||
formattedDate: formatDateTime(article.date),
|
title: truncateText(cleanHtmlContent(article.title), 100),
|
||||||
source: article.source?.toLowerCase() || 'unknown',
|
description: truncateText(cleanHtmlContent(article.description), 150),
|
||||||
category: article.category?.toUpperCase() || 'GENERAL',
|
date,
|
||||||
url: article.url || '#',
|
time,
|
||||||
}));
|
source: formatSource(article.source),
|
||||||
|
category: (article.category || 'GENERAL').toUpperCase(),
|
||||||
|
url: article.url || '#',
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
console.log(`Successfully fetched and formatted ${formattedNews.length} news articles`);
|
console.log(`Successfully fetched and formatted ${formattedNews.length} news articles`);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user