InventoryPage.tsx 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315
  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. // Spool IDs the "Reset all usage" button bulk-targets. Includes archived
  665. // spools too — without them, the broadened "Total Consumed" stat (which
  666. // sums archived consumption per the #1390 follow-up) would stay non-zero
  667. // after a Reset-all click, surprising the user. Backend reset endpoints
  668. // (both internal and Spoolman) already accept archived IDs without a
  669. // route-level guard, so this just removes the frontend filter.
  670. const resetableSpoolIds = useMemo(
  671. () => (spools ?? []).map((s) => s.id),
  672. [spools],
  673. );
  674. const handleSyncWeight = async (spool: InventorySpool) => {
  675. if (spool.last_scale_weight == null) return;
  676. try {
  677. if (spoolmanMode) {
  678. await api.syncSpoolmanSpoolWeight(spool.id, spool.last_scale_weight);
  679. } else {
  680. await spoolbuddyApi.updateSpoolWeight(spool.id, spool.last_scale_weight);
  681. }
  682. queryClient.invalidateQueries({ queryKey: spoolsQueryKey });
  683. const spoolName = [spool.brand, spool.material, spool.color_name].filter(Boolean).join(' ');
  684. showToast(`Synced "${spoolName}" to scale weight`, 'success');
  685. } catch (e) {
  686. const is404 = e instanceof ApiError && e.status === 404;
  687. const is503 = e instanceof ApiError && e.status === 503;
  688. if (is404) showToast(t('inventory.syncWeightSpoolNotFound'), 'error');
  689. else if (is503) showToast(t('inventory.syncWeightSpoolmanUnreachable'), 'error');
  690. else showToast(t('inventory.syncWeightFailed'), 'error');
  691. }
  692. };
  693. // Low stock threshold from backend settings
  694. const lowStockThreshold = settings?.low_stock_threshold ?? 20;
  695. const [showThresholdInput, setShowThresholdInput] = useState(false);
  696. const [thresholdInput, setThresholdInput] = useState(lowStockThreshold.toString());
  697. // Sync thresholdInput when lowStockThreshold changes and input is not shown
  698. useEffect(() => {
  699. if (!showThresholdInput) {
  700. setThresholdInput(lowStockThreshold.toString());
  701. }
  702. }, [lowStockThreshold, showThresholdInput]);
  703. const updateThresholdMutation = useMutation({
  704. mutationFn: (threshold: number) => api.updateSettings({ low_stock_threshold: threshold }),
  705. onSuccess: () => {
  706. queryClient.invalidateQueries({ queryKey: ['settings'] });
  707. showToast(t('common.saved'), 'success');
  708. setShowThresholdInput(false);
  709. },
  710. onError: () => {
  711. showToast(t('inventory.lowStockThresholdError'), 'error');
  712. },
  713. });
  714. // Stats calculation.
  715. //
  716. // "Total Consumed" sums over ALL spools (active AND archived) because it's
  717. // a running counter — past consumption of a now-archived spool is real
  718. // history and silently dropping it on archive made the running total
  719. // collapse mysteriously (#1390 follow-up). The other aggregates
  720. // (totalWeight, lowStock, byMaterial, activeCount) describe what's
  721. // currently in active inventory and stay active-only.
  722. const stats = useMemo(() => {
  723. if (!spools) return null;
  724. let totalWeight = 0;
  725. let totalConsumed = 0;
  726. let lowStock = 0;
  727. let activeCount = 0;
  728. const byMaterial: Record<string, { count: number; weight: number }> = {};
  729. for (const s of spools) {
  730. // "Total Consumed" is the resettable counter (weight_used - baseline)
  731. // rather than raw weight_used so the per-spool / bulk eraser zeroes
  732. // the stat without inflating remaining back to label_weight (#1390).
  733. // Computed before the archived-skip below so archived consumption
  734. // stays in the running total.
  735. totalConsumed += Math.max(0, s.weight_used - (s.weight_used_baseline ?? 0));
  736. if (s.archived_at) continue;
  737. activeCount++;
  738. const remaining = Math.max(0, s.label_weight - s.weight_used);
  739. totalWeight += remaining;
  740. const pct = s.label_weight > 0 ? (remaining / s.label_weight) * 100 : 0;
  741. const threshold = s.low_stock_threshold_pct ?? lowStockThreshold;
  742. if (pct < threshold) lowStock++;
  743. const mat = s.material || 'Unknown';
  744. if (!byMaterial[mat]) byMaterial[mat] = { count: 0, weight: 0 };
  745. byMaterial[mat].count++;
  746. byMaterial[mat].weight += remaining;
  747. }
  748. return { totalWeight, totalConsumed, lowStock, byMaterial, totalSpools: activeCount };
  749. }, [spools, lowStockThreshold]);
  750. const inPrinterCount =
  751. (assignments?.length ?? 0) + (spoolmanMode ? spoolmanSlotAssignments.length : 0);
  752. const currencySymbol = getCurrencySymbol(settings?.currency || 'USD');
  753. // Map spool_id -> location display data for the LOCATION column.
  754. // Local SpoolAssignment entries first, then Spoolman SlotAssignment fills in
  755. // remaining IDs. Local wins on collision (defensive — modes are exclusive in
  756. // practice, but a stray pair with the same numeric id would otherwise be
  757. // unpredictable). spool.id IS the spoolman_spool_id in Spoolman mode.
  758. const assignmentMap = useMemo<Record<number, LocationDisplay>>(() => {
  759. const map: Record<number, LocationDisplay> = {};
  760. for (const a of assignments || []) {
  761. map[a.spool_id] = {
  762. printer_id: a.printer_id,
  763. printer_name: a.printer_name,
  764. ams_id: a.ams_id,
  765. tray_id: a.tray_id,
  766. ams_label: a.ams_label ?? null,
  767. };
  768. }
  769. for (const a of spoolmanSlotAssignments) {
  770. // Defensive: skip malformed entries (missing or invalid spool id, ams id,
  771. // tray id). The Pydantic response model on the backend should already
  772. // reject these, but MITM proxies and stale CDN responses can drop fields.
  773. if (
  774. typeof a?.spoolman_spool_id !== 'number' ||
  775. a.spoolman_spool_id <= 0 ||
  776. typeof a.printer_id !== 'number' ||
  777. typeof a.ams_id !== 'number' ||
  778. typeof a.tray_id !== 'number'
  779. ) continue;
  780. if (!map[a.spoolman_spool_id]) {
  781. map[a.spoolman_spool_id] = {
  782. printer_id: a.printer_id,
  783. printer_name: a.printer_name ?? null,
  784. ams_id: a.ams_id,
  785. tray_id: a.tray_id,
  786. ams_label: a.ams_label ?? null,
  787. };
  788. }
  789. }
  790. return map;
  791. }, [assignments, spoolmanSlotAssignments]);
  792. // Map catalog_id -> catalog entry for spool name column
  793. const catalogMap = useMemo(() => {
  794. const map: Record<number, SpoolCatalogEntry> = {};
  795. for (const e of catalogEntries || []) {
  796. map[e.id] = e;
  797. }
  798. return map;
  799. }, [catalogEntries]);
  800. // Top materials by weight for stat card pills
  801. const topMaterials = useMemo(() => {
  802. if (!stats) return [];
  803. return Object.entries(stats.byMaterial)
  804. .sort((a, b) => b[1].weight - a[1].weight)
  805. .slice(0, 4);
  806. }, [stats]);
  807. // Filtering pipeline
  808. const filteredSpools = useMemo(() => {
  809. let filtered = spools || [];
  810. // Archive filter
  811. if (archiveFilter === 'active') {
  812. filtered = filtered.filter((s) => !s.archived_at);
  813. } else {
  814. filtered = filtered.filter((s) => !!s.archived_at);
  815. }
  816. // Usage filter
  817. if (usageFilter === 'used') {
  818. filtered = filtered.filter((s) => s.weight_used > 0);
  819. } else if (usageFilter === 'new') {
  820. filtered = filtered.filter((s) => s.weight_used === 0);
  821. } else if (usageFilter === 'lowstock') {
  822. filtered = filtered.filter((s) => {
  823. const remaining = Math.max(0, s.label_weight - s.weight_used);
  824. const pct = s.label_weight > 0 ? (remaining / s.label_weight) * 100 : 0;
  825. const threshold = s.low_stock_threshold_pct ?? lowStockThreshold;
  826. return pct < threshold;
  827. });
  828. }
  829. // Material dropdown
  830. if (materialFilter) {
  831. filtered = filtered.filter((s) => s.material === materialFilter);
  832. }
  833. // Brand dropdown
  834. if (brandFilter) {
  835. filtered = filtered.filter((s) => s.brand === brandFilter);
  836. }
  837. // Category dropdown (#729)
  838. if (categoryFilter) {
  839. if (categoryFilter === '__none__') {
  840. filtered = filtered.filter((s) => !s.category);
  841. } else {
  842. filtered = filtered.filter((s) => s.category === categoryFilter);
  843. }
  844. }
  845. // Spool name dropdown
  846. if (spoolFilter) {
  847. const catalogId = Number(spoolFilter);
  848. filtered = filtered.filter((s) => s.core_weight_catalog_id === catalogId);
  849. }
  850. // Storage location dropdown (#1400). `__none__` lets the user find
  851. // spools that haven't been assigned a storage location yet.
  852. if (storageLocationFilter) {
  853. if (storageLocationFilter === '__none__') {
  854. filtered = filtered.filter((s) => !s.storage_location?.trim());
  855. } else {
  856. filtered = filtered.filter((s) => s.storage_location?.trim() === storageLocationFilter);
  857. }
  858. }
  859. // Stock filter
  860. if (stockFilter === 'stock') {
  861. filtered = filtered.filter((s) => !s.slicer_filament);
  862. } else if (stockFilter === 'configured') {
  863. filtered = filtered.filter((s) => !!s.slicer_filament);
  864. }
  865. // Global search
  866. if (search) {
  867. filtered = filterSpoolsByQuery(filtered, search);
  868. }
  869. return filtered;
  870. }, [spools, archiveFilter, usageFilter, materialFilter, brandFilter, categoryFilter, spoolFilter, stockFilter, storageLocationFilter, search, lowStockThreshold]);
  871. // Reset page on filter changes
  872. const resetPage = () => setPageIndex(0);
  873. // Unique values for filter dropdowns
  874. const uniqueMaterials = [...new Set(spools?.map((s) => s.material) || [])].sort();
  875. const uniqueBrands = [...new Set(spools?.map((s) => s.brand).filter(Boolean) || [])].sort() as string[];
  876. const uniqueCategories = [...new Set(spools?.map((s) => s.category?.trim()).filter(Boolean) as string[] || [])].sort();
  877. const hasUncategorized = (spools ?? []).some((s) => !s.category);
  878. const uniqueSpoolCatalogIds = [...new Set(spools?.map((s) => s.core_weight_catalog_id).filter((id): id is number => id != null) || [])].sort((a, b) => {
  879. const nameA = (catalogMap[a]?.name || '').toLowerCase();
  880. const nameB = (catalogMap[b]?.name || '').toLowerCase();
  881. return nameA.localeCompare(nameB);
  882. });
  883. // #1400: storage-location distinct values. `.trim()` so accidental
  884. // trailing whitespace doesn't show up as a separate option.
  885. const uniqueStorageLocations = [...new Set(spools?.map((s) => s.storage_location?.trim()).filter(Boolean) as string[] || [])].sort();
  886. const hasUnsetStorageLocation = (spools ?? []).some((s) => !s.storage_location?.trim());
  887. // Check if any filters are non-default
  888. const hasActiveFilters = archiveFilter !== 'active' || usageFilter !== 'all' || !!materialFilter || !!brandFilter || !!categoryFilter || !!spoolFilter || !!storageLocationFilter || stockFilter !== 'all' || !!search;
  889. const handleColumnConfigSave = (config: ColumnConfig[]) => {
  890. setColumnConfig(config);
  891. saveColumnConfig(config);
  892. };
  893. // Visible column IDs in order
  894. const visibleColumns = useMemo(
  895. () => columnConfig.filter((c) => c.visible).map((c) => c.id),
  896. [columnConfig]
  897. );
  898. const handleSort = (colId: string) => {
  899. if (!columnSortValues[colId]) return; // Not sortable
  900. setSortState((prev) => {
  901. let next: SortState;
  902. if (prev?.column === colId) {
  903. // Toggle direction, or clear on third click
  904. next = prev.direction === 'asc' ? { column: colId, direction: 'desc' } : null;
  905. } else {
  906. next = { column: colId, direction: 'asc' };
  907. }
  908. saveSortState(next);
  909. return next;
  910. });
  911. resetPage();
  912. };
  913. // Sort filtered spools
  914. const sortedSpools = useMemo(() => {
  915. if (!sortState) return filteredSpools;
  916. const extractor = columnSortValues[sortState.column];
  917. if (!extractor) return filteredSpools;
  918. const sorted = [...filteredSpools].sort((a, b) => {
  919. const va = extractor(a, assignmentMap);
  920. const vb = extractor(b, assignmentMap);
  921. if (va < vb) return sortState.direction === 'asc' ? -1 : 1;
  922. if (va > vb) return sortState.direction === 'asc' ? 1 : -1;
  923. return 0;
  924. });
  925. return sorted;
  926. }, [filteredSpools, sortState, assignmentMap]);
  927. // Group similar spools when toggle is active
  928. const displayItems = useMemo((): DisplayItem[] => {
  929. if (!groupSimilar) return sortedSpools.map((s) => ({ type: 'single' as const, spool: s }));
  930. const groups = new Map<string, InventorySpool[]>();
  931. for (const spool of sortedSpools) {
  932. // Only group unused & unassigned spools
  933. if (spool.weight_used > 0 || assignmentMap[spool.id]) {
  934. // Will be added as singles in the walk below
  935. } else {
  936. const key = spoolGroupKey(spool);
  937. const arr = groups.get(key);
  938. if (arr) arr.push(spool);
  939. else groups.set(key, [spool]);
  940. }
  941. }
  942. const items: DisplayItem[] = [];
  943. const processedKeys = new Set<string>();
  944. // Walk sortedSpools order so groups appear at the position of their first member
  945. for (const spool of sortedSpools) {
  946. if (spool.weight_used > 0 || assignmentMap[spool.id]) {
  947. items.push({ type: 'single', spool });
  948. continue;
  949. }
  950. const key = spoolGroupKey(spool);
  951. if (processedKeys.has(key)) continue;
  952. processedKeys.add(key);
  953. const members = groups.get(key)!;
  954. if (members.length === 1) {
  955. items.push({ type: 'single', spool: members[0] });
  956. } else {
  957. items.push({ type: 'group', key, spools: members, representative: members[0] });
  958. }
  959. }
  960. return items;
  961. }, [sortedSpools, groupSimilar, assignmentMap]);
  962. // Pagination (after sorting) — pageSize -1 means "All"
  963. const showAll = pageSize === -1;
  964. const totalDisplayItems = displayItems.length;
  965. const effectivePageSize = showAll ? totalDisplayItems || 1 : pageSize;
  966. const totalPages = Math.max(1, Math.ceil(totalDisplayItems / effectivePageSize));
  967. const safePageIndex = showAll ? 0 : Math.min(pageIndex, totalPages - 1);
  968. const pagedItems = showAll
  969. ? displayItems
  970. : displayItems.slice(safePageIndex * effectivePageSize, (safePageIndex + 1) * effectivePageSize);
  971. const toggleGroupSimilar = () => {
  972. const next = !groupSimilar;
  973. setGroupSimilar(next);
  974. setExpandedGroups(new Set());
  975. resetPage();
  976. try { localStorage.setItem('bambuddy-inventory-group', String(next)); } catch { /* ignore */ }
  977. };
  978. const toggleGroupExpand = (key: string) => {
  979. setExpandedGroups((prev) => {
  980. const next = new Set(prev);
  981. if (next.has(key)) next.delete(key);
  982. else next.add(key);
  983. return next;
  984. });
  985. };
  986. const handlePageSizeChange = (size: number) => {
  987. setPageSize(size);
  988. setPageIndex(0);
  989. try { localStorage.setItem('bambuddy-inventory-pageSize', String(size)); } catch { /* ignore */ }
  990. };
  991. const clearAllFilters = () => {
  992. setArchiveFilter('active');
  993. setUsageFilter('all');
  994. setMaterialFilter('');
  995. setBrandFilter('');
  996. setCategoryFilter('');
  997. setSpoolFilter('');
  998. setStorageLocationFilter('');
  999. setStockFilter('all');
  1000. setSearch('');
  1001. resetPage();
  1002. };
  1003. return (
  1004. <div className="p-4 md:p-8 space-y-6">
  1005. {/* Header */}
  1006. <div className="flex items-center justify-between">
  1007. <div>
  1008. <h1 className="text-2xl font-bold text-white flex items-center gap-3">
  1009. <Package className="w-7 h-7 text-bambu-green" />
  1010. {t('inventory.title')}
  1011. </h1>
  1012. <p className="text-bambu-gray mt-1">{t('inventory.subtitle')}</p>
  1013. </div>
  1014. <div className="flex items-center gap-2">
  1015. <Button
  1016. variant="secondary"
  1017. disabled={filteredSpools.length === 0}
  1018. // Pre-select every visible spool so the user lands in "all
  1019. // checked", then refines downward in the modal. Per-card icon
  1020. // pre-selects only that spool — both flows share the same picker.
  1021. onClick={() => setLabelPickerSpoolIds(filteredSpools.map((s) => s.id))}
  1022. title={
  1023. filteredSpools.length === 0
  1024. ? t('inventory.labels.noSpoolsTitle', 'No spools to label')
  1025. : t('inventory.labels.bulkTitle', 'Pick spools to print labels for from the {{count}} currently shown', { count: filteredSpools.length })
  1026. }
  1027. >
  1028. <Printer className="w-4 h-4" />
  1029. {t('inventory.labels.printLabels', 'Print labels…')}
  1030. </Button>
  1031. <Button onClick={() => setFormModal({ spool: null, mode: 'create' })}>
  1032. <Plus className="w-4 h-4" />
  1033. {t('inventory.addSpool')}
  1034. </Button>
  1035. </div>
  1036. </div>
  1037. {/* Stats Bar */}
  1038. {stats && !isLoading && (
  1039. <div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-3">
  1040. {/* Total Inventory */}
  1041. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  1042. <div className="flex items-center gap-2 mb-1">
  1043. <Package className="w-4 h-4 text-bambu-green" />
  1044. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.totalInventory')}</span>
  1045. </div>
  1046. <div className="text-xl font-bold text-white">{formatWeight(stats.totalWeight, true)}</div>
  1047. <div className="text-xs text-bambu-gray mt-1">{stats.totalSpools} {stats.totalSpools !== 1 ? t('inventory.spools') : t('inventory.spool')}</div>
  1048. </div>
  1049. {/* Total Consumed */}
  1050. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  1051. <div className="flex items-center justify-between gap-2 mb-1">
  1052. <div className="flex items-center gap-2">
  1053. <TrendingDown className="w-4 h-4 text-blue-400" />
  1054. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.totalConsumed')}</span>
  1055. </div>
  1056. {stats.totalConsumed > 0 && resetableSpoolIds.length > 0 && (
  1057. <button
  1058. onClick={() => setConfirmAction({ type: 'reset-all-usage' })}
  1059. className="p-1 text-bambu-gray hover:text-red-400 rounded transition-colors"
  1060. title={t('inventory.resetAllUsageTooltip')}
  1061. aria-label={t('inventory.resetAllUsage')}
  1062. >
  1063. <Eraser className="w-3.5 h-3.5" />
  1064. </button>
  1065. )}
  1066. </div>
  1067. <div className="text-xl font-bold text-white">{formatWeight(stats.totalConsumed, true)}</div>
  1068. <div className="text-xs text-bambu-gray mt-1">{t('inventory.sinceTracking')}</div>
  1069. </div>
  1070. {/* By Material */}
  1071. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  1072. <div className="flex items-center gap-2 mb-1">
  1073. <Layers className="w-4 h-4 text-green-400" />
  1074. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.byMaterial')}</span>
  1075. </div>
  1076. <div className="flex flex-wrap gap-1.5 mt-1">
  1077. {topMaterials.map(([mat, data]) => (
  1078. <span
  1079. key={mat}
  1080. 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'}`}
  1081. >
  1082. {mat} <span className="opacity-70">{formatWeight(data.weight, true)}</span>
  1083. </span>
  1084. ))}
  1085. </div>
  1086. </div>
  1087. {/* In Printer */}
  1088. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  1089. <div className="flex items-center gap-2 mb-1">
  1090. <Printer className="w-4 h-4 text-purple-400" />
  1091. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.inPrinter')}</span>
  1092. </div>
  1093. <div className="text-xl font-bold text-white">{inPrinterCount}</div>
  1094. <div className="text-xs text-bambu-gray mt-1">{t('inventory.loadedInAms')}</div>
  1095. </div>
  1096. {/* Low Stock */}
  1097. <div className="bg-bambu-dark-secondary rounded-lg p-4">
  1098. <div className="flex items-center gap-2 mb-1">
  1099. <AlertTriangle className="w-4 h-4 text-yellow-400" />
  1100. <span className="text-xs text-bambu-gray font-medium uppercase tracking-wide">{t('inventory.lowStock')}</span>
  1101. </div>
  1102. <div className={`text-xl font-bold ${stats.lowStock > 0 ? 'text-yellow-400' : 'text-white'}`}>{stats.lowStock}</div>
  1103. <div className="text-xs text-bambu-gray mt-1 flex items-center gap-2">
  1104. {showThresholdInput ? (
  1105. <form
  1106. onSubmit={e => {
  1107. e.preventDefault();
  1108. const val = parseFloat(thresholdInput);
  1109. if (!isNaN(val) && val >= 0.1 && val <= 99.9) {
  1110. updateThresholdMutation.mutate(val);
  1111. } else {
  1112. showToast(t('inventory.lowStockThresholdError'), 'error');
  1113. }
  1114. }}
  1115. className="flex items-center gap-2"
  1116. >
  1117. <span className="text-xs text-bambu-gray">{'<'}</span>
  1118. <input
  1119. type="text"
  1120. inputMode="decimal"
  1121. pattern="^\d{0,2}(\.\d?)?$"
  1122. maxLength={4}
  1123. value={thresholdInput}
  1124. onChange={e => {
  1125. // Only allow up to 2 digits before decimal and 1 after
  1126. const val = e.target.value.replace(/[^\d.]/g, '');
  1127. if (/^\d{0,2}(\.\d?)?$/.test(val)) {
  1128. setThresholdInput(val);
  1129. }
  1130. }}
  1131. 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"
  1132. onWheel={e => e.currentTarget.blur()}
  1133. disabled={updateThresholdMutation.isPending}
  1134. />
  1135. <span className="text-xs text-bambu-gray">%</span>
  1136. <Button type="submit" size="sm" disabled={updateThresholdMutation.isPending}>{t('common.save')}</Button>
  1137. <Button type="button" size="sm" variant="ghost" onClick={() => setShowThresholdInput(false)} disabled={updateThresholdMutation.isPending}>{t('common.cancel')}</Button>
  1138. </form>
  1139. ) : (
  1140. <>
  1141. <span className="text-bambu-gray">{'< '}{lowStockThreshold}%</span>
  1142. <button
  1143. className="p-1.5 text-bambu-gray hover:text-white rounded transition-colors"
  1144. title={t('common.edit')}
  1145. onClick={() => {
  1146. setThresholdInput(lowStockThreshold.toString());
  1147. setShowThresholdInput(true);
  1148. }}
  1149. >
  1150. <Edit2 className="w-4 h-4" />
  1151. </button>
  1152. </>
  1153. )}
  1154. </div>
  1155. </div>
  1156. </div>
  1157. )}
  1158. {/* Toolbar: Search + View toggle */}
  1159. <div className="flex flex-col sm:flex-row gap-3 items-start sm:items-center justify-between">
  1160. <div className={`relative flex-1 max-w-md ${viewMode === 'forecast' ? 'invisible pointer-events-none' : ''}`}>
  1161. <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50" />
  1162. <input
  1163. type="text"
  1164. value={search}
  1165. onChange={(e) => { setSearch(e.target.value); resetPage(); }}
  1166. placeholder={t('inventory.search')}
  1167. 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"
  1168. />
  1169. {search && (
  1170. <button
  1171. onClick={() => { setSearch(''); resetPage(); }}
  1172. className="absolute right-3 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white"
  1173. >
  1174. <X className="w-4 h-4" />
  1175. </button>
  1176. )}
  1177. </div>
  1178. <div className="flex items-center gap-2">
  1179. {/* Columns button (table view only) */}
  1180. {viewMode === 'table' && (
  1181. <button
  1182. onClick={() => setShowColumnModal(true)}
  1183. 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"
  1184. title={t('inventory.configureColumns')}
  1185. >
  1186. <Columns className="w-4 h-4" />
  1187. <span className="hidden sm:inline">{t('inventory.columns')}</span>
  1188. </button>
  1189. )}
  1190. {/* Group similar toggle — hidden in forecast mode */}
  1191. {viewMode !== 'forecast' && (
  1192. <button
  1193. onClick={toggleGroupSimilar}
  1194. className={`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium border rounded-lg transition-colors ${
  1195. groupSimilar
  1196. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  1197. : 'text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  1198. }`}
  1199. title={t('inventory.groupSimilar')}
  1200. >
  1201. <Group className="w-4 h-4" />
  1202. <span className="hidden sm:inline">{t('inventory.groupSimilar')}</span>
  1203. </button>
  1204. )}
  1205. {/* Table / Cards toggle */}
  1206. <div className="flex bg-bambu-dark-primary border border-bambu-dark-tertiary rounded-lg overflow-hidden">
  1207. <button
  1208. onClick={() => setViewMode('table')}
  1209. className={`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${
  1210. viewMode === 'table'
  1211. ? 'bg-bambu-green text-white'
  1212. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1213. }`}
  1214. >
  1215. <TableProperties className="w-4 h-4" />
  1216. <span className="hidden sm:inline">{t('inventory.table')}</span>
  1217. </button>
  1218. <button
  1219. onClick={() => setViewMode('cards')}
  1220. className={`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${
  1221. viewMode === 'cards'
  1222. ? 'bg-bambu-green text-white'
  1223. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1224. }`}
  1225. >
  1226. <LayoutGrid className="w-4 h-4" />
  1227. <span className="hidden sm:inline">{t('inventory.cards')}</span>
  1228. </button>
  1229. <button
  1230. onClick={() => canViewForecast && setViewMode('forecast')}
  1231. disabled={!canViewForecast}
  1232. title={canViewForecast ? undefined : t('forecast.noReadAccess')}
  1233. 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 ${
  1234. viewMode === 'forecast'
  1235. ? 'bg-bambu-green text-white'
  1236. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1237. }`}
  1238. >
  1239. {canViewForecast ? <TrendingUp className="w-4 h-4" /> : <Lock className="w-4 h-4" />}
  1240. <span className="hidden sm:inline">{t('forecast.title')}</span>
  1241. </button>
  1242. </div>
  1243. </div>
  1244. </div>
  1245. {/* Filter chips row — hidden in forecast mode */}
  1246. <div className={`flex flex-wrap items-center gap-2 ${viewMode === 'forecast' ? 'hidden' : ''}`}>
  1247. {/* Active / Archived chips */}
  1248. <div className="flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden">
  1249. <button
  1250. onClick={() => { setArchiveFilter('active'); resetPage(); }}
  1251. className={`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${
  1252. archiveFilter === 'active'
  1253. ? 'bg-bambu-green/20 text-bambu-green'
  1254. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1255. }`}
  1256. >
  1257. <Package className="w-3.5 h-3.5" />
  1258. {t('inventory.active')}
  1259. </button>
  1260. <button
  1261. onClick={() => { setArchiveFilter('archived'); resetPage(); }}
  1262. className={`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${
  1263. archiveFilter === 'archived'
  1264. ? 'bg-bambu-green/20 text-bambu-green'
  1265. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1266. }`}
  1267. >
  1268. <Archive className="w-3.5 h-3.5" />
  1269. {t('inventory.archived')}
  1270. </button>
  1271. </div>
  1272. <div className="w-px h-5 bg-bambu-dark-tertiary" />
  1273. {/* All / Used / New chips */}
  1274. <div className="flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden">
  1275. <button
  1276. onClick={() => { setUsageFilter('all'); resetPage(); }}
  1277. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  1278. usageFilter === 'all'
  1279. ? 'bg-bambu-green/20 text-bambu-green'
  1280. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1281. }`}
  1282. >
  1283. {t('inventory.all')}
  1284. </button>
  1285. <button
  1286. onClick={() => { setUsageFilter('used'); resetPage(); }}
  1287. className={`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${
  1288. usageFilter === 'used'
  1289. ? 'bg-bambu-green/20 text-bambu-green'
  1290. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1291. }`}
  1292. >
  1293. <Clock className="w-3.5 h-3.5" />
  1294. {t('inventory.used')}
  1295. </button>
  1296. <button
  1297. onClick={() => { setUsageFilter('new'); resetPage(); }}
  1298. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  1299. usageFilter === 'new'
  1300. ? 'bg-bambu-green/20 text-bambu-green'
  1301. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1302. }`}
  1303. >
  1304. {t('inventory.new')}
  1305. </button>
  1306. <button
  1307. onClick={() => { setUsageFilter('lowstock'); resetPage(); }}
  1308. className={`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${
  1309. usageFilter === 'lowstock'
  1310. ? 'bg-yellow-500/20 text-yellow-400'
  1311. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1312. }`}
  1313. >
  1314. <AlertTriangle className="w-3.5 h-3.5" />
  1315. {t('inventory.lowStock')}
  1316. </button>
  1317. </div>
  1318. {/* Stock filter chips */}
  1319. <div className="flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden">
  1320. <button
  1321. onClick={() => { setStockFilter('all'); resetPage(); }}
  1322. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  1323. stockFilter === 'all'
  1324. ? 'bg-bambu-green/20 text-bambu-green'
  1325. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1326. }`}
  1327. >
  1328. {t('inventory.all')}
  1329. </button>
  1330. <button
  1331. onClick={() => { setStockFilter('stock'); resetPage(); }}
  1332. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  1333. stockFilter === 'stock'
  1334. ? 'bg-amber-500/20 text-amber-400'
  1335. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1336. }`}
  1337. >
  1338. {t('inventory.stock')}
  1339. </button>
  1340. <button
  1341. onClick={() => { setStockFilter('configured'); resetPage(); }}
  1342. className={`px-3 py-1.5 text-xs font-medium transition-colors ${
  1343. stockFilter === 'configured'
  1344. ? 'bg-bambu-green/20 text-bambu-green'
  1345. : 'text-bambu-gray hover:bg-bambu-dark-tertiary'
  1346. }`}
  1347. >
  1348. {t('inventory.configured')}
  1349. </button>
  1350. </div>
  1351. <div className="w-px h-5 bg-bambu-dark-tertiary" />
  1352. {/* Material dropdown chip */}
  1353. <select
  1354. value={materialFilter}
  1355. onChange={(e) => { setMaterialFilter(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. materialFilter
  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.material')}</option>
  1363. {uniqueMaterials.map((m) => (
  1364. <option key={m} value={m}>{m}</option>
  1365. ))}
  1366. </select>
  1367. {/* Brand dropdown chip */}
  1368. <select
  1369. value={brandFilter}
  1370. onChange={(e) => { setBrandFilter(e.target.value); resetPage(); }}
  1371. className={`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${
  1372. brandFilter
  1373. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  1374. : 'bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  1375. }`}
  1376. >
  1377. <option value="">{t('inventory.brand')}</option>
  1378. {uniqueBrands.map((b) => (
  1379. <option key={b} value={b}>{b}</option>
  1380. ))}
  1381. </select>
  1382. {/* Category dropdown chip (#729) — only render once at least one
  1383. spool carries a category, otherwise it's noise. */}
  1384. {(uniqueCategories.length > 0 || categoryFilter) && (
  1385. <select
  1386. value={categoryFilter}
  1387. onChange={(e) => { setCategoryFilter(e.target.value); resetPage(); }}
  1388. className={`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${
  1389. categoryFilter
  1390. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  1391. : 'bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  1392. }`}
  1393. >
  1394. <option value="">{t('inventory.category')}</option>
  1395. {uniqueCategories.map((c) => (
  1396. <option key={c} value={c}>{c}</option>
  1397. ))}
  1398. {hasUncategorized && (
  1399. <option value="__none__">{t('inventory.categoryNone')}</option>
  1400. )}
  1401. </select>
  1402. )}
  1403. {/* Spool name dropdown chip */}
  1404. {uniqueSpoolCatalogIds.length > 0 && (
  1405. <select
  1406. value={spoolFilter}
  1407. onChange={(e) => { setSpoolFilter(e.target.value); resetPage(); }}
  1408. className={`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${
  1409. spoolFilter
  1410. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  1411. : 'bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  1412. }`}
  1413. >
  1414. <option value="">{t('inventory.spoolName')}</option>
  1415. {uniqueSpoolCatalogIds.map((id) => (
  1416. <option key={id} value={id}>{catalogMap[id]?.name || `#${id}`}</option>
  1417. ))}
  1418. </select>
  1419. )}
  1420. {/* Storage location dropdown chip (#1400) — only render when at
  1421. least one spool carries a storage location, otherwise it's noise
  1422. (matches the category chip pattern). */}
  1423. {(uniqueStorageLocations.length > 0 || storageLocationFilter) && (
  1424. <select
  1425. value={storageLocationFilter}
  1426. onChange={(e) => { setStorageLocationFilter(e.target.value); resetPage(); }}
  1427. className={`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${
  1428. storageLocationFilter
  1429. ? 'bg-bambu-green/20 text-bambu-green border-bambu-green/30'
  1430. : 'bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary'
  1431. }`}
  1432. >
  1433. <option value="">{t('inventory.storageLocation')}</option>
  1434. {uniqueStorageLocations.map((loc) => (
  1435. <option key={loc} value={loc}>{loc}</option>
  1436. ))}
  1437. {hasUnsetStorageLocation && (
  1438. <option value="__none__">{t('inventory.storageLocationNone')}</option>
  1439. )}
  1440. </select>
  1441. )}
  1442. {/* Clear filters */}
  1443. {hasActiveFilters && (
  1444. <>
  1445. <div className="w-px h-5 bg-bambu-dark-tertiary" />
  1446. <button
  1447. onClick={clearAllFilters}
  1448. className="flex items-center gap-1 text-xs text-bambu-gray hover:text-bambu-green transition-colors"
  1449. >
  1450. <X className="w-3.5 h-3.5" />
  1451. {t('inventory.clearFilters')}
  1452. </button>
  1453. </>
  1454. )}
  1455. {/* Results count — hidden in forecast mode */}
  1456. {viewMode !== 'forecast' && (
  1457. <span className="ml-auto text-xs text-bambu-gray">
  1458. {sortedSpools.length} {sortedSpools.length !== 1 ? t('inventory.spools') : t('inventory.spool')}
  1459. {groupSimilar && totalDisplayItems < sortedSpools.length && ` (${totalDisplayItems} ${t('inventory.groupedRows')})`}
  1460. </span>
  1461. )}
  1462. </div>
  1463. {/* Content */}
  1464. {isLoading ? (
  1465. <div className="flex justify-center py-16">
  1466. <Loader2 className="w-8 h-8 text-bambu-green animate-spin" />
  1467. </div>
  1468. ) : viewMode === 'forecast' ? (
  1469. /* Forecast view */
  1470. <ForecastPanel spools={spools || []} />
  1471. ) : viewMode === 'cards' ? (
  1472. /* Cards view */
  1473. pagedItems.length > 0 ? (
  1474. <>
  1475. <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
  1476. {pagedItems.map((item) => {
  1477. if (item.type === 'group') {
  1478. const { key, spools: groupSpools, representative: rep } = item;
  1479. const groupBannerStyle = buildFilamentBackground({
  1480. rgba: rep.rgba,
  1481. extraColors: rep.extra_colors,
  1482. effectType: rep.effect_type,
  1483. subtype: rep.subtype,
  1484. effectSize: 'groupheader',
  1485. });
  1486. const isExpanded = expandedGroups.has(key);
  1487. return (
  1488. <div key={`group-${key}`} className="col-span-full">
  1489. {/* Group header card */}
  1490. <div
  1491. className="bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-green/30 hover:border-bambu-green transition-colors cursor-pointer"
  1492. onClick={() => toggleGroupExpand(key)}
  1493. >
  1494. <div className="h-10 flex items-center px-4 gap-3" style={groupBannerStyle}>
  1495. <span className="bg-white/90 text-gray-800 px-3 py-0.5 rounded-full text-sm font-medium">
  1496. {resolveSpoolColorName(rep.color_name, rep.rgba) || '-'}
  1497. </span>
  1498. </div>
  1499. <div className="px-4 py-3 flex items-center justify-between">
  1500. <div className="flex items-center gap-3">
  1501. <ChevronDown className={`w-4 h-4 text-bambu-gray transition-transform ${isExpanded ? '' : '-rotate-90'}`} />
  1502. <div>
  1503. <h3 className="font-semibold text-white">{rep.material}{rep.subtype ? ` ${rep.subtype}` : ''}</h3>
  1504. <p className="text-sm text-bambu-gray">{rep.brand || '-'}</p>
  1505. </div>
  1506. </div>
  1507. <div className="flex items-center gap-2">
  1508. <span className="text-sm text-bambu-gray">{formatWeight(rep.label_weight)}</span>
  1509. <span className="text-xs font-medium bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full">
  1510. {t('inventory.groupedSpools', { count: groupSpools.length })}
  1511. </span>
  1512. </div>
  1513. </div>
  1514. </div>
  1515. {/* Expanded individual spools */}
  1516. {isExpanded && (
  1517. <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4 mt-2 ml-4">
  1518. {groupSpools.map((spool) => {
  1519. const remaining = Math.max(0, spool.label_weight - spool.weight_used);
  1520. const pct = spool.label_weight > 0 ? (remaining / spool.label_weight) * 100 : 0;
  1521. return (
  1522. <SpoolCard
  1523. key={spool.id}
  1524. spool={spool}
  1525. remaining={remaining}
  1526. pct={pct}
  1527. onClick={() => setFormModal({ spool, mode: 'edit' })}
  1528. onPrintLabel={() => setLabelPickerSpoolIds([spool.id])}
  1529. onCopy={() => setFormModal({ spool: spool, mode: 'copy' })}
  1530. t={t}
  1531. />
  1532. );
  1533. })}
  1534. </div>
  1535. )}
  1536. </div>
  1537. );
  1538. }
  1539. const spool = item.spool;
  1540. const remaining = Math.max(0, spool.label_weight - spool.weight_used);
  1541. const pct = spool.label_weight > 0 ? (remaining / spool.label_weight) * 100 : 0;
  1542. return (
  1543. <SpoolCard
  1544. key={spool.id}
  1545. spool={spool}
  1546. remaining={remaining}
  1547. pct={pct}
  1548. onClick={() => setFormModal({ spool, mode: 'edit' })}
  1549. onPrintLabel={() => setLabelPickerSpoolIds([spool.id])}
  1550. onCopy={() => setFormModal({ spool: spool, mode: 'copy' })}
  1551. t={t}
  1552. />
  1553. );
  1554. })}
  1555. </div>
  1556. {/* Pagination for cards */}
  1557. <PaginationBar
  1558. pageIndex={safePageIndex}
  1559. pageSize={pageSize}
  1560. totalRows={totalDisplayItems}
  1561. totalPages={totalPages}
  1562. onPageChange={setPageIndex}
  1563. onPageSizeChange={handlePageSizeChange}
  1564. t={t}
  1565. />
  1566. </>
  1567. ) : (
  1568. <EmptyFilterState
  1569. hasFilters={hasActiveFilters}
  1570. onAddSpool={() => setFormModal({ spool: null, mode: 'create' })}
  1571. t={t}
  1572. />
  1573. )
  1574. ) : (
  1575. /* Table view */
  1576. pagedItems.length > 0 ? (
  1577. <div className="bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary">
  1578. <div className="overflow-x-auto">
  1579. <table className="w-full">
  1580. <thead>
  1581. <tr className="border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30">
  1582. {visibleColumns.map((colId) => {
  1583. const sortable = !!columnSortValues[colId];
  1584. const isActive = sortState?.column === colId;
  1585. return (
  1586. <th
  1587. key={colId}
  1588. className={`text-left py-3 px-4 text-xs font-medium uppercase tracking-wide select-none ${colId === 'remaining' ? 'min-w-[150px]' : ''} ${
  1589. sortable ? 'cursor-pointer hover:text-bambu-green transition-colors' : ''
  1590. } ${isActive ? 'text-bambu-green' : 'text-bambu-gray'}`}
  1591. onClick={sortable ? () => handleSort(colId) : undefined}
  1592. >
  1593. <span className="inline-flex items-center gap-1">
  1594. {columnHeaders[colId]?.(t) ?? colId}
  1595. {sortable && (
  1596. isActive
  1597. ? sortState.direction === 'asc'
  1598. ? <ArrowUp className="w-3 h-3" />
  1599. : <ArrowDown className="w-3 h-3" />
  1600. : <ArrowUpDown className="w-3 h-3 opacity-30" />
  1601. )}
  1602. </span>
  1603. </th>
  1604. );
  1605. })}
  1606. <th className="text-right py-3 px-4 text-xs font-medium text-bambu-gray uppercase tracking-wide">{t('common.actions')}</th>
  1607. </tr>
  1608. </thead>
  1609. <tbody>
  1610. {pagedItems.map((item) => {
  1611. if (item.type === 'group') {
  1612. const { key, spools: groupSpools, representative: rep } = item;
  1613. const isExpanded = expandedGroups.has(key);
  1614. const remaining = Math.max(0, rep.label_weight - rep.weight_used);
  1615. const pct = rep.label_weight > 0 ? (remaining / rep.label_weight) * 100 : 0;
  1616. return (
  1617. <SpoolTableGroup
  1618. key={`group-${key}`}
  1619. spools={groupSpools}
  1620. representative={rep}
  1621. remaining={remaining}
  1622. pct={pct}
  1623. isExpanded={isExpanded}
  1624. onToggle={() => toggleGroupExpand(key)}
  1625. onEdit={(s) => setFormModal({ spool: s, mode: 'edit' })}
  1626. onCopy={(s) => setFormModal({ spool: s, mode: 'copy' })}
  1627. onArchive={(id) => setConfirmAction({ type: 'archive', spoolId: id })}
  1628. onDelete={(id) => setConfirmAction({ type: 'delete', spoolId: id })}
  1629. onPrintLabel={(id) => setLabelPickerSpoolIds([id])}
  1630. onResetUsage={(id) => setConfirmAction({ type: 'reset-usage', spoolId: id })}
  1631. visibleColumns={visibleColumns}
  1632. assignmentMap={assignmentMap}
  1633. catalogMap={catalogMap}
  1634. currencySymbol={currencySymbol}
  1635. dateFormat={dateFormat}
  1636. t={t}
  1637. onSyncWeight={handleSyncWeight}
  1638. />
  1639. );
  1640. }
  1641. const spool = item.spool;
  1642. const remaining = Math.max(0, spool.label_weight - spool.weight_used);
  1643. const pct = spool.label_weight > 0 ? (remaining / spool.label_weight) * 100 : 0;
  1644. return (
  1645. <SpoolTableRow
  1646. key={spool.id}
  1647. spool={spool}
  1648. remaining={remaining}
  1649. pct={pct}
  1650. onEdit={() => setFormModal({ spool, mode: 'edit' })}
  1651. onCopy={() => setFormModal({ spool: spool, mode: 'copy' })}
  1652. onRestore={() => restoreMutation.mutate(spool.id)}
  1653. onArchive={() => setConfirmAction({ type: 'archive', spoolId: spool.id })}
  1654. onDelete={() => setConfirmAction({ type: 'delete', spoolId: spool.id })}
  1655. onPrintLabel={() => setLabelPickerSpoolIds([spool.id])}
  1656. onResetUsage={() => setConfirmAction({ type: 'reset-usage', spoolId: spool.id })}
  1657. visibleColumns={visibleColumns}
  1658. assignmentMap={assignmentMap}
  1659. catalogMap={catalogMap}
  1660. currencySymbol={currencySymbol}
  1661. dateFormat={dateFormat}
  1662. t={t}
  1663. onSyncWeight={handleSyncWeight}
  1664. />
  1665. );
  1666. })}
  1667. </tbody>
  1668. </table>
  1669. </div>
  1670. {/* Pagination inside card footer */}
  1671. <div className="flex items-center justify-between px-4 py-3 bg-bambu-dark-tertiary/50 border-t border-bambu-dark-tertiary text-sm">
  1672. <span className="text-bambu-gray">
  1673. {showAll
  1674. ? `${totalDisplayItems} ${totalDisplayItems !== 1 ? t('inventory.spools') : t('inventory.spool')}`
  1675. : <>{t('inventory.showing')} {safePageIndex * effectivePageSize + 1} {t('inventory.to')}{' '}
  1676. {Math.min((safePageIndex + 1) * effectivePageSize, totalDisplayItems)}{' '}
  1677. {t('inventory.of')} {totalDisplayItems} {t('inventory.spools')}</>
  1678. }
  1679. </span>
  1680. <div className="flex items-center gap-2">
  1681. <span className="text-bambu-gray">{t('inventory.show')}</span>
  1682. <select
  1683. value={pageSize}
  1684. onChange={(e) => handlePageSizeChange(Number(e.target.value))}
  1685. 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"
  1686. >
  1687. {[15, 30, 50, 100].map((n) => (
  1688. <option key={n} value={n}>{n}</option>
  1689. ))}
  1690. <option value={-1}>{t('inventory.all')}</option>
  1691. </select>
  1692. {!showAll && (
  1693. <>
  1694. <button
  1695. onClick={() => setPageIndex(0)}
  1696. disabled={safePageIndex === 0}
  1697. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1698. title="First page"
  1699. >
  1700. <ChevronsLeft className="w-4 h-4" />
  1701. </button>
  1702. <button
  1703. onClick={() => setPageIndex((p) => Math.max(0, p - 1))}
  1704. disabled={safePageIndex === 0}
  1705. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1706. >
  1707. <ChevronLeft className="w-4 h-4" />
  1708. </button>
  1709. <span className="text-bambu-gray px-2 whitespace-nowrap">
  1710. {t('inventory.page')} {safePageIndex + 1} {t('inventory.of')} {totalPages}
  1711. </span>
  1712. <button
  1713. onClick={() => setPageIndex((p) => Math.min(totalPages - 1, p + 1))}
  1714. disabled={safePageIndex >= totalPages - 1}
  1715. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1716. >
  1717. <ChevronRight className="w-4 h-4" />
  1718. </button>
  1719. <button
  1720. onClick={() => setPageIndex(totalPages - 1)}
  1721. disabled={safePageIndex >= totalPages - 1}
  1722. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1723. title="Last page"
  1724. >
  1725. <ChevronsRight className="w-4 h-4" />
  1726. </button>
  1727. </>
  1728. )}
  1729. </div>
  1730. </div>
  1731. </div>
  1732. ) : (
  1733. <EmptyFilterState
  1734. hasFilters={hasActiveFilters}
  1735. onAddSpool={() => setFormModal({ spool: null, mode: 'create' })}
  1736. t={t}
  1737. />
  1738. )
  1739. )}
  1740. {/* Spool Form Modal */}
  1741. {formModal !== null && (
  1742. <SpoolFormModal
  1743. isOpen={true}
  1744. onClose={() => setFormModal(null)}
  1745. spool={formModal.spool}
  1746. mode={formModal.mode}
  1747. currencySymbol={currencySymbol}
  1748. spoolmanMode={spoolmanMode}
  1749. spoolsQueryKey={spoolsQueryKey}
  1750. />
  1751. )}
  1752. {/* Confirm Modal (delete / archive / reset-usage / reset-all-usage) */}
  1753. {confirmAction && (
  1754. <ConfirmModal
  1755. title={
  1756. confirmAction.type === 'delete' ? t('common.delete') :
  1757. confirmAction.type === 'archive' ? t('inventory.archive') :
  1758. confirmAction.type === 'reset-usage' ? t('inventory.resetUsage') :
  1759. t('inventory.resetAllUsage')
  1760. }
  1761. message={
  1762. confirmAction.type === 'delete' ? t('inventory.deleteConfirm') :
  1763. confirmAction.type === 'archive' ? t('inventory.archiveConfirm') :
  1764. confirmAction.type === 'reset-usage' ? t('inventory.resetUsageConfirm') :
  1765. t('inventory.resetAllUsageConfirm', { count: resetableSpoolIds.length })
  1766. }
  1767. confirmText={
  1768. confirmAction.type === 'delete' ? t('common.delete') :
  1769. confirmAction.type === 'archive' ? t('inventory.archive') :
  1770. t('inventory.resetUsage')
  1771. }
  1772. variant={confirmAction.type === 'archive' ? 'warning' : 'danger'}
  1773. onConfirm={() => {
  1774. if (confirmAction.type === 'delete') {
  1775. deleteMutation.mutate(confirmAction.spoolId);
  1776. } else if (confirmAction.type === 'archive') {
  1777. archiveMutation.mutate(confirmAction.spoolId);
  1778. } else if (confirmAction.type === 'reset-usage') {
  1779. resetUsageMutation.mutate(confirmAction.spoolId);
  1780. } else {
  1781. bulkResetUsageMutation.mutate(resetableSpoolIds);
  1782. }
  1783. setConfirmAction(null);
  1784. }}
  1785. onCancel={() => setConfirmAction(null)}
  1786. />
  1787. )}
  1788. {/* Column Config Modal */}
  1789. <ColumnConfigModal
  1790. isOpen={showColumnModal}
  1791. onClose={() => setShowColumnModal(false)}
  1792. columns={columnConfig}
  1793. defaultColumns={DEFAULT_COLUMNS}
  1794. onSave={handleColumnConfigSave}
  1795. />
  1796. <LabelTemplatePickerModal
  1797. isOpen={labelPickerSpoolIds !== null}
  1798. onClose={() => setLabelPickerSpoolIds(null)}
  1799. availableSpools={filteredSpools}
  1800. initialSelectedIds={labelPickerSpoolIds ?? []}
  1801. spoolmanMode={spoolmanMode}
  1802. />
  1803. </div>
  1804. );
  1805. }
  1806. /* Pagination bar (reused for cards view) */
  1807. function PaginationBar({
  1808. pageIndex, pageSize, totalRows, totalPages, onPageChange, onPageSizeChange, t,
  1809. }: {
  1810. pageIndex: number;
  1811. pageSize: number;
  1812. totalRows: number;
  1813. totalPages: number;
  1814. onPageChange: (page: number) => void;
  1815. onPageSizeChange: (size: number) => void;
  1816. t: (key: string) => string;
  1817. }) {
  1818. const isShowAll = pageSize === -1;
  1819. if (totalPages <= 1 && !isShowAll) return null;
  1820. const effectiveSize = isShowAll ? totalRows || 1 : pageSize;
  1821. return (
  1822. <div className="flex items-center justify-between pt-2 text-sm">
  1823. <span className="text-bambu-gray">
  1824. {isShowAll
  1825. ? `${totalRows} ${totalRows !== 1 ? t('inventory.spools') : t('inventory.spool')}`
  1826. : <>{t('inventory.showing')} {pageIndex * effectiveSize + 1} {t('inventory.to')}{' '}
  1827. {Math.min((pageIndex + 1) * effectiveSize, totalRows)}{' '}
  1828. {t('inventory.of')} {totalRows} {t('inventory.spools')}</>
  1829. }
  1830. </span>
  1831. <div className="flex items-center gap-2">
  1832. <span className="text-bambu-gray">{t('inventory.show')}</span>
  1833. <select
  1834. value={pageSize}
  1835. onChange={(e) => onPageSizeChange(Number(e.target.value))}
  1836. 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"
  1837. >
  1838. {[15, 30, 50, 100].map((n) => (
  1839. <option key={n} value={n}>{n}</option>
  1840. ))}
  1841. <option value={-1}>{t('inventory.all')}</option>
  1842. </select>
  1843. {!isShowAll && (
  1844. <>
  1845. <button
  1846. onClick={() => onPageChange(0)}
  1847. disabled={pageIndex === 0}
  1848. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1849. >
  1850. <ChevronsLeft className="w-4 h-4" />
  1851. </button>
  1852. <button
  1853. onClick={() => onPageChange(Math.max(0, pageIndex - 1))}
  1854. disabled={pageIndex === 0}
  1855. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1856. >
  1857. <ChevronLeft className="w-4 h-4" />
  1858. </button>
  1859. <span className="text-bambu-gray px-2 whitespace-nowrap">
  1860. {t('inventory.page')} {pageIndex + 1} {t('inventory.of')} {totalPages}
  1861. </span>
  1862. <button
  1863. onClick={() => onPageChange(Math.min(totalPages - 1, pageIndex + 1))}
  1864. disabled={pageIndex >= totalPages - 1}
  1865. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1866. >
  1867. <ChevronRight className="w-4 h-4" />
  1868. </button>
  1869. <button
  1870. onClick={() => onPageChange(totalPages - 1)}
  1871. disabled={pageIndex >= totalPages - 1}
  1872. className="p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
  1873. >
  1874. <ChevronsRight className="w-4 h-4" />
  1875. </button>
  1876. </>
  1877. )}
  1878. </div>
  1879. </div>
  1880. );
  1881. }
  1882. /* Spool card for cards view */
  1883. function SpoolCard({
  1884. spool, remaining, pct, onClick, onPrintLabel, onCopy, t,
  1885. }: {
  1886. spool: InventorySpool;
  1887. remaining: number;
  1888. pct: number;
  1889. onClick: () => void;
  1890. onPrintLabel?: () => void;
  1891. onCopy?: () => void;
  1892. t: (key: string, opts?: Record<string, unknown>) => string;
  1893. }) {
  1894. const bannerStyle = buildFilamentBackground({
  1895. rgba: spool.rgba,
  1896. extraColors: spool.extra_colors,
  1897. effectType: spool.effect_type,
  1898. subtype: spool.subtype,
  1899. effectSize: 'card',
  1900. });
  1901. return (
  1902. <div
  1903. 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' : ''}`}
  1904. onClick={onClick}
  1905. >
  1906. <div className="h-14 flex items-center justify-center" style={bannerStyle}>
  1907. <span className="bg-white/90 text-gray-800 px-3 py-0.5 rounded-full text-sm font-medium">
  1908. {resolveSpoolColorName(spool.color_name, spool.rgba) || '-'}
  1909. </span>
  1910. {onCopy && (
  1911. <button
  1912. type="button"
  1913. onClick={(e) => { e.stopPropagation(); onCopy(); }}
  1914. className="p-1.5 bg-black/20 hover:bg-black/40 text-white rounded-full transition-colors"
  1915. title={t('inventory.copySpool')}
  1916. >
  1917. <Copy className="w-3.5 h-3.5" />
  1918. </button>
  1919. )}
  1920. </div>
  1921. <div className="p-4 space-y-3">
  1922. <div className="flex items-start justify-between gap-2">
  1923. <div>
  1924. <h3 className="font-semibold text-white">
  1925. {spool.material}{spool.subtype ? ` ${spool.subtype}` : ''}
  1926. </h3>
  1927. <p className="text-sm text-bambu-gray">{spool.brand || '-'}</p>
  1928. </div>
  1929. <div className="flex items-center gap-1">
  1930. {onPrintLabel && (
  1931. <button
  1932. onClick={(e) => { e.stopPropagation(); onPrintLabel(); }}
  1933. className="p-1 text-bambu-gray hover:text-white rounded transition-colors"
  1934. title={t('inventory.labels.printOne')}
  1935. aria-label={t('inventory.labels.printOne')}
  1936. >
  1937. <Printer className="w-4 h-4" />
  1938. </button>
  1939. )}
  1940. <span className="text-xs font-mono text-bambu-gray bg-bambu-dark-tertiary px-2 py-1 rounded">
  1941. #{spool.id}
  1942. </span>
  1943. </div>
  1944. </div>
  1945. <div>
  1946. <div className="flex justify-between text-xs text-bambu-gray mb-1">
  1947. <span>{t('inventory.remaining')}</span>
  1948. <span>{Math.round(pct)}%</span>
  1949. </div>
  1950. <div className="flex items-center gap-2">
  1951. <div className="flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden">
  1952. <div
  1953. className={`h-full rounded-full ${pct > 50 ? 'bg-bambu-green' : pct > 20 ? 'bg-yellow-500' : 'bg-red-500'}`}
  1954. style={{ width: `${Math.min(pct, 100)}%` }}
  1955. />
  1956. </div>
  1957. <span className="text-xs text-bambu-gray min-w-[40px] text-right">
  1958. {Math.round(remaining)}g
  1959. </span>
  1960. </div>
  1961. </div>
  1962. <div className="grid grid-cols-2 gap-2 text-xs">
  1963. <div>
  1964. <span className="text-bambu-gray/60">{t('inventory.labelWeight')}: </span>
  1965. <span className="text-bambu-gray">{formatWeight(spool.label_weight)}</span>
  1966. </div>
  1967. <div>
  1968. <span className="text-bambu-gray/60">{t('inventory.weightUsed')}: </span>
  1969. <span className="text-bambu-gray">
  1970. {spool.weight_used > 0 ? formatWeight(spool.weight_used) : '-'}
  1971. </span>
  1972. </div>
  1973. </div>
  1974. {spool.note && (
  1975. <div
  1976. className="text-xs text-bambu-gray/60 pt-2 border-t border-bambu-dark-tertiary truncate"
  1977. title={spool.note}
  1978. >
  1979. {spool.note}
  1980. </div>
  1981. )}
  1982. </div>
  1983. </div>
  1984. );
  1985. }
  1986. /* Single spool row for table view */
  1987. function SpoolTableRow({
  1988. spool, remaining, pct, onEdit, onCopy, onRestore, onArchive, onDelete, onPrintLabel, onResetUsage,
  1989. visibleColumns, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight,
  1990. }: {
  1991. spool: InventorySpool;
  1992. remaining: number;
  1993. pct: number;
  1994. onEdit: () => void;
  1995. onCopy?: () => void;
  1996. onRestore: () => void;
  1997. onArchive: () => void;
  1998. onDelete: () => void;
  1999. onPrintLabel?: () => void;
  2000. onResetUsage?: () => void;
  2001. visibleColumns: string[];
  2002. assignmentMap: Record<number, LocationDisplay>;
  2003. catalogMap: Record<number, SpoolCatalogEntry>;
  2004. currencySymbol: string;
  2005. dateFormat: DateFormat;
  2006. t: TFn;
  2007. onSyncWeight?: (spool: InventorySpool) => void;
  2008. }) {
  2009. return (
  2010. <tr
  2011. className={`border-b border-bambu-dark-tertiary/50 hover:bg-bambu-dark-tertiary/30 transition-colors cursor-pointer ${
  2012. spool.archived_at ? 'opacity-50' : ''
  2013. }`}
  2014. onClick={onEdit}
  2015. >
  2016. {visibleColumns.map((colId) => (
  2017. <td key={colId} className="py-3 px-4">
  2018. {columnCells[colId]?.({ spool, remaining, pct, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight })}
  2019. </td>
  2020. ))}
  2021. <td className="py-3 px-4">
  2022. <div className="flex items-center justify-end gap-1" onClick={(e) => e.stopPropagation()}>
  2023. <button onClick={onEdit} className="p-1.5 text-bambu-gray hover:text-white rounded transition-colors" title={t('common.edit')}>
  2024. <Edit2 className="w-4 h-4" />
  2025. </button>
  2026. {onCopy && (
  2027. <button onClick={onCopy} className="p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors" title={t('inventory.copySpool')}>
  2028. <Copy className="w-4 h-4" />
  2029. </button>
  2030. )}
  2031. {onPrintLabel && (
  2032. <button onClick={onPrintLabel} className="p-1.5 text-bambu-gray hover:text-white rounded transition-colors" title={t('inventory.labels.printOne')}>
  2033. <Printer className="w-4 h-4" />
  2034. </button>
  2035. )}
  2036. {onResetUsage && spool.weight_used > 0 && (
  2037. // Eraser also shows on archived spools (#1390 follow-up):
  2038. // archived consumed weight now counts in "Total Consumed", so
  2039. // the user needs a way to zero an archived spool's tracking
  2040. // counter individually without having to un-archive it first.
  2041. <button onClick={onResetUsage} className="p-1.5 text-bambu-gray hover:text-orange-400 rounded transition-colors" title={t('inventory.resetUsageTooltip')}>
  2042. <Eraser className="w-4 h-4" />
  2043. </button>
  2044. )}
  2045. {spool.archived_at ? (
  2046. <button onClick={onRestore} className="p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors" title={t('inventory.restore')}>
  2047. <RotateCcw className="w-4 h-4" />
  2048. </button>
  2049. ) : (
  2050. <button onClick={onArchive} className="p-1.5 text-bambu-gray hover:text-yellow-400 rounded transition-colors" title={t('inventory.archive')}>
  2051. <Archive className="w-4 h-4" />
  2052. </button>
  2053. )}
  2054. <button onClick={onDelete} className="p-1.5 text-bambu-gray hover:text-red-400 rounded transition-colors" title={t('common.delete')}>
  2055. <Trash2 className="w-4 h-4" />
  2056. </button>
  2057. </div>
  2058. </td>
  2059. </tr>
  2060. );
  2061. }
  2062. /* Grouped spool rows for table view */
  2063. function SpoolTableGroup({
  2064. spools, representative, remaining, pct, isExpanded, onToggle,
  2065. onEdit, onCopy, onArchive, onDelete, onPrintLabel, onResetUsage,
  2066. visibleColumns, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight,
  2067. }: {
  2068. spools: InventorySpool[];
  2069. representative: InventorySpool;
  2070. remaining: number;
  2071. pct: number;
  2072. isExpanded: boolean;
  2073. onToggle: () => void;
  2074. onEdit: (spool: InventorySpool) => void;
  2075. onCopy?: (spool: InventorySpool) => void;
  2076. onArchive: (id: number) => void;
  2077. onDelete: (id: number) => void;
  2078. onPrintLabel?: (spoolId: number) => void;
  2079. onResetUsage?: (id: number) => void;
  2080. visibleColumns: string[];
  2081. assignmentMap: Record<number, LocationDisplay>;
  2082. catalogMap: Record<number, SpoolCatalogEntry>;
  2083. currencySymbol: string;
  2084. dateFormat: DateFormat;
  2085. t: TFn;
  2086. onSyncWeight?: (spool: InventorySpool) => void;
  2087. }) {
  2088. return (
  2089. <>
  2090. {/* Group header row */}
  2091. <tr
  2092. className="border-b border-bambu-dark-tertiary/50 hover:bg-bambu-dark-tertiary/30 transition-colors cursor-pointer bg-bambu-green/5"
  2093. onClick={onToggle}
  2094. >
  2095. {visibleColumns.map((colId, idx) => (
  2096. <td key={colId} className="py-3 px-4">
  2097. {idx === 0 ? (
  2098. <div className="flex items-center gap-2">
  2099. <ChevronDown className={`w-4 h-4 text-bambu-gray transition-transform ${isExpanded ? '' : '-rotate-90'}`} />
  2100. {columnCells[colId]?.({ spool: representative, remaining, pct, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight })}
  2101. </div>
  2102. ) : colId === 'id' ? (
  2103. <span className="text-xs font-medium bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full">
  2104. {t('inventory.groupedSpools', { count: spools.length })}
  2105. </span>
  2106. ) : (
  2107. columnCells[colId]?.({ spool: representative, remaining, pct, assignmentMap, catalogMap, currencySymbol, dateFormat, t, onSyncWeight })
  2108. )}
  2109. </td>
  2110. ))}
  2111. <td className="py-3 px-4">
  2112. <span className="text-xs text-bambu-gray">
  2113. {spools.map((s) => `#${s.id}`).join(', ')}
  2114. </span>
  2115. </td>
  2116. </tr>
  2117. {/* Expanded individual rows */}
  2118. {isExpanded && spools.map((spool) => {
  2119. const r = Math.max(0, spool.label_weight - spool.weight_used);
  2120. const p = spool.label_weight > 0 ? (r / spool.label_weight) * 100 : 0;
  2121. return (
  2122. <SpoolTableRow
  2123. key={spool.id}
  2124. spool={spool}
  2125. remaining={r}
  2126. pct={p}
  2127. onEdit={() => onEdit(spool)}
  2128. onCopy={onCopy ? () => onCopy(spool) : undefined}
  2129. onRestore={() => {}}
  2130. onArchive={() => onArchive(spool.id)}
  2131. onDelete={() => onDelete(spool.id)}
  2132. onPrintLabel={onPrintLabel ? () => onPrintLabel(spool.id) : undefined}
  2133. onResetUsage={onResetUsage ? () => onResetUsage(spool.id) : undefined}
  2134. visibleColumns={visibleColumns}
  2135. assignmentMap={assignmentMap}
  2136. catalogMap={catalogMap}
  2137. currencySymbol={currencySymbol}
  2138. dateFormat={dateFormat}
  2139. t={t}
  2140. onSyncWeight={onSyncWeight}
  2141. />
  2142. );
  2143. })}
  2144. </>
  2145. );
  2146. }
  2147. /* Empty state matching SpoolBuddy's design */
  2148. function EmptyFilterState({
  2149. hasFilters,
  2150. onAddSpool,
  2151. t,
  2152. }: {
  2153. hasFilters: boolean;
  2154. onAddSpool: () => void;
  2155. t: (key: string) => string;
  2156. }) {
  2157. return (
  2158. <div className="flex flex-col items-center justify-center py-16 px-4">
  2159. <div className="relative mb-6">
  2160. <div className="absolute inset-0 -m-4 bg-bambu-green/5 rounded-full blur-2xl" />
  2161. <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">
  2162. <div className="absolute -top-1 -right-1 w-3 h-3 rounded-full bg-bambu-green/30" />
  2163. <div className="absolute -bottom-2 -left-2 w-2 h-2 rounded-full bg-bambu-green/20" />
  2164. {hasFilters ? (
  2165. <Search className="w-10 h-10 text-bambu-gray/40" strokeWidth={1.5} />
  2166. ) : (
  2167. <div className="relative">
  2168. <div className="w-14 h-14 rounded-full border-4 border-bambu-gray/20 flex items-center justify-center">
  2169. <div className="w-6 h-6 rounded-full bg-bambu-gray/10 border-2 border-bambu-gray/20" />
  2170. </div>
  2171. <div className="absolute -bottom-1 -right-1 w-6 h-6 rounded-full bg-bambu-green flex items-center justify-center shadow-md">
  2172. <span className="text-white text-lg font-bold leading-none">+</span>
  2173. </div>
  2174. </div>
  2175. )}
  2176. </div>
  2177. </div>
  2178. <h3 className="text-lg font-semibold text-white mb-2 text-center">
  2179. {hasFilters ? t('inventory.noSpoolsMatch') : t('inventory.noSpools').split('.')[0]}
  2180. </h3>
  2181. <p className="text-sm text-bambu-gray text-center max-w-sm mb-6">
  2182. {hasFilters
  2183. ? t('inventory.noSpoolsMatchDesc')
  2184. : t('inventory.noSpools')
  2185. }
  2186. </p>
  2187. {!hasFilters && (
  2188. <Button onClick={onAddSpool}>
  2189. <Package className="w-4 h-4" />
  2190. {t('inventory.addSpool')}
  2191. </Button>
  2192. )}
  2193. </div>
  2194. );
  2195. }