ConfigureAmsSlotModal.tsx 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. import { useState, useMemo, useEffect, useCallback } from 'react';
  2. import { useQuery, useMutation } from '@tanstack/react-query';
  3. import { useTranslation } from 'react-i18next';
  4. import { X, Loader2, Settings2, ChevronDown, CheckCircle2, RotateCcw } from 'lucide-react';
  5. import { api } from '../api/client';
  6. import type { KProfile } from '../api/client';
  7. import { Button } from './Button';
  8. interface SlotInfo {
  9. amsId: number;
  10. trayId: number;
  11. trayCount: number;
  12. trayType?: string;
  13. trayColor?: string;
  14. traySubBrands?: string;
  15. trayInfoIdx?: string;
  16. extruderId?: number;
  17. caliIdx?: number | null;
  18. savedPresetId?: string;
  19. }
  20. // Get proper AMS label (handles HT AMS with ID 128+)
  21. function getAmsLabel(amsId: number, trayCount: number): string {
  22. // External spool
  23. if (amsId === 255) return 'External';
  24. let normalizedId: number;
  25. let isHt = false;
  26. if (amsId >= 128 && amsId <= 135) {
  27. // HT AMS range: 128-135 → A-H
  28. normalizedId = amsId - 128;
  29. isHt = true;
  30. } else if (amsId >= 0 && amsId <= 3) {
  31. // Regular AMS range: 0-3 → A-D
  32. normalizedId = amsId;
  33. // Check tray count as secondary indicator
  34. isHt = trayCount === 1;
  35. } else {
  36. // Unknown range - fallback to A
  37. normalizedId = 0;
  38. }
  39. // Cap to valid letter range (A-H)
  40. normalizedId = Math.max(0, Math.min(normalizedId, 7));
  41. const letter = String.fromCharCode(65 + normalizedId);
  42. return isHt ? `HT-${letter}` : `AMS-${letter}`;
  43. }
  44. // Convert setting_id to tray_info_idx (filament_id format)
  45. // Bambu format: setting_id "GFSL05" → tray_info_idx "GFL05"
  46. function convertToTrayInfoIdx(settingId: string): string {
  47. // Strip version suffix if present (e.g., GFSL05_07 -> GFSL05)
  48. const baseId = settingId.includes('_') ? settingId.split('_')[0] : settingId;
  49. // Bambu presets start with "GFS" - remove the 'S' to get filament_id
  50. if (baseId.startsWith('GFS')) {
  51. return 'GF' + baseId.slice(3);
  52. }
  53. // User presets (PFUS*, PFSP*) - use the base setting_id (without version suffix)
  54. // This follows the pattern that filament_id and setting_id share the same base ID
  55. if (baseId.startsWith('PFUS') || baseId.startsWith('PFSP')) {
  56. return baseId; // Use base ID without version suffix
  57. }
  58. // For other formats, use as-is
  59. return baseId;
  60. }
  61. interface ConfigureAmsSlotModalProps {
  62. isOpen: boolean;
  63. onClose: () => void;
  64. printerId: number;
  65. slotInfo: SlotInfo;
  66. nozzleDiameter?: string;
  67. printerModel?: string;
  68. onSuccess?: () => void;
  69. }
  70. // Known filament material types
  71. const MATERIAL_TYPES = ['PLA', 'PETG', 'PCTG', 'ABS', 'ASA', 'TPU', 'PC', 'PA', 'NYLON', 'PVA', 'HIPS', 'PP', 'PET'];
  72. // Extract filament type from preset name by finding known material type
  73. function parsePresetName(name: string): { material: string; brand: string; variant: string } {
  74. // Remove printer/nozzle suffix first
  75. const withoutSuffix = name.replace(/@.+$/, '').trim();
  76. // Try to find a known material type in the name
  77. const upperName = withoutSuffix.toUpperCase();
  78. for (const mat of MATERIAL_TYPES) {
  79. // Use word boundary to match whole words only
  80. const regex = new RegExp(`\\b${mat}\\b`, 'i');
  81. if (regex.test(upperName)) {
  82. // Found material, extract brand (everything before material) and variant (after)
  83. const parts = withoutSuffix.split(regex);
  84. const brand = parts[0]?.trim() || '';
  85. const variant = parts[1]?.trim() || '';
  86. return { material: mat, brand, variant };
  87. }
  88. }
  89. // Fallback: assume first word is brand, second is material
  90. const parts = withoutSuffix.split(/\s+/);
  91. if (parts.length >= 2) {
  92. return { material: parts[1], brand: parts[0], variant: parts.slice(2).join(' ') };
  93. }
  94. return { material: withoutSuffix, brand: '', variant: '' };
  95. }
  96. // Check if a preset is a user preset (not built-in)
  97. function isUserPreset(settingId: string): boolean {
  98. // Built-in presets have specific patterns, user presets are UUIDs
  99. return !settingId.startsWith('GF') && !settingId.startsWith('P1');
  100. }
  101. // Common color name to hex mapping
  102. const COLOR_NAME_MAP: Record<string, string> = {
  103. // Basic colors
  104. 'white': 'FFFFFF',
  105. 'black': '000000',
  106. 'red': 'FF0000',
  107. 'green': '00FF00',
  108. 'blue': '0000FF',
  109. 'yellow': 'FFFF00',
  110. 'cyan': '00FFFF',
  111. 'magenta': 'FF00FF',
  112. 'orange': 'FFA500',
  113. 'purple': '800080',
  114. 'pink': 'FFC0CB',
  115. 'brown': '8B4513',
  116. 'gray': '808080',
  117. 'grey': '808080',
  118. // Filament-specific colors
  119. 'jade white': 'FFFEF2',
  120. 'ivory': 'FFFFF0',
  121. 'beige': 'F5F5DC',
  122. 'cream': 'FFFDD0',
  123. 'silver': 'C0C0C0',
  124. 'gold': 'FFD700',
  125. 'bronze': 'CD7F32',
  126. 'copper': 'B87333',
  127. 'navy': '000080',
  128. 'teal': '008080',
  129. 'olive': '808000',
  130. 'maroon': '800000',
  131. 'coral': 'FF7F50',
  132. 'salmon': 'FA8072',
  133. 'lime': '32CD32',
  134. 'mint': '98FF98',
  135. 'forest green': '228B22',
  136. 'sky blue': '87CEEB',
  137. 'royal blue': '4169E1',
  138. 'turquoise': '40E0D0',
  139. 'lavender': 'E6E6FA',
  140. 'violet': 'EE82EE',
  141. 'plum': 'DDA0DD',
  142. 'tan': 'D2B48C',
  143. 'chocolate': 'D2691E',
  144. 'charcoal': '36454F',
  145. 'slate': '708090',
  146. 'transparent': '000000', // Will need special handling
  147. 'natural': 'F5F5DC',
  148. 'wood': 'DEB887',
  149. };
  150. // Quick-select color presets (common filament colors)
  151. // Basic colors shown by default
  152. const QUICK_COLORS_BASIC = [
  153. { name: 'White', hex: 'FFFFFF' },
  154. { name: 'Black', hex: '000000' },
  155. { name: 'Red', hex: 'FF0000' },
  156. { name: 'Blue', hex: '0000FF' },
  157. { name: 'Green', hex: '00AA00' },
  158. { name: 'Yellow', hex: 'FFFF00' },
  159. { name: 'Orange', hex: 'FFA500' },
  160. { name: 'Gray', hex: '808080' },
  161. ];
  162. // Extended colors shown when expanded
  163. const QUICK_COLORS_EXTENDED = [
  164. { name: 'Cyan', hex: '00FFFF' },
  165. { name: 'Magenta', hex: 'FF00FF' },
  166. { name: 'Purple', hex: '800080' },
  167. { name: 'Pink', hex: 'FFC0CB' },
  168. { name: 'Brown', hex: '8B4513' },
  169. { name: 'Beige', hex: 'F5F5DC' },
  170. { name: 'Navy', hex: '000080' },
  171. { name: 'Teal', hex: '008080' },
  172. { name: 'Lime', hex: '32CD32' },
  173. { name: 'Gold', hex: 'FFD700' },
  174. { name: 'Silver', hex: 'C0C0C0' },
  175. { name: 'Maroon', hex: '800000' },
  176. { name: 'Olive', hex: '808000' },
  177. { name: 'Coral', hex: 'FF7F50' },
  178. { name: 'Salmon', hex: 'FA8072' },
  179. { name: 'Turquoise', hex: '40E0D0' },
  180. { name: 'Violet', hex: 'EE82EE' },
  181. { name: 'Indigo', hex: '4B0082' },
  182. { name: 'Chocolate', hex: 'D2691E' },
  183. { name: 'Tan', hex: 'D2B48C' },
  184. { name: 'Slate', hex: '708090' },
  185. { name: 'Charcoal', hex: '36454F' },
  186. { name: 'Ivory', hex: 'FFFFF0' },
  187. { name: 'Cream', hex: 'FFFDD0' },
  188. ];
  189. // Try to convert color name to hex
  190. function colorNameToHex(name: string): string | null {
  191. const normalized = name.toLowerCase().trim();
  192. return COLOR_NAME_MAP[normalized] || null;
  193. }
  194. // Extract printer model from preset name suffix "@BBL X1C 0.4 nozzle" → "X1C"
  195. function extractPresetModel(name: string): string | null {
  196. const atIdx = name.indexOf('@');
  197. if (atIdx < 0) return null;
  198. const suffix = name.slice(atIdx + 1).trim();
  199. const bblMatch = suffix.match(/^BBL\s+(.+?)(?:\s+[\d.]+\s*nozzle)?$/i);
  200. if (bblMatch) return bblMatch[1].trim();
  201. return null;
  202. }
  203. export function ConfigureAmsSlotModal({
  204. isOpen,
  205. onClose,
  206. printerId,
  207. slotInfo,
  208. nozzleDiameter = '0.4',
  209. printerModel,
  210. onSuccess,
  211. }: ConfigureAmsSlotModalProps) {
  212. const { t } = useTranslation();
  213. const [selectedPresetId, setSelectedPresetId] = useState<string>('');
  214. const [selectedKProfile, setSelectedKProfile] = useState<KProfile | null>(null);
  215. const [colorHex, setColorHex] = useState<string>(''); // Just the 6-char hex, no alpha
  216. const [colorInput, setColorInput] = useState<string>(''); // User's text input (name or hex)
  217. const [searchQuery, setSearchQuery] = useState('');
  218. const [showSuccess, setShowSuccess] = useState(false);
  219. const [showExtendedColors, setShowExtendedColors] = useState(false);
  220. // Fetch cloud settings (gracefully handle 401 when logged out)
  221. const { data: cloudSettings, isLoading: settingsLoading, isError: cloudError } = useQuery({
  222. queryKey: ['cloudSettings'],
  223. queryFn: () => api.getCloudSettings(),
  224. enabled: isOpen,
  225. retry: false,
  226. });
  227. // Fetch local presets
  228. const { data: localPresets, isLoading: localLoading } = useQuery({
  229. queryKey: ['localPresets'],
  230. queryFn: () => api.getLocalPresets(),
  231. enabled: isOpen,
  232. });
  233. // Fetch built-in filament names (static fallback)
  234. const { data: builtinFilaments, isLoading: builtinLoading } = useQuery({
  235. queryKey: ['builtinFilaments'],
  236. queryFn: () => api.getBuiltinFilaments(),
  237. enabled: isOpen,
  238. staleTime: Infinity,
  239. });
  240. // Fetch K profiles
  241. const { data: kprofilesData, isLoading: kprofilesLoading } = useQuery({
  242. queryKey: ['kprofiles', printerId, nozzleDiameter],
  243. queryFn: () => api.getKProfiles(printerId, nozzleDiameter),
  244. enabled: isOpen && !!printerId,
  245. });
  246. // Fetch color catalog
  247. const { data: colorCatalog } = useQuery({
  248. queryKey: ['colorCatalog'],
  249. queryFn: () => api.getColorCatalog(),
  250. enabled: isOpen,
  251. staleTime: Infinity,
  252. });
  253. // Configure slot mutation
  254. const configureMutation = useMutation({
  255. mutationFn: async () => {
  256. if (!selectedPresetId) throw new Error('No filament preset selected');
  257. // Determine preset source
  258. const isLocal = selectedPresetId.startsWith('local_');
  259. const isBuiltin = selectedPresetId.startsWith('builtin_');
  260. const localId = isLocal ? parseInt(selectedPresetId.replace('local_', ''), 10) : null;
  261. const builtinFilamentId = isBuiltin ? selectedPresetId.replace('builtin_', '') : null;
  262. const localPreset = isLocal
  263. ? localPresets?.filament.find(p => p.id === localId)
  264. : null;
  265. const builtinPreset = isBuiltin
  266. ? builtinFilaments?.find(b => b.filament_id === builtinFilamentId)
  267. : null;
  268. // Get the selected cloud preset details (null for local/builtin presets)
  269. const selectedPreset = (!isLocal && !isBuiltin)
  270. ? cloudSettings?.filament.find(p => p.setting_id === selectedPresetId)
  271. : null;
  272. if (!isLocal && !isBuiltin && !selectedPreset) throw new Error('Selected preset not found');
  273. if (isLocal && !localPreset) throw new Error('Selected local preset not found');
  274. if (isBuiltin && !builtinPreset) throw new Error('Selected builtin preset not found');
  275. // Parse the preset name for filament info
  276. const presetName = isLocal ? localPreset!.name : isBuiltin ? builtinPreset!.name : selectedPreset!.name;
  277. const parsed = parsePresetName(presetName);
  278. // Get cali_idx from selected K profile's slot_id (-1 = use default 0.020)
  279. const caliIdx = selectedKProfile?.slot_id ?? -1;
  280. // Use custom color if set, otherwise use current slot color or default
  281. const color = colorHex || slotInfo.trayColor?.slice(0, 6) || 'FFFFFF';
  282. // Create the tray_sub_brands from preset name (without printer/nozzle suffix)
  283. const traySubBrands = presetName.replace(/@.+$/, '').trim();
  284. let trayInfoIdx: string;
  285. let settingId: string;
  286. if (isLocal) {
  287. // Local presets have no Bambu Cloud setting_id, but need a valid
  288. // tray_info_idx for the printer to recognize the filament type.
  289. // Map the material type to the closest generic Bambu filament ID.
  290. const material = (localPreset?.filament_type || parsed.material || '').toUpperCase();
  291. const GENERIC_IDS: Record<string, string> = {
  292. 'PLA': 'GFL99', 'PLA-CF': 'GFL98', 'PLA SILK': 'GFL96', 'PLA HIGH SPEED': 'GFL95',
  293. 'PETG': 'GFG99', 'PETG HF': 'GFG96', 'PETG-CF': 'GFG98', 'PCTG': 'GFG97',
  294. 'ABS': 'GFB99', 'ASA': 'GFB98',
  295. 'PC': 'GFC99',
  296. 'PA': 'GFN99', 'PA-CF': 'GFN98', 'NYLON': 'GFN99',
  297. 'TPU': 'GFU99',
  298. 'PVA': 'GFS99', 'HIPS': 'GFS98',
  299. 'PE': 'GFP99', 'PP': 'GFP97',
  300. };
  301. // Try exact match first, then base material (strip suffixes like "-CF", "+", " HF")
  302. trayInfoIdx = GENERIC_IDS[material]
  303. || GENERIC_IDS[material.replace(/[-\s]?CF$/, '')]
  304. || GENERIC_IDS[material.replace(/\+$/, '')]
  305. || GENERIC_IDS[material.split(/[-\s]/)[0]]
  306. || '';
  307. settingId = '';
  308. } else if (isBuiltin) {
  309. // Built-in presets use the filament_id directly as tray_info_idx
  310. trayInfoIdx = builtinFilamentId!;
  311. settingId = '';
  312. } else {
  313. // Get tray_info_idx: for user presets, fetch detail to get filament_id or derive from base_id
  314. trayInfoIdx = convertToTrayInfoIdx(selectedPresetId);
  315. settingId = selectedPresetId;
  316. // For user presets (not starting with GF), fetch the detail to get the real filament_id
  317. if (!selectedPresetId.startsWith('GFS')) {
  318. try {
  319. const detail = await api.getCloudSettingDetail(selectedPresetId);
  320. if (detail.filament_id) {
  321. trayInfoIdx = detail.filament_id;
  322. } else if (detail.base_id) {
  323. trayInfoIdx = convertToTrayInfoIdx(detail.base_id);
  324. console.log(`Derived tray_info_idx from base_id: ${detail.base_id} -> ${trayInfoIdx}`);
  325. }
  326. } catch (e) {
  327. console.warn('Failed to fetch preset detail for filament_id:', e);
  328. }
  329. }
  330. }
  331. // Default temp range — use local preset core fields if available
  332. let tempMin = isLocal && localPreset?.nozzle_temp_min ? localPreset.nozzle_temp_min : 190;
  333. let tempMax = isLocal && localPreset?.nozzle_temp_max ? localPreset.nozzle_temp_max : 230;
  334. if (!isLocal || isBuiltin || (!localPreset?.nozzle_temp_min && !localPreset?.nozzle_temp_max)) {
  335. // Fall back to material-based defaults
  336. const material = (isLocal ? (localPreset?.filament_type || parsed.material) : parsed.material).toUpperCase();
  337. if (material.includes('PLA')) {
  338. tempMin = 190;
  339. tempMax = 230;
  340. } else if (material.includes('PETG')) {
  341. tempMin = 220;
  342. tempMax = 260;
  343. } else if (material.includes('ABS')) {
  344. tempMin = 240;
  345. tempMax = 280;
  346. } else if (material.includes('ASA')) {
  347. tempMin = 240;
  348. tempMax = 280;
  349. } else if (material.includes('TPU')) {
  350. tempMin = 200;
  351. tempMax = 240;
  352. } else if (material === 'PCTG') {
  353. tempMin = 220;
  354. tempMax = 260;
  355. } else if (material.includes('PC')) {
  356. tempMin = 260;
  357. tempMax = 300;
  358. } else if (material.includes('PA') || material.includes('NYLON')) {
  359. tempMin = 250;
  360. tempMax = 290;
  361. }
  362. }
  363. // Parse K value from selected profile
  364. const kValue = selectedKProfile?.k_value ? parseFloat(selectedKProfile.k_value) : 0;
  365. // Determine tray_type: use local preset's filament_type or parsed material
  366. const trayType = isLocal
  367. ? (localPreset?.filament_type || parsed.material || 'PLA')
  368. : (parsed.material || 'PLA');
  369. // Configure the slot via MQTT
  370. const result = await api.configureAmsSlot(printerId, slotInfo.amsId, slotInfo.trayId, {
  371. tray_info_idx: trayInfoIdx,
  372. tray_type: trayType,
  373. tray_sub_brands: traySubBrands,
  374. tray_color: color + 'FF', // Add alpha
  375. nozzle_temp_min: tempMin,
  376. nozzle_temp_max: tempMax,
  377. cali_idx: caliIdx,
  378. nozzle_diameter: nozzleDiameter,
  379. setting_id: settingId, // Full setting ID for slicer compatibility (empty for local)
  380. // Pass K profile's filament_id and setting_id for proper linking
  381. kprofile_filament_id: selectedKProfile?.filament_id,
  382. kprofile_setting_id: selectedKProfile?.setting_id || undefined,
  383. // Also pass the K value directly for extrusion_cali_set command
  384. k_value: kValue,
  385. });
  386. // Save the preset mapping so we can display the correct name in the UI
  387. // This is needed because user presets use filament_id (e.g., P285e239) as tray_info_idx,
  388. // which can't be resolved to a name via the filamentInfo API
  389. const mappingPresetId = isLocal ? `local_${localId}` : isBuiltin ? `builtin_${builtinFilamentId}` : selectedPresetId;
  390. const mappingSource = isLocal ? 'local' : isBuiltin ? 'builtin' : 'cloud';
  391. try {
  392. await api.saveSlotPreset(printerId, slotInfo.amsId, slotInfo.trayId, mappingPresetId, traySubBrands, mappingSource);
  393. } catch (e) {
  394. console.warn('Failed to save slot preset mapping:', e);
  395. // Don't fail the whole operation - slot was configured successfully
  396. }
  397. return result;
  398. },
  399. onSuccess: () => {
  400. setShowSuccess(true);
  401. onSuccess?.();
  402. // Close after showing success briefly
  403. setTimeout(() => {
  404. setShowSuccess(false);
  405. onClose();
  406. }, 1500);
  407. },
  408. });
  409. // Reset slot mutation
  410. const resetMutation = useMutation({
  411. mutationFn: async () => {
  412. return api.resetAmsSlot(printerId, slotInfo.amsId, slotInfo.trayId);
  413. },
  414. onSuccess: () => {
  415. setShowSuccess(true);
  416. onSuccess?.();
  417. setTimeout(() => {
  418. setShowSuccess(false);
  419. onClose();
  420. }, 1500);
  421. },
  422. });
  423. // Unified preset item for the list (cloud + local + builtin fallback)
  424. type PresetItem = { id: string; name: string; source: 'cloud' | 'local' | 'builtin'; isUser: boolean };
  425. // Filter filament presets based on search (merged cloud + local + builtin)
  426. const filteredPresets = useMemo(() => {
  427. const query = searchQuery.toLowerCase();
  428. const items: PresetItem[] = [];
  429. // Collect IDs already covered by cloud and local to avoid duplicates in fallback
  430. const coveredIds = new Set<string>();
  431. // Currently-configured preset should always be shown (bypass model filter)
  432. const savedId = slotInfo.savedPresetId;
  433. const trayIdx = slotInfo.trayInfoIdx;
  434. // 1. Cloud presets
  435. if (cloudSettings?.filament) {
  436. for (const cp of cloudSettings.filament) {
  437. coveredIds.add(cp.setting_id);
  438. // Keep preset if it matches the slot's saved mapping or current tray_info_idx
  439. const isCurrentPreset = savedId === cp.setting_id
  440. || (trayIdx && (cp.setting_id === trayIdx || convertToTrayInfoIdx(cp.setting_id) === trayIdx));
  441. if (!isCurrentPreset && query && !cp.name.toLowerCase().includes(query)) continue;
  442. // Filter by printer model if set (skip for current preset)
  443. if (!isCurrentPreset && printerModel) {
  444. const presetModel = extractPresetModel(cp.name);
  445. if (presetModel && presetModel.toUpperCase() !== printerModel.toUpperCase()) continue;
  446. }
  447. items.push({ id: cp.setting_id, name: cp.name, source: 'cloud', isUser: isUserPreset(cp.setting_id) });
  448. }
  449. }
  450. // 2. Local presets
  451. if (localPresets?.filament) {
  452. for (const lp of localPresets.filament) {
  453. const localId = `local_${lp.id}`;
  454. const isSaved = savedId === localId;
  455. if (!isSaved && query && !lp.name.toLowerCase().includes(query)) continue;
  456. // Filter by compatible_printers if set (skip for saved preset)
  457. if (!isSaved && printerModel && lp.compatible_printers) {
  458. const compatModels = lp.compatible_printers.split(';').map(p => {
  459. // Extract model from "BBL X1C" → "X1C"
  460. const trimmed = p.trim();
  461. const bblMatch = trimmed.match(/^BBL\s+(.+)/i);
  462. return bblMatch ? bblMatch[1].trim().toUpperCase() : trimmed.toUpperCase();
  463. }).filter(Boolean);
  464. if (compatModels.length > 0 && !compatModels.includes(printerModel.toUpperCase())) continue;
  465. }
  466. items.push({ id: localId, name: lp.name, source: 'local', isUser: false });
  467. }
  468. }
  469. // 3. Built-in filament names (fallback — only add entries not already covered)
  470. if (builtinFilaments) {
  471. for (const bf of builtinFilaments) {
  472. if (coveredIds.has(bf.filament_id)) continue;
  473. // Convert filament_id to setting_id format for cloud compatibility
  474. // e.g. "GFA00" → cloud setting_id would be "GFSA00" (insert S after GF)
  475. const settingId = bf.filament_id.startsWith('GF')
  476. ? 'GFS' + bf.filament_id.slice(2)
  477. : bf.filament_id;
  478. if (coveredIds.has(settingId)) continue;
  479. if (!query || bf.name.toLowerCase().includes(query)) {
  480. items.push({ id: `builtin_${bf.filament_id}`, name: bf.name, source: 'builtin', isUser: false });
  481. }
  482. }
  483. }
  484. // Sort: cloud user presets first, then cloud built-in, then local, then builtin fallback
  485. return items.sort((a, b) => {
  486. const sourceOrder = { cloud: 0, local: 1, builtin: 2 };
  487. if (a.source !== b.source) return sourceOrder[a.source] - sourceOrder[b.source];
  488. if (a.isUser && !b.isUser) return -1;
  489. if (!a.isUser && b.isUser) return 1;
  490. return a.name.localeCompare(b.name);
  491. });
  492. }, [cloudSettings?.filament, localPresets?.filament, builtinFilaments, searchQuery, printerModel, slotInfo.savedPresetId, slotInfo.trayInfoIdx]);
  493. // Get full preset name for K profile filtering (brand + material, without printer suffix)
  494. const selectedPresetInfo = useMemo(() => {
  495. if (!selectedPresetId) return null;
  496. // Resolve the name from cloud, local, or builtin presets
  497. let presetName: string | null = null;
  498. if (selectedPresetId.startsWith('local_')) {
  499. const localId = parseInt(selectedPresetId.replace('local_', ''), 10);
  500. const lp = localPresets?.filament.find(p => p.id === localId);
  501. presetName = lp?.name || null;
  502. } else if (selectedPresetId.startsWith('builtin_')) {
  503. const filamentId = selectedPresetId.replace('builtin_', '');
  504. const bf = builtinFilaments?.find(b => b.filament_id === filamentId);
  505. presetName = bf?.name || null;
  506. } else if (cloudSettings?.filament) {
  507. const cp = cloudSettings.filament.find(p => p.setting_id === selectedPresetId);
  508. presetName = cp?.name || null;
  509. }
  510. if (!presetName) return null;
  511. // Remove printer/nozzle suffix (e.g., "@BBL X1C" or "@0.4 nozzle")
  512. let nameWithoutSuffix = presetName.replace(/@.+$/, '').trim();
  513. // Strip leading "# " from custom preset names (user convention)
  514. if (nameWithoutSuffix.startsWith('# ')) {
  515. nameWithoutSuffix = nameWithoutSuffix.slice(2).trim();
  516. }
  517. const parsed = parsePresetName(nameWithoutSuffix);
  518. return {
  519. fullName: nameWithoutSuffix,
  520. material: parsed.material,
  521. brand: parsed.brand,
  522. };
  523. }, [selectedPresetId, cloudSettings?.filament, localPresets?.filament, builtinFilaments]);
  524. // For backwards compatibility with the label
  525. const selectedMaterial = selectedPresetInfo?.fullName || '';
  526. // Filter color catalog entries matching the selected preset's brand + material
  527. const catalogColors = useMemo(() => {
  528. if (!colorCatalog || !selectedPresetInfo) return [];
  529. const { fullName, brand } = selectedPresetInfo;
  530. // Try to find colors matching the full preset name (e.g., "PLA Metal")
  531. // The catalog uses the variant as part of the material field (e.g., material="PLA Metal")
  532. // Extract the full material+variant from the preset name
  533. const materialVariant = fullName.replace(/^(Bambu\s*(Lab)?|eSUN|Polymaker|Overture|Sunlu|Hatchbox)\s*/i, '').trim();
  534. return colorCatalog.filter(entry => {
  535. const entryMaterial = (entry.material || '').toUpperCase();
  536. const entryManufacturer = entry.manufacturer.toUpperCase();
  537. // Match material: try full material+variant first, then just material type
  538. const materialMatch = entryMaterial === materialVariant.toUpperCase()
  539. || entryMaterial.includes(materialVariant.toUpperCase())
  540. || materialVariant.toUpperCase().includes(entryMaterial);
  541. if (!materialMatch) return false;
  542. // If brand is present, also match manufacturer
  543. if (brand) {
  544. const upperBrand = brand.toUpperCase();
  545. // Fuzzy match: "Bambu" matches "Bambu Lab", etc.
  546. if (!entryManufacturer.includes(upperBrand) && !upperBrand.includes(entryManufacturer)) {
  547. return false;
  548. }
  549. }
  550. return true;
  551. });
  552. }, [colorCatalog, selectedPresetInfo]);
  553. const matchingKProfiles = useMemo(() => {
  554. if (!kprofilesData?.profiles || !selectedPresetInfo) return [];
  555. const { fullName, material, brand } = selectedPresetInfo;
  556. const upperFullName = fullName.toUpperCase();
  557. const upperMaterial = material.toUpperCase();
  558. const upperBrand = brand.toUpperCase();
  559. // Material must be at least 2 chars to avoid false positives
  560. if (!upperMaterial || upperMaterial.length < 2) return [];
  561. // Filter profiles - require brand match if brand is present in selected preset
  562. const filtered = kprofilesData.profiles.filter(p => {
  563. const profileName = p.name.toUpperCase();
  564. // If the selected preset has a brand (e.g., "Azurefilm PLA Wood"),
  565. // only show profiles that match the brand
  566. if (upperBrand) {
  567. // Must contain the brand name
  568. if (!profileName.includes(upperBrand)) {
  569. return false;
  570. }
  571. // And must contain the material type
  572. if (!profileName.includes(upperMaterial)) {
  573. return false;
  574. }
  575. return true;
  576. }
  577. // No brand in selected preset - match on full name or material
  578. // Priority 1: Exact match with full name
  579. if (profileName.includes(upperFullName)) {
  580. return true;
  581. }
  582. // Priority 2: Material type match (only when no brand specified)
  583. if (profileName.includes(upperMaterial)) {
  584. return true;
  585. }
  586. // Check for common material aliases
  587. const aliases: Record<string, string[]> = {
  588. 'NYLON': ['PA', 'PA-CF', 'PA6'],
  589. 'PA': ['NYLON'],
  590. };
  591. const materialAliases = aliases[upperMaterial] || [];
  592. for (const alias of materialAliases) {
  593. if (profileName.includes(alias)) {
  594. return true;
  595. }
  596. }
  597. return false;
  598. });
  599. // Deduplicate profiles with same name and k_value (multi-nozzle printers have duplicates)
  600. // Prefer the profile matching the slot's extruder (e.g. ext-R uses extruder 0, ext-L uses extruder 1)
  601. const seen = new Map<string, KProfile>();
  602. for (const profile of filtered) {
  603. const key = `${profile.name}|${profile.k_value}`;
  604. const existing = seen.get(key);
  605. if (!existing) {
  606. seen.set(key, profile);
  607. } else if (slotInfo.extruderId !== undefined && profile.extruder_id === slotInfo.extruderId && existing.extruder_id !== slotInfo.extruderId) {
  608. // Replace with profile matching slot's extruder
  609. seen.set(key, profile);
  610. }
  611. }
  612. return Array.from(seen.values());
  613. }, [kprofilesData?.profiles, selectedPresetInfo, slotInfo.extruderId]);
  614. // Pre-select current profile when modal opens, reset when closes
  615. useEffect(() => {
  616. if (isOpen) {
  617. // Pre-populate from saved preset mapping (most reliable)
  618. if (slotInfo.savedPresetId) {
  619. setSelectedPresetId(slotInfo.savedPresetId);
  620. } else if (slotInfo.trayInfoIdx && cloudSettings?.filament) {
  621. // Fallback: try to match by tray_info_idx in cloud presets
  622. // First try exact match on setting_id
  623. let currentPreset = cloudSettings.filament.find(
  624. p => p.setting_id === slotInfo.trayInfoIdx
  625. );
  626. // Then try matching by converting setting_id → filament_id format
  627. if (!currentPreset) {
  628. currentPreset = cloudSettings.filament.find(
  629. p => convertToTrayInfoIdx(p.setting_id) === slotInfo.trayInfoIdx
  630. );
  631. }
  632. if (currentPreset) {
  633. setSelectedPresetId(currentPreset.setting_id);
  634. }
  635. }
  636. // Pre-populate color from current slot (black is valid — empty slots don't pass trayColor)
  637. if (slotInfo.trayColor) {
  638. const hex = slotInfo.trayColor.slice(0, 6);
  639. if (hex) {
  640. setColorHex(hex);
  641. }
  642. }
  643. } else {
  644. // Reset when modal closes
  645. setSelectedPresetId('');
  646. setSelectedKProfile(null);
  647. setColorHex('');
  648. setColorInput('');
  649. setSearchQuery('');
  650. setShowSuccess(false);
  651. }
  652. }, [isOpen, slotInfo.savedPresetId, slotInfo.trayInfoIdx, slotInfo.trayColor, cloudSettings?.filament]);
  653. // Auto-select best matching K profile when preset changes
  654. useEffect(() => {
  655. if (matchingKProfiles.length > 0) {
  656. // Prefer the currently-active K-profile (by cali_idx) if available
  657. if (slotInfo.caliIdx != null && slotInfo.caliIdx > 0) {
  658. const active = matchingKProfiles.find(p => p.slot_id === slotInfo.caliIdx);
  659. if (active) {
  660. setSelectedKProfile(active);
  661. return;
  662. }
  663. }
  664. // Fallback: first matching profile
  665. setSelectedKProfile(matchingKProfiles[0]);
  666. } else {
  667. setSelectedKProfile(null);
  668. }
  669. }, [selectedPresetId, matchingKProfiles, slotInfo.caliIdx]);
  670. // Escape key handler
  671. const handleKeyDown = useCallback((e: KeyboardEvent) => {
  672. if (e.key === 'Escape') {
  673. onClose();
  674. }
  675. }, [onClose]);
  676. useEffect(() => {
  677. if (isOpen) {
  678. document.addEventListener('keydown', handleKeyDown);
  679. return () => document.removeEventListener('keydown', handleKeyDown);
  680. }
  681. }, [isOpen, handleKeyDown]);
  682. if (!isOpen) return null;
  683. const isLoading = (settingsLoading && !cloudError) || localLoading || builtinLoading || kprofilesLoading;
  684. const canSave = selectedPresetId && !configureMutation.isPending;
  685. // Get display color (custom or slot default)
  686. const displayColor = colorHex || slotInfo.trayColor?.slice(0, 6) || 'FFFFFF';
  687. return (
  688. <div className="fixed inset-0 z-50 flex items-center justify-center">
  689. {/* Backdrop */}
  690. <div
  691. className="absolute inset-0 bg-black/60 backdrop-blur-sm"
  692. onClick={onClose}
  693. />
  694. {/* Modal */}
  695. <div className="relative w-full max-w-lg mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl">
  696. {/* Header */}
  697. <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary">
  698. <div className="flex items-center gap-2">
  699. <Settings2 className="w-5 h-5 text-bambu-blue" />
  700. <h2 className="text-lg font-semibold text-white">{t('configureAmsSlot.title')}</h2>
  701. </div>
  702. <button
  703. onClick={onClose}
  704. className="p-1 text-bambu-gray hover:text-white rounded transition-colors"
  705. >
  706. <X className="w-5 h-5" />
  707. </button>
  708. </div>
  709. {/* Content */}
  710. <div className="p-4 space-y-4 max-h-[60vh] overflow-y-auto">
  711. {/* Success overlay */}
  712. {showSuccess && (
  713. <div className="absolute inset-0 bg-bambu-dark-secondary/95 z-10 flex items-center justify-center rounded-xl">
  714. <div className="text-center space-y-3">
  715. <CheckCircle2 className="w-16 h-16 text-bambu-green mx-auto" />
  716. <p className="text-lg font-semibold text-white">{t('configureAmsSlot.slotConfigured')}</p>
  717. <p className="text-sm text-bambu-gray">{t('configureAmsSlot.settingsSentToPrinter')}</p>
  718. </div>
  719. </div>
  720. )}
  721. {/* Slot info */}
  722. <div className="p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary">
  723. <p className="text-xs text-bambu-gray mb-1">{t('configureAmsSlot.configuringSlot')}</p>
  724. <div className="flex items-center gap-2">
  725. {slotInfo.trayColor && (
  726. <span
  727. className="w-4 h-4 rounded-full border border-white/20"
  728. style={{ backgroundColor: `#${slotInfo.trayColor.slice(0, 6)}` }}
  729. />
  730. )}
  731. <span className="text-white font-medium">
  732. {t('configureAmsSlot.slotLabel', { ams: getAmsLabel(slotInfo.amsId, slotInfo.trayCount), slot: slotInfo.trayId + 1 })}
  733. </span>
  734. {slotInfo.traySubBrands && (
  735. <span className="text-bambu-gray">({slotInfo.traySubBrands})</span>
  736. )}
  737. </div>
  738. </div>
  739. {isLoading ? (
  740. <div className="flex justify-center py-8">
  741. <Loader2 className="w-6 h-6 text-bambu-green animate-spin" />
  742. </div>
  743. ) : (
  744. <>
  745. {/* Filament Profile Select */}
  746. <div>
  747. <label className="block text-sm text-bambu-gray mb-2">
  748. {t('configureAmsSlot.filamentProfile')} <span className="text-red-400">*</span>
  749. </label>
  750. <div className="relative">
  751. <input
  752. type="text"
  753. placeholder={t('configureAmsSlot.searchPresets')}
  754. value={searchQuery}
  755. onChange={(e) => setSearchQuery(e.target.value)}
  756. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder:text-bambu-gray focus:border-bambu-green focus:outline-none mb-2"
  757. />
  758. <div className="max-h-48 overflow-y-auto space-y-1">
  759. {filteredPresets.length === 0 ? (
  760. <p className="text-center py-4 text-bambu-gray">
  761. {(cloudSettings?.filament?.length === 0 && !localPresets?.filament?.length)
  762. ? t('configureAmsSlot.noPresetsAvailable')
  763. : t('configureAmsSlot.noMatchingPresets')}
  764. </p>
  765. ) : (
  766. filteredPresets.map((preset) => (
  767. <button
  768. key={preset.id}
  769. ref={selectedPresetId === preset.id ? (el) => {
  770. el?.scrollIntoView({ block: 'nearest' });
  771. } : undefined}
  772. onClick={() => setSelectedPresetId(preset.id)}
  773. className={`w-full p-2 rounded-lg border text-left transition-colors ${
  774. selectedPresetId === preset.id
  775. ? 'bg-bambu-green/20 border-bambu-green'
  776. : 'bg-bambu-dark border-bambu-dark-tertiary hover:border-bambu-gray'
  777. }`}
  778. >
  779. <div className="flex items-center justify-between">
  780. <span className="text-white text-sm truncate">{preset.name}</span>
  781. <div className="flex items-center gap-1 flex-shrink-0">
  782. {preset.source === 'local' && (
  783. <span className="text-xs px-1.5 py-0.5 rounded bg-green-500/20 text-green-400">
  784. {t('profiles.localProfiles.badge')}
  785. </span>
  786. )}
  787. {preset.source === 'builtin' && (
  788. <span className="text-xs px-1.5 py-0.5 rounded bg-amber-500/20 text-amber-400">
  789. {t('configureAmsSlot.builtin')}
  790. </span>
  791. )}
  792. {preset.isUser && (
  793. <span className="text-xs px-1.5 py-0.5 rounded bg-bambu-blue/20 text-bambu-blue">
  794. {t('configureAmsSlot.custom')}
  795. </span>
  796. )}
  797. </div>
  798. </div>
  799. </button>
  800. ))
  801. )}
  802. </div>
  803. </div>
  804. </div>
  805. {/* K Profile Select */}
  806. <div>
  807. <label className="block text-sm text-bambu-gray mb-2">
  808. {t('configureAmsSlot.kProfileLabel')}
  809. {selectedMaterial && (
  810. <span className="ml-2 text-xs text-bambu-blue">
  811. {t('configureAmsSlot.filteringFor', { material: selectedMaterial })}
  812. </span>
  813. )}
  814. </label>
  815. {matchingKProfiles.length > 0 ? (
  816. <div className="relative">
  817. <select
  818. value={selectedKProfile?.name || ''}
  819. onChange={(e) => {
  820. const profile = matchingKProfiles.find(p => p.name === e.target.value);
  821. setSelectedKProfile(profile || null);
  822. }}
  823. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none appearance-none pr-10"
  824. >
  825. <option value="">{t('configureAmsSlot.noKProfile')}</option>
  826. {matchingKProfiles.map((profile) => (
  827. <option key={`${profile.name}-${profile.extruder_id}`} value={profile.name}>
  828. {profile.name} (K={profile.k_value})
  829. </option>
  830. ))}
  831. </select>
  832. <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
  833. </div>
  834. ) : selectedPresetId ? (
  835. <p className="text-sm text-bambu-gray italic py-2">
  836. {t('configureAmsSlot.noMatchingKProfiles')}
  837. </p>
  838. ) : (
  839. <span className="inline-block text-xs px-2 py-1 rounded bg-amber-500/20 text-amber-400 border border-amber-500/30">
  840. {t('configureAmsSlot.selectFilamentFirst')}
  841. </span>
  842. )}
  843. {selectedKProfile && (
  844. <p className="text-xs text-bambu-green mt-1">
  845. {t('configureAmsSlot.kFromCalibration', { value: selectedKProfile.k_value })}
  846. </p>
  847. )}
  848. </div>
  849. {/* Optional: Custom color */}
  850. <div>
  851. <label className="block text-sm text-bambu-gray mb-2">
  852. {t('configureAmsSlot.customColorLabel')}
  853. </label>
  854. {/* Catalog colors matching selected preset */}
  855. {catalogColors.length > 0 && (
  856. <div className="mb-3">
  857. <p className="text-xs text-bambu-gray mb-1.5">
  858. {t('configureAmsSlot.presetColors', { name: selectedPresetInfo?.fullName })}
  859. </p>
  860. <div className="flex flex-wrap gap-1.5">
  861. {catalogColors.map((entry) => (
  862. <button
  863. key={entry.id}
  864. onClick={() => {
  865. const hex = entry.hex_color.replace('#', '').toUpperCase();
  866. setColorHex(hex);
  867. setColorInput(entry.color_name);
  868. }}
  869. className={`h-7 px-2 rounded-md border-2 transition-all flex items-center gap-1.5 ${
  870. colorHex === entry.hex_color.replace('#', '').toUpperCase()
  871. ? 'border-bambu-green scale-105'
  872. : 'border-white/20 hover:border-white/40'
  873. }`}
  874. title={entry.color_name}
  875. >
  876. <span
  877. className="w-4 h-4 rounded-full border border-white/30 flex-shrink-0"
  878. style={{ backgroundColor: entry.hex_color }}
  879. />
  880. <span className="text-xs text-white/80 whitespace-nowrap">{entry.color_name}</span>
  881. </button>
  882. ))}
  883. </div>
  884. </div>
  885. )}
  886. {/* Quick color buttons */}
  887. <div className="flex flex-wrap gap-1.5 mb-2">
  888. {QUICK_COLORS_BASIC.map((color) => (
  889. <button
  890. key={color.hex}
  891. onClick={() => {
  892. setColorHex(color.hex);
  893. setColorInput(color.name);
  894. }}
  895. className={`w-7 h-7 rounded-md border-2 transition-all ${
  896. colorHex === color.hex
  897. ? 'border-bambu-green scale-110'
  898. : 'border-white/20 hover:border-white/40'
  899. }`}
  900. style={{ backgroundColor: `#${color.hex}` }}
  901. title={color.name}
  902. />
  903. ))}
  904. <button
  905. onClick={() => setShowExtendedColors(!showExtendedColors)}
  906. className="w-7 h-7 rounded-md border-2 border-white/20 hover:border-white/40 flex items-center justify-center text-white/60 hover:text-white/80 transition-all text-xs"
  907. title={showExtendedColors ? t('configureAmsSlot.showLessColors') : t('configureAmsSlot.showMoreColors')}
  908. >
  909. {showExtendedColors ? '−' : '+'}
  910. </button>
  911. </div>
  912. {/* Extended colors (collapsible) */}
  913. {showExtendedColors && (
  914. <div className="flex flex-wrap gap-1.5 mb-2">
  915. {QUICK_COLORS_EXTENDED.map((color) => (
  916. <button
  917. key={color.hex}
  918. onClick={() => {
  919. setColorHex(color.hex);
  920. setColorInput(color.name);
  921. }}
  922. className={`w-7 h-7 rounded-md border-2 transition-all ${
  923. colorHex === color.hex
  924. ? 'border-bambu-green scale-110'
  925. : 'border-white/20 hover:border-white/40'
  926. }`}
  927. style={{ backgroundColor: `#${color.hex}` }}
  928. title={color.name}
  929. />
  930. ))}
  931. </div>
  932. )}
  933. {/* Color input: name or hex */}
  934. <div className="flex gap-2 items-center">
  935. <div
  936. className="w-10 h-10 rounded-lg border-2 border-white/20 flex-shrink-0"
  937. style={{ backgroundColor: `#${displayColor}` }}
  938. />
  939. <input
  940. type="text"
  941. placeholder={t('configureAmsSlot.colorPlaceholder')}
  942. value={colorInput}
  943. onChange={(e) => {
  944. const input = e.target.value;
  945. setColorInput(input);
  946. // Try to parse as color name first
  947. const nameHex = colorNameToHex(input);
  948. if (nameHex) {
  949. setColorHex(nameHex);
  950. } else {
  951. // Try to parse as hex code
  952. const cleaned = input.replace(/[^0-9A-Fa-f]/g, '').toUpperCase();
  953. if (cleaned.length === 6) {
  954. setColorHex(cleaned);
  955. } else if (cleaned.length === 3) {
  956. // Expand shorthand hex (e.g., F00 -> FF0000)
  957. setColorHex(cleaned.split('').map(c => c + c).join(''));
  958. }
  959. }
  960. }}
  961. className="flex-1 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder:text-bambu-gray focus:border-bambu-green focus:outline-none text-sm"
  962. />
  963. {colorHex && (
  964. <button
  965. onClick={() => {
  966. setColorHex('');
  967. setColorInput('');
  968. }}
  969. className="px-2 py-1 text-xs text-bambu-gray hover:text-white bg-bambu-dark-tertiary rounded"
  970. title={t('configureAmsSlot.clearCustomColor')}
  971. >
  972. {t('configureAmsSlot.clear')}
  973. </button>
  974. )}
  975. </div>
  976. {colorHex && (
  977. <p className="text-xs text-bambu-gray mt-1.5">
  978. {t('configureAmsSlot.hexLabel', { hex: colorHex })}
  979. </p>
  980. )}
  981. </div>
  982. </>
  983. )}
  984. </div>
  985. {/* Footer */}
  986. <div className="flex justify-between p-4 border-t border-bambu-dark-tertiary">
  987. {/* Reset button on the left */}
  988. <Button
  989. variant="secondary"
  990. onClick={() => resetMutation.mutate()}
  991. disabled={resetMutation.isPending || configureMutation.isPending}
  992. className="text-red-400 hover:text-red-300 hover:bg-red-500/10"
  993. >
  994. {resetMutation.isPending ? (
  995. <>
  996. <Loader2 className="w-4 h-4 animate-spin" />
  997. {t('configureAmsSlot.resetting')}
  998. </>
  999. ) : (
  1000. <>
  1001. <RotateCcw className="w-4 h-4" />
  1002. {t('configureAmsSlot.resetSlot')}
  1003. </>
  1004. )}
  1005. </Button>
  1006. {/* Cancel and Configure buttons on the right */}
  1007. <div className="flex gap-2">
  1008. <Button variant="secondary" onClick={onClose}>
  1009. {t('configureAmsSlot.cancel')}
  1010. </Button>
  1011. <Button
  1012. onClick={() => configureMutation.mutate()}
  1013. disabled={!canSave}
  1014. >
  1015. {configureMutation.isPending ? (
  1016. <>
  1017. <Loader2 className="w-4 h-4 animate-spin" />
  1018. {t('configureAmsSlot.configuring')}
  1019. </>
  1020. ) : (
  1021. <>
  1022. <Settings2 className="w-4 h-4" />
  1023. {t('configureAmsSlot.configureSlot')}
  1024. </>
  1025. )}
  1026. </Button>
  1027. </div>
  1028. </div>
  1029. {/* Error */}
  1030. {(configureMutation.isError || resetMutation.isError) && (
  1031. <div className="mx-4 mb-4 p-2 bg-red-500/20 border border-red-500/50 rounded text-sm text-red-400">
  1032. {(configureMutation.error as Error)?.message || (resetMutation.error as Error)?.message}
  1033. </div>
  1034. )}
  1035. </div>
  1036. </div>
  1037. );
  1038. }