import { useState, useMemo, type ReactNode } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { useTranslation } from 'react-i18next'; import { Plus, Loader2, Trash2, Archive, RotateCcw, Edit2, Package, Search, ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight, TrendingDown, Layers, Printer, AlertTriangle, X, Clock, LayoutGrid, TableProperties, Columns, ArrowUp, ArrowDown, ArrowUpDown, Group, ChevronDown, } from 'lucide-react'; import { api } from '../api/client'; import type { InventorySpool, SpoolAssignment } from '../api/client'; import { Button } from '../components/Button'; import { SpoolFormModal } from '../components/SpoolFormModal'; import { ConfirmModal } from '../components/ConfirmModal'; import { ColumnConfigModal, type ColumnConfig } from '../components/ColumnConfigModal'; import { useToast } from '../contexts/ToastContext'; import { resolveSpoolColorName } from '../utils/colors'; import { getCurrencySymbol } from '../utils/currency'; import { formatDateInput, parseUTCDate, type DateFormat } from '../utils/date'; import { formatSlotLabel } from '../utils/amsHelpers'; type ArchiveFilter = 'active' | 'archived'; type UsageFilter = 'all' | 'used' | 'new'; type ViewMode = 'table' | 'cards'; type SortDirection = 'asc' | 'desc'; type SortState = { column: string; direction: SortDirection } | null; type DisplayItem = | { type: 'single'; spool: InventorySpool } | { type: 'group'; key: string; spools: InventorySpool[]; representative: InventorySpool }; function spoolGroupKey(s: InventorySpool): string { return `${s.material}|${s.subtype || ''}|${s.brand || ''}|${s.color_name || ''}|${s.rgba || ''}|${s.label_weight}`; } // Column definitions for the inventory table const COLUMN_CONFIG_KEY = 'bambuddy-inventory-columns'; const DEFAULT_COLUMNS: ColumnConfig[] = [ { id: 'id', label: '#', visible: true }, { id: 'added_time', label: 'Added', visible: true }, { id: 'encode_time', label: 'Encoded', visible: false }, { id: 'last_used_time', label: 'Last Used', visible: false }, { id: 'rgba', label: 'Color', visible: true }, { id: 'material', label: 'Material', visible: true }, { id: 'subtype', label: 'Subtype', visible: true }, { id: 'color_name', label: 'Color Name', visible: false }, { id: 'brand', label: 'Brand', visible: true }, { id: 'slicer_filament', label: 'Slicer Filament', visible: false }, { id: 'location', label: 'Location', visible: true }, { id: 'label_weight', label: 'Label', visible: true }, { id: 'net', label: 'Net', visible: true }, { id: 'gross', label: 'Gross', visible: false }, { id: 'added_full', label: 'Full', visible: false }, { id: 'used', label: 'Used', visible: false }, { id: 'printed_total', label: 'Printed Total', visible: false }, { id: 'printed_since_weight', label: 'Printed Since Weight', visible: false }, { id: 'note', label: 'Note', visible: false }, { id: 'pa_k', label: 'PA(K)', visible: true }, { id: 'tag_id', label: 'Tag ID', visible: false }, { id: 'data_origin', label: 'Data Origin', visible: false }, { id: 'tag_type', label: 'Linked Tag Type', visible: false }, { id: 'stock', label: 'Stock', visible: false }, { id: 'remaining', label: 'Remaining', visible: true }, { id: 'cost_per_kg', label: 'Cost/kg', visible: false }, ]; function loadColumnConfig(): ColumnConfig[] { try { const stored = localStorage.getItem(COLUMN_CONFIG_KEY); if (stored) { const parsed = JSON.parse(stored) as ColumnConfig[]; const defaultIds = new Set(DEFAULT_COLUMNS.map((c) => c.id)); const storedIds = new Set(parsed.map((c) => c.id)); // Keep stored columns that still exist in defaults const validStored = parsed.filter((c) => defaultIds.has(c.id)); // Add any new default columns not in stored config const newColumns = DEFAULT_COLUMNS.filter((c) => !storedIds.has(c.id)); return [...validStored, ...newColumns]; } } catch { // Ignore errors } return DEFAULT_COLUMNS.map((c) => ({ ...c })); } function saveColumnConfig(config: ColumnConfig[]) { try { localStorage.setItem(COLUMN_CONFIG_KEY, JSON.stringify(config)); } catch { // Ignore errors } } function formatWeight(g: number, useKg = false): string { if (useKg && g >= 1000) return `${(g / 1000).toFixed(1)}kg`; return `${Math.round(g)}g`; } // Material color mapping for pills const MATERIAL_COLORS: Record = { PLA: 'bg-green-500/20 text-green-400', ABS: 'bg-red-500/20 text-red-400', PETG: 'bg-blue-500/20 text-blue-400', TPU: 'bg-purple-500/20 text-purple-400', ASA: 'bg-orange-500/20 text-orange-400', PA: 'bg-yellow-500/20 text-yellow-400', PC: 'bg-cyan-500/20 text-cyan-400', PET: 'bg-sky-500/20 text-sky-400', }; type TFn = (key: string, opts?: Record) => string; function formatInventoryDate(dateStr: string | null, dateFormat: DateFormat = 'system'): string { if (!dateStr) return '-'; const date = parseUTCDate(dateStr); if (!date) return '-'; return formatDateInput(date, dateFormat); } type CellCtx = { spool: InventorySpool; remaining: number; pct: number; assignmentMap: Record; currencySymbol: string; dateFormat: DateFormat; t: TFn; }; // Column header labels (25 columns — matching SpoolBuddy exactly) const columnHeaders: Record string> = { id: () => '#', added_time: () => 'Added', encode_time: () => 'Encoded', last_used_time: () => 'Last Used', rgba: (t) => t('inventory.color'), material: (t) => t('inventory.material'), subtype: (t) => t('inventory.subtype'), color_name: (t) => t('inventory.colorName'), brand: (t) => t('inventory.brand'), slicer_filament: (t) => t('inventory.slicerFilament'), location: () => 'Location', label_weight: (t) => t('inventory.labelWeight'), net: (t) => t('inventory.net'), gross: () => 'Gross', added_full: () => 'Full', used: (t) => t('inventory.weightUsed'), printed_total: () => 'Printed Total', printed_since_weight: () => 'Printed Since Weight', note: (t) => t('inventory.note'), pa_k: () => 'PA(K)', tag_id: () => 'Tag ID', data_origin: () => 'Data Origin', tag_type: () => 'Linked Tag Type', stock: (t) => t('inventory.stock'), remaining: (t) => t('inventory.remaining'), cost_per_kg: (t) => t('inventory.costPerKg'), }; // Column cell renderers (25 columns — matching SpoolBuddy exactly) const columnCells: Record ReactNode> = { id: ({ spool }) => ( {spool.id} ), added_time: ({ spool, dateFormat }) => ( {formatInventoryDate(spool.created_at, dateFormat)} ), encode_time: ({ spool, dateFormat }) => ( {formatInventoryDate(spool.encode_time, dateFormat)} ), last_used_time: ({ spool, dateFormat }) => ( {spool.last_used ? formatInventoryDate(spool.last_used, dateFormat) : 'Never'} ), rgba: ({ spool }) => (
), material: ({ spool }) => ( {spool.material} ), subtype: ({ spool }) => ( {spool.subtype || '-'} ), color_name: ({ spool }) => ( {resolveSpoolColorName(spool.color_name, spool.rgba) || '-'} ), brand: ({ spool }) => ( {spool.brand || '-'} ), slicer_filament: ({ spool }) => ( {spool.slicer_filament_name || spool.slicer_filament || '-'} ), location: ({ spool, assignmentMap }) => { const assignment = assignmentMap[spool.id]; if (!assignment) return -; const printerLabel = assignment.printer_name || `Printer ${assignment.printer_id}`; const isExternal = assignment.ams_id === 254 || assignment.ams_id === 255; const isHt = !isExternal && assignment.ams_id >= 128; const slotLabel = formatSlotLabel(assignment.ams_id, assignment.tray_id, isHt, isExternal); return ( {printerLabel} {slotLabel} ); }, label_weight: ({ spool }) => ( {formatWeight(spool.label_weight)} ), net: ({ remaining }) => ( {formatWeight(remaining)} ), gross: ({ spool, remaining }) => ( {formatWeight(remaining + spool.core_weight)} ), added_full: ({ spool }) => ( {spool.added_full == null ? '-' : spool.added_full ? 'Yes' : 'No'} ), used: ({ spool }) => ( {spool.weight_used > 0 ? formatWeight(spool.weight_used) : '-'} ), printed_total: () => ( - ), printed_since_weight: () => ( - ), note: ({ spool }) => ( {spool.note || '-'} ), pa_k: ({ spool }) => { const count = spool.k_profiles?.length ?? 0; if (count === 0) return -; return ( K ); }, tag_id: ({ spool }) => { const tag = spool.tag_uid || spool.tray_uuid; if (!tag) return -; return ( {tag.length > 12 ? `${tag.slice(0, 6)}...${tag.slice(-4)}` : tag} ); }, data_origin: ({ spool }) => ( {spool.data_origin || '-'} ), tag_type: ({ spool }) => ( {spool.tag_type || '-'} ), stock: ({ spool, t }) => { if (!spool.slicer_filament) { return ( {t('inventory.stock')} ); } return -; }, remaining: ({ remaining, pct }) => (
50 ? 'bg-bambu-green' : pct > 20 ? 'bg-yellow-500' : 'bg-red-500'}`} style={{ width: `${Math.min(pct, 100)}%` }} />
{Math.round(remaining)}g
), cost_per_kg: ({ spool, currencySymbol }) => ( {spool.cost_per_kg != null ? `${currencySymbol}${spool.cost_per_kg.toFixed(2)}` : '-'} ), }; // Sort value extractors — return a comparable value for each sortable column const columnSortValues: Record) => string | number> = { id: (s) => s.id, added_time: (s) => s.created_at || '', encode_time: (s) => s.encode_time || '', last_used_time: (s) => s.last_used || '', material: (s) => (s.material || '').toLowerCase(), subtype: (s) => (s.subtype || '').toLowerCase(), color_name: (s) => (s.color_name || '').toLowerCase(), brand: (s) => (s.brand || '').toLowerCase(), slicer_filament: (s) => (s.slicer_filament_name || s.slicer_filament || '').toLowerCase(), location: (s, am) => { const a = am[s.id]; if (!a) return ''; const isExt = a.ams_id === 254 || a.ams_id === 255; const isHt = !isExt && a.ams_id >= 128; return `${a.printer_name || ''} ${formatSlotLabel(a.ams_id, a.tray_id, isHt, isExt)}`; }, label_weight: (s) => s.label_weight, net: (s) => Math.max(0, s.label_weight - s.weight_used), gross: (s) => Math.max(0, s.label_weight - s.weight_used) + s.core_weight, used: (s) => s.weight_used, remaining: (s) => s.label_weight > 0 ? Math.max(0, s.label_weight - s.weight_used) / s.label_weight : 0, note: (s) => (s.note || '').toLowerCase(), data_origin: (s) => (s.data_origin || '').toLowerCase(), tag_type: (s) => (s.tag_type || '').toLowerCase(), stock: (s) => s.slicer_filament ? 1 : 0, cost_per_kg: (s) => s.cost_per_kg ?? 0, }; const SORT_STATE_KEY = 'bambuddy-inventory-sort'; function loadSortState(): SortState { try { const stored = localStorage.getItem(SORT_STATE_KEY); if (stored) return JSON.parse(stored); } catch { /* ignore */ } return null; } function saveSortState(state: SortState) { try { if (state) { localStorage.setItem(SORT_STATE_KEY, JSON.stringify(state)); } else { localStorage.removeItem(SORT_STATE_KEY); } } catch { /* ignore */ } } export default function InventoryPage() { const { t } = useTranslation(); const queryClient = useQueryClient(); const { showToast } = useToast(); const [formModal, setFormModal] = useState<{ spool?: InventorySpool | null } | null>(null); const [confirmAction, setConfirmAction] = useState<{ type: 'delete' | 'archive'; spoolId: number } | null>(null); // Filter state const [archiveFilter, setArchiveFilter] = useState('active'); const [usageFilter, setUsageFilter] = useState('all'); const [materialFilter, setMaterialFilter] = useState(''); const [brandFilter, setBrandFilter] = useState(''); const [stockFilter, setStockFilter] = useState<'all' | 'stock' | 'configured'>('all'); const [search, setSearch] = useState(''); const [viewMode, setViewMode] = useState('table'); const [sortState, setSortState] = useState(loadSortState); const [columnConfig, setColumnConfig] = useState(loadColumnConfig); const [showColumnModal, setShowColumnModal] = useState(false); const [groupSimilar, setGroupSimilar] = useState(() => { try { return localStorage.getItem('bambuddy-inventory-group') === 'true'; } catch { return false; } }); const [expandedGroups, setExpandedGroups] = useState>(new Set()); // Pagination state (pageSize persisted to localStorage) const [pageIndex, setPageIndex] = useState(0); const [pageSize, setPageSize] = useState(() => { try { const stored = localStorage.getItem('bambuddy-inventory-pageSize'); if (stored) { const n = Number(stored); if ([15, 30, 50, 100, -1].includes(n)) return n; } } catch { /* ignore */ } return 15; }); const { data: settings } = useQuery({ queryKey: ['settings'], queryFn: api.getSettings, }); const dateFormat: DateFormat = settings?.date_format || 'system'; const { data: spools, isLoading } = useQuery({ queryKey: ['inventory-spools'], queryFn: () => api.getSpools(true), // Always fetch all, filter client-side refetchInterval: 30000, }); const { data: assignments } = useQuery({ queryKey: ['spool-assignments'], queryFn: () => api.getAssignments(), refetchInterval: 30000, }); const deleteMutation = useMutation({ mutationFn: (id: number) => api.deleteSpool(id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['inventory-spools'] }); showToast(t('inventory.spoolDeleted'), 'success'); }, }); const archiveMutation = useMutation({ mutationFn: (id: number) => api.archiveSpool(id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['inventory-spools'] }); showToast(t('inventory.spoolArchived'), 'success'); }, }); const restoreMutation = useMutation({ mutationFn: (id: number) => api.restoreSpool(id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['inventory-spools'] }); showToast(t('inventory.spoolRestored'), 'success'); }, }); // Stats calculation (active spools only) const stats = useMemo(() => { if (!spools) return null; let totalWeight = 0; let totalConsumed = 0; let lowStock = 0; let activeCount = 0; const byMaterial: Record = {}; for (const s of spools) { if (s.archived_at) continue; activeCount++; const remaining = Math.max(0, s.label_weight - s.weight_used); totalWeight += remaining; totalConsumed += s.weight_used; const pct = s.label_weight > 0 ? (remaining / s.label_weight) * 100 : 0; if (pct < 20) lowStock++; const mat = s.material || 'Unknown'; if (!byMaterial[mat]) byMaterial[mat] = { count: 0, weight: 0 }; byMaterial[mat].count++; byMaterial[mat].weight += remaining; } return { totalWeight, totalConsumed, lowStock, byMaterial, totalSpools: activeCount }; }, [spools]); const inPrinterCount = assignments?.length ?? 0; const currencySymbol = getCurrencySymbol(settings?.currency || 'USD'); // Map spool_id -> assignment for location column const assignmentMap = useMemo(() => { const map: Record = {}; for (const a of assignments || []) { map[a.spool_id] = a; } return map; }, [assignments]); // Top materials by weight for stat card pills const topMaterials = useMemo(() => { if (!stats) return []; return Object.entries(stats.byMaterial) .sort((a, b) => b[1].weight - a[1].weight) .slice(0, 4); }, [stats]); // Filtering pipeline const filteredSpools = useMemo(() => { let filtered = spools || []; // Archive filter if (archiveFilter === 'active') { filtered = filtered.filter((s) => !s.archived_at); } else { filtered = filtered.filter((s) => !!s.archived_at); } // Usage filter if (usageFilter === 'used') { filtered = filtered.filter((s) => s.weight_used > 0); } else if (usageFilter === 'new') { filtered = filtered.filter((s) => s.weight_used === 0); } // Material dropdown if (materialFilter) { filtered = filtered.filter((s) => s.material === materialFilter); } // Brand dropdown if (brandFilter) { filtered = filtered.filter((s) => s.brand === brandFilter); } // Stock filter if (stockFilter === 'stock') { filtered = filtered.filter((s) => !s.slicer_filament); } else if (stockFilter === 'configured') { filtered = filtered.filter((s) => !!s.slicer_filament); } // Global search if (search) { const q = search.toLowerCase(); filtered = filtered.filter((s) => s.brand?.toLowerCase().includes(q) || s.material.toLowerCase().includes(q) || s.color_name?.toLowerCase().includes(q) || s.subtype?.toLowerCase().includes(q) || s.note?.toLowerCase().includes(q) || s.slicer_filament_name?.toLowerCase().includes(q) ); } return filtered; }, [spools, archiveFilter, usageFilter, materialFilter, brandFilter, stockFilter, search]); // Reset page on filter changes const resetPage = () => setPageIndex(0); // Unique values for filter dropdowns const uniqueMaterials = [...new Set(spools?.map((s) => s.material) || [])].sort(); const uniqueBrands = [...new Set(spools?.map((s) => s.brand).filter(Boolean) || [])].sort() as string[]; // Check if any filters are non-default const hasActiveFilters = archiveFilter !== 'active' || usageFilter !== 'all' || !!materialFilter || !!brandFilter || stockFilter !== 'all' || !!search; const handleColumnConfigSave = (config: ColumnConfig[]) => { setColumnConfig(config); saveColumnConfig(config); }; // Visible column IDs in order const visibleColumns = useMemo( () => columnConfig.filter((c) => c.visible).map((c) => c.id), [columnConfig] ); const handleSort = (colId: string) => { if (!columnSortValues[colId]) return; // Not sortable setSortState((prev) => { let next: SortState; if (prev?.column === colId) { // Toggle direction, or clear on third click next = prev.direction === 'asc' ? { column: colId, direction: 'desc' } : null; } else { next = { column: colId, direction: 'asc' }; } saveSortState(next); return next; }); resetPage(); }; // Sort filtered spools const sortedSpools = useMemo(() => { if (!sortState) return filteredSpools; const extractor = columnSortValues[sortState.column]; if (!extractor) return filteredSpools; const sorted = [...filteredSpools].sort((a, b) => { const va = extractor(a, assignmentMap); const vb = extractor(b, assignmentMap); if (va < vb) return sortState.direction === 'asc' ? -1 : 1; if (va > vb) return sortState.direction === 'asc' ? 1 : -1; return 0; }); return sorted; }, [filteredSpools, sortState, assignmentMap]); // Group similar spools when toggle is active const displayItems = useMemo((): DisplayItem[] => { if (!groupSimilar) return sortedSpools.map((s) => ({ type: 'single' as const, spool: s })); const groups = new Map(); for (const spool of sortedSpools) { // Only group unused & unassigned spools if (spool.weight_used > 0 || assignmentMap[spool.id]) { // Will be added as singles in the walk below } else { const key = spoolGroupKey(spool); const arr = groups.get(key); if (arr) arr.push(spool); else groups.set(key, [spool]); } } const items: DisplayItem[] = []; const processedKeys = new Set(); // Walk sortedSpools order so groups appear at the position of their first member for (const spool of sortedSpools) { if (spool.weight_used > 0 || assignmentMap[spool.id]) { items.push({ type: 'single', spool }); continue; } const key = spoolGroupKey(spool); if (processedKeys.has(key)) continue; processedKeys.add(key); const members = groups.get(key)!; if (members.length === 1) { items.push({ type: 'single', spool: members[0] }); } else { items.push({ type: 'group', key, spools: members, representative: members[0] }); } } return items; }, [sortedSpools, groupSimilar, assignmentMap]); // Pagination (after sorting) — pageSize -1 means "All" const showAll = pageSize === -1; const totalDisplayItems = displayItems.length; const effectivePageSize = showAll ? totalDisplayItems || 1 : pageSize; const totalPages = Math.max(1, Math.ceil(totalDisplayItems / effectivePageSize)); const safePageIndex = showAll ? 0 : Math.min(pageIndex, totalPages - 1); const pagedItems = showAll ? displayItems : displayItems.slice(safePageIndex * effectivePageSize, (safePageIndex + 1) * effectivePageSize); const toggleGroupSimilar = () => { const next = !groupSimilar; setGroupSimilar(next); setExpandedGroups(new Set()); resetPage(); try { localStorage.setItem('bambuddy-inventory-group', String(next)); } catch { /* ignore */ } }; const toggleGroupExpand = (key: string) => { setExpandedGroups((prev) => { const next = new Set(prev); if (next.has(key)) next.delete(key); else next.add(key); return next; }); }; const handlePageSizeChange = (size: number) => { setPageSize(size); setPageIndex(0); try { localStorage.setItem('bambuddy-inventory-pageSize', String(size)); } catch { /* ignore */ } }; const clearAllFilters = () => { setArchiveFilter('active'); setUsageFilter('all'); setMaterialFilter(''); setBrandFilter(''); setStockFilter('all'); setSearch(''); resetPage(); }; return (
{/* Header */}

{t('inventory.title')}

{t('inventory.noSpools').split('.')[0] ? '' : ''}

{/* Stats Bar */} {stats && !isLoading && (
{/* Total Inventory */}
{t('inventory.totalInventory')}
{formatWeight(stats.totalWeight, true)}
{stats.totalSpools} {stats.totalSpools !== 1 ? t('inventory.spools') : t('inventory.spool')}
{/* Total Consumed */}
{t('inventory.totalConsumed')}
{formatWeight(stats.totalConsumed, true)}
{t('inventory.sinceTracking')}
{/* By Material */}
{t('inventory.byMaterial')}
{topMaterials.map(([mat, data]) => ( {mat} {formatWeight(data.weight, true)} ))}
{/* In Printer */}
{t('inventory.inPrinter')}
{inPrinterCount}
{t('inventory.loadedInAms')}
{/* Low Stock */}
{t('inventory.lowStock')}
0 ? 'text-yellow-400' : 'text-white'}`}>{stats.lowStock}
{t('inventory.lowStockThreshold')}
)} {/* Toolbar: Search + View toggle */}
{ setSearch(e.target.value); resetPage(); }} placeholder={t('inventory.search')} className="w-full pl-10 pr-8 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green" /> {search && ( )}
{/* Columns button (table view only) */} {viewMode === 'table' && ( )} {/* Group similar toggle */} {/* Table / Cards toggle */}
{/* Filter chips row */}
{/* Active / Archived chips */}
{/* All / Used / New chips */}
{/* Stock filter chips */}
{/* Material dropdown chip */} {/* Brand dropdown chip */} {/* Clear filters */} {hasActiveFilters && ( <>
)} {/* Results count */} {sortedSpools.length} {sortedSpools.length !== 1 ? t('inventory.spools') : t('inventory.spool')} {groupSimilar && totalDisplayItems < sortedSpools.length && ` (${totalDisplayItems} ${t('inventory.groupedRows')})`}
{/* Content */} {isLoading ? (
) : viewMode === 'cards' ? ( /* Cards view */ pagedItems.length > 0 ? ( <>
{pagedItems.map((item) => { if (item.type === 'group') { const { key, spools: groupSpools, representative: rep } = item; const colorStyle = rep.rgba ? `#${rep.rgba.substring(0, 6)}` : '#808080'; const isExpanded = expandedGroups.has(key); return (
{/* Group header card */}
toggleGroupExpand(key)} >
{resolveSpoolColorName(rep.color_name, rep.rgba) || '-'}

{rep.material}{rep.subtype ? ` ${rep.subtype}` : ''}

{rep.brand || '-'}

{formatWeight(rep.label_weight)} {t('inventory.groupedSpools', { count: groupSpools.length })}
{/* Expanded individual spools */} {isExpanded && (
{groupSpools.map((spool) => { const remaining = Math.max(0, spool.label_weight - spool.weight_used); const pct = spool.label_weight > 0 ? (remaining / spool.label_weight) * 100 : 0; const spoolColor = spool.rgba ? `#${spool.rgba.substring(0, 6)}` : '#808080'; return ( setFormModal({ spool })} t={t} /> ); })}
)}
); } const spool = item.spool; const remaining = Math.max(0, spool.label_weight - spool.weight_used); const pct = spool.label_weight > 0 ? (remaining / spool.label_weight) * 100 : 0; const colorStyle = spool.rgba ? `#${spool.rgba.substring(0, 6)}` : '#808080'; return ( setFormModal({ spool })} t={t} /> ); })}
{/* Pagination for cards */} ) : ( setFormModal({ spool: null })} t={t} /> ) ) : ( /* Table view */ pagedItems.length > 0 ? (
{visibleColumns.map((colId) => { const sortable = !!columnSortValues[colId]; const isActive = sortState?.column === colId; return ( ); })} {pagedItems.map((item) => { if (item.type === 'group') { const { key, spools: groupSpools, representative: rep } = item; const isExpanded = expandedGroups.has(key); const remaining = Math.max(0, rep.label_weight - rep.weight_used); const pct = rep.label_weight > 0 ? (remaining / rep.label_weight) * 100 : 0; return ( toggleGroupExpand(key)} onEdit={(s) => setFormModal({ spool: s })} onArchive={(id) => setConfirmAction({ type: 'archive', spoolId: id })} onDelete={(id) => setConfirmAction({ type: 'delete', spoolId: id })} visibleColumns={visibleColumns} assignmentMap={assignmentMap} currencySymbol={currencySymbol} dateFormat={dateFormat} t={t} /> ); } const spool = item.spool; const remaining = Math.max(0, spool.label_weight - spool.weight_used); const pct = spool.label_weight > 0 ? (remaining / spool.label_weight) * 100 : 0; return ( setFormModal({ spool })} onRestore={() => restoreMutation.mutate(spool.id)} onArchive={() => setConfirmAction({ type: 'archive', spoolId: spool.id })} onDelete={() => setConfirmAction({ type: 'delete', spoolId: spool.id })} visibleColumns={visibleColumns} assignmentMap={assignmentMap} currencySymbol={currencySymbol} dateFormat={dateFormat} t={t} /> ); })}
handleSort(colId) : undefined} > {columnHeaders[colId]?.(t) ?? colId} {sortable && ( isActive ? sortState.direction === 'asc' ? : : )} {t('common.actions')}
{/* Pagination inside card footer */}
{showAll ? `${totalDisplayItems} ${totalDisplayItems !== 1 ? t('inventory.spools') : t('inventory.spool')}` : <>{t('inventory.showing')} {safePageIndex * effectivePageSize + 1} {t('inventory.to')}{' '} {Math.min((safePageIndex + 1) * effectivePageSize, totalDisplayItems)}{' '} {t('inventory.of')} {totalDisplayItems} {t('inventory.spools')} }
{t('inventory.show')} {!showAll && ( <> {t('inventory.page')} {safePageIndex + 1} {t('inventory.of')} {totalPages} )}
) : ( setFormModal({ spool: null })} t={t} /> ) )} {/* Spool Form Modal */} {formModal !== null && ( setFormModal(null)} spool={formModal.spool} currencySymbol={currencySymbol} /> )} {/* Confirm Modal (delete / archive) */} {confirmAction && ( { if (confirmAction.type === 'delete') { deleteMutation.mutate(confirmAction.spoolId); } else { archiveMutation.mutate(confirmAction.spoolId); } setConfirmAction(null); }} onCancel={() => setConfirmAction(null)} /> )} {/* Column Config Modal */} setShowColumnModal(false)} columns={columnConfig} defaultColumns={DEFAULT_COLUMNS} onSave={handleColumnConfigSave} />
); } /* Pagination bar (reused for cards view) */ function PaginationBar({ pageIndex, pageSize, totalRows, totalPages, onPageChange, onPageSizeChange, t, }: { pageIndex: number; pageSize: number; totalRows: number; totalPages: number; onPageChange: (page: number) => void; onPageSizeChange: (size: number) => void; t: (key: string) => string; }) { const isShowAll = pageSize === -1; if (totalPages <= 1 && !isShowAll) return null; const effectiveSize = isShowAll ? totalRows || 1 : pageSize; return (
{isShowAll ? `${totalRows} ${totalRows !== 1 ? t('inventory.spools') : t('inventory.spool')}` : <>{t('inventory.showing')} {pageIndex * effectiveSize + 1} {t('inventory.to')}{' '} {Math.min((pageIndex + 1) * effectiveSize, totalRows)}{' '} {t('inventory.of')} {totalRows} {t('inventory.spools')} }
{t('inventory.show')} {!isShowAll && ( <> {t('inventory.page')} {pageIndex + 1} {t('inventory.of')} {totalPages} )}
); } /* Spool card for cards view */ function SpoolCard({ spool, remaining, pct, colorStyle, onClick, t, }: { spool: InventorySpool; remaining: number; pct: number; colorStyle: string; onClick: () => void; t: (key: string, opts?: Record) => string; }) { return (
{resolveSpoolColorName(spool.color_name, spool.rgba) || '-'}

{spool.material}{spool.subtype ? ` ${spool.subtype}` : ''}

{spool.brand || '-'}

#{spool.id}
{t('inventory.remaining')} {Math.round(pct)}%
50 ? 'bg-bambu-green' : pct > 20 ? 'bg-yellow-500' : 'bg-red-500'}`} style={{ width: `${Math.min(pct, 100)}%` }} />
{Math.round(remaining)}g
{t('inventory.labelWeight')}: {formatWeight(spool.label_weight)}
{t('inventory.weightUsed')}: {spool.weight_used > 0 ? formatWeight(spool.weight_used) : '-'}
{spool.note && (
{spool.note}
)}
); } /* Single spool row for table view */ function SpoolTableRow({ spool, remaining, pct, onEdit, onRestore, onArchive, onDelete, visibleColumns, assignmentMap, currencySymbol, dateFormat, t, }: { spool: InventorySpool; remaining: number; pct: number; onEdit: () => void; onRestore: () => void; onArchive: () => void; onDelete: () => void; visibleColumns: string[]; assignmentMap: Record; currencySymbol: string; dateFormat: DateFormat; t: TFn; }) { return ( {visibleColumns.map((colId) => ( {columnCells[colId]?.({ spool, remaining, pct, assignmentMap, currencySymbol, dateFormat, t })} ))}
e.stopPropagation()}> {spool.archived_at ? ( ) : ( )}
); } /* Grouped spool rows for table view */ function SpoolTableGroup({ spools, representative, remaining, pct, isExpanded, onToggle, onEdit, onArchive, onDelete, visibleColumns, assignmentMap, currencySymbol, dateFormat, t, }: { spools: InventorySpool[]; representative: InventorySpool; remaining: number; pct: number; isExpanded: boolean; onToggle: () => void; onEdit: (spool: InventorySpool) => void; onArchive: (id: number) => void; onDelete: (id: number) => void; visibleColumns: string[]; assignmentMap: Record; currencySymbol: string; dateFormat: DateFormat; t: TFn; }) { return ( <> {/* Group header row */} {visibleColumns.map((colId, idx) => ( {idx === 0 ? (
{columnCells[colId]?.({ spool: representative, remaining, pct, assignmentMap, currencySymbol, dateFormat, t })}
) : colId === 'id' ? ( {t('inventory.groupedSpools', { count: spools.length })} ) : ( columnCells[colId]?.({ spool: representative, remaining, pct, assignmentMap, currencySymbol, dateFormat, t }) )} ))} {spools.map((s) => `#${s.id}`).join(', ')} {/* Expanded individual rows */} {isExpanded && spools.map((spool) => { const r = Math.max(0, spool.label_weight - spool.weight_used); const p = spool.label_weight > 0 ? (r / spool.label_weight) * 100 : 0; return ( onEdit(spool)} onRestore={() => {}} onArchive={() => onArchive(spool.id)} onDelete={() => onDelete(spool.id)} visibleColumns={visibleColumns} assignmentMap={assignmentMap} currencySymbol={currencySymbol} dateFormat={dateFormat} t={t} /> ); })} ); } /* Empty state matching SpoolBuddy's design */ function EmptyFilterState({ hasFilters, onAddSpool, t, }: { hasFilters: boolean; onAddSpool: () => void; t: (key: string) => string; }) { return (
{hasFilters ? ( ) : (
+
)}

{hasFilters ? t('inventory.noSpoolsMatch') : t('inventory.noSpools').split('.')[0]}

{hasFilters ? t('inventory.noSpoolsMatchDesc') : t('inventory.noSpools') }

{!hasFilters && ( )}
); }