'use client' import { useState, useEffect } from 'react' import { Button } from './ui/button' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from './ui/dialog' import { Loader2, FileText, RefreshCw, Download } from 'lucide-react' import { toast } from 'sonner' import { useLanguage } from '@/lib/i18n' import type { NotebookSummary } from '@/lib/ai/services' import ReactMarkdown from 'react-markdown' interface NotebookSummaryDialogProps { open: boolean onOpenChange: (open: boolean) => void notebookId: string | null notebookName?: string } export function NotebookSummaryDialog({ open, onOpenChange, notebookId, notebookName, }: NotebookSummaryDialogProps) { const { t } = useLanguage() const [summary, setSummary] = useState(null) const [loading, setLoading] = useState(false) const [regenerating, setRegenerating] = useState(false) // Fetch summary when dialog opens with a notebook useEffect(() => { if (open && notebookId) { fetchSummary() } else { // Reset state when closing setSummary(null) } }, [open, notebookId]) const fetchSummary = async () => { if (!notebookId) return setLoading(true) try { const response = await fetch('/api/ai/notebook-summary', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ notebookId, language: document.documentElement.lang || 'en', }), }) const data = await response.json() if (data.success && data.data) { setSummary(data.data) } else { toast.error(data.error || t('notebook.summaryError')) onOpenChange(false) } } catch (error) { toast.error(t('notebook.summaryError')) onOpenChange(false) } finally { setLoading(false) } } const handleRegenerate = async () => { if (!notebookId) return setRegenerating(true) await fetchSummary() setRegenerating(false) } const handleExportPDF = () => { if (!summary) return const printWindow = window.open('', '_blank') if (!printWindow) return const date = new Date(summary.generatedAt).toLocaleString() const labels = summary.stats.labelsUsed.length > 0 ? `

${t('notebook.labels')} ${summary.stats.labelsUsed.join(', ')}

` : '' printWindow.document.write(` ${t('notebook.pdfTitle', { name: summary.notebookName })}

${t('notebook.pdfTitle', { name: summary.notebookName })}

${t('notebook.pdfNotesLabel')} ${summary.stats.totalNotes}

${labels}

${t('notebook.pdfGeneratedOn')} ${date}

${summary.summary .replace(/^### (.+)$/gm, '

$1

') .replace(/^## (.+)$/gm, '

$1

') .replace(/^# (.+)$/gm, '

$1

') .replace(/\*\*(.+?)\*\*/g, '$1') .replace(/\*(.+?)\*/g, '$1') .replace(/`(.+?)`/g, '$1') .replace(/^> (.+)$/gm, '
$1
') .replace(/^[-*] (.+)$/gm, '
  • $1
  • ') .replace(/(
  • .*<\/li>\n?)+/g, '
      $&
    ') .replace(/\n\n/g, '

    ') .replace(/^(?!<[hHuUoOblp])(.+)$/gm, '

    $1

    ')}
  • `) printWindow.document.close() printWindow.focus() setTimeout(() => { printWindow.print() }, 300) } if (loading) { return ( {t('notebook.generating')} {t('notebook.generatingDescription') || 'Please wait...'}

    {t('notebook.generating')}

    ) } if (!summary) { return null } return ( {/* En-tĂȘte fixe */}
    {t('notebook.summary')}

    {t('notebook.summaryDescription', { notebook: summary.notebookName, count: summary.stats.totalNotes, })}

    {/* Zone scrollable */}
    {/* Stats */}
    {t('ai.autoLabels.notesCount', { count: summary.stats.totalNotes })}
    {summary.stats.labelsUsed.length > 0 && (
    {t('notebook.labels')} {summary.stats.labelsUsed.join(', ')}
    )}
    {new Date(summary.generatedAt).toLocaleString()}
    {/* Contenu Markdown */}
    {summary.summary}
    ) }