SpoolFormModal.tsx 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  1. import { useState, useEffect, useMemo, useRef } from 'react';
  2. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  3. import { useTranslation } from 'react-i18next';
  4. import { X, Loader2, Save, Beaker, Palette, Zap, Tag, Link2, Unlink } from 'lucide-react';
  5. import { api, ApiError } from '../api/client';
  6. import type { InventorySpool, SlicerSetting, SpoolCatalogEntry, LocalPreset, BuiltinFilament, SpoolmanBulkCreateResult, SpoolFilamentPresetInput, SpoolKProfileInput, SpoolmanFilamentEntry, ResolvedProfileDefault } from '../api/client';
  7. import { Button } from './Button';
  8. import { useToast } from '../contexts/ToastContext';
  9. import type {
  10. CalibrationProfile,
  11. ColorPreset,
  12. PresetChoice,
  13. PrinterWithCalibrations,
  14. SpoolFormData,
  15. SpoolFormMode,
  16. } from './spool-form/types';
  17. import { defaultFormData, validateForm, SPOOLMAN_LINKED_FIELDS } from './spool-form/types';
  18. import { buildFilamentOptions, extractBrandsFromPresets, fetchPrinterCalibrations, findPresetOption, loadRecentColors, pairedOptions, parsePresetKey, parsePresetName, presetKey, saveRecentColor, selectionFromKProfiles, withCurrentValue } from './spool-form/utils';
  19. import { MATERIALS } from './spool-form/constants';
  20. import { FilamentSection } from './spool-form/FilamentSection';
  21. import { ColorSection } from './spool-form/ColorSection';
  22. import { AdditionalSection } from './spool-form/AdditionalSection';
  23. import { SupplierSection, type SupplierLinkDraft } from './spool-form/SupplierSection';
  24. import { SpoolmanFilamentPicker } from './spool-form/SpoolmanFilamentPicker';
  25. import { PrinterProfilesSection } from './spool-form/PrinterProfilesSection';
  26. import { ProfileDefaultOption, type ProfileDefaultScope } from './spool-form/ProfileDefaultOption';
  27. import { normaliseFlow } from '../utils/nozzleFlow';
  28. import { parseUTCDate, toDateTimeLocalValue } from '../utils/date';
  29. import { SpoolUsageHistory } from './SpoolUsageHistory';
  30. import { AssignToAmsModal } from './spoolbuddy/AssignToAmsModal';
  31. import {
  32. invalidateInventoryLocations,
  33. invalidateSpoolAndLocationQueries,
  34. } from '../utils/inventoryQueries';
  35. import { useUiSettings } from '../hooks/useUiSettings';
  36. type TabId = 'filament' | 'appearance' | 'printers';
  37. const CLEAR_TAG_PAYLOAD = { tag_uid: null, tray_uuid: null, tag_type: null, data_origin: null };
  38. export type { SpoolFormMode };
  39. interface SpoolFormModalProps {
  40. isOpen: boolean;
  41. onClose: () => void;
  42. spool?: InventorySpool | null;
  43. mode: SpoolFormMode;
  44. printersWithCalibrations?: PrinterWithCalibrations[];
  45. currencySymbol: string;
  46. onSpoolsCreated?: (spools: InventorySpool[]) => void;
  47. /** When true, CRUD operations target the Spoolman inventory proxy endpoints. */
  48. spoolmanMode?: boolean;
  49. /** Query key to invalidate after mutations (differs for Spoolman vs local). */
  50. spoolsQueryKey?: string[];
  51. }
  52. export function SpoolFormModal({
  53. isOpen,
  54. onClose,
  55. spool,
  56. mode,
  57. printersWithCalibrations = [],
  58. currencySymbol,
  59. onSpoolsCreated,
  60. spoolmanMode = false,
  61. spoolsQueryKey = ['inventory-spools'],
  62. }: SpoolFormModalProps) {
  63. const { t } = useTranslation();
  64. const queryClient = useQueryClient();
  65. const { showToast } = useToast();
  66. const refreshSpoolQueries = () =>
  67. invalidateSpoolAndLocationQueries(queryClient, spoolsQueryKey);
  68. const isEditing = mode === 'edit';
  69. const isCopying = mode === 'copy';
  70. // Form state
  71. const [formData, setFormData] = useState<SpoolFormData>(defaultFormData);
  72. const formDataRef = useRef(formData);
  73. formDataRef.current = formData;
  74. const [errors, setErrors] = useState<Partial<Record<keyof SpoolFormData, string>>>({});
  75. const [activeTab, setActiveTab] = useState<TabId>('filament');
  76. const [weightTouched, setWeightTouched] = useState(false);
  77. // Keyed on core_weight, not core_weight_catalog_id: SpoolWeightPicker selects
  78. // a catalogue entry by itself on mount when one matches the current weight,
  79. // so the id changes on forms nobody has touched. Both real user actions go
  80. // through core_weight.
  81. const [coreWeightTouched, setCoreWeightTouched] = useState(false);
  82. const [locationIdTouched, setLocationIdTouched] = useState(false);
  83. // Sent only when changed, so saving an unrelated edit cannot put back a
  84. // drying date that an AMS run stamped while the form was open (#2863).
  85. const [lastDriedTouched, setLastDriedTouched] = useState(false);
  86. // Supplier assignments (#2988). Held outside SpoolFormData — they are
  87. // relational and saved through their own replace-all endpoint. An untouched
  88. // create does not send them, so it keeps the backend's inherited
  89. // assignments instead of wiping them with an empty list; a copy always
  90. // sends them, see saveSupplierLinks.
  91. const [supplierLinks, setSupplierLinks] = useState<SupplierLinkDraft[]>([]);
  92. const [supplierLinksTouched, setSupplierLinksTouched] = useState(false);
  93. const [quickAdd, setQuickAdd] = useState(false);
  94. const [quantity, setQuantity] = useState(1);
  95. const [showAssignModal, setShowAssignModal] = useState(false);
  96. useEffect(() => {
  97. setShowAssignModal(false);
  98. }, [isOpen, spool?.id]);
  99. // Cloud presets
  100. const [cloudAuthenticated, setCloudAuthenticated] = useState(false);
  101. const [loadingCloudPresets, setLoadingCloudPresets] = useState(false);
  102. const [cloudPresets, setCloudPresets] = useState<SlicerSetting[]>([]);
  103. const [orcaSettingIds, setOrcaSettingIds] = useState<Set<string>>(new Set());
  104. const [presetInputValue, setPresetInputValue] = useState('');
  105. // Spool catalog
  106. const [spoolCatalog, setSpoolCatalog] = useState<SpoolCatalogEntry[]>([]);
  107. const [storageLocations, setStorageLocations] = useState<{ id: number; name: string }[]>([]);
  108. // Local presets (OrcaSlicer imports)
  109. const [localPresets, setLocalPresets] = useState<LocalPreset[]>([]);
  110. // Built-in filaments (static fallback)
  111. const [builtinFilaments, setBuiltinFilaments] = useState<BuiltinFilament[]>([]);
  112. // Color catalog
  113. const [colorCatalog, setColorCatalog] = useState<{
  114. manufacturer: string;
  115. color_name: string;
  116. hex_color: string;
  117. material: string | null;
  118. // #1340: gradient + effect carried from the catalog entry through to the
  119. // color picker so they're applied alongside hex + name on selection.
  120. extra_colors?: string | null;
  121. effect_type?: string | null;
  122. }[]>([]);
  123. // Color state
  124. const [recentColors, setRecentColors] = useState<ColorPreset[]>([]);
  125. // PA Profile state
  126. const [fetchedCalibrations, setFetchedCalibrations] = useState<PrinterWithCalibrations[]>([]);
  127. // Whether the calibration fetch above is still running. Without it the
  128. // Printers tab renders its "no printers configured" empty state while the
  129. // printers are still being asked -- which reads as a wrong answer rather
  130. // than as a wait, and the fetch is several round trips per machine.
  131. const [loadingCalibrations, setLoadingCalibrations] = useState(false);
  132. // One K profile per hotend, keyed `printerId:extruder:diameter`. A Map rather
  133. // than a Set of composite keys because the Printers tab presents each hotend
  134. // as a single-choice dropdown -- the shape makes "two profiles for one
  135. // hotend" unrepresentable instead of relying on eviction logic to prevent it.
  136. const [selectedProfiles, setSelectedProfiles] = useState<Map<string, CalibrationProfile>>(new Map());
  137. // Per-printer-model preset overrides, keyed by `presetKey(model, diameter)`.
  138. // Only the models the user has actually overridden are present; an absent
  139. // entry means "inherit this spool's own preset", which is exactly what the
  140. // backend cascade does with a missing row.
  141. const [modelPresets, setModelPresets] = useState<Map<string, PresetChoice>>(new Map());
  142. const [selectedGroupId, setSelectedGroupId] = useState<string>('');
  143. // Profile defaults (#1326). Whether the user changed anything on the
  144. // Printers tab: an untouched create does not save the tab, so it keeps the
  145. // profile default the backend applies instead of wiping it with an empty
  146. // list -- the same rule as the supplier assignments.
  147. const [printerProfilesTouched, setPrinterProfilesTouched] = useState(false);
  148. // The tab (and an empty preset field) was filled in from the product's default.
  149. const [prefilledFromDefault, setPrefilledFromDefault] = useState(false);
  150. // What the default put in, per hotend and per (model, nozzle), so the next
  151. // product's default can take back what the user left as it was.
  152. const kFromDefault = useRef<Map<string, CalibrationProfile>>(new Map());
  153. const presetsFromDefault = useRef<Map<string, PresetChoice>>(new Map());
  154. // Hotends and (model, nozzle) keys the user set or cleared on the tab: theirs,
  155. // so a later default never puts back what they removed.
  156. const userKKeys = useRef<Set<string>>(new Set());
  157. const userPresetKeys = useRef<Set<string>>(new Set());
  158. // A ref, not state: the lookup's answer arrives after the render that asked
  159. // for it, and must see a preset the user picked in the meantime.
  160. const presetFromDefault = useRef(false);
  161. const [saveAsDefault, setSaveAsDefault] = useState(false);
  162. const [defaultScope, setDefaultScope] = useState<ProfileDefaultScope>('all');
  163. // The Printers tab's own setters mark it touched; the loaders here use the raw ones.
  164. const markChanged = <V,>(prev: Map<string, V>, next: Map<string, V>, keys: Set<string>) => {
  165. for (const key of new Set([...prev.keys(), ...next.keys()])) {
  166. if (prev.get(key) !== next.get(key)) keys.add(key);
  167. }
  168. };
  169. const editModelPresets: typeof setModelPresets = (value) => {
  170. setPrinterProfilesTouched(true);
  171. setModelPresets(prev => {
  172. const next = typeof value === 'function' ? value(prev) : value;
  173. markChanged(prev, next, userPresetKeys.current);
  174. return next;
  175. });
  176. };
  177. const editSelectedProfiles: typeof setSelectedProfiles = (value) => {
  178. setPrinterProfilesTouched(true);
  179. setSelectedProfiles(prev => {
  180. const next = typeof value === 'function' ? value(prev) : value;
  181. markChanged(prev, next, userKKeys.current);
  182. return next;
  183. });
  184. };
  185. // Use prop if provided, otherwise use self-fetched data
  186. const resolvedCalibrations = printersWithCalibrations.length > 0
  187. ? printersWithCalibrations
  188. : fetchedCalibrations;
  189. // Tab badge: everything the user has configured under Printers, K profiles
  190. // and preset overrides alike, since both live on that tab now.
  191. const selectedProfileCount = selectedProfiles.size + modelPresets.size;
  192. // Fetch Spoolman filament catalog when in Spoolman mode
  193. // retry:false — Spoolman may be intentionally disabled (400); don't flood the server
  194. const { data: spoolmanFilaments = [], isLoading: isLoadingFilaments, error: filamentsError } = useQuery<SpoolmanFilamentEntry[], Error>({
  195. queryKey: ['spoolman-inventory-filaments'],
  196. queryFn: () => api.getSpoolmanInventoryFilaments(),
  197. enabled: spoolmanMode && isOpen,
  198. staleTime: 60_000,
  199. retry: false,
  200. });
  201. // Load recent colors on mount
  202. useEffect(() => {
  203. setRecentColors(loadRecentColors());
  204. }, []);
  205. // Fetch cloud presets and catalog when modal opens. Fetches Bambu Cloud
  206. // and Orca Cloud in parallel; merges Orca filaments into ``cloudPresets``
  207. // since ``OrcaProfileMeta`` is structurally identical to ``SlicerSetting``
  208. // (same fields, same semantics). ``cloudAuthenticated`` flips on if either
  209. // cloud is connected — the UI only uses it to gate "no cloud" hints.
  210. useEffect(() => {
  211. // ``cancelled`` gates every state setter so a fetch that resolves AFTER
  212. // the modal closes / unmounts can't fire setState on a torn-down
  213. // component. Without this guard the parallel Promise.allSettled chain
  214. // can still hit ``setLoadingCloudPresets(false)`` in its ``finally``
  215. // after vitest has dismantled the JSDOM window — surfaced as an
  216. // "Unhandled Rejection: window is not defined" in CI runs.
  217. let cancelled = false;
  218. if (isOpen) {
  219. const fetchData = async () => {
  220. setLoadingCloudPresets(true);
  221. try {
  222. const [bambuResult, orcaResult] = await Promise.allSettled([
  223. (async () => {
  224. const status = await api.getCloudStatus();
  225. if (!status.is_authenticated) return { connected: false, presets: [] as SlicerSetting[] };
  226. const presets = await api.getFilamentPresets();
  227. return { connected: true, presets };
  228. })(),
  229. (async () => {
  230. const status = await api.orcaCloudStatus();
  231. if (!status.connected) return { connected: false, presets: [] as SlicerSetting[] };
  232. const list = await api.orcaCloudListProfiles();
  233. // OrcaProfileMeta is structurally identical to SlicerSetting.
  234. return { connected: true, presets: list.filament as unknown as SlicerSetting[] };
  235. })(),
  236. ]);
  237. if (cancelled) return;
  238. const bambuConnected = bambuResult.status === 'fulfilled' && bambuResult.value.connected;
  239. const orcaConnected = orcaResult.status === 'fulfilled' && orcaResult.value.connected;
  240. const bambuPresets = bambuResult.status === 'fulfilled' ? bambuResult.value.presets : [];
  241. const orcaPresets = orcaResult.status === 'fulfilled' ? orcaResult.value.presets : [];
  242. setCloudAuthenticated(bambuConnected || orcaConnected);
  243. setCloudPresets([...bambuPresets, ...orcaPresets]);
  244. // The two clouds are merged into one list, so remember which ids came
  245. // from Orca -- it is the only way the origin badge can tell them
  246. // apart afterwards.
  247. setOrcaSettingIds(new Set(orcaPresets.map(p => p.setting_id)));
  248. } catch (e) {
  249. if (cancelled) return;
  250. console.error('Failed to fetch cloud presets:', e);
  251. setCloudAuthenticated(false);
  252. } finally {
  253. if (!cancelled) setLoadingCloudPresets(false);
  254. }
  255. };
  256. fetchData();
  257. // Fetched in Spoolman mode too: the empty spool weight picker is shown
  258. // there now, and its catalogue is Bambuddy's own either way (#2908).
  259. api.getSpoolCatalog().then(setSpoolCatalog).catch(console.error);
  260. api.getColorCatalog().then(setColorCatalog).catch(console.error);
  261. api.getLocalPresets().then(r => setLocalPresets(r.filament)).catch(console.error);
  262. api.getBuiltinFilaments().then(setBuiltinFilaments).catch(console.error);
  263. api.getLocations().then((locs) => setStorageLocations(locs.map((l) => ({ id: l.id, name: l.name })))).catch(console.error);
  264. // Fetch printer calibrations if not provided via props
  265. if (printersWithCalibrations.length === 0) {
  266. (async () => {
  267. setLoadingCalibrations(true);
  268. try {
  269. const printers = await api.getPrinters();
  270. const statuses = await Promise.all(
  271. printers.map(p => api.getPrinterStatus(p.id).catch(() => null)),
  272. );
  273. // Printers in parallel, diameters within a printer in series.
  274. // Separate machines are separate MQTT connections and do not
  275. // interfere; it is one printer's own firmware that drops a
  276. // concurrent burst of calibration requests (see
  277. // fetchPrinterCalibrations). Walking the fleet one machine at a
  278. // time made the whole tab wait for the sum of every printer.
  279. const results = await Promise.all(
  280. printers.map(async (printer, i) => {
  281. const status = statuses[i];
  282. const connected = status?.connected ?? false;
  283. let calibrations: PrinterWithCalibrations['calibrations'] = [];
  284. if (connected) {
  285. // Across every nozzle size, so a profile for a size that is
  286. // not currently fitted is still offered (#2618 fetched only
  287. // the fitted ones).
  288. calibrations = await fetchPrinterCalibrations(printer.id, status);
  289. }
  290. // Keep the reported nozzle hardware: the Printers tab lists a
  291. // model's installed diameters from it. Read as a set of
  292. // diameters only -- never indexed by extruder.
  293. return { printer: { ...printer, connected }, calibrations, nozzles: status?.nozzles };
  294. }),
  295. );
  296. if (!cancelled) setFetchedCalibrations(results);
  297. } catch (e) {
  298. console.error('Failed to fetch printer calibrations:', e);
  299. } finally {
  300. if (!cancelled) setLoadingCalibrations(false);
  301. }
  302. })();
  303. }
  304. }
  305. // Depends only on `isOpen` (and the prop-side calibration count). It used
  306. // to read spoolmanMode for the catalogue fetch and left it out of the deps
  307. // on purpose -- re-running on every toggle raced the in-flight fetches with
  308. // unmount and emitted "test environment was torn down" errors in vitest.
  309. // It no longer reads it, so the deps are complete as written.
  310. return () => {
  311. cancelled = true;
  312. };
  313. }, [isOpen, printersWithCalibrations.length]);
  314. // Build filament options: cloud → local → fallback
  315. const filamentOptions = useMemo(
  316. () => buildFilamentOptions(cloudPresets, new Set(), localPresets, builtinFilaments, orcaSettingIds),
  317. [cloudPresets, localPresets, builtinFilaments, orcaSettingIds],
  318. );
  319. // Extract brands from presets
  320. const baseAvailableBrands = useMemo(() => {
  321. const presetBrands = extractBrandsFromPresets(cloudPresets, localPresets);
  322. const catalogBrands = colorCatalog
  323. .map(entry => entry.manufacturer?.trim())
  324. .filter((brand): brand is string => !!brand);
  325. const brandSet = new Set<string>([...presetBrands, ...catalogBrands]);
  326. return Array.from(brandSet).sort((a, b) => a.localeCompare(b));
  327. }, [cloudPresets, localPresets, colorCatalog]);
  328. const baseAvailableMaterials = useMemo(() => {
  329. const catalogMaterials = colorCatalog
  330. .map(entry => entry.material?.trim())
  331. .filter((material): material is string => !!material);
  332. const materialSet = new Set<string>([...MATERIALS, ...catalogMaterials]);
  333. return Array.from(materialSet).sort((a, b) => a.localeCompare(b));
  334. }, [colorCatalog]);
  335. const brandMaterialPairs = useMemo(() => {
  336. const pairs: Array<{ brand: string; material: string }> = [];
  337. for (const entry of colorCatalog) {
  338. const brand = entry.manufacturer?.trim();
  339. const material = entry.material?.trim();
  340. if (brand && material) pairs.push({ brand, material });
  341. }
  342. for (const preset of cloudPresets) {
  343. const parsed = parsePresetName(preset.name);
  344. if (parsed.brand && parsed.material) {
  345. pairs.push({ brand: parsed.brand, material: parsed.material });
  346. }
  347. }
  348. for (const preset of localPresets) {
  349. const parsed = parsePresetName(preset.name);
  350. const brand = preset.filament_vendor?.trim() || parsed.brand;
  351. const material = parsed.material;
  352. if (brand && material) {
  353. pairs.push({ brand, material });
  354. }
  355. }
  356. return pairs;
  357. }, [cloudPresets, colorCatalog, localPresets]);
  358. const brandToMaterials = useMemo(() => {
  359. const map = new Map<string, Set<string>>();
  360. for (const pair of brandMaterialPairs) {
  361. const brandKey = pair.brand.toLowerCase();
  362. const materialKey = pair.material.toLowerCase();
  363. if (!map.has(brandKey)) map.set(brandKey, new Set());
  364. map.get(brandKey)!.add(materialKey);
  365. }
  366. return map;
  367. }, [brandMaterialPairs]);
  368. const materialToBrands = useMemo(() => {
  369. const map = new Map<string, Set<string>>();
  370. for (const pair of brandMaterialPairs) {
  371. const brandKey = pair.brand.toLowerCase();
  372. const materialKey = pair.material.toLowerCase();
  373. if (!map.has(materialKey)) map.set(materialKey, new Set());
  374. map.get(materialKey)!.add(brandKey);
  375. }
  376. return map;
  377. }, [brandMaterialPairs]);
  378. // #1905: the brand and material dropdowns used to be filtered down to the
  379. // pairs seen in the color catalog / slicer presets, which hid perfectly valid
  380. // combinations — "Elegoo" exists (as a PLA brand) but vanished from the list
  381. // once ASA was selected, making the entry look impossible. Both lists now
  382. // always offer everything we know about, plus whatever the spool already has
  383. // stored (a custom brand saved earlier was missing from its own dropdown).
  384. // The pairing knowledge survives as `suggestedBrands`/`suggestedMaterials`,
  385. // which the dropdowns sort to the top instead of filtering by.
  386. const availableBrands = useMemo(
  387. () => withCurrentValue(baseAvailableBrands, formData.brand),
  388. [baseAvailableBrands, formData.brand],
  389. );
  390. const availableMaterials = useMemo(
  391. () => withCurrentValue(baseAvailableMaterials, formData.material),
  392. [baseAvailableMaterials, formData.material],
  393. );
  394. const suggestedBrands = useMemo(
  395. () => pairedOptions(availableBrands, formData.material, materialToBrands),
  396. [availableBrands, formData.material, materialToBrands],
  397. );
  398. const suggestedMaterials = useMemo(
  399. () => pairedOptions(availableMaterials, formData.brand, brandToMaterials),
  400. [availableMaterials, formData.brand, brandToMaterials],
  401. );
  402. // Find selected preset option
  403. const selectedPresetOption = useMemo(
  404. () => findPresetOption(formData.slicer_filament, filamentOptions),
  405. [formData.slicer_filament, filamentOptions],
  406. );
  407. // Reset form when modal opens/closes or spool changes
  408. useEffect(() => {
  409. if (isOpen) {
  410. if (spool) {
  411. // Legacy rows may carry a malformed rgba (e.g. the 7-char 'FFFFFFF'
  412. // from #1055 before the create/update pattern was enforced). The
  413. // backend SpoolUpdate schema rejects non-8-char hex on PATCH, so
  414. // re-submitting a malformed value would 422 every edit on that spool
  415. // — even edits that don't touch color. Normalize on load: any value
  416. // that isn't exactly 8 hex chars falls back to the default, so the
  417. // user can save unrelated fields (weight, material, note) without
  418. // first being forced to fix a color they may not even be aware is
  419. // broken. Saving also purges the bad value from the DB.
  420. const validRgba = spool.rgba && /^[0-9A-Fa-f]{8}$/.test(spool.rgba) ? spool.rgba : '808080FF';
  421. setFormData({
  422. material: spool.material || '',
  423. subtype: spool.subtype || '',
  424. brand: spool.brand || '',
  425. // #1319: leave color_name blank when the backend reports it was
  426. // synthesised from subtype — otherwise the form would round-trip
  427. // the synth value to Spoolman on save as if the user had set it,
  428. // which is what produced the "color reverts to subtype" symptom.
  429. color_name: spool.color_name_is_synthesized ? '' : (spool.color_name || ''),
  430. rgba: validRgba,
  431. extra_colors: spool.extra_colors || '',
  432. effect_type: spool.effect_type || '',
  433. label_weight: spool.label_weight || 1000,
  434. // ?? not ||: 0 g is a real tare (a spool-less coil) and must load as 0.
  435. core_weight: spool.core_weight ?? 250,
  436. core_weight_catalog_id: spool.core_weight_catalog_id ?? null,
  437. weight_used: isCopying ? 0 : spool.weight_used || 0,
  438. slicer_filament: spool.slicer_filament || '',
  439. note: spool.note || '',
  440. cost_per_kg: spool.cost_per_kg ?? null,
  441. category: spool.category || '',
  442. low_stock_threshold_pct: spool.low_stock_threshold_pct ?? null,
  443. material_number: spool.material_number || '',
  444. location_id: spool.location_id ?? null,
  445. last_dried_at: (() => {
  446. const dried = isCopying ? null : parseUTCDate(spool.last_dried_at);
  447. return dried ? toDateTimeLocalValue(dried) : '';
  448. })(),
  449. spoolman_filament_id: null,
  450. });
  451. setPresetInputValue(spool.slicer_filament_name || spool.slicer_filament || '');
  452. // Load K-profiles for this spool.
  453. setSelectedProfiles(selectionFromKProfiles(spool.k_profiles ?? []));
  454. // Supplier assignments (#2988) — copied on copy: they describe where
  455. // the product is bought, which is what a copy shares.
  456. setSupplierLinks(
  457. (spool.suppliers ?? []).map((link) => ({
  458. supplier_id: link.supplier_id,
  459. supplier_name: link.supplier_name,
  460. supplier_article_number: link.supplier_article_number ?? '',
  461. quoted_price_per_kg: link.quoted_price_per_kg,
  462. // Where a COPY was bought is unknown — only a real edit keeps it.
  463. is_purchase_source: isCopying ? false : link.is_purchase_source,
  464. }))
  465. );
  466. } else {
  467. setFormData(defaultFormData);
  468. setPresetInputValue('');
  469. setSelectedProfiles(new Map());
  470. setSupplierLinks([]);
  471. }
  472. // Reset on every open, not just the create path (#1905). The modal keeps
  473. // its state while closed, and the Quick Add toggle only renders in create
  474. // mode — so quick-adding a spool and then opening Edit left the edit form
  475. // stuck in quick-add layout (no preset field, no PA-profile tab) with no
  476. // control to switch back.
  477. setQuickAdd(false);
  478. setQuantity(1);
  479. setErrors({});
  480. setActiveTab('filament');
  481. setSelectedGroupId('');
  482. // Cleared on every open, both branches: the modal keeps its state while
  483. // closed, so editing spool B after spool A would otherwise show (and
  484. // save) A's per-model overrides on B. Refilled by the fetch below.
  485. setModelPresets(new Map());
  486. setWeightTouched(false);
  487. setLastDriedTouched(false);
  488. // A copy of a Spoolman spool with its own tare carries that tare, as it
  489. // would any other field shown in the form; one that inherits keeps
  490. // inheriting. Only Spoolman spools report the flag (#2908).
  491. setCoreWeightTouched(isCopying && spool?.core_weight_is_inherited === false);
  492. setLocationIdTouched(false);
  493. setSupplierLinksTouched(false);
  494. setPrinterProfilesTouched(false);
  495. setPrefilledFromDefault(false);
  496. kFromDefault.current = new Map();
  497. presetsFromDefault.current = new Map();
  498. userKKeys.current = new Set();
  499. userPresetKeys.current = new Set();
  500. presetFromDefault.current = false;
  501. setSaveAsDefault(false);
  502. setDefaultScope('all');
  503. }
  504. }, [isOpen, spool, mode, isCopying]);
  505. // Load this spool's per-printer-model preset overrides. Fetched rather than
  506. // read off the spool: they are deliberately not embedded in the spool
  507. // response, which the inventory list returns once per spool the user owns.
  508. // Copying a spool copies its overrides -- they describe the filament on the
  509. // spool, which is what a copy has too.
  510. useEffect(() => {
  511. if (!isOpen || !spool) return;
  512. let cancelled = false;
  513. const load = spoolmanMode ? api.getSpoolmanFilamentPresets : api.getSpoolFilamentPresets;
  514. load(spool.id)
  515. .then(rows => {
  516. if (cancelled) return;
  517. const next = new Map<string, PresetChoice>();
  518. for (const row of rows) {
  519. next.set(presetKey(row.printer_model, row.nozzle_diameter || ''), {
  520. code: row.slicer_filament || '',
  521. name: row.slicer_filament_name || '',
  522. });
  523. }
  524. setModelPresets(next);
  525. })
  526. .catch(e => {
  527. // Non-fatal: the tab still works, it just starts with nothing
  528. // overridden. Saving from that state WOULD clear the stored rows, so
  529. // say so rather than letting the user save over what they cannot see.
  530. if (cancelled) return;
  531. console.error('Failed to load filament preset overrides:', e);
  532. showToast(t('inventory.filamentPresetsLoadFailed'), 'warning');
  533. });
  534. return () => {
  535. cancelled = true;
  536. };
  537. // eslint-disable-next-line react-hooks/exhaustive-deps
  538. }, [isOpen, spool?.id, spoolmanMode]);
  539. // A new spool starts from its product's profile default (#1326): fill the
  540. // Printers tab, and an empty preset field, with what the create will apply,
  541. // so the user sees it and can change it before saving. Only for a fresh
  542. // create -- an edit shows the spool's own profiles and a copy the copied
  543. // ones. What the user set wins: the default only fills hotends and models
  544. // left empty, so a touched tab still shows (and saves) the rest of it.
  545. // Follows the product as it is typed: what one product's default put in and
  546. // the user left alone gives way to the next product's.
  547. const isCreating = !isEditing && !isCopying;
  548. useEffect(() => {
  549. if (!isOpen || !isCreating) return;
  550. const material = formData.material.trim();
  551. let cancelled = false;
  552. const apply = (resolved: ResolvedProfileDefault | null) => {
  553. if (cancelled) return;
  554. const kRows = selectionFromKProfiles(resolved?.k_profiles ?? []);
  555. // Read before the updaters, which React may run twice.
  556. const previousK = kFromDefault.current;
  557. const previousPresets = presetsFromDefault.current;
  558. setSelectedProfiles(prev => {
  559. const next = new Map(prev);
  560. for (const [key, value] of previousK) if (next.get(key) === value) next.delete(key);
  561. const filled = new Map<string, CalibrationProfile>();
  562. for (const [key, value] of kRows) {
  563. if (next.has(key) || userKKeys.current.has(key)) continue;
  564. next.set(key, value);
  565. filled.set(key, value);
  566. }
  567. kFromDefault.current = filled;
  568. return next;
  569. });
  570. const presetRows = new Map<string, PresetChoice>();
  571. for (const row of resolved?.presets ?? []) {
  572. presetRows.set(presetKey(row.printer_model, row.nozzle_diameter || ''), {
  573. code: row.slicer_filament || '',
  574. name: row.slicer_filament_name || '',
  575. });
  576. }
  577. setModelPresets(prev => {
  578. const next = new Map(prev);
  579. for (const [key, value] of previousPresets) if (next.get(key) === value) next.delete(key);
  580. const filled = new Map<string, PresetChoice>();
  581. for (const [key, value] of presetRows) {
  582. if (next.has(key) || userPresetKeys.current.has(key)) continue;
  583. next.set(key, value);
  584. filled.set(key, value);
  585. }
  586. presetsFromDefault.current = filled;
  587. return next;
  588. });
  589. setPrefilledFromDefault(kRows.size > 0 || presetRows.size > 0 || !!resolved?.slicer_filament);
  590. // The current preset, not the one of the render that asked.
  591. if (resolved?.slicer_filament && (!formDataRef.current.slicer_filament || presetFromDefault.current)) {
  592. setFormData(prev => ({ ...prev, slicer_filament: resolved.slicer_filament || '' }));
  593. setPresetInputValue(resolved.slicer_filament_name || resolved.slicer_filament);
  594. presetFromDefault.current = true;
  595. } else if (presetFromDefault.current) {
  596. // The previous product's preset; this one's default has none.
  597. setFormData(prev => ({ ...prev, slicer_filament: '' }));
  598. setPresetInputValue('');
  599. presetFromDefault.current = false;
  600. }
  601. };
  602. if (!material) {
  603. apply(null);
  604. return;
  605. }
  606. // Typed field by field: ask once the typing stops.
  607. const timer = setTimeout(() => {
  608. api
  609. .resolveProfileDefault({
  610. brand: formData.brand.trim(),
  611. material,
  612. subtype: formData.subtype.trim(),
  613. color_name: formData.color_name.trim(),
  614. })
  615. .then(apply)
  616. .catch(e => {
  617. // Non-fatal: the create still applies the default on the server.
  618. if (!cancelled) console.error('Failed to look up the profile default:', e);
  619. });
  620. }, 300);
  621. return () => {
  622. cancelled = true;
  623. clearTimeout(timer);
  624. };
  625. }, [isOpen, isCreating, formData.brand, formData.material, formData.subtype, formData.color_name]);
  626. // Legacy rows may have storage_location text but no location_id yet — link when catalog loads.
  627. useEffect(() => {
  628. if (!isOpen || !spool || locationIdTouched || formData.location_id != null) return;
  629. const legacy = spool.storage_location?.trim();
  630. if (!legacy || storageLocations.length === 0) return;
  631. const match = storageLocations.find((l) => l.name.toLowerCase() === legacy.toLowerCase());
  632. if (match) {
  633. setFormData((prev) => (prev.location_id === match.id ? prev : { ...prev, location_id: match.id }));
  634. }
  635. }, [isOpen, spool, storageLocations, formData.location_id, locationIdTouched]);
  636. // Update field helper
  637. const updateField = <K extends keyof SpoolFormData>(key: K, value: SpoolFormData[K]) => {
  638. const isLinkedField = SPOOLMAN_LINKED_FIELDS.has(key);
  639. if (spoolmanMode && isLinkedField && formData.spoolman_filament_id !== null) {
  640. showToast(t('inventory.spoolmanFilamentUnlinked'), 'info');
  641. }
  642. setFormData(prev => ({
  643. ...prev,
  644. [key]: value,
  645. ...(spoolmanMode && isLinkedField && prev.spoolman_filament_id !== null
  646. ? { spoolman_filament_id: null }
  647. : {}),
  648. }));
  649. if (key === 'weight_used') setWeightTouched(true);
  650. if (key === 'core_weight') setCoreWeightTouched(true);
  651. if (key === 'location_id') setLocationIdTouched(true);
  652. if (key === 'last_dried_at') setLastDriedTouched(true);
  653. // A preset the user picked is theirs: the next product's default leaves it alone.
  654. if (key === 'slicer_filament') presetFromDefault.current = false;
  655. if (errors[key]) {
  656. setErrors(prev => ({ ...prev, [key]: undefined }));
  657. }
  658. };
  659. // Prefill form from a Spoolman filament catalog entry
  660. // subtype extraction mirrors _spoolman_helpers.py logic
  661. const handleFilamentSelect = (filament: SpoolmanFilamentEntry) => {
  662. const material = filament.material || '';
  663. const name = filament.name || '';
  664. const subtype = material && name.startsWith(material) ? name.slice(material.length).trim() : name;
  665. const rawHex = (filament.color_hex ?? '').replace('#', '').toUpperCase();
  666. // Guard against short/malformed hex values — 6 chars (RRGGBB), or 8 when the
  667. // filament is translucent and carries its own alpha (#2912). Rejecting 8 here
  668. // prefilled a clear filament picked from the Spoolman catalogue as 808080FF.
  669. const colorHex = /^[0-9A-F]{6}(?:[0-9A-F]{2})?$/.test(rawHex) ? rawHex : '808080';
  670. const prefillRgba = colorHex.length === 8 ? colorHex : `${colorHex}FF`;
  671. setFormData(prev => ({
  672. ...prev,
  673. spoolman_filament_id: filament.id,
  674. material,
  675. subtype,
  676. brand: filament.vendor?.name || '',
  677. rgba: prefillRgba,
  678. color_name: filament.color_name || '',
  679. label_weight: filament.weight ?? prev.label_weight,
  680. }));
  681. showToast(t('inventory.spoolmanFilamentSelected'), 'success');
  682. };
  683. // Handle color selection
  684. const handleColorUsed = (color: ColorPreset) => {
  685. setRecentColors(prev => saveRecentColor(color, prev));
  686. };
  687. // Mutations – dispatch to Spoolman proxy or local inventory based on mode
  688. const createMutation = useMutation({
  689. mutationFn: (data: Record<string, unknown>) =>
  690. spoolmanMode
  691. ? api.createSpoolmanInventorySpool(data as Parameters<typeof api.createSpoolmanInventorySpool>[0])
  692. : api.createSpool(data as Parameters<typeof api.createSpool>[0]),
  693. onSuccess: async (newSpool) => {
  694. if (newSpool?.id) {
  695. const ok = await savePrinterProfiles(newSpool.id);
  696. if (!ok) return;
  697. const suppliersOk = await saveSupplierLinks(newSpool.id);
  698. if (!suppliersOk) return;
  699. }
  700. await saveProfileDefault();
  701. await refreshSpoolQueries();
  702. if (onSpoolsCreated) onSpoolsCreated([newSpool]);
  703. showToast(t('inventory.spoolCreated'), 'success');
  704. onClose();
  705. },
  706. onError: (error: Error) => {
  707. if (error instanceof ApiError && error.status === 503) {
  708. showToast(t('inventory.spoolmanUnreachable'), 'error');
  709. } else {
  710. showToast(t('inventory.saveFailed'), 'error');
  711. }
  712. },
  713. });
  714. const bulkCreateMutation = useMutation<
  715. SpoolmanBulkCreateResult | InventorySpool[],
  716. Error,
  717. { data: Record<string, unknown>; qty: number }
  718. >({
  719. mutationFn: ({ data, qty }) =>
  720. spoolmanMode
  721. ? api.bulkCreateSpoolmanInventorySpools(data as Parameters<typeof api.bulkCreateSpoolmanInventorySpools>[0], qty)
  722. : api.bulkCreateSpools(data as Parameters<typeof api.bulkCreateSpools>[0], qty),
  723. onSuccess: async (result) => {
  724. // Spoolman bulk-create returns SpoolmanBulkCreateResult (207); local returns InventorySpool[].
  725. // Cast via unknown to satisfy strict TypeScript — the runtime shape is guaranteed by
  726. // the duck-type check ('created' in result) before any property access.
  727. const spoolmanResult = (spoolmanMode && 'created' in result)
  728. ? (result as unknown as SpoolmanBulkCreateResult)
  729. : null;
  730. const createdSpools: InventorySpool[] = spoolmanResult
  731. ? spoolmanResult.created
  732. : (result as InventorySpool[]);
  733. // Bulk create: every copy gets the same profiles and overrides. A no-op
  734. // when the user left the Printers tab alone (savePrinterProfiles), so a
  735. // plain bulk add neither fires two writes per spool nor clears the
  736. // profile default the backend gave every copy (#1326).
  737. for (const s of createdSpools) {
  738. await savePrinterProfiles(s.id);
  739. }
  740. // Every copy of a bulk add shares the same supplier assignments (#2988).
  741. if (shouldSaveSupplierLinks) {
  742. for (const s of createdSpools) {
  743. await saveSupplierLinks(s.id);
  744. }
  745. }
  746. await saveProfileDefault();
  747. await refreshSpoolQueries();
  748. if (onSpoolsCreated) onSpoolsCreated(createdSpools);
  749. if (spoolmanResult && spoolmanResult.failed_count > 0) {
  750. showToast(
  751. t('inventory.spoolsPartiallyCreated', {
  752. created: createdSpools.length,
  753. total: spoolmanResult.requested_count,
  754. }),
  755. 'warning',
  756. );
  757. } else {
  758. showToast(t('inventory.spoolsCreated', { count: createdSpools.length }), 'success');
  759. }
  760. onClose();
  761. },
  762. onError: (error: Error) => {
  763. if (error instanceof ApiError && error.status === 503) {
  764. showToast(t('inventory.spoolmanUnreachable'), 'error');
  765. } else {
  766. showToast(t('inventory.saveFailed'), 'error');
  767. }
  768. },
  769. });
  770. const updateMutation = useMutation({
  771. mutationFn: (data: Record<string, unknown>) =>
  772. spoolmanMode
  773. ? api.updateSpoolmanInventorySpool(spool!.id, data as Parameters<typeof api.updateSpoolmanInventorySpool>[1])
  774. : api.updateSpool(spool!.id, data as Parameters<typeof api.updateSpool>[1]),
  775. onSuccess: async () => {
  776. if (spool?.id) {
  777. const ok = await savePrinterProfiles(spool.id);
  778. if (!ok) return;
  779. const suppliersOk = await saveSupplierLinks(spool.id);
  780. if (!suppliersOk) return;
  781. }
  782. await saveProfileDefault();
  783. await refreshSpoolQueries();
  784. showToast(t('inventory.spoolUpdated'), 'success');
  785. onClose();
  786. },
  787. onError: (error: Error) => {
  788. if (error instanceof ApiError && error.status === 503) {
  789. showToast(t('inventory.spoolmanUnreachable'), 'error');
  790. } else {
  791. showToast(t('inventory.saveFailed'), 'error');
  792. }
  793. },
  794. });
  795. const deleteTagMutation = useMutation({
  796. mutationFn: () => {
  797. if (spoolmanMode) {
  798. return api.updateSpoolmanInventorySpool(spool!.id, CLEAR_TAG_PAYLOAD as Parameters<typeof api.updateSpoolmanInventorySpool>[1]);
  799. }
  800. return api.updateSpool(spool!.id, CLEAR_TAG_PAYLOAD as Parameters<typeof api.updateSpool>[1]);
  801. },
  802. onSuccess: async () => {
  803. await refreshSpoolQueries();
  804. showToast(t('inventory.rfidCleared', 'RFID tag cleared'), 'success');
  805. onClose();
  806. },
  807. onError: (error: Error) => {
  808. if (error instanceof ApiError && error.status === 503) {
  809. showToast(t('inventory.spoolmanUnreachable'), 'error');
  810. } else {
  811. showToast(t('inventory.tagClearFailed'), 'error');
  812. }
  813. },
  814. });
  815. // Fetch assignment for this spool (to show Unassign button). In Spoolman mode
  816. // the slot assignment lives in the spoolman_slot_assignments table keyed by
  817. // spoolman_spool_id, not in the legacy spool_assignments table — #1336 was the
  818. // resulting "Unassign button is always disabled" report.
  819. const { data: assignments, isLoading: assignmentsLoading } = useQuery({
  820. queryKey: ['spool-assignments'],
  821. queryFn: () => api.getAssignments(),
  822. enabled: isOpen && isEditing && !spoolmanMode,
  823. });
  824. const { data: spoolmanSlotAssignments, isLoading: spoolmanAssignmentsLoading } = useQuery({
  825. queryKey: ['spoolman-slot-assignments', 'all'],
  826. queryFn: () => api.getSpoolmanSlotAssignments(),
  827. enabled: isOpen && isEditing && spoolmanMode,
  828. });
  829. const spoolAssignment = (() => {
  830. if (!spool) return undefined;
  831. if (spoolmanMode) {
  832. return spoolmanSlotAssignments?.find(a => a.spoolman_spool_id === spool.id);
  833. }
  834. return assignments?.find(a => a.spool_id === spool.id);
  835. })();
  836. const assignmentLoading = spoolmanMode ? spoolmanAssignmentsLoading : assignmentsLoading;
  837. // Read inventory + settings caches (already populated by InventoryPage) to
  838. // drive the category autocomplete and low-stock-threshold placeholder. #729
  839. const { data: allSpools } = useQuery({
  840. queryKey: ['inventory-spools'],
  841. queryFn: () => api.getSpools(true),
  842. enabled: isOpen,
  843. });
  844. const { data: settingsForForm } = useUiSettings({ enabled: isOpen });
  845. // Backend Bambu printer-model registry, so the Printers tab can read the
  846. // model out of a preset name and offer each model only its own presets. The
  847. // same query key and staleTime the Configure AMS Slot modal uses -- the
  848. // registry only changes across backend releases, so this is a cache hit
  849. // whenever that modal has been opened.
  850. const { data: printerModelsData } = useQuery({
  851. queryKey: ['slicerPrinterModels'],
  852. queryFn: api.getSlicerPrinterModels,
  853. enabled: isOpen,
  854. staleTime: Infinity,
  855. });
  856. const availableCategories = (() => {
  857. const set = new Set<string>();
  858. for (const s of allSpools ?? []) {
  859. const c = s.category?.trim();
  860. if (c) set.add(c);
  861. }
  862. return Array.from(set).sort((a, b) => a.localeCompare(b));
  863. })();
  864. // Autocomplete for the internal material number (#2870), mirroring the
  865. // category datalist above.
  866. const availableMaterialNumbers = (() => {
  867. const set = new Set<string>();
  868. for (const s of allSpools ?? []) {
  869. const n = s.material_number?.trim();
  870. if (n) set.add(n);
  871. }
  872. return Array.from(set).sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
  873. })();
  874. const globalLowStockThreshold = settingsForForm?.low_stock_threshold ?? 20;
  875. const unassignMutation = useMutation({
  876. mutationFn: async () => {
  877. if (!spoolAssignment) throw new Error('No assignment');
  878. if (spoolmanMode) {
  879. if (!spool) throw new Error('No spool');
  880. await api.unassignSpoolmanSlot(spool.id);
  881. return;
  882. }
  883. await api.unassignSpool(spoolAssignment.printer_id, spoolAssignment.ams_id, spoolAssignment.tray_id);
  884. },
  885. onSuccess: async () => {
  886. if (spoolmanMode) {
  887. await queryClient.invalidateQueries({ queryKey: ['spoolman-slot-assignments', 'all'] });
  888. await queryClient.invalidateQueries({ queryKey: ['spoolman-slot-assignments'] });
  889. } else {
  890. await queryClient.invalidateQueries({ queryKey: ['spool-assignments'] });
  891. }
  892. showToast(t('inventory.unassignSuccess', 'Spool unassigned'), 'success');
  893. onClose();
  894. },
  895. onError: (error: Error) => {
  896. showToast(error.message, 'error');
  897. },
  898. });
  899. // Supplier assignments (#2988): replace-all save.
  900. //
  901. // Skipped on an untouched create so the backend's inheritance can fill the
  902. // new spool in — an empty list would wipe what it just attached. A COPY is
  903. // the opposite case and always saves: the dialog seeded the chips from the
  904. // spool being copied and showed them, so they have to be what the copy
  905. // gets. Inheritance cannot stand in for that — it keys on the (material,
  906. // subtype, brand, color_name) tuple and so resolves to the NEWEST spool of
  907. // the product rather than the one on screen, and in Spoolman mode there is
  908. // no inheritance at all.
  909. const shouldSaveSupplierLinks = supplierLinksTouched || isCopying;
  910. const saveSupplierLinks = async (spoolId: number): Promise<boolean> => {
  911. if (!shouldSaveSupplierLinks) return true;
  912. // Spoolman parity (#2988): the assignment rows live Bambuddy-side either
  913. // way; only the endpoint differs (twin table keyed by the remote id).
  914. const save = spoolmanMode ? api.setSpoolmanSpoolSuppliers : api.setSpoolSuppliers;
  915. try {
  916. await save(
  917. spoolId,
  918. supplierLinks.map((link) => ({
  919. supplier_id: link.supplier_id,
  920. supplier_article_number: link.supplier_article_number.trim() || null,
  921. quoted_price_per_kg: link.quoted_price_per_kg,
  922. is_purchase_source: link.is_purchase_source,
  923. })),
  924. );
  925. return true;
  926. } catch (err) {
  927. console.error('SpoolFormModal.saveSupplierLinks failed:', err);
  928. showToast(t('inventory.suppliers.saveFailed'), 'error');
  929. return false;
  930. }
  931. };
  932. // Save everything the Printers tab holds: one K profile per hotend and the
  933. // per-printer-model preset overrides. Returns false if either write failed,
  934. // which keeps the modal open so the user does not lose what they picked.
  935. // On a create, only once the user changed something there: an untouched
  936. // tab holds nothing, or the profile default the backend applies itself.
  937. const shouldSavePrinterProfiles = isEditing || isCopying || printerProfilesTouched;
  938. const buildPrinterProfiles = (): { profiles: SpoolKProfileInput[]; presets: SpoolFilamentPresetInput[] } => {
  939. // The selection Map is keyed by hotend and holds the calibration itself,
  940. // so nothing has to be resolved back out of the printer's live list. That
  941. // also fixes a real defect in the old key-based lookup: it matched a
  942. // calibration by cali_idx alone, and cali_idx is numbered PER NOZZLE --
  943. // on a dual-nozzle printer it could resolve the other hotend's entry and
  944. // persist that entry's K value and diameter.
  945. const profiles: SpoolKProfileInput[] = [];
  946. for (const [key, cal] of selectedProfiles) {
  947. const [printerIdStr, extruderStr, diameter] = key.split(':');
  948. profiles.push({
  949. printer_id: parseInt(printerIdStr),
  950. extruder: parseInt(extruderStr),
  951. nozzle_diameter: diameter || '0.4',
  952. // The flow the profile was measured on, when the printer declares one.
  953. // Null where it does not (an X1C declares none on any profile), which
  954. // the backend reads as "applies to whatever is fitted" -- the same rule
  955. // that keeps every profile stored before this working.
  956. nozzle_type: normaliseFlow(cal.nozzle_id),
  957. k_value: cal.k_value,
  958. name: cal.name || null,
  959. cali_idx: cal.cali_idx,
  960. setting_id: cal.setting_id || null,
  961. });
  962. }
  963. const presets: SpoolFilamentPresetInput[] = [];
  964. for (const [key, choice] of modelPresets) {
  965. const { model, diameter } = parsePresetKey(key);
  966. if (!model) continue;
  967. presets.push({
  968. printer_model: model,
  969. nozzle_diameter: diameter,
  970. slicer_filament: choice.code || null,
  971. slicer_filament_name: choice.name || null,
  972. });
  973. }
  974. return { profiles, presets };
  975. };
  976. const savePrinterProfiles = async (spoolId: number): Promise<boolean> => {
  977. if (!shouldSavePrinterProfiles) return true;
  978. const saveKApi = spoolmanMode ? api.saveSpoolmanKProfiles : api.saveSpoolKProfiles;
  979. const savePresetApi = spoolmanMode ? api.saveSpoolmanFilamentPresets : api.saveSpoolFilamentPresets;
  980. const { profiles, presets } = buildPrinterProfiles();
  981. // Both are full replacements, so both run even when empty -- that is how
  982. // the user clears the last profile or the last override.
  983. try {
  984. await saveKApi(spoolId, profiles);
  985. } catch (e) {
  986. console.error('Failed to save K-profiles:', e);
  987. showToast(t('inventory.kProfileSaveFailed'), 'warning');
  988. return false;
  989. }
  990. try {
  991. await savePresetApi(spoolId, presets);
  992. } catch (e) {
  993. console.error('Failed to save filament preset overrides:', e);
  994. showToast(t('inventory.filamentPresetSaveFailed'), 'warning');
  995. return false;
  996. }
  997. return true;
  998. };
  999. const profileDefaultProduct = [formData.brand, formData.material, formData.subtype]
  1000. .map(part => part.trim())
  1001. .filter(Boolean)
  1002. .join(' ');
  1003. // "Save as default" on the Printers tab (#1326): the tab and the spool's own
  1004. // preset become what new spools of this product start with. After the
  1005. // spool is saved, so a refused spool save leaves the default alone; a failed
  1006. // default save only warns -- the spool is saved either way.
  1007. const saveProfileDefault = async (): Promise<void> => {
  1008. if (!saveAsDefault || !formData.material.trim()) return;
  1009. const { profiles, presets } = buildPrinterProfiles();
  1010. const slicerFilament = formData.slicer_filament || null;
  1011. try {
  1012. await api.saveProfileDefault({
  1013. brand: formData.brand.trim(),
  1014. material: formData.material.trim(),
  1015. subtype: formData.subtype.trim(),
  1016. // "Only <colour>" needs a colour; without one the tick box shows, and
  1017. // this saves, the default for all colours.
  1018. color_name: defaultScope === 'colour' ? formData.color_name.trim() : '',
  1019. slicer_filament: slicerFilament,
  1020. slicer_filament_name: slicerFilament ? selectedPresetOption?.displayName || presetInputValue || null : null,
  1021. presets,
  1022. k_profiles: profiles,
  1023. });
  1024. await queryClient.invalidateQueries({ queryKey: ['profile-defaults'] });
  1025. } catch (e) {
  1026. console.error('Failed to save the profile default:', e);
  1027. showToast(t('inventory.profileDefaults.saveFailed'), 'warning');
  1028. }
  1029. };
  1030. // Close on Escape key
  1031. useEffect(() => {
  1032. if (!isOpen) return;
  1033. const handleKeyDown = (e: KeyboardEvent) => {
  1034. // The nested AMS assignment dialog owns Escape while it is open. Both
  1035. // listeners live on document, so propagation control in the child would
  1036. // not prevent this editor handler from running for the same key press.
  1037. if (e.key === 'Escape' && !showAssignModal) onClose();
  1038. };
  1039. document.addEventListener('keydown', handleKeyDown);
  1040. return () => document.removeEventListener('keydown', handleKeyDown);
  1041. }, [isOpen, onClose, showAssignModal]);
  1042. if (!isOpen) return null;
  1043. const handleSubmit = () => {
  1044. const validation = validateForm(formData, quickAdd, spoolmanMode, mode);
  1045. if (!validation.isValid) {
  1046. setErrors(validation.errors);
  1047. if (validation.errors.slicer_filament || validation.errors.material || validation.errors.brand || validation.errors.subtype) {
  1048. setActiveTab('filament');
  1049. }
  1050. return;
  1051. }
  1052. // The chosen preset's name; none without a preset (#3596).
  1053. const presetName = formData.slicer_filament
  1054. ? selectedPresetOption?.displayName || presetInputValue || null
  1055. : null;
  1056. const data: Record<string, unknown> = {
  1057. material: formData.material || null,
  1058. subtype: formData.subtype || null,
  1059. brand: formData.brand || null,
  1060. color_name: formData.color_name || null,
  1061. rgba: formData.rgba || null,
  1062. extra_colors: formData.extra_colors || null,
  1063. effect_type: formData.effect_type || null,
  1064. label_weight: formData.label_weight,
  1065. // In Spoolman mode the picker opens on the tare the spool resolves to,
  1066. // which is the filament type's unless the spool has its own. Sending it
  1067. // untouched would copy that inherited value onto the spool and stop it
  1068. // following the filament, so only a value the user set goes out (#2908).
  1069. // The catalogue id has no field on the Spoolman side.
  1070. ...(spoolmanMode
  1071. ? coreWeightTouched
  1072. ? { core_weight: formData.core_weight }
  1073. : {}
  1074. : { core_weight: formData.core_weight, core_weight_catalog_id: formData.core_weight_catalog_id }),
  1075. slicer_filament: formData.slicer_filament || null,
  1076. slicer_filament_name: presetName,
  1077. nozzle_temp_min: null,
  1078. nozzle_temp_max: null,
  1079. note: formData.note || null,
  1080. cost_per_kg: formData.cost_per_kg,
  1081. category: formData.category.trim() || null,
  1082. low_stock_threshold_pct: formData.low_stock_threshold_pct,
  1083. material_number: formData.material_number.trim() || null,
  1084. ...(spoolmanMode ? { spoolman_filament_id: formData.spoolman_filament_id } : {}),
  1085. };
  1086. // Only send weight_used when creating or when explicitly changed by the user.
  1087. // This prevents stale cached values from overwriting usage-tracker data.
  1088. if (!isEditing || weightTouched) {
  1089. data.weight_used = formData.weight_used;
  1090. }
  1091. // Only send location_id when creating or when explicitly changed by the user.
  1092. // Backend derives storage_location; omitting on untouched edit avoids stale overwrites.
  1093. if (!isEditing || locationIdTouched) {
  1094. data.location_id = formData.location_id;
  1095. }
  1096. if (isEditing && lastDriedTouched) {
  1097. data.last_dried_at = formData.last_dried_at ? new Date(formData.last_dried_at).toISOString() : null;
  1098. }
  1099. if (isEditing) {
  1100. updateMutation.mutate(data);
  1101. } else if (quantity > 1) {
  1102. bulkCreateMutation.mutate({ data, qty: quantity });
  1103. } else {
  1104. createMutation.mutate(data);
  1105. }
  1106. };
  1107. const isPending = createMutation.isPending || bulkCreateMutation.isPending || updateMutation.isPending || deleteTagMutation.isPending || unassignMutation.isPending;
  1108. return (
  1109. <>
  1110. <div className="fixed inset-0 z-50 flex items-center justify-center">
  1111. <div
  1112. className="absolute inset-0 bg-black/60 backdrop-blur-sm"
  1113. onClick={onClose}
  1114. />
  1115. {/* Wider than the old max-w-xl: the Printers tab is a model list beside
  1116. a detail pane holding a preset row per nozzle size and a hotend-by-
  1117. size grid, which needs room for both. Held constant across tabs
  1118. rather than sized per tab -- a modal that resizes as you switch tabs
  1119. reads as a layout bug. */}
  1120. <div className="relative w-full max-w-5xl mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] flex flex-col">
  1121. {/* Header */}
  1122. <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary flex-shrink-0">
  1123. <h2 className="text-lg font-semibold text-white flex items-baseline gap-2">
  1124. {isEditing ? t('inventory.editSpool') : isCopying ? t('inventory.copySpool') : t('inventory.addSpool')}
  1125. {isEditing && spool && (
  1126. <span className="text-sm font-mono text-bambu-gray">#{spool.id}</span>
  1127. )}
  1128. </h2>
  1129. <button
  1130. onClick={onClose}
  1131. className="p-1 text-bambu-gray hover:text-white rounded transition-colors"
  1132. >
  1133. <X className="w-5 h-5" />
  1134. </button>
  1135. </div>
  1136. {/* Quick Add toggle — only in create mode (not edit, not copy).
  1137. In copy mode the modal title is the singular "Copy Spool", so the
  1138. quantity-driven bulkCreateMutation path would silently produce N
  1139. copies under a misleading title — keep this toggle out of that
  1140. mode entirely. */}
  1141. {mode === 'create' && (
  1142. <div className="flex items-center justify-between px-4 py-2 border-b border-bambu-dark-tertiary flex-shrink-0">
  1143. <div className="flex items-center gap-2">
  1144. <Zap className="w-4 h-4 text-amber-600 dark:text-amber-400" />
  1145. <span className="text-sm text-white">{t('inventory.quickAdd')}</span>
  1146. </div>
  1147. <button
  1148. type="button"
  1149. onClick={() => {
  1150. setQuickAdd(!quickAdd);
  1151. if (!quickAdd) setActiveTab('filament');
  1152. }}
  1153. className={`relative inline-flex h-5 w-9 items-center rounded-full transition-colors ${
  1154. quickAdd ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
  1155. }`}
  1156. >
  1157. <span
  1158. className={`inline-block h-3.5 w-3.5 rounded-full bg-white transition-transform ${
  1159. quickAdd ? 'translate-x-4' : 'translate-x-0.5'
  1160. }`}
  1161. />
  1162. </button>
  1163. </div>
  1164. )}
  1165. {/* Tabs */}
  1166. <div className="flex border-b border-bambu-dark-tertiary flex-shrink-0">
  1167. <button
  1168. onClick={() => setActiveTab('filament')}
  1169. className={`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${
  1170. activeTab === 'filament'
  1171. ? 'text-bambu-green border-b-2 border-bambu-green'
  1172. : 'text-bambu-gray hover:text-white'
  1173. }`}
  1174. >
  1175. <Beaker className="w-4 h-4" />
  1176. {t('inventory.filamentInfoTab')}
  1177. </button>
  1178. {!quickAdd && (
  1179. <button
  1180. onClick={() => setActiveTab('appearance')}
  1181. className={`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${
  1182. activeTab === 'appearance'
  1183. ? 'text-bambu-green border-b-2 border-bambu-green'
  1184. : 'text-bambu-gray hover:text-white'
  1185. }`}
  1186. >
  1187. <Palette className="w-4 h-4" />
  1188. {t('inventory.colorAndCostTab')}
  1189. </button>
  1190. )}
  1191. {!quickAdd && (
  1192. <button
  1193. onClick={() => setActiveTab('printers')}
  1194. className={`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${
  1195. activeTab === 'printers'
  1196. ? 'text-bambu-green border-b-2 border-bambu-green'
  1197. : 'text-bambu-gray hover:text-white'
  1198. }`}
  1199. >
  1200. <Zap className="w-4 h-4" />
  1201. {t('inventory.printersTab')}
  1202. {selectedProfileCount > 0 && (
  1203. <span className="text-xs px-1.5 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green">
  1204. {selectedProfileCount}
  1205. </span>
  1206. )}
  1207. </button>
  1208. )}
  1209. </div>
  1210. {/* Content */}
  1211. <div className="p-4 overflow-y-auto flex-1" style={{ scrollbarGutter: 'stable' }}>
  1212. {activeTab === 'filament' ? (
  1213. <div className="space-y-6">
  1214. {/* Spoolman Filament Catalog Picker — only when creating a spool in Spoolman mode */}
  1215. {spoolmanMode && !isEditing && (
  1216. <div>
  1217. {filamentsError ? (
  1218. <p className="text-sm text-red-700 dark:text-red-400 px-1">{t('inventory.spoolmanCatalogLoadFailed')}</p>
  1219. ) : (
  1220. <SpoolmanFilamentPicker
  1221. filaments={spoolmanFilaments}
  1222. isLoading={isLoadingFilaments}
  1223. selectedId={formData.spoolman_filament_id}
  1224. onSelect={handleFilamentSelect}
  1225. />
  1226. )}
  1227. </div>
  1228. )}
  1229. {/* Filament Info Section */}
  1230. <div>
  1231. <h3 className="text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3">
  1232. {t('inventory.filamentInfo')}
  1233. </h3>
  1234. <FilamentSection
  1235. formData={formData}
  1236. updateField={updateField}
  1237. cloudAuthenticated={cloudAuthenticated}
  1238. loadingCloudPresets={loadingCloudPresets}
  1239. presetInputValue={presetInputValue}
  1240. setPresetInputValue={setPresetInputValue}
  1241. selectedPresetOption={selectedPresetOption}
  1242. filamentOptions={filamentOptions}
  1243. availableBrands={availableBrands}
  1244. availableMaterials={availableMaterials}
  1245. suggestedBrands={suggestedBrands}
  1246. suggestedMaterials={suggestedMaterials}
  1247. quickAdd={quickAdd}
  1248. detailsRequired={!quickAdd && !spoolmanMode && mode === 'create'}
  1249. quantity={quantity}
  1250. onQuantityChange={setQuantity}
  1251. errors={errors}
  1252. />
  1253. </div>
  1254. </div>
  1255. ) : activeTab === 'appearance' ? (
  1256. <div className="space-y-6">
  1257. {/* Color Section */}
  1258. <div>
  1259. <h3 className="text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3">
  1260. {t('inventory.color')}
  1261. </h3>
  1262. <ColorSection
  1263. formData={formData}
  1264. updateField={updateField}
  1265. recentColors={recentColors}
  1266. onColorUsed={handleColorUsed}
  1267. catalogColors={colorCatalog}
  1268. />
  1269. </div>
  1270. {/* Additional Section */}
  1271. <div>
  1272. <h3 className="text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3">
  1273. {t('inventory.additional')}
  1274. </h3>
  1275. <AdditionalSection
  1276. formData={formData}
  1277. updateField={updateField}
  1278. spoolCatalog={spoolCatalog}
  1279. currencySymbol={currencySymbol}
  1280. availableCategories={availableCategories}
  1281. availableMaterialNumbers={availableMaterialNumbers}
  1282. availableLocations={storageLocations}
  1283. showLastDried={isEditing}
  1284. onCreateLocation={async (name) => {
  1285. try {
  1286. const created = await api.createLocation({ name });
  1287. setStorageLocations((prev) => [...prev, { id: created.id, name: created.name }].sort((a, b) => a.name.localeCompare(b.name)));
  1288. await invalidateInventoryLocations(queryClient);
  1289. return { id: created.id, name: created.name };
  1290. } catch (e) {
  1291. // Surface the backend's actual error so the user can
  1292. // distinguish 409 duplicate / 400 validation / 500 from
  1293. // a generic "save failed" message.
  1294. console.error(e);
  1295. const message = e instanceof Error ? e.message : t('locations.saveFailed');
  1296. showToast(message || t('locations.saveFailed'), 'error');
  1297. return null;
  1298. }
  1299. }}
  1300. globalLowStockThreshold={globalLowStockThreshold}
  1301. spoolmanMode={spoolmanMode}
  1302. />
  1303. </div>
  1304. {/* Suppliers (#2988) — both inventories: the assignments live
  1305. Bambuddy-side either way (Spoolman's vendor is the
  1306. manufacturer, not the seller), so the same section renders
  1307. in Spoolman mode and saves to the twin endpoint. */}
  1308. <div>
  1309. <SupplierSection
  1310. links={supplierLinks}
  1311. onChange={(next) => {
  1312. setSupplierLinks(next);
  1313. setSupplierLinksTouched(true);
  1314. }}
  1315. currencySymbol={currencySymbol}
  1316. />
  1317. </div>
  1318. {/* Usage History: in Spoolman mode, the prints Bambuddy charged to the spool (#3407) */}
  1319. {isEditing && spool && (
  1320. <div>
  1321. <SpoolUsageHistory spoolId={spool.id} spoolman={spoolmanMode} createdAt={spool.created_at} />
  1322. </div>
  1323. )}
  1324. </div>
  1325. ) : (
  1326. <PrinterProfilesSection
  1327. formData={formData}
  1328. printersWithCalibrations={resolvedCalibrations}
  1329. filamentOptions={filamentOptions}
  1330. modelPresets={modelPresets}
  1331. setModelPresets={editModelPresets}
  1332. selectedProfiles={selectedProfiles}
  1333. setSelectedProfiles={editSelectedProfiles}
  1334. selectedGroupId={selectedGroupId}
  1335. setSelectedGroupId={setSelectedGroupId}
  1336. printerModels={printerModelsData}
  1337. isLoading={loadingCalibrations}
  1338. />
  1339. )}
  1340. {activeTab === 'printers' && (
  1341. <ProfileDefaultOption
  1342. product={profileDefaultProduct}
  1343. colorName={formData.color_name}
  1344. prefilled={prefilledFromDefault}
  1345. checked={saveAsDefault}
  1346. onCheckedChange={setSaveAsDefault}
  1347. scope={defaultScope}
  1348. onScopeChange={setDefaultScope}
  1349. disabled={!formData.material.trim()}
  1350. />
  1351. )}
  1352. </div>
  1353. {/* Footer */}
  1354. <div className="flex gap-2 p-4 border-t border-bambu-dark-tertiary flex-shrink-0">
  1355. {isEditing && (
  1356. <div className="flex gap-2 mr-auto">
  1357. {/* Either identifier counts as "tagged". A Bambu Lab spool is
  1358. linked by its 32-char tray UUID and carries no tag_uid at all
  1359. -- in Spoolman mode that is every Bambu spool, because
  1360. _map_spoolman_spool splits extra.tag by length -- so gating on
  1361. tag_uid alone left this permanently greyed out for them, while
  1362. the Tag ID column beside it showed the UUID and the payload
  1363. below already cleared both fields (#3109). */}
  1364. <Button
  1365. variant="secondary"
  1366. onClick={() => deleteTagMutation.mutate()}
  1367. disabled={isPending || !(spool?.tag_uid || spool?.tray_uuid)}
  1368. >
  1369. <Tag className="w-4 h-4" />
  1370. {t('inventory.clearRfid', 'Clear RFID Tag')}
  1371. </Button>
  1372. {spoolAssignment ? (
  1373. <Button
  1374. variant="secondary"
  1375. onClick={() => unassignMutation.mutate()}
  1376. disabled={isPending || assignmentLoading}
  1377. >
  1378. <Unlink className="w-4 h-4" />
  1379. {t('inventory.unassignSpool', 'Unassign')}
  1380. </Button>
  1381. ) : (
  1382. <Button
  1383. variant="secondary"
  1384. onClick={() => setShowAssignModal(true)}
  1385. disabled={isPending || assignmentLoading}
  1386. >
  1387. <Link2 className="w-4 h-4" />
  1388. {t('inventory.assignSpool', 'Assign Spool')}
  1389. </Button>
  1390. )}
  1391. </div>
  1392. )}
  1393. <div className="flex gap-2 ml-auto">
  1394. <Button variant="secondary" onClick={onClose}>
  1395. {t('common.cancel')}
  1396. </Button>
  1397. <Button
  1398. onClick={handleSubmit}
  1399. disabled={isPending}
  1400. >
  1401. {isPending ? (
  1402. <>
  1403. <Loader2 className="w-4 h-4 animate-spin" />
  1404. {t('common.saving')}
  1405. </>
  1406. ) : (
  1407. <>
  1408. <Save className="w-4 h-4" />
  1409. {isEditing ? t('common.save') : isCopying ? t('inventory.copySpool') : t('inventory.addSpool')}
  1410. </>
  1411. )}
  1412. </Button>
  1413. </div>
  1414. </div>
  1415. </div>
  1416. </div>
  1417. {isEditing && spool && (
  1418. <AssignToAmsModal
  1419. isOpen={showAssignModal}
  1420. onClose={() => setShowAssignModal(false)}
  1421. spool={spool}
  1422. printerId={null}
  1423. variant="dialog"
  1424. spoolmanMode={spoolmanMode}
  1425. />
  1426. )}
  1427. </>
  1428. );
  1429. }