LocalProfilesView.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. import { useState, useMemo, useCallback } from 'react';
  2. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  3. import { useTranslation } from 'react-i18next';
  4. import {
  5. Upload,
  6. Loader2,
  7. Search,
  8. Trash2,
  9. ChevronDown,
  10. ChevronUp,
  11. HardDrive,
  12. Droplet,
  13. Settings2,
  14. Layers,
  15. AlertCircle,
  16. } from 'lucide-react';
  17. import { api } from '../api/client';
  18. import type { LocalPreset } from '../api/client';
  19. import { Card, CardContent } from './Card';
  20. import { Button } from './Button';
  21. import { useToast } from '../contexts/ToastContext';
  22. import { useAuth } from '../contexts/AuthContext';
  23. // Known material types for name-parsing fallback
  24. const MATERIAL_TYPES = ['PLA', 'PETG', 'PCTG', 'ABS', 'ASA', 'TPU', 'PC', 'PA', 'PVA', 'HIPS', 'PP', 'PET', 'NYLON'];
  25. const FILAMENT_TYPE_COLORS: Record<string, string> = {
  26. PLA: 'E8E8E8', PETG: '4A90D9', ABS: 'E67E22', ASA: 'D35400',
  27. TPU: '9B59B6', PC: 'BDC3C7', PA: '2ECC71', NYLON: '2ECC71',
  28. PVA: 'F1C40F', HIPS: '95A5A6', PP: 'ECF0F1', PET: '3498DB',
  29. };
  30. // Extract material type from preset name as fallback
  31. function parseMaterialFromName(name: string): string | null {
  32. const upper = name.toUpperCase();
  33. for (const mat of MATERIAL_TYPES) {
  34. if (new RegExp(`\\b${mat}\\b`).test(upper)) return mat;
  35. }
  36. return null;
  37. }
  38. // Extract vendor from preset name (text before the material type)
  39. function parseVendorFromName(name: string): string | null {
  40. // Strip printer/nozzle suffix first (e.g. "@BBL X1C")
  41. const clean = name.replace(/@.+$/, '').trim();
  42. const upper = clean.toUpperCase();
  43. for (const mat of MATERIAL_TYPES) {
  44. const idx = upper.indexOf(mat);
  45. if (idx > 0) {
  46. const vendor = clean.slice(0, idx).trim();
  47. // Skip if vendor looks like a generic prefix (e.g., "Generic", "Bambu")
  48. if (vendor && vendor.length > 1) return vendor;
  49. }
  50. }
  51. return null;
  52. }
  53. function PresetCard({
  54. preset,
  55. onDelete,
  56. onExpand,
  57. isExpanded,
  58. }: {
  59. preset: LocalPreset;
  60. onDelete: (id: number) => void;
  61. onExpand: (id: number | null) => void;
  62. isExpanded: boolean;
  63. }) {
  64. const { t } = useTranslation();
  65. const { hasPermission } = useAuth();
  66. // Resolve material type: DB field → parse from name
  67. const material = preset.filament_type || parseMaterialFromName(preset.name);
  68. // Resolve vendor: DB field → parse from name
  69. const vendor = preset.filament_vendor || parseVendorFromName(preset.name);
  70. // Parse colour for swatch — try explicit colour, then fall back to material type
  71. let colourHex: string | null = null;
  72. let hasExplicitColour = false;
  73. if (preset.default_filament_colour) {
  74. try {
  75. const parsed = JSON.parse(preset.default_filament_colour);
  76. const raw = Array.isArray(parsed) ? parsed[0] : parsed;
  77. if (typeof raw === 'string' && /^#?[0-9a-fA-F]{6,8}$/.test(raw.replace('#', ''))) {
  78. colourHex = raw.replace('#', '').slice(0, 6);
  79. hasExplicitColour = true;
  80. }
  81. } catch {
  82. const raw = preset.default_filament_colour;
  83. if (/^#?[0-9a-fA-F]{6,8}$/.test(raw.replace('#', ''))) {
  84. colourHex = raw.replace('#', '').slice(0, 6);
  85. hasExplicitColour = true;
  86. }
  87. }
  88. }
  89. if (!colourHex && material) {
  90. colourHex = FILAMENT_TYPE_COLORS[material.toUpperCase()] || null;
  91. }
  92. return (
  93. <Card className="bg-bambu-dark border-bambu-dark-tertiary hover:border-bambu-dark-tertiary/80 transition-colors">
  94. <CardContent className="p-3">
  95. <div className="flex items-start justify-between gap-2">
  96. <div className="flex-1 min-w-0">
  97. <div className="flex items-center gap-2 mb-1">
  98. {/* 1) Color dot — always shown for filament presets, dimmed if no explicit colour */}
  99. {preset.preset_type === 'filament' && (
  100. <div
  101. className={`w-4 h-4 rounded-full border border-white/20 flex-shrink-0 ${
  102. !hasExplicitColour && !colourHex ? 'opacity-25' : !hasExplicitColour ? 'opacity-50' : ''
  103. }`}
  104. style={{ backgroundColor: colourHex ? `#${colourHex}` : '#666' }}
  105. />
  106. )}
  107. <span className="text-sm font-medium text-white truncate">{preset.name}</span>
  108. </div>
  109. <div className="flex items-center gap-2 flex-wrap">
  110. {/* 2) Material tag — fallback to name parsing */}
  111. {material && (
  112. <span className="text-xs px-1.5 py-0.5 rounded bg-bambu-green/20 text-bambu-green">
  113. {material}
  114. </span>
  115. )}
  116. {/* 3) Vendor — fallback to name parsing */}
  117. {vendor && (
  118. <span className="text-xs text-bambu-gray">{vendor}</span>
  119. )}
  120. <span className="text-xs px-1.5 py-0.5 rounded bg-blue-500/20 text-blue-400">
  121. {t('profiles.localProfiles.badge')}
  122. </span>
  123. </div>
  124. </div>
  125. <div className="flex items-center gap-1 flex-shrink-0">
  126. {/* 4) Only delete, no edit */}
  127. {hasPermission('settings:update') && (
  128. <button
  129. onClick={() => onDelete(preset.id)}
  130. className="p-1 text-bambu-gray hover:text-red-400 transition-colors"
  131. title={t('profiles.localProfiles.delete')}
  132. >
  133. <Trash2 className="w-3.5 h-3.5" />
  134. </button>
  135. )}
  136. <button
  137. onClick={() => onExpand(isExpanded ? null : preset.id)}
  138. className="p-1 text-bambu-gray hover:text-white transition-colors"
  139. >
  140. {isExpanded ? <ChevronUp className="w-3.5 h-3.5" /> : <ChevronDown className="w-3.5 h-3.5" />}
  141. </button>
  142. </div>
  143. </div>
  144. {/* 5) Expanded detail — show meaningful fields, hide self-inherits */}
  145. {isExpanded && (
  146. <div className="mt-3 pt-3 border-t border-bambu-dark-tertiary text-xs space-y-1.5">
  147. {material && (
  148. <div className="flex justify-between">
  149. <span className="text-bambu-gray">{t('profiles.localProfiles.filamentType')}</span>
  150. <span className="text-white">{material}</span>
  151. </div>
  152. )}
  153. {vendor && (
  154. <div className="flex justify-between">
  155. <span className="text-bambu-gray">{t('profiles.localProfiles.vendor')}</span>
  156. <span className="text-white">{vendor}</span>
  157. </div>
  158. )}
  159. {preset.nozzle_temp_min != null && preset.nozzle_temp_max != null && (
  160. <div className="flex justify-between">
  161. <span className="text-bambu-gray">{t('profiles.localProfiles.nozzleTemp')}</span>
  162. <span className="text-white">{preset.nozzle_temp_min}–{preset.nozzle_temp_max}°C</span>
  163. </div>
  164. )}
  165. {preset.filament_cost && (
  166. <div className="flex justify-between">
  167. <span className="text-bambu-gray">{t('profiles.localProfiles.cost')}</span>
  168. <span className="text-white">{preset.filament_cost}</span>
  169. </div>
  170. )}
  171. {preset.filament_density && (
  172. <div className="flex justify-between">
  173. <span className="text-bambu-gray">{t('profiles.localProfiles.density')}</span>
  174. <span className="text-white">{preset.filament_density} g/cm³</span>
  175. </div>
  176. )}
  177. {preset.pressure_advance && (
  178. <div className="flex justify-between">
  179. <span className="text-bambu-gray">{t('profiles.localProfiles.pressureAdvance')}</span>
  180. <span className="text-white">{preset.pressure_advance}</span>
  181. </div>
  182. )}
  183. {preset.compatible_printers && (
  184. <div className="flex justify-between">
  185. <span className="text-bambu-gray">{t('profiles.localProfiles.compatiblePrinters')}</span>
  186. <span className="text-white truncate ml-2">
  187. {(() => { try { return JSON.parse(preset.compatible_printers).join(', '); } catch { return preset.compatible_printers; } })()}
  188. </span>
  189. </div>
  190. )}
  191. {/* Only show inherits if different from own name */}
  192. {preset.inherits && preset.inherits !== preset.name && (
  193. <div className="flex justify-between">
  194. <span className="text-bambu-gray">{t('profiles.localProfiles.inheritsFrom')}</span>
  195. <span className="text-white truncate ml-2">{preset.inherits}</span>
  196. </div>
  197. )}
  198. <div className="flex justify-between">
  199. <span className="text-bambu-gray">{t('profiles.localProfiles.source')}</span>
  200. <span className="text-white capitalize">{preset.source}</span>
  201. </div>
  202. </div>
  203. )}
  204. </CardContent>
  205. </Card>
  206. );
  207. }
  208. export function LocalProfilesView() {
  209. const { t } = useTranslation();
  210. const { hasPermission } = useAuth();
  211. const queryClient = useQueryClient();
  212. const { showToast } = useToast();
  213. const [searchQuery, setSearchQuery] = useState('');
  214. const [expandedId, setExpandedId] = useState<number | null>(null);
  215. const [isDragging, setIsDragging] = useState(false);
  216. const [deleteConfirm, setDeleteConfirm] = useState<number | null>(null);
  217. const { data: presets, isLoading } = useQuery({
  218. queryKey: ['localPresets'],
  219. queryFn: () => api.getLocalPresets(),
  220. });
  221. const importMutation = useMutation({
  222. mutationFn: async (files: FileList) => {
  223. const results = [];
  224. for (const file of Array.from(files)) {
  225. const formData = new FormData();
  226. formData.append('file', file);
  227. results.push(await api.importLocalPresets(formData));
  228. }
  229. return results;
  230. },
  231. onSuccess: (results) => {
  232. queryClient.invalidateQueries({ queryKey: ['localPresets'] });
  233. let totalImported = 0;
  234. let totalSkipped = 0;
  235. let totalErrors = 0;
  236. for (const r of results) {
  237. totalImported += r.imported;
  238. totalSkipped += r.skipped;
  239. totalErrors += r.errors.length;
  240. }
  241. if (totalImported > 0) {
  242. showToast(t('profiles.localProfiles.toast.importSuccess', { count: totalImported }));
  243. }
  244. if (totalSkipped > 0) {
  245. showToast(t('profiles.localProfiles.toast.importSkipped', { count: totalSkipped }), 'warning');
  246. }
  247. if (totalErrors > 0) {
  248. showToast(t('profiles.localProfiles.toast.importError', { count: totalErrors }), 'error');
  249. }
  250. },
  251. onError: (err: Error) => {
  252. showToast(err.message, 'error');
  253. },
  254. });
  255. const deleteMutation = useMutation({
  256. mutationFn: (id: number) => api.deleteLocalPreset(id),
  257. onSuccess: () => {
  258. queryClient.invalidateQueries({ queryKey: ['localPresets'] });
  259. setDeleteConfirm(null);
  260. showToast(t('profiles.localProfiles.toast.deleted'));
  261. },
  262. });
  263. const handleFiles = useCallback((files: FileList | null) => {
  264. if (!files || files.length === 0) return;
  265. importMutation.mutate(files);
  266. }, [importMutation]);
  267. const handleDrop = useCallback((e: React.DragEvent) => {
  268. e.preventDefault();
  269. setIsDragging(false);
  270. handleFiles(e.dataTransfer.files);
  271. }, [handleFiles]);
  272. const filterPresets = useCallback((list: LocalPreset[]) => {
  273. if (!searchQuery) return list;
  274. const q = searchQuery.toLowerCase();
  275. return list.filter(p =>
  276. p.name.toLowerCase().includes(q) ||
  277. p.filament_type?.toLowerCase().includes(q) ||
  278. p.filament_vendor?.toLowerCase().includes(q)
  279. );
  280. }, [searchQuery]);
  281. const filaments = useMemo(() => filterPresets(presets?.filament || []), [presets?.filament, filterPresets]);
  282. const printers = useMemo(() => filterPresets(presets?.printer || []), [presets?.printer, filterPresets]);
  283. const processes = useMemo(() => filterPresets(presets?.process || []), [presets?.process, filterPresets]);
  284. const totalCount = filaments.length + printers.length + processes.length;
  285. if (isLoading) {
  286. return (
  287. <div className="flex items-center justify-center py-16">
  288. <Loader2 className="w-8 h-8 text-bambu-green animate-spin" />
  289. </div>
  290. );
  291. }
  292. return (
  293. <div className="space-y-6">
  294. {/* Import Zone */}
  295. {hasPermission('settings:update') && (
  296. <div
  297. onDragOver={(e) => { e.preventDefault(); setIsDragging(true); }}
  298. onDragLeave={() => setIsDragging(false)}
  299. onDrop={handleDrop}
  300. className={`relative border-2 border-dashed rounded-lg p-6 text-center transition-colors ${
  301. isDragging
  302. ? 'border-bambu-green bg-bambu-green/10'
  303. : 'border-bambu-dark-tertiary hover:border-bambu-gray'
  304. }`}
  305. >
  306. <input
  307. type="file"
  308. accept=".json,.zip,.orca_filament,.bbscfg,.bbsflmt"
  309. multiple
  310. className="absolute inset-0 w-full h-full opacity-0 cursor-pointer"
  311. onChange={(e) => handleFiles(e.target.files)}
  312. />
  313. {importMutation.isPending ? (
  314. <div className="flex items-center justify-center gap-2">
  315. <Loader2 className="w-5 h-5 text-bambu-green animate-spin" />
  316. <span className="text-bambu-gray">{t('profiles.localProfiles.importing')}</span>
  317. </div>
  318. ) : (
  319. <>
  320. <Upload className="w-8 h-8 text-bambu-gray mx-auto mb-2" />
  321. <p className="text-sm text-white font-medium">{t('profiles.localProfiles.import')}</p>
  322. <p className="text-xs text-bambu-gray mt-1">{t('profiles.localProfiles.importDesc')}</p>
  323. </>
  324. )}
  325. </div>
  326. )}
  327. {/* Search Bar */}
  328. {totalCount > 0 && (
  329. <div className="relative">
  330. <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray" />
  331. <input
  332. type="text"
  333. value={searchQuery}
  334. onChange={(e) => setSearchQuery(e.target.value)}
  335. placeholder={t('profiles.localProfiles.search')}
  336. className="w-full pl-9 pr-4 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green"
  337. />
  338. </div>
  339. )}
  340. {/* No Presets */}
  341. {totalCount === 0 && !isLoading && (
  342. <div className="text-center py-12">
  343. <HardDrive className="w-12 h-12 text-bambu-gray mx-auto mb-3 opacity-50" />
  344. <p className="text-bambu-gray">{t('profiles.localProfiles.noPresets')}</p>
  345. <p className="text-xs text-bambu-gray/60 mt-1">{t('profiles.localProfiles.importDesc')}</p>
  346. </div>
  347. )}
  348. {/* 3-Column Preset Lists */}
  349. {totalCount > 0 && (
  350. <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
  351. {/* Filament Column */}
  352. {filaments.length > 0 && (
  353. <div>
  354. <div className="flex items-center gap-2 mb-3">
  355. <Droplet className="w-4 h-4 text-bambu-green" />
  356. <h3 className="text-sm font-medium text-white">
  357. {t('profiles.localProfiles.filament')}
  358. </h3>
  359. <span className="text-xs text-bambu-gray">({filaments.length})</span>
  360. </div>
  361. <div className="space-y-2">
  362. {filaments.map(p => (
  363. <PresetCard
  364. key={p.id}
  365. preset={p}
  366. onDelete={(id) => setDeleteConfirm(id)}
  367. onExpand={setExpandedId}
  368. isExpanded={expandedId === p.id}
  369. />
  370. ))}
  371. </div>
  372. </div>
  373. )}
  374. {/* Process Column */}
  375. {processes.length > 0 && (
  376. <div>
  377. <div className="flex items-center gap-2 mb-3">
  378. <Layers className="w-4 h-4 text-blue-400" />
  379. <h3 className="text-sm font-medium text-white">
  380. {t('profiles.localProfiles.process')}
  381. </h3>
  382. <span className="text-xs text-bambu-gray">({processes.length})</span>
  383. </div>
  384. <div className="space-y-2">
  385. {processes.map(p => (
  386. <PresetCard
  387. key={p.id}
  388. preset={p}
  389. onDelete={(id) => setDeleteConfirm(id)}
  390. onExpand={setExpandedId}
  391. isExpanded={expandedId === p.id}
  392. />
  393. ))}
  394. </div>
  395. </div>
  396. )}
  397. {/* Printer Column */}
  398. {printers.length > 0 && (
  399. <div>
  400. <div className="flex items-center gap-2 mb-3">
  401. <Settings2 className="w-4 h-4 text-orange-400" />
  402. <h3 className="text-sm font-medium text-white">
  403. {t('profiles.localProfiles.printer')}
  404. </h3>
  405. <span className="text-xs text-bambu-gray">({printers.length})</span>
  406. </div>
  407. <div className="space-y-2">
  408. {printers.map(p => (
  409. <PresetCard
  410. key={p.id}
  411. preset={p}
  412. onDelete={(id) => setDeleteConfirm(id)}
  413. onExpand={setExpandedId}
  414. isExpanded={expandedId === p.id}
  415. />
  416. ))}
  417. </div>
  418. </div>
  419. )}
  420. </div>
  421. )}
  422. {/* Delete Confirmation Modal */}
  423. {deleteConfirm !== null && (
  424. <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
  425. <div className="bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg p-6 max-w-sm mx-4">
  426. <div className="flex items-center gap-2 mb-3">
  427. <AlertCircle className="w-5 h-5 text-red-400" />
  428. <h3 className="text-white font-medium">{t('profiles.localProfiles.deleteConfirmTitle')}</h3>
  429. </div>
  430. <p className="text-sm text-bambu-gray mb-4">{t('profiles.localProfiles.deleteConfirm')}</p>
  431. <div className="flex justify-end gap-2">
  432. <Button variant="secondary" size="sm" onClick={() => setDeleteConfirm(null)}>
  433. {t('profiles.localProfiles.cancel')}
  434. </Button>
  435. <Button
  436. variant="danger"
  437. size="sm"
  438. onClick={() => deleteMutation.mutate(deleteConfirm)}
  439. disabled={deleteMutation.isPending}
  440. >
  441. {deleteMutation.isPending ? <Loader2 className="w-4 h-4 animate-spin" /> : <Trash2 className="w-4 h-4" />}
  442. {t('profiles.localProfiles.delete')}
  443. </Button>
  444. </div>
  445. </div>
  446. </div>
  447. )}
  448. </div>
  449. );
  450. }