'use client' import { useState, useEffect } from 'react' import { Button } from './ui/button' import { Input } from './ui/input' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from './ui/dialog' import { Badge } from './ui/badge' import { Tag, X, Plus, Palette } from 'lucide-react' import { LABEL_COLORS, LabelColorName } from '@/lib/types' import { cn } from '@/lib/utils' import { useLabels, Label } from '@/context/LabelContext' interface LabelManagerProps { existingLabels: string[] onUpdate: (labels: string[]) => void } export function LabelManager({ existingLabels, onUpdate }: LabelManagerProps) { const { labels, loading, addLabel, updateLabel, deleteLabel, getLabelColor } = useLabels() const [open, setOpen] = useState(false) const [newLabel, setNewLabel] = useState('') const [selectedLabels, setSelectedLabels] = useState(existingLabels) const [editingColor, setEditingColor] = useState(null) // Sync selected labels with existingLabels prop useEffect(() => { setSelectedLabels(existingLabels) }, [existingLabels]) const handleAddLabel = async () => { const trimmed = newLabel.trim() if (trimmed && !selectedLabels.includes(trimmed)) { try { // Get existing label color or use random const existingLabel = labels.find(l => l.name === trimmed) const color = existingLabel?.color || (Object.keys(LABEL_COLORS) as LabelColorName[])[Math.floor(Math.random() * Object.keys(LABEL_COLORS).length)] await addLabel(trimmed, color) const updated = [...selectedLabels, trimmed] setSelectedLabels(updated) setNewLabel('') } catch (error) { console.error('Failed to add label:', error) } } } const handleRemoveLabel = (label: string) => { setSelectedLabels(selectedLabels.filter(l => l !== label)) } const handleSelectExisting = (label: string) => { if (!selectedLabels.includes(label)) { setSelectedLabels([...selectedLabels, label]) } else { setSelectedLabels(selectedLabels.filter(l => l !== label)) } } const handleChangeColor = async (label: string, color: LabelColorName) => { const labelObj = labels.find(l => l.name === label) if (labelObj) { try { await updateLabel(labelObj.id, { color }) setEditingColor(null) } catch (error) { console.error('Failed to update label color:', error) } } } const handleSave = () => { onUpdate(selectedLabels) setOpen(false) } const handleCancel = () => { setSelectedLabels(existingLabels) setEditingColor(null) setOpen(false) } return ( { if (!isOpen) { handleCancel() } else { setOpen(true) } }}> Manage Labels Add or remove labels for this note. Click on a label to change its color.
{/* Add new label */}
setNewLabel(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault() handleAddLabel() } }} />
{/* Selected labels */} {selectedLabels.length > 0 && (

Selected Labels

{selectedLabels.map((label) => { const labelObj = labels.find(l => l.name === label) const colorClasses = labelObj ? LABEL_COLORS[labelObj.color] : LABEL_COLORS.gray const isEditing = editingColor === label return (
{isEditing && labelObj ? (
{(Object.keys(LABEL_COLORS) as LabelColorName[]).map((color) => { const classes = LABEL_COLORS[color] return (
) : null} setEditingColor(isEditing ? null : label)} > {label}
) })}
)} {/* Available labels from context */} {!loading && labels.length > 0 && (

All Labels

{labels .filter(label => !selectedLabels.includes(label.name)) .map((label) => { const colorClasses = LABEL_COLORS[label.color] return ( handleSelectExisting(label.name)} > {label.name} ) })}
)}
) }