'use client' import { useState } from 'react' import { Button } from './ui/button' import { Input } from './ui/input' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from './ui/dialog' import { Settings, Plus, Palette, Trash2, Sparkles } from 'lucide-react' import { LABEL_COLORS, LabelColorName } from '@/lib/types' import { cn } from '@/lib/utils' import { useNotebooks } from '@/context/notebooks-context' import { useLanguage } from '@/lib/i18n' import { useRefresh } from '@/lib/use-refresh' export interface LabelManagementDialogProps { open?: boolean onOpenChange?: (open: boolean) => void } export function LabelManagementDialog(props: LabelManagementDialogProps = {}) { const { open, onOpenChange } = props const { labels, isLoading: loading, addLabel, updateLabel, deleteLabel } = useNotebooks() const { t, language } = useLanguage() const { refreshLabels } = useRefresh() const [confirmDeleteId, setConfirmDeleteId] = useState(null) const [newLabel, setNewLabel] = useState('') const [editingColorId, setEditingColorId] = useState(null) const controlled = open !== undefined && onOpenChange !== undefined const handleAddLabel = async () => { const trimmed = newLabel.trim() if (trimmed) { try { await addLabel(trimmed, 'gray') refreshLabels() setNewLabel('') } catch (error) { console.error('Failed to add label:', error) } } } const handleDeleteLabel = async (id: string) => { try { const labelToDelete = labels.find(l => l.id === id) await deleteLabel(id) refreshLabels() if (labelToDelete) { window.dispatchEvent(new CustomEvent('label-deleted', { detail: { name: labelToDelete.name } })) } setConfirmDeleteId(null) } catch (error) { console.error('Failed to delete label:', error) } } const handleChangeColor = async (id: string, color: LabelColorName) => { try { await updateLabel(id, { color }) refreshLabels() setEditingColorId(null) } catch (error) { console.error('Failed to update label color:', error) } } const dialogContent = ( { const target = event.target as HTMLElement; const isSonnerElement = target.closest('[data-sonner-toast]') || target.closest('[data-sonner-toaster]') || target.closest('[data-icon]') || target.closest('[data-content]') || target.closest('[data-description]') || target.closest('[data-title]') || target.closest('[data-button]'); if (isSonnerElement) { event.preventDefault(); return; } if (target.getAttribute('data-sonner-toaster') !== null) { event.preventDefault(); return; } }} > {t('labels.editLabels')} {t('labels.editLabelsDescription')}
setNewLabel(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault() handleAddLabel() } }} />
{loading ? (

{t('labels.loading')}

) : labels.length === 0 ? (

{t('labels.noLabelsFound')}

) : ( labels.map((label) => { const colorClasses = LABEL_COLORS[label.color] const isEditing = editingColorId === label.id const isAI = label.type === 'ai' return (
{isAI ? ( ) : (
)} {label.name} {isAI && ( IA )} {isEditing && (
{(Object.keys(LABEL_COLORS) as LabelColorName[]).map((color) => { const classes = LABEL_COLORS[color] return (
)}
{confirmDeleteId === label.id ? (
{t('labels.confirmDeleteShort') || 'Confirmer ?'}
) : (
)}
) }) )}
) if (controlled) { return ( {dialogContent} ) } return ( {dialogContent} ) }