import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Check, Plus, Tag, X, Settings } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Popover, PopoverContent, PopoverTrigger, } from '@/components/ui/popover'; import { Separator } from '@/components/ui/separator'; import { Input } from '@/components/ui/input'; import { useLabels } from '@/hooks/useLabels'; import { getLabelColor, LABEL_COLORS, type Label, type LabelColorName } from '@/lib/labels'; import { useProjectLabels } from '@/hooks/useProjectLabels'; import { LabelsManageDialog } from './LabelsManageDialog'; import { cn } from '@/lib/utils'; interface LabelSelectorProps { projectId: string; /** Render as a compact badge list (for inline display) */ compact?: boolean; } const getRandomColor = (): LabelColorName => { const randomIndex = Math.floor(Math.random() * LABEL_COLORS.length); return LABEL_COLORS[randomIndex].name; }; export function LabelSelector({ projectId, compact }: LabelSelectorProps) { const { t } = useTranslation(); const { labels, createLabel } = useLabels(); const { getProjectLabels, addLabelToProject, removeLabelFromProject } = useProjectLabels(); const [open, setOpen] = useState(false); const [manageDialogOpen, setManageDialogOpen] = useState(false); const [showCreateForm, setShowCreateForm] = useState(false); const [newLabelName, setNewLabelName] = useState(''); const [selectedColor, setSelectedColor] = useState(getRandomColor()); const projectLabelIds = getProjectLabels(projectId); const projectLabels = labels.filter(label => projectLabelIds.includes(label.id)); const handleToggleLabel = (label: Label) => { if (projectLabelIds.includes(label.id)) { removeLabelFromProject(projectId, label.id); } else { addLabelToProject(projectId, label.id); } }; const handleCreateLabel = () => { if (!newLabelName.trim()) return; const newLabel = createLabel(newLabelName.trim(), selectedColor); addLabelToProject(projectId, newLabel.id); // Reset form setNewLabelName(''); setSelectedColor(getRandomColor()); setShowCreateForm(false); }; const handleCancelCreate = () => { setNewLabelName(''); setSelectedColor(getRandomColor()); setShowCreateForm(false); }; if (compact) { return (
{projectLabels.map((label) => { const colorConfig = getLabelColor(label.color); return ( {label.name} ); })}
); } return ( <>

{t('selectLabels')}

{labels.length > 0 ? (
{ // Allow wheel scrolling within this container e.stopPropagation(); }} > {labels.map((label) => { const colorConfig = getLabelColor(label.color); const isSelected = projectLabelIds.includes(label.id); return ( ); })}
) : (
{t('noLabelsCreated')}
)}
{showCreateForm ? (
setNewLabelName(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') { handleCreateLabel(); } else if (e.key === 'Escape') { handleCancelCreate(); } }} className="flex-1 h-8" autoFocus />
{LABEL_COLORS.map((color) => (
) : ( )}
); }