import React, { useState } from 'react' import { Plus, Trash2, Loader2 } from 'lucide-react' import Modal from '../shared/Modal' import type { CollectionLabel, CollectionLabelUpdateRequest } from '@trek/shared' import type { TranslationFn } from '../../types' const SWATCHES = ['#6366f1', '#0ea5e9', '#10b981', '#f59e0b', '#ef4444', '#ec4899', '#8b5cf6', '#64748b'] interface LabelManagerProps { isOpen: boolean labels: CollectionLabel[] onCreate: (name: string, color?: string) => Promise | void onUpdate: (labelId: number, body: CollectionLabelUpdateRequest) => Promise | void onDelete: (labelId: number) => Promise | void onClose: () => void t: TranslationFn } /** Swatch row shared by the create form and each row's recolor control. */ function Swatches({ value, onPick }: { value: string; onPick: (c: string) => void }): React.ReactElement { return (
{SWATCHES.map(c => (
) } /** One existing label: inline rename (save on blur/Enter), recolor, delete. */ function LabelRow({ label, onUpdate, onDelete, t }: { label: CollectionLabel onUpdate: LabelManagerProps['onUpdate'] onDelete: LabelManagerProps['onDelete'] t: TranslationFn }): React.ReactElement { const [name, setName] = useState(label.name) const [color, setColor] = useState(label.color || '#6366f1') const [busy, setBusy] = useState(false) const commitName = async () => { const trimmed = name.trim() if (!trimmed || trimmed === label.name) { setName(label.name); return } setBusy(true) try { await onUpdate(label.id, { name: trimmed }) } finally { setBusy(false) } } const pickColor = async (c: string) => { setColor(c) setBusy(true) try { await onUpdate(label.id, { color: c }) } finally { setBusy(false) } } return (
setName(e.target.value)} onBlur={commitName} onKeyDown={e => { if (e.key === 'Enter') (e.target as HTMLInputElement).blur() }} maxLength={60} className="flex-1 min-w-0 bg-transparent text-[13px] text-content outline-none" aria-label={t('collections.labels.name')} /> {busy && }
) } /** * Manage a list's custom labels — create, rename, recolor and delete. Available * to any member who can edit the list; the labels are shared by the whole list. */ export default function LabelManager({ isOpen, labels, onCreate, onUpdate, onDelete, onClose, t }: LabelManagerProps): React.ReactElement { const [newName, setNewName] = useState('') const [newColor, setNewColor] = useState(SWATCHES[0]) const [adding, setAdding] = useState(false) const add = async () => { const trimmed = newName.trim() if (!trimmed || adding) return setAdding(true) try { await onCreate(trimmed, newColor) setNewName('') setNewColor(SWATCHES[0]) } finally { setAdding(false) } } return (
{labels.length === 0 ? (

{t('collections.labels.empty')}

) : (
{labels.map(l => )}
)}
setNewName(e.target.value)} onKeyDown={e => { if (e.key === 'Enter') add() }} maxLength={60} placeholder={t('collections.labels.namePlaceholder')} className="flex-1 min-w-0 px-3 py-2 rounded-lg border border-edge bg-surface-input text-content text-[13px] outline-none focus:border-accent" />
) }