AssignSpoolModal.tsx 28 KB

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