InventoryPage.tsx 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. import { useState, useMemo, useEffect, type ReactNode } from 'react';
  2. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  3. import { useTranslation } from 'react-i18next';
  4. import {
  5. Plus, Loader2, Trash2, Archive, RotateCcw, Edit2, Package,
  6. Search, ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight,
  7. TrendingDown, Layers, Printer, AlertTriangle, X, Clock, LayoutGrid, TableProperties, Columns,
  8. ArrowUp, ArrowDown, ArrowUpDown, Group, ChevronDown, Check, RefreshCw,
  9. } from 'lucide-react';
  10. import { api, spoolbuddyApi } from '../api/client';
  11. import type { InventorySpool, SpoolAssignment, SpoolCatalogEntry } from '../api/client';
  12. import { Button } from '../components/Button';
  13. import { SpoolFormModal } from '../components/SpoolFormModal';
  14. import { ConfirmModal } from '../components/ConfirmModal';
  15. import { ColumnConfigModal, type ColumnConfig } from '../components/ColumnConfigModal';
  16. import { useToast } from '../contexts/ToastContext';
  17. import { resolveSpoolColorName } from '../utils/colors';
  18. import { getCurrencySymbol } from '../utils/currency';
  19. import { formatDateInput, parseUTCDate, type DateFormat } from '../utils/date';
  20. import { formatSlotLabel } from '../utils/amsHelpers';
  21. type ArchiveFilter = 'active' | 'archived';
  22. type UsageFilter = 'all' | 'used' | 'new' | 'lowstock';
  23. type ViewMode = 'table' | 'cards';
  24. type SortDirection = 'asc' | 'desc';
  25. type SortState = { column: string; direction: SortDirection } | null;
  26. type DisplayItem =
  27. | { type: 'single'; spool: InventorySpool }
  28. | { type: 'group'; key: string; spools: InventorySpool[]; representative: InventorySpool };
  29. function spoolGroupKey(s: InventorySpool): string {
  30. return `${s.material}|${s.subtype || ''}|${s.brand || ''}|${s.color_name || ''}|${s.rgba || ''}|${s.label_weight}`;
  31. }
  32. // Column definitions for the inventory table
  33. const COLUMN_CONFIG_KEY = 'bambuddy-inventory-columns';
  34. const DEFAULT_COLUMNS: ColumnConfig[] = [
  35. { id: 'id', label: '#', visible: true },
  36. { id: 'added_time', label: 'Added', visible: true },
  37. { id: 'encode_time', label: 'Encoded', visible: false },
  38. { id: 'last_used_time', label: 'Last Used', visible: false },
  39. { id: 'rgba', label: 'Color', visible: true },
  40. { id: 'material', label: 'Material', visible: true },
  41. { id: 'subtype', label: 'Subtype', visible: true },
  42. { id: 'color_name', label: 'Color Name', visible: false },
  43. { id: 'brand', label: 'Brand', visible: true },
  44. { id: 'slicer_filament', label: 'Slicer Filament', visible: false },
  45. { id: 'location', label: 'Location', visible: true },
  46. { id: 'label_weight', label: 'Label', visible: true },
  47. { id: 'net', label: 'Net', visible: true },
  48. { id: 'gross', label: 'Gross', visible: false },
  49. { id: 'added_full', label: 'Full', visible: false },
  50. { id: 'used', label: 'Used', visible: false },
  51. { id: 'printed_total', label: 'Printed Total', visible: false },
  52. { id: 'printed_since_weight', label: 'Printed Since Weight', visible: false },
  53. { id: 'note', label: 'Note', visible: false },
  54. { id: 'pa_k', label: 'PA(K)', visible: true },
  55. { id: 'tag_id', label: 'Tag ID', visible: false },
  56. { id: 'data_origin', label: 'Data Origin', visible: false },
  57. { id: 'tag_type', label: 'Linked Tag Type', visible: false },
  58. { id: 'stock', label: 'Stock', visible: false },
  59. { id: 'remaining', label: 'Remaining', visible: true },
  60. { id: 'spool_name', label: 'Spool', visible: false },
  61. { id: 'cost_per_kg', label: 'Cost/kg', visible: false },
  62. { id: 'weight_check', label: 'Weight Check', visible: false },
  63. ];
  64. function loadColumnConfig(): ColumnConfig[] {
  65. try {
  66. const stored = localStorage.getItem(COLUMN_CONFIG_KEY);
  67. if (stored) {
  68. const parsed = JSON.parse(stored) as ColumnConfig[];
  69. const defaultIds = new Set(DEFAULT_COLUMNS.map((c) => c.id));
  70. const storedIds = new Set(parsed.map((c) => c.id));
  71. // Keep stored columns that still exist in defaults
  72. const validStored = parsed.filter((c) => defaultIds.has(c.id));
  73. // Add any new default columns not in stored config
  74. const newColumns = DEFAULT_COLUMNS.filter((c) => !storedIds.has(c.id));
  75. return [...validStored, ...newColumns];
  76. }
  77. } catch {
  78. // Ignore errors
  79. }
  80. return DEFAULT_COLUMNS.map((c) => ({ ...c }));
  81. }
  82. function saveColumnConfig(config: ColumnConfig[]) {
  83. try {
  84. localStorage.setItem(COLUMN_CONFIG_KEY, JSON.stringify(config));
  85. } catch {
  86. // Ignore errors
  87. }
  88. }
  89. function formatWeight(g: number, useKg = false): string {
  90. if (useKg && g >= 1000) return `${(g / 1000).toFixed(1)}kg`;
  91. return `${Math.round(g)}g`;
  92. }
  93. // Material color mapping for pills
  94. const MATERIAL_COLORS: Record<string, string> = {
  95. PLA: 'bg-green-500/20 text-green-400',
  96. ABS: 'bg-red-500/20 text-red-400',
  97. PETG: 'bg-blue-500/20 text-blue-400',
  98. TPU: 'bg-purple-500/20 text-purple-400',
  99. ASA: 'bg-orange-500/20 text-orange-400',
  100. PA: 'bg-yellow-500/20 text-yellow-400',
  101. PC: 'bg-cyan-500/20 text-cyan-400',
  102. PET: 'bg-sky-500/20 text-sky-400',
  103. };
  104. type TFn = (key: string, opts?: Record<string, unknown>) => string;
  105. function formatInventoryDate(dateStr: string | null, dateFormat: DateFormat = 'system'): string {
  106. if (!dateStr) return '-';
  107. const date = parseUTCDate(dateStr);
  108. if (!date) return '-';
  109. return formatDateInput(date, dateFormat);
  110. }
  111. type CellCtx = {
  112. spool: InventorySpool;
  113. remaining: number;
  114. pct: number;
  115. assignmentMap: Record<number, SpoolAssignment>;
  116. catalogMap: Record<number, SpoolCatalogEntry>;
  117. currencySymbol: string;
  118. dateFormat: DateFormat;
  119. t: TFn;
  120. onSyncWeight?: (spool: InventorySpool) => void;
  121. };
  122. // Column header labels (25 columns — matching SpoolBuddy exactly)
  123. const columnHeaders: Record<string, (t: TFn) => string> = {
  124. id: () => '#',
  125. added_time: () => 'Added',
  126. encode_time: () => 'Encoded',
  127. last_used_time: () => 'Last Used',
  128. rgba: (t) => t('inventory.color'),
  129. material: (t) => t('inventory.material'),
  130. subtype: (t) => t('inventory.subtype'),
  131. color_name: (t) => t('inventory.colorName'),
  132. brand: (t) => t('inventory.brand'),
  133. slicer_filament: (t) => t('inventory.slicerFilament'),
  134. location: () => 'Location',
  135. label_weight: (t) => t('inventory.labelWeight'),
  136. net: (t) => t('inventory.net'),
  137. gross: () => 'Gross',
  138. added_full: () => 'Full',
  139. used: (t) => t('inventory.weightUsed'),
  140. printed_total: () => 'Printed Total',
  141. printed_since_weight: () => 'Printed Since Weight',
  142. note: (t) => t('inventory.note'),
  143. pa_k: () => 'PA(K)',
  144. tag_id: () => 'Tag ID',
  145. data_origin: () => 'Data Origin',
  146. tag_type: () => 'Linked Tag Type',
  147. stock: (t) => t('inventory.stock'),
  148. remaining: (t) => t('inventory.remaining'),
  149. spool_name: (t) => t('inventory.spoolName'),
  150. cost_per_kg: (t) => t('inventory.costPerKg'),
  151. weight_check: (t) => t('inventory.weightCheck'),
  152. };
  153. // Column cell renderers (25 columns — matching SpoolBuddy exactly)
  154. const columnCells: Record<string, (ctx: CellCtx) => ReactNode> = {
  155. id: ({ spool }) => (
  156. <span className="text-sm font-medium text-white">{spool.id}</span>
  157. ),
  158. added_time: ({ spool, dateFormat }) => (
  159. <span className="text-sm text-bambu-gray">{formatInventoryDate(spool.created_at, dateFormat)}</span>
  160. ),
  161. encode_time: ({ spool, dateFormat }) => (
  162. <span className="text-sm text-bambu-gray">{formatInventoryDate(spool.encode_time, dateFormat)}</span>
  163. ),
  164. last_used_time: ({ spool, dateFormat }) => (
  165. <span className="text-sm text-bambu-gray">{spool.last_used ? formatInventoryDate(spool.last_used, dateFormat) : 'Never'}</span>
  166. ),
  167. rgba: ({ spool }) => (
  168. <div className="flex items-center justify-center">
  169. <span
  170. className="w-5 h-5 rounded-full border border-black/20 flex-shrink-0"
  171. style={{ backgroundColor: spool.rgba ? `#${spool.rgba.substring(0, 6)}` : '#808080' }}
  172. title={spool.rgba ? `#${spool.rgba.substring(0, 6)}` : undefined}
  173. />
  174. </div>
  175. ),
  176. material: ({ spool }) => (
  177. <span className="text-sm text-white">{spool.material}</span>
  178. ),
  179. subtype: ({ spool }) => (
  180. <span className="text-sm text-bambu-gray">{spool.subtype || '-'}</span>
  181. ),
  182. color_name: ({ spool }) => (
  183. <span className="text-sm text-bambu-gray">{resolveSpoolColorName(spool.color_name, spool.rgba) || '-'}</span>
  184. ),
  185. brand: ({ spool }) => (
  186. <span className="text-sm text-bambu-gray">{spool.brand || '-'}</span>
  187. ),
  188. slicer_filament: ({ spool }) => (
  189. <span className="text-sm text-bambu-gray" title={spool.slicer_filament || undefined}>
  190. {spool.slicer_filament_name || spool.slicer_filament || '-'}
  191. </span>
  192. ),
  193. location: ({ spool, assignmentMap }) => {
  194. const assignment = assignmentMap[spool.id];
  195. if (!assignment) return <span className="text-sm text-bambu-gray">-</span>;
  196. const printerLabel = assignment.printer_name || `Printer ${assignment.printer_id}`;
  197. const isExternal = assignment.ams_id === 254 || assignment.ams_id === 255;
  198. const isHt = !isExternal && assignment.ams_id >= 128;
  199. const slotLabel = formatSlotLabel(assignment.ams_id, assignment.tray_id, isHt, isExternal);
  200. return (
  201. <span className="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-purple-500/20 text-purple-400">
  202. {printerLabel} {slotLabel}{assignment.ams_label ? ` (${assignment.ams_label})` : ''}
  203. </span>
  204. );
  205. },
  206. label_weight: ({ spool }) => (
  207. <span className="text-sm text-white">{formatWeight(spool.label_weight)}</span>
  208. ),
  209. net: ({ remaining }) => (
  210. <span className="text-sm text-white">{formatWeight(remaining)}</span>
  211. ),
  212. gross: ({ spool, remaining }) => (
  213. <span className="text-sm text-bambu-gray">{formatWeight(remaining + spool.core_weight)}</span>
  214. ),
  215. added_full: ({ spool }) => (
  216. <span className="text-sm text-bambu-gray">{spool.added_full == null ? '-' : spool.added_full ? 'Yes' : 'No'}</span>
  217. ),
  218. used: ({ spool }) => (
  219. <span className="text-sm text-bambu-gray">{spool.weight_used > 0 ? formatWeight(spool.weight_used) : '-'}</span>
  220. ),
  221. printed_total: () => (
  222. <span className="text-sm text-bambu-gray/50">-</span>
  223. ),
  224. printed_since_weight: () => (
  225. <span className="text-sm text-bambu-gray/50">-</span>
  226. ),
  227. note: ({ spool }) => (
  228. <span className="text-sm text-bambu-gray max-w-[150px] truncate block" title={spool.note || undefined}>{spool.note || '-'}</span>
  229. ),
  230. pa_k: ({ spool }) => {
  231. const count = spool.k_profiles?.length ?? 0;
  232. if (count === 0) return <span className="text-sm text-bambu-gray">-</span>;
  233. return (
  234. <span className="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-bambu-green/20 text-bambu-green">
  235. K
  236. </span>
  237. );
  238. },
  239. tag_id: ({ spool }) => {
  240. const tag = spool.tag_uid || spool.tray_uuid;
  241. if (!tag) return <span className="text-sm text-bambu-gray/50">-</span>;
  242. return (
  243. <span className="text-sm text-bambu-gray font-mono" title={tag}>
  244. {tag.length > 12 ? `${tag.slice(0, 6)}...${tag.slice(-4)}` : tag}
  245. </span>
  246. );
  247. },
  248. data_origin: ({ spool }) => (
  249. <span className="text-sm text-bambu-gray">{spool.data_origin || '-'}</span>
  250. ),
  251. tag_type: ({ spool }) => (
  252. <span className="text-sm text-bambu-gray">{spool.tag_type || '-'}</span>
  253. ),
  254. stock: ({ spool, t }) => {
  255. if (!spool.slicer_filament) {
  256. return (
  257. <span className="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-amber-500/20 text-amber-400">
  258. {t('inventory.stock')}
  259. </span>
  260. );
  261. }
  262. return <span className="text-sm text-bambu-gray">-</span>;
  263. },
  264. remaining: ({ remaining, pct }) => (
  265. <div className="flex items-center gap-2">
  266. <div className="flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden">
  267. <div
  268. className={`h-full rounded-full ${pct > 50 ? 'bg-bambu-green' : pct > 20 ? 'bg-yellow-500' : 'bg-red-500'}`}
  269. style={{ width: `${Math.min(pct, 100)}%` }}
  270. />
  271. </div>
  272. <span className="text-xs text-bambu-gray min-w-[40px] text-right">{Math.round(remaining)}g</span>
  273. </div>
  274. ),
  275. spool_name: ({ spool, catalogMap }) => {
  276. const entry = spool.core_weight_catalog_id != null ? catalogMap[spool.core_weight_catalog_id] : undefined;
  277. return <span className="text-sm text-bambu-gray">{entry?.name || '-'}</span>;
  278. },
  279. cost_per_kg: ({ spool, currencySymbol }) => (
  280. <span className="text-sm text-bambu-gray">
  281. {spool.cost_per_kg != null ? `${currencySymbol}${spool.cost_per_kg.toFixed(2)}` : '-'}
  282. </span>
  283. ),
  284. weight_check: ({ spool, onSyncWeight }) => {
  285. const scaleWeight = spool.last_scale_weight;
  286. if (scaleWeight == null) return <span className="text-sm text-bambu-gray/50" title="No scale measurement">-</span>;
  287. const coreWeight = spool.core_weight || 0;
  288. const calculatedWeight = Math.max(0, spool.label_weight - spool.weight_used) + coreWeight;
  289. // Edge case: scale < core_weight means spool is empty or not on scale — treat as match
  290. let difference: number;
  291. let isMatch: boolean;
  292. if (scaleWeight < coreWeight) {
  293. difference = scaleWeight - coreWeight;
  294. isMatch = true;
  295. } else {
  296. difference = scaleWeight - calculatedWeight;
  297. isMatch = Math.abs(difference) <= 50;
  298. }
  299. const diffStr = difference > 0 ? `+${Math.round(difference)}` : `${Math.round(difference)}`;
  300. const tooltip = isMatch
  301. ? `Scale: ${Math.round(scaleWeight)}g\nCalculated: ${Math.round(calculatedWeight)}g\nDifference: ${diffStr}g (within tolerance)`
  302. : `Scale: ${Math.round(scaleWeight)}g\nCalculated: ${Math.round(calculatedWeight)}g\nDifference: ${diffStr}g (mismatch!)`;
  303. return (
  304. <div
  305. className={`flex items-center gap-1 text-sm font-medium ${isMatch ? 'text-green-400' : 'text-yellow-400'}`}
  306. title={tooltip}
  307. >
  308. <span>{Math.round(scaleWeight)}g</span>
  309. {isMatch ? (
  310. <Check className="w-3 h-3" />
  311. ) : (
  312. <>
  313. <AlertTriangle className="w-3 h-3" />
  314. {onSyncWeight && (
  315. <button
  316. type="button"
  317. onClick={(e) => {
  318. e.stopPropagation();
  319. e.preventDefault();
  320. onSyncWeight(spool);
  321. }}
  322. className="p-1 hover:bg-bambu-green/20 rounded transition-colors text-bambu-green"
  323. title="Sync: trust scale weight and reset tracking"
  324. >
  325. <RefreshCw className="w-3.5 h-3.5" />
  326. </button>
  327. )}
  328. </>
  329. )}
  330. </div>
  331. );
  332. },
  333. };
  334. // Sort value extractors — return a comparable value for each sortable column
  335. const columnSortValues: Record<string, (spool: InventorySpool, assignmentMap: Record<number, SpoolAssignment>) => string | number> = {
  336. id: (s) => s.id,
  337. added_time: (s) => s.created_at || '',
  338. encode_time: (s) => s.encode_time || '',
  339. last_used_time: (s) => s.last_used || '',
  340. material: (s) => (s.material || '').toLowerCase(),
  341. subtype: (s) => (s.subtype || '').toLowerCase(),
  342. color_name: (s) => (s.color_name || '').toLowerCase(),
  343. brand: (s) => (s.brand || '').toLowerCase(),
  344. slicer_filament: (s) => (s.slicer_filament_name || s.slicer_filament || '').toLowerCase(),
  345. location: (s, am) => {
  346. const a = am[s.id];
  347. if (!a) return '';
  348. const isExt = a.ams_id === 254 || a.ams_id === 255;
  349. const isHt = !isExt && a.ams_id >= 128;
  350. const label = a.ams_label ? ` (${a.ams_label})` : '';
  351. return `${a.printer_name || ''} ${formatSlotLabel(a.ams_id, a.tray_id, isHt, isExt)}${label}`;
  352. },
  353. label_weight: (s) => s.label_weight,
  354. net: (s) => Math.max(0, s.label_weight - s.weight_used),
  355. gross: (s) => Math.max(0, s.label_weight - s.weight_used) + s.core_weight,
  356. used: (s) => s.weight_used,
  357. remaining: (s) => s.label_weight > 0 ? Math.max(0, s.label_weight - s.weight_used) / s.label_weight : 0,
  358. note: (s) => (s.note || '').toLowerCase(),
  359. data_origin: (s) => (s.data_origin || '').toLowerCase(),
  360. tag_type: (s) => (s.tag_type || '').toLowerCase(),
  361. stock: (s) => s.slicer_filament ? 1 : 0,
  362. spool_name: (s) => s.core_weight_catalog_id ?? 0,
  363. cost_per_kg: (s) => s.cost_per_kg ?? 0,
  364. weight_check: (s) => {
  365. if (s.last_scale_weight == null) return -1;
  366. const expectedGross = Math.max(0, s.label_weight - s.weight_used) + s.core_weight;
  367. return Math.abs(s.last_scale_weight - expectedGross);
  368. },
  369. };
  370. const SORT_STATE_KEY = 'bambuddy-inventory-sort';
  371. function loadSortState(): SortState {
  372. try {
  373. const stored = localStorage.getItem(SORT_STATE_KEY);
  374. if (stored) return JSON.parse(stored);
  375. } catch { /* ignore */ }
  376. return null;
  377. }
  378. function saveSortState(state: SortState) {
  379. try {
  380. if (state) {
  381. localStorage.setItem(SORT_STATE_KEY, JSON.stringify(state));
  382. } else {
  383. localStorage.removeItem(SORT_STATE_KEY);
  384. }
  385. } catch { /* ignore */ }
  386. }
  387. // Wrapper: when Spoolman is enabled, embed its UI; otherwise show internal inventory
  388. export default function InventoryPageRouter() {
  389. const { data: spoolmanSettings } = useQuery({
  390. queryKey: ['spoolman-settings'],
  391. queryFn: api.getSpoolmanSettings,
  392. staleTime: 5 * 60 * 1000,
  393. });
  394. if (spoolmanSettings?.spoolman_enabled === 'true' && spoolmanSettings?.spoolman_url) {
  395. return (
  396. <iframe
  397. src={`${spoolmanSettings.spoolman_url.replace(/\/+$/, '')}/spool`}
  398. className="h-full w-full border-0"
  399. title="Spoolman"
  400. sandbox="allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox"
  401. />
  402. );
  403. }
  404. return <InventoryPage />;
  405. }
  406. function InventoryPage() {
  407. const { t } = useTranslation();
  408. const queryClient = useQueryClient();
  409. const { showToast } = useToast();
  410. const [formModal, setFormModal] = useState<{ spool?: InventorySpool | null } | null>(null);
  411. const [confirmAction, setConfirmAction] = useState<{ type: 'delete' | 'archive'; spoolId: number } | null>(null);
  412. // Filter state
  413. const [archiveFilter, setArchiveFilter] = useState<ArchiveFilter>('active');
  414. const [usageFilter, setUsageFilter] = useState<UsageFilter>('all');
  415. const [materialFilter, setMaterialFilter] = useState('');
  416. const [brandFilter, setBrandFilter] = useState('');
  417. const [spoolFilter, setSpoolFilter] = useState('');
  418. const [stockFilter, setStockFilter] = useState<'all' | 'stock' | 'configured'>('all');
  419. const [search, setSearch] = useState('');
  420. const [viewMode, setViewMode] = useState<ViewMode>('table');
  421. const [sortState, setSortState] = useState<SortState>(loadSortState);
  422. const [columnConfig, setColumnConfig] = useState<ColumnConfig[]>(loadColumnConfig);
  423. const [showColumnModal, setShowColumnModal] = useState(false);
  424. const [groupSimilar, setGroupSimilar] = useState(() => {
  425. try {
  426. return localStorage.getItem('bambuddy-inventory-group') === 'true';
  427. } catch { return false; }
  428. });
  429. const [expandedGroups, setExpandedGroups] = useState<Set<string>>(new Set());
  430. // Pagination state (pageSize persisted to localStorage)
  431. const [pageIndex, setPageIndex] = useState(0);
  432. const [pageSize, setPageSize] = useState(() => {
  433. try {
  434. const stored = localStorage.getItem('bambuddy-inventory-pageSize');
  435. if (stored) {
  436. const n = Number(stored);
  437. if ([15, 30, 50, 100, -1].includes(n)) return n;
  438. }
  439. } catch { /* ignore */ }
  440. return 15;
  441. });
  442. const { data: settings } = useQuery({
  443. queryKey: ['settings'],
  444. queryFn: api.getSettings,
  445. });
  446. const dateFormat: DateFormat = settings?.date_format || 'system';
  447. const { data: spools, isLoading } = useQuery({
  448. queryKey: ['inventory-spools'],
  449. queryFn: () => api.getSpools(true), // Always fetch all, filter client-side
  450. refetchInterval: 30000,
  451. });
  452. const { data: assignments } = useQuery({
  453. queryKey: ['spool-assignments'],
  454. queryFn: () => api.getAssignments(),
  455. refetchInterval: 30000,
  456. });
  457. const { data: catalogEntries } = useQuery({
  458. queryKey: ['spool-catalog'],
  459. queryFn: () => api.getSpoolCatalog(),
  460. });
  461. const deleteMutation = useMutation({
  462. mutationFn: (id: number) => api.deleteSpool(id),
  463. onSuccess: () => {
  464. queryClient.invalidateQueries({ queryKey: ['inventory-spools'] });
  465. showToast(t('inventory.spoolDeleted'), 'success');
  466. },
  467. });
  468. const archiveMutation = useMutation({
  469. mutationFn: (id: number) => api.archiveSpool(id),
  470. onSuccess: () => {
  471. queryClient.invalidateQueries({ queryKey: ['inventory-spools'] });
  472. showToast(t('inventory.spoolArchived'), 'success');
  473. },
  474. });
  475. const restoreMutation = useMutation({
  476. mutationFn: (id: number) => api.restoreSpool(id),
  477. onSuccess: () => {
  478. queryClient.invalidateQueries({ queryKey: ['inventory-spools'] });
  479. showToast(t('inventory.spoolRestored'), 'success');
  480. },
  481. });
  482. const handleSyncWeight = async (spool: InventorySpool) => {
  483. if (spool.last_scale_weight == null) return;
  484. try {
  485. await spoolbuddyApi.updateSpoolWeight(spool.id, spool.last_scale_weight);
  486. queryClient.invalidateQueries({ queryKey: ['inventory-spools'] });
  487. const spoolName = [spool.brand, spool.material, spool.color_name].filter(Boolean).join(' ');
  488. showToast(`Synced "${spoolName}" to scale weight`, 'success');
  489. } catch {
  490. showToast('Failed to sync weight', 'error');
  491. }
  492. };
  493. // Low stock threshold from backend settings
  494. const lowStockThreshold = settings?.low_stock_threshold ?? 20;
  495. const [showThresholdInput, setShowThresholdInput] = useState(false);
  496. const [thresholdInput, setThresholdInput] = useState(lowStockThreshold.toString());
  497. // Sync thresholdInput when lowStockThreshold changes and input is not shown
  498. useEffect(() => {
  499. if (!showThresholdInput) {
  500. setThresholdInput(lowStockThreshold.toString());
  501. }
  502. }, [lowStockThreshold, showThresholdInput]);
  503. const updateThresholdMutation = useMutation({
  504. mutationFn: (threshold: number) => api.updateSettings({ low_stock_threshold: threshold }),
  505. onSuccess: () => {
  506. queryClient.invalidateQueries({ queryKey: ['settings'] });
  507. showToast(t('common.saved'), 'success');
  508. setShowThresholdInput(false);
  509. },
  510. onError: () => {
  511. showToast(t('inventory.lowStockThresholdError'), 'error');
  512. },
  513. });
  514. // Stats calculation (active spools only)
  515. const stats = useMemo(() => {
  516. if (!spools) return null;
  517. let totalWeight = 0;
  518. let totalConsumed = 0;
  519. let lowStock = 0;
  520. let activeCount = 0;
  521. const byMaterial: Record<string, { count: number; weight: number }> = {};
  522. for (const s of spools) {
  523. if (s.archived_at) continue;
  524. activeCount++;
  525. const remaining = Math.max(0, s.label_weight - s.weight_used);
  526. totalWeight += remaining;
  527. totalConsumed += s.weight_used;
  528. const pct = s.label_weight > 0 ? (remaining / s.label_weight) * 100 : 0;
  529. if (pct < lowStockThreshold) lowStock++;
  530. const mat = s.material || 'Unknown';
  531. if (!byMaterial[mat]) byMaterial[mat] = { count: 0, weight: 0 };
  532. byMaterial[mat].count++;
  533. byMaterial[mat].weight += remaining;
  534. }
  535. return { totalWeight, totalConsumed, lowStock, byMaterial, totalSpools: activeCount };
  536. }, [spools, lowStockThreshold]);
  537. const inPrinterCount = assignments?.length ?? 0;
  538. const currencySymbol = getCurrencySymbol(settings?.currency || 'USD');
  539. // Map spool_id -> assignment for location column
  540. const assignmentMap = useMemo(() => {
  541. const map: Record<number, SpoolAssignment> = {};
  542. for (const a of assignments || []) {
  543. map[a.spool_id] = a;
  544. }
  545. return map;
  546. }, [assignments]);
  547. // Map catalog_id -> catalog entry for spool name column
  548. const catalogMap = useMemo(() => {
  549. const map: Record<number, SpoolCatalogEntry> = {};
  550. for (const e of catalogEntries || []) {
  551. map[e.id] = e;
  552. }
  553. return map;
  554. }, [catalogEntries]);
  555. // Top materials by weight for stat card pills
  556. const topMaterials = useMemo(() => {
  557. if (!stats) return [];
  558. return Object.entries(stats.byMaterial)
  559. .sort((a, b) => b[1].weight - a[1].weight)
  560. .slice(0, 4);
  561. }, [stats]);
  562. // Filtering pipeline
  563. const filteredSpools = useMemo(() => {
  564. let filtered = spools || [];
  565. // Archive filter
  566. if (archiveFilter === 'active') {
  567. filtered = filtered.filter((s) => !s.archived_at);
  568. } else {
  569. filtered = filtered.filter((s) => !!s.archived_at);
  570. }
  571. // Usage filter
  572. if (usageFilter === 'used') {
  573. filtered = filtered.filter((s) => s.weight_used > 0);
  574. } else if (usageFilter === 'new') {
  575. filtered = filtered.filter((s) => s.weight_used === 0);
  576. } else if (usageFilter === 'lowstock') {
  577. filtered = filtered.filter((s) => {
  578. const remaining = Math.max(0, s.label_weight - s.weight_used);
  579. const pct = s.label_weight > 0 ? (remaining / s.label_weight) * 100 : 0;
  580. return pct < lowStockThreshold;
  581. });
  582. }
  583. // Material dropdown
  584. if (materialFilter) {
  585. filtered = filtered.filter((s) => s.material === materialFilter);
  586. }
  587. // Brand dropdown
  588. if (brandFilter) {
  589. filtered = filtered.filter((s) => s.brand === brandFilter);
  590. }
  591. // Spool name dropdown
  592. if (spoolFilter) {
  593. const catalogId = Number(spoolFilter);
  594. filtered = filtered.filter((s) => s.core_weight_catalog_id === catalogId);
  595. }
  596. // Stock filter
  597. if (stockFilter === 'stock') {
  598. filtered = filtered.filter((s) => !s.slicer_filament);
  599. } else if (stockFilter === 'configured') {
  600. filtered = filtered.filter((s) => !!s.slicer_filament);
  601. }
  602. // Global search
  603. if (search) {
  604. const q = search.toLowerCase();
  605. filtered = filtered.filter((s) =>
  606. s.brand?.toLowerCase().includes(q) ||
  607. s.material.toLowerCase().includes(q) ||
  608. s.color_name?.toLowerCase().includes(q) ||
  609. s.subtype?.toLowerCase().includes(q) ||
  610. s.note?.toLowerCase().includes(q) ||
  611. s.slicer_filament_name?.toLowerCase().includes(q)
  612. );
  613. }
  614. return filtered;
  615. }, [spools, archiveFilter, usageFilter, materialFilter, brandFilter, spoolFilter, stockFilter, search, lowStockThreshold]);
  616. // Reset page on filter changes
  617. const resetPage = () => setPageIndex(0);
  618. // Unique values for filter dropdowns
  619. const uniqueMaterials = [...new Set(spools?.map((s) => s.material) || [])].sort();
  620. const uniqueBrands = [...new Set(spools?.map((s) => s.brand).filter(Boolean) || [])].sort() as string[];
  621. const uniqueSpoolCatalogIds = [...new Set(spools?.map((s) => s.core_weight_catalog_id).filter((id): id is number => id != null) || [])].sort((a, b) => {
  622. const nameA = (catalogMap[a]?.name || '').toLowerCase();
  623. const nameB = (catalogMap[b]?.name || '').toLowerCase();
  624. return nameA.localeCompare(nameB);
  625. });
  626. // Check if any filters are non-default
  627. const hasActiveFilters = archiveFilter !== 'active' || usageFilter !== 'all' || !!materialFilter || !!brandFilter || !!spoolFilter || stockFilter !== 'all' || !!search;
  628. const handleColumnConfigSave = (config: ColumnConfig[]) => {
  629. setColumnConfig(config);
  630. saveColumnConfig(config);
  631. };
  632. // Visible column IDs in order
  633. const visibleColumns = useMemo(
  634. () => columnConfig.filter((c) => c.visible).map((c) => c.id),
  635. [columnConfig]
  636. );
  637. const handleSort = (colId: string) => {
  638. if (!columnSortValues[colId]) return; // Not sortable
  639. setSortState((prev) => {
  640. let next: SortState;
  641. if (prev?.column === colId) {
  642. // Toggle direction, or clear on third click
  643. next = prev.direction === 'asc' ? { column: colId, direction: 'desc' } : null;
  644. } else {
  645. next = { column: colId, direction: 'asc' };
  646. }
  647. saveSortState(next);
  648. return next;
  649. });
  650. resetPage();
  651. };
  652. // Sort filtered spools
  653. const sortedSpools = useMemo(() => {
  654. if (!sortState) return filteredSpools;
  655. const extractor = columnSortValues[sortState.column];
  656. if (!extractor) return filteredSpools;
  657. const sorted = [...filteredSpools].sort((a, b) => {
  658. const va = extractor(a, assignmentMap);
  659. const vb = extractor(b, assignmentMap);
  660. if (va < vb) return sortState.direction === 'asc' ? -1 : 1;
  661. if (va > vb) return sortState.direction === 'asc' ? 1 : -1;
  662. return 0;
  663. });
  664. return sorted;
  665. }, [filteredSpools, sortState, assignmentMap]);
  666. // Group similar spools when toggle is active
  667. const displayItems = useMemo((): DisplayItem[] => {
  668. if (!groupSimilar) return sortedSpools.map((s) => ({ type: 'single' as const, spool: s }));
  669. const groups = new Map<string, InventorySpool[]>();
  670. for (const spool of sortedSpools) {
  671. // Only group unused & unassigned spools
  672. if (spool.weight_used > 0 || assignmentMap[spool.id]) {
  673. // Will be added as singles in the walk below
  674. } else {
  675. const key = spoolGroupKey(spool);
  676. const arr = groups.get(key);
  677. if (arr) arr.push(spool);
  678. else groups.set(key, [spool]);
  679. }
  680. }
  681. const items: DisplayItem[] = [];
  682. const processedKeys = new Set<string>();
  683. // Walk sortedSpools order so groups appear at the position of their first member
  684. for (const spool of sortedSpools) {
  685. if (spool.weight_used > 0 || assignmentMap[spool.id]) {
  686. items.push({ type: 'single', spool });
  687. continue;
  688. }
  689. const key = spoolGroupKey(spool);
  690. if (processedKeys.has(key)) continue;
  691. processedKeys.add(key);
  692. const members = groups.get(key)!;
  693. if (members.length === 1) {
  694. items.push({ type: 'single', spool: members[0] });
  695. } else {
  696. items.push({ type: 'group', key, spools: members, representative: members[0] });
  697. }
  698. }
  699. return items;
  700. }, [sortedSpools, groupSimilar, assignmentMap]);
  701. // Pagination (after sorting) — pageSize -1 means "All"
  702. const showAll = pageSize === -1;
  703. const totalDisplayItems = displayItems.length;
  704. const effectivePageSize = showAll ? totalDisplayItems || 1 : pageSize;
  705. const totalPages = Math.max(1, Math.ceil(totalDisplayItems / effectivePageSize));
  706. const safePageIndex = showAll ? 0 : Math.min(pageIndex, totalPages - 1);
  707. const pagedItems = showAll
  708. ? displayItems
  709. : displayItems.slice(safePageIndex * effectivePageSize, (safePageIndex + 1) * effectivePageSize);
  710. const toggleGroupSimilar = () => {
  711. const next = !groupSimilar;
  712. setGroupSimilar(next);
  713. setExpandedGroups(new Set());
  714. resetPage();
  715. try { localStorage.setItem('bambuddy-inventory-group', String(next)); } catch { /* ignore */ }
  716. };
  717. const toggleGroupExpand = (key: string) => {
  718. setExpandedGroups((prev) => {
  719. const next = new Set(prev);
  720. if (next.has(key)) next.delete(key);
  721. else next.add(key);
  722. return next;
  723. });
  724. };
  725. const handlePageSizeChange = (size: number) => {
  726. setPageSize(size);
  727. setPageIndex(0);
  728. try { localStorage.setItem('bambuddy-inventory-pageSize', String(size)); } catch { /* ignore */ }
  729. };
  730. const clearAllFilters = () => {
  731. setArchiveFilter('active');
  732. setUsageFilter('all');
  733. setMaterialFilter('');
  734. setBrandFilter('');
  735. setSpoolFilter('');
  736. setStockFilter('all');
  737. setSearch('');
  738. resetPage();
  739. };
  740. return (
  741. <div className="p-4 md:p-6 space-y-6">
  742. {/* Header */}
  743. <div className="flex items-center justify-between">
  744. <div>
  745. <div className="flex items-center gap-3">
  746. <Package className="w-6 h-6 text-bambu-green" />
  747. <h1 className="text-2xl font-bold text-white">{t('inventory.title')}</h1>
  748. </div>
  749. <p className="text-sm text-bambu-gray mt-1 ml-9">{t('inventory.noSpools').split('.')[0] ? '' : ''}</p>
  750. </div>
  751. <Button onClick={() => setFormModal({ spool: null })}>
  752. <Plus className="w-4 h-4" />
  753. {t('inventory.addSpool')}
  754. </Button>
  755. </div>
  756. {/* Stats Bar */}
  757. {stats && !isLoading && (
  758. <div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-3">
  759. {/* Total Inventory */}
  760. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  761. <div className="flex items-center gap-2 mb-1">
  762. <Package className="w-4 h-4 text-bambu-green" />
  763. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.totalInventory')}</span>
  764. </div>
  765. <div className="text-xl font-bold text-white">{formatWeight(stats.totalWeight, true)}</div>
  766. <div className="text-xs text-bambu-gray mt-1">{stats.totalSpools} {stats.totalSpools !== 1 ? t('inventory.spools') : t('inventory.spool')}</div>
  767. </div>
  768. {/* Total Consumed */}
  769. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  770. <div className="flex items-center gap-2 mb-1">
  771. <TrendingDown className="w-4 h-4 text-blue-400" />
  772. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.totalConsumed')}</span>
  773. </div>
  774. <div className="text-xl font-bold text-white">{formatWeight(stats.totalConsumed, true)}</div>
  775. <div className="text-xs text-bambu-gray mt-1">{t('inventory.sinceTracking')}</div>
  776. </div>
  777. {/* By Material */}
  778. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  779. <div className="flex items-center gap-2 mb-1">
  780. <Layers className="w-4 h-4 text-green-400" />
  781. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.byMaterial')}</span>
  782. </div>
  783. <div className="flex flex-wrap gap-1.5 mt-1">
  784. {topMaterials.map(([mat, data]) => (
  785. <span
  786. key={mat}
  787. className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium ${MATERIAL_COLORS[mat] || 'bg-bambu-dark-tertiary text-bambu-gray'}`}
  788. >
  789. {mat} <span className="opacity-70">{formatWeight(data.weight, true)}</span>
  790. </span>
  791. ))}
  792. </div>
  793. </div>
  794. {/* In Printer */}
  795. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  796. <div className="flex items-center gap-2 mb-1">
  797. <Printer className="w-4 h-4 text-purple-400" />
  798. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.inPrinter')}</span>
  799. </div>
  800. <div className="text-xl font-bold text-white">{inPrinterCount}</div>
  801. <div className="text-xs text-bambu-gray mt-1">{t('inventory.loadedInAms')}</div>
  802. </div>
  803. {/* Low Stock */}
  804. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  805. <div className="flex items-center gap-2 mb-1">
  806. <AlertTriangle className="w-4 h-4 text-yellow-400" />
  807. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.lowStock')}</span>
  808. </div>
  809. <div className={`text-xl font-bold ${stats.lowStock > 0 ? 'text-yellow-400' : 'text-white'}`}>{stats.lowStock}</div>
  810. <div className="text-xs text-bambu-gray mt-1 flex items-center gap-2">
  811. {showThresholdInput ? (
  812. <form
  813. onSubmit={e => {
  814. e.preventDefault();
  815. const val = parseFloat(thresholdInput);
  816. if (!isNaN(val) && val >= 0.1 && val <= 99.9) {
  817. updateThresholdMutation.mutate(val);
  818. } else {
  819. showToast(t('inventory.lowStockThresholdError'), 'error');
  820. }
  821. }}
  822. className="flex items-center gap-2"
  823. >
  824. <span className="text-xs text-bambu-gray">{'<'}</span>
  825. <input
  826. type="text"
  827. inputMode="decimal"
  828. pattern="^\d{0,2}(\.\d?)?$"
  829. maxLength={4}
  830. value={thresholdInput}
  831. onChange={e => {
  832. // Only allow up to 2 digits before decimal and 1 after
  833. const val = e.target.value.replace(/[^\d.]/g, '');
  834. if (/^\d{0,2}(\.\d?)?$/.test(val)) {
  835. setThresholdInput(val);
  836. }
  837. }}
  838. className="px-1.5 py-1 rounded border border-bambu-dark-tertiary text-xs text-white bg-bambu-dark-secondary focus:outline-none focus:border-bambu-green w-14 text-center"
  839. onWheel={e => e.currentTarget.blur()}
  840. disabled={updateThresholdMutation.isPending}
  841. />
  842. <span className="text-xs text-bambu-gray">%</span>
  843. <Button type="submit" size="sm" disabled={updateThresholdMutation.isPending}>{t('common.save')}</Button>
  844. <Button type="button" size="sm" variant="ghost" onClick={() => setShowThresholdInput(false)} disabled={updateThresholdMutation.isPending}>{t('common.cancel')}</Button>
  845. </form>
  846. ) : (
  847. <>
  848. <span className="text-bambu-gray">{'< '}{lowStockThreshold}%</span>
  849. <button
  850. className="p-1.5 text-bambu-gray hover:text-white rounded transition-colors"
  851. title={t('common.edit')}
  852. onClick={() => {
  853. setThresholdInput(lowStockThreshold.toString());
  854. setShowThresholdInput(true);
  855. }}
  856. >
  857. <Edit2 className="w-4 h-4" />
  858. </button>
  859. </>
  860. )}
  861. </div>
  862. </div>
  863. </div>
  864. )}
  865. {/* Toolbar: Search + View toggle */}
  866. <div className="flex flex-col sm:flex-row gap-3 items-start sm:items-center justify-between">
  867. <div className="relative flex-1 max-w-md">
  868. <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50" />
  869. <input
  870. type="text"
  871. value={search}
  872. onChange={(e) => { setSearch(e.target.value); resetPage(); }}
  873. placeholder={t('inventory.search')}
  874. 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"
  875. />
  876. {search && (
  877. <button
  878. onClick={() => { setSearch(''); resetPage(); }}
  879. className="absolute right-3 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white"
  880. >
  881. <X className="w-4 h-4" />
  882. </button>
  883. )}
  884. </div>
  885. <div className="flex items-center gap-2">
  886. {/* Columns button (table view only) */}
  887. {viewMode === 'table' && (
  888. <button
  889. onClick={() => setShowColumnModal(true)}
  890. className="flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium text-bambu-gray border border-bambu-dark-tertiary rounded-lg hover:bg-bambu-dark-tertiary transition-colors"
  891. title={t('inventory.configureColumns')}
  892. >
  893. <Columns className="w-4 h-4" />
  894. <span className="hidden sm:inline">{t('inventory.columns')}</span>
  895. </button>
  896. )}
  897. {/* Group similar toggle */}
  898. <button
  899. onClick={toggleGroupSimilar}
  900. className={`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium border rounded-lg transition-colors ${
  901. groupSimilar
  902. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  903. : 'text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  904. }`}
  905. title={t('inventory.groupSimilar')}
  906. >
  907. <Group className="w-4 h-4" />
  908. <span className="hidden sm:inline">{t('inventory.groupSimilar')}</span>
  909. </button>
  910. {/* Table / Cards toggle */}
  911. <div className="flex bg-bambu-dark-primary border border-bambu-dark-tertiary rounded-lg overflow-hidden">
  912. <button
  913. onClick={() => setViewMode('table')}
  914. className={`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${
  915. viewMode === 'table'
  916. ? 'bg-bambu-green text-white'
  917. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  918. }`}
  919. >
  920. <TableProperties className="w-4 h-4" />
  921. <span className="hidden sm:inline">{t('inventory.table')}</span>
  922. </button>
  923. <button
  924. onClick={() => setViewMode('cards')}
  925. className={`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${
  926. viewMode === 'cards'
  927. ? 'bg-bambu-green text-white'
  928. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  929. }`}
  930. >
  931. <LayoutGrid className="w-4 h-4" />
  932. <span className="hidden sm:inline">{t('inventory.cards')}</span>
  933. </button>
  934. </div>
  935. </div>
  936. </div>
  937. {/* Filter chips row */}
  938. <div className="flex flex-wrap items-center gap-2">
  939. {/* Active / Archived chips */}
  940. <div className="flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden">
  941. <button
  942. onClick={() => { setArchiveFilter('active'); resetPage(); }}
  943. className={`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${
  944. archiveFilter === 'active'
  945. ? 'bg-bambu-green/20 text-bambu-green'
  946. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  947. }`}
  948. >
  949. <Package className="w-3.5 h-3.5" />
  950. {t('inventory.active')}
  951. </button>
  952. <button
  953. onClick={() => { setArchiveFilter('archived'); resetPage(); }}
  954. className={`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${
  955. archiveFilter === 'archived'
  956. ? 'bg-bambu-green/20 text-bambu-green'
  957. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  958. }`}
  959. >
  960. <Archive className="w-3.5 h-3.5" />
  961. {t('inventory.archived')}
  962. </button>
  963. </div>
  964. <div className="w-px h-5 bg-bambu-dark-tertiary" />
  965. {/* All / Used / New chips */}
  966. <div className="flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden">
  967. <button
  968. onClick={() => { setUsageFilter('all'); resetPage(); }}
  969. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  970. usageFilter === 'all'
  971. ? 'bg-bambu-green/20 text-bambu-green'
  972. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  973. }`}
  974. >
  975. {t('inventory.all')}
  976. </button>
  977. <button
  978. onClick={() => { setUsageFilter('used'); resetPage(); }}
  979. className={`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${
  980. usageFilter === 'used'
  981. ? 'bg-bambu-green/20 text-bambu-green'
  982. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  983. }`}
  984. >
  985. <Clock className="w-3.5 h-3.5" />
  986. {t('inventory.used')}
  987. </button>
  988. <button
  989. onClick={() => { setUsageFilter('new'); resetPage(); }}
  990. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  991. usageFilter === 'new'
  992. ? 'bg-bambu-green/20 text-bambu-green'
  993. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  994. }`}
  995. >
  996. {t('inventory.new')}
  997. </button>
  998. <button
  999. onClick={() => { setUsageFilter('lowstock'); resetPage(); }}
  1000. className={`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${
  1001. usageFilter === 'lowstock'
  1002. ? 'bg-yellow-500/20 text-yellow-400'
  1003. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1004. }`}
  1005. >
  1006. <AlertTriangle className="w-3.5 h-3.5" />
  1007. {t('inventory.lowStock')}
  1008. </button>
  1009. </div>
  1010. {/* Stock filter chips */}
  1011. <div className="flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden">
  1012. <button
  1013. onClick={() => { setStockFilter('all'); resetPage(); }}
  1014. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  1015. stockFilter === 'all'
  1016. ? 'bg-bambu-green/20 text-bambu-green'
  1017. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1018. }`}
  1019. >
  1020. {t('inventory.all')}
  1021. </button>
  1022. <button
  1023. onClick={() => { setStockFilter('stock'); resetPage(); }}
  1024. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  1025. stockFilter === 'stock'
  1026. ? 'bg-amber-500/20 text-amber-400'
  1027. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1028. }`}
  1029. >
  1030. {t('inventory.stock')}
  1031. </button>
  1032. <button
  1033. onClick={() => { setStockFilter('configured'); resetPage(); }}
  1034. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  1035. stockFilter === 'configured'
  1036. ? 'bg-bambu-green/20 text-bambu-green'
  1037. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1038. }`}
  1039. >
  1040. {t('inventory.configured')}
  1041. </button>
  1042. </div>
  1043. <div className="w-px h-5 bg-bambu-dark-tertiary" />
  1044. {/* Material dropdown chip */}
  1045. <select
  1046. value={materialFilter}
  1047. onChange={(e) => { setMaterialFilter(e.target.value); resetPage(); }}
  1048. className={`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${
  1049. materialFilter
  1050. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  1051. : 'bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  1052. }`}
  1053. >
  1054. <option value="">{t('inventory.material')}</option>
  1055. {uniqueMaterials.map((m) => (
  1056. <option key={m} value={m}>{m}</option>
  1057. ))}
  1058. </select>
  1059. {/* Brand dropdown chip */}
  1060. <select
  1061. value={brandFilter}
  1062. onChange={(e) => { setBrandFilter(e.target.value); resetPage(); }}
  1063. className={`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${
  1064. brandFilter
  1065. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  1066. : 'bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  1067. }`}
  1068. >
  1069. <option value="">{t('inventory.brand')}</option>
  1070. {uniqueBrands.map((b) => (
  1071. <option key={b} value={b}>{b}</option>
  1072. ))}
  1073. </select>
  1074. {/* Spool name dropdown chip */}
  1075. {uniqueSpoolCatalogIds.length > 0 && (
  1076. <select
  1077. value={spoolFilter}
  1078. onChange={(e) => { setSpoolFilter(e.target.value); resetPage(); }}
  1079. className={`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${
  1080. spoolFilter
  1081. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  1082. : 'bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  1083. }`}
  1084. >
  1085. <option value="">{t('inventory.spoolName')}</option>
  1086. {uniqueSpoolCatalogIds.map((id) => (
  1087. <option key={id} value={id}>{catalogMap[id]?.name || `#${id}`}</option>
  1088. ))}
  1089. </select>
  1090. )}
  1091. {/* Clear filters */}
  1092. {hasActiveFilters && (
  1093. <>
  1094. <div className="w-px h-5 bg-bambu-dark-tertiary" />
  1095. <button
  1096. onClick={clearAllFilters}
  1097. className="flex items-center gap-1 text-xs text-bambu-gray hover:text-bambu-green transition-colors"
  1098. >
  1099. <X className="w-3.5 h-3.5" />
  1100. {t('inventory.clearFilters')}
  1101. </button>
  1102. </>
  1103. )}
  1104. {/* Results count */}
  1105. <span className="ml-auto text-xs text-bambu-gray">
  1106. {sortedSpools.length} {sortedSpools.length !== 1 ? t('inventory.spools') : t('inventory.spool')}
  1107. {groupSimilar && totalDisplayItems < sortedSpools.length && ` (${totalDisplayItems} ${t('inventory.groupedRows')})`}
  1108. </span>
  1109. </div>
  1110. {/* Content */}
  1111. {isLoading ? (
  1112. <div className="flex justify-center py-16">
  1113. <Loader2 className="w-8 h-8 text-bambu-green animate-spin" />
  1114. </div>
  1115. ) : viewMode === 'cards' ? (
  1116. /* Cards view */
  1117. pagedItems.length > 0 ? (
  1118. <>
  1119. <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
  1120. {pagedItems.map((item) => {
  1121. if (item.type === 'group') {
  1122. const { key, spools: groupSpools, representative: rep } = item;
  1123. const colorStyle = rep.rgba ? `#${rep.rgba.substring(0, 6)}` : '#808080';
  1124. const isExpanded = expandedGroups.has(key);
  1125. return (
  1126. <div key={`group-${key}`} className="col-span-full">
  1127. {/* Group header card */}
  1128. <div
  1129. className="bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-green/30 hover:border-bambu-green transition-colors cursor-pointer"
  1130. onClick={() => toggleGroupExpand(key)}
  1131. >
  1132. <div className="h-10 flex items-center px-4 gap-3" style={{ backgroundColor: colorStyle }}>
  1133. <span className="bg-white/90 text-gray-800 px-3 py-0.5 rounded-full text-sm font-medium">
  1134. {resolveSpoolColorName(rep.color_name, rep.rgba) || '-'}
  1135. </span>
  1136. </div>
  1137. <div className="px-4 py-3 flex items-center justify-between">
  1138. <div className="flex items-center gap-3">
  1139. <ChevronDown className={`w-4 h-4 text-bambu-gray transition-transform ${isExpanded ? '' : '-rotate-90'}`} />
  1140. <div>
  1141. <h3 className="font-semibold text-white">{rep.material}{rep.subtype ? ` ${rep.subtype}` : ''}</h3>
  1142. <p className="text-sm text-bambu-gray">{rep.brand || '-'}</p>
  1143. </div>
  1144. </div>
  1145. <div className="flex items-center gap-2">
  1146. <span className="text-sm text-bambu-gray">{formatWeight(rep.label_weight)}</span>
  1147. <span className="text-xs font-medium bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full">
  1148. {t('inventory.groupedSpools', { count: groupSpools.length })}
  1149. </span>
  1150. </div>
  1151. </div>
  1152. </div>
  1153. {/* Expanded individual spools */}
  1154. {isExpanded && (
  1155. <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4 mt-2 ml-4">
  1156. {groupSpools.map((spool) => {
  1157. const remaining = Math.max(0, spool.label_weight - spool.weight_used);
  1158. const pct = spool.label_weight > 0 ? (remaining / spool.label_weight) * 100 : 0;
  1159. const spoolColor = spool.rgba ? `#${spool.rgba.substring(0, 6)}` : '#808080';
  1160. return (
  1161. <SpoolCard
  1162. key={spool.id}
  1163. spool={spool}
  1164. remaining={remaining}
  1165. pct={pct}
  1166. colorStyle={spoolColor}
  1167. onClick={() => setFormModal({ spool })}
  1168. t={t}
  1169. />
  1170. );
  1171. })}
  1172. </div>
  1173. )}
  1174. </div>
  1175. );
  1176. }
  1177. const spool = item.spool;
  1178. const remaining = Math.max(0, spool.label_weight - spool.weight_used);
  1179. const pct = spool.label_weight > 0 ? (remaining / spool.label_weight) * 100 : 0;
  1180. const colorStyle = spool.rgba ? `#${spool.rgba.substring(0, 6)}` : '#808080';
  1181. return (
  1182. <SpoolCard
  1183. key={spool.id}
  1184. spool={spool}
  1185. remaining={remaining}
  1186. pct={pct}
  1187. colorStyle={colorStyle}
  1188. onClick={() => setFormModal({ spool })}
  1189. t={t}
  1190. />
  1191. );
  1192. })}
  1193. </div>
  1194. {/* Pagination for cards */}
  1195. <PaginationBar
  1196. pageIndex={safePageIndex}
  1197. pageSize={pageSize}
  1198. totalRows={totalDisplayItems}
  1199. totalPages={totalPages}
  1200. onPageChange={setPageIndex}
  1201. onPageSizeChange={handlePageSizeChange}
  1202. t={t}
  1203. />
  1204. </>
  1205. ) : (
  1206. <EmptyFilterState
  1207. hasFilters={hasActiveFilters}
  1208. onAddSpool={() => setFormModal({ spool: null })}
  1209. t={t}
  1210. />
  1211. )
  1212. ) : (
  1213. /* Table view */
  1214. pagedItems.length > 0 ? (
  1215. <div className="bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary">
  1216. <div className="overflow-x-auto">
  1217. <table className="w-full">
  1218. <thead>
  1219. <tr className="border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30">
  1220. {visibleColumns.map((colId) => {
  1221. const sortable = !!columnSortValues[colId];
  1222. const isActive = sortState?.column === colId;
  1223. return (
  1224. <th
  1225. key={colId}
  1226. className={`text-left py-3 px-4 text-xs font-medium uppercase tracking-wide select-none ${colId === 'remaining' ? 'min-w-[150px]' : ''} ${
  1227. sortable ? 'cursor-pointer hover:text-bambu-green transition-colors' : ''
  1228. } ${isActive ? 'text-bambu-green' : 'text-bambu-gray'}`}
  1229. onClick={sortable ? () => handleSort(colId) : undefined}
  1230. >
  1231. <span className="inline-flex items-center gap-1">
  1232. {columnHeaders[colId]?.(t) ?? colId}
  1233. {sortable && (
  1234. isActive
  1235. ? sortState.direction === 'asc'
  1236. ? <ArrowUp className="w-3 h-3" />
  1237. : <ArrowDown className="w-3 h-3" />
  1238. : <ArrowUpDown className="w-3 h-3 opacity-30" />
  1239. )}
  1240. </span>
  1241. </th>
  1242. );
  1243. })}
  1244. <th className="text-right py-3 px-4 text-xs font-medium text-bambu-gray uppercase tracking-wide">{t('common.actions')}</th>
  1245. </tr>
  1246. </thead>
  1247. <tbody>
  1248. {pagedItems.map((item) => {
  1249. if (item.type === 'group') {
  1250. const { key, spools: groupSpools, representative: rep } = item;
  1251. const isExpanded = expandedGroups.has(key);
  1252. const remaining = Math.max(0, rep.label_weight - rep.weight_used);
  1253. const pct = rep.label_weight > 0 ? (remaining / rep.label_weight) * 100 : 0;
  1254. return (
  1255. <SpoolTableGroup
  1256. key={`group-${key}`}
  1257. spools={groupSpools}
  1258. representative={rep}
  1259. remaining={remaining}
  1260. pct={pct}
  1261. isExpanded={isExpanded}
  1262. onToggle={() => toggleGroupExpand(key)}
  1263. onEdit={(s) => setFormModal({ spool: s })}
  1264. onArchive={(id) => setConfirmAction({ type: 'archive', spoolId: id })}
  1265. onDelete={(id) => setConfirmAction({ type: 'delete', spoolId: id })}
  1266. visibleColumns={visibleColumns}
  1267. assignmentMap={assignmentMap}
  1268. catalogMap={catalogMap}
  1269. currencySymbol={currencySymbol}
  1270. dateFormat={dateFormat}
  1271. t={t}
  1272. onSyncWeight={handleSyncWeight}
  1273. />
  1274. );
  1275. }
  1276. const spool = item.spool;
  1277. const remaining = Math.max(0, spool.label_weight - spool.weight_used);
  1278. const pct = spool.label_weight > 0 ? (remaining / spool.label_weight) * 100 : 0;
  1279. return (
  1280. <SpoolTableRow
  1281. key={spool.id}
  1282. spool={spool}
  1283. remaining={remaining}
  1284. pct={pct}
  1285. onEdit={() => setFormModal({ spool })}
  1286. onRestore={() => restoreMutation.mutate(spool.id)}
  1287. onArchive={() => setConfirmAction({ type: 'archive', spoolId: spool.id })}
  1288. onDelete={() => setConfirmAction({ type: 'delete', spoolId: spool.id })}
  1289. visibleColumns={visibleColumns}
  1290. assignmentMap={assignmentMap}
  1291. catalogMap={catalogMap}
  1292. currencySymbol={currencySymbol}
  1293. dateFormat={dateFormat}
  1294. t={t}
  1295. onSyncWeight={handleSyncWeight}
  1296. />
  1297. );
  1298. })}
  1299. </tbody>
  1300. </table>
  1301. </div>
  1302. {/* Pagination inside card footer */}
  1303. <div className="flex items-center justify-between px-4 py-3 bg-bambu-dark-tertiary/50 border-t border-bambu-dark-tertiary text-sm">
  1304. <span className="text-bambu-gray">
  1305. {showAll
  1306. ? `${totalDisplayItems} ${totalDisplayItems !== 1 ? t('inventory.spools') : t('inventory.spool')}`
  1307. : <>{t('inventory.showing')} {safePageIndex * effectivePageSize + 1} {t('inventory.to')}{' '}
  1308. {Math.min((safePageIndex + 1) * effectivePageSize, totalDisplayItems)}{' '}
  1309. {t('inventory.of')} {totalDisplayItems} {t('inventory.spools')}</>
  1310. }
  1311. </span>
  1312. <div className="flex items-center gap-2">
  1313. <span className="text-bambu-gray">{t('inventory.show')}</span>
  1314. <select
  1315. value={pageSize}
  1316. onChange={(e) => handlePageSizeChange(Number(e.target.value))}
  1317. className="px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:border-bambu-green"
  1318. >
  1319. {[15, 30, 50, 100].map((n) => (
  1320. <option key={n} value={n}>{n}</option>
  1321. ))}
  1322. <option value={-1}>{t('inventory.all')}</option>
  1323. </select>
  1324. {!showAll && (
  1325. <>
  1326. <button
  1327. onClick={() => setPageIndex(0)}
  1328. disabled={safePageIndex === 0}
  1329. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1330. title="First page"
  1331. >
  1332. <ChevronsLeft className="w-4 h-4" />
  1333. </button>
  1334. <button
  1335. onClick={() => setPageIndex((p) => Math.max(0, p - 1))}
  1336. disabled={safePageIndex === 0}
  1337. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1338. >
  1339. <ChevronLeft className="w-4 h-4" />
  1340. </button>
  1341. <span className="text-bambu-gray px-2 whitespace-nowrap">
  1342. {t('inventory.page')} {safePageIndex + 1} {t('inventory.of')} {totalPages}
  1343. </span>
  1344. <button
  1345. onClick={() => setPageIndex((p) => Math.min(totalPages - 1, p + 1))}
  1346. disabled={safePageIndex >= totalPages - 1}
  1347. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1348. >
  1349. <ChevronRight className="w-4 h-4" />
  1350. </button>
  1351. <button
  1352. onClick={() => setPageIndex(totalPages - 1)}
  1353. disabled={safePageIndex >= totalPages - 1}
  1354. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1355. title="Last page"
  1356. >
  1357. <ChevronsRight className="w-4 h-4" />
  1358. </button>
  1359. </>
  1360. )}
  1361. </div>
  1362. </div>
  1363. </div>
  1364. ) : (
  1365. <EmptyFilterState
  1366. hasFilters={hasActiveFilters}
  1367. onAddSpool={() => setFormModal({ spool: null })}
  1368. t={t}
  1369. />
  1370. )
  1371. )}
  1372. {/* Spool Form Modal */}
  1373. {formModal !== null && (
  1374. <SpoolFormModal
  1375. isOpen={true}
  1376. onClose={() => setFormModal(null)}
  1377. spool={formModal.spool}
  1378. currencySymbol={currencySymbol}
  1379. />
  1380. )}
  1381. {/* Confirm Modal (delete / archive) */}
  1382. {confirmAction && (
  1383. <ConfirmModal
  1384. title={confirmAction.type === 'delete' ? t('common.delete') : t('inventory.archive')}
  1385. message={confirmAction.type === 'delete' ? t('inventory.deleteConfirm') : t('inventory.archiveConfirm')}
  1386. confirmText={confirmAction.type === 'delete' ? t('common.delete') : t('inventory.archive')}
  1387. variant={confirmAction.type === 'delete' ? 'danger' : 'warning'}
  1388. onConfirm={() => {
  1389. if (confirmAction.type === 'delete') {
  1390. deleteMutation.mutate(confirmAction.spoolId);
  1391. } else {
  1392. archiveMutation.mutate(confirmAction.spoolId);
  1393. }
  1394. setConfirmAction(null);
  1395. }}
  1396. onCancel={() => setConfirmAction(null)}
  1397. />
  1398. )}
  1399. {/* Column Config Modal */}
  1400. <ColumnConfigModal
  1401. isOpen={showColumnModal}
  1402. onClose={() => setShowColumnModal(false)}
  1403. columns={columnConfig}
  1404. defaultColumns={DEFAULT_COLUMNS}
  1405. onSave={handleColumnConfigSave}
  1406. />
  1407. </div>
  1408. );
  1409. }
  1410. /* Pagination bar (reused for cards view) */
  1411. function PaginationBar({
  1412. pageIndex, pageSize, totalRows, totalPages, onPageChange, onPageSizeChange, t,
  1413. }: {
  1414. pageIndex: number;
  1415. pageSize: number;
  1416. totalRows: number;
  1417. totalPages: number;
  1418. onPageChange: (page: number) => void;
  1419. onPageSizeChange: (size: number) => void;
  1420. t: (key: string) => string;
  1421. }) {
  1422. const isShowAll = pageSize === -1;
  1423. if (totalPages <= 1 && !isShowAll) return null;
  1424. const effectiveSize = isShowAll ? totalRows || 1 : pageSize;
  1425. return (
  1426. <div className="flex items-center justify-between pt-2 text-sm">
  1427. <span className="text-bambu-gray">
  1428. {isShowAll
  1429. ? `${totalRows} ${totalRows !== 1 ? t('inventory.spools') : t('inventory.spool')}`
  1430. : <>{t('inventory.showing')} {pageIndex * effectiveSize + 1} {t('inventory.to')}{' '}
  1431. {Math.min((pageIndex + 1) * effectiveSize, totalRows)}{' '}
  1432. {t('inventory.of')} {totalRows} {t('inventory.spools')}</>
  1433. }
  1434. </span>
  1435. <div className="flex items-center gap-2">
  1436. <span className="text-bambu-gray">{t('inventory.show')}</span>
  1437. <select
  1438. value={pageSize}
  1439. onChange={(e) => onPageSizeChange(Number(e.target.value))}
  1440. className="px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:border-bambu-green"
  1441. >
  1442. {[15, 30, 50, 100].map((n) => (
  1443. <option key={n} value={n}>{n}</option>
  1444. ))}
  1445. <option value={-1}>{t('inventory.all')}</option>
  1446. </select>
  1447. {!isShowAll && (
  1448. <>
  1449. <button
  1450. onClick={() => onPageChange(0)}
  1451. disabled={pageIndex === 0}
  1452. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1453. >
  1454. <ChevronsLeft className="w-4 h-4" />
  1455. </button>
  1456. <button
  1457. onClick={() => onPageChange(Math.max(0, pageIndex - 1))}
  1458. disabled={pageIndex === 0}
  1459. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1460. >
  1461. <ChevronLeft className="w-4 h-4" />
  1462. </button>
  1463. <span className="text-bambu-gray px-2 whitespace-nowrap">
  1464. {t('inventory.page')} {pageIndex + 1} {t('inventory.of')} {totalPages}
  1465. </span>
  1466. <button
  1467. onClick={() => onPageChange(Math.min(totalPages - 1, pageIndex + 1))}
  1468. disabled={pageIndex >= totalPages - 1}
  1469. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1470. >
  1471. <ChevronRight className="w-4 h-4" />
  1472. </button>
  1473. <button
  1474. onClick={() => onPageChange(totalPages - 1)}
  1475. disabled={pageIndex >= totalPages - 1}
  1476. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1477. >
  1478. <ChevronsRight className="w-4 h-4" />
  1479. </button>
  1480. </>
  1481. )}
  1482. </div>
  1483. </div>
  1484. );
  1485. }
  1486. /* Spool card for cards view */
  1487. function SpoolCard({
  1488. spool, remaining, pct, colorStyle, onClick, t,
  1489. }: {
  1490. spool: InventorySpool;
  1491. remaining: number;
  1492. pct: number;
  1493. colorStyle: string;
  1494. onClick: () => void;
  1495. t: (key: string, opts?: Record<string, unknown>) => string;
  1496. }) {
  1497. return (
  1498. <div
  1499. className={`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary hover:border-bambu-green transition-colors cursor-pointer ${spool.archived_at ? 'opacity-50' : ''}`}
  1500. onClick={onClick}
  1501. >
  1502. <div className="h-14 flex items-center justify-center" style={{ backgroundColor: colorStyle }}>
  1503. <span className="bg-white/90 text-gray-800 px-3 py-0.5 rounded-full text-sm font-medium">
  1504. {resolveSpoolColorName(spool.color_name, spool.rgba) || '-'}
  1505. </span>
  1506. </div>
  1507. <div className="p-4 space-y-3">
  1508. <div className="flex items-start justify-between gap-2">
  1509. <div>
  1510. <h3 className="font-semibold text-white">
  1511. {spool.material}{spool.subtype ? ` ${spool.subtype}` : ''}
  1512. </h3>
  1513. <p className="text-sm text-bambu-gray">{spool.brand || '-'}</p>
  1514. </div>
  1515. <span className="text-xs font-mono text-bambu-gray bg-bambu-dark-tertiary px-2 py-1 rounded">
  1516. #{spool.id}
  1517. </span>
  1518. </div>
  1519. <div>
  1520. <div className="flex justify-between text-xs text-bambu-gray mb-1">
  1521. <span>{t('inventory.remaining')}</span>
  1522. <span>{Math.round(pct)}%</span>
  1523. </div>
  1524. <div className="flex items-center gap-2">
  1525. <div className="flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden">
  1526. <div
  1527. className={`h-full rounded-full ${pct > 50 ? 'bg-bambu-green' : pct > 20 ? 'bg-yellow-500' : 'bg-red-500'}`}
  1528. style={{ width: `${Math.min(pct, 100)}%` }}
  1529. />
  1530. </div>
  1531. <span className="text-xs text-bambu-gray min-w-[40px] text-right">
  1532. {Math.round(remaining)}g
  1533. </span>
  1534. </div>
  1535. </div>
  1536. <div className="grid grid-cols-2 gap-2 text-xs">
  1537. <div>
  1538. <span className="text-bambu-gray/60">{t('inventory.labelWeight')}: </span>
  1539. <span className="text-bambu-gray">{formatWeight(spool.label_weight)}</span>
  1540. </div>
  1541. <div>
  1542. <span className="text-bambu-gray/60">{t('inventory.weightUsed')}: </span>
  1543. <span className="text-bambu-gray">
  1544. {spool.weight_used > 0 ? formatWeight(spool.weight_used) : '-'}
  1545. </span>
  1546. </div>
  1547. </div>
  1548. {spool.note && (
  1549. <div
  1550. className="text-xs text-bambu-gray/60 pt-2 border-t border-bambu-dark-tertiary truncate"
  1551. title={spool.note}
  1552. >
  1553. {spool.note}
  1554. </div>
  1555. )}
  1556. </div>
  1557. </div>
  1558. );
  1559. }
  1560. /* Single spool row for table view */
  1561. function SpoolTableRow({
  1562. spool, remaining, pct, onEdit, onRestore, onArchive, onDelete,
  1563. visibleColumns, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight,
  1564. }: {
  1565. spool: InventorySpool;
  1566. remaining: number;
  1567. pct: number;
  1568. onEdit: () => void;
  1569. onRestore: () => void;
  1570. onArchive: () => void;
  1571. onDelete: () => void;
  1572. visibleColumns: string[];
  1573. assignmentMap: Record<number, SpoolAssignment>;
  1574. catalogMap: Record<number, SpoolCatalogEntry>;
  1575. currencySymbol: string;
  1576. dateFormat: DateFormat;
  1577. t: TFn;
  1578. onSyncWeight?: (spool: InventorySpool) => void;
  1579. }) {
  1580. return (
  1581. <tr
  1582. className={`border-b border-bambu-dark-tertiary/50 hover:bg-bambu-dark-tertiary/30 transition-colors cursor-pointer ${
  1583. spool.archived_at ? 'opacity-50' : ''
  1584. }`}
  1585. onClick={onEdit}
  1586. >
  1587. {visibleColumns.map((colId) => (
  1588. <td key={colId} className="py-3 px-4">
  1589. {columnCells[colId]?.({ spool, remaining, pct, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight })}
  1590. </td>
  1591. ))}
  1592. <td className="py-3 px-4">
  1593. <div className="flex items-center justify-end gap-1" onClick={(e) => e.stopPropagation()}>
  1594. <button onClick={onEdit} className="p-1.5 text-bambu-gray hover:text-white rounded transition-colors" title={t('common.edit')}>
  1595. <Edit2 className="w-4 h-4" />
  1596. </button>
  1597. {spool.archived_at ? (
  1598. <button onClick={onRestore} className="p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors" title={t('inventory.restore')}>
  1599. <RotateCcw className="w-4 h-4" />
  1600. </button>
  1601. ) : (
  1602. <button onClick={onArchive} className="p-1.5 text-bambu-gray hover:text-yellow-400 rounded transition-colors" title={t('inventory.archive')}>
  1603. <Archive className="w-4 h-4" />
  1604. </button>
  1605. )}
  1606. <button onClick={onDelete} className="p-1.5 text-bambu-gray hover:text-red-400 rounded transition-colors" title={t('common.delete')}>
  1607. <Trash2 className="w-4 h-4" />
  1608. </button>
  1609. </div>
  1610. </td>
  1611. </tr>
  1612. );
  1613. }
  1614. /* Grouped spool rows for table view */
  1615. function SpoolTableGroup({
  1616. spools, representative, remaining, pct, isExpanded, onToggle,
  1617. onEdit, onArchive, onDelete,
  1618. visibleColumns, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight,
  1619. }: {
  1620. spools: InventorySpool[];
  1621. representative: InventorySpool;
  1622. remaining: number;
  1623. pct: number;
  1624. isExpanded: boolean;
  1625. onToggle: () => void;
  1626. onEdit: (spool: InventorySpool) => void;
  1627. onArchive: (id: number) => void;
  1628. onDelete: (id: number) => void;
  1629. visibleColumns: string[];
  1630. assignmentMap: Record<number, SpoolAssignment>;
  1631. catalogMap: Record<number, SpoolCatalogEntry>;
  1632. currencySymbol: string;
  1633. dateFormat: DateFormat;
  1634. t: TFn;
  1635. onSyncWeight?: (spool: InventorySpool) => void;
  1636. }) {
  1637. return (
  1638. <>
  1639. {/* Group header row */}
  1640. <tr
  1641. className="border-b border-bambu-dark-tertiary/50 hover:bg-bambu-dark-tertiary/30 transition-colors cursor-pointer bg-bambu-green/5"
  1642. onClick={onToggle}
  1643. >
  1644. {visibleColumns.map((colId, idx) => (
  1645. <td key={colId} className="py-3 px-4">
  1646. {idx === 0 ? (
  1647. <div className="flex items-center gap-2">
  1648. <ChevronDown className={`w-4 h-4 text-bambu-gray transition-transform ${isExpanded ? '' : '-rotate-90'}`} />
  1649. {columnCells[colId]?.({ spool: representative, remaining, pct, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight })}
  1650. </div>
  1651. ) : colId === 'id' ? (
  1652. <span className="text-xs font-medium bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full">
  1653. {t('inventory.groupedSpools', { count: spools.length })}
  1654. </span>
  1655. ) : (
  1656. columnCells[colId]?.({ spool: representative, remaining, pct, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight })
  1657. )}
  1658. </td>
  1659. ))}
  1660. <td className="py-3 px-4">
  1661. <span className="text-xs text-bambu-gray">
  1662. {spools.map((s) => `#${s.id}`).join(', ')}
  1663. </span>
  1664. </td>
  1665. </tr>
  1666. {/* Expanded individual rows */}
  1667. {isExpanded && spools.map((spool) => {
  1668. const r = Math.max(0, spool.label_weight - spool.weight_used);
  1669. const p = spool.label_weight > 0 ? (r / spool.label_weight) * 100 : 0;
  1670. return (
  1671. <SpoolTableRow
  1672. key={spool.id}
  1673. spool={spool}
  1674. remaining={r}
  1675. pct={p}
  1676. onEdit={() => onEdit(spool)}
  1677. onRestore={() => {}}
  1678. onArchive={() => onArchive(spool.id)}
  1679. onDelete={() => onDelete(spool.id)}
  1680. visibleColumns={visibleColumns}
  1681. assignmentMap={assignmentMap}
  1682. catalogMap={catalogMap}
  1683. currencySymbol={currencySymbol}
  1684. dateFormat={dateFormat}
  1685. t={t}
  1686. onSyncWeight={onSyncWeight}
  1687. />
  1688. );
  1689. })}
  1690. </>
  1691. );
  1692. }
  1693. /* Empty state matching SpoolBuddy's design */
  1694. function EmptyFilterState({
  1695. hasFilters,
  1696. onAddSpool,
  1697. t,
  1698. }: {
  1699. hasFilters: boolean;
  1700. onAddSpool: () => void;
  1701. t: (key: string) => string;
  1702. }) {
  1703. return (
  1704. <div className="flex flex-col items-center justify-center py-16 px-4">
  1705. <div className="relative mb-6">
  1706. <div className="absolute inset-0 -m-4 bg-bambu-green/5 rounded-full blur-2xl" />
  1707. <div className="relative flex items-center justify-center w-24 h-24 rounded-2xl bg-gradient-to-br from-bambu-dark-secondary to-bambu-dark-tertiary border border-bambu-dark-tertiary shadow-lg">
  1708. <div className="absolute -top-1 -right-1 w-3 h-3 rounded-full bg-bambu-green/30" />
  1709. <div className="absolute -bottom-2 -left-2 w-2 h-2 rounded-full bg-bambu-green/20" />
  1710. {hasFilters ? (
  1711. <Search className="w-10 h-10 text-bambu-gray/40" strokeWidth={1.5} />
  1712. ) : (
  1713. <div className="relative">
  1714. <div className="w-14 h-14 rounded-full border-4 border-bambu-gray/20 flex items-center justify-center">
  1715. <div className="w-6 h-6 rounded-full bg-bambu-gray/10 border-2 border-bambu-gray/20" />
  1716. </div>
  1717. <div className="absolute -bottom-1 -right-1 w-6 h-6 rounded-full bg-bambu-green flex items-center justify-center shadow-md">
  1718. <span className="text-white text-lg font-bold leading-none">+</span>
  1719. </div>
  1720. </div>
  1721. )}
  1722. </div>
  1723. </div>
  1724. <h3 className="text-lg font-semibold text-white mb-2 text-center">
  1725. {hasFilters ? t('inventory.noSpoolsMatch') : t('inventory.noSpools').split('.')[0]}
  1726. </h3>
  1727. <p className="text-sm text-bambu-gray text-center max-w-sm mb-6">
  1728. {hasFilters
  1729. ? t('inventory.noSpoolsMatchDesc')
  1730. : t('inventory.noSpools')
  1731. }
  1732. </p>
  1733. {!hasFilters && (
  1734. <Button onClick={onAddSpool}>
  1735. <Package className="w-4 h-4" />
  1736. {t('inventory.addSpool')}
  1737. </Button>
  1738. )}
  1739. </div>
  1740. );
  1741. }