import { useTranslation } from '../../i18n' import { Plus, Trash2, Pencil, Package, X, Check, ChevronRight, FolderPlus, Loader2 } from 'lucide-react' import { fs } from '../shared/DialogShell' import { Tooltip } from '../shared/Tooltip' import ConfirmDialog from '../shared/ConfirmDialog' import { COMPOSER } from '../Packing/packingPopoverStyles' import { SettingRows, SettingsCard, SettingsHint, StatusPill, SETTINGS_BUTTON_PRIMARY, SETTINGS_ICON_BUTTON } from '../Settings/settingsKit' import { type TemplateItem, usePackingTemplateAdmin } from './usePackingTemplateAdmin' /** The planner's composer (#2541): a framed field on the card with its confirm and cancel beside it. */ const COMPOSER_ROW = 'flex items-center gap-2 rounded-[10px] border border-edge bg-surface-card py-1 ps-3 pe-1' const COMPOSER_INPUT = 'min-w-0 flex-1 border-0 bg-transparent py-1 font-medium text-content outline-none placeholder:text-content-faint' const COMPOSER_CANCEL = 'grid h-7 w-7 flex-none place-items-center rounded-[8px] text-content-faint hover:bg-surface-secondary hover:text-content' /** A name being renamed in place, sized to the row it sits in. */ const ROW_INPUT = 'block min-w-0 rounded-[8px] border border-edge bg-surface-input px-2 py-1 text-content outline-none focus:ring-2 focus:ring-[color:var(--text-primary)]' /** The quieter icon action of a category head or an item row, named by its tooltip. */ const SMALL_ICON_BUTTON = 'grid h-7 w-7 flex-none place-items-center rounded-[8px] text-content-faint transition-colors hover:bg-surface-card hover:text-content' /** An item's actions show while the pointer or the focus is on its row. */ const REVEAL = 'opacity-0 focus-visible:opacity-100 group-hover:opacity-100 group-focus-within:opacity-100' /** "2×, 300 g, Backpack": what a template item brings along besides its name (#1131). */ function templateItemMeta(item: TemplateItem): string { const parts: string[] = [] if ((item.quantity ?? 1) > 1) parts.push(`${item.quantity}×`) if (item.weight_grams) parts.push(item.weight_grams >= 1000 ? `${(item.weight_grams / 1000).toFixed(1)} kg` : `${item.weight_grams} g`) if (item.bag_name) parts.push(item.bag_name) return parts.join(', ') } export default function PackingTemplateManager() { const { templates, isLoading, showCreate, setShowCreate, createName, setCreateName, expandedId, categories, items, editingTemplate, setEditingTemplate, editTemplateName, setEditTemplateName, editingCatId, setEditingCatId, editCatName, setEditCatName, editingItemId, setEditingItemId, editItemName, setEditItemName, addingCategory, setAddingCategory, newCatName, setNewCatName, addingItemToCatId, setAddingItemToCatId, newItemName, setNewItemName, addItemRef, toggleExpand, handleCreateTemplate, deleteTarget, setDeleteTarget, requestDeleteTemplate, requestDeleteCategory, confirmDelete, handleRenameTemplate, handleAddCategory, handleRenameCategory, handleAddItem, handleRenameItem, handleDeleteItem, } = usePackingTemplateAdmin() const { t } = useTranslation() const composerConfirmClass = (enabled: boolean) => `grid h-7 w-7 flex-none place-items-center rounded-[8px] bg-accent text-accent-text transition-opacity disabled:cursor-default ${enabled ? 'hover:opacity-90' : 'opacity-40'}` return ( 0 ? {templates.length} : undefined} action={ } > {/* Create template: the planner's composer, a framed field with its confirm beside it */} {showCreate && (
setCreateName(e.target.value)} onKeyDown={e => { if (e.key === 'Enter') void handleCreateTemplate(); if (e.key === 'Escape') setShowCreate(false) }} placeholder={t('admin.packingTemplates.namePlaceholder')} className={COMPOSER_INPUT} style={fs(13, 'body')} />
)} {/* Template list */} {isLoading ? (
) : templates.length === 0 ? (
{t('admin.packingTemplates.empty')}
) : ( {templates.map(tmpl => { const expanded = expandedId === tmpl.id return (
{/* Template row */}
{editingTemplate === tmpl.id ? ( setEditTemplateName(e.target.value)} onBlur={() => handleRenameTemplate(tmpl.id)} onKeyDown={e => { if (e.key === 'Enter') void handleRenameTemplate(tmpl.id); if (e.key === 'Escape') setEditingTemplate(null) }} aria-label={t('common.rename')} className={`${ROW_INPUT} flex-1 font-semibold`} style={fs(13, 'body')} /> ) : ( )} {tmpl.category_count} {t('admin.packingTemplates.categories', { count: tmpl.category_count })}, {tmpl.item_count} {t('admin.packingTemplates.items', { count: tmpl.item_count })}
{/* Expanded content: the template's categories as the packing list draws them */} {expanded && (
{categories.map(cat => { const catItems = items.filter(i => i.category_id === cat.id) return (
{/* Category header */}
{editingCatId === cat.id ? ( setEditCatName(e.target.value)} onBlur={() => handleRenameCategory(cat.id)} onKeyDown={e => { if (e.key === 'Enter') void handleRenameCategory(cat.id); if (e.key === 'Escape') setEditingCatId(null) }} aria-label={t('common.rename')} className={`${ROW_INPUT} flex-1 font-semibold`} style={fs(12.5, 'body')} /> ) : ( {cat.name} )} {catItems.length}
{/* Items */} {(catItems.length > 0 || addingItemToCatId === cat.id) && (
{catItems.map(item => (
{editingItemId === item.id ? ( <> setEditItemName(e.target.value)} onKeyDown={e => { if (e.key === 'Enter') void handleRenameItem(item.id); if (e.key === 'Escape') setEditingItemId(null) }} aria-label={t('common.rename')} className={`${ROW_INPUT} flex-1`} style={fs(12.5, 'body')} /> ) : ( <> {item.name} {templateItemMeta(item) && ( {templateItemMeta(item)} )} )}
))} {/* Add item inline */} {addingItemToCatId === cat.id && (
setNewItemName(e.target.value)} onKeyDown={e => { if (e.key === 'Enter' && newItemName.trim()) void handleAddItem(cat.id); if (e.key === 'Escape') { setAddingItemToCatId(null); setNewItemName('') } }} placeholder={t('admin.packingTemplates.itemName')} className={`${ROW_INPUT} flex-1`} style={fs(12.5, 'body')} />
)}
)}
) })} {/* Add category */} {addingCategory ? (
setNewCatName(e.target.value)} onKeyDown={e => { if (e.key === 'Enter') void handleAddCategory(); if (e.key === 'Escape') { setAddingCategory(false); setNewCatName('') } }} placeholder={t('admin.packingTemplates.categoryName')} className={COMPOSER_INPUT} style={fs(13, 'body')} />
) : ( )}
)}
) })}
)} setDeleteTarget(null)} onConfirm={() => void confirmDelete()} title={t(deleteTarget?.kind === 'category' ? 'admin.packingTemplates.deleteCategoryTitle' : 'admin.packingTemplates.deleteTemplateTitle')} message={t(deleteTarget?.kind === 'category' ? 'admin.packingTemplates.deleteCategoryConfirm' : 'admin.packingTemplates.deleteTemplateConfirm', { name: deleteTarget?.name ?? '' })} confirmLabel={t('common.delete')} danger />
) }