'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 { Badge } from './ui/badge' import { Settings, Plus, Palette, Trash2, Tag } from 'lucide-react' import { LABEL_COLORS, LabelColorName } from '@/lib/types' import { cn } from '@/lib/utils' import { useLabels } from '@/context/LabelContext' import { useLanguage } from '@/lib/i18n' export function LabelManagementDialog() { const { labels, loading, addLabel, updateLabel, deleteLabel } = useLabels() const { t } = useLanguage() const [newLabel, setNewLabel] = useState('') const [editingColorId, setEditingColorId] = useState(null) const handleAddLabel = async () => { const trimmed = newLabel.trim() if (trimmed) { try { await addLabel(trimmed, 'gray') setNewLabel('') } catch (error) { console.error('Failed to add label:', error) } } } const handleDeleteLabel = async (id: string) => { if (confirm(t('labels.confirmDelete'))) { try { await deleteLabel(id) } catch (error) { console.error('Failed to delete label:', error) } } } const handleChangeColor = async (id: string, color: LabelColorName) => { try { await updateLabel(id, { color }) setEditingColorId(null) } catch (error) { console.error('Failed to update label color:', error) } } return ( { // Prevent dialog from closing when interacting with Sonner toasts 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')}
{/* Add new label */}
setNewLabel(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault() handleAddLabel() } }} />
{/* List labels */}
{loading ? (

{t('labels.loading')}

) : labels.length === 0 ? (

{t('labels.noLabelsFound')}

) : ( labels.map((label) => { const colorClasses = LABEL_COLORS[label.color] const isEditing = editingColorId === label.id return (
{label.name} {/* Color Picker Popover */} {isEditing && (
{(Object.keys(LABEL_COLORS) as LabelColorName[]).map((color) => { const classes = LABEL_COLORS[color] return (
)}
) }) )}
) }