import { useId } from 'react' import { Plus, Pencil, Trash2, Pipette, Tags, Loader2 } from 'lucide-react' import { DialogButton, DialogFooter, DialogHeader, DialogSection, DialogShell, DialogTile, FooterSpacer, NEUTRAL_TINT, fs } from '../shared/DialogShell' import { Tooltip } from '../shared/Tooltip' import ConfirmDialog from '../shared/ConfirmDialog' import { SettingRows, SettingsCard, SettingsHint, StatusPill, SETTINGS_BUTTON_PRIMARY, SETTINGS_ICON_BUTTON } from '../Settings/settingsKit' import { CATEGORY_ICON_MAP, ICON_LABELS, getCategoryIcon } from '../shared/categoryIcons' import { useTranslation } from '../../i18n' import { ICON_NAMES, PRESET_COLORS, useCategoryAdmin } from './useCategoryAdmin' export default function CategoryManager() { const { categories, showForm, editingId, form, setForm, isSaving, isLoading, deleteId, setDeleteId, colorInputRef, handleStartEdit, handleStartCreate, handleCancel, handleSave, handleDelete, isPresetColor, } = useCategoryAdmin() const { t } = useTranslation() const labelId = useId() const PreviewIcon = getCategoryIcon(form.icon) const formOpen = showForm || editingId !== null const saveDisabled = isSaving || !form.name.trim() const categoryDialog = ( } tint={NEUTRAL_TINT} labelId={labelId} onClose={handleCancel} eyebrow={editingId ? t('common.edit') : t('categories.new')} titleInput={{ value: form.name, onChange: name => setForm(prev => ({ ...prev, name })), label: t('categories.namePlaceholder'), placeholder: t('categories.namePlaceholder'), autoFocus: true, onKeyDown: e => { if (e.key === 'Enter' && !saveDisabled) void handleSave() }, }} /> } footer={ {t('common.cancel')} {isSaving ? t('common.saving') : editingId ? t('categories.update') : t('categories.create')} } > {ICON_NAMES.map(name => { const Icon = CATEGORY_ICON_MAP[name] const isSelected = form.icon === name return ( setForm(prev => ({ ...prev, icon: name }))} className={`grid h-[38px] place-items-center rounded-[10px] transition-colors ${ isSelected ? 'bg-surface-card shadow-sm ring-2 ring-content' : 'text-content-secondary hover:bg-surface-card' }`} style={isSelected ? { background: `${form.color}18` } : undefined} > ) })} {PRESET_COLORS.map(color => ( setForm(prev => ({ ...prev, color }))} aria-label={color} aria-pressed={form.color === color} className={`h-7 w-7 rounded-full transition-transform hover:scale-110 ${form.color === color ? 'scale-110 ring-2 ring-content ring-offset-2 ring-offset-surface-card' : ''}`} style={{ backgroundColor: color }} /> ))} {/* Custom color button */} setForm(prev => ({ ...prev, color: e.target.value }))} className="sr-only" tabIndex={-1} aria-hidden /> colorInputRef.current?.click()} className={`grid h-7 w-7 place-items-center rounded-full border-2 transition-transform hover:scale-110 ${ !isPresetColor ? 'scale-110 border-transparent ring-2 ring-content ring-offset-2 ring-offset-surface-card' : 'border-dashed border-edge text-content-faint hover:border-content-faint hover:text-content-muted' }`} style={!isPresetColor ? { backgroundColor: form.color } : undefined} > {isPresetColor && } {form.name || t('categories.defaultName')} ) return ( 0 ? {categories.length} : undefined} action={ {t('categories.new')} } > {isLoading ? ( ) : categories.length === 0 ? ( {t('categories.empty')} ) : ( {categories.map(cat => { const Icon = getCategoryIcon(cat.icon) return ( {cat.name} {cat.color} handleStartEdit(cat)} aria-label={t('common.edit')} className={SETTINGS_ICON_BUTTON}> setDeleteId(cat.id)} aria-label={t('common.delete')} className={`${SETTINGS_ICON_BUTTON} hover:!text-danger`}> ) })} )} {formOpen && categoryDialog} setDeleteId(null)} onConfirm={() => { if (deleteId !== null) void handleDelete(deleteId) }} message={t('categories.confirm.delete')} confirmLabel={t('common.delete')} danger /> ) }