import React, { useEffect, useState } from 'react'; import { collection, query, orderBy, onSnapshot, Timestamp, where, deleteDoc, doc, getDoc } from 'firebase/firestore'; import { Link } from 'react-router-dom'; import { db, handleFirestoreError, OperationType } from '../firebase'; import { useAuth } from '../components/AuthContext'; import { SynthesisDocument } from '../types'; import { format } from 'date-fns'; import { fr } from 'date-fns/locale'; import { FileText, Plus, Trash2, AlertTriangle } from 'lucide-react'; export default function Home() { const { user } = useAuth(); const [syntheses, setSyntheses] = useState([]); const [loading, setLoading] = useState(true); const [deletingId, setDeletingId] = useState(null); const [maxItems, setMaxItems] = useState(4); useEffect(() => { if (!user) return; // Fetch settings to get maxItemsPerCategory const fetchSettings = async () => { try { const settingsDoc = await getDoc(doc(db, 'settings', user.uid)); if (settingsDoc.exists()) { const data = settingsDoc.data(); if (data.maxItemsPerCategory) { setMaxItems(data.maxItemsPerCategory); } } } catch (err) { console.warn("Could not fetch settings", err); } }; fetchSettings(); const q = query( collection(db, 'syntheses'), where('authorUid', '==', user.uid) ); const unsubscribe = onSnapshot(q, (snapshot) => { const docs = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })) as SynthesisDocument[]; // Sort client-side to avoid requiring a composite index docs.sort((a, b) => b.createdAt.toMillis() - a.createdAt.toMillis()); setSyntheses(docs); setLoading(false); }, (error) => { handleFirestoreError(error, OperationType.LIST, 'syntheses'); }); return () => unsubscribe(); }, [user]); const handleDelete = async (e: React.MouseEvent, id: string) => { e.preventDefault(); e.stopPropagation(); if (deletingId === id) { // Confirm deletion try { await deleteDoc(doc(db, 'syntheses', id)); setDeletingId(null); } catch (err) { handleFirestoreError(err, OperationType.DELETE, `syntheses/${id}`); setDeletingId(null); } } else { // Show confirmation state setDeletingId(id); // Auto-cancel after 3 seconds setTimeout(() => { setDeletingId((current) => current === id ? null : current); }, 3000); } }; if (loading) { return (
); } return (

Synthèses d'Actualités par IA

Retrouvez ici toutes vos synthèses hebdomadaires générées automatiquement.

{syntheses.length === 0 ? (

Aucune synthèse

Commencez par générer votre première synthèse hebdomadaire.

) : (
{syntheses.map((synth) => (
Semaine {synth.week.split('-W')[1]} {format(synth.createdAt.toDate(), 'dd MMM yyyy', { locale: fr })}

Synthèse de la semaine

{(() => { const items = synth.sections?.[0]?.items || synth.majorAnnouncements || []; if (items.length === 0) { return

Aucune annonce majeure cette semaine.

; } return items.slice(0, maxItems).map((item, idx) => (

• {item.title}

)); })()}
Lire la synthèse →
))}
)}
); }