ConfigureAmsSlotModal.tsx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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. }
  17. // Get proper AMS label (handles HT AMS with ID 128+)
  18. function getAmsLabel(amsId: number, trayCount: number): string {
  19. // External spool
  20. if (amsId === 255) return 'External';
  21. let normalizedId: number;
  22. let isHt = false;
  23. if (amsId >= 128 && amsId <= 135) {
  24. // HT AMS range: 128-135 → A-H
  25. normalizedId = amsId - 128;
  26. isHt = true;
  27. } else if (amsId >= 0 && amsId <= 3) {
  28. // Regular AMS range: 0-3 → A-D
  29. normalizedId = amsId;
  30. // Check tray count as secondary indicator
  31. isHt = trayCount === 1;
  32. } else {
  33. // Unknown range - fallback to A
  34. normalizedId = 0;
  35. }
  36. // Cap to valid letter range (A-H)
  37. normalizedId = Math.max(0, Math.min(normalizedId, 7));
  38. const letter = String.fromCharCode(65 + normalizedId);
  39. return isHt ? `HT-${letter}` : `AMS-${letter}`;
  40. }
  41. // Convert setting_id to tray_info_idx (filament_id format)
  42. // Bambu format: setting_id "GFSL05" → tray_info_idx "GFL05"
  43. function convertToTrayInfoIdx(settingId: string): string {
  44. // Strip version suffix if present (e.g., GFSL05_07 -> GFSL05)
  45. const baseId = settingId.includes('_') ? settingId.split('_')[0] : settingId;
  46. // Bambu presets start with "GFS" - remove the 'S' to get filament_id
  47. if (baseId.startsWith('GFS')) {
  48. return 'GF' + baseId.slice(3);
  49. }
  50. // User presets (PFUS*, PFSP*) - use the base setting_id (without version suffix)
  51. // This follows the pattern that filament_id and setting_id share the same base ID
  52. if (baseId.startsWith('PFUS') || baseId.startsWith('PFSP')) {
  53. return baseId; // Use base ID without version suffix
  54. }
  55. // For other formats, use as-is
  56. return baseId;
  57. }
  58. interface ConfigureAmsSlotModalProps {
  59. isOpen: boolean;
  60. onClose: () => void;
  61. printerId: number;
  62. slotInfo: SlotInfo;
  63. nozzleDiameter?: string;
  64. onSuccess?: () => void;
  65. }
  66. // Known filament material types
  67. const MATERIAL_TYPES = ['PLA', 'PETG', 'ABS', 'ASA', 'TPU', 'PC', 'PA', 'NYLON', 'PVA', 'HIPS', 'PP', 'PET'];
  68. // Extract filament type from preset name by finding known material type
  69. function parsePresetName(name: string): { material: string; brand: string; variant: string } {
  70. // Remove printer/nozzle suffix first
  71. const withoutSuffix = name.replace(/@.+$/, '').trim();
  72. // Try to find a known material type in the name
  73. const upperName = withoutSuffix.toUpperCase();
  74. for (const mat of MATERIAL_TYPES) {
  75. // Use word boundary to match whole words only
  76. const regex = new RegExp(`\\b${mat}\\b`, 'i');
  77. if (regex.test(upperName)) {
  78. // Found material, extract brand (everything before material) and variant (after)
  79. const parts = withoutSuffix.split(regex);
  80. const brand = parts[0]?.trim() || '';
  81. const variant = parts[1]?.trim() || '';
  82. return { material: mat, brand, variant };
  83. }
  84. }
  85. // Fallback: assume first word is brand, second is material
  86. const parts = withoutSuffix.split(/\s+/);
  87. if (parts.length >= 2) {
  88. return { material: parts[1], brand: parts[0], variant: parts.slice(2).join(' ') };
  89. }
  90. return { material: withoutSuffix, brand: '', variant: '' };
  91. }
  92. // Check if a preset is a user preset (not built-in)
  93. function isUserPreset(settingId: string): boolean {
  94. // Built-in presets have specific patterns, user presets are UUIDs
  95. return !settingId.startsWith('GF') && !settingId.startsWith('P1');
  96. }
  97. // Common color name to hex mapping
  98. const COLOR_NAME_MAP: Record<string, string> = {
  99. // Basic colors
  100. 'white': 'FFFFFF',
  101. 'black': '000000',
  102. 'red': 'FF0000',
  103. 'green': '00FF00',
  104. 'blue': '0000FF',
  105. 'yellow': 'FFFF00',
  106. 'cyan': '00FFFF',
  107. 'magenta': 'FF00FF',
  108. 'orange': 'FFA500',
  109. 'purple': '800080',
  110. 'pink': 'FFC0CB',
  111. 'brown': '8B4513',
  112. 'gray': '808080',
  113. 'grey': '808080',
  114. // Filament-specific colors
  115. 'jade white': 'FFFEF2',
  116. 'ivory': 'FFFFF0',
  117. 'beige': 'F5F5DC',
  118. 'cream': 'FFFDD0',
  119. 'silver': 'C0C0C0',
  120. 'gold': 'FFD700',
  121. 'bronze': 'CD7F32',
  122. 'copper': 'B87333',
  123. 'navy': '000080',
  124. 'teal': '008080',
  125. 'olive': '808000',
  126. 'maroon': '800000',
  127. 'coral': 'FF7F50',
  128. 'salmon': 'FA8072',
  129. 'lime': '32CD32',
  130. 'mint': '98FF98',
  131. 'forest green': '228B22',
  132. 'sky blue': '87CEEB',
  133. 'royal blue': '4169E1',
  134. 'turquoise': '40E0D0',
  135. 'lavender': 'E6E6FA',
  136. 'violet': 'EE82EE',
  137. 'plum': 'DDA0DD',
  138. 'tan': 'D2B48C',
  139. 'chocolate': 'D2691E',
  140. 'charcoal': '36454F',
  141. 'slate': '708090',
  142. 'transparent': '000000', // Will need special handling
  143. 'natural': 'F5F5DC',
  144. 'wood': 'DEB887',
  145. };
  146. // Quick-select color presets (common filament colors)
  147. // Basic colors shown by default
  148. const QUICK_COLORS_BASIC = [
  149. { name: 'White', hex: 'FFFFFF' },
  150. { name: 'Black', hex: '000000' },
  151. { name: 'Red', hex: 'FF0000' },
  152. { name: 'Blue', hex: '0000FF' },
  153. { name: 'Green', hex: '00AA00' },
  154. { name: 'Yellow', hex: 'FFFF00' },
  155. { name: 'Orange', hex: 'FFA500' },
  156. { name: 'Gray', hex: '808080' },
  157. ];
  158. // Extended colors shown when expanded
  159. const QUICK_COLORS_EXTENDED = [
  160. { name: 'Cyan', hex: '00FFFF' },
  161. { name: 'Magenta', hex: 'FF00FF' },
  162. { name: 'Purple', hex: '800080' },
  163. { name: 'Pink', hex: 'FFC0CB' },
  164. { name: 'Brown', hex: '8B4513' },
  165. { name: 'Beige', hex: 'F5F5DC' },
  166. { name: 'Navy', hex: '000080' },
  167. { name: 'Teal', hex: '008080' },
  168. { name: 'Lime', hex: '32CD32' },
  169. { name: 'Gold', hex: 'FFD700' },
  170. { name: 'Silver', hex: 'C0C0C0' },
  171. { name: 'Maroon', hex: '800000' },
  172. { name: 'Olive', hex: '808000' },
  173. { name: 'Coral', hex: 'FF7F50' },
  174. { name: 'Salmon', hex: 'FA8072' },
  175. { name: 'Turquoise', hex: '40E0D0' },
  176. { name: 'Violet', hex: 'EE82EE' },
  177. { name: 'Indigo', hex: '4B0082' },
  178. { name: 'Chocolate', hex: 'D2691E' },
  179. { name: 'Tan', hex: 'D2B48C' },
  180. { name: 'Slate', hex: '708090' },
  181. { name: 'Charcoal', hex: '36454F' },
  182. { name: 'Ivory', hex: 'FFFFF0' },
  183. { name: 'Cream', hex: 'FFFDD0' },
  184. ];
  185. // Try to convert color name to hex
  186. function colorNameToHex(name: string): string | null {
  187. const normalized = name.toLowerCase().trim();
  188. return COLOR_NAME_MAP[normalized] || null;
  189. }
  190. export function ConfigureAmsSlotModal({
  191. isOpen,
  192. onClose,
  193. printerId,
  194. slotInfo,
  195. nozzleDiameter = '0.4',
  196. onSuccess,
  197. }: ConfigureAmsSlotModalProps) {
  198. const { t } = useTranslation();
  199. const [selectedPresetId, setSelectedPresetId] = useState<string>('');
  200. const [selectedKProfile, setSelectedKProfile] = useState<KProfile | null>(null);
  201. const [colorHex, setColorHex] = useState<string>(''); // Just the 6-char hex, no alpha
  202. const [colorInput, setColorInput] = useState<string>(''); // User's text input (name or hex)
  203. const [searchQuery, setSearchQuery] = useState('');
  204. const [showSuccess, setShowSuccess] = useState(false);
  205. const [showExtendedColors, setShowExtendedColors] = useState(false);
  206. // Fetch cloud settings
  207. const { data: cloudSettings, isLoading: settingsLoading } = useQuery({
  208. queryKey: ['cloudSettings'],
  209. queryFn: () => api.getCloudSettings(),
  210. enabled: isOpen,
  211. });
  212. // Fetch local presets
  213. const { data: localPresets, isLoading: localLoading } = useQuery({
  214. queryKey: ['localPresets'],
  215. queryFn: () => api.getLocalPresets(),
  216. enabled: isOpen,
  217. });
  218. // Fetch K profiles
  219. const { data: kprofilesData, isLoading: kprofilesLoading } = useQuery({
  220. queryKey: ['kprofiles', printerId, nozzleDiameter],
  221. queryFn: () => api.getKProfiles(printerId, nozzleDiameter),
  222. enabled: isOpen && !!printerId,
  223. });
  224. // Configure slot mutation
  225. const configureMutation = useMutation({
  226. mutationFn: async () => {
  227. if (!selectedPresetId) throw new Error('No filament preset selected');
  228. // Check if this is a local preset
  229. const isLocal = selectedPresetId.startsWith('local_');
  230. const localId = isLocal ? parseInt(selectedPresetId.replace('local_', ''), 10) : null;
  231. const localPreset = isLocal
  232. ? localPresets?.filament.find(p => p.id === localId)
  233. : null;
  234. // Get the selected cloud preset details (null for local presets)
  235. const selectedPreset = !isLocal
  236. ? cloudSettings?.filament.find(p => p.setting_id === selectedPresetId)
  237. : null;
  238. if (!isLocal && !selectedPreset) throw new Error('Selected preset not found');
  239. if (isLocal && !localPreset) throw new Error('Selected local preset not found');
  240. // Parse the preset name for filament info
  241. const presetName = isLocal ? localPreset!.name : selectedPreset!.name;
  242. const parsed = parsePresetName(presetName);
  243. // Get cali_idx from selected K profile's slot_id (-1 = use default 0.020)
  244. const caliIdx = selectedKProfile?.slot_id ?? -1;
  245. // Use custom color if set, otherwise use current slot color or default
  246. const color = colorHex || slotInfo.trayColor?.slice(0, 6) || 'FFFFFF';
  247. // Create the tray_sub_brands from preset name (without printer/nozzle suffix)
  248. const traySubBrands = presetName.replace(/@.+$/, '').trim();
  249. let trayInfoIdx: string;
  250. let settingId: string;
  251. if (isLocal) {
  252. // Local presets have no Bambu Cloud mapping
  253. trayInfoIdx = '';
  254. settingId = '';
  255. } else {
  256. // Get tray_info_idx: for user presets, fetch detail to get filament_id or derive from base_id
  257. trayInfoIdx = convertToTrayInfoIdx(selectedPresetId);
  258. settingId = selectedPresetId;
  259. // For user presets (not starting with GF), fetch the detail to get the real filament_id
  260. if (!selectedPresetId.startsWith('GFS')) {
  261. try {
  262. const detail = await api.getCloudSettingDetail(selectedPresetId);
  263. if (detail.filament_id) {
  264. trayInfoIdx = detail.filament_id;
  265. } else if (detail.base_id) {
  266. trayInfoIdx = convertToTrayInfoIdx(detail.base_id);
  267. console.log(`Derived tray_info_idx from base_id: ${detail.base_id} -> ${trayInfoIdx}`);
  268. }
  269. } catch (e) {
  270. console.warn('Failed to fetch preset detail for filament_id:', e);
  271. }
  272. }
  273. }
  274. // Default temp range — use local preset core fields if available
  275. let tempMin = isLocal && localPreset?.nozzle_temp_min ? localPreset.nozzle_temp_min : 190;
  276. let tempMax = isLocal && localPreset?.nozzle_temp_max ? localPreset.nozzle_temp_max : 230;
  277. if (!isLocal || (!localPreset?.nozzle_temp_min && !localPreset?.nozzle_temp_max)) {
  278. // Fall back to material-based defaults
  279. const material = (isLocal ? (localPreset?.filament_type || parsed.material) : parsed.material).toUpperCase();
  280. if (material.includes('PLA')) {
  281. tempMin = 190;
  282. tempMax = 230;
  283. } else if (material.includes('PETG')) {
  284. tempMin = 220;
  285. tempMax = 260;
  286. } else if (material.includes('ABS')) {
  287. tempMin = 240;
  288. tempMax = 280;
  289. } else if (material.includes('ASA')) {
  290. tempMin = 240;
  291. tempMax = 280;
  292. } else if (material.includes('TPU')) {
  293. tempMin = 200;
  294. tempMax = 240;
  295. } else if (material.includes('PC')) {
  296. tempMin = 260;
  297. tempMax = 300;
  298. } else if (material.includes('PA') || material.includes('NYLON')) {
  299. tempMin = 250;
  300. tempMax = 290;
  301. }
  302. }
  303. // Parse K value from selected profile
  304. const kValue = selectedKProfile?.k_value ? parseFloat(selectedKProfile.k_value) : 0;
  305. // Determine tray_type: use local preset's filament_type or parsed material
  306. const trayType = isLocal
  307. ? (localPreset?.filament_type || parsed.material || 'PLA')
  308. : (parsed.material || 'PLA');
  309. // Configure the slot via MQTT
  310. const result = await api.configureAmsSlot(printerId, slotInfo.amsId, slotInfo.trayId, {
  311. tray_info_idx: trayInfoIdx,
  312. tray_type: trayType,
  313. tray_sub_brands: traySubBrands,
  314. tray_color: color + 'FF', // Add alpha
  315. nozzle_temp_min: tempMin,
  316. nozzle_temp_max: tempMax,
  317. cali_idx: caliIdx,
  318. nozzle_diameter: nozzleDiameter,
  319. setting_id: settingId, // Full setting ID for slicer compatibility (empty for local)
  320. // Pass K profile's filament_id and setting_id for proper linking
  321. kprofile_filament_id: selectedKProfile?.filament_id,
  322. kprofile_setting_id: selectedKProfile?.setting_id || undefined,
  323. // Also pass the K value directly for extrusion_cali_set command
  324. k_value: kValue,
  325. });
  326. // Save the preset mapping so we can display the correct name in the UI
  327. // This is needed because user presets use filament_id (e.g., P285e239) as tray_info_idx,
  328. // which can't be resolved to a name via the filamentInfo API
  329. const mappingPresetId = isLocal ? `local_${localId}` : selectedPresetId;
  330. const mappingSource = isLocal ? 'local' : 'cloud';
  331. try {
  332. await api.saveSlotPreset(printerId, slotInfo.amsId, slotInfo.trayId, mappingPresetId, traySubBrands, mappingSource);
  333. } catch (e) {
  334. console.warn('Failed to save slot preset mapping:', e);
  335. // Don't fail the whole operation - slot was configured successfully
  336. }
  337. return result;
  338. },
  339. onSuccess: () => {
  340. setShowSuccess(true);
  341. onSuccess?.();
  342. // Close after showing success briefly
  343. setTimeout(() => {
  344. setShowSuccess(false);
  345. onClose();
  346. }, 1500);
  347. },
  348. });
  349. // Reset slot mutation
  350. const resetMutation = useMutation({
  351. mutationFn: async () => {
  352. return api.resetAmsSlot(printerId, slotInfo.amsId, slotInfo.trayId);
  353. },
  354. onSuccess: () => {
  355. setShowSuccess(true);
  356. onSuccess?.();
  357. setTimeout(() => {
  358. setShowSuccess(false);
  359. onClose();
  360. }, 1500);
  361. },
  362. });
  363. // Unified preset item for the list (cloud + local)
  364. type PresetItem = { id: string; name: string; source: 'cloud' | 'local'; isUser: boolean };
  365. // Filter filament presets based on search (merged cloud + local)
  366. const filteredPresets = useMemo(() => {
  367. const query = searchQuery.toLowerCase();
  368. const items: PresetItem[] = [];
  369. // Add local presets first
  370. if (localPresets?.filament) {
  371. for (const lp of localPresets.filament) {
  372. if (!query || lp.name.toLowerCase().includes(query)) {
  373. items.push({ id: `local_${lp.id}`, name: lp.name, source: 'local', isUser: false });
  374. }
  375. }
  376. }
  377. // Add cloud presets
  378. if (cloudSettings?.filament) {
  379. for (const cp of cloudSettings.filament) {
  380. if (!query || cp.name.toLowerCase().includes(query)) {
  381. items.push({ id: cp.setting_id, name: cp.name, source: 'cloud', isUser: isUserPreset(cp.setting_id) });
  382. }
  383. }
  384. }
  385. // Sort: local first, then user cloud presets, then built-in, alphabetically within groups
  386. return items.sort((a, b) => {
  387. if (a.source === 'local' && b.source !== 'local') return -1;
  388. if (a.source !== 'local' && b.source === 'local') return 1;
  389. if (a.isUser && !b.isUser) return -1;
  390. if (!a.isUser && b.isUser) return 1;
  391. return a.name.localeCompare(b.name);
  392. });
  393. }, [cloudSettings?.filament, localPresets?.filament, searchQuery]);
  394. // Get full preset name for K profile filtering (brand + material, without printer suffix)
  395. const selectedPresetInfo = useMemo(() => {
  396. if (!selectedPresetId) return null;
  397. // Resolve the name from either local or cloud presets
  398. let presetName: string | null = null;
  399. if (selectedPresetId.startsWith('local_')) {
  400. const localId = parseInt(selectedPresetId.replace('local_', ''), 10);
  401. const lp = localPresets?.filament.find(p => p.id === localId);
  402. presetName = lp?.name || null;
  403. } else if (cloudSettings?.filament) {
  404. const cp = cloudSettings.filament.find(p => p.setting_id === selectedPresetId);
  405. presetName = cp?.name || null;
  406. }
  407. if (!presetName) return null;
  408. // Remove printer/nozzle suffix (e.g., "@BBL X1C" or "@0.4 nozzle")
  409. let nameWithoutSuffix = presetName.replace(/@.+$/, '').trim();
  410. // Strip leading "# " from custom preset names (user convention)
  411. if (nameWithoutSuffix.startsWith('# ')) {
  412. nameWithoutSuffix = nameWithoutSuffix.slice(2).trim();
  413. }
  414. const parsed = parsePresetName(nameWithoutSuffix);
  415. return {
  416. fullName: nameWithoutSuffix,
  417. material: parsed.material,
  418. brand: parsed.brand,
  419. };
  420. }, [selectedPresetId, cloudSettings?.filament, localPresets?.filament]);
  421. // For backwards compatibility with the label
  422. const selectedMaterial = selectedPresetInfo?.fullName || '';
  423. const matchingKProfiles = useMemo(() => {
  424. if (!kprofilesData?.profiles || !selectedPresetInfo) return [];
  425. const { fullName, material, brand } = selectedPresetInfo;
  426. const upperFullName = fullName.toUpperCase();
  427. const upperMaterial = material.toUpperCase();
  428. const upperBrand = brand.toUpperCase();
  429. // Material must be at least 2 chars to avoid false positives
  430. if (!upperMaterial || upperMaterial.length < 2) return [];
  431. // Filter profiles - require brand match if brand is present in selected preset
  432. const filtered = kprofilesData.profiles.filter(p => {
  433. const profileName = p.name.toUpperCase();
  434. // If the selected preset has a brand (e.g., "Azurefilm PLA Wood"),
  435. // only show profiles that match the brand
  436. if (upperBrand) {
  437. // Must contain the brand name
  438. if (!profileName.includes(upperBrand)) {
  439. return false;
  440. }
  441. // And must contain the material type
  442. if (!profileName.includes(upperMaterial)) {
  443. return false;
  444. }
  445. return true;
  446. }
  447. // No brand in selected preset - match on full name or material
  448. // Priority 1: Exact match with full name
  449. if (profileName.includes(upperFullName)) {
  450. return true;
  451. }
  452. // Priority 2: Material type match (only when no brand specified)
  453. if (profileName.includes(upperMaterial)) {
  454. return true;
  455. }
  456. // Check for common material aliases
  457. const aliases: Record<string, string[]> = {
  458. 'NYLON': ['PA', 'PA-CF', 'PA6'],
  459. 'PA': ['NYLON'],
  460. };
  461. const materialAliases = aliases[upperMaterial] || [];
  462. for (const alias of materialAliases) {
  463. if (profileName.includes(alias)) {
  464. return true;
  465. }
  466. }
  467. return false;
  468. });
  469. // Deduplicate profiles with same name and k_value (multi-nozzle printers have duplicates)
  470. // Prefer extruder_id=1 (High Flow) profiles as they're more commonly used on H2D
  471. const seen = new Map<string, KProfile>();
  472. for (const profile of filtered) {
  473. const key = `${profile.name}|${profile.k_value}`;
  474. const existing = seen.get(key);
  475. if (!existing) {
  476. seen.set(key, profile);
  477. } else if (profile.extruder_id === 1 && existing.extruder_id === 0) {
  478. // Replace extruder_id=0 profile with extruder_id=1 (High Flow) profile
  479. seen.set(key, profile);
  480. }
  481. }
  482. return Array.from(seen.values());
  483. }, [kprofilesData?.profiles, selectedPresetInfo]);
  484. // Pre-select current profile when modal opens, reset when closes
  485. useEffect(() => {
  486. if (isOpen && cloudSettings?.filament) {
  487. // Try to pre-select current profile based on trayInfoIdx
  488. if (slotInfo.trayInfoIdx) {
  489. const currentPreset = cloudSettings.filament.find(
  490. p => p.setting_id === slotInfo.trayInfoIdx
  491. );
  492. if (currentPreset) {
  493. setSelectedPresetId(currentPreset.setting_id);
  494. }
  495. }
  496. } else if (!isOpen) {
  497. // Reset when modal closes
  498. setSelectedPresetId('');
  499. setSelectedKProfile(null);
  500. setColorHex('');
  501. setColorInput('');
  502. setSearchQuery('');
  503. setShowSuccess(false);
  504. }
  505. }, [isOpen, cloudSettings?.filament, slotInfo.trayInfoIdx]);
  506. // Auto-select best matching K profile when preset changes
  507. useEffect(() => {
  508. if (matchingKProfiles.length > 0) {
  509. // Auto-select first matching profile
  510. setSelectedKProfile(matchingKProfiles[0]);
  511. } else {
  512. setSelectedKProfile(null);
  513. }
  514. }, [selectedPresetId, matchingKProfiles]);
  515. // Escape key handler
  516. const handleKeyDown = useCallback((e: KeyboardEvent) => {
  517. if (e.key === 'Escape') {
  518. onClose();
  519. }
  520. }, [onClose]);
  521. useEffect(() => {
  522. if (isOpen) {
  523. document.addEventListener('keydown', handleKeyDown);
  524. return () => document.removeEventListener('keydown', handleKeyDown);
  525. }
  526. }, [isOpen, handleKeyDown]);
  527. if (!isOpen) return null;
  528. const isLoading = settingsLoading || localLoading || kprofilesLoading;
  529. const canSave = selectedPresetId && !configureMutation.isPending;
  530. // Get display color (custom or slot default)
  531. const displayColor = colorHex || slotInfo.trayColor?.slice(0, 6) || 'FFFFFF';
  532. return (
  533. <div className="fixed inset-0 z-50 flex items-center justify-center">
  534. {/* Backdrop */}
  535. <div
  536. className="absolute inset-0 bg-black/60 backdrop-blur-sm"
  537. onClick={onClose}
  538. />
  539. {/* Modal */}
  540. <div className="relative w-full max-w-lg mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl">
  541. {/* Header */}
  542. <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary">
  543. <div className="flex items-center gap-2">
  544. <Settings2 className="w-5 h-5 text-bambu-blue" />
  545. <h2 className="text-lg font-semibold text-white">Configure AMS Slot</h2>
  546. </div>
  547. <button
  548. onClick={onClose}
  549. className="p-1 text-bambu-gray hover:text-white rounded transition-colors"
  550. >
  551. <X className="w-5 h-5" />
  552. </button>
  553. </div>
  554. {/* Content */}
  555. <div className="p-4 space-y-4 max-h-[60vh] overflow-y-auto">
  556. {/* Success overlay */}
  557. {showSuccess && (
  558. <div className="absolute inset-0 bg-bambu-dark-secondary/95 z-10 flex items-center justify-center rounded-xl">
  559. <div className="text-center space-y-3">
  560. <CheckCircle2 className="w-16 h-16 text-bambu-green mx-auto" />
  561. <p className="text-lg font-semibold text-white">Slot Configured!</p>
  562. <p className="text-sm text-bambu-gray">Settings sent to printer</p>
  563. </div>
  564. </div>
  565. )}
  566. {/* Slot info */}
  567. <div className="p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary">
  568. <p className="text-xs text-bambu-gray mb-1">Configuring slot:</p>
  569. <div className="flex items-center gap-2">
  570. {slotInfo.trayColor && (
  571. <span
  572. className="w-4 h-4 rounded-full border border-white/20"
  573. style={{ backgroundColor: `#${slotInfo.trayColor.slice(0, 6)}` }}
  574. />
  575. )}
  576. <span className="text-white font-medium">
  577. {getAmsLabel(slotInfo.amsId, slotInfo.trayCount)} Slot {slotInfo.trayId + 1}
  578. </span>
  579. {slotInfo.traySubBrands && (
  580. <span className="text-bambu-gray">({slotInfo.traySubBrands})</span>
  581. )}
  582. </div>
  583. </div>
  584. {isLoading ? (
  585. <div className="flex justify-center py-8">
  586. <Loader2 className="w-6 h-6 text-bambu-green animate-spin" />
  587. </div>
  588. ) : (
  589. <>
  590. {/* Filament Profile Select */}
  591. <div>
  592. <label className="block text-sm text-bambu-gray mb-2">
  593. Filament Profile <span className="text-red-400">*</span>
  594. </label>
  595. <div className="relative">
  596. <input
  597. type="text"
  598. placeholder="Search presets..."
  599. value={searchQuery}
  600. onChange={(e) => setSearchQuery(e.target.value)}
  601. 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"
  602. />
  603. <div className="max-h-48 overflow-y-auto space-y-1">
  604. {filteredPresets.length === 0 ? (
  605. <p className="text-center py-4 text-bambu-gray">
  606. {(cloudSettings?.filament?.length === 0 && !localPresets?.filament?.length)
  607. ? 'No presets available. Login to Bambu Cloud or import local profiles.'
  608. : 'No matching presets found.'}
  609. </p>
  610. ) : (
  611. filteredPresets.map((preset) => (
  612. <button
  613. key={preset.id}
  614. onClick={() => setSelectedPresetId(preset.id)}
  615. className={`w-full p-2 rounded-lg border text-left transition-colors ${
  616. selectedPresetId === preset.id
  617. ? 'bg-bambu-green/20 border-bambu-green'
  618. : 'bg-bambu-dark border-bambu-dark-tertiary hover:border-bambu-gray'
  619. }`}
  620. >
  621. <div className="flex items-center justify-between">
  622. <span className="text-white text-sm truncate">{preset.name}</span>
  623. <div className="flex items-center gap-1 flex-shrink-0">
  624. {preset.source === 'local' && (
  625. <span className="text-xs px-1.5 py-0.5 rounded bg-green-500/20 text-green-400">
  626. {t('profiles.localProfiles.badge')}
  627. </span>
  628. )}
  629. {preset.isUser && (
  630. <span className="text-xs px-1.5 py-0.5 rounded bg-bambu-blue/20 text-bambu-blue">
  631. Custom
  632. </span>
  633. )}
  634. </div>
  635. </div>
  636. </button>
  637. ))
  638. )}
  639. </div>
  640. </div>
  641. </div>
  642. {/* K Profile Select */}
  643. <div>
  644. <label className="block text-sm text-bambu-gray mb-2">
  645. K Profile (Pressure Advance)
  646. {selectedMaterial && (
  647. <span className="ml-2 text-xs text-bambu-blue">
  648. Filtering for: {selectedMaterial}
  649. </span>
  650. )}
  651. </label>
  652. {matchingKProfiles.length > 0 ? (
  653. <div className="relative">
  654. <select
  655. value={selectedKProfile?.name || ''}
  656. onChange={(e) => {
  657. const profile = matchingKProfiles.find(p => p.name === e.target.value);
  658. setSelectedKProfile(profile || null);
  659. }}
  660. 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"
  661. >
  662. <option value="">No K profile (use default 0.020)</option>
  663. {matchingKProfiles.map((profile) => (
  664. <option key={`${profile.name}-${profile.extruder_id}`} value={profile.name}>
  665. {profile.name} (K={profile.k_value})
  666. </option>
  667. ))}
  668. </select>
  669. <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
  670. </div>
  671. ) : selectedPresetId ? (
  672. <p className="text-sm text-bambu-gray italic py-2">
  673. No matching K profiles found. Default K=0.020 will be used.
  674. </p>
  675. ) : (
  676. <span className="inline-block text-xs px-2 py-1 rounded bg-amber-500/20 text-amber-400 border border-amber-500/30">
  677. Select a filament profile first
  678. </span>
  679. )}
  680. {selectedKProfile && (
  681. <p className="text-xs text-bambu-green mt-1">
  682. K={selectedKProfile.k_value} from printer calibration
  683. </p>
  684. )}
  685. </div>
  686. {/* Optional: Custom color */}
  687. <div>
  688. <label className="block text-sm text-bambu-gray mb-2">
  689. Custom Color (optional)
  690. </label>
  691. {/* Quick color buttons */}
  692. <div className="flex flex-wrap gap-1.5 mb-2">
  693. {QUICK_COLORS_BASIC.map((color) => (
  694. <button
  695. key={color.hex}
  696. onClick={() => {
  697. setColorHex(color.hex);
  698. setColorInput(color.name);
  699. }}
  700. className={`w-7 h-7 rounded-md border-2 transition-all ${
  701. colorHex === color.hex
  702. ? 'border-bambu-green scale-110'
  703. : 'border-white/20 hover:border-white/40'
  704. }`}
  705. style={{ backgroundColor: `#${color.hex}` }}
  706. title={color.name}
  707. />
  708. ))}
  709. <button
  710. onClick={() => setShowExtendedColors(!showExtendedColors)}
  711. 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"
  712. title={showExtendedColors ? 'Show less colors' : 'Show more colors'}
  713. >
  714. {showExtendedColors ? '−' : '+'}
  715. </button>
  716. </div>
  717. {/* Extended colors (collapsible) */}
  718. {showExtendedColors && (
  719. <div className="flex flex-wrap gap-1.5 mb-2">
  720. {QUICK_COLORS_EXTENDED.map((color) => (
  721. <button
  722. key={color.hex}
  723. onClick={() => {
  724. setColorHex(color.hex);
  725. setColorInput(color.name);
  726. }}
  727. className={`w-7 h-7 rounded-md border-2 transition-all ${
  728. colorHex === color.hex
  729. ? 'border-bambu-green scale-110'
  730. : 'border-white/20 hover:border-white/40'
  731. }`}
  732. style={{ backgroundColor: `#${color.hex}` }}
  733. title={color.name}
  734. />
  735. ))}
  736. </div>
  737. )}
  738. {/* Color input: name or hex */}
  739. <div className="flex gap-2 items-center">
  740. <div
  741. className="w-10 h-10 rounded-lg border-2 border-white/20 flex-shrink-0"
  742. style={{ backgroundColor: `#${displayColor}` }}
  743. />
  744. <input
  745. type="text"
  746. placeholder="Color name or hex (e.g., brown, FF8800)"
  747. value={colorInput}
  748. onChange={(e) => {
  749. const input = e.target.value;
  750. setColorInput(input);
  751. // Try to parse as color name first
  752. const nameHex = colorNameToHex(input);
  753. if (nameHex) {
  754. setColorHex(nameHex);
  755. } else {
  756. // Try to parse as hex code
  757. const cleaned = input.replace(/[^0-9A-Fa-f]/g, '').toUpperCase();
  758. if (cleaned.length === 6) {
  759. setColorHex(cleaned);
  760. } else if (cleaned.length === 3) {
  761. // Expand shorthand hex (e.g., F00 -> FF0000)
  762. setColorHex(cleaned.split('').map(c => c + c).join(''));
  763. }
  764. }
  765. }}
  766. 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"
  767. />
  768. {colorHex && (
  769. <button
  770. onClick={() => {
  771. setColorHex('');
  772. setColorInput('');
  773. }}
  774. className="px-2 py-1 text-xs text-bambu-gray hover:text-white bg-bambu-dark-tertiary rounded"
  775. title="Clear custom color"
  776. >
  777. Clear
  778. </button>
  779. )}
  780. </div>
  781. {colorHex && (
  782. <p className="text-xs text-bambu-gray mt-1.5">
  783. Hex: #{colorHex}
  784. </p>
  785. )}
  786. </div>
  787. </>
  788. )}
  789. </div>
  790. {/* Footer */}
  791. <div className="flex justify-between p-4 border-t border-bambu-dark-tertiary">
  792. {/* Reset button on the left */}
  793. <Button
  794. variant="secondary"
  795. onClick={() => resetMutation.mutate()}
  796. disabled={resetMutation.isPending || configureMutation.isPending}
  797. className="text-red-400 hover:text-red-300 hover:bg-red-500/10"
  798. >
  799. {resetMutation.isPending ? (
  800. <>
  801. <Loader2 className="w-4 h-4 animate-spin" />
  802. Resetting...
  803. </>
  804. ) : (
  805. <>
  806. <RotateCcw className="w-4 h-4" />
  807. Reset Slot
  808. </>
  809. )}
  810. </Button>
  811. {/* Cancel and Configure buttons on the right */}
  812. <div className="flex gap-2">
  813. <Button variant="secondary" onClick={onClose}>
  814. Cancel
  815. </Button>
  816. <Button
  817. onClick={() => configureMutation.mutate()}
  818. disabled={!canSave}
  819. >
  820. {configureMutation.isPending ? (
  821. <>
  822. <Loader2 className="w-4 h-4 animate-spin" />
  823. Configuring...
  824. </>
  825. ) : (
  826. <>
  827. <Settings2 className="w-4 h-4" />
  828. Configure Slot
  829. </>
  830. )}
  831. </Button>
  832. </div>
  833. </div>
  834. {/* Error */}
  835. {(configureMutation.isError || resetMutation.isError) && (
  836. <div className="mx-4 mb-4 p-2 bg-red-500/20 border border-red-500/50 rounded text-sm text-red-400">
  837. {(configureMutation.error as Error)?.message || (resetMutation.error as Error)?.message}
  838. </div>
  839. )}
  840. </div>
  841. </div>
  842. );
  843. }