'use client' import { useState, useEffect } from 'react' import { Button } from './ui/button' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from './ui/dialog' import { Checkbox } from './ui/checkbox' import { Tag, Loader2, Sparkles, CheckCircle2 } from 'lucide-react' import { toast } from 'sonner' import { useLanguage } from '@/lib/i18n' import type { AutoLabelSuggestion, SuggestedLabel } from '@/lib/ai/services' interface AutoLabelSuggestionDialogProps { open: boolean onOpenChange: (open: boolean) => void notebookId: string | null onLabelsCreated: () => void } export function AutoLabelSuggestionDialog({ open, onOpenChange, notebookId, onLabelsCreated, }: AutoLabelSuggestionDialogProps) { const { t } = useLanguage() const [suggestions, setSuggestions] = useState(null) const [loading, setLoading] = useState(false) const [creating, setCreating] = useState(false) const [selectedLabels, setSelectedLabels] = useState>(new Set()) // Fetch suggestions when dialog opens with a notebook useEffect(() => { if (open && notebookId) { fetchSuggestions() } else { // Reset state when closing setSuggestions(null) setSelectedLabels(new Set()) } }, [open, notebookId]) const fetchSuggestions = async () => { if (!notebookId) return setLoading(true) try { const response = await fetch('/api/ai/auto-labels', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ notebookId }), }) const data = await response.json() if (data.success && data.data) { setSuggestions(data.data) // Select all labels by default const allLabelNames = new Set(data.data.suggestedLabels.map((l: SuggestedLabel) => l.name as string)) setSelectedLabels(allLabelNames) } else { // No suggestions is not an error - just close the dialog if (data.message) { } onOpenChange(false) } } catch (error) { console.error('Failed to fetch label suggestions:', error) toast.error('Failed to fetch label suggestions') onOpenChange(false) } finally { setLoading(false) } } const toggleLabelSelection = (labelName: string) => { const newSelected = new Set(selectedLabels) if (newSelected.has(labelName)) { newSelected.delete(labelName) } else { newSelected.add(labelName) } setSelectedLabels(newSelected) } const handleCreateLabels = async () => { if (!suggestions || selectedLabels.size === 0) { toast.error('No labels selected') return } setCreating(true) try { const response = await fetch('/api/ai/auto-labels', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ suggestions, selectedLabels: Array.from(selectedLabels), }), }) const data = await response.json() if (data.success) { toast.success( t('ai.autoLabels.created', { count: data.data.createdCount }) || `${data.data.createdCount} labels created successfully` ) onLabelsCreated() onOpenChange(false) } else { toast.error(data.error || 'Failed to create labels') } } catch (error) { console.error('Failed to create labels:', error) toast.error('Failed to create labels') } finally { setCreating(false) } } if (loading) { return (

{t('ai.autoLabels.analyzing')}

) } if (!suggestions) { return null } return ( {t('ai.autoLabels.title')} {t('ai.autoLabels.description', { notebook: suggestions.notebookName, count: suggestions.totalNotes, })}
{suggestions.suggestedLabels.map((label) => (
toggleLabelSelection(label.name)} > toggleLabelSelection(label.name)} aria-label={`Select label: ${label.name}`} />
{label.name}
{t('ai.autoLabels.notesCount', { count: label.count })} {Math.round(label.confidence * 100)}% confidence
))}
) }