AssignSpoolModal.tsx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. import { useEffect, useMemo, useState } from 'react';
  2. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  3. import { useTranslation } from 'react-i18next';
  4. import { X, Loader2, Package, Search } from 'lucide-react';
  5. import { api } from '../api/client';
  6. import type { InventorySpool, SpoolAssignment } from '../api/client';
  7. import { Button } from './Button';
  8. import { ConfirmModal } from './ConfirmModal';
  9. import { useToast } from '../contexts/ToastContext';
  10. import { filterSpoolsByQuery } from '../utils/inventorySearch';
  11. interface AssignSpoolModalProps {
  12. isOpen: boolean;
  13. onClose: () => void;
  14. printerId: number;
  15. amsId: number;
  16. trayId: number;
  17. trayInfo?: {
  18. type: string;
  19. material?: string;
  20. profile?: string;
  21. color: string;
  22. location: string;
  23. };
  24. spoolmanEnabled?: boolean;
  25. }
  26. export function AssignSpoolModal({ isOpen, onClose, printerId, amsId, trayId, trayInfo, spoolmanEnabled }: AssignSpoolModalProps) {
  27. const { t } = useTranslation();
  28. const queryClient = useQueryClient();
  29. const { showToast } = useToast();
  30. const [disableFiltering, setDisableFiltering] = useState(false);
  31. const [selectedSpoolId, setSelectedSpoolId] = useState<number | null>(null);
  32. const [selectedSpoolmanSpoolId, setSelectedSpoolmanSpoolId] = useState<number | null>(null);
  33. useEffect(() => {
  34. setSelectedSpoolId(null);
  35. setSelectedSpoolmanSpoolId(null);
  36. }, [disableFiltering]);
  37. const [searchFilter, setSearchFilter] = useState('');
  38. const [pendingAssignId, setPendingAssignId] = useState<number | null>(null);
  39. const [showMismatchConfirm, setShowMismatchConfirm] = useState(false);
  40. const [mismatchDetails, setMismatchDetails] = useState<{
  41. type: 'material' | 'partial' | 'profile' | 'material_profile' | 'partial_profile';
  42. spoolMaterial: string;
  43. trayMaterial: string;
  44. spoolProfile?: string;
  45. trayProfile?: string;
  46. } | null>(null);
  47. useEffect(() => {
  48. if (isOpen) {
  49. setDisableFiltering(false);
  50. }
  51. }, [isOpen]);
  52. // Unique cache key — different consumers of `['inventory-spools']` call
  53. // `getSpools()` with different `includeArchived` arguments (InventoryPage:
  54. // true, SpoolBuddyDashboard / SpoolBuddyInventoryPage: false), but they
  55. // all share the same key. React Query treats them as one query and
  56. // serves whichever response landed first, so a SpoolBuddy component
  57. // priming the cache with the archived-excluded payload makes the picker
  58. // miss spools that *are* archived OR (more subtly) miss any spool that
  59. // wasn't yet present when SpoolBuddy ran its initial fetch. The picker
  60. // gets its own key + a fetch-everything call so this consumer is never
  61. // at the mercy of someone else's cache state. Archived spools are then
  62. // explicitly excluded client-side because the backend rejects archived
  63. // assignments with HTTP 400 anyway, so listing them would only let the
  64. // user click a button that fails.
  65. const { data: spools, isLoading } = useQuery({
  66. queryKey: ['inventory-spools', 'assign-modal'],
  67. queryFn: () => api.getSpools(true),
  68. enabled: isOpen && !spoolmanEnabled,
  69. });
  70. const { data: assignments } = useQuery({
  71. queryKey: ['spool-assignments'],
  72. queryFn: () => api.getAssignments(),
  73. enabled: isOpen,
  74. });
  75. const { data: settings } = useQuery({
  76. queryKey: ['settings'],
  77. queryFn: () => api.getSettings(),
  78. enabled: isOpen,
  79. });
  80. const { data: spoolmanSpools, isLoading: spoolmanLoading } = useQuery({
  81. queryKey: ['spoolman-inventory-spools', 'assign-modal'],
  82. queryFn: () => api.getSpoolmanInventorySpools(false),
  83. enabled: isOpen && !!spoolmanEnabled,
  84. });
  85. // Spoolman SlotAssignments across all printers — used to filter out spools
  86. // already bound to another slot. Without this filter the modal offers spools
  87. // that are already in use elsewhere (e.g. an h2d-1 slot's spool appearing
  88. // in the x1c-2 assign list), and assigning would silently steal it from
  89. // the other printer's slot.
  90. const { data: allSpoolmanAssignments } = useQuery({
  91. queryKey: ['spoolman-slot-assignments-all'],
  92. queryFn: () => api.getSpoolmanSlotAssignments(),
  93. enabled: isOpen && !!spoolmanEnabled,
  94. });
  95. // ids of spools already in some Spoolman slot — excluding the current slot
  96. // (so a user could in theory re-pick the same spool, though the modal is
  97. // typically only opened from empty slots).
  98. const assignedSpoolmanSpoolIds = useMemo(() => {
  99. if (!allSpoolmanAssignments) return new Set<number>();
  100. return new Set(
  101. allSpoolmanAssignments
  102. .filter(a => !(a.printer_id === printerId && a.ams_id === amsId && a.tray_id === trayId))
  103. .map(a => a.spoolman_spool_id),
  104. );
  105. }, [allSpoolmanAssignments, printerId, amsId, trayId]);
  106. // #1414: nudge the printer to republish its state after we assign a
  107. // spool. The backend assign-spool path already issues an MQTT command,
  108. // but firmware (especially A1 mini external slots and any non-RFID
  109. // assignment) doesn't always echo the new tray state back on its own,
  110. // so the printer card sits on stale data and the user has to press
  111. // Force-refresh to see the assignment. Calling /refresh-status forces
  112. // a pushall the way the Force-refresh button does. Failures are
  113. // intentionally swallowed — the assignment itself succeeded; if the
  114. // refresh is offline the next poll / websocket update will catch up.
  115. const nudgePrinterRepublish = () => {
  116. api.refreshPrinterStatus(printerId).catch(() => {});
  117. queryClient.invalidateQueries({ queryKey: ['printerStatus', printerId] });
  118. };
  119. const assignMutation = useMutation({
  120. mutationFn: (spoolId: number) =>
  121. api.assignSpool({ spool_id: spoolId, printer_id: printerId, ams_id: amsId, tray_id: trayId }),
  122. onSuccess: (newAssignment) => {
  123. // Immediately update cache so UI reflects the new assignment without waiting for refetch
  124. queryClient.setQueryData<SpoolAssignment[]>(['spool-assignments'], (old) => {
  125. const filtered = (old || []).filter(a =>
  126. !(a.printer_id === printerId && a.ams_id === amsId && a.tray_id === trayId)
  127. );
  128. filtered.push(newAssignment);
  129. return filtered;
  130. });
  131. queryClient.invalidateQueries({ queryKey: ['spool-assignments'] });
  132. nudgePrinterRepublish();
  133. showToast(t('inventory.assignSuccess'), 'success');
  134. setShowMismatchConfirm(false);
  135. setPendingAssignId(null);
  136. setMismatchDetails(null);
  137. onClose();
  138. },
  139. onError: (error: Error) => {
  140. showToast(`${t('inventory.assignFailed')}: ${error.message}`, 'error');
  141. },
  142. });
  143. const assignSpoolmanMutation = useMutation({
  144. mutationFn: (spoolmanSpoolId: number) =>
  145. api.assignSpoolmanSlot({
  146. spoolman_spool_id: spoolmanSpoolId,
  147. printer_id: printerId,
  148. ams_id: amsId,
  149. tray_id: trayId,
  150. }),
  151. onSuccess: () => {
  152. queryClient.invalidateQueries({ queryKey: ['spoolman-inventory-spools'] });
  153. queryClient.invalidateQueries({ queryKey: ['spoolman-slot-assignments'] });
  154. nudgePrinterRepublish();
  155. showToast(t('inventory.assignSuccess'), 'success');
  156. onClose();
  157. },
  158. onError: (error: Error) => {
  159. showToast(`${t('inventory.assignFailed')}: ${error.message}`, 'error');
  160. },
  161. });
  162. // --- Material/profile mismatch logic ---
  163. const normalizeValue = (value: string | undefined | null) =>
  164. (value ?? '').trim().toUpperCase();
  165. const checkMaterialMatch = (
  166. spoolMaterial: string | undefined | null,
  167. trayMaterial: string | undefined | null
  168. ): 'exact' | 'partial' | 'none' => {
  169. const normalizedSpool = normalizeValue(spoolMaterial);
  170. const normalizedTray = normalizeValue(trayMaterial);
  171. if (!normalizedSpool || !normalizedTray) return 'none';
  172. if (normalizedSpool === normalizedTray) return 'exact';
  173. if (normalizedTray.includes(normalizedSpool) || normalizedSpool.includes(normalizedTray)) {
  174. return 'partial';
  175. }
  176. return 'none';
  177. };
  178. // Bambu Studio / OrcaSlicer profile names carry a printer/nozzle/variant qualifier after
  179. // `@` (e.g. "Devil Design PLA Basic @Bambu Lab H2D 0.4 nozzle (Custom)"), while the tray's
  180. // profile is typically the bare base name. Strip the qualifier before comparing so identical
  181. // base profiles don't trigger a mismatch warning (#1047).
  182. const stripProfileQualifier = (value: string) => value.split('@')[0].trim();
  183. const checkProfileMatch = (
  184. spoolProfile: string | undefined | null,
  185. trayProfile: string | undefined | null
  186. ): boolean => {
  187. const normalizedSpoolProfile = stripProfileQualifier(normalizeValue(spoolProfile));
  188. const normalizedTrayProfile = stripProfileQualifier(normalizeValue(trayProfile));
  189. if (!normalizedSpoolProfile || !normalizedTrayProfile) return false;
  190. return normalizedSpoolProfile === normalizedTrayProfile;
  191. };
  192. if (!isOpen) return null;
  193. // Filter out spools already assigned to other slots
  194. const assignedSpoolIds = new Set(
  195. (assignments || [])
  196. .filter(a => !(a.printer_id === printerId && a.ams_id === amsId && a.tray_id === trayId))
  197. .map(a => a.spool_id)
  198. );
  199. // Show every spool that isn't already taken by another slot — including
  200. // RFID-tagged Bambu Lab spools (#1133). The earlier "manual spools only"
  201. // gate (tag_uid && tray_uuid both null) blocked the workflow where a
  202. // user has a Bambu Lab spool in inventory but doesn't want to scan it
  203. // via SpoolBuddy NFC every time and just wants to pick it from the list.
  204. // External slots (amsId 254/255) have always been allowed to pick from
  205. // any spool because the slot itself has no RFID reader; that
  206. // distinction collapses now that AMS slots also accept any spool.
  207. //
  208. // The "Show all spools" toggle (disableFiltering) bypasses BOTH this
  209. // gate and the material/profile filter below, making it a real escape
  210. // hatch for cases where MQTT has auto-reassigned a spool to another
  211. // slot a fraction of a second after a manual unassign — without this,
  212. // the toggle's label is a lie ("Show all" but actually filters by
  213. // assignment). The backend's assign_spool route is upsert-per-
  214. // (printer, ams, tray), so picking a spool that's currently taken by
  215. // a different slot creates a second assignment row; that's a foot-gun
  216. // for normal flows but exactly the recovery path the toggle is for.
  217. const availableSpools = spools?.filter((spool: InventorySpool) =>
  218. !spool.archived_at &&
  219. (disableFiltering || !assignedSpoolIds.has(spool.id))
  220. );
  221. // Filtering logic with toggle: search filter always applies, AMS tray profile filter is optional.
  222. // Show a spool if EITHER the slicer profile matches exactly OR the material overlaps with the
  223. // tray's material (partial-match both directions — "PLA" spool accepts a "PLA Basic" slot and
  224. // vice versa). Manually-added inventory spools typically have no slicer_filament_name; gating
  225. // on strict profile equality alone hid them even when the material matched (#1047).
  226. let filteredSpools = availableSpools;
  227. if (!disableFiltering) {
  228. const trayProfile = stripProfileQualifier(normalizeValue(trayInfo?.profile));
  229. const trayMaterial = normalizeValue(trayInfo?.material || trayInfo?.type);
  230. if (trayProfile || trayMaterial) {
  231. filteredSpools = filteredSpools?.filter((spool: InventorySpool) => {
  232. const spoolProfile = stripProfileQualifier(normalizeValue(spool.slicer_filament_name || spool.slicer_filament));
  233. const spoolMaterial = normalizeValue(spool.material);
  234. if (trayProfile && spoolProfile && spoolProfile === trayProfile) return true;
  235. if (trayMaterial && spoolMaterial) {
  236. return (
  237. spoolMaterial === trayMaterial ||
  238. trayMaterial.includes(spoolMaterial) ||
  239. spoolMaterial.includes(trayMaterial)
  240. );
  241. }
  242. // Neither side has filterable info on whatever dimension remains — show it.
  243. return !spoolProfile && !spoolMaterial;
  244. });
  245. }
  246. }
  247. if (searchFilter && filteredSpools) {
  248. filteredSpools = filterSpoolsByQuery(filteredSpools, searchFilter);
  249. }
  250. const handleAssign = () => {
  251. if (selectedSpoolmanSpoolId !== null) {
  252. assignSpoolmanMutation.mutate(selectedSpoolmanSpoolId);
  253. return;
  254. }
  255. if (!selectedSpoolId) return;
  256. const selectedSpool = spools?.find((spool: InventorySpool) => spool.id === selectedSpoolId);
  257. if (!selectedSpool) {
  258. showToast(t('inventory.assignFailed'), 'error');
  259. return;
  260. }
  261. if (!settings?.disable_filament_warnings && trayInfo) {
  262. const trayMaterial = trayInfo.material || trayInfo.type;
  263. const materialMatchResult = checkMaterialMatch(selectedSpool.material, trayMaterial);
  264. const spoolProfile = selectedSpool.slicer_filament_name || selectedSpool.slicer_filament;
  265. const trayProfile = trayInfo.profile || trayInfo.type;
  266. const profileMatches = checkProfileMatch(spoolProfile, trayProfile);
  267. // Always evaluate both checks; if both fail, show a combined warning.
  268. if (materialMatchResult !== 'exact' || !profileMatches) {
  269. let mismatchType: 'material' | 'partial' | 'profile' | 'material_profile' | 'partial_profile' = 'profile';
  270. if (materialMatchResult === 'none' && !profileMatches) {
  271. mismatchType = 'material_profile';
  272. } else if (materialMatchResult === 'partial' && !profileMatches) {
  273. mismatchType = 'partial_profile';
  274. } else if (materialMatchResult === 'none') {
  275. mismatchType = 'material';
  276. } else if (materialMatchResult === 'partial') {
  277. mismatchType = 'partial';
  278. }
  279. setPendingAssignId(selectedSpoolId);
  280. setMismatchDetails({
  281. type: mismatchType,
  282. spoolMaterial: selectedSpool.material || '',
  283. trayMaterial: trayMaterial || '',
  284. spoolProfile: spoolProfile || undefined,
  285. trayProfile: trayProfile || undefined,
  286. });
  287. setShowMismatchConfirm(true);
  288. return;
  289. }
  290. }
  291. assignMutation.mutate(selectedSpoolId);
  292. };
  293. const handleConfirmMismatch = () => {
  294. if (!pendingAssignId) return;
  295. assignMutation.mutate(pendingAssignId);
  296. setShowMismatchConfirm(false);
  297. setPendingAssignId(null);
  298. };
  299. return (
  300. <>
  301. <div className="fixed inset-0 z-[100] flex items-start sm:items-center justify-center p-4 overflow-y-auto">
  302. <div
  303. className="absolute inset-0 bg-black/60 backdrop-blur-sm"
  304. onClick={onClose}
  305. />
  306. <div className="relative w-full max-w-2xl bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] overflow-hidden flex flex-col my-auto">
  307. {/* Header */}
  308. <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary">
  309. <div className="flex items-center gap-2">
  310. <Package className="w-5 h-5 text-bambu-green" />
  311. <h2 className="text-lg font-semibold text-white">{t('inventory.assignSpool')}</h2>
  312. </div>
  313. <button
  314. onClick={onClose}
  315. className="p-1 text-bambu-gray hover:text-white rounded transition-colors"
  316. >
  317. <X className="w-5 h-5" />
  318. </button>
  319. </div>
  320. {/* Content */}
  321. <div className="p-4 space-y-4 overflow-y-auto">
  322. {/* Tray info */}
  323. {trayInfo && (
  324. <div className="p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary">
  325. <p className="text-xs text-bambu-gray mb-1">{t('inventory.selectSpool')}:</p>
  326. <div className="flex items-center gap-2">
  327. {trayInfo.color && (
  328. <span
  329. className="w-4 h-4 rounded-full border border-black/20"
  330. style={{ backgroundColor: `#${trayInfo.color}` }}
  331. />
  332. )}
  333. <span className="text-white font-medium">{trayInfo.type || t('ams.emptySlot')}</span>
  334. <span className="text-bambu-gray">({trayInfo.location})</span>
  335. </div>
  336. </div>
  337. )}
  338. {/* Search filter */}
  339. <div className="relative">
  340. <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray" />
  341. <input
  342. type="text"
  343. value={searchFilter}
  344. onChange={(e) => setSearchFilter(e.target.value)}
  345. placeholder={t('inventory.searchSpools')}
  346. className="w-full pl-9 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray focus:outline-none focus:border-bambu-green"
  347. />
  348. </div>
  349. {/* Spool list */}
  350. <div className="space-y-3">
  351. {!spoolmanEnabled && (isLoading ? (
  352. <div className="flex justify-center py-8">
  353. <Loader2 className="w-6 h-6 text-bambu-green animate-spin" />
  354. </div>
  355. ) : filteredSpools && filteredSpools.length > 0 ? (
  356. <div className="max-h-96 overflow-y-auto grid grid-cols-2 sm:grid-cols-3 gap-2">
  357. {filteredSpools.map((spool: InventorySpool) => (
  358. <button
  359. key={spool.id}
  360. onClick={() => { setSelectedSpoolId(spool.id); setSelectedSpoolmanSpoolId(null); }}
  361. title={spool.note || undefined}
  362. className={`p-2.5 rounded-lg border text-left transition-colors ${
  363. selectedSpoolId === spool.id
  364. ? 'bg-bambu-green/20 border-bambu-green'
  365. : 'bg-bambu-dark border-bambu-dark-tertiary hover:border-bambu-gray'
  366. }`}
  367. >
  368. <p className="text-white text-sm font-medium truncate">
  369. {spool.brand ? `${spool.brand} ` : ''}{spool.material}{spool.subtype ? ` ${spool.subtype}` : ''}
  370. </p>
  371. <div className="flex items-center gap-1.5 mt-1">
  372. {spool.rgba && (
  373. <span
  374. className="w-3 h-3 rounded-full border border-black/20 flex-shrink-0"
  375. style={{ backgroundColor: `#${spool.rgba.substring(0, 6)}` }}
  376. />
  377. )}
  378. <span className="text-xs text-bambu-gray truncate">{spool.color_name || ''}</span>
  379. </div>
  380. {spool.label_weight && (
  381. <p className="text-xs text-bambu-gray mt-1">
  382. {Math.max(0, Math.round(spool.label_weight - spool.weight_used))} / {spool.label_weight}g
  383. </p>
  384. )}
  385. </button>
  386. ))}
  387. </div>
  388. ) : availableSpools && availableSpools.length === 0 ? (
  389. <div className="text-center py-8 text-bambu-gray">
  390. <p>{t('inventory.noAvailableSpools')}</p>
  391. {/* Diagnostic counter — when the picker is empty, having
  392. the raw fetch / filter counts visible makes a
  393. "spool I expected to see is missing" report
  394. immediately answerable: if `total fetched` is 0 the
  395. backend / cache returned nothing; if it's > 0 then
  396. the archived / assigned-elsewhere filter ate the
  397. spool and the toggle is the right escape hatch. */}
  398. {spools && (
  399. <p className="text-[10px] mt-2 opacity-60">
  400. {spools.length} fetched · {spools.filter(s => s.archived_at).length} archived ·{' '}
  401. {spools.filter(s => assignedSpoolIds.has(s.id)).length} assigned to other slots
  402. </p>
  403. )}
  404. </div>
  405. ) : (
  406. <div className="text-center py-8 text-bambu-gray">
  407. <p>{t('inventory.noSpoolsMatch')}</p>
  408. {availableSpools && (
  409. <p className="text-[10px] mt-2 opacity-60">
  410. {availableSpools.length} unassigned spools — {(availableSpools.length) - (filteredSpools?.length ?? 0)} filtered by tray match. Try "Show all spools".
  411. </p>
  412. )}
  413. </div>
  414. ))}
  415. {spoolmanEnabled && (
  416. <>
  417. {spoolmanLoading ? (
  418. <div className="flex justify-center py-4">
  419. <Loader2 className="w-5 h-5 text-bambu-green animate-spin" />
  420. </div>
  421. ) : spoolmanSpools && spoolmanSpools.filter(s => !s.archived_at && !assignedSpoolmanSpoolIds.has(s.id)).length > 0 ? (
  422. <>
  423. <p className="text-xs font-medium text-bambu-gray uppercase tracking-wide pt-1">
  424. {t('inventory.spoolmanSpools')}
  425. </p>
  426. <div className="max-h-64 overflow-y-auto grid grid-cols-2 sm:grid-cols-3 gap-2">
  427. {filterSpoolsByQuery(spoolmanSpools.filter(s => !s.archived_at && !assignedSpoolmanSpoolIds.has(s.id)), searchFilter)
  428. .map((spool: InventorySpool) => (
  429. <button
  430. key={`spoolman-${spool.id}`}
  431. onClick={() => {
  432. setSelectedSpoolmanSpoolId(spool.id);
  433. setSelectedSpoolId(null);
  434. }}
  435. title={spool.note || undefined}
  436. className={`p-2.5 rounded-lg border text-left transition-colors ${
  437. selectedSpoolmanSpoolId === spool.id
  438. ? 'bg-bambu-green/20 border-bambu-green'
  439. : 'bg-bambu-dark border-bambu-dark-tertiary hover:border-bambu-gray'
  440. }`}
  441. >
  442. <p className="text-white text-sm font-medium truncate">
  443. {spool.brand ? `${spool.brand} ` : ''}{spool.material}{spool.subtype ? ` ${spool.subtype}` : ''}
  444. </p>
  445. <div className="flex items-center gap-1.5 mt-1">
  446. {spool.rgba && (
  447. <span
  448. className="w-3 h-3 rounded-full border border-black/20 flex-shrink-0"
  449. style={{ backgroundColor: `#${spool.rgba.substring(0, 6)}` }}
  450. />
  451. )}
  452. <span className="text-xs text-bambu-gray truncate">{spool.color_name || ''}</span>
  453. </div>
  454. {spool.label_weight && (
  455. <p className="text-xs text-bambu-gray mt-1">
  456. {Math.max(0, Math.round(spool.label_weight - spool.weight_used))} / {spool.label_weight}g
  457. </p>
  458. )}
  459. </button>
  460. ))}
  461. </div>
  462. </>
  463. ) : null}
  464. </>
  465. )}
  466. </div>
  467. </div>
  468. {/* Footer with filtering toggle */}
  469. <div className="flex justify-between items-center p-4 border-t border-bambu-dark-tertiary">
  470. <div className="flex items-center gap-2">
  471. <input
  472. id="disable-filtering-toggle"
  473. type="checkbox"
  474. checked={disableFiltering}
  475. onChange={() => setDisableFiltering(v => !v)}
  476. className="accent-bambu-green w-4 h-4 rounded focus:ring-0 border-bambu-dark-tertiary"
  477. />
  478. <label htmlFor="disable-filtering-toggle" className="text-xs text-bambu-gray select-none cursor-pointer">
  479. {t('inventory.showAllSpools')}
  480. </label>
  481. </div>
  482. <div className="flex gap-2">
  483. <Button variant="secondary" onClick={onClose}>
  484. {t('common.cancel')}
  485. </Button>
  486. <Button
  487. onClick={handleAssign}
  488. disabled={(!selectedSpoolId && selectedSpoolmanSpoolId === null) || assignMutation.isPending || assignSpoolmanMutation.isPending}
  489. >
  490. {(assignMutation.isPending || assignSpoolmanMutation.isPending) ? (
  491. <>
  492. <Loader2 className="w-4 h-4 animate-spin" />
  493. {t('inventory.assigning')}
  494. </>
  495. ) : (
  496. <>
  497. <Package className="w-4 h-4" />
  498. {t('inventory.assignSpool')}
  499. </>
  500. )}
  501. </Button>
  502. </div>
  503. </div>
  504. {assignMutation.isError && (
  505. <div className="mx-4 mb-4 p-2 bg-red-500/20 border border-red-500/50 rounded text-sm text-red-400">
  506. {(assignMutation.error as Error).message}
  507. </div>
  508. )}
  509. </div>
  510. </div>
  511. {showMismatchConfirm && trayInfo && selectedSpoolId && mismatchDetails && (() => {
  512. let message = '';
  513. if (mismatchDetails.type === 'material') {
  514. message = t('inventory.assignMismatchMessage', {
  515. spoolMaterial: mismatchDetails.spoolMaterial,
  516. trayMaterial: mismatchDetails.trayMaterial,
  517. location: trayInfo.location,
  518. });
  519. } else if (mismatchDetails.type === 'partial') {
  520. message = t('inventory.assignPartialMismatchMessage', {
  521. spoolMaterial: mismatchDetails.spoolMaterial,
  522. trayMaterial: mismatchDetails.trayMaterial,
  523. location: trayInfo.location,
  524. });
  525. } else if (mismatchDetails.type === 'material_profile') {
  526. message = `${t('inventory.assignMismatchMessage', {
  527. spoolMaterial: mismatchDetails.spoolMaterial,
  528. trayMaterial: mismatchDetails.trayMaterial,
  529. location: trayInfo.location,
  530. })}\n\n${t('inventory.assignProfileMismatchMessage', {
  531. spoolProfile: mismatchDetails.spoolProfile || t('common.unknown'),
  532. trayProfile: mismatchDetails.trayProfile || t('common.unknown'),
  533. location: trayInfo.location,
  534. })}`;
  535. } else if (mismatchDetails.type === 'partial_profile') {
  536. message = `${t('inventory.assignPartialMismatchMessage', {
  537. spoolMaterial: mismatchDetails.spoolMaterial,
  538. trayMaterial: mismatchDetails.trayMaterial,
  539. location: trayInfo.location,
  540. })}\n\n${t('inventory.assignProfileMismatchMessage', {
  541. spoolProfile: mismatchDetails.spoolProfile || t('common.unknown'),
  542. trayProfile: mismatchDetails.trayProfile || t('common.unknown'),
  543. location: trayInfo.location,
  544. })}`;
  545. } else if (mismatchDetails.type === 'profile') {
  546. message = t('inventory.assignProfileMismatchMessage', {
  547. spoolProfile: mismatchDetails.spoolProfile || t('common.unknown'),
  548. trayProfile: mismatchDetails.trayProfile || t('common.unknown'),
  549. location: trayInfo.location,
  550. });
  551. }
  552. return (
  553. <ConfirmModal
  554. title={t('inventory.assignMismatchTitle')}
  555. message={message}
  556. confirmText={t('inventory.assignMismatchConfirm')}
  557. variant="warning"
  558. // Sit above the AssignSpoolModal wrapper (z-[100], #1336) —
  559. // without this the mismatch dialog is hidden behind its parent.
  560. overlayZIndex="z-[110]"
  561. isLoading={assignMutation.isPending}
  562. onConfirm={handleConfirmMismatch}
  563. onCancel={() => {
  564. if (!assignMutation.isPending) {
  565. setShowMismatchConfirm(false);
  566. setPendingAssignId(null);
  567. setMismatchDetails(null);
  568. }
  569. }}
  570. />
  571. );
  572. })()}
  573. </>
  574. );
  575. }