AssignSpoolModal.tsx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. const assignMutation = useMutation({
  107. mutationFn: (spoolId: number) =>
  108. api.assignSpool({ spool_id: spoolId, printer_id: printerId, ams_id: amsId, tray_id: trayId }),
  109. onSuccess: (newAssignment) => {
  110. // Immediately update cache so UI reflects the new assignment without waiting for refetch
  111. queryClient.setQueryData<SpoolAssignment[]>(['spool-assignments'], (old) => {
  112. const filtered = (old || []).filter(a =>
  113. !(a.printer_id === printerId && a.ams_id === amsId && a.tray_id === trayId)
  114. );
  115. filtered.push(newAssignment);
  116. return filtered;
  117. });
  118. queryClient.invalidateQueries({ queryKey: ['spool-assignments'] });
  119. showToast(t('inventory.assignSuccess'), 'success');
  120. setShowMismatchConfirm(false);
  121. setPendingAssignId(null);
  122. setMismatchDetails(null);
  123. onClose();
  124. },
  125. onError: (error: Error) => {
  126. showToast(`${t('inventory.assignFailed')}: ${error.message}`, 'error');
  127. },
  128. });
  129. const assignSpoolmanMutation = useMutation({
  130. mutationFn: (spoolmanSpoolId: number) =>
  131. api.assignSpoolmanSlot({
  132. spoolman_spool_id: spoolmanSpoolId,
  133. printer_id: printerId,
  134. ams_id: amsId,
  135. tray_id: trayId,
  136. }),
  137. onSuccess: () => {
  138. queryClient.invalidateQueries({ queryKey: ['spoolman-inventory-spools'] });
  139. queryClient.invalidateQueries({ queryKey: ['spoolman-slot-assignments'] });
  140. showToast(t('inventory.assignSuccess'), 'success');
  141. onClose();
  142. },
  143. onError: (error: Error) => {
  144. showToast(`${t('inventory.assignFailed')}: ${error.message}`, 'error');
  145. },
  146. });
  147. // --- Material/profile mismatch logic ---
  148. const normalizeValue = (value: string | undefined | null) =>
  149. (value ?? '').trim().toUpperCase();
  150. const checkMaterialMatch = (
  151. spoolMaterial: string | undefined | null,
  152. trayMaterial: string | undefined | null
  153. ): 'exact' | 'partial' | 'none' => {
  154. const normalizedSpool = normalizeValue(spoolMaterial);
  155. const normalizedTray = normalizeValue(trayMaterial);
  156. if (!normalizedSpool || !normalizedTray) return 'none';
  157. if (normalizedSpool === normalizedTray) return 'exact';
  158. if (normalizedTray.includes(normalizedSpool) || normalizedSpool.includes(normalizedTray)) {
  159. return 'partial';
  160. }
  161. return 'none';
  162. };
  163. // Bambu Studio / OrcaSlicer profile names carry a printer/nozzle/variant qualifier after
  164. // `@` (e.g. "Devil Design PLA Basic @Bambu Lab H2D 0.4 nozzle (Custom)"), while the tray's
  165. // profile is typically the bare base name. Strip the qualifier before comparing so identical
  166. // base profiles don't trigger a mismatch warning (#1047).
  167. const stripProfileQualifier = (value: string) => value.split('@')[0].trim();
  168. const checkProfileMatch = (
  169. spoolProfile: string | undefined | null,
  170. trayProfile: string | undefined | null
  171. ): boolean => {
  172. const normalizedSpoolProfile = stripProfileQualifier(normalizeValue(spoolProfile));
  173. const normalizedTrayProfile = stripProfileQualifier(normalizeValue(trayProfile));
  174. if (!normalizedSpoolProfile || !normalizedTrayProfile) return false;
  175. return normalizedSpoolProfile === normalizedTrayProfile;
  176. };
  177. if (!isOpen) return null;
  178. // Filter out spools already assigned to other slots
  179. const assignedSpoolIds = new Set(
  180. (assignments || [])
  181. .filter(a => !(a.printer_id === printerId && a.ams_id === amsId && a.tray_id === trayId))
  182. .map(a => a.spool_id)
  183. );
  184. // Show every spool that isn't already taken by another slot — including
  185. // RFID-tagged Bambu Lab spools (#1133). The earlier "manual spools only"
  186. // gate (tag_uid && tray_uuid both null) blocked the workflow where a
  187. // user has a Bambu Lab spool in inventory but doesn't want to scan it
  188. // via SpoolBuddy NFC every time and just wants to pick it from the list.
  189. // External slots (amsId 254/255) have always been allowed to pick from
  190. // any spool because the slot itself has no RFID reader; that
  191. // distinction collapses now that AMS slots also accept any spool.
  192. //
  193. // The "Show all spools" toggle (disableFiltering) bypasses BOTH this
  194. // gate and the material/profile filter below, making it a real escape
  195. // hatch for cases where MQTT has auto-reassigned a spool to another
  196. // slot a fraction of a second after a manual unassign — without this,
  197. // the toggle's label is a lie ("Show all" but actually filters by
  198. // assignment). The backend's assign_spool route is upsert-per-
  199. // (printer, ams, tray), so picking a spool that's currently taken by
  200. // a different slot creates a second assignment row; that's a foot-gun
  201. // for normal flows but exactly the recovery path the toggle is for.
  202. const availableSpools = spools?.filter((spool: InventorySpool) =>
  203. !spool.archived_at &&
  204. (disableFiltering || !assignedSpoolIds.has(spool.id))
  205. );
  206. // Filtering logic with toggle: search filter always applies, AMS tray profile filter is optional.
  207. // Show a spool if EITHER the slicer profile matches exactly OR the material overlaps with the
  208. // tray's material (partial-match both directions — "PLA" spool accepts a "PLA Basic" slot and
  209. // vice versa). Manually-added inventory spools typically have no slicer_filament_name; gating
  210. // on strict profile equality alone hid them even when the material matched (#1047).
  211. let filteredSpools = availableSpools;
  212. if (!disableFiltering) {
  213. const trayProfile = stripProfileQualifier(normalizeValue(trayInfo?.profile));
  214. const trayMaterial = normalizeValue(trayInfo?.material || trayInfo?.type);
  215. if (trayProfile || trayMaterial) {
  216. filteredSpools = filteredSpools?.filter((spool: InventorySpool) => {
  217. const spoolProfile = stripProfileQualifier(normalizeValue(spool.slicer_filament_name || spool.slicer_filament));
  218. const spoolMaterial = normalizeValue(spool.material);
  219. if (trayProfile && spoolProfile && spoolProfile === trayProfile) return true;
  220. if (trayMaterial && spoolMaterial) {
  221. return (
  222. spoolMaterial === trayMaterial ||
  223. trayMaterial.includes(spoolMaterial) ||
  224. spoolMaterial.includes(trayMaterial)
  225. );
  226. }
  227. // Neither side has filterable info on whatever dimension remains — show it.
  228. return !spoolProfile && !spoolMaterial;
  229. });
  230. }
  231. }
  232. if (searchFilter && filteredSpools) {
  233. filteredSpools = filterSpoolsByQuery(filteredSpools, searchFilter);
  234. }
  235. const handleAssign = () => {
  236. if (selectedSpoolmanSpoolId !== null) {
  237. assignSpoolmanMutation.mutate(selectedSpoolmanSpoolId);
  238. return;
  239. }
  240. if (!selectedSpoolId) return;
  241. const selectedSpool = spools?.find((spool: InventorySpool) => spool.id === selectedSpoolId);
  242. if (!selectedSpool) {
  243. showToast(t('inventory.assignFailed'), 'error');
  244. return;
  245. }
  246. if (!settings?.disable_filament_warnings && trayInfo) {
  247. const trayMaterial = trayInfo.material || trayInfo.type;
  248. const materialMatchResult = checkMaterialMatch(selectedSpool.material, trayMaterial);
  249. const spoolProfile = selectedSpool.slicer_filament_name || selectedSpool.slicer_filament;
  250. const trayProfile = trayInfo.profile || trayInfo.type;
  251. const profileMatches = checkProfileMatch(spoolProfile, trayProfile);
  252. // Always evaluate both checks; if both fail, show a combined warning.
  253. if (materialMatchResult !== 'exact' || !profileMatches) {
  254. let mismatchType: 'material' | 'partial' | 'profile' | 'material_profile' | 'partial_profile' = 'profile';
  255. if (materialMatchResult === 'none' && !profileMatches) {
  256. mismatchType = 'material_profile';
  257. } else if (materialMatchResult === 'partial' && !profileMatches) {
  258. mismatchType = 'partial_profile';
  259. } else if (materialMatchResult === 'none') {
  260. mismatchType = 'material';
  261. } else if (materialMatchResult === 'partial') {
  262. mismatchType = 'partial';
  263. }
  264. setPendingAssignId(selectedSpoolId);
  265. setMismatchDetails({
  266. type: mismatchType,
  267. spoolMaterial: selectedSpool.material || '',
  268. trayMaterial: trayMaterial || '',
  269. spoolProfile: spoolProfile || undefined,
  270. trayProfile: trayProfile || undefined,
  271. });
  272. setShowMismatchConfirm(true);
  273. return;
  274. }
  275. }
  276. assignMutation.mutate(selectedSpoolId);
  277. };
  278. const handleConfirmMismatch = () => {
  279. if (!pendingAssignId) return;
  280. assignMutation.mutate(pendingAssignId);
  281. setShowMismatchConfirm(false);
  282. setPendingAssignId(null);
  283. };
  284. return (
  285. <>
  286. <div className="fixed inset-0 z-50 flex items-start sm:items-center justify-center p-4 overflow-y-auto">
  287. <div
  288. className="absolute inset-0 bg-black/60 backdrop-blur-sm"
  289. onClick={onClose}
  290. />
  291. <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">
  292. {/* Header */}
  293. <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary">
  294. <div className="flex items-center gap-2">
  295. <Package className="w-5 h-5 text-bambu-green" />
  296. <h2 className="text-lg font-semibold text-white">{t('inventory.assignSpool')}</h2>
  297. </div>
  298. <button
  299. onClick={onClose}
  300. className="p-1 text-bambu-gray hover:text-white rounded transition-colors"
  301. >
  302. <X className="w-5 h-5" />
  303. </button>
  304. </div>
  305. {/* Content */}
  306. <div className="p-4 space-y-4 overflow-y-auto">
  307. {/* Tray info */}
  308. {trayInfo && (
  309. <div className="p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary">
  310. <p className="text-xs text-bambu-gray mb-1">{t('inventory.selectSpool')}:</p>
  311. <div className="flex items-center gap-2">
  312. {trayInfo.color && (
  313. <span
  314. className="w-4 h-4 rounded-full border border-black/20"
  315. style={{ backgroundColor: `#${trayInfo.color}` }}
  316. />
  317. )}
  318. <span className="text-white font-medium">{trayInfo.type || t('ams.emptySlot')}</span>
  319. <span className="text-bambu-gray">({trayInfo.location})</span>
  320. </div>
  321. </div>
  322. )}
  323. {/* Search filter */}
  324. <div className="relative">
  325. <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray" />
  326. <input
  327. type="text"
  328. value={searchFilter}
  329. onChange={(e) => setSearchFilter(e.target.value)}
  330. placeholder={t('inventory.searchSpools')}
  331. 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"
  332. />
  333. </div>
  334. {/* Spool list */}
  335. <div className="space-y-3">
  336. {!spoolmanEnabled && (isLoading ? (
  337. <div className="flex justify-center py-8">
  338. <Loader2 className="w-6 h-6 text-bambu-green animate-spin" />
  339. </div>
  340. ) : filteredSpools && filteredSpools.length > 0 ? (
  341. <div className="max-h-96 overflow-y-auto grid grid-cols-2 sm:grid-cols-3 gap-2">
  342. {filteredSpools.map((spool: InventorySpool) => (
  343. <button
  344. key={spool.id}
  345. onClick={() => { setSelectedSpoolId(spool.id); setSelectedSpoolmanSpoolId(null); }}
  346. title={spool.note || undefined}
  347. className={`p-2.5 rounded-lg border text-left transition-colors ${
  348. selectedSpoolId === spool.id
  349. ? 'bg-bambu-green/20 border-bambu-green'
  350. : 'bg-bambu-dark border-bambu-dark-tertiary hover:border-bambu-gray'
  351. }`}
  352. >
  353. <p className="text-white text-sm font-medium truncate">
  354. {spool.brand ? `${spool.brand} ` : ''}{spool.material}{spool.subtype ? ` ${spool.subtype}` : ''}
  355. </p>
  356. <div className="flex items-center gap-1.5 mt-1">
  357. {spool.rgba && (
  358. <span
  359. className="w-3 h-3 rounded-full border border-black/20 flex-shrink-0"
  360. style={{ backgroundColor: `#${spool.rgba.substring(0, 6)}` }}
  361. />
  362. )}
  363. <span className="text-xs text-bambu-gray truncate">{spool.color_name || ''}</span>
  364. </div>
  365. {spool.label_weight && (
  366. <p className="text-xs text-bambu-gray mt-1">
  367. {Math.max(0, Math.round(spool.label_weight - spool.weight_used))} / {spool.label_weight}g
  368. </p>
  369. )}
  370. </button>
  371. ))}
  372. </div>
  373. ) : availableSpools && availableSpools.length === 0 ? (
  374. <div className="text-center py-8 text-bambu-gray">
  375. <p>{t('inventory.noAvailableSpools')}</p>
  376. {/* Diagnostic counter — when the picker is empty, having
  377. the raw fetch / filter counts visible makes a
  378. "spool I expected to see is missing" report
  379. immediately answerable: if `total fetched` is 0 the
  380. backend / cache returned nothing; if it's > 0 then
  381. the archived / assigned-elsewhere filter ate the
  382. spool and the toggle is the right escape hatch. */}
  383. {spools && (
  384. <p className="text-[10px] mt-2 opacity-60">
  385. {spools.length} fetched · {spools.filter(s => s.archived_at).length} archived ·{' '}
  386. {spools.filter(s => assignedSpoolIds.has(s.id)).length} assigned to other slots
  387. </p>
  388. )}
  389. </div>
  390. ) : (
  391. <div className="text-center py-8 text-bambu-gray">
  392. <p>{t('inventory.noSpoolsMatch')}</p>
  393. {availableSpools && (
  394. <p className="text-[10px] mt-2 opacity-60">
  395. {availableSpools.length} unassigned spools — {(availableSpools.length) - (filteredSpools?.length ?? 0)} filtered by tray match. Try "Show all spools".
  396. </p>
  397. )}
  398. </div>
  399. ))}
  400. {spoolmanEnabled && (
  401. <>
  402. {spoolmanLoading ? (
  403. <div className="flex justify-center py-4">
  404. <Loader2 className="w-5 h-5 text-bambu-green animate-spin" />
  405. </div>
  406. ) : spoolmanSpools && spoolmanSpools.filter(s => !s.archived_at && !assignedSpoolmanSpoolIds.has(s.id)).length > 0 ? (
  407. <>
  408. <p className="text-xs font-medium text-bambu-gray uppercase tracking-wide pt-1">
  409. {t('inventory.spoolmanSpools')}
  410. </p>
  411. <div className="max-h-64 overflow-y-auto grid grid-cols-2 sm:grid-cols-3 gap-2">
  412. {filterSpoolsByQuery(spoolmanSpools.filter(s => !s.archived_at && !assignedSpoolmanSpoolIds.has(s.id)), searchFilter)
  413. .map((spool: InventorySpool) => (
  414. <button
  415. key={`spoolman-${spool.id}`}
  416. onClick={() => {
  417. setSelectedSpoolmanSpoolId(spool.id);
  418. setSelectedSpoolId(null);
  419. }}
  420. title={spool.note || undefined}
  421. className={`p-2.5 rounded-lg border text-left transition-colors ${
  422. selectedSpoolmanSpoolId === spool.id
  423. ? 'bg-bambu-green/20 border-bambu-green'
  424. : 'bg-bambu-dark border-bambu-dark-tertiary hover:border-bambu-gray'
  425. }`}
  426. >
  427. <p className="text-white text-sm font-medium truncate">
  428. {spool.brand ? `${spool.brand} ` : ''}{spool.material}{spool.subtype ? ` ${spool.subtype}` : ''}
  429. </p>
  430. <div className="flex items-center gap-1.5 mt-1">
  431. {spool.rgba && (
  432. <span
  433. className="w-3 h-3 rounded-full border border-black/20 flex-shrink-0"
  434. style={{ backgroundColor: `#${spool.rgba.substring(0, 6)}` }}
  435. />
  436. )}
  437. <span className="text-xs text-bambu-gray truncate">{spool.color_name || ''}</span>
  438. </div>
  439. {spool.label_weight && (
  440. <p className="text-xs text-bambu-gray mt-1">
  441. {Math.max(0, Math.round(spool.label_weight - spool.weight_used))} / {spool.label_weight}g
  442. </p>
  443. )}
  444. </button>
  445. ))}
  446. </div>
  447. </>
  448. ) : null}
  449. </>
  450. )}
  451. </div>
  452. </div>
  453. {/* Footer with filtering toggle */}
  454. <div className="flex justify-between items-center p-4 border-t border-bambu-dark-tertiary">
  455. <div className="flex items-center gap-2">
  456. <input
  457. id="disable-filtering-toggle"
  458. type="checkbox"
  459. checked={disableFiltering}
  460. onChange={() => setDisableFiltering(v => !v)}
  461. className="accent-bambu-green w-4 h-4 rounded focus:ring-0 border-bambu-dark-tertiary"
  462. />
  463. <label htmlFor="disable-filtering-toggle" className="text-xs text-bambu-gray select-none cursor-pointer">
  464. {t('inventory.showAllSpools')}
  465. </label>
  466. </div>
  467. <div className="flex gap-2">
  468. <Button variant="secondary" onClick={onClose}>
  469. {t('common.cancel')}
  470. </Button>
  471. <Button
  472. onClick={handleAssign}
  473. disabled={(!selectedSpoolId && selectedSpoolmanSpoolId === null) || assignMutation.isPending || assignSpoolmanMutation.isPending}
  474. >
  475. {(assignMutation.isPending || assignSpoolmanMutation.isPending) ? (
  476. <>
  477. <Loader2 className="w-4 h-4 animate-spin" />
  478. {t('inventory.assigning')}
  479. </>
  480. ) : (
  481. <>
  482. <Package className="w-4 h-4" />
  483. {t('inventory.assignSpool')}
  484. </>
  485. )}
  486. </Button>
  487. </div>
  488. </div>
  489. {assignMutation.isError && (
  490. <div className="mx-4 mb-4 p-2 bg-red-500/20 border border-red-500/50 rounded text-sm text-red-400">
  491. {(assignMutation.error as Error).message}
  492. </div>
  493. )}
  494. </div>
  495. </div>
  496. {showMismatchConfirm && trayInfo && selectedSpoolId && mismatchDetails && (() => {
  497. let message = '';
  498. if (mismatchDetails.type === 'material') {
  499. message = t('inventory.assignMismatchMessage', {
  500. spoolMaterial: mismatchDetails.spoolMaterial,
  501. trayMaterial: mismatchDetails.trayMaterial,
  502. location: trayInfo.location,
  503. });
  504. } else if (mismatchDetails.type === 'partial') {
  505. message = t('inventory.assignPartialMismatchMessage', {
  506. spoolMaterial: mismatchDetails.spoolMaterial,
  507. trayMaterial: mismatchDetails.trayMaterial,
  508. location: trayInfo.location,
  509. });
  510. } else if (mismatchDetails.type === 'material_profile') {
  511. message = `${t('inventory.assignMismatchMessage', {
  512. spoolMaterial: mismatchDetails.spoolMaterial,
  513. trayMaterial: mismatchDetails.trayMaterial,
  514. location: trayInfo.location,
  515. })}\n\n${t('inventory.assignProfileMismatchMessage', {
  516. spoolProfile: mismatchDetails.spoolProfile || t('common.unknown'),
  517. trayProfile: mismatchDetails.trayProfile || t('common.unknown'),
  518. location: trayInfo.location,
  519. })}`;
  520. } else if (mismatchDetails.type === 'partial_profile') {
  521. message = `${t('inventory.assignPartialMismatchMessage', {
  522. spoolMaterial: mismatchDetails.spoolMaterial,
  523. trayMaterial: mismatchDetails.trayMaterial,
  524. location: trayInfo.location,
  525. })}\n\n${t('inventory.assignProfileMismatchMessage', {
  526. spoolProfile: mismatchDetails.spoolProfile || t('common.unknown'),
  527. trayProfile: mismatchDetails.trayProfile || t('common.unknown'),
  528. location: trayInfo.location,
  529. })}`;
  530. } else if (mismatchDetails.type === 'profile') {
  531. message = t('inventory.assignProfileMismatchMessage', {
  532. spoolProfile: mismatchDetails.spoolProfile || t('common.unknown'),
  533. trayProfile: mismatchDetails.trayProfile || t('common.unknown'),
  534. location: trayInfo.location,
  535. });
  536. }
  537. return (
  538. <ConfirmModal
  539. title={t('inventory.assignMismatchTitle')}
  540. message={message}
  541. confirmText={t('inventory.assignMismatchConfirm')}
  542. variant="warning"
  543. isLoading={assignMutation.isPending}
  544. onConfirm={handleConfirmMismatch}
  545. onCancel={() => {
  546. if (!assignMutation.isPending) {
  547. setShowMismatchConfirm(false);
  548. setPendingAssignId(null);
  549. setMismatchDetails(null);
  550. }
  551. }}
  552. />
  553. );
  554. })()}
  555. </>
  556. );
  557. }