SliceModal.tsx 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. import { Cloud, CloudOff, Cog, Loader2, Package, X } from 'lucide-react';
  2. import { useEffect, useMemo, useState } from 'react';
  3. import { useTranslation } from 'react-i18next';
  4. import { useMutation, useQuery } from '@tanstack/react-query';
  5. import {
  6. api,
  7. type PresetRef,
  8. type PresetSource,
  9. type SliceBundleSpec,
  10. type SliceJobProgress,
  11. type SliceRequest,
  12. type SlicerBundle,
  13. type SlicerCloudStatus,
  14. type UnifiedPreset,
  15. type UnifiedPresetsBySlot,
  16. type UnifiedPresetsResponse,
  17. } from '../api/client';
  18. import { useSliceJobTracker } from '../contexts/SliceJobTrackerContext';
  19. import { useToast } from '../contexts/ToastContext';
  20. import { PlatePickerModal } from './PlatePickerModal';
  21. import type { PlateFilament } from '../types/plates';
  22. import { normalizeColorForCompare, colorsAreSimilar } from '../utils/amsHelpers';
  23. import {
  24. presetCompatibility,
  25. buildCompatibilityIndex,
  26. EMPTY_COMPATIBILITY_INDEX,
  27. type PrinterCompatibilityIndex,
  28. } from '../utils/slicerPrinterMatch';
  29. export type SliceSource =
  30. | { kind: 'libraryFile'; id: number; filename: string }
  31. | { kind: 'archive'; id: number; filename: string };
  32. interface SliceModalProps {
  33. source: SliceSource;
  34. onClose: () => void;
  35. }
  36. type Slot = 'printer' | 'process' | 'filament';
  37. // SliceModal-specific tier priority: local (imported) → cloud → standard.
  38. // Imported profiles are surfaced first because they're the user's curated
  39. // picks (often colour/type-tagged), cloud is second since names alone can't
  40. // drive metadata-aware match, standard is the bundled fallback. This is
  41. // distinct from the listing endpoint's dedup order and only affects what
  42. // the SliceModal renders / pre-picks.
  43. const SLICE_MODAL_TIER_ORDER = ['local', 'cloud', 'standard'] as const;
  44. function pickDefault(by: UnifiedPresetsResponse, slot: Slot): PresetRef | null {
  45. for (const tier of SLICE_MODAL_TIER_ORDER) {
  46. const list = by[tier][slot];
  47. if (list.length > 0) {
  48. return { source: list[0].source, id: list[0].id };
  49. }
  50. }
  51. return null;
  52. }
  53. // Resolve a PresetRef back to its UnifiedPreset within the named slot, or
  54. // null if it no longer resolves (e.g. the preset was deleted between the
  55. // listing fetch and selection).
  56. function findPreset(
  57. by: UnifiedPresetsResponse,
  58. ref: PresetRef | null,
  59. slot: Slot,
  60. ): UnifiedPreset | null {
  61. if (!ref) return null;
  62. return by[ref.source][slot].find((p) => p.id === ref.id) ?? null;
  63. }
  64. // Find a preset by exact name across tiers (local → cloud → standard). Used
  65. // to honour the printer / process preset names a 3MF was prepared with.
  66. function findPresetByName(
  67. by: UnifiedPresetsResponse,
  68. slot: Slot,
  69. name: string | null | undefined,
  70. ): PresetRef | null {
  71. if (!name) return null;
  72. for (const tier of SLICE_MODAL_TIER_ORDER) {
  73. const p = by[tier][slot].find((x) => x.name === name);
  74. if (p) return { source: p.source, id: p.id };
  75. }
  76. return null;
  77. }
  78. // Process default: honour the process preset the 3MF was prepared with
  79. // (preferredName) when it's available and not incompatible with the selected
  80. // printer; otherwise the first preset compatible with the printer in tier
  81. // order, then the first whose compatibility is merely unknown, then plain
  82. // priority. Keeps the pre-pick honest with both the embedded config and the
  83. // printer filter instead of blindly taking list[0] (#1325).
  84. function pickProcessDefault(
  85. by: UnifiedPresetsResponse,
  86. printerName: string | null,
  87. compatIndex: PrinterCompatibilityIndex,
  88. preferredName?: string | null,
  89. ): PresetRef | null {
  90. const preferred = findPresetByName(by, 'process', preferredName);
  91. if (preferred) {
  92. const p = findPreset(by, preferred, 'process');
  93. if (p && presetCompatibility(p, 'process', printerName, compatIndex) !== 'mismatch') {
  94. return preferred;
  95. }
  96. }
  97. for (const wanted of ['match', 'unknown'] as const) {
  98. for (const tier of SLICE_MODAL_TIER_ORDER) {
  99. for (const p of by[tier].process) {
  100. if (presetCompatibility(p, 'process', printerName, compatIndex) === wanted) {
  101. return { source: p.source, id: p.id };
  102. }
  103. }
  104. }
  105. }
  106. return pickDefault(by, 'process');
  107. }
  108. const TIER_BONUS: Record<PresetSource, number> = {
  109. local: 1.5,
  110. cloud: 1.0,
  111. standard: 0.5,
  112. };
  113. function pickFilamentForSlot(
  114. by: UnifiedPresetsResponse,
  115. required: { type: string; color: string },
  116. printerName: string | null,
  117. compatIndex: PrinterCompatibilityIndex,
  118. ): PresetRef | null {
  119. // Score every filament preset against the plate slot's required (type,
  120. // colour) and pick the highest. Mirrors the AMS slot-mapping match in the
  121. // print/schedule modal: type match dominates, exact-colour-match bumps over
  122. // similar-colour-match, and a small per-tier bonus breaks ties so cloud
  123. // user customisations win over standard bundled fallbacks of equal merit.
  124. const reqType = required.type.trim().toUpperCase();
  125. const reqColor = normalizeColorForCompare(required.color);
  126. let best: { ref: PresetRef; score: number } | null = null;
  127. for (const tier of SLICE_MODAL_TIER_ORDER) {
  128. for (const p of by[tier].filament) {
  129. let score = 0;
  130. const presetType = (p.filament_type ?? '').trim().toUpperCase();
  131. const presetColor = normalizeColorForCompare(p.filament_colour ?? '');
  132. if (reqType && presetType && reqType === presetType) score += 10;
  133. if (reqColor && presetColor) {
  134. if (presetColor === reqColor) score += 5;
  135. else if (colorsAreSimilar(p.filament_colour ?? '', required.color)) score += 2;
  136. }
  137. score += TIER_BONUS[tier];
  138. // Demote printer-incompatible filaments (#1325): a penalty rather than a
  139. // hard skip so the pick still degrades gracefully if every filament
  140. // mismatches the selected printer.
  141. if (presetCompatibility(p, 'filament', printerName, compatIndex) === 'mismatch') {
  142. score -= 100;
  143. }
  144. if (best == null || score > best.score) {
  145. best = { ref: { source: p.source, id: p.id }, score };
  146. }
  147. }
  148. }
  149. // Fall back to plain priority pick if every preset scored 0+tier (i.e. no
  150. // metadata matched). The fallback is exactly the single-color default —
  151. // first preset in the highest-priority non-empty tier.
  152. if (best == null) return pickDefault(by, 'filament');
  153. return best.ref;
  154. }
  155. function toRefValue(ref: PresetRef | null): string {
  156. // The HTML `<select>` value space is flat strings; encode source + id so
  157. // the same preset name can live in multiple tiers without collision.
  158. return ref ? `${ref.source}:${ref.id}` : '';
  159. }
  160. function fromRefValue(raw: string): PresetRef | null {
  161. if (!raw) return null;
  162. const idx = raw.indexOf(':');
  163. if (idx < 0) return null;
  164. const source = raw.slice(0, idx) as PresetSource;
  165. const id = raw.slice(idx + 1);
  166. if (source !== 'cloud' && source !== 'local' && source !== 'standard') return null;
  167. return { source, id };
  168. }
  169. // Inline spinner for the filament-requirements query. The backend runs a
  170. // preview slice on first open of an unsliced project file (cached after);
  171. // on a complex multi-color model that's a real slice — multi-second to
  172. // multi-minute. The static "Analyzing plate filaments…" string left
  173. // users wondering whether anything was happening, so the spinner now
  174. // shows elapsed seconds, polls the sidecar's --pipe progress (via the
  175. // /slicer/preview-progress proxy) for live stage + percent, and after ~5s
  176. // surfaces a "this is a one-time slice — repeat opens are instant"
  177. // note so users don't worry it'll be slow forever.
  178. //
  179. // requestId: a UUID generated by the modal when the filament-requirements
  180. // fetch starts. Forwarded to the sidecar via the API call AND used here
  181. // to poll the matching progress snapshot. Same id, two consumers.
  182. function FilamentAnalysisSpinner({
  183. requestId,
  184. sourceName,
  185. }: {
  186. requestId: string;
  187. sourceName: string;
  188. }) {
  189. const { t } = useTranslation();
  190. const { showPersistentToast, dismissToast } = useToast();
  191. const [elapsed, setElapsed] = useState(0);
  192. const [progress, setProgress] = useState<SliceJobProgress | null>(null);
  193. // Defensive decode — see prettifyFilename comment in SliceJobTrackerContext.
  194. let prettyName = sourceName;
  195. try {
  196. prettyName = decodeURIComponent(sourceName);
  197. } catch {
  198. /* keep raw on malformed encoding */
  199. }
  200. // Elapsed-time tick.
  201. useEffect(() => {
  202. const startedAt = Date.now();
  203. const id = setInterval(() => setElapsed(Math.floor((Date.now() - startedAt) / 1000)), 1000);
  204. return () => clearInterval(id);
  205. }, []);
  206. // Progress polling — once per second while the spinner is mounted.
  207. // Mirrors the slice-job tracker's cadence. Sidecar 404s during the
  208. // race window between fetch start and progressStore.start() are
  209. // swallowed by the API method (returns null) so we keep polling.
  210. useEffect(() => {
  211. let cancelled = false;
  212. const id = setInterval(async () => {
  213. if (cancelled) return;
  214. const snap = await api.getPreviewSliceProgress(requestId);
  215. if (!cancelled && snap) setProgress(snap);
  216. }, 1000);
  217. return () => {
  218. cancelled = true;
  219. clearInterval(id);
  220. };
  221. }, [requestId]);
  222. // Mirror the spinner's contents into a persistent toast so the user
  223. // sees activity even when their cursor is elsewhere on the page.
  224. // Dismissed in the parent's effect when the requirements arrive.
  225. const toastId = `slice-preview-${requestId}`;
  226. useEffect(() => {
  227. const hasUseful = progress && progress.stage && progress.total_percent > 0;
  228. const elapsedStr = formatElapsed(elapsed);
  229. if (hasUseful) {
  230. showPersistentToast(
  231. toastId,
  232. t(
  233. 'slice.previewWithProgress',
  234. 'Analyzing {{name}} — {{stage}} ({{percent}}%) — {{elapsed}}',
  235. {
  236. name: prettyName,
  237. stage: progress!.stage,
  238. percent: Math.min(100, Math.max(0, Math.round(progress!.total_percent))),
  239. elapsed: elapsedStr,
  240. },
  241. ),
  242. 'loading',
  243. );
  244. } else {
  245. showPersistentToast(
  246. toastId,
  247. t('slice.previewToast', {
  248. name: prettyName,
  249. elapsed: elapsedStr,
  250. }),
  251. 'loading',
  252. );
  253. }
  254. return () => {
  255. dismissToast(toastId);
  256. };
  257. }, [elapsed, progress, prettyName, showPersistentToast, dismissToast, t, toastId]);
  258. const stage = progress?.stage;
  259. const percent = progress?.total_percent;
  260. const inlineLabel =
  261. stage && typeof percent === 'number' && percent > 0
  262. ? `${stage} (${Math.min(100, Math.max(0, Math.round(percent)))}%)`
  263. : t('slice.analyzingPlateFilaments');
  264. return (
  265. <div className="flex flex-col gap-1 text-bambu-gray text-sm py-2">
  266. <div className="flex items-center gap-2">
  267. <Loader2 className="w-4 h-4 animate-spin" />
  268. {inlineLabel}
  269. <span className="text-xs tabular-nums">{elapsed}s</span>
  270. </div>
  271. {elapsed >= 5 && (
  272. <div className="text-xs text-bambu-gray/70 pl-6">
  273. {t(
  274. 'slice.analyzingPlateFilamentsHint',
  275. 'Running a preview slice to discover which AMS slots this plate uses. Cached after — re-opening is instant.',
  276. )}
  277. </div>
  278. )}
  279. </div>
  280. );
  281. }
  282. function formatElapsed(seconds: number): string {
  283. const s = Math.max(0, Math.floor(seconds));
  284. if (s < 60) return `${s}s`;
  285. const m = Math.floor(s / 60);
  286. const remS = s % 60;
  287. if (m < 60) return `${m}m ${remS}s`;
  288. const h = Math.floor(m / 60);
  289. const remM = m % 60;
  290. return `${h}h ${remM}m`;
  291. }
  292. export function SliceModal({ source, onClose }: SliceModalProps) {
  293. const { t } = useTranslation();
  294. const { trackJob } = useSliceJobTracker();
  295. const [printerPreset, setPrinterPreset] = useState<PresetRef | null>(null);
  296. const [processPreset, setProcessPreset] = useState<PresetRef | null>(null);
  297. // One filament ref per plate slot, in plate order. For STL / single-plate /
  298. // single-color sources this is a one-element array; multi-color 3MFs get one
  299. // entry per AMS slot the plate uses. Pre-pick (effect below) initialises
  300. // each slot from the source plate's required (type, colour).
  301. const [filamentPresets, setFilamentPresets] = useState<(PresetRef | null)[]>([]);
  302. // Bundle dispatch (alternative to the preset triplet). When non-null, the
  303. // SliceModal hides the cloud/local/standard preset dropdowns and shows
  304. // bundle-scoped pickers (process + per-slot filament from the chosen
  305. // bundle's contents). Submit routes through the backend's bundle dispatch
  306. // (`SliceRequest.bundle`) which skips PresetRef resolution.
  307. const [selectedBundleId, setSelectedBundleId] = useState<string | null>(null);
  308. const [bundleProcessName, setBundleProcessName] = useState<string | null>(null);
  309. const [bundleFilamentNames, setBundleFilamentNames] = useState<(string | null)[]>([]);
  310. const [errorMessage, setErrorMessage] = useState<string | null>(null);
  311. // null = plate not yet picked (or single-plate / non-3MF — picker is skipped
  312. // and we'll backfill 1 at submit time). Set to a 1-indexed plate number once
  313. // the user picks one (or implicitly for single-plate sources).
  314. const [selectedPlate, setSelectedPlate] = useState<number | null>(null);
  315. // "Slice all plates" mode: sends ``plate=0`` to the backend which forwards
  316. // ``--slice 0`` to the BS CLI, producing a single output 3MF whose
  317. // ``Metadata/plate_N.gcode`` entries are *all* plates sliced together —
  318. // one archive, one file, all plates. Distinct from the per-plate
  319. // ``selectedPlate`` mode (which slices just that one plate). Filament
  320. // selection in this mode covers every slot the project defines, not
  321. // just the slots the currently-visible plate happens to use — see
  322. // ``allProjectFilamentSlots`` below.
  323. const [sliceAllPlates, setSliceAllPlates] = useState(false);
  324. // Build-plate override (#1337). null = inherit from the process preset
  325. // (the default). Set to a canonical slicer enum value to patch
  326. // curr_bed_type into the resolved process JSON before slicing — needed
  327. // because the process preset's default plate (typically "Cool Plate") is
  328. // incompatible with high-temp filaments like ABS / ASA / PC, and the
  329. // user had no way to switch plates without cloning the preset.
  330. const [bedType, setBedType] = useState<string | null>(null);
  331. const platesQuery = useQuery({
  332. queryKey: ['slicePlates', source.kind, source.id],
  333. queryFn: async () => {
  334. if (source.kind === 'libraryFile') {
  335. return api.getLibraryFilePlates(source.id);
  336. }
  337. return api.getArchivePlates(source.id);
  338. },
  339. staleTime: 60_000,
  340. });
  341. const isMultiPlate =
  342. !!platesQuery.data?.is_multi_plate && (platesQuery.data?.plates?.length ?? 0) > 1;
  343. // Single-plate / non-3MF / fetch failure: skip the picker, default to plate 1
  344. // at submit time so the backend's existing default behaviour is preserved.
  345. const needsPlatePicker = isMultiPlate && selectedPlate == null;
  346. // Per-plate filament requirements via the same endpoint the print/schedule
  347. // modal uses. Reusing it here keeps the SliceModal honest with whatever
  348. // logic that endpoint applies (slice_info parsing, future enhancements for
  349. // unsliced project files, dual-nozzle fields, etc.) instead of duplicating
  350. // extraction. plate_id is always sent: single-plate falls through to plate
  351. // 1 server-side; multi-plate uses the user's pick.
  352. const effectivePlateId = selectedPlate ?? 1;
  353. // Generate a request_id per (source, plate) pair so the backend's
  354. // preview-slice and the FilamentAnalysisSpinner's progress poll share
  355. // the same id. useMemo keeps it stable across renders within the same
  356. // pair; switching plates regenerates so a stale poll doesn't bleed
  357. // progress between plates.
  358. const previewRequestId = useMemo(() => {
  359. const random =
  360. typeof crypto !== 'undefined' && 'randomUUID' in crypto
  361. ? crypto.randomUUID()
  362. : `${Date.now()}-${Math.random().toString(36).slice(2)}`;
  363. // Tag the id with the (source, plate) so logs/Network panel show which
  364. // pair owns the poll. Also lets the lint rule see the deps in use.
  365. return `${source.kind}-${source.id}-p${effectivePlateId}-${random}`;
  366. }, [source.kind, source.id, effectivePlateId]);
  367. const filamentReqsQuery = useQuery({
  368. queryKey: ['sliceFilamentReqs', source.kind, source.id, effectivePlateId],
  369. queryFn: async () => {
  370. if (source.kind === 'libraryFile') {
  371. return api.getLibraryFileFilamentRequirements(source.id, effectivePlateId, previewRequestId);
  372. }
  373. return api.getArchiveFilamentRequirements(source.id, effectivePlateId, previewRequestId);
  374. },
  375. enabled: !needsPlatePicker,
  376. staleTime: 60_000,
  377. });
  378. // Filament slot list for the active plate. Falls back to one synthetic slot
  379. // for STL/STEP and any "no metadata available" case so the modal still
  380. // works (single dropdown, mono-color slice). In ``sliceAllPlates`` mode
  381. // we keep the same slot list (the backend already returns every project
  382. // slot via ``extract_project_filaments_from_3mf``'s fallback path when
  383. // slice_info doesn't carry per-plate filaments) but override every
  384. // slot's ``used_in_plate`` flag to ``true`` so the dropdown labels
  385. // drop the "— not used by this plate" suffix and the dropdowns become
  386. // selectable. Across the whole project, every defined slot IS used by
  387. // at least one plate, so this is correct in slice-all mode.
  388. const filamentSlots = useMemo<PlateFilament[]>(() => {
  389. const reqs = filamentReqsQuery.data?.filaments ?? [];
  390. const base: PlateFilament[] =
  391. reqs.length > 0
  392. ? (reqs as PlateFilament[])
  393. : [{ slot_id: 1, type: '', color: '', used_grams: 0, used_meters: 0 }];
  394. if (sliceAllPlates) {
  395. return base.map((slot) => ({ ...slot, used_in_plate: true }));
  396. }
  397. return base;
  398. }, [sliceAllPlates, filamentReqsQuery.data]);
  399. const presetsQuery = useQuery({
  400. queryKey: ['slicerPresets'],
  401. queryFn: () => api.getSlicerPresets(),
  402. staleTime: 60_000,
  403. // Don't fetch presets while the plate picker is on screen — saves a
  404. // round-trip if the user cancels out of the plate step.
  405. enabled: !platesQuery.isLoading && !needsPlatePicker,
  406. });
  407. // Imported Printer Preset Bundles (.bbscfg). Empty list when no sidecar
  408. // configured / no bundles imported yet; the bundle picker hides itself
  409. // in that case so users without bundles see the original modal layout.
  410. const bundlesQuery = useQuery({
  411. queryKey: ['slicerBundles'],
  412. queryFn: api.listSlicerBundles,
  413. staleTime: 60_000,
  414. enabled: !platesQuery.isLoading && !needsPlatePicker,
  415. // Bundle listing is a hard 503 when the sidecar is offline; don't
  416. // retry tight loops in that case.
  417. retry: false,
  418. });
  419. // Canonical Bambu printer-model registry — drives the @BBL <code> name
  420. // fallback in slicerPrinterMatch when no slicer bundle covers a cloud /
  421. // standard preset (#1325 follow-up). Long staleTime: the registry only
  422. // changes across backend releases.
  423. const printerModelsQuery = useQuery({
  424. queryKey: ['slicerPrinterModels'],
  425. queryFn: api.getSlicerPrinterModels,
  426. staleTime: Infinity,
  427. });
  428. const selectedBundle: SlicerBundle | null = useMemo(() => {
  429. if (!selectedBundleId || !bundlesQuery.data) return null;
  430. return bundlesQuery.data.find((b) => b.id === selectedBundleId) ?? null;
  431. }, [selectedBundleId, bundlesQuery.data]);
  432. const isBundleMode = selectedBundle != null;
  433. // Selected-printer context for the process / filament filter (#1325).
  434. const selectedPrinterName = useMemo<string | null>(() => {
  435. if (!presetsQuery.data || !printerPreset) return null;
  436. return findPreset(presetsQuery.data, printerPreset, 'printer')?.name ?? null;
  437. }, [presetsQuery.data, printerPreset]);
  438. // Compatibility ground truth: the user's uploaded Slicer Bundles plus the
  439. // backend Bambu printer-model registry (#1325 + follow-up). The bundle
  440. // path handles imported / custom presets; the registry-driven @BBL name
  441. // fallback inside slicerPrinterMatch picks up cloud / standard presets
  442. // for users who haven't uploaded bundles yet.
  443. const compatIndex = useMemo<PrinterCompatibilityIndex>(
  444. () => buildCompatibilityIndex(bundlesQuery.data ?? [], printerModelsQuery.data ?? {}),
  445. [bundlesQuery.data, printerModelsQuery.data],
  446. );
  447. // Printer / process preset names the source 3MF was prepared with. The
  448. // plates query resolves before the presets query (the latter is gated on
  449. // it), so these are known by the time the pre-pick effects run.
  450. const embeddedPrinter = platesQuery.data?.embedded_printer ?? null;
  451. const embeddedProcess = platesQuery.data?.embedded_process ?? null;
  452. // Printer pre-pick: defaults to the printer the 3MF was prepared for when
  453. // that preset is available, else the first listed printer. Runs once when
  454. // presets first arrive; later re-renders preserve any manual choice.
  455. useEffect(() => {
  456. const data = presetsQuery.data;
  457. if (!data) return;
  458. if (printerPreset == null) {
  459. setPrinterPreset(
  460. findPresetByName(data, 'printer', embeddedPrinter) ?? pickDefault(data, 'printer'),
  461. );
  462. }
  463. // eslint-disable-next-line react-hooks/exhaustive-deps
  464. }, [presetsQuery.data, embeddedPrinter]);
  465. // Process pre-pick / re-pick (#1325): defaults to a process compatible with
  466. // the selected printer, and re-defaults when a printer change leaves the
  467. // current process incompatible. A compatible or unknown manual pick is kept.
  468. useEffect(() => {
  469. const data = presetsQuery.data;
  470. if (!data) return;
  471. setProcessPreset((current) => {
  472. if (current) {
  473. const p = findPreset(data, current, 'process');
  474. if (p && presetCompatibility(p, 'process', selectedPrinterName, compatIndex) !== 'mismatch') {
  475. return current;
  476. }
  477. }
  478. return pickProcessDefault(data, selectedPrinterName, compatIndex, embeddedProcess);
  479. });
  480. }, [presetsQuery.data, selectedPrinterName, compatIndex, embeddedProcess]);
  481. // Filament pre-pick: re-runs when the active filament-slot count changes
  482. // (plate selection, single-plate metadata arriving) or the selected printer
  483. // changes. Each slot scores every available filament preset against the
  484. // slot's required (type, colour); an existing pick (incl. a user override)
  485. // is kept as long as it's still compatible with the selected printer, while
  486. // null slots and printer-incompatible picks are re-picked (#1325).
  487. useEffect(() => {
  488. const data = presetsQuery.data;
  489. if (!data) return;
  490. setFilamentPresets((current) => {
  491. return filamentSlots.map((slot, i) => {
  492. const cur = current[i] ?? null;
  493. if (cur) {
  494. const p = findPreset(data, cur, 'filament');
  495. if (p && presetCompatibility(p, 'filament', selectedPrinterName, compatIndex) !== 'mismatch') {
  496. return cur;
  497. }
  498. }
  499. return pickFilamentForSlot(
  500. data,
  501. { type: slot.type, color: slot.color },
  502. selectedPrinterName,
  503. compatIndex,
  504. );
  505. });
  506. });
  507. }, [presetsQuery.data, filamentSlots, selectedPrinterName, compatIndex]);
  508. // Bundle-mode auto-pick: when the user picks a bundle (or the slot count
  509. // changes after the picker is open), default the process to the bundle's
  510. // first listed process and every filament slot to the bundle's first
  511. // listed filament. Plain string match — bundles store delta files keyed
  512. // by user preset name, no scoring needed since the user picks per-slot
  513. // afterwards if the default is wrong.
  514. useEffect(() => {
  515. if (!selectedBundle) {
  516. // Reset bundle picks when bundle is cleared so re-selection
  517. // re-defaults rather than carrying stale values.
  518. setBundleProcessName(null);
  519. setBundleFilamentNames([]);
  520. return;
  521. }
  522. setBundleProcessName((current) => {
  523. // Preserve a manual pick if it still exists in the bundle; otherwise
  524. // re-default. Same shape as the preset auto-pick effect above.
  525. if (current && selectedBundle.process.includes(current)) return current;
  526. return selectedBundle.process[0] ?? null;
  527. });
  528. setBundleFilamentNames((current) => {
  529. if (current.length === filamentSlots.length && current.every((n) => n != null)) {
  530. return current;
  531. }
  532. const fallback = selectedBundle.filament[0] ?? null;
  533. return filamentSlots.map((_, i) => current[i] ?? fallback);
  534. });
  535. }, [selectedBundle, filamentSlots]);
  536. const enqueueMutation = useMutation({
  537. mutationFn: async (plate: number | null) => {
  538. const body = buildSliceBody(plate);
  539. if (source.kind === 'libraryFile') {
  540. return api.sliceLibraryFile(source.id, body);
  541. }
  542. return api.sliceArchive(source.id, body);
  543. },
  544. onSuccess: (enqueue) => {
  545. trackJob(enqueue.job_id, source.kind, source.filename);
  546. onClose();
  547. },
  548. onError: (err: unknown) => {
  549. const msg = err instanceof Error ? err.message : String(err);
  550. setErrorMessage(msg);
  551. },
  552. });
  553. // Body builder shared by the single-plate and slice-all paths. ``plate``
  554. // is the 1-indexed plate number to slice, or ``null`` for STL / single-
  555. // plate 3MF sources where the field is omitted entirely.
  556. function buildSliceBody(plate: number | null): SliceRequest {
  557. if (isBundleMode) {
  558. if (
  559. !selectedBundle ||
  560. !bundleProcessName ||
  561. bundleFilamentNames.length === 0 ||
  562. bundleFilamentNames.some((n) => n == null)
  563. ) {
  564. throw new Error(t('slice.bundleAllRequired'));
  565. }
  566. const bundleSpec: SliceBundleSpec = {
  567. bundle_id: selectedBundle.id,
  568. printer_name: selectedBundle.printer[0] ?? selectedBundle.printer_preset_name,
  569. process_name: bundleProcessName,
  570. filament_names: bundleFilamentNames as string[],
  571. };
  572. return {
  573. bundle: bundleSpec,
  574. ...(plate != null ? { plate } : {}),
  575. // Bed-type override (#1337) also flows through the bundle path —
  576. // the sidecar forwards `bedType` as --curr_bed_type to the CLI.
  577. ...(bedType != null ? { bed_type: bedType } : {}),
  578. };
  579. }
  580. if (
  581. !printerPreset ||
  582. !processPreset ||
  583. filamentPresets.length === 0 ||
  584. filamentPresets.some((r) => r == null)
  585. ) {
  586. throw new Error(t('slice.allPresetsRequired'));
  587. }
  588. return {
  589. printer_preset: printerPreset,
  590. process_preset: processPreset,
  591. filament_preset: filamentPresets[0] as PresetRef,
  592. filament_presets: filamentPresets as PresetRef[],
  593. ...(plate != null ? { plate } : {}),
  594. ...(bedType != null ? { bed_type: bedType } : {}),
  595. };
  596. }
  597. // Slice button stays disabled until the preview slice / embedded-metadata
  598. // read has succeeded (filamentReqsQuery.isSuccess) and every filament slot
  599. // has a picked profile.
  600. const isReady = isBundleMode
  601. ? selectedBundle != null &&
  602. bundleProcessName != null &&
  603. filamentReqsQuery.isSuccess &&
  604. bundleFilamentNames.length > 0 &&
  605. bundleFilamentNames.every((n) => n != null)
  606. : printerPreset != null &&
  607. processPreset != null &&
  608. filamentReqsQuery.isSuccess &&
  609. filamentPresets.length > 0 &&
  610. filamentPresets.every((r) => r != null);
  611. const isEnqueuing = enqueueMutation.isPending;
  612. const totalPlateCount = platesQuery.data?.plates?.length ?? 0;
  613. const canSliceAll = isMultiPlate && totalPlateCount > 1 && !needsPlatePicker;
  614. // Step 1: plate picker for multi-plate 3MF sources. Cancelling closes the
  615. // entire flow (matches the existing PlatePickerModal contract used by the
  616. // archive g-code-viewer entry point).
  617. if (needsPlatePicker && platesQuery.data) {
  618. return (
  619. <PlatePickerModal
  620. plates={platesQuery.data.plates}
  621. onSelect={(plateIndex) => setSelectedPlate(plateIndex)}
  622. onClose={onClose}
  623. />
  624. );
  625. }
  626. // Step 2 (or only step for single-plate / non-3MF / load-failure): preset
  627. // picker. While the plates query is in-flight we still render the shell
  628. // because the presets query is gated on it; the loader covers both.
  629. return (
  630. <div
  631. className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4"
  632. onClick={() => {
  633. if (!isEnqueuing) onClose();
  634. }}
  635. >
  636. <div
  637. className="w-full max-w-xl max-h-[85vh] flex flex-col rounded-lg bg-bambu-dark-secondary border border-bambu-dark-tertiary/60"
  638. onClick={(e) => e.stopPropagation()}
  639. >
  640. {/* Header */}
  641. <div className="flex-shrink-0 flex items-start justify-between gap-3 px-4 pt-4 pb-3 border-b border-bambu-dark-tertiary/40">
  642. <div className="min-w-0">
  643. <h3 className="text-white font-medium flex items-center gap-2">
  644. <Cog className="w-4 h-4" />
  645. {t('slice.title')}
  646. </h3>
  647. <p className="text-xs text-bambu-gray mt-1 truncate" title={source.filename}>
  648. {source.filename}
  649. {selectedPlate != null
  650. ? ` • ${t('archives.platePicker.plateLabel', { index: selectedPlate })}`
  651. : ''}
  652. </p>
  653. </div>
  654. <button
  655. onClick={onClose}
  656. disabled={isEnqueuing}
  657. className="flex-shrink-0 text-bambu-gray hover:text-white transition-colors disabled:opacity-50"
  658. aria-label={t('common.close')}
  659. >
  660. <X className="w-5 h-5" />
  661. </button>
  662. </div>
  663. {/* Body */}
  664. <div className="flex-1 overflow-y-auto p-4 space-y-4">
  665. {/* Preset listing loader — printer/process dropdowns can't render
  666. without it. Plate query reuses the same spinner since it's
  667. also blocking. */}
  668. {(platesQuery.isLoading || presetsQuery.isLoading) && (
  669. <div className="flex items-center gap-2 text-bambu-gray text-sm">
  670. <Loader2 className="w-4 h-4 animate-spin" />
  671. {t('slice.loadingPresets')}
  672. </div>
  673. )}
  674. {presetsQuery.isError && (
  675. <div className="text-sm text-red-400" role="alert">
  676. {t(
  677. 'slice.presetsLoadFailed',
  678. 'Failed to load presets. Open Settings → Profiles to import them, or sign in to Bambu Cloud.',
  679. )}
  680. </div>
  681. )}
  682. {presetsQuery.data && (
  683. <>
  684. <CloudStatusBanner status={presetsQuery.data.cloud_status} />
  685. {/* Bundle picker — only renders when at least one .bbscfg has
  686. been imported via Settings → Slicer Bundles. Lets the user
  687. trade the cloud/local/standard tier for a single curated
  688. triplet from a previously-uploaded BambuStudio bundle. */}
  689. {bundlesQuery.data && bundlesQuery.data.length > 0 && (
  690. <BundlePicker
  691. bundles={bundlesQuery.data}
  692. selectedId={selectedBundleId}
  693. onChange={setSelectedBundleId}
  694. disabled={isEnqueuing}
  695. />
  696. )}
  697. {/* Preset triplet — hidden when a bundle is selected so the
  698. user only sees one tier at a time. The bundle's process +
  699. filament dropdowns render below in their stead. */}
  700. {!isBundleMode && (
  701. <>
  702. <PresetDropdown
  703. label={t('slice.printer')}
  704. slot="printer"
  705. data={presetsQuery.data}
  706. value={printerPreset}
  707. onChange={setPrinterPreset}
  708. disabled={isEnqueuing}
  709. />
  710. <PresetDropdown
  711. label={t('slice.process')}
  712. slot="process"
  713. data={presetsQuery.data}
  714. value={processPreset}
  715. onChange={setProcessPreset}
  716. disabled={isEnqueuing}
  717. selectedPrinterName={selectedPrinterName}
  718. compatIndex={compatIndex}
  719. />
  720. </>
  721. )}
  722. {isBundleMode && selectedBundle && (
  723. <>
  724. {/* Bundle's printer is implicit (each .bbscfg has exactly
  725. one). Show it as a read-only label so the user can
  726. verify the printer they're slicing for. */}
  727. <div>
  728. <label className="block text-sm text-bambu-gray mb-1">
  729. {t('slice.printer')}
  730. </label>
  731. <div className="px-3 py-2 rounded-md bg-bambu-dark/40 border border-bambu-dark-tertiary text-white text-sm">
  732. {selectedBundle.printer_preset_name}
  733. </div>
  734. </div>
  735. <BundleStringDropdown
  736. label={t('slice.process')}
  737. options={selectedBundle.process}
  738. value={bundleProcessName}
  739. onChange={setBundleProcessName}
  740. disabled={isEnqueuing}
  741. />
  742. </>
  743. )}
  744. {/* Bed-type override (#1337). Always visible, always enabled.
  745. In non-bundle mode the backend patches curr_bed_type on the
  746. resolved process JSON before forwarding to the sidecar; in
  747. bundle mode the same value rides through as a sidecar form
  748. field so the bundle's materialised process JSON gets the
  749. override applied there too. */}
  750. <BedTypeDropdown
  751. value={bedType}
  752. onChange={setBedType}
  753. disabled={isEnqueuing}
  754. />
  755. {/* Filament reqs may need a server-side preview-slice for
  756. unsliced project files (single-pass, then cached). Show a
  757. scoped spinner so the user sees the printer/process
  758. dropdowns instead of an opaque "Loading presets…" wait. */}
  759. {filamentReqsQuery.isLoading ? (
  760. <FilamentAnalysisSpinner
  761. requestId={previewRequestId}
  762. sourceName={source.filename}
  763. />
  764. ) : isBundleMode && selectedBundle ? (
  765. filamentSlots.map((slot, idx) => {
  766. const isUsed = slot.used_in_plate !== false;
  767. const baseLabel =
  768. filamentSlots.length > 1
  769. ? t('slice.filamentSlot', {
  770. index: idx + 1,
  771. type: slot.type,
  772. })
  773. : t('slice.filament');
  774. const label = isUsed
  775. ? baseLabel
  776. : `${baseLabel} ${t('slice.notUsedByPlate')}`;
  777. return (
  778. <BundleStringDropdown
  779. key={`bundle-filament-${idx}`}
  780. label={label}
  781. options={selectedBundle.filament}
  782. value={bundleFilamentNames[idx] ?? null}
  783. onChange={(name) =>
  784. setBundleFilamentNames((current) => {
  785. const next = current.length === filamentSlots.length
  786. ? [...current]
  787. : filamentSlots.map((_, i) => current[i] ?? null);
  788. next[idx] = name;
  789. return next;
  790. })
  791. }
  792. disabled={isEnqueuing || !isUsed}
  793. swatchColor={filamentSlots.length > 1 ? slot.color : undefined}
  794. />
  795. );
  796. })
  797. ) : (
  798. filamentSlots.map((slot, idx) => {
  799. // Slots flagged by the backend as not used by the
  800. // picked plate are auto-picked from project metadata
  801. // and disabled — the slicer CLI still needs a
  802. // profile per project slot, but the user shouldn't
  803. // have to think about slots their plate doesn't
  804. // paint with. used_in_plate defaults to true when
  805. // missing (sliced 3MFs and the no-flag legacy path).
  806. const isUsed = slot.used_in_plate !== false;
  807. const baseLabel =
  808. filamentSlots.length > 1
  809. ? t('slice.filamentSlot', {
  810. index: idx + 1,
  811. type: slot.type,
  812. })
  813. : t('slice.filament');
  814. const label = isUsed
  815. ? baseLabel
  816. : `${baseLabel} ${t('slice.notUsedByPlate')}`;
  817. return (
  818. <PresetDropdown
  819. key={`filament-${idx}`}
  820. label={label}
  821. slot="filament"
  822. data={presetsQuery.data}
  823. value={filamentPresets[idx] ?? null}
  824. onChange={(ref) =>
  825. setFilamentPresets((current) => {
  826. const next = current.length === filamentSlots.length
  827. ? [...current]
  828. : filamentSlots.map((_, i) => current[i] ?? null);
  829. next[idx] = ref;
  830. return next;
  831. })
  832. }
  833. disabled={isEnqueuing || !isUsed}
  834. swatchColor={filamentSlots.length > 1 ? slot.color : undefined}
  835. selectedPrinterName={selectedPrinterName}
  836. compatIndex={compatIndex}
  837. />
  838. );
  839. })
  840. )}
  841. </>
  842. )}
  843. {errorMessage && (
  844. <div className="text-sm text-red-400 bg-red-900/20 border border-red-900/40 rounded p-2" role="alert">
  845. {errorMessage}
  846. </div>
  847. )}
  848. </div>
  849. {/* Footer */}
  850. <div className="flex-shrink-0 flex justify-end gap-2 px-4 py-3 border-t border-bambu-dark-tertiary/40">
  851. <button
  852. type="button"
  853. onClick={onClose}
  854. disabled={isEnqueuing}
  855. className="px-3 py-1.5 text-sm rounded-md border border-bambu-dark-tertiary text-bambu-gray hover:text-white hover:border-bambu-gray transition-colors disabled:opacity-50"
  856. >
  857. {t('common.cancel')}
  858. </button>
  859. {canSliceAll && (
  860. <label
  861. className="flex items-center gap-2 mr-auto text-sm text-bambu-gray cursor-pointer select-none"
  862. title={t('slice.actionAllTitle', { count: totalPlateCount })}
  863. >
  864. <input
  865. type="checkbox"
  866. checked={sliceAllPlates}
  867. onChange={(e) => setSliceAllPlates(e.target.checked)}
  868. disabled={isEnqueuing}
  869. className="cursor-pointer"
  870. />
  871. {t('slice.allPlatesToggle', { count: totalPlateCount })}
  872. </label>
  873. )}
  874. <button
  875. type="button"
  876. onClick={() => {
  877. setErrorMessage(null);
  878. // ``plate=0`` is the sidecar's "all plates" sentinel — passes
  879. // ``--slice 0`` to the BS CLI which produces a single 3MF
  880. // with one ``Metadata/plate_N.gcode`` entry per plate.
  881. const platePayload = sliceAllPlates ? 0 : selectedPlate;
  882. enqueueMutation.mutate(platePayload);
  883. }}
  884. disabled={!isReady || isEnqueuing}
  885. className="px-3 py-1.5 text-sm rounded-md bg-bambu-green hover:bg-bambu-green/90 text-bambu-dark font-medium transition-colors disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-2"
  886. >
  887. {isEnqueuing ? (
  888. <>
  889. <Loader2 className="w-4 h-4 animate-spin" />
  890. {t('slice.enqueuing')}
  891. </>
  892. ) : sliceAllPlates ? (
  893. t('slice.actionAll', { count: totalPlateCount })
  894. ) : (
  895. t('slice.action')
  896. )}
  897. </button>
  898. </div>
  899. </div>
  900. </div>
  901. );
  902. }
  903. function CloudStatusBanner({ status }: { status: SlicerCloudStatus }) {
  904. const { t } = useTranslation();
  905. if (status === 'ok') return null;
  906. // Map each non-ok status to the appropriate icon + tone. None of these are
  907. // hard errors — the user can still slice using local + standard presets,
  908. // so we use info / warn styling rather than error red.
  909. const config: Record<Exclude<SlicerCloudStatus, 'ok'>, { tone: string; icon: typeof Cloud; key: string; fallback: string }> = {
  910. not_authenticated: {
  911. tone: 'border-bambu-dark-tertiary/40 bg-bambu-dark text-bambu-gray',
  912. icon: Cloud,
  913. key: 'slice.cloud.notAuthenticated',
  914. fallback: 'Sign in to Bambu Cloud (Settings → Profiles → Cloud) to see your cloud presets.',
  915. },
  916. expired: {
  917. tone: 'border-amber-700/40 bg-amber-900/20 text-amber-200',
  918. icon: CloudOff,
  919. key: 'slice.cloud.expired',
  920. fallback: 'Bambu Cloud session expired — sign in again to refresh your cloud presets.',
  921. },
  922. unreachable: {
  923. tone: 'border-bambu-dark-tertiary/40 bg-bambu-dark text-bambu-gray',
  924. icon: CloudOff,
  925. key: 'slice.cloud.unreachable',
  926. fallback: 'Bambu Cloud is unreachable right now. Local and standard presets still work.',
  927. },
  928. };
  929. const { tone, icon: Icon, key, fallback } = config[status];
  930. return (
  931. <div className={`flex items-start gap-2 text-xs rounded-md border p-2 ${tone}`} role="status">
  932. <Icon className="w-4 h-4 flex-shrink-0 mt-0.5" />
  933. <span>{t(key, fallback)}</span>
  934. </div>
  935. );
  936. }
  937. // Build-plate options offered in the SliceModal (#1337). Values are the
  938. // canonical strings the slicer's StaticPrintConfig validator accepts as
  939. // `curr_bed_type` — BambuStudio is the default sidecar, so this matches its
  940. // enum; OrcaSlicer accepts the same set with a Supertack alias that users
  941. // can target via the same dropdown if they re-import their presets.
  942. const BED_TYPE_OPTIONS: { value: string; labelKey: string; fallback: string }[] = [
  943. { value: 'Cool Plate', labelKey: 'slice.bedType.coolPlate', fallback: 'Cool Plate' },
  944. {
  945. value: 'Cool Plate (SuperTack)',
  946. labelKey: 'slice.bedType.coolPlateSuperTack',
  947. fallback: 'Cool Plate SuperTack',
  948. },
  949. { value: 'Engineering Plate', labelKey: 'slice.bedType.engineering', fallback: 'Engineering Plate' },
  950. { value: 'High Temp Plate', labelKey: 'slice.bedType.highTemp', fallback: 'High Temp Plate' },
  951. { value: 'Textured PEI Plate', labelKey: 'slice.bedType.texturedPEI', fallback: 'Textured PEI Plate' },
  952. { value: 'Smooth PEI Plate', labelKey: 'slice.bedType.smoothPEI', fallback: 'Smooth PEI Plate' },
  953. ];
  954. function BedTypeDropdown({
  955. value,
  956. onChange,
  957. disabled,
  958. }: {
  959. value: string | null;
  960. onChange: (value: string | null) => void;
  961. disabled?: boolean;
  962. }) {
  963. const { t } = useTranslation();
  964. return (
  965. <label className="block">
  966. <span className="block text-xs text-bambu-gray mb-1">
  967. {t('slice.bedType.label')}
  968. </span>
  969. <select
  970. value={value ?? ''}
  971. onChange={(e) => onChange(e.target.value === '' ? null : e.target.value)}
  972. disabled={disabled}
  973. className="w-full px-3 py-2 rounded-md bg-bambu-dark border border-bambu-dark-tertiary text-white text-sm focus:outline-none focus:border-bambu-gray disabled:opacity-50"
  974. >
  975. <option value="">{t('slice.bedType.auto')}</option>
  976. {BED_TYPE_OPTIONS.map((opt) => (
  977. <option key={opt.value} value={opt.value}>
  978. {t(opt.labelKey, opt.fallback)}
  979. </option>
  980. ))}
  981. </select>
  982. </label>
  983. );
  984. }
  985. interface PresetDropdownProps {
  986. label: string;
  987. slot: Slot;
  988. data: UnifiedPresetsResponse;
  989. value: PresetRef | null;
  990. onChange: (ref: PresetRef | null) => void;
  991. disabled?: boolean;
  992. // Optional colour swatch shown next to the label — used for multi-color
  993. // filament slots so the user can see at a glance which slot they're
  994. // configuring against the source 3MF's per-slot colour.
  995. swatchColor?: string;
  996. // Selected printer context (#1325). When provided for a process / filament
  997. // slot, presets that resolve to a different printer (per the uploaded
  998. // Slicer Bundles in compatIndex) move into a trailing "Other printers"
  999. // group instead of the main tier list.
  1000. selectedPrinterName?: string | null;
  1001. compatIndex?: PrinterCompatibilityIndex;
  1002. }
  1003. function PresetDropdown({
  1004. label,
  1005. slot,
  1006. data,
  1007. value,
  1008. onChange,
  1009. disabled,
  1010. swatchColor,
  1011. selectedPrinterName,
  1012. compatIndex,
  1013. }: PresetDropdownProps) {
  1014. const { t } = useTranslation();
  1015. // Tier sections (imported → cloud → standard), plus — for a process /
  1016. // filament slot with a selected printer — a trailing group of presets that
  1017. // resolve to a different printer (#1325). Compatibility-unknown presets
  1018. // stay in their tier, so a custom / untagged preset is never hidden, and
  1019. // empty sections collapse out.
  1020. const { sections, otherEntries } = useMemo(() => {
  1021. const tiers: { key: keyof UnifiedPresetsResponse; label: string; fallback: string }[] = [
  1022. { key: 'local', label: 'slice.tier.local', fallback: 'Imported' },
  1023. { key: 'cloud', label: 'slice.tier.cloud', fallback: 'Cloud' },
  1024. { key: 'standard', label: 'slice.tier.standard', fallback: 'Standard' },
  1025. ];
  1026. const filterByPrinter = slot !== 'printer';
  1027. const compatSections: { tierLabel: string; entries: UnifiedPreset[] }[] = [];
  1028. const other: UnifiedPreset[] = [];
  1029. for (const { key, label: lk, fallback } of tiers) {
  1030. const entries = (data[key] as UnifiedPresetsBySlot)[slot];
  1031. if (!filterByPrinter) {
  1032. if (entries.length > 0) compatSections.push({ tierLabel: t(lk, fallback), entries });
  1033. continue;
  1034. }
  1035. const compatible: UnifiedPreset[] = [];
  1036. for (const p of entries) {
  1037. if (
  1038. presetCompatibility(
  1039. p,
  1040. // filterByPrinter is true here, so slot is never 'printer'.
  1041. slot as 'process' | 'filament',
  1042. selectedPrinterName ?? null,
  1043. compatIndex ?? EMPTY_COMPATIBILITY_INDEX,
  1044. ) === 'mismatch'
  1045. ) {
  1046. other.push(p);
  1047. } else {
  1048. compatible.push(p);
  1049. }
  1050. }
  1051. if (compatible.length > 0) {
  1052. compatSections.push({ tierLabel: t(lk, fallback), entries: compatible });
  1053. }
  1054. }
  1055. return { sections: compatSections, otherEntries: other };
  1056. }, [data, slot, t, selectedPrinterName, compatIndex]);
  1057. const totalEntries =
  1058. sections.reduce((sum, s) => sum + s.entries.length, 0) + otherEntries.length;
  1059. return (
  1060. <label className="block">
  1061. <span className="flex items-center gap-2 text-xs text-bambu-gray mb-1">
  1062. {swatchColor && (
  1063. <span
  1064. className="inline-block w-3 h-3 rounded-full border border-bambu-dark-tertiary"
  1065. style={{ backgroundColor: swatchColor || 'transparent' }}
  1066. aria-hidden
  1067. />
  1068. )}
  1069. <span>{label}</span>
  1070. </span>
  1071. <select
  1072. value={toRefValue(value)}
  1073. onChange={(e) => onChange(fromRefValue(e.target.value))}
  1074. disabled={disabled || totalEntries === 0}
  1075. className="w-full px-3 py-2 rounded-md bg-bambu-dark border border-bambu-dark-tertiary text-white text-sm focus:outline-none focus:border-bambu-gray disabled:opacity-50"
  1076. >
  1077. <option value="">
  1078. {totalEntries === 0
  1079. ? t('slice.noPresetsForSlot')
  1080. : t('slice.selectPreset')}
  1081. </option>
  1082. {sections.map((section) => (
  1083. <optgroup key={section.tierLabel} label={section.tierLabel}>
  1084. {section.entries.map((p) => (
  1085. <option key={`${p.source}:${p.id}`} value={`${p.source}:${p.id}`}>
  1086. {p.name}
  1087. </option>
  1088. ))}
  1089. </optgroup>
  1090. ))}
  1091. {otherEntries.length > 0 && (
  1092. <optgroup label={t('slice.otherPrinters')}>
  1093. {otherEntries.map((p) => (
  1094. <option key={`${p.source}:${p.id}`} value={`${p.source}:${p.id}`}>
  1095. {p.name}
  1096. </option>
  1097. ))}
  1098. </optgroup>
  1099. )}
  1100. </select>
  1101. </label>
  1102. );
  1103. }
  1104. // Top-of-modal bundle picker. The "None" option leaves the user on the
  1105. // cloud/local/standard tier path; selecting a bundle id flips the modal
  1106. // into bundle dispatch mode (see SliceModal state above).
  1107. interface BundlePickerProps {
  1108. bundles: SlicerBundle[];
  1109. selectedId: string | null;
  1110. onChange: (id: string | null) => void;
  1111. disabled?: boolean;
  1112. }
  1113. function BundlePicker({ bundles, selectedId, onChange, disabled }: BundlePickerProps) {
  1114. const { t } = useTranslation();
  1115. return (
  1116. <label className="block">
  1117. <span className="block text-sm text-bambu-gray mb-1 inline-flex items-center gap-1.5">
  1118. <Package className="w-3.5 h-3.5" />
  1119. {t('slice.bundle')}
  1120. </span>
  1121. <select
  1122. value={selectedId ?? ''}
  1123. onChange={(e) => onChange(e.target.value || null)}
  1124. disabled={disabled}
  1125. className="w-full px-3 py-2 rounded-md bg-bambu-dark border border-bambu-dark-tertiary text-white text-sm focus:outline-none focus:border-bambu-gray disabled:opacity-50"
  1126. >
  1127. <option value="">
  1128. {t('slice.bundleNone')}
  1129. </option>
  1130. {bundles.map((b) => (
  1131. <option key={b.id} value={b.id}>
  1132. {b.printer_preset_name}
  1133. </option>
  1134. ))}
  1135. </select>
  1136. </label>
  1137. );
  1138. }
  1139. // Plain-string dropdown used for bundle-mode process / filament selectors.
  1140. // Bundles store presets as a flat list of names within their printer-tied
  1141. // directory, so a `<select>` of strings is enough — no source tier, no
  1142. // optgroups. Same swatch / disabled affordances as the cloud/local/standard
  1143. // PresetDropdown above so the visual rhythm of the form stays consistent.
  1144. interface BundleStringDropdownProps {
  1145. label: string;
  1146. options: string[];
  1147. value: string | null;
  1148. onChange: (next: string | null) => void;
  1149. disabled?: boolean;
  1150. swatchColor?: string;
  1151. }
  1152. function BundleStringDropdown({
  1153. label,
  1154. options,
  1155. value,
  1156. onChange,
  1157. disabled,
  1158. swatchColor,
  1159. }: BundleStringDropdownProps) {
  1160. const { t } = useTranslation();
  1161. return (
  1162. <label className="block">
  1163. <span className="block text-sm text-bambu-gray mb-1 inline-flex items-center gap-1.5">
  1164. {swatchColor && (
  1165. <span
  1166. className="inline-block w-3 h-3 rounded-sm border border-black/20"
  1167. style={{ backgroundColor: swatchColor || 'transparent' }}
  1168. aria-hidden
  1169. />
  1170. )}
  1171. <span>{label}</span>
  1172. </span>
  1173. <select
  1174. value={value ?? ''}
  1175. onChange={(e) => onChange(e.target.value || null)}
  1176. disabled={disabled || options.length === 0}
  1177. className="w-full px-3 py-2 rounded-md bg-bambu-dark border border-bambu-dark-tertiary text-white text-sm focus:outline-none focus:border-bambu-gray disabled:opacity-50"
  1178. >
  1179. <option value="">
  1180. {options.length === 0
  1181. ? t('slice.noPresetsForSlot')
  1182. : t('slice.selectPreset')}
  1183. </option>
  1184. {options.map((name) => (
  1185. <option key={name} value={name}>
  1186. {name}
  1187. </option>
  1188. ))}
  1189. </select>
  1190. </label>
  1191. );
  1192. }