InventoryPage.tsx 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296
  1. import { useState, useMemo, useEffect, useRef, useCallback, type ReactNode } from 'react';
  2. import { useSearchParams } from 'react-router-dom';
  3. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  4. import { useTranslation } from 'react-i18next';
  5. import {
  6. Plus, Loader2, Trash2, Archive, RotateCcw, Edit2, Package,
  7. Search, ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight,
  8. TrendingDown, Layers, Printer, AlertTriangle, X, Clock, LayoutGrid, TableProperties, Columns,
  9. ArrowUp, ArrowDown, ArrowUpDown, Group, ChevronDown, Check, RefreshCw, TrendingUp, Lock, Copy, Eraser,
  10. } from 'lucide-react';
  11. import { ForecastPanel } from '../components/ForecastPanel';
  12. import { api, spoolbuddyApi, ApiError } from '../api/client';
  13. import type { InventorySpool, SpoolCatalogEntry } from '../api/client';
  14. import { Button } from '../components/Button';
  15. import { FilamentSwatch } from '../components/FilamentSwatch';
  16. import { buildFilamentBackground } from '../components/filamentSwatchHelpers';
  17. import {SpoolFormModal, type SpoolFormMode} from '../components/SpoolFormModal';
  18. import { ConfirmModal } from '../components/ConfirmModal';
  19. import { ColumnConfigModal, type ColumnConfig } from '../components/ColumnConfigModal';
  20. import { LabelTemplatePickerModal } from '../components/LabelTemplatePickerModal';
  21. import { useToast } from '../contexts/ToastContext';
  22. import { useAuth } from '../contexts/AuthContext';
  23. import { resolveSpoolColorName } from '../utils/colors';
  24. import { getCurrencySymbol } from '../utils/currency';
  25. import { formatDateInput, parseUTCDate, type DateFormat } from '../utils/date';
  26. import { formatSlotLabel } from '../utils/amsHelpers';
  27. import { filterSpoolsByQuery } from '../utils/inventorySearch';
  28. type ArchiveFilter = 'active' | 'archived';
  29. type UsageFilter = 'all' | 'used' | 'new' | 'lowstock';
  30. type ViewMode = 'table' | 'cards' | 'forecast';
  31. type SortDirection = 'asc' | 'desc';
  32. type SortState = { column: string; direction: SortDirection } | null;
  33. type DisplayItem =
  34. | { type: 'single'; spool: InventorySpool }
  35. | { type: 'group'; key: string; spools: InventorySpool[]; representative: InventorySpool };
  36. function spoolGroupKey(s: InventorySpool): string {
  37. // Include extra_colors + effect_type so the "Group similar" toggle does
  38. // not collapse two spools that share the base colour but differ on
  39. // gradient stops or visual effect (#1154).
  40. return `${s.material}|${s.subtype || ''}|${s.brand || ''}|${s.color_name || ''}|${s.rgba || ''}|${s.extra_colors || ''}|${s.effect_type || ''}|${s.label_weight}`;
  41. }
  42. // Column definitions for the inventory table
  43. const COLUMN_CONFIG_KEY = 'bambuddy-inventory-columns';
  44. const DEFAULT_COLUMNS: ColumnConfig[] = [
  45. { id: 'id', label: '#', visible: true },
  46. { id: 'added_time', label: 'Added', visible: true },
  47. { id: 'encode_time', label: 'Encoded', visible: false },
  48. { id: 'last_used_time', label: 'Last Used', visible: false },
  49. { id: 'rgba', label: 'Color', visible: true },
  50. { id: 'material', label: 'Material', visible: true },
  51. { id: 'subtype', label: 'Subtype', visible: true },
  52. { id: 'color_name', label: 'Color Name', visible: false },
  53. { id: 'brand', label: 'Brand', visible: true },
  54. { id: 'slicer_filament', label: 'Slicer Filament', visible: false },
  55. { id: 'location', label: 'Location', visible: true },
  56. { id: 'storage_location', label: 'Storage Location', visible: false },
  57. { id: 'label_weight', label: 'Label', visible: true },
  58. { id: 'net', label: 'Net', visible: true },
  59. { id: 'gross', label: 'Gross', visible: false },
  60. { id: 'added_full', label: 'Full', visible: false },
  61. { id: 'used', label: 'Used', visible: false },
  62. { id: 'printed_total', label: 'Printed Total', visible: false },
  63. { id: 'printed_since_weight', label: 'Printed Since Weight', visible: false },
  64. { id: 'note', label: 'Note', visible: false },
  65. { id: 'pa_k', label: 'PA(K)', visible: true },
  66. { id: 'tag_id', label: 'Tag ID', visible: false },
  67. { id: 'data_origin', label: 'Data Origin', visible: false },
  68. { id: 'tag_type', label: 'Linked Tag Type', visible: false },
  69. { id: 'stock', label: 'Stock', visible: false },
  70. { id: 'remaining', label: 'Remaining', visible: true },
  71. { id: 'spool_name', label: 'Spool', visible: false },
  72. { id: 'cost_per_kg', label: 'Cost/kg', visible: false },
  73. { id: 'weight_check', label: 'Weight Check', visible: false },
  74. ];
  75. function loadColumnConfig(): ColumnConfig[] {
  76. try {
  77. const stored = localStorage.getItem(COLUMN_CONFIG_KEY);
  78. if (stored) {
  79. const parsed = JSON.parse(stored) as ColumnConfig[];
  80. const defaultIds = new Set(DEFAULT_COLUMNS.map((c) => c.id));
  81. const storedIds = new Set(parsed.map((c) => c.id));
  82. // Keep stored columns that still exist in defaults
  83. const validStored = parsed.filter((c) => defaultIds.has(c.id));
  84. // Add any new default columns not in stored config
  85. const newColumns = DEFAULT_COLUMNS.filter((c) => !storedIds.has(c.id));
  86. return [...validStored, ...newColumns];
  87. }
  88. } catch {
  89. // Ignore errors
  90. }
  91. return DEFAULT_COLUMNS.map((c) => ({ ...c }));
  92. }
  93. function saveColumnConfig(config: ColumnConfig[]) {
  94. try {
  95. localStorage.setItem(COLUMN_CONFIG_KEY, JSON.stringify(config));
  96. } catch {
  97. // Ignore errors
  98. }
  99. }
  100. function formatWeight(g: number, useKg = false): string {
  101. if (useKg && g >= 1000) return `${(g / 1000).toFixed(1)}kg`;
  102. return `${Math.round(g)}g`;
  103. }
  104. // Material color mapping for pills
  105. const MATERIAL_COLORS: Record<string, string> = {
  106. PLA: 'bg-green-500/20 text-green-400',
  107. ABS: 'bg-red-500/20 text-red-400',
  108. PETG: 'bg-blue-500/20 text-blue-400',
  109. TPU: 'bg-purple-500/20 text-purple-400',
  110. ASA: 'bg-orange-500/20 text-orange-400',
  111. PA: 'bg-yellow-500/20 text-yellow-400',
  112. PC: 'bg-cyan-500/20 text-cyan-400',
  113. PET: 'bg-sky-500/20 text-sky-400',
  114. };
  115. type TFn = (key: string, opts?: Record<string, unknown>) => string;
  116. function formatInventoryDate(dateStr: string | null, dateFormat: DateFormat = 'system'): string {
  117. if (!dateStr) return '-';
  118. const date = parseUTCDate(dateStr);
  119. if (!date) return '-';
  120. return formatDateInput(date, dateFormat);
  121. }
  122. // Slim shape for the LOCATION column — only the fields actually rendered.
  123. // Sourced from either local SpoolAssignment (lokal) or SpoolmanSlotAssignment
  124. // (Spoolman mode), so we can't reuse SpoolAssignment without dummy values.
  125. type LocationDisplay = {
  126. printer_id: number;
  127. printer_name: string | null;
  128. ams_id: number;
  129. tray_id: number;
  130. ams_label: string | null;
  131. };
  132. type CellCtx = {
  133. spool: InventorySpool;
  134. remaining: number;
  135. pct: number;
  136. assignmentMap: Record<number, LocationDisplay>;
  137. catalogMap: Record<number, SpoolCatalogEntry>;
  138. currencySymbol: string;
  139. dateFormat: DateFormat;
  140. t: TFn;
  141. onSyncWeight?: (spool: InventorySpool) => void;
  142. };
  143. // Column header labels (25 columns — matching SpoolBuddy exactly)
  144. const columnHeaders: Record<string, (t: TFn) => string> = {
  145. id: () => '#',
  146. added_time: () => 'Added',
  147. encode_time: () => 'Encoded',
  148. last_used_time: () => 'Last Used',
  149. rgba: (t) => t('inventory.color'),
  150. material: (t) => t('inventory.material'),
  151. subtype: (t) => t('inventory.subtype'),
  152. color_name: (t) => t('inventory.colorName'),
  153. brand: (t) => t('inventory.brand'),
  154. slicer_filament: (t) => t('inventory.slicerFilament'),
  155. location: () => 'Location',
  156. storage_location: (t) => t('inventory.storageLocation'),
  157. label_weight: (t) => t('inventory.labelWeight'),
  158. net: (t) => t('inventory.net'),
  159. gross: () => 'Gross',
  160. added_full: () => 'Full',
  161. used: (t) => t('inventory.weightUsed'),
  162. printed_total: () => 'Printed Total',
  163. printed_since_weight: () => 'Printed Since Weight',
  164. note: (t) => t('inventory.note'),
  165. pa_k: () => 'PA(K)',
  166. tag_id: () => 'Tag ID',
  167. data_origin: () => 'Data Origin',
  168. tag_type: () => 'Linked Tag Type',
  169. stock: (t) => t('inventory.stock'),
  170. remaining: (t) => t('inventory.remaining'),
  171. spool_name: (t) => t('inventory.spoolName'),
  172. cost_per_kg: (t) => t('inventory.costPerKg'),
  173. weight_check: (t) => t('inventory.weightCheck'),
  174. };
  175. // Column cell renderers (25 columns — matching SpoolBuddy exactly)
  176. const columnCells: Record<string, (ctx: CellCtx) => ReactNode> = {
  177. id: ({ spool }) => (
  178. <span className="text-sm font-medium text-white">{spool.id}</span>
  179. ),
  180. added_time: ({ spool, dateFormat }) => (
  181. <span className="text-sm text-bambu-gray">{formatInventoryDate(spool.created_at, dateFormat)}</span>
  182. ),
  183. encode_time: ({ spool, dateFormat }) => (
  184. <span className="text-sm text-bambu-gray">{formatInventoryDate(spool.encode_time, dateFormat)}</span>
  185. ),
  186. last_used_time: ({ spool, dateFormat }) => (
  187. <span className="text-sm text-bambu-gray">{spool.last_used ? formatInventoryDate(spool.last_used, dateFormat) : 'Never'}</span>
  188. ),
  189. rgba: ({ spool }) => (
  190. <div className="flex items-center justify-center">
  191. <FilamentSwatch
  192. rgba={spool.rgba}
  193. extraColors={spool.extra_colors}
  194. effectType={spool.effect_type}
  195. effectSize="table"
  196. subtype={spool.subtype}
  197. />
  198. </div>
  199. ),
  200. material: ({ spool }) => (
  201. <span className="text-sm text-white">{spool.material}</span>
  202. ),
  203. subtype: ({ spool }) => (
  204. <span className="text-sm text-bambu-gray">{spool.subtype || '-'}</span>
  205. ),
  206. color_name: ({ spool }) => (
  207. <span className="text-sm text-bambu-gray">{resolveSpoolColorName(spool.color_name, spool.rgba) || '-'}</span>
  208. ),
  209. brand: ({ spool }) => (
  210. <span className="text-sm text-bambu-gray">{spool.brand || '-'}</span>
  211. ),
  212. slicer_filament: ({ spool }) => (
  213. <span className="text-sm text-bambu-gray" title={spool.slicer_filament || undefined}>
  214. {spool.slicer_filament_name || spool.slicer_filament || '-'}
  215. </span>
  216. ),
  217. location: ({ spool, assignmentMap }) => {
  218. const assignment = assignmentMap[spool.id];
  219. if (!assignment) return <span className="text-sm text-bambu-gray">-</span>;
  220. const printerLabel = assignment.printer_name || `Printer ${assignment.printer_id}`;
  221. const isExternal = assignment.ams_id === 254 || assignment.ams_id === 255;
  222. const isHt = !isExternal && assignment.ams_id >= 128;
  223. const slotLabel = formatSlotLabel(assignment.ams_id, assignment.tray_id, isHt, isExternal);
  224. return (
  225. <span className="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-purple-500/20 text-purple-400">
  226. {printerLabel} {slotLabel}{assignment.ams_label ? ` (${assignment.ams_label})` : ''}
  227. </span>
  228. );
  229. },
  230. storage_location: ({ spool }) => {
  231. if (!spool.storage_location) return <span className="text-sm text-bambu-gray">-</span>;
  232. return (
  233. <span className="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-blue-500/20 text-blue-400">
  234. {spool.storage_location}
  235. </span>
  236. );
  237. },
  238. label_weight: ({ spool }) => (
  239. <span className="text-sm text-white">{formatWeight(spool.label_weight)}</span>
  240. ),
  241. net: ({ remaining }) => (
  242. <span className="text-sm text-white">{formatWeight(remaining)}</span>
  243. ),
  244. gross: ({ spool, remaining }) => (
  245. <span className="text-sm text-bambu-gray">{formatWeight(remaining + spool.core_weight)}</span>
  246. ),
  247. added_full: ({ spool }) => (
  248. <span className="text-sm text-bambu-gray">{spool.added_full == null ? '-' : spool.added_full ? 'Yes' : 'No'}</span>
  249. ),
  250. used: ({ spool }) => (
  251. <span className="text-sm text-bambu-gray">{spool.weight_used > 0 ? formatWeight(spool.weight_used) : '-'}</span>
  252. ),
  253. printed_total: () => (
  254. <span className="text-sm text-bambu-gray/50">-</span>
  255. ),
  256. printed_since_weight: () => (
  257. <span className="text-sm text-bambu-gray/50">-</span>
  258. ),
  259. note: ({ spool }) => (
  260. <span className="text-sm text-bambu-gray max-w-[150px] truncate block" title={spool.note || undefined}>{spool.note || '-'}</span>
  261. ),
  262. pa_k: ({ spool }) => {
  263. const count = spool.k_profiles?.length ?? 0;
  264. if (count === 0) return <span className="text-sm text-bambu-gray">-</span>;
  265. return (
  266. <span className="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-bambu-green/20 text-bambu-green">
  267. K
  268. </span>
  269. );
  270. },
  271. tag_id: ({ spool }) => {
  272. const tag = spool.tag_uid || spool.tray_uuid;
  273. if (!tag) return <span className="text-sm text-bambu-gray/50">-</span>;
  274. return (
  275. <span className="text-sm text-bambu-gray font-mono" title={tag}>
  276. {tag.length > 12 ? `${tag.slice(0, 6)}...${tag.slice(-4)}` : tag}
  277. </span>
  278. );
  279. },
  280. data_origin: ({ spool }) => (
  281. <span className="text-sm text-bambu-gray">{spool.data_origin || '-'}</span>
  282. ),
  283. tag_type: ({ spool }) => (
  284. <span className="text-sm text-bambu-gray">{spool.tag_type || '-'}</span>
  285. ),
  286. stock: ({ spool, t }) => {
  287. if (!spool.slicer_filament) {
  288. return (
  289. <span className="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-amber-500/20 text-amber-400">
  290. {t('inventory.stock')}
  291. </span>
  292. );
  293. }
  294. return <span className="text-sm text-bambu-gray">-</span>;
  295. },
  296. remaining: ({ remaining, pct }) => (
  297. <div className="flex items-center gap-2">
  298. <div className="flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden">
  299. <div
  300. className={`h-full rounded-full ${pct > 50 ? 'bg-bambu-green' : pct > 20 ? 'bg-yellow-500' : 'bg-red-500'}`}
  301. style={{ width: `${Math.min(pct, 100)}%` }}
  302. />
  303. </div>
  304. <span className="text-xs text-bambu-gray min-w-[40px] text-right">{Math.round(remaining)}g</span>
  305. </div>
  306. ),
  307. spool_name: ({ spool, catalogMap }) => {
  308. const entry = spool.core_weight_catalog_id != null ? catalogMap[spool.core_weight_catalog_id] : undefined;
  309. return <span className="text-sm text-bambu-gray">{entry?.name || '-'}</span>;
  310. },
  311. cost_per_kg: ({ spool, currencySymbol }) => (
  312. <span className="text-sm text-bambu-gray">
  313. {spool.cost_per_kg != null ? `${currencySymbol}${spool.cost_per_kg.toFixed(2)}` : '-'}
  314. </span>
  315. ),
  316. weight_check: ({ spool, onSyncWeight }) => {
  317. const scaleWeight = spool.last_scale_weight;
  318. if (scaleWeight == null) return <span className="text-sm text-bambu-gray/50" title="No scale measurement">-</span>;
  319. const coreWeight = spool.core_weight || 0;
  320. const calculatedWeight = Math.max(0, spool.label_weight - spool.weight_used) + coreWeight;
  321. // Edge case: scale < core_weight means spool is empty or not on scale — treat as match
  322. let difference: number;
  323. let isMatch: boolean;
  324. if (scaleWeight < coreWeight) {
  325. difference = scaleWeight - coreWeight;
  326. isMatch = true;
  327. } else {
  328. difference = scaleWeight - calculatedWeight;
  329. isMatch = Math.abs(difference) <= 50;
  330. }
  331. const diffStr = difference > 0 ? `+${Math.round(difference)}` : `${Math.round(difference)}`;
  332. const tooltip = isMatch
  333. ? `Scale: ${Math.round(scaleWeight)}g\nCalculated: ${Math.round(calculatedWeight)}g\nDifference: ${diffStr}g (within tolerance)`
  334. : `Scale: ${Math.round(scaleWeight)}g\nCalculated: ${Math.round(calculatedWeight)}g\nDifference: ${diffStr}g (mismatch!)`;
  335. return (
  336. <div
  337. className={`flex items-center gap-1 text-sm font-medium ${isMatch ? 'text-green-400' : 'text-yellow-400'}`}
  338. title={tooltip}
  339. >
  340. <span>{Math.round(scaleWeight)}g</span>
  341. {isMatch ? (
  342. <Check className="w-3 h-3" />
  343. ) : (
  344. <>
  345. <AlertTriangle className="w-3 h-3" />
  346. {onSyncWeight && (
  347. <button
  348. type="button"
  349. onClick={(e) => {
  350. e.stopPropagation();
  351. e.preventDefault();
  352. onSyncWeight(spool);
  353. }}
  354. className="p-1 hover:bg-bambu-green/20 rounded transition-colors text-bambu-green"
  355. title="Sync: trust scale weight and reset tracking"
  356. >
  357. <RefreshCw className="w-3.5 h-3.5" />
  358. </button>
  359. )}
  360. </>
  361. )}
  362. </div>
  363. );
  364. },
  365. };
  366. // Sort value extractors — return a comparable value for each sortable column
  367. const columnSortValues: Record<string, (spool: InventorySpool, assignmentMap: Record<number, LocationDisplay>) => string | number> = {
  368. id: (s) => s.id,
  369. added_time: (s) => s.created_at || '',
  370. encode_time: (s) => s.encode_time || '',
  371. last_used_time: (s) => s.last_used || '',
  372. material: (s) => (s.material || '').toLowerCase(),
  373. subtype: (s) => (s.subtype || '').toLowerCase(),
  374. color_name: (s) => (s.color_name || '').toLowerCase(),
  375. brand: (s) => (s.brand || '').toLowerCase(),
  376. slicer_filament: (s) => (s.slicer_filament_name || s.slicer_filament || '').toLowerCase(),
  377. location: (s, am) => {
  378. const a = am[s.id];
  379. if (!a) return '';
  380. const isExt = a.ams_id === 254 || a.ams_id === 255;
  381. const isHt = !isExt && a.ams_id >= 128;
  382. const label = a.ams_label ? ` (${a.ams_label})` : '';
  383. return `${a.printer_name || ''} ${formatSlotLabel(a.ams_id, a.tray_id, isHt, isExt)}${label}`;
  384. },
  385. storage_location: (s) => (s.storage_location || '').toLowerCase(),
  386. label_weight: (s) => s.label_weight,
  387. net: (s) => Math.max(0, s.label_weight - s.weight_used),
  388. gross: (s) => Math.max(0, s.label_weight - s.weight_used) + s.core_weight,
  389. used: (s) => s.weight_used,
  390. remaining: (s) => s.label_weight > 0 ? Math.max(0, s.label_weight - s.weight_used) / s.label_weight : 0,
  391. note: (s) => (s.note || '').toLowerCase(),
  392. data_origin: (s) => (s.data_origin || '').toLowerCase(),
  393. tag_type: (s) => (s.tag_type || '').toLowerCase(),
  394. stock: (s) => s.slicer_filament ? 1 : 0,
  395. spool_name: (s) => s.core_weight_catalog_id ?? 0,
  396. cost_per_kg: (s) => s.cost_per_kg ?? 0,
  397. weight_check: (s) => {
  398. if (s.last_scale_weight == null) return -1;
  399. const expectedGross = Math.max(0, s.label_weight - s.weight_used) + s.core_weight;
  400. return Math.abs(s.last_scale_weight - expectedGross);
  401. },
  402. };
  403. const SORT_STATE_KEY = 'bambuddy-inventory-sort';
  404. function loadSortState(): SortState {
  405. try {
  406. const stored = localStorage.getItem(SORT_STATE_KEY);
  407. if (stored) return JSON.parse(stored);
  408. } catch { /* ignore */ }
  409. return null;
  410. }
  411. function saveSortState(state: SortState) {
  412. try {
  413. if (state) {
  414. localStorage.setItem(SORT_STATE_KEY, JSON.stringify(state));
  415. } else {
  416. localStorage.removeItem(SORT_STATE_KEY);
  417. }
  418. } catch { /* ignore */ }
  419. }
  420. // Wrapper: detects Spoolman mode and passes it to the shared inventory UI
  421. export default function InventoryPageRouter() {
  422. const { data: spoolmanSettings } = useQuery({
  423. queryKey: ['spoolman-settings'],
  424. queryFn: api.getSpoolmanSettings,
  425. staleTime: 5 * 60 * 1000,
  426. });
  427. const spoolmanModeReady = spoolmanSettings !== undefined;
  428. const spoolmanMode =
  429. spoolmanSettings?.spoolman_enabled === 'true' && !!spoolmanSettings?.spoolman_url;
  430. return <InventoryPage spoolmanMode={spoolmanMode} spoolmanModeReady={spoolmanModeReady} />;
  431. }
  432. function InventoryPage({ spoolmanMode = false, spoolmanModeReady = true }: { spoolmanMode?: boolean; spoolmanModeReady?: boolean }) {
  433. const { t } = useTranslation();
  434. const queryClient = useQueryClient();
  435. const { showToast } = useToast();
  436. const { hasPermission, loading: authLoading } = useAuth();
  437. const canViewForecast = !authLoading && hasPermission('inventory:forecast_read');
  438. const [searchParams, setSearchParams] = useSearchParams();
  439. const [formModal, setFormModal] = useState<{ spool?: InventorySpool | null; mode: SpoolFormMode } | null>(null);
  440. const deepLinkHandled = useRef(false);
  441. const [confirmAction, setConfirmAction] = useState<
  442. | { type: 'delete' | 'archive' | 'reset-usage'; spoolId: number }
  443. | { type: 'reset-all-usage' }
  444. | null
  445. >(null);
  446. // Label printing (#809). null = closed; otherwise the IDs to print labels for.
  447. const [labelPickerSpoolIds, setLabelPickerSpoolIds] = useState<number[] | null>(null);
  448. // Filter state
  449. const [archiveFilter, setArchiveFilter] = useState<ArchiveFilter>('active');
  450. const [usageFilter, setUsageFilter] = useState<UsageFilter>('all');
  451. const [materialFilter, setMaterialFilter] = useState('');
  452. const [brandFilter, setBrandFilter] = useState('');
  453. const [categoryFilter, setCategoryFilter] = useState('');
  454. const [spoolFilter, setSpoolFilter] = useState('');
  455. const [stockFilter, setStockFilter] = useState<'all' | 'stock' | 'configured'>('all');
  456. // #1400: storage-location dropdown. Uses the sentinel `__none__` for the
  457. // "no storage location set" group, same pattern as the category filter so
  458. // users can find unfiled spools.
  459. const [storageLocationFilter, setStorageLocationFilter] = useState('');
  460. const [search, setSearch] = useState('');
  461. const [viewMode, setViewMode] = useState<ViewMode>('table');
  462. const [sortState, setSortState] = useState<SortState>(loadSortState);
  463. const [columnConfig, setColumnConfig] = useState<ColumnConfig[]>(loadColumnConfig);
  464. const [showColumnModal, setShowColumnModal] = useState(false);
  465. const [groupSimilar, setGroupSimilar] = useState(() => {
  466. try {
  467. return localStorage.getItem('bambuddy-inventory-group') === 'true';
  468. } catch { return false; }
  469. });
  470. const [expandedGroups, setExpandedGroups] = useState<Set<string>>(new Set());
  471. // Pagination state (pageSize persisted to localStorage)
  472. const [pageIndex, setPageIndex] = useState(0);
  473. const [pageSize, setPageSize] = useState(() => {
  474. try {
  475. const stored = localStorage.getItem('bambuddy-inventory-pageSize');
  476. if (stored) {
  477. const n = Number(stored);
  478. if ([15, 30, 50, 100, -1].includes(n)) return n;
  479. }
  480. } catch { /* ignore */ }
  481. return 15;
  482. });
  483. const { data: settings } = useQuery({
  484. queryKey: ['settings'],
  485. queryFn: api.getSettings,
  486. });
  487. const dateFormat: DateFormat = settings?.date_format || 'system';
  488. // Query key and fetch function differ based on data source
  489. const spoolsQueryKey = spoolmanMode ? ['spoolman-inventory-spools'] : ['inventory-spools'];
  490. const { data: spools, isLoading } = useQuery({
  491. queryKey: spoolsQueryKey,
  492. queryFn: () =>
  493. spoolmanMode ? api.getSpoolmanInventorySpools(true) : api.getSpools(true),
  494. refetchInterval: 30000,
  495. });
  496. // Deep-link: open edit modal for ?spool=<id>
  497. // Prefer the already-loaded spool list (no extra API call); fall back to a
  498. // targeted fetch for the rare case where the full list hasn't arrived yet.
  499. const _rawSpoolParam = searchParams.get('spool');
  500. // Only accept strings of digits representing a positive integer — guards against
  501. // NaN (Number('abc')), 0, negatives, and floats like '1.5' that would produce
  502. // an invalid path parameter and trigger unnecessary 422 responses from the API.
  503. const deepLinkSpoolId =
  504. _rawSpoolParam && /^\d+$/.test(_rawSpoolParam) && Number(_rawSpoolParam) > 0
  505. ? Number(_rawSpoolParam)
  506. : null;
  507. const deepLinkInList = spools?.find((s) => s.id === deepLinkSpoolId) ?? null;
  508. const clearDeepLinkParam = useCallback(() => {
  509. deepLinkHandled.current = true;
  510. setSearchParams((prev) => { prev.delete('spool'); return prev; }, { replace: true });
  511. }, [setSearchParams]);
  512. // Targeted fetch — only fires when mode is known and spool isn't in the list yet
  513. const { data: deepLinkSpool, isError: deepLinkFetchFailed, error: deepLinkError } = useQuery({
  514. queryKey: spoolmanMode
  515. ? ['spoolman-inventory-spool', deepLinkSpoolId]
  516. : ['inventory-spool', deepLinkSpoolId],
  517. queryFn: () =>
  518. spoolmanMode
  519. ? api.getSpoolmanInventorySpool(deepLinkSpoolId!)
  520. : api.getSpool(deepLinkSpoolId!),
  521. enabled: spoolmanModeReady && deepLinkSpoolId !== null && deepLinkInList === null,
  522. staleTime: Infinity,
  523. retry: (failureCount, error) =>
  524. failureCount < 2 && !(error instanceof ApiError && error.status === 404),
  525. });
  526. useEffect(() => {
  527. if (deepLinkHandled.current) return;
  528. // Case 1: spool is already in the fetched list
  529. if (spoolmanModeReady && deepLinkSpoolId && deepLinkInList) {
  530. clearDeepLinkParam();
  531. setFormModal({ spool: deepLinkInList, mode: 'edit' });
  532. return;
  533. }
  534. // Case 2: spool was fetched individually
  535. if (deepLinkSpool) {
  536. clearDeepLinkParam();
  537. setFormModal({ spool: deepLinkSpool, mode: 'edit' });
  538. return;
  539. }
  540. // Case 3: fetch failed
  541. if (deepLinkFetchFailed) {
  542. clearDeepLinkParam();
  543. const is404 = deepLinkError instanceof ApiError && deepLinkError.status === 404;
  544. showToast(t(is404 ? 'inventory.deepLinkSpoolNotFound' : 'inventory.deepLinkFetchFailed'), 'error');
  545. }
  546. }, [
  547. spoolmanModeReady,
  548. deepLinkSpoolId,
  549. deepLinkInList,
  550. deepLinkSpool,
  551. deepLinkFetchFailed,
  552. deepLinkError,
  553. clearDeepLinkParam,
  554. showToast,
  555. t,
  556. ]);
  557. const { data: assignments } = useQuery({
  558. queryKey: ['spool-assignments'],
  559. queryFn: () => api.getAssignments(),
  560. refetchInterval: 30000,
  561. });
  562. // Spoolman-mode slot assignments. spool.id IS the spoolman_spool_id, so this
  563. // feeds into the same assignmentMap that the LOCATION column reads.
  564. const {
  565. data: spoolmanSlotAssignments = [],
  566. isError: spoolmanSlotAssignmentsError,
  567. } = useQuery({
  568. queryKey: ['spoolman-slot-assignments-all'],
  569. queryFn: () => api.getSpoolmanSlotAssignments(),
  570. enabled: spoolmanMode,
  571. refetchInterval: 30000,
  572. staleTime: 10000,
  573. retry: 1,
  574. });
  575. // Surface a single toast when the slot-assignment endpoint goes down — the
  576. // LOCATION column would otherwise silently show "-" for every Spoolman spool.
  577. // useRef guard prevents repeated toasts during refetchInterval polls.
  578. const slotErrorToastShown = useRef(false);
  579. useEffect(() => {
  580. if (spoolmanSlotAssignmentsError && !slotErrorToastShown.current) {
  581. slotErrorToastShown.current = true;
  582. showToast(t('inventory.spoolmanUnreachable'), 'error');
  583. } else if (!spoolmanSlotAssignmentsError) {
  584. slotErrorToastShown.current = false;
  585. }
  586. }, [spoolmanSlotAssignmentsError, showToast, t]);
  587. const { data: catalogEntries } = useQuery({
  588. queryKey: ['spool-catalog'],
  589. queryFn: () => api.getSpoolCatalog(),
  590. });
  591. const deleteMutation = useMutation({
  592. mutationFn: (id: number) =>
  593. spoolmanMode ? api.deleteSpoolmanInventorySpool(id) : api.deleteSpool(id),
  594. onSuccess: () => {
  595. queryClient.invalidateQueries({ queryKey: spoolsQueryKey });
  596. showToast(t('inventory.spoolDeleted'), 'success');
  597. },
  598. onError: (error: Error) => {
  599. if (error instanceof ApiError && error.status === 404) {
  600. showToast(t('inventory.deleteSpoolNotFound'), 'error');
  601. } else if (error instanceof ApiError && error.status === 503) {
  602. showToast(t('inventory.spoolmanUnreachable'), 'error');
  603. } else {
  604. showToast(t('inventory.deleteFailed'), 'error');
  605. }
  606. },
  607. });
  608. const archiveMutation = useMutation({
  609. mutationFn: (id: number) =>
  610. spoolmanMode ? api.archiveSpoolmanInventorySpool(id) : api.archiveSpool(id),
  611. onSuccess: () => {
  612. queryClient.invalidateQueries({ queryKey: spoolsQueryKey });
  613. showToast(t('inventory.spoolArchived'), 'success');
  614. },
  615. onError: (error: Error) => {
  616. if (error instanceof ApiError && error.status === 404) {
  617. showToast(t('inventory.archiveSpoolNotFound'), 'error');
  618. } else if (error instanceof ApiError && error.status === 503) {
  619. showToast(t('inventory.spoolmanUnreachable'), 'error');
  620. } else {
  621. showToast(t('inventory.archiveFailed'), 'error');
  622. }
  623. },
  624. });
  625. const restoreMutation = useMutation({
  626. mutationFn: (id: number) =>
  627. spoolmanMode ? api.restoreSpoolmanInventorySpool(id) : api.restoreSpool(id),
  628. onSuccess: () => {
  629. queryClient.invalidateQueries({ queryKey: spoolsQueryKey });
  630. showToast(t('inventory.spoolRestored'), 'success');
  631. },
  632. onError: (error: Error) => {
  633. if (error instanceof ApiError && error.status === 404) {
  634. showToast(t('inventory.restoreSpoolNotFound'), 'error');
  635. } else if (error instanceof ApiError && error.status === 503) {
  636. showToast(t('inventory.spoolmanUnreachable'), 'error');
  637. } else {
  638. showToast(t('inventory.restoreFailed'), 'error');
  639. }
  640. },
  641. });
  642. const resetUsageMutation = useMutation({
  643. mutationFn: (id: number) =>
  644. spoolmanMode ? api.resetSpoolmanInventorySpoolUsage(id) : api.resetSpoolUsage(id),
  645. onSuccess: () => {
  646. queryClient.invalidateQueries({ queryKey: spoolsQueryKey });
  647. showToast(t('inventory.usageReset'), 'success');
  648. },
  649. onError: () => {
  650. showToast(t('inventory.resetUsageFailed'), 'error');
  651. },
  652. });
  653. const bulkResetUsageMutation = useMutation({
  654. mutationFn: (ids: number[]) =>
  655. spoolmanMode ? api.bulkResetSpoolmanInventorySpoolUsage(ids) : api.bulkResetSpoolUsage(ids),
  656. onSuccess: (data) => {
  657. queryClient.invalidateQueries({ queryKey: spoolsQueryKey });
  658. showToast(t('inventory.allUsageReset', { count: data.reset }), 'success');
  659. },
  660. onError: () => {
  661. showToast(t('inventory.resetUsageFailed'), 'error');
  662. },
  663. });
  664. const activeSpoolIds = useMemo(
  665. () => (spools ?? []).filter((s) => !s.archived_at).map((s) => s.id),
  666. [spools],
  667. );
  668. const handleSyncWeight = async (spool: InventorySpool) => {
  669. if (spool.last_scale_weight == null) return;
  670. try {
  671. if (spoolmanMode) {
  672. await api.syncSpoolmanSpoolWeight(spool.id, spool.last_scale_weight);
  673. } else {
  674. await spoolbuddyApi.updateSpoolWeight(spool.id, spool.last_scale_weight);
  675. }
  676. queryClient.invalidateQueries({ queryKey: spoolsQueryKey });
  677. const spoolName = [spool.brand, spool.material, spool.color_name].filter(Boolean).join(' ');
  678. showToast(`Synced "${spoolName}" to scale weight`, 'success');
  679. } catch (e) {
  680. const is404 = e instanceof ApiError && e.status === 404;
  681. const is503 = e instanceof ApiError && e.status === 503;
  682. if (is404) showToast(t('inventory.syncWeightSpoolNotFound'), 'error');
  683. else if (is503) showToast(t('inventory.syncWeightSpoolmanUnreachable'), 'error');
  684. else showToast(t('inventory.syncWeightFailed'), 'error');
  685. }
  686. };
  687. // Low stock threshold from backend settings
  688. const lowStockThreshold = settings?.low_stock_threshold ?? 20;
  689. const [showThresholdInput, setShowThresholdInput] = useState(false);
  690. const [thresholdInput, setThresholdInput] = useState(lowStockThreshold.toString());
  691. // Sync thresholdInput when lowStockThreshold changes and input is not shown
  692. useEffect(() => {
  693. if (!showThresholdInput) {
  694. setThresholdInput(lowStockThreshold.toString());
  695. }
  696. }, [lowStockThreshold, showThresholdInput]);
  697. const updateThresholdMutation = useMutation({
  698. mutationFn: (threshold: number) => api.updateSettings({ low_stock_threshold: threshold }),
  699. onSuccess: () => {
  700. queryClient.invalidateQueries({ queryKey: ['settings'] });
  701. showToast(t('common.saved'), 'success');
  702. setShowThresholdInput(false);
  703. },
  704. onError: () => {
  705. showToast(t('inventory.lowStockThresholdError'), 'error');
  706. },
  707. });
  708. // Stats calculation (active spools only)
  709. const stats = useMemo(() => {
  710. if (!spools) return null;
  711. let totalWeight = 0;
  712. let totalConsumed = 0;
  713. let lowStock = 0;
  714. let activeCount = 0;
  715. const byMaterial: Record<string, { count: number; weight: number }> = {};
  716. for (const s of spools) {
  717. if (s.archived_at) continue;
  718. activeCount++;
  719. const remaining = Math.max(0, s.label_weight - s.weight_used);
  720. totalWeight += remaining;
  721. // "Total Consumed" is the resettable counter (weight_used - baseline)
  722. // rather than raw weight_used so the per-spool / bulk eraser zeroes
  723. // the stat without inflating remaining back to label_weight (#1390).
  724. totalConsumed += Math.max(0, s.weight_used - (s.weight_used_baseline ?? 0));
  725. const pct = s.label_weight > 0 ? (remaining / s.label_weight) * 100 : 0;
  726. const threshold = s.low_stock_threshold_pct ?? lowStockThreshold;
  727. if (pct < threshold) lowStock++;
  728. const mat = s.material || 'Unknown';
  729. if (!byMaterial[mat]) byMaterial[mat] = { count: 0, weight: 0 };
  730. byMaterial[mat].count++;
  731. byMaterial[mat].weight += remaining;
  732. }
  733. return { totalWeight, totalConsumed, lowStock, byMaterial, totalSpools: activeCount };
  734. }, [spools, lowStockThreshold]);
  735. const inPrinterCount =
  736. (assignments?.length ?? 0) + (spoolmanMode ? spoolmanSlotAssignments.length : 0);
  737. const currencySymbol = getCurrencySymbol(settings?.currency || 'USD');
  738. // Map spool_id -> location display data for the LOCATION column.
  739. // Local SpoolAssignment entries first, then Spoolman SlotAssignment fills in
  740. // remaining IDs. Local wins on collision (defensive — modes are exclusive in
  741. // practice, but a stray pair with the same numeric id would otherwise be
  742. // unpredictable). spool.id IS the spoolman_spool_id in Spoolman mode.
  743. const assignmentMap = useMemo<Record<number, LocationDisplay>>(() => {
  744. const map: Record<number, LocationDisplay> = {};
  745. for (const a of assignments || []) {
  746. map[a.spool_id] = {
  747. printer_id: a.printer_id,
  748. printer_name: a.printer_name,
  749. ams_id: a.ams_id,
  750. tray_id: a.tray_id,
  751. ams_label: a.ams_label ?? null,
  752. };
  753. }
  754. for (const a of spoolmanSlotAssignments) {
  755. // Defensive: skip malformed entries (missing or invalid spool id, ams id,
  756. // tray id). The Pydantic response model on the backend should already
  757. // reject these, but MITM proxies and stale CDN responses can drop fields.
  758. if (
  759. typeof a?.spoolman_spool_id !== 'number' ||
  760. a.spoolman_spool_id <= 0 ||
  761. typeof a.printer_id !== 'number' ||
  762. typeof a.ams_id !== 'number' ||
  763. typeof a.tray_id !== 'number'
  764. ) continue;
  765. if (!map[a.spoolman_spool_id]) {
  766. map[a.spoolman_spool_id] = {
  767. printer_id: a.printer_id,
  768. printer_name: a.printer_name ?? null,
  769. ams_id: a.ams_id,
  770. tray_id: a.tray_id,
  771. ams_label: a.ams_label ?? null,
  772. };
  773. }
  774. }
  775. return map;
  776. }, [assignments, spoolmanSlotAssignments]);
  777. // Map catalog_id -> catalog entry for spool name column
  778. const catalogMap = useMemo(() => {
  779. const map: Record<number, SpoolCatalogEntry> = {};
  780. for (const e of catalogEntries || []) {
  781. map[e.id] = e;
  782. }
  783. return map;
  784. }, [catalogEntries]);
  785. // Top materials by weight for stat card pills
  786. const topMaterials = useMemo(() => {
  787. if (!stats) return [];
  788. return Object.entries(stats.byMaterial)
  789. .sort((a, b) => b[1].weight - a[1].weight)
  790. .slice(0, 4);
  791. }, [stats]);
  792. // Filtering pipeline
  793. const filteredSpools = useMemo(() => {
  794. let filtered = spools || [];
  795. // Archive filter
  796. if (archiveFilter === 'active') {
  797. filtered = filtered.filter((s) => !s.archived_at);
  798. } else {
  799. filtered = filtered.filter((s) => !!s.archived_at);
  800. }
  801. // Usage filter
  802. if (usageFilter === 'used') {
  803. filtered = filtered.filter((s) => s.weight_used > 0);
  804. } else if (usageFilter === 'new') {
  805. filtered = filtered.filter((s) => s.weight_used === 0);
  806. } else if (usageFilter === 'lowstock') {
  807. filtered = filtered.filter((s) => {
  808. const remaining = Math.max(0, s.label_weight - s.weight_used);
  809. const pct = s.label_weight > 0 ? (remaining / s.label_weight) * 100 : 0;
  810. const threshold = s.low_stock_threshold_pct ?? lowStockThreshold;
  811. return pct < threshold;
  812. });
  813. }
  814. // Material dropdown
  815. if (materialFilter) {
  816. filtered = filtered.filter((s) => s.material === materialFilter);
  817. }
  818. // Brand dropdown
  819. if (brandFilter) {
  820. filtered = filtered.filter((s) => s.brand === brandFilter);
  821. }
  822. // Category dropdown (#729)
  823. if (categoryFilter) {
  824. if (categoryFilter === '__none__') {
  825. filtered = filtered.filter((s) => !s.category);
  826. } else {
  827. filtered = filtered.filter((s) => s.category === categoryFilter);
  828. }
  829. }
  830. // Spool name dropdown
  831. if (spoolFilter) {
  832. const catalogId = Number(spoolFilter);
  833. filtered = filtered.filter((s) => s.core_weight_catalog_id === catalogId);
  834. }
  835. // Storage location dropdown (#1400). `__none__` lets the user find
  836. // spools that haven't been assigned a storage location yet.
  837. if (storageLocationFilter) {
  838. if (storageLocationFilter === '__none__') {
  839. filtered = filtered.filter((s) => !s.storage_location?.trim());
  840. } else {
  841. filtered = filtered.filter((s) => s.storage_location?.trim() === storageLocationFilter);
  842. }
  843. }
  844. // Stock filter
  845. if (stockFilter === 'stock') {
  846. filtered = filtered.filter((s) => !s.slicer_filament);
  847. } else if (stockFilter === 'configured') {
  848. filtered = filtered.filter((s) => !!s.slicer_filament);
  849. }
  850. // Global search
  851. if (search) {
  852. filtered = filterSpoolsByQuery(filtered, search);
  853. }
  854. return filtered;
  855. }, [spools, archiveFilter, usageFilter, materialFilter, brandFilter, categoryFilter, spoolFilter, stockFilter, storageLocationFilter, search, lowStockThreshold]);
  856. // Reset page on filter changes
  857. const resetPage = () => setPageIndex(0);
  858. // Unique values for filter dropdowns
  859. const uniqueMaterials = [...new Set(spools?.map((s) => s.material) || [])].sort();
  860. const uniqueBrands = [...new Set(spools?.map((s) => s.brand).filter(Boolean) || [])].sort() as string[];
  861. const uniqueCategories = [...new Set(spools?.map((s) => s.category?.trim()).filter(Boolean) as string[] || [])].sort();
  862. const hasUncategorized = (spools ?? []).some((s) => !s.category);
  863. const uniqueSpoolCatalogIds = [...new Set(spools?.map((s) => s.core_weight_catalog_id).filter((id): id is number => id != null) || [])].sort((a, b) => {
  864. const nameA = (catalogMap[a]?.name || '').toLowerCase();
  865. const nameB = (catalogMap[b]?.name || '').toLowerCase();
  866. return nameA.localeCompare(nameB);
  867. });
  868. // #1400: storage-location distinct values. `.trim()` so accidental
  869. // trailing whitespace doesn't show up as a separate option.
  870. const uniqueStorageLocations = [...new Set(spools?.map((s) => s.storage_location?.trim()).filter(Boolean) as string[] || [])].sort();
  871. const hasUnsetStorageLocation = (spools ?? []).some((s) => !s.storage_location?.trim());
  872. // Check if any filters are non-default
  873. const hasActiveFilters = archiveFilter !== 'active' || usageFilter !== 'all' || !!materialFilter || !!brandFilter || !!categoryFilter || !!spoolFilter || !!storageLocationFilter || stockFilter !== 'all' || !!search;
  874. const handleColumnConfigSave = (config: ColumnConfig[]) => {
  875. setColumnConfig(config);
  876. saveColumnConfig(config);
  877. };
  878. // Visible column IDs in order
  879. const visibleColumns = useMemo(
  880. () => columnConfig.filter((c) => c.visible).map((c) => c.id),
  881. [columnConfig]
  882. );
  883. const handleSort = (colId: string) => {
  884. if (!columnSortValues[colId]) return; // Not sortable
  885. setSortState((prev) => {
  886. let next: SortState;
  887. if (prev?.column === colId) {
  888. // Toggle direction, or clear on third click
  889. next = prev.direction === 'asc' ? { column: colId, direction: 'desc' } : null;
  890. } else {
  891. next = { column: colId, direction: 'asc' };
  892. }
  893. saveSortState(next);
  894. return next;
  895. });
  896. resetPage();
  897. };
  898. // Sort filtered spools
  899. const sortedSpools = useMemo(() => {
  900. if (!sortState) return filteredSpools;
  901. const extractor = columnSortValues[sortState.column];
  902. if (!extractor) return filteredSpools;
  903. const sorted = [...filteredSpools].sort((a, b) => {
  904. const va = extractor(a, assignmentMap);
  905. const vb = extractor(b, assignmentMap);
  906. if (va < vb) return sortState.direction === 'asc' ? -1 : 1;
  907. if (va > vb) return sortState.direction === 'asc' ? 1 : -1;
  908. return 0;
  909. });
  910. return sorted;
  911. }, [filteredSpools, sortState, assignmentMap]);
  912. // Group similar spools when toggle is active
  913. const displayItems = useMemo((): DisplayItem[] => {
  914. if (!groupSimilar) return sortedSpools.map((s) => ({ type: 'single' as const, spool: s }));
  915. const groups = new Map<string, InventorySpool[]>();
  916. for (const spool of sortedSpools) {
  917. // Only group unused & unassigned spools
  918. if (spool.weight_used > 0 || assignmentMap[spool.id]) {
  919. // Will be added as singles in the walk below
  920. } else {
  921. const key = spoolGroupKey(spool);
  922. const arr = groups.get(key);
  923. if (arr) arr.push(spool);
  924. else groups.set(key, [spool]);
  925. }
  926. }
  927. const items: DisplayItem[] = [];
  928. const processedKeys = new Set<string>();
  929. // Walk sortedSpools order so groups appear at the position of their first member
  930. for (const spool of sortedSpools) {
  931. if (spool.weight_used > 0 || assignmentMap[spool.id]) {
  932. items.push({ type: 'single', spool });
  933. continue;
  934. }
  935. const key = spoolGroupKey(spool);
  936. if (processedKeys.has(key)) continue;
  937. processedKeys.add(key);
  938. const members = groups.get(key)!;
  939. if (members.length === 1) {
  940. items.push({ type: 'single', spool: members[0] });
  941. } else {
  942. items.push({ type: 'group', key, spools: members, representative: members[0] });
  943. }
  944. }
  945. return items;
  946. }, [sortedSpools, groupSimilar, assignmentMap]);
  947. // Pagination (after sorting) — pageSize -1 means "All"
  948. const showAll = pageSize === -1;
  949. const totalDisplayItems = displayItems.length;
  950. const effectivePageSize = showAll ? totalDisplayItems || 1 : pageSize;
  951. const totalPages = Math.max(1, Math.ceil(totalDisplayItems / effectivePageSize));
  952. const safePageIndex = showAll ? 0 : Math.min(pageIndex, totalPages - 1);
  953. const pagedItems = showAll
  954. ? displayItems
  955. : displayItems.slice(safePageIndex * effectivePageSize, (safePageIndex + 1) * effectivePageSize);
  956. const toggleGroupSimilar = () => {
  957. const next = !groupSimilar;
  958. setGroupSimilar(next);
  959. setExpandedGroups(new Set());
  960. resetPage();
  961. try { localStorage.setItem('bambuddy-inventory-group', String(next)); } catch { /* ignore */ }
  962. };
  963. const toggleGroupExpand = (key: string) => {
  964. setExpandedGroups((prev) => {
  965. const next = new Set(prev);
  966. if (next.has(key)) next.delete(key);
  967. else next.add(key);
  968. return next;
  969. });
  970. };
  971. const handlePageSizeChange = (size: number) => {
  972. setPageSize(size);
  973. setPageIndex(0);
  974. try { localStorage.setItem('bambuddy-inventory-pageSize', String(size)); } catch { /* ignore */ }
  975. };
  976. const clearAllFilters = () => {
  977. setArchiveFilter('active');
  978. setUsageFilter('all');
  979. setMaterialFilter('');
  980. setBrandFilter('');
  981. setCategoryFilter('');
  982. setSpoolFilter('');
  983. setStorageLocationFilter('');
  984. setStockFilter('all');
  985. setSearch('');
  986. resetPage();
  987. };
  988. return (
  989. <div className="p-4 md:p-8 space-y-6">
  990. {/* Header */}
  991. <div className="flex items-center justify-between">
  992. <div>
  993. <h1 className="text-2xl font-bold text-white flex items-center gap-3">
  994. <Package className="w-7 h-7 text-bambu-green" />
  995. {t('inventory.title')}
  996. </h1>
  997. <p className="text-bambu-gray mt-1">{t('inventory.subtitle')}</p>
  998. </div>
  999. <div className="flex items-center gap-2">
  1000. <Button
  1001. variant="secondary"
  1002. disabled={filteredSpools.length === 0}
  1003. // Pre-select every visible spool so the user lands in "all
  1004. // checked", then refines downward in the modal. Per-card icon
  1005. // pre-selects only that spool — both flows share the same picker.
  1006. onClick={() => setLabelPickerSpoolIds(filteredSpools.map((s) => s.id))}
  1007. title={
  1008. filteredSpools.length === 0
  1009. ? t('inventory.labels.noSpoolsTitle', 'No spools to label')
  1010. : t('inventory.labels.bulkTitle', 'Pick spools to print labels for from the {{count}} currently shown', { count: filteredSpools.length })
  1011. }
  1012. >
  1013. <Printer className="w-4 h-4" />
  1014. {t('inventory.labels.printLabels', 'Print labels…')}
  1015. </Button>
  1016. <Button onClick={() => setFormModal({ spool: null, mode: 'create' })}>
  1017. <Plus className="w-4 h-4" />
  1018. {t('inventory.addSpool')}
  1019. </Button>
  1020. </div>
  1021. </div>
  1022. {/* Stats Bar */}
  1023. {stats && !isLoading && (
  1024. <div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-3">
  1025. {/* Total Inventory */}
  1026. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  1027. <div className="flex items-center gap-2 mb-1">
  1028. <Package className="w-4 h-4 text-bambu-green" />
  1029. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.totalInventory')}</span>
  1030. </div>
  1031. <div className="text-xl font-bold text-white">{formatWeight(stats.totalWeight, true)}</div>
  1032. <div className="text-xs text-bambu-gray mt-1">{stats.totalSpools} {stats.totalSpools !== 1 ? t('inventory.spools') : t('inventory.spool')}</div>
  1033. </div>
  1034. {/* Total Consumed */}
  1035. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  1036. <div className="flex items-center justify-between gap-2 mb-1">
  1037. <div className="flex items-center gap-2">
  1038. <TrendingDown className="w-4 h-4 text-blue-400" />
  1039. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.totalConsumed')}</span>
  1040. </div>
  1041. {stats.totalConsumed > 0 && activeSpoolIds.length > 0 && (
  1042. <button
  1043. onClick={() => setConfirmAction({ type: 'reset-all-usage' })}
  1044. className="p-1 text-bambu-gray hover:text-red-400 rounded transition-colors"
  1045. title={t('inventory.resetAllUsageTooltip')}
  1046. aria-label={t('inventory.resetAllUsage')}
  1047. >
  1048. <Eraser className="w-3.5 h-3.5" />
  1049. </button>
  1050. )}
  1051. </div>
  1052. <div className="text-xl font-bold text-white">{formatWeight(stats.totalConsumed, true)}</div>
  1053. <div className="text-xs text-bambu-gray mt-1">{t('inventory.sinceTracking')}</div>
  1054. </div>
  1055. {/* By Material */}
  1056. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  1057. <div className="flex items-center gap-2 mb-1">
  1058. <Layers className="w-4 h-4 text-green-400" />
  1059. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.byMaterial')}</span>
  1060. </div>
  1061. <div className="flex flex-wrap gap-1.5 mt-1">
  1062. {topMaterials.map(([mat, data]) => (
  1063. <span
  1064. key={mat}
  1065. 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'}`}
  1066. >
  1067. {mat} <span className="opacity-70">{formatWeight(data.weight, true)}</span>
  1068. </span>
  1069. ))}
  1070. </div>
  1071. </div>
  1072. {/* In Printer */}
  1073. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  1074. <div className="flex items-center gap-2 mb-1">
  1075. <Printer className="w-4 h-4 text-purple-400" />
  1076. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.inPrinter')}</span>
  1077. </div>
  1078. <div className="text-xl font-bold text-white">{inPrinterCount}</div>
  1079. <div className="text-xs text-bambu-gray mt-1">{t('inventory.loadedInAms')}</div>
  1080. </div>
  1081. {/* Low Stock */}
  1082. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  1083. <div className="flex items-center gap-2 mb-1">
  1084. <AlertTriangle className="w-4 h-4 text-yellow-400" />
  1085. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.lowStock')}</span>
  1086. </div>
  1087. <div className={`text-xl font-bold ${stats.lowStock > 0 ? 'text-yellow-400' : 'text-white'}`}>{stats.lowStock}</div>
  1088. <div className="text-xs text-bambu-gray mt-1 flex items-center gap-2">
  1089. {showThresholdInput ? (
  1090. <form
  1091. onSubmit={e => {
  1092. e.preventDefault();
  1093. const val = parseFloat(thresholdInput);
  1094. if (!isNaN(val) && val >= 0.1 && val <= 99.9) {
  1095. updateThresholdMutation.mutate(val);
  1096. } else {
  1097. showToast(t('inventory.lowStockThresholdError'), 'error');
  1098. }
  1099. }}
  1100. className="flex items-center gap-2"
  1101. >
  1102. <span className="text-xs text-bambu-gray">{'<'}</span>
  1103. <input
  1104. type="text"
  1105. inputMode="decimal"
  1106. pattern="^\d{0,2}(\.\d?)?$"
  1107. maxLength={4}
  1108. value={thresholdInput}
  1109. onChange={e => {
  1110. // Only allow up to 2 digits before decimal and 1 after
  1111. const val = e.target.value.replace(/[^\d.]/g, '');
  1112. if (/^\d{0,2}(\.\d?)?$/.test(val)) {
  1113. setThresholdInput(val);
  1114. }
  1115. }}
  1116. 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"
  1117. onWheel={e => e.currentTarget.blur()}
  1118. disabled={updateThresholdMutation.isPending}
  1119. />
  1120. <span className="text-xs text-bambu-gray">%</span>
  1121. <Button type="submit" size="sm" disabled={updateThresholdMutation.isPending}>{t('common.save')}</Button>
  1122. <Button type="button" size="sm" variant="ghost" onClick={() => setShowThresholdInput(false)} disabled={updateThresholdMutation.isPending}>{t('common.cancel')}</Button>
  1123. </form>
  1124. ) : (
  1125. <>
  1126. <span className="text-bambu-gray">{'< '}{lowStockThreshold}%</span>
  1127. <button
  1128. className="p-1.5 text-bambu-gray hover:text-white rounded transition-colors"
  1129. title={t('common.edit')}
  1130. onClick={() => {
  1131. setThresholdInput(lowStockThreshold.toString());
  1132. setShowThresholdInput(true);
  1133. }}
  1134. >
  1135. <Edit2 className="w-4 h-4" />
  1136. </button>
  1137. </>
  1138. )}
  1139. </div>
  1140. </div>
  1141. </div>
  1142. )}
  1143. {/* Toolbar: Search + View toggle */}
  1144. <div className="flex flex-col sm:flex-row gap-3 items-start sm:items-center justify-between">
  1145. <div className={`relative flex-1 max-w-md ${viewMode === 'forecast' ? 'invisible pointer-events-none' : ''}`}>
  1146. <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50" />
  1147. <input
  1148. type="text"
  1149. value={search}
  1150. onChange={(e) => { setSearch(e.target.value); resetPage(); }}
  1151. placeholder={t('inventory.search')}
  1152. 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"
  1153. />
  1154. {search && (
  1155. <button
  1156. onClick={() => { setSearch(''); resetPage(); }}
  1157. className="absolute right-3 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white"
  1158. >
  1159. <X className="w-4 h-4" />
  1160. </button>
  1161. )}
  1162. </div>
  1163. <div className="flex items-center gap-2">
  1164. {/* Columns button (table view only) */}
  1165. {viewMode === 'table' && (
  1166. <button
  1167. onClick={() => setShowColumnModal(true)}
  1168. 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"
  1169. title={t('inventory.configureColumns')}
  1170. >
  1171. <Columns className="w-4 h-4" />
  1172. <span className="hidden sm:inline">{t('inventory.columns')}</span>
  1173. </button>
  1174. )}
  1175. {/* Group similar toggle — hidden in forecast mode */}
  1176. {viewMode !== 'forecast' && (
  1177. <button
  1178. onClick={toggleGroupSimilar}
  1179. className={`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium border rounded-lg transition-colors ${
  1180. groupSimilar
  1181. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  1182. : 'text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  1183. }`}
  1184. title={t('inventory.groupSimilar')}
  1185. >
  1186. <Group className="w-4 h-4" />
  1187. <span className="hidden sm:inline">{t('inventory.groupSimilar')}</span>
  1188. </button>
  1189. )}
  1190. {/* Table / Cards toggle */}
  1191. <div className="flex bg-bambu-dark-primary border border-bambu-dark-tertiary rounded-lg overflow-hidden">
  1192. <button
  1193. onClick={() => setViewMode('table')}
  1194. className={`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${
  1195. viewMode === 'table'
  1196. ? 'bg-bambu-green text-white'
  1197. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1198. }`}
  1199. >
  1200. <TableProperties className="w-4 h-4" />
  1201. <span className="hidden sm:inline">{t('inventory.table')}</span>
  1202. </button>
  1203. <button
  1204. onClick={() => setViewMode('cards')}
  1205. className={`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${
  1206. viewMode === 'cards'
  1207. ? 'bg-bambu-green text-white'
  1208. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1209. }`}
  1210. >
  1211. <LayoutGrid className="w-4 h-4" />
  1212. <span className="hidden sm:inline">{t('inventory.cards')}</span>
  1213. </button>
  1214. <button
  1215. onClick={() => canViewForecast && setViewMode('forecast')}
  1216. disabled={!canViewForecast}
  1217. title={canViewForecast ? undefined : t('forecast.noReadAccess')}
  1218. className={`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors disabled:opacity-40 disabled:cursor-not-allowed ${
  1219. viewMode === 'forecast'
  1220. ? 'bg-bambu-green text-white'
  1221. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1222. }`}
  1223. >
  1224. {canViewForecast ? <TrendingUp className="w-4 h-4" /> : <Lock className="w-4 h-4" />}
  1225. <span className="hidden sm:inline">{t('forecast.title')}</span>
  1226. </button>
  1227. </div>
  1228. </div>
  1229. </div>
  1230. {/* Filter chips row — hidden in forecast mode */}
  1231. <div className={`flex flex-wrap items-center gap-2 ${viewMode === 'forecast' ? 'hidden' : ''}`}>
  1232. {/* Active / Archived chips */}
  1233. <div className="flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden">
  1234. <button
  1235. onClick={() => { setArchiveFilter('active'); resetPage(); }}
  1236. className={`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${
  1237. archiveFilter === 'active'
  1238. ? 'bg-bambu-green/20 text-bambu-green'
  1239. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1240. }`}
  1241. >
  1242. <Package className="w-3.5 h-3.5" />
  1243. {t('inventory.active')}
  1244. </button>
  1245. <button
  1246. onClick={() => { setArchiveFilter('archived'); resetPage(); }}
  1247. className={`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${
  1248. archiveFilter === 'archived'
  1249. ? 'bg-bambu-green/20 text-bambu-green'
  1250. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1251. }`}
  1252. >
  1253. <Archive className="w-3.5 h-3.5" />
  1254. {t('inventory.archived')}
  1255. </button>
  1256. </div>
  1257. <div className="w-px h-5 bg-bambu-dark-tertiary" />
  1258. {/* All / Used / New chips */}
  1259. <div className="flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden">
  1260. <button
  1261. onClick={() => { setUsageFilter('all'); resetPage(); }}
  1262. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  1263. usageFilter === 'all'
  1264. ? 'bg-bambu-green/20 text-bambu-green'
  1265. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1266. }`}
  1267. >
  1268. {t('inventory.all')}
  1269. </button>
  1270. <button
  1271. onClick={() => { setUsageFilter('used'); resetPage(); }}
  1272. className={`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${
  1273. usageFilter === 'used'
  1274. ? 'bg-bambu-green/20 text-bambu-green'
  1275. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1276. }`}
  1277. >
  1278. <Clock className="w-3.5 h-3.5" />
  1279. {t('inventory.used')}
  1280. </button>
  1281. <button
  1282. onClick={() => { setUsageFilter('new'); resetPage(); }}
  1283. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  1284. usageFilter === 'new'
  1285. ? 'bg-bambu-green/20 text-bambu-green'
  1286. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1287. }`}
  1288. >
  1289. {t('inventory.new')}
  1290. </button>
  1291. <button
  1292. onClick={() => { setUsageFilter('lowstock'); resetPage(); }}
  1293. className={`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${
  1294. usageFilter === 'lowstock'
  1295. ? 'bg-yellow-500/20 text-yellow-400'
  1296. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1297. }`}
  1298. >
  1299. <AlertTriangle className="w-3.5 h-3.5" />
  1300. {t('inventory.lowStock')}
  1301. </button>
  1302. </div>
  1303. {/* Stock filter chips */}
  1304. <div className="flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden">
  1305. <button
  1306. onClick={() => { setStockFilter('all'); resetPage(); }}
  1307. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  1308. stockFilter === 'all'
  1309. ? 'bg-bambu-green/20 text-bambu-green'
  1310. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1311. }`}
  1312. >
  1313. {t('inventory.all')}
  1314. </button>
  1315. <button
  1316. onClick={() => { setStockFilter('stock'); resetPage(); }}
  1317. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  1318. stockFilter === 'stock'
  1319. ? 'bg-amber-500/20 text-amber-400'
  1320. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1321. }`}
  1322. >
  1323. {t('inventory.stock')}
  1324. </button>
  1325. <button
  1326. onClick={() => { setStockFilter('configured'); resetPage(); }}
  1327. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  1328. stockFilter === 'configured'
  1329. ? 'bg-bambu-green/20 text-bambu-green'
  1330. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1331. }`}
  1332. >
  1333. {t('inventory.configured')}
  1334. </button>
  1335. </div>
  1336. <div className="w-px h-5 bg-bambu-dark-tertiary" />
  1337. {/* Material dropdown chip */}
  1338. <select
  1339. value={materialFilter}
  1340. onChange={(e) => { setMaterialFilter(e.target.value); resetPage(); }}
  1341. className={`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${
  1342. materialFilter
  1343. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  1344. : 'bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  1345. }`}
  1346. >
  1347. <option value="">{t('inventory.material')}</option>
  1348. {uniqueMaterials.map((m) => (
  1349. <option key={m} value={m}>{m}</option>
  1350. ))}
  1351. </select>
  1352. {/* Brand dropdown chip */}
  1353. <select
  1354. value={brandFilter}
  1355. onChange={(e) => { setBrandFilter(e.target.value); resetPage(); }}
  1356. className={`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${
  1357. brandFilter
  1358. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  1359. : 'bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  1360. }`}
  1361. >
  1362. <option value="">{t('inventory.brand')}</option>
  1363. {uniqueBrands.map((b) => (
  1364. <option key={b} value={b}>{b}</option>
  1365. ))}
  1366. </select>
  1367. {/* Category dropdown chip (#729) — only render once at least one
  1368. spool carries a category, otherwise it's noise. */}
  1369. {(uniqueCategories.length > 0 || categoryFilter) && (
  1370. <select
  1371. value={categoryFilter}
  1372. onChange={(e) => { setCategoryFilter(e.target.value); resetPage(); }}
  1373. className={`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${
  1374. categoryFilter
  1375. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  1376. : 'bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  1377. }`}
  1378. >
  1379. <option value="">{t('inventory.category')}</option>
  1380. {uniqueCategories.map((c) => (
  1381. <option key={c} value={c}>{c}</option>
  1382. ))}
  1383. {hasUncategorized && (
  1384. <option value="__none__">{t('inventory.categoryNone')}</option>
  1385. )}
  1386. </select>
  1387. )}
  1388. {/* Spool name dropdown chip */}
  1389. {uniqueSpoolCatalogIds.length > 0 && (
  1390. <select
  1391. value={spoolFilter}
  1392. onChange={(e) => { setSpoolFilter(e.target.value); resetPage(); }}
  1393. className={`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${
  1394. spoolFilter
  1395. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  1396. : 'bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  1397. }`}
  1398. >
  1399. <option value="">{t('inventory.spoolName')}</option>
  1400. {uniqueSpoolCatalogIds.map((id) => (
  1401. <option key={id} value={id}>{catalogMap[id]?.name || `#${id}`}</option>
  1402. ))}
  1403. </select>
  1404. )}
  1405. {/* Storage location dropdown chip (#1400) — only render when at
  1406. least one spool carries a storage location, otherwise it's noise
  1407. (matches the category chip pattern). */}
  1408. {(uniqueStorageLocations.length > 0 || storageLocationFilter) && (
  1409. <select
  1410. value={storageLocationFilter}
  1411. onChange={(e) => { setStorageLocationFilter(e.target.value); resetPage(); }}
  1412. className={`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${
  1413. storageLocationFilter
  1414. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  1415. : 'bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  1416. }`}
  1417. >
  1418. <option value="">{t('inventory.storageLocation')}</option>
  1419. {uniqueStorageLocations.map((loc) => (
  1420. <option key={loc} value={loc}>{loc}</option>
  1421. ))}
  1422. {hasUnsetStorageLocation && (
  1423. <option value="__none__">{t('inventory.storageLocationNone')}</option>
  1424. )}
  1425. </select>
  1426. )}
  1427. {/* Clear filters */}
  1428. {hasActiveFilters && (
  1429. <>
  1430. <div className="w-px h-5 bg-bambu-dark-tertiary" />
  1431. <button
  1432. onClick={clearAllFilters}
  1433. className="flex items-center gap-1 text-xs text-bambu-gray hover:text-bambu-green transition-colors"
  1434. >
  1435. <X className="w-3.5 h-3.5" />
  1436. {t('inventory.clearFilters')}
  1437. </button>
  1438. </>
  1439. )}
  1440. {/* Results count — hidden in forecast mode */}
  1441. {viewMode !== 'forecast' && (
  1442. <span className="ml-auto text-xs text-bambu-gray">
  1443. {sortedSpools.length} {sortedSpools.length !== 1 ? t('inventory.spools') : t('inventory.spool')}
  1444. {groupSimilar && totalDisplayItems < sortedSpools.length && ` (${totalDisplayItems} ${t('inventory.groupedRows')})`}
  1445. </span>
  1446. )}
  1447. </div>
  1448. {/* Content */}
  1449. {isLoading ? (
  1450. <div className="flex justify-center py-16">
  1451. <Loader2 className="w-8 h-8 text-bambu-green animate-spin" />
  1452. </div>
  1453. ) : viewMode === 'forecast' ? (
  1454. /* Forecast view */
  1455. <ForecastPanel spools={spools || []} />
  1456. ) : viewMode === 'cards' ? (
  1457. /* Cards view */
  1458. pagedItems.length > 0 ? (
  1459. <>
  1460. <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
  1461. {pagedItems.map((item) => {
  1462. if (item.type === 'group') {
  1463. const { key, spools: groupSpools, representative: rep } = item;
  1464. const groupBannerStyle = buildFilamentBackground({
  1465. rgba: rep.rgba,
  1466. extraColors: rep.extra_colors,
  1467. effectType: rep.effect_type,
  1468. subtype: rep.subtype,
  1469. effectSize: 'groupheader',
  1470. });
  1471. const isExpanded = expandedGroups.has(key);
  1472. return (
  1473. <div key={`group-${key}`} className="col-span-full">
  1474. {/* Group header card */}
  1475. <div
  1476. className="bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-green/30 hover:border-bambu-green transition-colors cursor-pointer"
  1477. onClick={() => toggleGroupExpand(key)}
  1478. >
  1479. <div className="h-10 flex items-center px-4 gap-3" style={groupBannerStyle}>
  1480. <span className="bg-white/90 text-gray-800 px-3 py-0.5 rounded-full text-sm font-medium">
  1481. {resolveSpoolColorName(rep.color_name, rep.rgba) || '-'}
  1482. </span>
  1483. </div>
  1484. <div className="px-4 py-3 flex items-center justify-between">
  1485. <div className="flex items-center gap-3">
  1486. <ChevronDown className={`w-4 h-4 text-bambu-gray transition-transform ${isExpanded ? '' : '-rotate-90'}`} />
  1487. <div>
  1488. <h3 className="font-semibold text-white">{rep.material}{rep.subtype ? ` ${rep.subtype}` : ''}</h3>
  1489. <p className="text-sm text-bambu-gray">{rep.brand || '-'}</p>
  1490. </div>
  1491. </div>
  1492. <div className="flex items-center gap-2">
  1493. <span className="text-sm text-bambu-gray">{formatWeight(rep.label_weight)}</span>
  1494. <span className="text-xs font-medium bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full">
  1495. {t('inventory.groupedSpools', { count: groupSpools.length })}
  1496. </span>
  1497. </div>
  1498. </div>
  1499. </div>
  1500. {/* Expanded individual spools */}
  1501. {isExpanded && (
  1502. <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4 mt-2 ml-4">
  1503. {groupSpools.map((spool) => {
  1504. const remaining = Math.max(0, spool.label_weight - spool.weight_used);
  1505. const pct = spool.label_weight > 0 ? (remaining / spool.label_weight) * 100 : 0;
  1506. return (
  1507. <SpoolCard
  1508. key={spool.id}
  1509. spool={spool}
  1510. remaining={remaining}
  1511. pct={pct}
  1512. onClick={() => setFormModal({ spool, mode: 'edit' })}
  1513. onPrintLabel={() => setLabelPickerSpoolIds([spool.id])}
  1514. onCopy={() => setFormModal({ spool: spool, mode: 'copy' })}
  1515. t={t}
  1516. />
  1517. );
  1518. })}
  1519. </div>
  1520. )}
  1521. </div>
  1522. );
  1523. }
  1524. const spool = item.spool;
  1525. const remaining = Math.max(0, spool.label_weight - spool.weight_used);
  1526. const pct = spool.label_weight > 0 ? (remaining / spool.label_weight) * 100 : 0;
  1527. return (
  1528. <SpoolCard
  1529. key={spool.id}
  1530. spool={spool}
  1531. remaining={remaining}
  1532. pct={pct}
  1533. onClick={() => setFormModal({ spool, mode: 'edit' })}
  1534. onPrintLabel={() => setLabelPickerSpoolIds([spool.id])}
  1535. onCopy={() => setFormModal({ spool: spool, mode: 'copy' })}
  1536. t={t}
  1537. />
  1538. );
  1539. })}
  1540. </div>
  1541. {/* Pagination for cards */}
  1542. <PaginationBar
  1543. pageIndex={safePageIndex}
  1544. pageSize={pageSize}
  1545. totalRows={totalDisplayItems}
  1546. totalPages={totalPages}
  1547. onPageChange={setPageIndex}
  1548. onPageSizeChange={handlePageSizeChange}
  1549. t={t}
  1550. />
  1551. </>
  1552. ) : (
  1553. <EmptyFilterState
  1554. hasFilters={hasActiveFilters}
  1555. onAddSpool={() => setFormModal({ spool: null, mode: 'create' })}
  1556. t={t}
  1557. />
  1558. )
  1559. ) : (
  1560. /* Table view */
  1561. pagedItems.length > 0 ? (
  1562. <div className="bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary">
  1563. <div className="overflow-x-auto">
  1564. <table className="w-full">
  1565. <thead>
  1566. <tr className="border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30">
  1567. {visibleColumns.map((colId) => {
  1568. const sortable = !!columnSortValues[colId];
  1569. const isActive = sortState?.column === colId;
  1570. return (
  1571. <th
  1572. key={colId}
  1573. className={`text-left py-3 px-4 text-xs font-medium uppercase tracking-wide select-none ${colId === 'remaining' ? 'min-w-[150px]' : ''} ${
  1574. sortable ? 'cursor-pointer hover:text-bambu-green transition-colors' : ''
  1575. } ${isActive ? 'text-bambu-green' : 'text-bambu-gray'}`}
  1576. onClick={sortable ? () => handleSort(colId) : undefined}
  1577. >
  1578. <span className="inline-flex items-center gap-1">
  1579. {columnHeaders[colId]?.(t) ?? colId}
  1580. {sortable && (
  1581. isActive
  1582. ? sortState.direction === 'asc'
  1583. ? <ArrowUp className="w-3 h-3" />
  1584. : <ArrowDown className="w-3 h-3" />
  1585. : <ArrowUpDown className="w-3 h-3 opacity-30" />
  1586. )}
  1587. </span>
  1588. </th>
  1589. );
  1590. })}
  1591. <th className="text-right py-3 px-4 text-xs font-medium text-bambu-gray uppercase tracking-wide">{t('common.actions')}</th>
  1592. </tr>
  1593. </thead>
  1594. <tbody>
  1595. {pagedItems.map((item) => {
  1596. if (item.type === 'group') {
  1597. const { key, spools: groupSpools, representative: rep } = item;
  1598. const isExpanded = expandedGroups.has(key);
  1599. const remaining = Math.max(0, rep.label_weight - rep.weight_used);
  1600. const pct = rep.label_weight > 0 ? (remaining / rep.label_weight) * 100 : 0;
  1601. return (
  1602. <SpoolTableGroup
  1603. key={`group-${key}`}
  1604. spools={groupSpools}
  1605. representative={rep}
  1606. remaining={remaining}
  1607. pct={pct}
  1608. isExpanded={isExpanded}
  1609. onToggle={() => toggleGroupExpand(key)}
  1610. onEdit={(s) => setFormModal({ spool: s, mode: 'edit' })}
  1611. onCopy={(s) => setFormModal({ spool: s, mode: 'copy' })}
  1612. onArchive={(id) => setConfirmAction({ type: 'archive', spoolId: id })}
  1613. onDelete={(id) => setConfirmAction({ type: 'delete', spoolId: id })}
  1614. onPrintLabel={(id) => setLabelPickerSpoolIds([id])}
  1615. onResetUsage={(id) => setConfirmAction({ type: 'reset-usage', spoolId: id })}
  1616. visibleColumns={visibleColumns}
  1617. assignmentMap={assignmentMap}
  1618. catalogMap={catalogMap}
  1619. currencySymbol={currencySymbol}
  1620. dateFormat={dateFormat}
  1621. t={t}
  1622. onSyncWeight={handleSyncWeight}
  1623. />
  1624. );
  1625. }
  1626. const spool = item.spool;
  1627. const remaining = Math.max(0, spool.label_weight - spool.weight_used);
  1628. const pct = spool.label_weight > 0 ? (remaining / spool.label_weight) * 100 : 0;
  1629. return (
  1630. <SpoolTableRow
  1631. key={spool.id}
  1632. spool={spool}
  1633. remaining={remaining}
  1634. pct={pct}
  1635. onEdit={() => setFormModal({ spool, mode: 'edit' })}
  1636. onCopy={() => setFormModal({ spool: spool, mode: 'copy' })}
  1637. onRestore={() => restoreMutation.mutate(spool.id)}
  1638. onArchive={() => setConfirmAction({ type: 'archive', spoolId: spool.id })}
  1639. onDelete={() => setConfirmAction({ type: 'delete', spoolId: spool.id })}
  1640. onPrintLabel={() => setLabelPickerSpoolIds([spool.id])}
  1641. onResetUsage={() => setConfirmAction({ type: 'reset-usage', spoolId: spool.id })}
  1642. visibleColumns={visibleColumns}
  1643. assignmentMap={assignmentMap}
  1644. catalogMap={catalogMap}
  1645. currencySymbol={currencySymbol}
  1646. dateFormat={dateFormat}
  1647. t={t}
  1648. onSyncWeight={handleSyncWeight}
  1649. />
  1650. );
  1651. })}
  1652. </tbody>
  1653. </table>
  1654. </div>
  1655. {/* Pagination inside card footer */}
  1656. <div className="flex items-center justify-between px-4 py-3 bg-bambu-dark-tertiary/50 border-t border-bambu-dark-tertiary text-sm">
  1657. <span className="text-bambu-gray">
  1658. {showAll
  1659. ? `${totalDisplayItems} ${totalDisplayItems !== 1 ? t('inventory.spools') : t('inventory.spool')}`
  1660. : <>{t('inventory.showing')} {safePageIndex * effectivePageSize + 1} {t('inventory.to')}{' '}
  1661. {Math.min((safePageIndex + 1) * effectivePageSize, totalDisplayItems)}{' '}
  1662. {t('inventory.of')} {totalDisplayItems} {t('inventory.spools')}</>
  1663. }
  1664. </span>
  1665. <div className="flex items-center gap-2">
  1666. <span className="text-bambu-gray">{t('inventory.show')}</span>
  1667. <select
  1668. value={pageSize}
  1669. onChange={(e) => handlePageSizeChange(Number(e.target.value))}
  1670. 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"
  1671. >
  1672. {[15, 30, 50, 100].map((n) => (
  1673. <option key={n} value={n}>{n}</option>
  1674. ))}
  1675. <option value={-1}>{t('inventory.all')}</option>
  1676. </select>
  1677. {!showAll && (
  1678. <>
  1679. <button
  1680. onClick={() => setPageIndex(0)}
  1681. disabled={safePageIndex === 0}
  1682. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1683. title="First page"
  1684. >
  1685. <ChevronsLeft className="w-4 h-4" />
  1686. </button>
  1687. <button
  1688. onClick={() => setPageIndex((p) => Math.max(0, p - 1))}
  1689. disabled={safePageIndex === 0}
  1690. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1691. >
  1692. <ChevronLeft className="w-4 h-4" />
  1693. </button>
  1694. <span className="text-bambu-gray px-2 whitespace-nowrap">
  1695. {t('inventory.page')} {safePageIndex + 1} {t('inventory.of')} {totalPages}
  1696. </span>
  1697. <button
  1698. onClick={() => setPageIndex((p) => Math.min(totalPages - 1, p + 1))}
  1699. disabled={safePageIndex >= totalPages - 1}
  1700. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1701. >
  1702. <ChevronRight className="w-4 h-4" />
  1703. </button>
  1704. <button
  1705. onClick={() => setPageIndex(totalPages - 1)}
  1706. disabled={safePageIndex >= totalPages - 1}
  1707. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1708. title="Last page"
  1709. >
  1710. <ChevronsRight className="w-4 h-4" />
  1711. </button>
  1712. </>
  1713. )}
  1714. </div>
  1715. </div>
  1716. </div>
  1717. ) : (
  1718. <EmptyFilterState
  1719. hasFilters={hasActiveFilters}
  1720. onAddSpool={() => setFormModal({ spool: null, mode: 'create' })}
  1721. t={t}
  1722. />
  1723. )
  1724. )}
  1725. {/* Spool Form Modal */}
  1726. {formModal !== null && (
  1727. <SpoolFormModal
  1728. isOpen={true}
  1729. onClose={() => setFormModal(null)}
  1730. spool={formModal.spool}
  1731. mode={formModal.mode}
  1732. currencySymbol={currencySymbol}
  1733. spoolmanMode={spoolmanMode}
  1734. spoolsQueryKey={spoolsQueryKey}
  1735. />
  1736. )}
  1737. {/* Confirm Modal (delete / archive / reset-usage / reset-all-usage) */}
  1738. {confirmAction && (
  1739. <ConfirmModal
  1740. title={
  1741. confirmAction.type === 'delete' ? t('common.delete') :
  1742. confirmAction.type === 'archive' ? t('inventory.archive') :
  1743. confirmAction.type === 'reset-usage' ? t('inventory.resetUsage') :
  1744. t('inventory.resetAllUsage')
  1745. }
  1746. message={
  1747. confirmAction.type === 'delete' ? t('inventory.deleteConfirm') :
  1748. confirmAction.type === 'archive' ? t('inventory.archiveConfirm') :
  1749. confirmAction.type === 'reset-usage' ? t('inventory.resetUsageConfirm') :
  1750. t('inventory.resetAllUsageConfirm', { count: activeSpoolIds.length })
  1751. }
  1752. confirmText={
  1753. confirmAction.type === 'delete' ? t('common.delete') :
  1754. confirmAction.type === 'archive' ? t('inventory.archive') :
  1755. t('inventory.resetUsage')
  1756. }
  1757. variant={confirmAction.type === 'archive' ? 'warning' : 'danger'}
  1758. onConfirm={() => {
  1759. if (confirmAction.type === 'delete') {
  1760. deleteMutation.mutate(confirmAction.spoolId);
  1761. } else if (confirmAction.type === 'archive') {
  1762. archiveMutation.mutate(confirmAction.spoolId);
  1763. } else if (confirmAction.type === 'reset-usage') {
  1764. resetUsageMutation.mutate(confirmAction.spoolId);
  1765. } else {
  1766. bulkResetUsageMutation.mutate(activeSpoolIds);
  1767. }
  1768. setConfirmAction(null);
  1769. }}
  1770. onCancel={() => setConfirmAction(null)}
  1771. />
  1772. )}
  1773. {/* Column Config Modal */}
  1774. <ColumnConfigModal
  1775. isOpen={showColumnModal}
  1776. onClose={() => setShowColumnModal(false)}
  1777. columns={columnConfig}
  1778. defaultColumns={DEFAULT_COLUMNS}
  1779. onSave={handleColumnConfigSave}
  1780. />
  1781. <LabelTemplatePickerModal
  1782. isOpen={labelPickerSpoolIds !== null}
  1783. onClose={() => setLabelPickerSpoolIds(null)}
  1784. availableSpools={filteredSpools}
  1785. initialSelectedIds={labelPickerSpoolIds ?? []}
  1786. spoolmanMode={spoolmanMode}
  1787. />
  1788. </div>
  1789. );
  1790. }
  1791. /* Pagination bar (reused for cards view) */
  1792. function PaginationBar({
  1793. pageIndex, pageSize, totalRows, totalPages, onPageChange, onPageSizeChange, t,
  1794. }: {
  1795. pageIndex: number;
  1796. pageSize: number;
  1797. totalRows: number;
  1798. totalPages: number;
  1799. onPageChange: (page: number) => void;
  1800. onPageSizeChange: (size: number) => void;
  1801. t: (key: string) => string;
  1802. }) {
  1803. const isShowAll = pageSize === -1;
  1804. if (totalPages <= 1 && !isShowAll) return null;
  1805. const effectiveSize = isShowAll ? totalRows || 1 : pageSize;
  1806. return (
  1807. <div className="flex items-center justify-between pt-2 text-sm">
  1808. <span className="text-bambu-gray">
  1809. {isShowAll
  1810. ? `${totalRows} ${totalRows !== 1 ? t('inventory.spools') : t('inventory.spool')}`
  1811. : <>{t('inventory.showing')} {pageIndex * effectiveSize + 1} {t('inventory.to')}{' '}
  1812. {Math.min((pageIndex + 1) * effectiveSize, totalRows)}{' '}
  1813. {t('inventory.of')} {totalRows} {t('inventory.spools')}</>
  1814. }
  1815. </span>
  1816. <div className="flex items-center gap-2">
  1817. <span className="text-bambu-gray">{t('inventory.show')}</span>
  1818. <select
  1819. value={pageSize}
  1820. onChange={(e) => onPageSizeChange(Number(e.target.value))}
  1821. 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"
  1822. >
  1823. {[15, 30, 50, 100].map((n) => (
  1824. <option key={n} value={n}>{n}</option>
  1825. ))}
  1826. <option value={-1}>{t('inventory.all')}</option>
  1827. </select>
  1828. {!isShowAll && (
  1829. <>
  1830. <button
  1831. onClick={() => onPageChange(0)}
  1832. disabled={pageIndex === 0}
  1833. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1834. >
  1835. <ChevronsLeft className="w-4 h-4" />
  1836. </button>
  1837. <button
  1838. onClick={() => onPageChange(Math.max(0, pageIndex - 1))}
  1839. disabled={pageIndex === 0}
  1840. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1841. >
  1842. <ChevronLeft className="w-4 h-4" />
  1843. </button>
  1844. <span className="text-bambu-gray px-2 whitespace-nowrap">
  1845. {t('inventory.page')} {pageIndex + 1} {t('inventory.of')} {totalPages}
  1846. </span>
  1847. <button
  1848. onClick={() => onPageChange(Math.min(totalPages - 1, pageIndex + 1))}
  1849. disabled={pageIndex >= totalPages - 1}
  1850. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1851. >
  1852. <ChevronRight className="w-4 h-4" />
  1853. </button>
  1854. <button
  1855. onClick={() => onPageChange(totalPages - 1)}
  1856. disabled={pageIndex >= totalPages - 1}
  1857. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1858. >
  1859. <ChevronsRight className="w-4 h-4" />
  1860. </button>
  1861. </>
  1862. )}
  1863. </div>
  1864. </div>
  1865. );
  1866. }
  1867. /* Spool card for cards view */
  1868. function SpoolCard({
  1869. spool, remaining, pct, onClick, onPrintLabel, onCopy, t,
  1870. }: {
  1871. spool: InventorySpool;
  1872. remaining: number;
  1873. pct: number;
  1874. onClick: () => void;
  1875. onPrintLabel?: () => void;
  1876. onCopy?: () => void;
  1877. t: (key: string, opts?: Record<string, unknown>) => string;
  1878. }) {
  1879. const bannerStyle = buildFilamentBackground({
  1880. rgba: spool.rgba,
  1881. extraColors: spool.extra_colors,
  1882. effectType: spool.effect_type,
  1883. subtype: spool.subtype,
  1884. effectSize: 'card',
  1885. });
  1886. return (
  1887. <div
  1888. 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' : ''}`}
  1889. onClick={onClick}
  1890. >
  1891. <div className="h-14 flex items-center justify-center" style={bannerStyle}>
  1892. <span className="bg-white/90 text-gray-800 px-3 py-0.5 rounded-full text-sm font-medium">
  1893. {resolveSpoolColorName(spool.color_name, spool.rgba) || '-'}
  1894. </span>
  1895. {onCopy && (
  1896. <button
  1897. type="button"
  1898. onClick={(e) => { e.stopPropagation(); onCopy(); }}
  1899. className="p-1.5 bg-black/20 hover:bg-black/40 text-white rounded-full transition-colors"
  1900. title={t('inventory.copySpool')}
  1901. >
  1902. <Copy className="w-3.5 h-3.5" />
  1903. </button>
  1904. )}
  1905. </div>
  1906. <div className="p-4 space-y-3">
  1907. <div className="flex items-start justify-between gap-2">
  1908. <div>
  1909. <h3 className="font-semibold text-white">
  1910. {spool.material}{spool.subtype ? ` ${spool.subtype}` : ''}
  1911. </h3>
  1912. <p className="text-sm text-bambu-gray">{spool.brand || '-'}</p>
  1913. </div>
  1914. <div className="flex items-center gap-1">
  1915. {onPrintLabel && (
  1916. <button
  1917. onClick={(e) => { e.stopPropagation(); onPrintLabel(); }}
  1918. className="p-1 text-bambu-gray hover:text-white rounded transition-colors"
  1919. title={t('inventory.labels.printOne')}
  1920. aria-label={t('inventory.labels.printOne')}
  1921. >
  1922. <Printer className="w-4 h-4" />
  1923. </button>
  1924. )}
  1925. <span className="text-xs font-mono text-bambu-gray bg-bambu-dark-tertiary px-2 py-1 rounded">
  1926. #{spool.id}
  1927. </span>
  1928. </div>
  1929. </div>
  1930. <div>
  1931. <div className="flex justify-between text-xs text-bambu-gray mb-1">
  1932. <span>{t('inventory.remaining')}</span>
  1933. <span>{Math.round(pct)}%</span>
  1934. </div>
  1935. <div className="flex items-center gap-2">
  1936. <div className="flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden">
  1937. <div
  1938. className={`h-full rounded-full ${pct > 50 ? 'bg-bambu-green' : pct > 20 ? 'bg-yellow-500' : 'bg-red-500'}`}
  1939. style={{ width: `${Math.min(pct, 100)}%` }}
  1940. />
  1941. </div>
  1942. <span className="text-xs text-bambu-gray min-w-[40px] text-right">
  1943. {Math.round(remaining)}g
  1944. </span>
  1945. </div>
  1946. </div>
  1947. <div className="grid grid-cols-2 gap-2 text-xs">
  1948. <div>
  1949. <span className="text-bambu-gray/60">{t('inventory.labelWeight')}: </span>
  1950. <span className="text-bambu-gray">{formatWeight(spool.label_weight)}</span>
  1951. </div>
  1952. <div>
  1953. <span className="text-bambu-gray/60">{t('inventory.weightUsed')}: </span>
  1954. <span className="text-bambu-gray">
  1955. {spool.weight_used > 0 ? formatWeight(spool.weight_used) : '-'}
  1956. </span>
  1957. </div>
  1958. </div>
  1959. {spool.note && (
  1960. <div
  1961. className="text-xs text-bambu-gray/60 pt-2 border-t border-bambu-dark-tertiary truncate"
  1962. title={spool.note}
  1963. >
  1964. {spool.note}
  1965. </div>
  1966. )}
  1967. </div>
  1968. </div>
  1969. );
  1970. }
  1971. /* Single spool row for table view */
  1972. function SpoolTableRow({
  1973. spool, remaining, pct, onEdit, onCopy, onRestore, onArchive, onDelete, onPrintLabel, onResetUsage,
  1974. visibleColumns, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight,
  1975. }: {
  1976. spool: InventorySpool;
  1977. remaining: number;
  1978. pct: number;
  1979. onEdit: () => void;
  1980. onCopy?: () => void;
  1981. onRestore: () => void;
  1982. onArchive: () => void;
  1983. onDelete: () => void;
  1984. onPrintLabel?: () => void;
  1985. onResetUsage?: () => void;
  1986. visibleColumns: string[];
  1987. assignmentMap: Record<number, LocationDisplay>;
  1988. catalogMap: Record<number, SpoolCatalogEntry>;
  1989. currencySymbol: string;
  1990. dateFormat: DateFormat;
  1991. t: TFn;
  1992. onSyncWeight?: (spool: InventorySpool) => void;
  1993. }) {
  1994. return (
  1995. <tr
  1996. className={`border-b border-bambu-dark-tertiary/50 hover:bg-bambu-dark-tertiary/30 transition-colors cursor-pointer ${
  1997. spool.archived_at ? 'opacity-50' : ''
  1998. }`}
  1999. onClick={onEdit}
  2000. >
  2001. {visibleColumns.map((colId) => (
  2002. <td key={colId} className="py-3 px-4">
  2003. {columnCells[colId]?.({ spool, remaining, pct, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight })}
  2004. </td>
  2005. ))}
  2006. <td className="py-3 px-4">
  2007. <div className="flex items-center justify-end gap-1" onClick={(e) => e.stopPropagation()}>
  2008. <button onClick={onEdit} className="p-1.5 text-bambu-gray hover:text-white rounded transition-colors" title={t('common.edit')}>
  2009. <Edit2 className="w-4 h-4" />
  2010. </button>
  2011. {onCopy && (
  2012. <button onClick={onCopy} className="p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors" title={t('inventory.copySpool')}>
  2013. <Copy className="w-4 h-4" />
  2014. </button>
  2015. )}
  2016. {onPrintLabel && (
  2017. <button onClick={onPrintLabel} className="p-1.5 text-bambu-gray hover:text-white rounded transition-colors" title={t('inventory.labels.printOne')}>
  2018. <Printer className="w-4 h-4" />
  2019. </button>
  2020. )}
  2021. {onResetUsage && !spool.archived_at && spool.weight_used > 0 && (
  2022. <button onClick={onResetUsage} className="p-1.5 text-bambu-gray hover:text-orange-400 rounded transition-colors" title={t('inventory.resetUsageTooltip')}>
  2023. <Eraser className="w-4 h-4" />
  2024. </button>
  2025. )}
  2026. {spool.archived_at ? (
  2027. <button onClick={onRestore} className="p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors" title={t('inventory.restore')}>
  2028. <RotateCcw className="w-4 h-4" />
  2029. </button>
  2030. ) : (
  2031. <button onClick={onArchive} className="p-1.5 text-bambu-gray hover:text-yellow-400 rounded transition-colors" title={t('inventory.archive')}>
  2032. <Archive className="w-4 h-4" />
  2033. </button>
  2034. )}
  2035. <button onClick={onDelete} className="p-1.5 text-bambu-gray hover:text-red-400 rounded transition-colors" title={t('common.delete')}>
  2036. <Trash2 className="w-4 h-4" />
  2037. </button>
  2038. </div>
  2039. </td>
  2040. </tr>
  2041. );
  2042. }
  2043. /* Grouped spool rows for table view */
  2044. function SpoolTableGroup({
  2045. spools, representative, remaining, pct, isExpanded, onToggle,
  2046. onEdit, onCopy, onArchive, onDelete, onPrintLabel, onResetUsage,
  2047. visibleColumns, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight,
  2048. }: {
  2049. spools: InventorySpool[];
  2050. representative: InventorySpool;
  2051. remaining: number;
  2052. pct: number;
  2053. isExpanded: boolean;
  2054. onToggle: () => void;
  2055. onEdit: (spool: InventorySpool) => void;
  2056. onCopy?: (spool: InventorySpool) => void;
  2057. onArchive: (id: number) => void;
  2058. onDelete: (id: number) => void;
  2059. onPrintLabel?: (spoolId: number) => void;
  2060. onResetUsage?: (id: number) => void;
  2061. visibleColumns: string[];
  2062. assignmentMap: Record<number, LocationDisplay>;
  2063. catalogMap: Record<number, SpoolCatalogEntry>;
  2064. currencySymbol: string;
  2065. dateFormat: DateFormat;
  2066. t: TFn;
  2067. onSyncWeight?: (spool: InventorySpool) => void;
  2068. }) {
  2069. return (
  2070. <>
  2071. {/* Group header row */}
  2072. <tr
  2073. className="border-b border-bambu-dark-tertiary/50 hover:bg-bambu-dark-tertiary/30 transition-colors cursor-pointer bg-bambu-green/5"
  2074. onClick={onToggle}
  2075. >
  2076. {visibleColumns.map((colId, idx) => (
  2077. <td key={colId} className="py-3 px-4">
  2078. {idx === 0 ? (
  2079. <div className="flex items-center gap-2">
  2080. <ChevronDown className={`w-4 h-4 text-bambu-gray transition-transform ${isExpanded ? '' : '-rotate-90'}`} />
  2081. {columnCells[colId]?.({ spool: representative, remaining, pct, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight })}
  2082. </div>
  2083. ) : colId === 'id' ? (
  2084. <span className="text-xs font-medium bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full">
  2085. {t('inventory.groupedSpools', { count: spools.length })}
  2086. </span>
  2087. ) : (
  2088. columnCells[colId]?.({ spool: representative, remaining, pct, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight })
  2089. )}
  2090. </td>
  2091. ))}
  2092. <td className="py-3 px-4">
  2093. <span className="text-xs text-bambu-gray">
  2094. {spools.map((s) => `#${s.id}`).join(', ')}
  2095. </span>
  2096. </td>
  2097. </tr>
  2098. {/* Expanded individual rows */}
  2099. {isExpanded && spools.map((spool) => {
  2100. const r = Math.max(0, spool.label_weight - spool.weight_used);
  2101. const p = spool.label_weight > 0 ? (r / spool.label_weight) * 100 : 0;
  2102. return (
  2103. <SpoolTableRow
  2104. key={spool.id}
  2105. spool={spool}
  2106. remaining={r}
  2107. pct={p}
  2108. onEdit={() => onEdit(spool)}
  2109. onCopy={onCopy ? () => onCopy(spool) : undefined}
  2110. onRestore={() => {}}
  2111. onArchive={() => onArchive(spool.id)}
  2112. onDelete={() => onDelete(spool.id)}
  2113. onPrintLabel={onPrintLabel ? () => onPrintLabel(spool.id) : undefined}
  2114. onResetUsage={onResetUsage ? () => onResetUsage(spool.id) : undefined}
  2115. visibleColumns={visibleColumns}
  2116. assignmentMap={assignmentMap}
  2117. catalogMap={catalogMap}
  2118. currencySymbol={currencySymbol}
  2119. dateFormat={dateFormat}
  2120. t={t}
  2121. onSyncWeight={onSyncWeight}
  2122. />
  2123. );
  2124. })}
  2125. </>
  2126. );
  2127. }
  2128. /* Empty state matching SpoolBuddy's design */
  2129. function EmptyFilterState({
  2130. hasFilters,
  2131. onAddSpool,
  2132. t,
  2133. }: {
  2134. hasFilters: boolean;
  2135. onAddSpool: () => void;
  2136. t: (key: string) => string;
  2137. }) {
  2138. return (
  2139. <div className="flex flex-col items-center justify-center py-16 px-4">
  2140. <div className="relative mb-6">
  2141. <div className="absolute inset-0 -m-4 bg-bambu-green/5 rounded-full blur-2xl" />
  2142. <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">
  2143. <div className="absolute -top-1 -right-1 w-3 h-3 rounded-full bg-bambu-green/30" />
  2144. <div className="absolute -bottom-2 -left-2 w-2 h-2 rounded-full bg-bambu-green/20" />
  2145. {hasFilters ? (
  2146. <Search className="w-10 h-10 text-bambu-gray/40" strokeWidth={1.5} />
  2147. ) : (
  2148. <div className="relative">
  2149. <div className="w-14 h-14 rounded-full border-4 border-bambu-gray/20 flex items-center justify-center">
  2150. <div className="w-6 h-6 rounded-full bg-bambu-gray/10 border-2 border-bambu-gray/20" />
  2151. </div>
  2152. <div className="absolute -bottom-1 -right-1 w-6 h-6 rounded-full bg-bambu-green flex items-center justify-center shadow-md">
  2153. <span className="text-white text-lg font-bold leading-none">+</span>
  2154. </div>
  2155. </div>
  2156. )}
  2157. </div>
  2158. </div>
  2159. <h3 className="text-lg font-semibold text-white mb-2 text-center">
  2160. {hasFilters ? t('inventory.noSpoolsMatch') : t('inventory.noSpools').split('.')[0]}
  2161. </h3>
  2162. <p className="text-sm text-bambu-gray text-center max-w-sm mb-6">
  2163. {hasFilters
  2164. ? t('inventory.noSpoolsMatchDesc')
  2165. : t('inventory.noSpools')
  2166. }
  2167. </p>
  2168. {!hasFilters && (
  2169. <Button onClick={onAddSpool}>
  2170. <Package className="w-4 h-4" />
  2171. {t('inventory.addSpool')}
  2172. </Button>
  2173. )}
  2174. </div>
  2175. );
  2176. }