'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 } 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 }), }) 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) } if (loading) { return (

{t('notebook.generating')}

) } if (!summary) { return null } return (
{t('notebook.summary')}
{t('notebook.summaryDescription', { notebook: summary.notebookName, count: summary.stats.totalNotes, })}
{/* Stats */}
{summary.stats.totalNotes} {summary.stats.totalNotes === 1 ? 'note' : 'notes'}
{summary.stats.labelsUsed.length > 0 && (
Labels: {summary.stats.labelsUsed.join(', ')}
)}
{new Date(summary.generatedAt).toLocaleString()}
{/* Summary Content */}
{summary.summary}
) }