ConfigureAmsSlotModal.tsx 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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', '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 mapping
  288. trayInfoIdx = '';
  289. settingId = '';
  290. } else if (isBuiltin) {
  291. // Built-in presets use the filament_id directly as tray_info_idx
  292. trayInfoIdx = builtinFilamentId!;
  293. settingId = '';
  294. } else {
  295. // Get tray_info_idx: for user presets, fetch detail to get filament_id or derive from base_id
  296. trayInfoIdx = convertToTrayInfoIdx(selectedPresetId);
  297. settingId = selectedPresetId;
  298. // For user presets (not starting with GF), fetch the detail to get the real filament_id
  299. if (!selectedPresetId.startsWith('GFS')) {
  300. try {
  301. const detail = await api.getCloudSettingDetail(selectedPresetId);
  302. if (detail.filament_id) {
  303. trayInfoIdx = detail.filament_id;
  304. } else if (detail.base_id) {
  305. trayInfoIdx = convertToTrayInfoIdx(detail.base_id);
  306. console.log(`Derived tray_info_idx from base_id: ${detail.base_id} -> ${trayInfoIdx}`);
  307. }
  308. } catch (e) {
  309. console.warn('Failed to fetch preset detail for filament_id:', e);
  310. }
  311. }
  312. }
  313. // Default temp range — use local preset core fields if available
  314. let tempMin = isLocal && localPreset?.nozzle_temp_min ? localPreset.nozzle_temp_min : 190;
  315. let tempMax = isLocal && localPreset?.nozzle_temp_max ? localPreset.nozzle_temp_max : 230;
  316. if (!isLocal || isBuiltin || (!localPreset?.nozzle_temp_min && !localPreset?.nozzle_temp_max)) {
  317. // Fall back to material-based defaults
  318. const material = (isLocal ? (localPreset?.filament_type || parsed.material) : parsed.material).toUpperCase();
  319. if (material.includes('PLA')) {
  320. tempMin = 190;
  321. tempMax = 230;
  322. } else if (material.includes('PETG')) {
  323. tempMin = 220;
  324. tempMax = 260;
  325. } else if (material.includes('ABS')) {
  326. tempMin = 240;
  327. tempMax = 280;
  328. } else if (material.includes('ASA')) {
  329. tempMin = 240;
  330. tempMax = 280;
  331. } else if (material.includes('TPU')) {
  332. tempMin = 200;
  333. tempMax = 240;
  334. } else if (material.includes('PC')) {
  335. tempMin = 260;
  336. tempMax = 300;
  337. } else if (material.includes('PA') || material.includes('NYLON')) {
  338. tempMin = 250;
  339. tempMax = 290;
  340. }
  341. }
  342. // Parse K value from selected profile
  343. const kValue = selectedKProfile?.k_value ? parseFloat(selectedKProfile.k_value) : 0;
  344. // Determine tray_type: use local preset's filament_type or parsed material
  345. const trayType = isLocal
  346. ? (localPreset?.filament_type || parsed.material || 'PLA')
  347. : (parsed.material || 'PLA');
  348. // Configure the slot via MQTT
  349. const result = await api.configureAmsSlot(printerId, slotInfo.amsId, slotInfo.trayId, {
  350. tray_info_idx: trayInfoIdx,
  351. tray_type: trayType,
  352. tray_sub_brands: traySubBrands,
  353. tray_color: color + 'FF', // Add alpha
  354. nozzle_temp_min: tempMin,
  355. nozzle_temp_max: tempMax,
  356. cali_idx: caliIdx,
  357. nozzle_diameter: nozzleDiameter,
  358. setting_id: settingId, // Full setting ID for slicer compatibility (empty for local)
  359. // Pass K profile's filament_id and setting_id for proper linking
  360. kprofile_filament_id: selectedKProfile?.filament_id,
  361. kprofile_setting_id: selectedKProfile?.setting_id || undefined,
  362. // Also pass the K value directly for extrusion_cali_set command
  363. k_value: kValue,
  364. });
  365. // Save the preset mapping so we can display the correct name in the UI
  366. // This is needed because user presets use filament_id (e.g., P285e239) as tray_info_idx,
  367. // which can't be resolved to a name via the filamentInfo API
  368. const mappingPresetId = isLocal ? `local_${localId}` : isBuiltin ? `builtin_${builtinFilamentId}` : selectedPresetId;
  369. const mappingSource = isLocal ? 'local' : isBuiltin ? 'builtin' : 'cloud';
  370. try {
  371. await api.saveSlotPreset(printerId, slotInfo.amsId, slotInfo.trayId, mappingPresetId, traySubBrands, mappingSource);
  372. } catch (e) {
  373. console.warn('Failed to save slot preset mapping:', e);
  374. // Don't fail the whole operation - slot was configured successfully
  375. }
  376. return result;
  377. },
  378. onSuccess: () => {
  379. setShowSuccess(true);
  380. onSuccess?.();
  381. // Close after showing success briefly
  382. setTimeout(() => {
  383. setShowSuccess(false);
  384. onClose();
  385. }, 1500);
  386. },
  387. });
  388. // Reset slot mutation
  389. const resetMutation = useMutation({
  390. mutationFn: async () => {
  391. return api.resetAmsSlot(printerId, slotInfo.amsId, slotInfo.trayId);
  392. },
  393. onSuccess: () => {
  394. setShowSuccess(true);
  395. onSuccess?.();
  396. setTimeout(() => {
  397. setShowSuccess(false);
  398. onClose();
  399. }, 1500);
  400. },
  401. });
  402. // Unified preset item for the list (cloud + local + builtin fallback)
  403. type PresetItem = { id: string; name: string; source: 'cloud' | 'local' | 'builtin'; isUser: boolean };
  404. // Filter filament presets based on search (merged cloud + local + builtin)
  405. const filteredPresets = useMemo(() => {
  406. const query = searchQuery.toLowerCase();
  407. const items: PresetItem[] = [];
  408. // Collect IDs already covered by cloud and local to avoid duplicates in fallback
  409. const coveredIds = new Set<string>();
  410. // Currently-configured preset should always be shown (bypass model filter)
  411. const savedId = slotInfo.savedPresetId;
  412. const trayIdx = slotInfo.trayInfoIdx;
  413. // 1. Cloud presets
  414. if (cloudSettings?.filament) {
  415. for (const cp of cloudSettings.filament) {
  416. coveredIds.add(cp.setting_id);
  417. // Keep preset if it matches the slot's saved mapping or current tray_info_idx
  418. const isCurrentPreset = savedId === cp.setting_id
  419. || (trayIdx && (cp.setting_id === trayIdx || convertToTrayInfoIdx(cp.setting_id) === trayIdx));
  420. if (!isCurrentPreset && query && !cp.name.toLowerCase().includes(query)) continue;
  421. // Filter by printer model if set (skip for current preset)
  422. if (!isCurrentPreset && printerModel) {
  423. const presetModel = extractPresetModel(cp.name);
  424. if (presetModel && presetModel.toUpperCase() !== printerModel.toUpperCase()) continue;
  425. }
  426. items.push({ id: cp.setting_id, name: cp.name, source: 'cloud', isUser: isUserPreset(cp.setting_id) });
  427. }
  428. }
  429. // 2. Local presets
  430. if (localPresets?.filament) {
  431. for (const lp of localPresets.filament) {
  432. const localId = `local_${lp.id}`;
  433. const isSaved = savedId === localId;
  434. if (!isSaved && query && !lp.name.toLowerCase().includes(query)) continue;
  435. // Filter by compatible_printers if set (skip for saved preset)
  436. if (!isSaved && printerModel && lp.compatible_printers) {
  437. const compatModels = lp.compatible_printers.split(';').map(p => {
  438. // Extract model from "BBL X1C" → "X1C"
  439. const trimmed = p.trim();
  440. const bblMatch = trimmed.match(/^BBL\s+(.+)/i);
  441. return bblMatch ? bblMatch[1].trim().toUpperCase() : trimmed.toUpperCase();
  442. }).filter(Boolean);
  443. if (compatModels.length > 0 && !compatModels.includes(printerModel.toUpperCase())) continue;
  444. }
  445. items.push({ id: localId, name: lp.name, source: 'local', isUser: false });
  446. }
  447. }
  448. // 3. Built-in filament names (fallback — only add entries not already covered)
  449. if (builtinFilaments) {
  450. for (const bf of builtinFilaments) {
  451. if (coveredIds.has(bf.filament_id)) continue;
  452. // Convert filament_id to setting_id format for cloud compatibility
  453. // e.g. "GFA00" → cloud setting_id would be "GFSA00" (insert S after GF)
  454. const settingId = bf.filament_id.startsWith('GF')
  455. ? 'GFS' + bf.filament_id.slice(2)
  456. : bf.filament_id;
  457. if (coveredIds.has(settingId)) continue;
  458. if (!query || bf.name.toLowerCase().includes(query)) {
  459. items.push({ id: `builtin_${bf.filament_id}`, name: bf.name, source: 'builtin', isUser: false });
  460. }
  461. }
  462. }
  463. // Sort: cloud user presets first, then cloud built-in, then local, then builtin fallback
  464. return items.sort((a, b) => {
  465. const sourceOrder = { cloud: 0, local: 1, builtin: 2 };
  466. if (a.source !== b.source) return sourceOrder[a.source] - sourceOrder[b.source];
  467. if (a.isUser && !b.isUser) return -1;
  468. if (!a.isUser && b.isUser) return 1;
  469. return a.name.localeCompare(b.name);
  470. });
  471. }, [cloudSettings?.filament, localPresets?.filament, builtinFilaments, searchQuery, printerModel, slotInfo.savedPresetId, slotInfo.trayInfoIdx]);
  472. // Get full preset name for K profile filtering (brand + material, without printer suffix)
  473. const selectedPresetInfo = useMemo(() => {
  474. if (!selectedPresetId) return null;
  475. // Resolve the name from cloud, local, or builtin presets
  476. let presetName: string | null = null;
  477. if (selectedPresetId.startsWith('local_')) {
  478. const localId = parseInt(selectedPresetId.replace('local_', ''), 10);
  479. const lp = localPresets?.filament.find(p => p.id === localId);
  480. presetName = lp?.name || null;
  481. } else if (selectedPresetId.startsWith('builtin_')) {
  482. const filamentId = selectedPresetId.replace('builtin_', '');
  483. const bf = builtinFilaments?.find(b => b.filament_id === filamentId);
  484. presetName = bf?.name || null;
  485. } else if (cloudSettings?.filament) {
  486. const cp = cloudSettings.filament.find(p => p.setting_id === selectedPresetId);
  487. presetName = cp?.name || null;
  488. }
  489. if (!presetName) return null;
  490. // Remove printer/nozzle suffix (e.g., "@BBL X1C" or "@0.4 nozzle")
  491. let nameWithoutSuffix = presetName.replace(/@.+$/, '').trim();
  492. // Strip leading "# " from custom preset names (user convention)
  493. if (nameWithoutSuffix.startsWith('# ')) {
  494. nameWithoutSuffix = nameWithoutSuffix.slice(2).trim();
  495. }
  496. const parsed = parsePresetName(nameWithoutSuffix);
  497. return {
  498. fullName: nameWithoutSuffix,
  499. material: parsed.material,
  500. brand: parsed.brand,
  501. };
  502. }, [selectedPresetId, cloudSettings?.filament, localPresets?.filament, builtinFilaments]);
  503. // For backwards compatibility with the label
  504. const selectedMaterial = selectedPresetInfo?.fullName || '';
  505. // Filter color catalog entries matching the selected preset's brand + material
  506. const catalogColors = useMemo(() => {
  507. if (!colorCatalog || !selectedPresetInfo) return [];
  508. const { fullName, brand } = selectedPresetInfo;
  509. // Try to find colors matching the full preset name (e.g., "PLA Metal")
  510. // The catalog uses the variant as part of the material field (e.g., material="PLA Metal")
  511. // Extract the full material+variant from the preset name
  512. const materialVariant = fullName.replace(/^(Bambu\s*(Lab)?|eSUN|Polymaker|Overture|Sunlu|Hatchbox)\s*/i, '').trim();
  513. return colorCatalog.filter(entry => {
  514. const entryMaterial = (entry.material || '').toUpperCase();
  515. const entryManufacturer = entry.manufacturer.toUpperCase();
  516. // Match material: try full material+variant first, then just material type
  517. const materialMatch = entryMaterial === materialVariant.toUpperCase()
  518. || entryMaterial.includes(materialVariant.toUpperCase())
  519. || materialVariant.toUpperCase().includes(entryMaterial);
  520. if (!materialMatch) return false;
  521. // If brand is present, also match manufacturer
  522. if (brand) {
  523. const upperBrand = brand.toUpperCase();
  524. // Fuzzy match: "Bambu" matches "Bambu Lab", etc.
  525. if (!entryManufacturer.includes(upperBrand) && !upperBrand.includes(entryManufacturer)) {
  526. return false;
  527. }
  528. }
  529. return true;
  530. });
  531. }, [colorCatalog, selectedPresetInfo]);
  532. const matchingKProfiles = useMemo(() => {
  533. if (!kprofilesData?.profiles || !selectedPresetInfo) return [];
  534. const { fullName, material, brand } = selectedPresetInfo;
  535. const upperFullName = fullName.toUpperCase();
  536. const upperMaterial = material.toUpperCase();
  537. const upperBrand = brand.toUpperCase();
  538. // Material must be at least 2 chars to avoid false positives
  539. if (!upperMaterial || upperMaterial.length < 2) return [];
  540. // Filter profiles - require brand match if brand is present in selected preset
  541. const filtered = kprofilesData.profiles.filter(p => {
  542. const profileName = p.name.toUpperCase();
  543. // If the selected preset has a brand (e.g., "Azurefilm PLA Wood"),
  544. // only show profiles that match the brand
  545. if (upperBrand) {
  546. // Must contain the brand name
  547. if (!profileName.includes(upperBrand)) {
  548. return false;
  549. }
  550. // And must contain the material type
  551. if (!profileName.includes(upperMaterial)) {
  552. return false;
  553. }
  554. return true;
  555. }
  556. // No brand in selected preset - match on full name or material
  557. // Priority 1: Exact match with full name
  558. if (profileName.includes(upperFullName)) {
  559. return true;
  560. }
  561. // Priority 2: Material type match (only when no brand specified)
  562. if (profileName.includes(upperMaterial)) {
  563. return true;
  564. }
  565. // Check for common material aliases
  566. const aliases: Record<string, string[]> = {
  567. 'NYLON': ['PA', 'PA-CF', 'PA6'],
  568. 'PA': ['NYLON'],
  569. };
  570. const materialAliases = aliases[upperMaterial] || [];
  571. for (const alias of materialAliases) {
  572. if (profileName.includes(alias)) {
  573. return true;
  574. }
  575. }
  576. return false;
  577. });
  578. // Deduplicate profiles with same name and k_value (multi-nozzle printers have duplicates)
  579. // Prefer the profile matching the slot's extruder (e.g. ext-R uses extruder 0, ext-L uses extruder 1)
  580. const seen = new Map<string, KProfile>();
  581. for (const profile of filtered) {
  582. const key = `${profile.name}|${profile.k_value}`;
  583. const existing = seen.get(key);
  584. if (!existing) {
  585. seen.set(key, profile);
  586. } else if (slotInfo.extruderId !== undefined && profile.extruder_id === slotInfo.extruderId && existing.extruder_id !== slotInfo.extruderId) {
  587. // Replace with profile matching slot's extruder
  588. seen.set(key, profile);
  589. }
  590. }
  591. return Array.from(seen.values());
  592. }, [kprofilesData?.profiles, selectedPresetInfo, slotInfo.extruderId]);
  593. // Pre-select current profile when modal opens, reset when closes
  594. useEffect(() => {
  595. if (isOpen) {
  596. // Pre-populate from saved preset mapping (most reliable)
  597. if (slotInfo.savedPresetId) {
  598. setSelectedPresetId(slotInfo.savedPresetId);
  599. } else if (slotInfo.trayInfoIdx && cloudSettings?.filament) {
  600. // Fallback: try to match by tray_info_idx in cloud presets
  601. // First try exact match on setting_id
  602. let currentPreset = cloudSettings.filament.find(
  603. p => p.setting_id === slotInfo.trayInfoIdx
  604. );
  605. // Then try matching by converting setting_id → filament_id format
  606. if (!currentPreset) {
  607. currentPreset = cloudSettings.filament.find(
  608. p => convertToTrayInfoIdx(p.setting_id) === slotInfo.trayInfoIdx
  609. );
  610. }
  611. if (currentPreset) {
  612. setSelectedPresetId(currentPreset.setting_id);
  613. }
  614. }
  615. // Pre-populate color from current slot (black is valid — empty slots don't pass trayColor)
  616. if (slotInfo.trayColor) {
  617. const hex = slotInfo.trayColor.slice(0, 6);
  618. if (hex) {
  619. setColorHex(hex);
  620. }
  621. }
  622. } else {
  623. // Reset when modal closes
  624. setSelectedPresetId('');
  625. setSelectedKProfile(null);
  626. setColorHex('');
  627. setColorInput('');
  628. setSearchQuery('');
  629. setShowSuccess(false);
  630. }
  631. }, [isOpen, slotInfo.savedPresetId, slotInfo.trayInfoIdx, slotInfo.trayColor, cloudSettings?.filament]);
  632. // Auto-select best matching K profile when preset changes
  633. useEffect(() => {
  634. if (matchingKProfiles.length > 0) {
  635. // Prefer the currently-active K-profile (by cali_idx) if available
  636. if (slotInfo.caliIdx != null && slotInfo.caliIdx > 0) {
  637. const active = matchingKProfiles.find(p => p.slot_id === slotInfo.caliIdx);
  638. if (active) {
  639. setSelectedKProfile(active);
  640. return;
  641. }
  642. }
  643. // Fallback: first matching profile
  644. setSelectedKProfile(matchingKProfiles[0]);
  645. } else {
  646. setSelectedKProfile(null);
  647. }
  648. }, [selectedPresetId, matchingKProfiles, slotInfo.caliIdx]);
  649. // Escape key handler
  650. const handleKeyDown = useCallback((e: KeyboardEvent) => {
  651. if (e.key === 'Escape') {
  652. onClose();
  653. }
  654. }, [onClose]);
  655. useEffect(() => {
  656. if (isOpen) {
  657. document.addEventListener('keydown', handleKeyDown);
  658. return () => document.removeEventListener('keydown', handleKeyDown);
  659. }
  660. }, [isOpen, handleKeyDown]);
  661. if (!isOpen) return null;
  662. const isLoading = (settingsLoading && !cloudError) || localLoading || builtinLoading || kprofilesLoading;
  663. const canSave = selectedPresetId && !configureMutation.isPending;
  664. // Get display color (custom or slot default)
  665. const displayColor = colorHex || slotInfo.trayColor?.slice(0, 6) || 'FFFFFF';
  666. return (
  667. <div className="fixed inset-0 z-50 flex items-center justify-center">
  668. {/* Backdrop */}
  669. <div
  670. className="absolute inset-0 bg-black/60 backdrop-blur-sm"
  671. onClick={onClose}
  672. />
  673. {/* Modal */}
  674. <div className="relative w-full max-w-lg mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl">
  675. {/* Header */}
  676. <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary">
  677. <div className="flex items-center gap-2">
  678. <Settings2 className="w-5 h-5 text-bambu-blue" />
  679. <h2 className="text-lg font-semibold text-white">{t('configureAmsSlot.title')}</h2>
  680. </div>
  681. <button
  682. onClick={onClose}
  683. className="p-1 text-bambu-gray hover:text-white rounded transition-colors"
  684. >
  685. <X className="w-5 h-5" />
  686. </button>
  687. </div>
  688. {/* Content */}
  689. <div className="p-4 space-y-4 max-h-[60vh] overflow-y-auto">
  690. {/* Success overlay */}
  691. {showSuccess && (
  692. <div className="absolute inset-0 bg-bambu-dark-secondary/95 z-10 flex items-center justify-center rounded-xl">
  693. <div className="text-center space-y-3">
  694. <CheckCircle2 className="w-16 h-16 text-bambu-green mx-auto" />
  695. <p className="text-lg font-semibold text-white">{t('configureAmsSlot.slotConfigured')}</p>
  696. <p className="text-sm text-bambu-gray">{t('configureAmsSlot.settingsSentToPrinter')}</p>
  697. </div>
  698. </div>
  699. )}
  700. {/* Slot info */}
  701. <div className="p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary">
  702. <p className="text-xs text-bambu-gray mb-1">{t('configureAmsSlot.configuringSlot')}</p>
  703. <div className="flex items-center gap-2">
  704. {slotInfo.trayColor && (
  705. <span
  706. className="w-4 h-4 rounded-full border border-white/20"
  707. style={{ backgroundColor: `#${slotInfo.trayColor.slice(0, 6)}` }}
  708. />
  709. )}
  710. <span className="text-white font-medium">
  711. {t('configureAmsSlot.slotLabel', { ams: getAmsLabel(slotInfo.amsId, slotInfo.trayCount), slot: slotInfo.trayId + 1 })}
  712. </span>
  713. {slotInfo.traySubBrands && (
  714. <span className="text-bambu-gray">({slotInfo.traySubBrands})</span>
  715. )}
  716. </div>
  717. </div>
  718. {isLoading ? (
  719. <div className="flex justify-center py-8">
  720. <Loader2 className="w-6 h-6 text-bambu-green animate-spin" />
  721. </div>
  722. ) : (
  723. <>
  724. {/* Filament Profile Select */}
  725. <div>
  726. <label className="block text-sm text-bambu-gray mb-2">
  727. {t('configureAmsSlot.filamentProfile')} <span className="text-red-400">*</span>
  728. </label>
  729. <div className="relative">
  730. <input
  731. type="text"
  732. placeholder={t('configureAmsSlot.searchPresets')}
  733. value={searchQuery}
  734. onChange={(e) => setSearchQuery(e.target.value)}
  735. 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"
  736. />
  737. <div className="max-h-48 overflow-y-auto space-y-1">
  738. {filteredPresets.length === 0 ? (
  739. <p className="text-center py-4 text-bambu-gray">
  740. {(cloudSettings?.filament?.length === 0 && !localPresets?.filament?.length)
  741. ? t('configureAmsSlot.noPresetsAvailable')
  742. : t('configureAmsSlot.noMatchingPresets')}
  743. </p>
  744. ) : (
  745. filteredPresets.map((preset) => (
  746. <button
  747. key={preset.id}
  748. ref={selectedPresetId === preset.id ? (el) => {
  749. el?.scrollIntoView({ block: 'nearest' });
  750. } : undefined}
  751. onClick={() => setSelectedPresetId(preset.id)}
  752. className={`w-full p-2 rounded-lg border text-left transition-colors ${
  753. selectedPresetId === preset.id
  754. ? 'bg-bambu-green/20 border-bambu-green'
  755. : 'bg-bambu-dark border-bambu-dark-tertiary hover:border-bambu-gray'
  756. }`}
  757. >
  758. <div className="flex items-center justify-between">
  759. <span className="text-white text-sm truncate">{preset.name}</span>
  760. <div className="flex items-center gap-1 flex-shrink-0">
  761. {preset.source === 'local' && (
  762. <span className="text-xs px-1.5 py-0.5 rounded bg-green-500/20 text-green-400">
  763. {t('profiles.localProfiles.badge')}
  764. </span>
  765. )}
  766. {preset.source === 'builtin' && (
  767. <span className="text-xs px-1.5 py-0.5 rounded bg-amber-500/20 text-amber-400">
  768. {t('configureAmsSlot.builtin')}
  769. </span>
  770. )}
  771. {preset.isUser && (
  772. <span className="text-xs px-1.5 py-0.5 rounded bg-bambu-blue/20 text-bambu-blue">
  773. {t('configureAmsSlot.custom')}
  774. </span>
  775. )}
  776. </div>
  777. </div>
  778. </button>
  779. ))
  780. )}
  781. </div>
  782. </div>
  783. </div>
  784. {/* K Profile Select */}
  785. <div>
  786. <label className="block text-sm text-bambu-gray mb-2">
  787. {t('configureAmsSlot.kProfileLabel')}
  788. {selectedMaterial && (
  789. <span className="ml-2 text-xs text-bambu-blue">
  790. {t('configureAmsSlot.filteringFor', { material: selectedMaterial })}
  791. </span>
  792. )}
  793. </label>
  794. {matchingKProfiles.length > 0 ? (
  795. <div className="relative">
  796. <select
  797. value={selectedKProfile?.name || ''}
  798. onChange={(e) => {
  799. const profile = matchingKProfiles.find(p => p.name === e.target.value);
  800. setSelectedKProfile(profile || null);
  801. }}
  802. 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"
  803. >
  804. <option value="">{t('configureAmsSlot.noKProfile')}</option>
  805. {matchingKProfiles.map((profile) => (
  806. <option key={`${profile.name}-${profile.extruder_id}`} value={profile.name}>
  807. {profile.name} (K={profile.k_value})
  808. </option>
  809. ))}
  810. </select>
  811. <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
  812. </div>
  813. ) : selectedPresetId ? (
  814. <p className="text-sm text-bambu-gray italic py-2">
  815. {t('configureAmsSlot.noMatchingKProfiles')}
  816. </p>
  817. ) : (
  818. <span className="inline-block text-xs px-2 py-1 rounded bg-amber-500/20 text-amber-400 border border-amber-500/30">
  819. {t('configureAmsSlot.selectFilamentFirst')}
  820. </span>
  821. )}
  822. {selectedKProfile && (
  823. <p className="text-xs text-bambu-green mt-1">
  824. {t('configureAmsSlot.kFromCalibration', { value: selectedKProfile.k_value })}
  825. </p>
  826. )}
  827. </div>
  828. {/* Optional: Custom color */}
  829. <div>
  830. <label className="block text-sm text-bambu-gray mb-2">
  831. {t('configureAmsSlot.customColorLabel')}
  832. </label>
  833. {/* Catalog colors matching selected preset */}
  834. {catalogColors.length > 0 && (
  835. <div className="mb-3">
  836. <p className="text-xs text-bambu-gray mb-1.5">
  837. {t('configureAmsSlot.presetColors', { name: selectedPresetInfo?.fullName })}
  838. </p>
  839. <div className="flex flex-wrap gap-1.5">
  840. {catalogColors.map((entry) => (
  841. <button
  842. key={entry.id}
  843. onClick={() => {
  844. const hex = entry.hex_color.replace('#', '').toUpperCase();
  845. setColorHex(hex);
  846. setColorInput(entry.color_name);
  847. }}
  848. className={`h-7 px-2 rounded-md border-2 transition-all flex items-center gap-1.5 ${
  849. colorHex === entry.hex_color.replace('#', '').toUpperCase()
  850. ? 'border-bambu-green scale-105'
  851. : 'border-white/20 hover:border-white/40'
  852. }`}
  853. title={entry.color_name}
  854. >
  855. <span
  856. className="w-4 h-4 rounded-full border border-white/30 flex-shrink-0"
  857. style={{ backgroundColor: entry.hex_color }}
  858. />
  859. <span className="text-xs text-white/80 whitespace-nowrap">{entry.color_name}</span>
  860. </button>
  861. ))}
  862. </div>
  863. </div>
  864. )}
  865. {/* Quick color buttons */}
  866. <div className="flex flex-wrap gap-1.5 mb-2">
  867. {QUICK_COLORS_BASIC.map((color) => (
  868. <button
  869. key={color.hex}
  870. onClick={() => {
  871. setColorHex(color.hex);
  872. setColorInput(color.name);
  873. }}
  874. className={`w-7 h-7 rounded-md border-2 transition-all ${
  875. colorHex === color.hex
  876. ? 'border-bambu-green scale-110'
  877. : 'border-white/20 hover:border-white/40'
  878. }`}
  879. style={{ backgroundColor: `#${color.hex}` }}
  880. title={color.name}
  881. />
  882. ))}
  883. <button
  884. onClick={() => setShowExtendedColors(!showExtendedColors)}
  885. 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"
  886. title={showExtendedColors ? t('configureAmsSlot.showLessColors') : t('configureAmsSlot.showMoreColors')}
  887. >
  888. {showExtendedColors ? '−' : '+'}
  889. </button>
  890. </div>
  891. {/* Extended colors (collapsible) */}
  892. {showExtendedColors && (
  893. <div className="flex flex-wrap gap-1.5 mb-2">
  894. {QUICK_COLORS_EXTENDED.map((color) => (
  895. <button
  896. key={color.hex}
  897. onClick={() => {
  898. setColorHex(color.hex);
  899. setColorInput(color.name);
  900. }}
  901. className={`w-7 h-7 rounded-md border-2 transition-all ${
  902. colorHex === color.hex
  903. ? 'border-bambu-green scale-110'
  904. : 'border-white/20 hover:border-white/40'
  905. }`}
  906. style={{ backgroundColor: `#${color.hex}` }}
  907. title={color.name}
  908. />
  909. ))}
  910. </div>
  911. )}
  912. {/* Color input: name or hex */}
  913. <div className="flex gap-2 items-center">
  914. <div
  915. className="w-10 h-10 rounded-lg border-2 border-white/20 flex-shrink-0"
  916. style={{ backgroundColor: `#${displayColor}` }}
  917. />
  918. <input
  919. type="text"
  920. placeholder={t('configureAmsSlot.colorPlaceholder')}
  921. value={colorInput}
  922. onChange={(e) => {
  923. const input = e.target.value;
  924. setColorInput(input);
  925. // Try to parse as color name first
  926. const nameHex = colorNameToHex(input);
  927. if (nameHex) {
  928. setColorHex(nameHex);
  929. } else {
  930. // Try to parse as hex code
  931. const cleaned = input.replace(/[^0-9A-Fa-f]/g, '').toUpperCase();
  932. if (cleaned.length === 6) {
  933. setColorHex(cleaned);
  934. } else if (cleaned.length === 3) {
  935. // Expand shorthand hex (e.g., F00 -> FF0000)
  936. setColorHex(cleaned.split('').map(c => c + c).join(''));
  937. }
  938. }
  939. }}
  940. 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"
  941. />
  942. {colorHex && (
  943. <button
  944. onClick={() => {
  945. setColorHex('');
  946. setColorInput('');
  947. }}
  948. className="px-2 py-1 text-xs text-bambu-gray hover:text-white bg-bambu-dark-tertiary rounded"
  949. title={t('configureAmsSlot.clearCustomColor')}
  950. >
  951. {t('configureAmsSlot.clear')}
  952. </button>
  953. )}
  954. </div>
  955. {colorHex && (
  956. <p className="text-xs text-bambu-gray mt-1.5">
  957. {t('configureAmsSlot.hexLabel', { hex: colorHex })}
  958. </p>
  959. )}
  960. </div>
  961. </>
  962. )}
  963. </div>
  964. {/* Footer */}
  965. <div className="flex justify-between p-4 border-t border-bambu-dark-tertiary">
  966. {/* Reset button on the left */}
  967. <Button
  968. variant="secondary"
  969. onClick={() => resetMutation.mutate()}
  970. disabled={resetMutation.isPending || configureMutation.isPending}
  971. className="text-red-400 hover:text-red-300 hover:bg-red-500/10"
  972. >
  973. {resetMutation.isPending ? (
  974. <>
  975. <Loader2 className="w-4 h-4 animate-spin" />
  976. {t('configureAmsSlot.resetting')}
  977. </>
  978. ) : (
  979. <>
  980. <RotateCcw className="w-4 h-4" />
  981. {t('configureAmsSlot.resetSlot')}
  982. </>
  983. )}
  984. </Button>
  985. {/* Cancel and Configure buttons on the right */}
  986. <div className="flex gap-2">
  987. <Button variant="secondary" onClick={onClose}>
  988. {t('configureAmsSlot.cancel')}
  989. </Button>
  990. <Button
  991. onClick={() => configureMutation.mutate()}
  992. disabled={!canSave}
  993. >
  994. {configureMutation.isPending ? (
  995. <>
  996. <Loader2 className="w-4 h-4 animate-spin" />
  997. {t('configureAmsSlot.configuring')}
  998. </>
  999. ) : (
  1000. <>
  1001. <Settings2 className="w-4 h-4" />
  1002. {t('configureAmsSlot.configureSlot')}
  1003. </>
  1004. )}
  1005. </Button>
  1006. </div>
  1007. </div>
  1008. {/* Error */}
  1009. {(configureMutation.isError || resetMutation.isError) && (
  1010. <div className="mx-4 mb-4 p-2 bg-red-500/20 border border-red-500/50 rounded text-sm text-red-400">
  1011. {(configureMutation.error as Error)?.message || (resetMutation.error as Error)?.message}
  1012. </div>
  1013. )}
  1014. </div>
  1015. </div>
  1016. );
  1017. }