ConfigureAmsSlotModal.tsx 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. import { useState, useMemo, useEffect, useCallback, useRef } 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. fullScreen?: boolean;
  70. }
  71. // Known filament material types
  72. const MATERIAL_TYPES = ['PLA', 'PETG', 'PCTG', 'ABS', 'ASA', 'TPU', 'PC', 'PA', 'NYLON', 'PVA', 'HIPS', 'PP', 'PET'];
  73. // Extract filament type from preset name by finding known material type
  74. function parsePresetName(name: string): { material: string; brand: string; variant: string } {
  75. // Remove printer/nozzle suffix first
  76. const withoutSuffix = name.replace(/@.+$/, '').trim();
  77. const upperName = withoutSuffix.toUpperCase();
  78. // Handle "X Support for Y" pattern: the filament type is Y, not X.
  79. // e.g. "PLA Support for PETG PETG Basic" → material is PETG
  80. const supportMatch = upperName.match(/\bSUPPORT\s+FOR\s+/);
  81. if (supportMatch) {
  82. const afterSupport = upperName.slice(supportMatch.index! + supportMatch[0].length);
  83. for (const mat of MATERIAL_TYPES) {
  84. const regex = new RegExp(`\\b${mat}\\b`);
  85. if (regex.test(afterSupport)) {
  86. const brand = withoutSuffix.slice(0, supportMatch.index).trim();
  87. return { material: mat, brand, variant: 'Support' };
  88. }
  89. }
  90. }
  91. // Try to find a known material type in the name
  92. for (const mat of MATERIAL_TYPES) {
  93. // Use word boundary to match whole words only
  94. const regex = new RegExp(`\\b${mat}\\b`, 'i');
  95. if (regex.test(upperName)) {
  96. // Found material, extract brand (everything before material) and variant (after)
  97. const parts = withoutSuffix.split(regex);
  98. const brand = parts[0]?.trim() || '';
  99. const variant = parts[1]?.trim() || '';
  100. return { material: mat, brand, variant };
  101. }
  102. }
  103. // Fallback: assume first word is brand, second is material
  104. const parts = withoutSuffix.split(/\s+/);
  105. if (parts.length >= 2) {
  106. return { material: parts[1], brand: parts[0], variant: parts.slice(2).join(' ') };
  107. }
  108. return { material: withoutSuffix, brand: '', variant: '' };
  109. }
  110. // Check if a preset is a user preset (not built-in)
  111. function isUserPreset(settingId: string): boolean {
  112. // Built-in presets have specific patterns, user presets are UUIDs
  113. return !settingId.startsWith('GF') && !settingId.startsWith('P1');
  114. }
  115. // Common color name to hex mapping
  116. const COLOR_NAME_MAP: Record<string, string> = {
  117. // Basic colors
  118. 'white': 'FFFFFF',
  119. 'black': '000000',
  120. 'red': 'FF0000',
  121. 'green': '00FF00',
  122. 'blue': '0000FF',
  123. 'yellow': 'FFFF00',
  124. 'cyan': '00FFFF',
  125. 'magenta': 'FF00FF',
  126. 'orange': 'FFA500',
  127. 'purple': '800080',
  128. 'pink': 'FFC0CB',
  129. 'brown': '8B4513',
  130. 'gray': '808080',
  131. 'grey': '808080',
  132. // Filament-specific colors
  133. 'jade white': 'FFFEF2',
  134. 'ivory': 'FFFFF0',
  135. 'beige': 'F5F5DC',
  136. 'cream': 'FFFDD0',
  137. 'silver': 'C0C0C0',
  138. 'gold': 'FFD700',
  139. 'bronze': 'CD7F32',
  140. 'copper': 'B87333',
  141. 'navy': '000080',
  142. 'teal': '008080',
  143. 'olive': '808000',
  144. 'maroon': '800000',
  145. 'coral': 'FF7F50',
  146. 'salmon': 'FA8072',
  147. 'lime': '32CD32',
  148. 'mint': '98FF98',
  149. 'forest green': '228B22',
  150. 'sky blue': '87CEEB',
  151. 'royal blue': '4169E1',
  152. 'turquoise': '40E0D0',
  153. 'lavender': 'E6E6FA',
  154. 'violet': 'EE82EE',
  155. 'plum': 'DDA0DD',
  156. 'tan': 'D2B48C',
  157. 'chocolate': 'D2691E',
  158. 'charcoal': '36454F',
  159. 'slate': '708090',
  160. 'transparent': '000000', // Will need special handling
  161. 'natural': 'F5F5DC',
  162. 'wood': 'DEB887',
  163. };
  164. // Quick-select color presets (common filament colors)
  165. // Basic colors shown by default
  166. const QUICK_COLORS_BASIC = [
  167. { name: 'White', hex: 'FFFFFF' },
  168. { name: 'Black', hex: '000000' },
  169. { name: 'Red', hex: 'FF0000' },
  170. { name: 'Blue', hex: '0000FF' },
  171. { name: 'Green', hex: '00AA00' },
  172. { name: 'Yellow', hex: 'FFFF00' },
  173. { name: 'Orange', hex: 'FFA500' },
  174. { name: 'Gray', hex: '808080' },
  175. ];
  176. // Extended colors shown when expanded
  177. const QUICK_COLORS_EXTENDED = [
  178. { name: 'Cyan', hex: '00FFFF' },
  179. { name: 'Magenta', hex: 'FF00FF' },
  180. { name: 'Purple', hex: '800080' },
  181. { name: 'Pink', hex: 'FFC0CB' },
  182. { name: 'Brown', hex: '8B4513' },
  183. { name: 'Beige', hex: 'F5F5DC' },
  184. { name: 'Navy', hex: '000080' },
  185. { name: 'Teal', hex: '008080' },
  186. { name: 'Lime', hex: '32CD32' },
  187. { name: 'Gold', hex: 'FFD700' },
  188. { name: 'Silver', hex: 'C0C0C0' },
  189. { name: 'Maroon', hex: '800000' },
  190. { name: 'Olive', hex: '808000' },
  191. { name: 'Coral', hex: 'FF7F50' },
  192. { name: 'Salmon', hex: 'FA8072' },
  193. { name: 'Turquoise', hex: '40E0D0' },
  194. { name: 'Violet', hex: 'EE82EE' },
  195. { name: 'Indigo', hex: '4B0082' },
  196. { name: 'Chocolate', hex: 'D2691E' },
  197. { name: 'Tan', hex: 'D2B48C' },
  198. { name: 'Slate', hex: '708090' },
  199. { name: 'Charcoal', hex: '36454F' },
  200. { name: 'Ivory', hex: 'FFFFF0' },
  201. { name: 'Cream', hex: 'FFFDD0' },
  202. ];
  203. // Try to convert color name to hex
  204. function colorNameToHex(name: string): string | null {
  205. const normalized = name.toLowerCase().trim();
  206. return COLOR_NAME_MAP[normalized] || null;
  207. }
  208. // Extract printer model from preset name suffix "@BBL X1C 0.4 nozzle" → "X1C"
  209. function extractPresetModel(name: string): string | null {
  210. const atIdx = name.indexOf('@');
  211. if (atIdx < 0) return null;
  212. const suffix = name.slice(atIdx + 1).trim();
  213. const bblMatch = suffix.match(/^BBL\s+(.+?)(?:\s+[\d.]+\s*nozzle)?$/i);
  214. if (bblMatch) return bblMatch[1].trim();
  215. return null;
  216. }
  217. export function ConfigureAmsSlotModal({
  218. isOpen,
  219. onClose,
  220. printerId,
  221. slotInfo,
  222. nozzleDiameter = '0.4',
  223. printerModel,
  224. onSuccess,
  225. fullScreen,
  226. }: ConfigureAmsSlotModalProps) {
  227. const { t } = useTranslation();
  228. const [selectedPresetId, setSelectedPresetId] = useState<string>('');
  229. const [selectedKProfile, setSelectedKProfile] = useState<KProfile | null>(null);
  230. const [colorHex, setColorHex] = useState<string>(''); // Just the 6-char hex, no alpha
  231. const [colorInput, setColorInput] = useState<string>(''); // User's text input (name or hex)
  232. const [searchQuery, setSearchQuery] = useState('');
  233. const [showSuccess, setShowSuccess] = useState(false);
  234. const [showExtendedColors, setShowExtendedColors] = useState(false);
  235. const scrolledToRef = useRef<string>('');
  236. // Fetch cloud settings (gracefully handle 401 when logged out)
  237. const { data: cloudSettings, isLoading: settingsLoading, isError: cloudError } = useQuery({
  238. queryKey: ['cloudSettings'],
  239. queryFn: () => api.getCloudSettings(),
  240. enabled: isOpen,
  241. retry: false,
  242. });
  243. // Fetch local presets
  244. const { data: localPresets, isLoading: localLoading } = useQuery({
  245. queryKey: ['localPresets'],
  246. queryFn: () => api.getLocalPresets(),
  247. enabled: isOpen,
  248. });
  249. // Fetch built-in filament names (static fallback)
  250. const { data: builtinFilaments, isLoading: builtinLoading } = useQuery({
  251. queryKey: ['builtinFilaments'],
  252. queryFn: () => api.getBuiltinFilaments(),
  253. enabled: isOpen,
  254. staleTime: Infinity,
  255. });
  256. // Fetch K profiles
  257. const { data: kprofilesData, isLoading: kprofilesLoading } = useQuery({
  258. queryKey: ['kprofiles', printerId, nozzleDiameter],
  259. queryFn: () => api.getKProfiles(printerId, nozzleDiameter),
  260. enabled: isOpen && !!printerId,
  261. });
  262. // Fetch color catalog
  263. const { data: colorCatalog } = useQuery({
  264. queryKey: ['colorCatalog'],
  265. queryFn: () => api.getColorCatalog(),
  266. enabled: isOpen,
  267. staleTime: Infinity,
  268. });
  269. // Configure slot mutation
  270. const configureMutation = useMutation({
  271. mutationFn: async () => {
  272. if (!selectedPresetId) throw new Error('No filament preset selected');
  273. // Determine preset source
  274. const isLocal = selectedPresetId.startsWith('local_');
  275. const isBuiltin = selectedPresetId.startsWith('builtin_');
  276. const localId = isLocal ? parseInt(selectedPresetId.replace('local_', ''), 10) : null;
  277. const builtinFilamentId = isBuiltin ? selectedPresetId.replace('builtin_', '') : null;
  278. const localPreset = isLocal
  279. ? localPresets?.filament.find(p => p.id === localId)
  280. : null;
  281. const builtinPreset = isBuiltin
  282. ? builtinFilaments?.find(b => b.filament_id === builtinFilamentId)
  283. : null;
  284. // Get the selected cloud preset details (null for local/builtin presets)
  285. const selectedPreset = (!isLocal && !isBuiltin)
  286. ? cloudSettings?.filament.find(p => p.setting_id === selectedPresetId)
  287. : null;
  288. if (!isLocal && !isBuiltin && !selectedPreset) throw new Error('Selected preset not found');
  289. if (isLocal && !localPreset) throw new Error('Selected local preset not found');
  290. if (isBuiltin && !builtinPreset) throw new Error('Selected builtin preset not found');
  291. // Parse the preset name for filament info
  292. const presetName = isLocal ? localPreset!.name : isBuiltin ? builtinPreset!.name : selectedPreset!.name;
  293. const parsed = parsePresetName(presetName);
  294. // Get cali_idx from selected K profile's slot_id (-1 = use default 0.020)
  295. const caliIdx = selectedKProfile?.slot_id ?? -1;
  296. // Use custom color if set, otherwise use current slot color or default
  297. const color = colorHex || slotInfo.trayColor?.slice(0, 6) || 'FFFFFF';
  298. // Create the tray_sub_brands from preset name (without printer/nozzle suffix)
  299. const traySubBrands = presetName.replace(/@.+$/, '').trim();
  300. let trayInfoIdx: string;
  301. let settingId: string;
  302. // Parsed material from preset name — handles "Support for" patterns correctly.
  303. // Prefer this over stored filament_type which may have been parsed with old logic.
  304. const parsedMat = parsed.material.toUpperCase();
  305. if (isLocal) {
  306. // Local presets have no Bambu Cloud setting_id, but need a valid
  307. // tray_info_idx for the printer to recognize the filament type.
  308. // Map the material type to the closest generic Bambu filament ID.
  309. const material = (MATERIAL_TYPES.includes(parsedMat) ? parsedMat : localPreset?.filament_type || parsed.material || '').toUpperCase();
  310. const GENERIC_IDS: Record<string, string> = {
  311. 'PLA': 'GFL99', 'PLA-CF': 'GFL98', 'PLA SILK': 'GFL96', 'PLA HIGH SPEED': 'GFL95',
  312. 'PETG': 'GFG99', 'PETG HF': 'GFG96', 'PETG-CF': 'GFG98', 'PCTG': 'GFG97',
  313. 'ABS': 'GFB99', 'ASA': 'GFB98',
  314. 'PC': 'GFC99',
  315. 'PA': 'GFN99', 'PA-CF': 'GFN98', 'NYLON': 'GFN99',
  316. 'TPU': 'GFU99',
  317. 'PVA': 'GFS99', 'HIPS': 'GFS98',
  318. 'PE': 'GFP99', 'PP': 'GFP97',
  319. };
  320. // Try exact match first, then base material (strip suffixes like "-CF", "+", " HF")
  321. trayInfoIdx = GENERIC_IDS[material]
  322. || GENERIC_IDS[material.replace(/[-\s]?CF$/, '')]
  323. || GENERIC_IDS[material.replace(/\+$/, '')]
  324. || GENERIC_IDS[material.split(/[-\s]/)[0]]
  325. || '';
  326. settingId = '';
  327. } else if (isBuiltin) {
  328. // Built-in presets use the filament_id directly as tray_info_idx
  329. trayInfoIdx = builtinFilamentId!;
  330. settingId = '';
  331. } else {
  332. trayInfoIdx = convertToTrayInfoIdx(selectedPresetId);
  333. settingId = selectedPresetId;
  334. // User cloud presets may carry a distinct filament_id in the cloud detail
  335. // (e.g. "P285e239"); prefer it when present. Never fall back to base_id —
  336. // that collapses custom presets to the inherited generic's filament_id and
  337. // makes the slicer resolve the slot to "Generic …" instead (#1053).
  338. if (!selectedPresetId.startsWith('GFS')) {
  339. try {
  340. const detail = await api.getCloudSettingDetail(selectedPresetId);
  341. if (detail.filament_id) {
  342. trayInfoIdx = detail.filament_id;
  343. }
  344. } catch (e) {
  345. console.warn('Failed to fetch preset detail for filament_id:', e);
  346. }
  347. }
  348. }
  349. // Default temp range — use local preset core fields if available
  350. let tempMin = isLocal && localPreset?.nozzle_temp_min ? localPreset.nozzle_temp_min : 190;
  351. let tempMax = isLocal && localPreset?.nozzle_temp_max ? localPreset.nozzle_temp_max : 230;
  352. if (!isLocal || isBuiltin || (!localPreset?.nozzle_temp_min && !localPreset?.nozzle_temp_max)) {
  353. // Fall back to material-based defaults (prefer parsed material for "Support for" handling)
  354. const material = (isLocal
  355. ? (MATERIAL_TYPES.includes(parsedMat) ? parsedMat : localPreset?.filament_type || parsed.material || '')
  356. : parsed.material).toUpperCase();
  357. if (material.includes('PLA')) {
  358. tempMin = 190;
  359. tempMax = 230;
  360. } else if (material.includes('PETG')) {
  361. tempMin = 220;
  362. tempMax = 260;
  363. } else if (material.includes('ABS')) {
  364. tempMin = 240;
  365. tempMax = 280;
  366. } else if (material.includes('ASA')) {
  367. tempMin = 240;
  368. tempMax = 280;
  369. } else if (material.includes('TPU')) {
  370. tempMin = 200;
  371. tempMax = 240;
  372. } else if (material === 'PCTG') {
  373. tempMin = 220;
  374. tempMax = 260;
  375. } else if (material.includes('PC')) {
  376. tempMin = 260;
  377. tempMax = 300;
  378. } else if (material.includes('PA') || material.includes('NYLON')) {
  379. tempMin = 250;
  380. tempMax = 290;
  381. }
  382. }
  383. // Parse K value from selected profile
  384. const kValue = selectedKProfile?.k_value ? parseFloat(selectedKProfile.k_value) : 0;
  385. // Determine tray_type: prefer parsed material from preset name (handles "Support for"
  386. // patterns correctly) over stored filament_type which may have been parsed with old logic.
  387. const trayType = isLocal
  388. ? (MATERIAL_TYPES.includes(parsedMat) ? parsedMat : localPreset?.filament_type || parsed.material || 'PLA')
  389. : (parsed.material || 'PLA');
  390. // Configure the slot via MQTT
  391. const result = await api.configureAmsSlot(printerId, slotInfo.amsId, slotInfo.trayId, {
  392. tray_info_idx: trayInfoIdx,
  393. tray_type: trayType,
  394. tray_sub_brands: traySubBrands,
  395. tray_color: color + 'FF', // Add alpha
  396. nozzle_temp_min: tempMin,
  397. nozzle_temp_max: tempMax,
  398. cali_idx: caliIdx,
  399. nozzle_diameter: nozzleDiameter,
  400. setting_id: settingId, // Full setting ID for slicer compatibility (empty for local)
  401. // Pass K profile's filament_id and setting_id for proper linking
  402. kprofile_filament_id: selectedKProfile?.filament_id,
  403. kprofile_setting_id: selectedKProfile?.setting_id || undefined,
  404. // Also pass the K value directly for extrusion_cali_set command
  405. k_value: kValue,
  406. });
  407. // Save the preset mapping so we can display the correct name in the UI
  408. // This is needed because user presets use filament_id (e.g., P285e239) as tray_info_idx,
  409. // which can't be resolved to a name via the filamentInfo API
  410. const mappingPresetId = isLocal ? `local_${localId}` : isBuiltin ? `builtin_${builtinFilamentId}` : selectedPresetId;
  411. const mappingSource = isLocal ? 'local' : isBuiltin ? 'builtin' : 'cloud';
  412. try {
  413. await api.saveSlotPreset(printerId, slotInfo.amsId, slotInfo.trayId, mappingPresetId, traySubBrands, mappingSource);
  414. } catch (e) {
  415. console.warn('Failed to save slot preset mapping:', e);
  416. // Don't fail the whole operation - slot was configured successfully
  417. }
  418. return result;
  419. },
  420. onSuccess: () => {
  421. setShowSuccess(true);
  422. onSuccess?.();
  423. // Close after showing success briefly
  424. setTimeout(() => {
  425. setShowSuccess(false);
  426. onClose();
  427. }, 1500);
  428. },
  429. });
  430. // Reset slot mutation
  431. const resetMutation = useMutation({
  432. mutationFn: async () => {
  433. return api.resetAmsSlot(printerId, slotInfo.amsId, slotInfo.trayId);
  434. },
  435. onSuccess: () => {
  436. setShowSuccess(true);
  437. onSuccess?.();
  438. setTimeout(() => {
  439. setShowSuccess(false);
  440. onClose();
  441. }, 1500);
  442. },
  443. });
  444. // Unified preset item for the list (cloud + local + builtin fallback)
  445. type PresetItem = { id: string; name: string; source: 'cloud' | 'local' | 'builtin'; isUser: boolean };
  446. // Filter filament presets based on search (merged cloud + local + builtin)
  447. const filteredPresets = useMemo(() => {
  448. const query = searchQuery.toLowerCase();
  449. const items: PresetItem[] = [];
  450. // Collect IDs already covered by cloud and local to avoid duplicates in fallback
  451. const coveredIds = new Set<string>();
  452. // Currently-configured preset should always be shown (bypass model filter)
  453. const savedId = slotInfo.savedPresetId;
  454. const trayIdx = slotInfo.trayInfoIdx;
  455. // 1. Cloud presets
  456. if (cloudSettings?.filament) {
  457. for (const cp of cloudSettings.filament) {
  458. coveredIds.add(cp.setting_id);
  459. // Keep preset if it matches the slot's saved mapping or current tray_info_idx
  460. const isSavedPreset = savedId === cp.setting_id;
  461. const isCurrentPreset = isSavedPreset
  462. || (trayIdx && (cp.setting_id === trayIdx || convertToTrayInfoIdx(cp.setting_id) === trayIdx));
  463. // Search filter applies to ALL presets (including saved) — no bypass
  464. if (query && !cp.name.toLowerCase().includes(query)) continue;
  465. // Filter by printer model if set (skip for current preset)
  466. if (!isCurrentPreset && printerModel) {
  467. const presetModel = extractPresetModel(cp.name);
  468. if (presetModel && presetModel.toUpperCase() !== printerModel.toUpperCase()) continue;
  469. }
  470. items.push({ id: cp.setting_id, name: cp.name, source: 'cloud', isUser: isUserPreset(cp.setting_id) });
  471. }
  472. }
  473. // 2. Local presets (always shown — user-imported profiles work on any printer)
  474. if (localPresets?.filament) {
  475. for (const lp of localPresets.filament) {
  476. const localId = `local_${lp.id}`;
  477. if (query && !lp.name.toLowerCase().includes(query)) continue;
  478. items.push({ id: localId, name: lp.name, source: 'local', isUser: false });
  479. }
  480. }
  481. // 3. Built-in filament names (fallback — only add entries not already covered)
  482. if (builtinFilaments) {
  483. for (const bf of builtinFilaments) {
  484. if (coveredIds.has(bf.filament_id)) continue;
  485. // Convert filament_id to setting_id format for cloud compatibility
  486. // e.g. "GFA00" → cloud setting_id would be "GFSA00" (insert S after GF)
  487. const settingId = bf.filament_id.startsWith('GF')
  488. ? 'GFS' + bf.filament_id.slice(2)
  489. : bf.filament_id;
  490. if (coveredIds.has(settingId)) continue;
  491. if (!query || bf.name.toLowerCase().includes(query)) {
  492. items.push({ id: `builtin_${bf.filament_id}`, name: bf.name, source: 'builtin', isUser: false });
  493. }
  494. }
  495. }
  496. // Sort: cloud user presets first, then cloud built-in, then local, then builtin fallback
  497. return items.sort((a, b) => {
  498. const sourceOrder = { cloud: 0, local: 1, builtin: 2 };
  499. if (a.source !== b.source) return sourceOrder[a.source] - sourceOrder[b.source];
  500. if (a.isUser && !b.isUser) return -1;
  501. if (!a.isUser && b.isUser) return 1;
  502. return a.name.localeCompare(b.name);
  503. });
  504. }, [cloudSettings?.filament, localPresets?.filament, builtinFilaments, searchQuery, printerModel, slotInfo.savedPresetId, slotInfo.trayInfoIdx]);
  505. // Get full preset name for K profile filtering (brand + material, without printer suffix)
  506. const selectedPresetInfo = useMemo(() => {
  507. if (!selectedPresetId) return null;
  508. // Resolve the name from cloud, local, or builtin presets
  509. let presetName: string | null = null;
  510. if (selectedPresetId.startsWith('local_')) {
  511. const localId = parseInt(selectedPresetId.replace('local_', ''), 10);
  512. const lp = localPresets?.filament.find(p => p.id === localId);
  513. presetName = lp?.name || null;
  514. } else if (selectedPresetId.startsWith('builtin_')) {
  515. const filamentId = selectedPresetId.replace('builtin_', '');
  516. const bf = builtinFilaments?.find(b => b.filament_id === filamentId);
  517. presetName = bf?.name || null;
  518. } else if (cloudSettings?.filament) {
  519. const cp = cloudSettings.filament.find(p => p.setting_id === selectedPresetId);
  520. presetName = cp?.name || null;
  521. } else {
  522. // No cloud settings available
  523. }
  524. if (!presetName) {
  525. return null;
  526. }
  527. // Remove printer/nozzle suffix (e.g., "@BBL X1C" or "@0.4 nozzle")
  528. let nameWithoutSuffix = presetName.replace(/@.+$/, '').trim();
  529. // Strip leading "# " from custom preset names (user convention)
  530. if (nameWithoutSuffix.startsWith('# ')) {
  531. nameWithoutSuffix = nameWithoutSuffix.slice(2).trim();
  532. }
  533. const parsed = parsePresetName(nameWithoutSuffix);
  534. return {
  535. fullName: nameWithoutSuffix,
  536. material: parsed.material,
  537. brand: parsed.brand,
  538. };
  539. }, [selectedPresetId, cloudSettings?.filament, localPresets?.filament, builtinFilaments]);
  540. // For backwards compatibility with the label
  541. const selectedMaterial = selectedPresetInfo?.fullName || '';
  542. // Filter color catalog entries matching the selected preset's brand + material
  543. const catalogColors = useMemo(() => {
  544. if (!colorCatalog || !selectedPresetInfo) return [];
  545. const { fullName, brand } = selectedPresetInfo;
  546. // Try to find colors matching the full preset name (e.g., "PLA Metal")
  547. // The catalog uses the variant as part of the material field (e.g., material="PLA Metal")
  548. // Extract the full material+variant from the preset name
  549. const materialVariant = fullName.replace(/^(Bambu\s*(Lab)?|eSUN|Polymaker|Overture|Sunlu|Hatchbox)\s*/i, '').trim();
  550. return colorCatalog.filter(entry => {
  551. const entryMaterial = (entry.material || '').toUpperCase();
  552. const entryManufacturer = entry.manufacturer.toUpperCase();
  553. // Match material: try full material+variant first, then just material type
  554. const materialMatch = entryMaterial === materialVariant.toUpperCase()
  555. || entryMaterial.includes(materialVariant.toUpperCase())
  556. || materialVariant.toUpperCase().includes(entryMaterial);
  557. if (!materialMatch) return false;
  558. // If brand is present, also match manufacturer
  559. if (brand) {
  560. const upperBrand = brand.toUpperCase();
  561. // Fuzzy match: "Bambu" matches "Bambu Lab", etc.
  562. if (!entryManufacturer.includes(upperBrand) && !upperBrand.includes(entryManufacturer)) {
  563. return false;
  564. }
  565. }
  566. return true;
  567. });
  568. }, [colorCatalog, selectedPresetInfo]);
  569. const matchingKProfiles = useMemo(() => {
  570. if (!kprofilesData?.profiles || !selectedPresetInfo) return [];
  571. const { fullName, material, brand } = selectedPresetInfo;
  572. const upperFullName = fullName.toUpperCase();
  573. const upperMaterial = material.toUpperCase();
  574. const upperBrand = brand.toUpperCase();
  575. // Material must be at least 2 chars to avoid false positives
  576. if (!upperMaterial || upperMaterial.length < 2) return [];
  577. // Filter profiles - require brand match if brand is present in selected preset
  578. const filtered = kprofilesData.profiles.filter(p => {
  579. const profileName = p.name.toUpperCase();
  580. // If the selected preset has a brand (e.g., "Azurefilm PLA Wood"),
  581. // only show profiles that match the brand
  582. if (upperBrand) {
  583. // Must contain the brand name
  584. if (!profileName.includes(upperBrand)) {
  585. return false;
  586. }
  587. // And must contain the material type
  588. if (!profileName.includes(upperMaterial)) {
  589. return false;
  590. }
  591. return true;
  592. }
  593. // No brand in selected preset - match on full name or material
  594. // Priority 1: Exact match with full name
  595. if (profileName.includes(upperFullName)) {
  596. return true;
  597. }
  598. // Priority 2: Material type match (only when no brand specified)
  599. if (profileName.includes(upperMaterial)) {
  600. return true;
  601. }
  602. // Check for common material aliases
  603. const aliases: Record<string, string[]> = {
  604. 'NYLON': ['PA', 'PA-CF', 'PA6'],
  605. 'PA': ['NYLON'],
  606. };
  607. const materialAliases = aliases[upperMaterial] || [];
  608. for (const alias of materialAliases) {
  609. if (profileName.includes(alias)) {
  610. return true;
  611. }
  612. }
  613. return false;
  614. });
  615. // Deduplicate profiles with same name and k_value (multi-nozzle printers have duplicates)
  616. // Prefer the profile matching the slot's extruder (e.g. ext-R uses extruder 0, ext-L uses extruder 1)
  617. const seen = new Map<string, KProfile>();
  618. for (const profile of filtered) {
  619. const key = `${profile.name}|${profile.k_value}`;
  620. const existing = seen.get(key);
  621. if (!existing) {
  622. seen.set(key, profile);
  623. } else if (slotInfo.extruderId !== undefined && profile.extruder_id === slotInfo.extruderId && existing.extruder_id !== slotInfo.extruderId) {
  624. // Replace with profile matching slot's extruder
  625. seen.set(key, profile);
  626. }
  627. }
  628. return Array.from(seen.values());
  629. }, [kprofilesData?.profiles, selectedPresetInfo, slotInfo.extruderId]);
  630. // Pre-select current profile when modal opens, reset when closes
  631. useEffect(() => {
  632. if (isOpen) {
  633. // Pre-populate from saved preset mapping (most reliable)
  634. if (slotInfo.savedPresetId) {
  635. setSelectedPresetId(slotInfo.savedPresetId);
  636. } else if (slotInfo.trayInfoIdx && cloudSettings?.filament) {
  637. // Fallback: try to match by tray_info_idx in cloud presets
  638. // First try exact match on setting_id
  639. let currentPreset = cloudSettings.filament.find(
  640. p => p.setting_id === slotInfo.trayInfoIdx
  641. );
  642. // Then try matching by converting setting_id → filament_id format
  643. if (!currentPreset) {
  644. currentPreset = cloudSettings.filament.find(
  645. p => convertToTrayInfoIdx(p.setting_id) === slotInfo.trayInfoIdx
  646. );
  647. }
  648. if (currentPreset) {
  649. setSelectedPresetId(currentPreset.setting_id);
  650. }
  651. } else if (slotInfo.trayInfoIdx && builtinFilaments?.length) {
  652. // Last resort: match trayInfoIdx against builtin presets
  653. const trayIdx = slotInfo.trayInfoIdx;
  654. const match = builtinFilaments.find(bf => bf.filament_id === trayIdx);
  655. if (match) {
  656. setSelectedPresetId(`builtin_${match.filament_id}`);
  657. }
  658. }
  659. // Pre-populate color from current slot (black is valid — empty slots don't pass trayColor)
  660. if (slotInfo.trayColor) {
  661. const hex = slotInfo.trayColor.slice(0, 6);
  662. if (hex) {
  663. setColorHex(hex);
  664. }
  665. }
  666. } else {
  667. // Reset when modal closes
  668. setSelectedPresetId('');
  669. setSelectedKProfile(null);
  670. setColorHex('');
  671. setColorInput('');
  672. setSearchQuery('');
  673. setShowSuccess(false);
  674. scrolledToRef.current = '';
  675. }
  676. }, [isOpen, slotInfo.savedPresetId, slotInfo.trayInfoIdx, slotInfo.trayColor, cloudSettings?.filament, builtinFilaments]);
  677. // Auto-select best matching K profile when preset changes
  678. useEffect(() => {
  679. if (matchingKProfiles.length > 0) {
  680. // Prefer the currently-active K-profile (by cali_idx) if available
  681. if (slotInfo.caliIdx != null && slotInfo.caliIdx > 0) {
  682. const active = matchingKProfiles.find(p => p.slot_id === slotInfo.caliIdx);
  683. if (active) {
  684. setSelectedKProfile(active);
  685. return;
  686. }
  687. }
  688. // Fallback: first matching profile
  689. setSelectedKProfile(matchingKProfiles[0]);
  690. } else {
  691. setSelectedKProfile(null);
  692. }
  693. }, [selectedPresetId, matchingKProfiles, slotInfo.caliIdx]);
  694. // Escape key handler
  695. const handleKeyDown = useCallback((e: KeyboardEvent) => {
  696. if (e.key === 'Escape') {
  697. onClose();
  698. }
  699. }, [onClose]);
  700. useEffect(() => {
  701. if (isOpen) {
  702. document.addEventListener('keydown', handleKeyDown);
  703. return () => document.removeEventListener('keydown', handleKeyDown);
  704. }
  705. }, [isOpen, handleKeyDown]);
  706. const isLoading = (settingsLoading && !cloudError) || localLoading || builtinLoading || kprofilesLoading;
  707. // Scroll selected preset into view when data finishes loading or the selection changes.
  708. // Uses a ref guard so scrollIntoView only fires once per selection, preventing the
  709. // infinite scroll loop that occurred on Windows with inline callback refs.
  710. useEffect(() => {
  711. if (!isLoading && selectedPresetId && selectedPresetId !== scrolledToRef.current) {
  712. const raf = requestAnimationFrame(() => {
  713. const modal = document.querySelector('[class*="fixed inset-0 z-50"]');
  714. const el = modal?.querySelector(`[data-preset-id="${CSS.escape(selectedPresetId)}"]`);
  715. if (el) {
  716. scrolledToRef.current = selectedPresetId;
  717. el.scrollIntoView({ block: 'nearest' });
  718. }
  719. });
  720. return () => cancelAnimationFrame(raf);
  721. }
  722. }, [selectedPresetId, isLoading]);
  723. if (!isOpen) return null;
  724. const canSave = selectedPresetId && !configureMutation.isPending;
  725. // Get display color (custom or slot default)
  726. const displayColor = colorHex || slotInfo.trayColor?.slice(0, 6) || 'FFFFFF';
  727. return (
  728. <div className={`fixed inset-0 z-50 flex ${fullScreen ? '' : 'items-center justify-center'}`}>
  729. {/* Backdrop */}
  730. {!fullScreen && (
  731. <div
  732. className="absolute inset-0 bg-black/60 backdrop-blur-sm"
  733. onClick={onClose}
  734. />
  735. )}
  736. {/* Modal */}
  737. <div className={fullScreen
  738. ? 'relative w-full h-full bg-bambu-dark-secondary flex flex-col'
  739. : 'relative w-full max-w-lg mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl'
  740. }>
  741. {/* Header */}
  742. <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary shrink-0">
  743. <div className="flex items-center gap-2">
  744. <Settings2 className="w-5 h-5 text-bambu-blue" />
  745. <h2 className="text-lg font-semibold text-white">{t('configureAmsSlot.title')}</h2>
  746. {/* Inline slot info in fullScreen mode */}
  747. {fullScreen && (
  748. <div className="flex items-center gap-2 ml-4 text-sm text-bambu-gray">
  749. <span className="text-white/30">|</span>
  750. {slotInfo.trayColor && (
  751. <span
  752. className="w-4 h-4 rounded-full border border-black/20"
  753. style={{ backgroundColor: `#${slotInfo.trayColor.slice(0, 6)}` }}
  754. />
  755. )}
  756. <span className="text-white/70">
  757. {t('configureAmsSlot.slotLabel', { ams: getAmsLabel(slotInfo.amsId, slotInfo.trayCount), slot: slotInfo.trayId + 1 })}
  758. </span>
  759. {slotInfo.traySubBrands && (
  760. <span>({slotInfo.traySubBrands})</span>
  761. )}
  762. </div>
  763. )}
  764. </div>
  765. <button
  766. onClick={onClose}
  767. className="p-1 text-bambu-gray hover:text-white rounded transition-colors"
  768. >
  769. <X className="w-5 h-5" />
  770. </button>
  771. </div>
  772. {/* Content */}
  773. <div className={`p-4 overflow-y-auto ${fullScreen ? 'flex-1 min-h-0' : 'space-y-4 max-h-[60vh]'}`}>
  774. {/* Success overlay */}
  775. {showSuccess && (
  776. <div className="absolute inset-0 bg-bambu-dark-secondary/95 z-10 flex items-center justify-center rounded-xl">
  777. <div className="text-center space-y-3">
  778. <CheckCircle2 className="w-16 h-16 text-bambu-green mx-auto" />
  779. <p className="text-lg font-semibold text-white">{t('configureAmsSlot.slotConfigured')}</p>
  780. <p className="text-sm text-bambu-gray">{t('configureAmsSlot.settingsSentToPrinter')}</p>
  781. </div>
  782. </div>
  783. )}
  784. {/* Slot info */}
  785. {!fullScreen && (
  786. <div className="p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary">
  787. <p className="text-xs text-bambu-gray mb-1">{t('configureAmsSlot.configuringSlot')}</p>
  788. <div className="flex items-center gap-2">
  789. {slotInfo.trayColor && (
  790. <span
  791. className="w-4 h-4 rounded-full border border-black/20"
  792. style={{ backgroundColor: `#${slotInfo.trayColor.slice(0, 6)}` }}
  793. />
  794. )}
  795. <span className="text-white font-medium">
  796. {t('configureAmsSlot.slotLabel', { ams: getAmsLabel(slotInfo.amsId, slotInfo.trayCount), slot: slotInfo.trayId + 1 })}
  797. </span>
  798. {slotInfo.traySubBrands && (
  799. <span className="text-bambu-gray">({slotInfo.traySubBrands})</span>
  800. )}
  801. </div>
  802. </div>
  803. )}
  804. {isLoading ? (
  805. <div className="flex justify-center py-8">
  806. <Loader2 className="w-6 h-6 text-bambu-green animate-spin" />
  807. </div>
  808. ) : fullScreen ? (
  809. /* Two-column layout for kiosk display */
  810. <div className="flex gap-4 h-full">
  811. {/* Left column: Filament preset list (takes full height) */}
  812. <div className="w-1/2 flex flex-col min-h-0">
  813. <label className="block text-sm text-bambu-gray mb-2">
  814. {t('configureAmsSlot.filamentProfile')} <span className="text-red-400">*</span>
  815. </label>
  816. <input
  817. type="text"
  818. placeholder={t('configureAmsSlot.searchPresets')}
  819. value={searchQuery}
  820. onChange={(e) => setSearchQuery(e.target.value)}
  821. 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 shrink-0"
  822. />
  823. <div className="flex-1 min-h-0 overflow-y-auto space-y-1">
  824. {filteredPresets.length === 0 ? (
  825. <p className="text-center py-4 text-bambu-gray">
  826. {(cloudSettings?.filament?.length === 0 && !localPresets?.filament?.length)
  827. ? t('configureAmsSlot.noPresetsAvailable')
  828. : t('configureAmsSlot.noMatchingPresets')}
  829. </p>
  830. ) : (
  831. filteredPresets.map((preset) => (
  832. <button
  833. key={preset.id}
  834. data-preset-id={preset.id}
  835. onClick={() => setSelectedPresetId(preset.id)}
  836. className={`w-full p-2 rounded-lg border text-left transition-colors ${
  837. selectedPresetId === preset.id
  838. ? 'bg-bambu-green/20 border-bambu-green'
  839. : 'bg-bambu-dark border-bambu-dark-tertiary hover:border-bambu-gray'
  840. }`}
  841. >
  842. <div className="flex items-center justify-between">
  843. <span className="text-white text-sm truncate">{preset.name}</span>
  844. <div className="flex items-center gap-1 flex-shrink-0">
  845. {preset.source === 'local' && (
  846. <span className="text-xs px-1.5 py-0.5 rounded bg-green-500/20 text-green-400">
  847. {t('profiles.localProfiles.badge')}
  848. </span>
  849. )}
  850. {preset.source === 'builtin' && (
  851. <span className="text-xs px-1.5 py-0.5 rounded bg-amber-500/20 text-amber-400">
  852. {t('configureAmsSlot.builtin')}
  853. </span>
  854. )}
  855. {preset.isUser && (
  856. <span className="text-xs px-1.5 py-0.5 rounded bg-bambu-blue/20 text-bambu-blue">
  857. {t('configureAmsSlot.custom')}
  858. </span>
  859. )}
  860. </div>
  861. </div>
  862. </button>
  863. ))
  864. )}
  865. </div>
  866. </div>
  867. {/* Right column: K Profile + Color */}
  868. <div className="w-1/2 flex flex-col gap-4 min-h-0 overflow-y-auto">
  869. {/* K Profile Select */}
  870. <div>
  871. <label className="block text-sm text-bambu-gray mb-2">
  872. {t('configureAmsSlot.kProfileLabel')}
  873. {selectedMaterial && (
  874. <span className="ml-2 text-xs text-bambu-blue">
  875. {t('configureAmsSlot.filteringFor', { material: selectedMaterial })}
  876. </span>
  877. )}
  878. </label>
  879. {matchingKProfiles.length > 0 ? (
  880. <div className="relative">
  881. <select
  882. value={selectedKProfile?.name || ''}
  883. onChange={(e) => {
  884. const profile = matchingKProfiles.find(p => p.name === e.target.value);
  885. setSelectedKProfile(profile || null);
  886. }}
  887. 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"
  888. >
  889. <option value="">{t('configureAmsSlot.noKProfile')}</option>
  890. {matchingKProfiles.map((profile) => (
  891. <option key={`${profile.name}-${profile.extruder_id}`} value={profile.name}>
  892. {profile.name} (K={profile.k_value})
  893. </option>
  894. ))}
  895. </select>
  896. <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
  897. </div>
  898. ) : selectedPresetId ? (
  899. <p className="text-sm text-bambu-gray italic py-2">
  900. {t('configureAmsSlot.noMatchingKProfiles')}
  901. </p>
  902. ) : (
  903. <span className="inline-block text-xs px-2 py-1 rounded bg-amber-500/20 text-amber-400 border border-amber-500/30">
  904. {t('configureAmsSlot.selectFilamentFirst')}
  905. </span>
  906. )}
  907. {selectedKProfile && (
  908. <p className="text-xs text-bambu-green mt-1">
  909. {t('configureAmsSlot.kFromCalibration', { value: selectedKProfile.k_value })}
  910. </p>
  911. )}
  912. </div>
  913. {/* Custom color */}
  914. <div>
  915. <label className="block text-sm text-bambu-gray mb-2">
  916. {t('configureAmsSlot.customColorLabel')}
  917. </label>
  918. {catalogColors.length > 0 && (
  919. <div className="mb-3">
  920. <p className="text-xs text-bambu-gray mb-1.5">
  921. {t('configureAmsSlot.presetColors', { name: selectedPresetInfo?.fullName })}
  922. </p>
  923. <div className="flex flex-wrap gap-1.5">
  924. {catalogColors.map((entry) => (
  925. <button
  926. key={entry.id}
  927. onClick={() => {
  928. const hex = entry.hex_color.replace('#', '').toUpperCase();
  929. setColorHex(hex);
  930. setColorInput(entry.color_name);
  931. }}
  932. className={`h-7 px-2 rounded-md border-2 transition-all flex items-center gap-1.5 ${
  933. colorHex === entry.hex_color.replace('#', '').toUpperCase()
  934. ? 'border-bambu-green scale-105'
  935. : 'border-white/20 hover:border-white/40'
  936. }`}
  937. title={entry.color_name}
  938. >
  939. <span
  940. className="w-4 h-4 rounded-full border border-black/20 flex-shrink-0"
  941. style={{ backgroundColor: entry.hex_color }}
  942. />
  943. <span className="text-xs text-white/80 whitespace-nowrap">{entry.color_name}</span>
  944. </button>
  945. ))}
  946. </div>
  947. </div>
  948. )}
  949. <div className="flex flex-wrap gap-1.5 mb-2">
  950. {QUICK_COLORS_BASIC.map((color) => (
  951. <button
  952. key={color.hex}
  953. onClick={() => {
  954. setColorHex(color.hex);
  955. setColorInput(color.name);
  956. }}
  957. className={`w-7 h-7 rounded-md border-2 transition-all ${
  958. colorHex === color.hex
  959. ? 'border-bambu-green scale-110'
  960. : 'border-white/20 hover:border-white/40'
  961. }`}
  962. style={{ backgroundColor: `#${color.hex}` }}
  963. title={color.name}
  964. />
  965. ))}
  966. <button
  967. onClick={() => setShowExtendedColors(!showExtendedColors)}
  968. 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"
  969. title={showExtendedColors ? t('configureAmsSlot.showLessColors') : t('configureAmsSlot.showMoreColors')}
  970. >
  971. {showExtendedColors ? '−' : '+'}
  972. </button>
  973. </div>
  974. {showExtendedColors && (
  975. <div className="flex flex-wrap gap-1.5 mb-2">
  976. {QUICK_COLORS_EXTENDED.map((color) => (
  977. <button
  978. key={color.hex}
  979. onClick={() => {
  980. setColorHex(color.hex);
  981. setColorInput(color.name);
  982. }}
  983. className={`w-7 h-7 rounded-md border-2 transition-all ${
  984. colorHex === color.hex
  985. ? 'border-bambu-green scale-110'
  986. : 'border-white/20 hover:border-white/40'
  987. }`}
  988. style={{ backgroundColor: `#${color.hex}` }}
  989. title={color.name}
  990. />
  991. ))}
  992. </div>
  993. )}
  994. <div className="flex gap-2 items-center">
  995. <div
  996. className="w-10 h-10 rounded-lg border-2 border-white/20 flex-shrink-0"
  997. style={{ backgroundColor: `#${displayColor}` }}
  998. />
  999. <input
  1000. type="text"
  1001. placeholder={t('configureAmsSlot.colorPlaceholder')}
  1002. value={colorInput}
  1003. onChange={(e) => {
  1004. const input = e.target.value;
  1005. setColorInput(input);
  1006. const nameHex = colorNameToHex(input);
  1007. if (nameHex) {
  1008. setColorHex(nameHex);
  1009. } else {
  1010. const cleaned = input.replace(/[^0-9A-Fa-f]/g, '').toUpperCase();
  1011. if (cleaned.length === 6) {
  1012. setColorHex(cleaned);
  1013. } else if (cleaned.length === 3) {
  1014. setColorHex(cleaned.split('').map(c => c + c).join(''));
  1015. }
  1016. }
  1017. }}
  1018. 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"
  1019. />
  1020. {colorHex && (
  1021. <button
  1022. onClick={() => {
  1023. setColorHex('');
  1024. setColorInput('');
  1025. }}
  1026. className="px-2 py-1 text-xs text-bambu-gray hover:text-white bg-bambu-dark-tertiary rounded"
  1027. title={t('configureAmsSlot.clearCustomColor')}
  1028. >
  1029. {t('configureAmsSlot.clear')}
  1030. </button>
  1031. )}
  1032. </div>
  1033. {colorHex && (
  1034. <p className="text-xs text-bambu-gray mt-1.5">
  1035. {t('configureAmsSlot.hexLabel', { hex: colorHex })}
  1036. </p>
  1037. )}
  1038. </div>
  1039. </div>
  1040. </div>
  1041. ) : (
  1042. <>
  1043. {/* Filament Profile Select */}
  1044. <div>
  1045. <label className="block text-sm text-bambu-gray mb-2">
  1046. {t('configureAmsSlot.filamentProfile')} <span className="text-red-400">*</span>
  1047. </label>
  1048. <div className="relative">
  1049. <input
  1050. type="text"
  1051. placeholder={t('configureAmsSlot.searchPresets')}
  1052. value={searchQuery}
  1053. onChange={(e) => setSearchQuery(e.target.value)}
  1054. 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"
  1055. />
  1056. <div className="max-h-48 overflow-y-auto space-y-1">
  1057. {filteredPresets.length === 0 ? (
  1058. <p className="text-center py-4 text-bambu-gray">
  1059. {(cloudSettings?.filament?.length === 0 && !localPresets?.filament?.length)
  1060. ? t('configureAmsSlot.noPresetsAvailable')
  1061. : t('configureAmsSlot.noMatchingPresets')}
  1062. </p>
  1063. ) : (
  1064. filteredPresets.map((preset) => (
  1065. <button
  1066. key={preset.id}
  1067. data-preset-id={preset.id}
  1068. onClick={() => setSelectedPresetId(preset.id)}
  1069. className={`w-full p-2 rounded-lg border text-left transition-colors ${
  1070. selectedPresetId === preset.id
  1071. ? 'bg-bambu-green/20 border-bambu-green'
  1072. : 'bg-bambu-dark border-bambu-dark-tertiary hover:border-bambu-gray'
  1073. }`}
  1074. >
  1075. <div className="flex items-center justify-between">
  1076. <span className="text-white text-sm truncate">{preset.name}</span>
  1077. <div className="flex items-center gap-1 flex-shrink-0">
  1078. {preset.source === 'local' && (
  1079. <span className="text-xs px-1.5 py-0.5 rounded bg-green-500/20 text-green-400">
  1080. {t('profiles.localProfiles.badge')}
  1081. </span>
  1082. )}
  1083. {preset.source === 'builtin' && (
  1084. <span className="text-xs px-1.5 py-0.5 rounded bg-amber-500/20 text-amber-400">
  1085. {t('configureAmsSlot.builtin')}
  1086. </span>
  1087. )}
  1088. {preset.isUser && (
  1089. <span className="text-xs px-1.5 py-0.5 rounded bg-bambu-blue/20 text-bambu-blue">
  1090. {t('configureAmsSlot.custom')}
  1091. </span>
  1092. )}
  1093. </div>
  1094. </div>
  1095. </button>
  1096. ))
  1097. )}
  1098. </div>
  1099. </div>
  1100. </div>
  1101. {/* K Profile Select */}
  1102. <div>
  1103. <label className="block text-sm text-bambu-gray mb-2">
  1104. {t('configureAmsSlot.kProfileLabel')}
  1105. {selectedMaterial && (
  1106. <span className="ml-2 text-xs text-bambu-blue">
  1107. {t('configureAmsSlot.filteringFor', { material: selectedMaterial })}
  1108. </span>
  1109. )}
  1110. </label>
  1111. {matchingKProfiles.length > 0 ? (
  1112. <div className="relative">
  1113. <select
  1114. value={selectedKProfile?.name || ''}
  1115. onChange={(e) => {
  1116. const profile = matchingKProfiles.find(p => p.name === e.target.value);
  1117. setSelectedKProfile(profile || null);
  1118. }}
  1119. 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"
  1120. >
  1121. <option value="">{t('configureAmsSlot.noKProfile')}</option>
  1122. {matchingKProfiles.map((profile) => (
  1123. <option key={`${profile.name}-${profile.extruder_id}`} value={profile.name}>
  1124. {profile.name} (K={profile.k_value})
  1125. </option>
  1126. ))}
  1127. </select>
  1128. <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
  1129. </div>
  1130. ) : selectedPresetId ? (
  1131. <p className="text-sm text-bambu-gray italic py-2">
  1132. {t('configureAmsSlot.noMatchingKProfiles')}
  1133. </p>
  1134. ) : (
  1135. <span className="inline-block text-xs px-2 py-1 rounded bg-amber-500/20 text-amber-400 border border-amber-500/30">
  1136. {t('configureAmsSlot.selectFilamentFirst')}
  1137. </span>
  1138. )}
  1139. {selectedKProfile && (
  1140. <p className="text-xs text-bambu-green mt-1">
  1141. {t('configureAmsSlot.kFromCalibration', { value: selectedKProfile.k_value })}
  1142. </p>
  1143. )}
  1144. </div>
  1145. {/* Optional: Custom color */}
  1146. <div>
  1147. <label className="block text-sm text-bambu-gray mb-2">
  1148. {t('configureAmsSlot.customColorLabel')}
  1149. </label>
  1150. {/* Catalog colors matching selected preset */}
  1151. {catalogColors.length > 0 && (
  1152. <div className="mb-3">
  1153. <p className="text-xs text-bambu-gray mb-1.5">
  1154. {t('configureAmsSlot.presetColors', { name: selectedPresetInfo?.fullName })}
  1155. </p>
  1156. <div className="flex flex-wrap gap-1.5">
  1157. {catalogColors.map((entry) => (
  1158. <button
  1159. key={entry.id}
  1160. onClick={() => {
  1161. const hex = entry.hex_color.replace('#', '').toUpperCase();
  1162. setColorHex(hex);
  1163. setColorInput(entry.color_name);
  1164. }}
  1165. className={`h-7 px-2 rounded-md border-2 transition-all flex items-center gap-1.5 ${
  1166. colorHex === entry.hex_color.replace('#', '').toUpperCase()
  1167. ? 'border-bambu-green scale-105'
  1168. : 'border-white/20 hover:border-white/40'
  1169. }`}
  1170. title={entry.color_name}
  1171. >
  1172. <span
  1173. className="w-4 h-4 rounded-full border border-black/20 flex-shrink-0"
  1174. style={{ backgroundColor: entry.hex_color }}
  1175. />
  1176. <span className="text-xs text-white/80 whitespace-nowrap">{entry.color_name}</span>
  1177. </button>
  1178. ))}
  1179. </div>
  1180. </div>
  1181. )}
  1182. {/* Quick color buttons */}
  1183. <div className="flex flex-wrap gap-1.5 mb-2">
  1184. {QUICK_COLORS_BASIC.map((color) => (
  1185. <button
  1186. key={color.hex}
  1187. onClick={() => {
  1188. setColorHex(color.hex);
  1189. setColorInput(color.name);
  1190. }}
  1191. className={`w-7 h-7 rounded-md border-2 transition-all ${
  1192. colorHex === color.hex
  1193. ? 'border-bambu-green scale-110'
  1194. : 'border-white/20 hover:border-white/40'
  1195. }`}
  1196. style={{ backgroundColor: `#${color.hex}` }}
  1197. title={color.name}
  1198. />
  1199. ))}
  1200. <button
  1201. onClick={() => setShowExtendedColors(!showExtendedColors)}
  1202. 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"
  1203. title={showExtendedColors ? t('configureAmsSlot.showLessColors') : t('configureAmsSlot.showMoreColors')}
  1204. >
  1205. {showExtendedColors ? '−' : '+'}
  1206. </button>
  1207. </div>
  1208. {/* Extended colors (collapsible) */}
  1209. {showExtendedColors && (
  1210. <div className="flex flex-wrap gap-1.5 mb-2">
  1211. {QUICK_COLORS_EXTENDED.map((color) => (
  1212. <button
  1213. key={color.hex}
  1214. onClick={() => {
  1215. setColorHex(color.hex);
  1216. setColorInput(color.name);
  1217. }}
  1218. className={`w-7 h-7 rounded-md border-2 transition-all ${
  1219. colorHex === color.hex
  1220. ? 'border-bambu-green scale-110'
  1221. : 'border-white/20 hover:border-white/40'
  1222. }`}
  1223. style={{ backgroundColor: `#${color.hex}` }}
  1224. title={color.name}
  1225. />
  1226. ))}
  1227. </div>
  1228. )}
  1229. {/* Color input: name or hex */}
  1230. <div className="flex gap-2 items-center">
  1231. <div
  1232. className="w-10 h-10 rounded-lg border-2 border-white/20 flex-shrink-0"
  1233. style={{ backgroundColor: `#${displayColor}` }}
  1234. />
  1235. <input
  1236. type="text"
  1237. placeholder={t('configureAmsSlot.colorPlaceholder')}
  1238. value={colorInput}
  1239. onChange={(e) => {
  1240. const input = e.target.value;
  1241. setColorInput(input);
  1242. // Try to parse as color name first
  1243. const nameHex = colorNameToHex(input);
  1244. if (nameHex) {
  1245. setColorHex(nameHex);
  1246. } else {
  1247. // Try to parse as hex code
  1248. const cleaned = input.replace(/[^0-9A-Fa-f]/g, '').toUpperCase();
  1249. if (cleaned.length === 6) {
  1250. setColorHex(cleaned);
  1251. } else if (cleaned.length === 3) {
  1252. // Expand shorthand hex (e.g., F00 -> FF0000)
  1253. setColorHex(cleaned.split('').map(c => c + c).join(''));
  1254. }
  1255. }
  1256. }}
  1257. 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"
  1258. />
  1259. {colorHex && (
  1260. <button
  1261. onClick={() => {
  1262. setColorHex('');
  1263. setColorInput('');
  1264. }}
  1265. className="px-2 py-1 text-xs text-bambu-gray hover:text-white bg-bambu-dark-tertiary rounded"
  1266. title={t('configureAmsSlot.clearCustomColor')}
  1267. >
  1268. {t('configureAmsSlot.clear')}
  1269. </button>
  1270. )}
  1271. </div>
  1272. {colorHex && (
  1273. <p className="text-xs text-bambu-gray mt-1.5">
  1274. {t('configureAmsSlot.hexLabel', { hex: colorHex })}
  1275. </p>
  1276. )}
  1277. </div>
  1278. </>
  1279. )}
  1280. </div>
  1281. {/* Footer */}
  1282. <div className="flex justify-between p-4 border-t border-bambu-dark-tertiary shrink-0">
  1283. {/* Reset button on the left */}
  1284. <Button
  1285. variant="secondary"
  1286. onClick={() => resetMutation.mutate()}
  1287. disabled={resetMutation.isPending || configureMutation.isPending}
  1288. className="text-red-400 hover:text-red-300 hover:bg-red-500/10"
  1289. >
  1290. {resetMutation.isPending ? (
  1291. <>
  1292. <Loader2 className="w-4 h-4 animate-spin" />
  1293. {t('configureAmsSlot.resetting')}
  1294. </>
  1295. ) : (
  1296. <>
  1297. <RotateCcw className="w-4 h-4" />
  1298. {t('configureAmsSlot.resetSlot')}
  1299. </>
  1300. )}
  1301. </Button>
  1302. {/* Cancel and Configure buttons on the right */}
  1303. <div className="flex gap-2">
  1304. <Button variant="secondary" onClick={onClose}>
  1305. {t('configureAmsSlot.cancel')}
  1306. </Button>
  1307. <Button
  1308. onClick={() => configureMutation.mutate()}
  1309. disabled={!canSave}
  1310. >
  1311. {configureMutation.isPending ? (
  1312. <>
  1313. <Loader2 className="w-4 h-4 animate-spin" />
  1314. {t('configureAmsSlot.configuring')}
  1315. </>
  1316. ) : (
  1317. <>
  1318. <Settings2 className="w-4 h-4" />
  1319. {t('configureAmsSlot.configureSlot')}
  1320. </>
  1321. )}
  1322. </Button>
  1323. </div>
  1324. </div>
  1325. {/* Error */}
  1326. {(configureMutation.isError || resetMutation.isError) && (
  1327. <div className="mx-4 mb-4 p-2 bg-red-500/20 border border-red-500/50 rounded text-sm text-red-400">
  1328. {(configureMutation.error as Error)?.message || (resetMutation.error as Error)?.message}
  1329. </div>
  1330. )}
  1331. </div>
  1332. </div>
  1333. );
  1334. }