'use client' import { useEffect, useState } from 'react' import { useRouter } from 'next/navigation' import { X, FolderOpen } from 'lucide-react' import { useNotebooks } from '@/context/notebooks-context' import { cn } from '@/lib/utils' import { useLanguage } from '@/lib/i18n' interface NotebookSuggestionToastProps { noteId: string noteContent: string onDismiss: () => void onMoveToNotebook?: (notebookId: string) => void } export function NotebookSuggestionToast({ noteId, noteContent, onDismiss, onMoveToNotebook }: NotebookSuggestionToastProps) { const { t } = useLanguage() const [suggestion, setSuggestion] = useState(null) const [isLoading, setIsLoading] = useState(false) const [visible, setVisible] = useState(true) const [timeLeft, setTimeLeft] = useState(30) // 30 second countdown const router = useRouter() const { moveNoteToNotebookOptimistic } = useNotebooks() // Auto-dismiss after 30 seconds useEffect(() => { const timer = setInterval(() => { setTimeLeft(prev => { if (prev <= 1) { handleDismiss() return 0 } return prev - 1 }) }, 1000) return () => clearInterval(timer) }, []) // Fetch suggestion when component mounts useEffect(() => { const fetchSuggestion = async () => { // Only suggest if content is long enough (> 20 words) const wordCount = noteContent.trim().split(/\s+/).length if (wordCount < 20) { return } setIsLoading(true) try { const response = await fetch('/api/ai/suggest-notebook', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ noteContent }) }) const data = await response.json() if (response.ok) { if (data.suggestion && data.confidence > 0.7) { setSuggestion(data.suggestion) } } } catch (error) { // Error fetching notebook suggestion } finally { setIsLoading(false) } } fetchSuggestion() }, [noteContent]) const handleDismiss = () => { setVisible(false) setTimeout(() => onDismiss(), 300) // Wait for animation } const handleMoveToNotebook = async () => { if (!suggestion) return try { // Move note to suggested notebook await moveNoteToNotebookOptimistic(noteId, suggestion.id) router.refresh() handleDismiss() } catch (error) { console.error('Failed to move note to notebook:', error) } } // Don't render if no suggestion or loading or dismissed if (!visible || isLoading || !suggestion) { return null } return (
{/* Icon */}
{/* Content */}

{t('notebookSuggestion.title', { icon: suggestion.icon, name: suggestion.name })}

{t('notebookSuggestion.description')}

{/* Actions */}
{/* Move button */} {/* Dismiss button */}
) }