import { useId, useState } from 'react' import { Plus, Tags, Trash2 } from 'lucide-react' import { NOTE_COLORS } from './CollabNotes.constants' import { EditableCatName } from './CollabNotesEditableCatName' import { originalCategoryName } from './CollabNotes.helpers' import { DialogButton, DialogFooter, DialogHeader, DialogSection, DialogShell, DialogTile, FooterSpacer, NEUTRAL_TINT, fs } from '../shared/DialogShell' import { INPUT } from '../shared/dialogParts' import { Tooltip } from '../shared/Tooltip' // ── Category Settings Modal ────────────────────────────────────────────────── interface CategorySettingsModalProps { onClose: () => void categories: string[] categoryColors: Record onSave: (colors: Record, renames: Record) => void | Promise onRenameCategory: (oldName: string, newName: string) => Promise t: (key: string) => string } export function CategorySettingsModal({ onClose, categories, categoryColors, onSave, onRenameCategory, t }: CategorySettingsModalProps) { // One list of rows: the colours known so far plus every category a note still uses. const [localColors, setLocalColors] = useState>(() => { const seeded = { ...categoryColors } for (const cat of categories) if (!seeded[cat]) seeded[cat] = NOTE_COLORS[0].value return seeded }) const [renames, setRenames] = useState>({}) // { oldName: newName } const [newCatName, setNewCatName] = useState('') const labelId = useId() const handleColorChange = (cat, color) => { setLocalColors(prev => ({ ...prev, [cat]: color })) } const handleAddCategory = () => { if (!newCatName.trim() || localColors[newCatName.trim()]) return setLocalColors(prev => ({ ...prev, [newCatName.trim()]: NOTE_COLORS[Object.keys(prev).length % NOTE_COLORS.length].value })) setNewCatName('') } const handleRemoveCategory = (cat) => { setLocalColors(prev => { const n = { ...prev }; delete n[cat]; return n }) } const handleRenameCategory = (oldName, newName) => { if (!newName.trim() || newName.trim() === oldName || localColors[newName.trim()]) return // Track rename for saving to DB later const originalName = Object.entries(renames).find(([, v]) => v === oldName)?.[0] || oldName setRenames(prev => ({ ...prev, [originalName]: newName.trim() })) setLocalColors(prev => { const n = {} for (const [k, v] of Object.entries(prev)) { n[k === oldName ? newName.trim() : k] = v } return n }) } const handleSave = async () => { // Apply renames to notes in DB. A rejected rename keeps the modal open — closing // it would look like the rename went through; the caller has already reported it. try { for (const [oldName, newName] of Object.entries(renames)) { if (oldName !== newName) await onRenameCategory(oldName, newName) } } catch { return } await onSave(localColors, renames) onClose() } const allCats = Object.keys(localColors) // A category that notes still use cannot be removed here; those notes would only bring it back. const inUse = (cat: string) => categories.includes(originalCategoryName(cat, renames)) return ( } tint={NEUTRAL_TINT} labelId={labelId} onClose={onClose} title={t('collab.notes.categorySettings') || 'Category Settings'} /> )} footer={( {t('collab.notes.cancel')} {t('collab.notes.save')} )} > {allCats.length === 0 ? (

{t('collab.notes.noCategoriesYet') || 'No categories yet'}

) : (
{allCats.map(cat => { const current = localColors[cat] || NOTE_COLORS[0].value return (
{/* Category name — editable */} handleRenameCategory(cat, newName)} renameLabel={t('common.rename')} /> {/* Color swatches */}
{NOTE_COLORS.map(c => { const on = current === c.value return (
{/* Delete */}
) })}
)} {/* Add new */}
setNewCatName(e.target.value)} onKeyDown={e => e.key === 'Enter' && handleAddCategory()} placeholder={t('collab.notes.newCategory')} className={`${INPUT} flex-1`} />

{t('collab.notes.categoriesLocalHint')}

) }