diff --git a/app/api/news/route.ts b/app/api/news/route.ts index 57af065..bb8e73c 100644 --- a/app/api/news/route.ts +++ b/app/api/news/route.ts @@ -88,20 +88,20 @@ export async function GET() { console.log('Connected to PostgreSQL database'); const result = await client.query( - 'SELECT * FROM news ORDER BY date DESC LIMIT 5' + 'SELECT * FROM news ORDER BY date DESC LIMIT 10' ); const formattedNews = result.rows.map(article => { const { displayDate, timestamp } = formatDateTime(article.date); return { id: article.id, - title: truncateText(article.title, 100), - description: truncateText(article.description, 150), + title: truncateText(article.title, 70), + description: truncateText(article.description, 100), displayDate, timestamp, source: formatSource(article.source), category: formatCategory(article.category), - url: article.url || '#', + url: article.url || '#' }; }); @@ -109,17 +109,14 @@ export async function GET() { return NextResponse.json(formattedNews); } catch (error) { - console.error('Error in news API:', { + console.error('Database error:', { error: error instanceof Error ? error.message : 'Unknown error', - stack: error instanceof Error ? error.stack : undefined, - timestamp: new Date().toISOString(), }); return NextResponse.json( { error: 'Failed to fetch news', - details: error instanceof Error ? error.message : 'Unknown error', - timestamp: new Date().toISOString(), + details: error instanceof Error ? error.message : 'Unknown error' }, { status: 500 } ); diff --git a/components/news.tsx b/components/news.tsx index 356c5d2..cbb7d8e 100644 --- a/components/news.tsx +++ b/components/news.tsx @@ -5,12 +5,13 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { RefreshCw } from "lucide-react"; import { useSession } from "next-auth/react"; +import { formatDistance } from 'date-fns'; +import { fr } from 'date-fns/locale'; interface NewsItem { id: number; title: string; - displayDate: string; - timestamp: string; + date: string; source: string; description: string | null; category: string | null; @@ -52,11 +53,24 @@ export function News() { } }, [status]); + const formatDate = (dateString: string) => { + try { + const date = new Date(dateString); + return formatDistance(date, new Date(), { + addSuffix: true, + locale: fr + }); + } catch (err) { + console.error('Error formatting date:', err); + return dateString; + } + }; + if (status === 'loading' || loading) { return ( - - - News + + + News
@@ -68,9 +82,9 @@ export function News() { } return ( - - - News + + + News - + {error ? ( -

{error}

+

{error}

) : ( -
+
{news.length === 0 ? ( -

No news available

+

No news available

) : ( news.map((item) => (
window.open(item.url, '_blank')} > -

- {item.title} -

- {item.description && ( -

- {item.description} -

- )} -
- {item.displayDate} +
+ {formatDate(item.date)} {item.category && ( - + {item.category} )}
+

+ {item.title} +

+ {item.description && ( +

+ {item.description} +

+ )}
)) )}