FileManagerModal.tsx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. import { useState, useEffect } from 'react';
  2. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  3. import { useTranslation } from 'react-i18next';
  4. import {
  5. X,
  6. Folder,
  7. File,
  8. ChevronLeft,
  9. Download,
  10. Trash2,
  11. Loader2,
  12. HardDrive,
  13. RefreshCw,
  14. Film,
  15. FileBox,
  16. FileText,
  17. Image,
  18. Search,
  19. ArrowUpDown,
  20. CheckSquare,
  21. Square,
  22. MinusSquare,
  23. Box,
  24. } from 'lucide-react';
  25. import { api } from '../api/client';
  26. import { Button } from './Button';
  27. import { ConfirmModal } from './ConfirmModal';
  28. import { ModelViewer } from './ModelViewer';
  29. import { GcodeViewer } from './GcodeViewer';
  30. import type { PlateMetadata } from '../types/plates';
  31. import { useToast } from '../contexts/ToastContext';
  32. import { formatFileSize } from '../utils/file';
  33. interface FileManagerModalProps {
  34. printerId: number;
  35. printerName: string;
  36. onClose: () => void;
  37. }
  38. type PrinterViewerTab = '3d' | 'gcode';
  39. interface PrinterFileViewerModalProps {
  40. printerId: number;
  41. filePath: string;
  42. filename: string;
  43. onClose: () => void;
  44. }
  45. function PrinterFileViewerModal({ printerId, filePath, filename, onClose }: PrinterFileViewerModalProps) {
  46. const [activeTab, setActiveTab] = useState<PrinterViewerTab | null>(null);
  47. const [plates, setPlates] = useState<PlateMetadata[]>([]);
  48. const [platesLoading, setPlatesLoading] = useState(false);
  49. const [selectedPlateId, setSelectedPlateId] = useState<number | null>(null);
  50. const ext = filename.toLowerCase().split('.').pop() || '';
  51. const hasModel = ext === '3mf' || ext === 'stl';
  52. const hasGcode = ext === 'gcode' || ext === '3mf';
  53. useEffect(() => {
  54. setActiveTab(hasModel ? '3d' : hasGcode ? 'gcode' : null);
  55. }, [hasModel, hasGcode]);
  56. useEffect(() => {
  57. setPlates([]);
  58. setSelectedPlateId(null);
  59. if (!hasModel) return;
  60. setPlatesLoading(true);
  61. api.getPrinterFilePlates(printerId, filePath)
  62. .then((data) => setPlates(data.plates || []))
  63. .catch(() => setPlates([]))
  64. .finally(() => setPlatesLoading(false));
  65. }, [filePath, hasModel, printerId]);
  66. const hasMultiplePlates = plates.length > 1;
  67. const selectedPlate = selectedPlateId == null
  68. ? null
  69. : plates.find((plate) => plate.index === selectedPlateId) ?? null;
  70. return (
  71. <div className="fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-6" onClick={onClose}>
  72. <div
  73. className="bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary w-full max-w-4xl h-[80vh] flex flex-col"
  74. onClick={(e) => e.stopPropagation()}
  75. >
  76. <div className="flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary">
  77. <h2 className="text-lg font-semibold text-white truncate flex-1 mr-4">{filename}</h2>
  78. <Button variant="ghost" size="sm" onClick={onClose}>
  79. <X className="w-5 h-5" />
  80. </Button>
  81. </div>
  82. <div className="flex border-b border-bambu-dark-tertiary">
  83. <button
  84. onClick={() => hasModel && setActiveTab('3d')}
  85. disabled={!hasModel}
  86. className={`flex items-center gap-2 px-6 py-3 text-sm font-medium transition-colors ${
  87. activeTab === '3d'
  88. ? 'text-bambu-green border-b-2 border-bambu-green'
  89. : hasModel
  90. ? 'text-bambu-gray hover:text-white'
  91. : 'text-bambu-gray/30 cursor-not-allowed'
  92. }`}
  93. >
  94. <Box className="w-4 h-4" />
  95. 3D Model
  96. {!hasModel && <span className="text-xs">(not available)</span>}
  97. </button>
  98. <button
  99. onClick={() => hasGcode && setActiveTab('gcode')}
  100. disabled={!hasGcode}
  101. className={`flex items-center gap-2 px-6 py-3 text-sm font-medium transition-colors ${
  102. activeTab === 'gcode'
  103. ? 'text-bambu-green border-b-2 border-bambu-green'
  104. : hasGcode
  105. ? 'text-bambu-gray hover:text-white'
  106. : 'text-bambu-gray/30 cursor-not-allowed'
  107. }`}
  108. >
  109. <FileText className="w-4 h-4" />
  110. G-code Preview
  111. {!hasGcode && <span className="text-xs">(not sliced)</span>}
  112. </button>
  113. </div>
  114. <div className="flex-1 overflow-hidden p-4">
  115. {activeTab === '3d' && hasModel ? (
  116. <div className="w-full h-full flex flex-col gap-3">
  117. {hasMultiplePlates && (
  118. <div className="rounded-lg border border-bambu-dark-tertiary bg-bambu-dark p-3">
  119. <div className="flex items-center gap-2 text-sm text-bambu-gray mb-2">
  120. <Box className="w-4 h-4" />
  121. Plates
  122. {platesLoading && <Loader2 className="w-3 h-3 animate-spin" />}
  123. </div>
  124. <div className="grid grid-cols-2 md:grid-cols-3 gap-2">
  125. <button
  126. type="button"
  127. onClick={() => setSelectedPlateId(null)}
  128. className={`flex items-center gap-2 rounded-lg border p-2 text-left transition-colors ${
  129. selectedPlateId == null
  130. ? 'border-bambu-green bg-bambu-green/10'
  131. : 'border-bambu-dark-tertiary bg-bambu-dark-secondary hover:border-bambu-gray'
  132. }`}
  133. >
  134. <div className="w-10 h-10 rounded bg-bambu-dark-tertiary flex items-center justify-center">
  135. <Box className="w-5 h-5 text-bambu-gray" />
  136. </div>
  137. <div className="min-w-0 flex-1">
  138. <p className="text-sm text-white font-medium truncate">All Plates</p>
  139. <p className="text-xs text-bambu-gray truncate">
  140. {plates.length} plate{plates.length !== 1 ? 's' : ''}
  141. </p>
  142. </div>
  143. {selectedPlateId == null && (
  144. <CheckSquare className="w-4 h-4 text-bambu-green flex-shrink-0" />
  145. )}
  146. </button>
  147. {plates.map((plate) => (
  148. <button
  149. key={plate.index}
  150. type="button"
  151. onClick={() => setSelectedPlateId(plate.index)}
  152. className={`flex items-center gap-2 rounded-lg border p-2 text-left transition-colors ${
  153. selectedPlateId === plate.index
  154. ? 'border-bambu-green bg-bambu-green/10'
  155. : 'border-bambu-dark-tertiary bg-bambu-dark-secondary hover:border-bambu-gray'
  156. }`}
  157. >
  158. {plate.has_thumbnail ? (
  159. <img
  160. src={api.getPrinterFilePlateThumbnail(printerId, plate.index, filePath)}
  161. alt={`Plate ${plate.index}`}
  162. className="w-10 h-10 rounded object-cover bg-bambu-dark-tertiary"
  163. />
  164. ) : (
  165. <div className="w-10 h-10 rounded bg-bambu-dark-tertiary flex items-center justify-center">
  166. <Box className="w-5 h-5 text-bambu-gray" />
  167. </div>
  168. )}
  169. <div className="min-w-0 flex-1">
  170. <p className="text-sm text-white font-medium truncate">
  171. {plate.name || `Plate ${plate.index}`}
  172. </p>
  173. <p className="text-xs text-bambu-gray truncate">
  174. {plate.objects.length > 0
  175. ? plate.objects.slice(0, 2).join(', ') + (plate.objects.length > 2 ? '…' : '')
  176. : `${plate.filaments.length} filament${plate.filaments.length !== 1 ? 's' : ''}`}
  177. </p>
  178. </div>
  179. {selectedPlateId === plate.index && (
  180. <CheckSquare className="w-4 h-4 text-bambu-green flex-shrink-0" />
  181. )}
  182. </button>
  183. ))}
  184. </div>
  185. {selectedPlate && (
  186. <div className="mt-3 text-xs text-bambu-gray flex flex-wrap gap-x-4 gap-y-1">
  187. <span>Plate {selectedPlate.index}</span>
  188. {selectedPlate.print_time_seconds != null && (
  189. <span>ETA {Math.round(selectedPlate.print_time_seconds / 60)} min</span>
  190. )}
  191. {selectedPlate.filament_used_grams != null && (
  192. <span>{selectedPlate.filament_used_grams.toFixed(1)} g</span>
  193. )}
  194. {selectedPlate.filaments.length > 0 && (
  195. <span>{selectedPlate.filaments.length} filament{selectedPlate.filaments.length !== 1 ? 's' : ''}</span>
  196. )}
  197. </div>
  198. )}
  199. </div>
  200. )}
  201. <div className="flex-1">
  202. <ModelViewer
  203. url={api.getPrinterFileDownloadUrl(printerId, filePath)}
  204. fileType={ext}
  205. selectedPlateId={selectedPlateId}
  206. className="w-full h-full"
  207. />
  208. </div>
  209. </div>
  210. ) : activeTab === 'gcode' && hasGcode ? (
  211. <GcodeViewer
  212. gcodeUrl={api.getPrinterFileGcodeUrl(printerId, filePath)}
  213. className="w-full h-full"
  214. />
  215. ) : (
  216. <div className="w-full h-full flex items-center justify-center text-bambu-gray">
  217. No preview available for this file
  218. </div>
  219. )}
  220. </div>
  221. </div>
  222. </div>
  223. );
  224. }
  225. function formatStorageSize(bytes: number): string {
  226. if (bytes === 0) return '0 GB';
  227. const gb = bytes / (1024 * 1024 * 1024);
  228. if (gb >= 1) {
  229. return `${gb.toFixed(1)} GB`;
  230. }
  231. const mb = bytes / (1024 * 1024);
  232. return `${mb.toFixed(0)} MB`;
  233. }
  234. function getFileIcon(filename: string, isDirectory: boolean) {
  235. if (isDirectory) return Folder;
  236. const ext = filename.toLowerCase().split('.').pop() || '';
  237. switch (ext) {
  238. case '3mf':
  239. return FileBox;
  240. case 'gcode':
  241. return FileText;
  242. case 'mp4':
  243. case 'avi':
  244. return Film;
  245. case 'png':
  246. case 'jpg':
  247. case 'jpeg':
  248. return Image;
  249. default:
  250. return File;
  251. }
  252. }
  253. type SortOption = 'name-asc' | 'name-desc' | 'size-asc' | 'size-desc' | 'date-asc' | 'date-desc';
  254. const SORT_OPTIONS: { value: SortOption; label: string }[] = [
  255. { value: 'name-asc', label: 'Name (A-Z)' },
  256. { value: 'name-desc', label: 'Name (Z-A)' },
  257. { value: 'size-asc', label: 'Size (smallest)' },
  258. { value: 'size-desc', label: 'Size (largest)' },
  259. { value: 'date-asc', label: 'Date (oldest)' },
  260. { value: 'date-desc', label: 'Date (newest)' },
  261. ];
  262. export function FileManagerModal({ printerId, printerName, onClose }: FileManagerModalProps) {
  263. const { t } = useTranslation();
  264. const { showToast } = useToast();
  265. const queryClient = useQueryClient();
  266. const [currentPath, setCurrentPath] = useState('/');
  267. const [selectedFiles, setSelectedFiles] = useState<Set<string>>(new Set());
  268. const [searchQuery, setSearchQuery] = useState('');
  269. const [filesToDelete, setFilesToDelete] = useState<string[]>([]);
  270. const [sortBy, setSortBy] = useState<SortOption>('name-asc');
  271. const [downloadProgress, setDownloadProgress] = useState<{ current: number; total: number } | null>(null);
  272. const [viewerFile, setViewerFile] = useState<{ path: string; name: string } | null>(null);
  273. // Close on Escape key
  274. useEffect(() => {
  275. const handleKeyDown = (e: KeyboardEvent) => {
  276. if (e.key === 'Escape') onClose();
  277. };
  278. window.addEventListener('keydown', handleKeyDown);
  279. return () => window.removeEventListener('keydown', handleKeyDown);
  280. }, [onClose]);
  281. const { data, isLoading, refetch } = useQuery({
  282. queryKey: ['printerFiles', printerId, currentPath],
  283. queryFn: () => api.getPrinterFiles(printerId, currentPath),
  284. });
  285. const { data: storageData } = useQuery({
  286. queryKey: ['printerStorage', printerId],
  287. queryFn: () => api.getPrinterStorage(printerId),
  288. staleTime: 30000, // Cache for 30 seconds
  289. });
  290. const deleteMutation = useMutation({
  291. mutationFn: async (paths: string[]) => {
  292. // Delete files one by one
  293. for (const path of paths) {
  294. await api.deletePrinterFile(printerId, path);
  295. }
  296. },
  297. onSuccess: () => {
  298. showToast(t('printerFiles.toast.filesDeleted', { count: filesToDelete.length }));
  299. queryClient.invalidateQueries({ queryKey: ['printerFiles', printerId] });
  300. setSelectedFiles(new Set());
  301. setFilesToDelete([]);
  302. },
  303. onError: (error: Error) => {
  304. showToast(t('printerFiles.toast.deleteFailed', { error: error.message }), 'error');
  305. },
  306. });
  307. const navigateToFolder = (path: string) => {
  308. setCurrentPath(path);
  309. setSelectedFiles(new Set());
  310. };
  311. const navigateUp = () => {
  312. if (currentPath === '/') return;
  313. const parts = currentPath.split('/').filter(Boolean);
  314. parts.pop();
  315. setCurrentPath(parts.length ? '/' + parts.join('/') : '/');
  316. setSelectedFiles(new Set());
  317. };
  318. const toggleFileSelection = (path: string, e: React.MouseEvent) => {
  319. e.stopPropagation();
  320. setSelectedFiles(prev => {
  321. const next = new Set(prev);
  322. if (next.has(path)) {
  323. next.delete(path);
  324. } else {
  325. next.add(path);
  326. }
  327. return next;
  328. });
  329. };
  330. const selectAllFiles = () => {
  331. if (!data?.files) return;
  332. const filePaths = data.files
  333. .filter(f => !f.is_directory && (!searchQuery || f.name.toLowerCase().includes(searchQuery.toLowerCase())))
  334. .map(f => f.path);
  335. setSelectedFiles(new Set(filePaths));
  336. };
  337. const deselectAllFiles = () => {
  338. setSelectedFiles(new Set());
  339. };
  340. const handleDownload = async () => {
  341. if (selectedFiles.size === 0) return;
  342. const paths = Array.from(selectedFiles);
  343. if (paths.length === 1) {
  344. // Single file - direct download with auth
  345. api.downloadPrinterFile(printerId, paths[0]).catch((err) => {
  346. console.error('Printer file download failed:', err);
  347. });
  348. setSelectedFiles(new Set());
  349. return;
  350. }
  351. // Multiple files - download as ZIP
  352. setDownloadProgress({ current: 0, total: paths.length });
  353. try {
  354. const blob = await api.downloadPrinterFilesAsZip(printerId, paths);
  355. const url = URL.createObjectURL(blob);
  356. const a = document.createElement('a');
  357. a.href = url;
  358. a.download = `${printerName.replace(/[^a-zA-Z0-9]/g, '_')}-files.zip`;
  359. document.body.appendChild(a);
  360. a.click();
  361. document.body.removeChild(a);
  362. URL.revokeObjectURL(url);
  363. showToast(`Downloaded ${paths.length} files as ZIP`);
  364. setSelectedFiles(new Set());
  365. } catch (error) {
  366. showToast(`Download failed: ${error instanceof Error ? error.message : 'Unknown error'}`, 'error');
  367. } finally {
  368. setDownloadProgress(null);
  369. }
  370. };
  371. const handleDelete = () => {
  372. if (selectedFiles.size === 0) return;
  373. setFilesToDelete(Array.from(selectedFiles));
  374. };
  375. // Quick navigation buttons for common directories
  376. const quickDirs = [
  377. { path: '/', label: 'Root' },
  378. { path: '/cache', label: 'Cache' },
  379. { path: '/model', label: 'Models' },
  380. { path: '/timelapse', label: 'Timelapse' },
  381. ];
  382. return (
  383. <div
  384. className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4"
  385. onClick={onClose}
  386. >
  387. <div
  388. className="w-full max-w-3xl max-h-[85vh] flex flex-col bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary overflow-hidden"
  389. onClick={(e) => e.stopPropagation()}
  390. >
  391. {/* Header */}
  392. <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary flex-shrink-0">
  393. <div className="flex items-center gap-3">
  394. <HardDrive className="w-5 h-5 text-bambu-green" />
  395. <div>
  396. <h2 className="text-lg font-semibold text-white">{t('printerFiles.title')}</h2>
  397. <p className="text-sm text-bambu-gray">{printerName}</p>
  398. </div>
  399. </div>
  400. <div className="flex items-center gap-4">
  401. {/* Storage info */}
  402. {storageData && (storageData.used_bytes != null || storageData.free_bytes != null) && (
  403. <div className="text-sm text-bambu-gray flex items-center gap-2">
  404. {storageData.used_bytes != null && (
  405. <span>{t('printerFiles.storageUsed')} {formatStorageSize(storageData.used_bytes)}</span>
  406. )}
  407. {storageData.used_bytes != null && storageData.free_bytes != null && (
  408. <span className="text-bambu-dark-tertiary">|</span>
  409. )}
  410. {storageData.free_bytes != null && (
  411. <span>{t('printerFiles.storageFree')} {formatStorageSize(storageData.free_bytes)}</span>
  412. )}
  413. </div>
  414. )}
  415. <button
  416. onClick={onClose}
  417. className="text-bambu-gray hover:text-white transition-colors"
  418. title="Close file manager"
  419. aria-label="Close file manager"
  420. >
  421. <X className="w-5 h-5" />
  422. </button>
  423. </div>
  424. </div>
  425. {/* Quick Navigation */}
  426. <div className="flex items-center gap-2 p-3 border-b border-bambu-dark-tertiary bg-bambu-dark/50 flex-shrink-0">
  427. {quickDirs.map((dir) => (
  428. <button
  429. key={dir.path}
  430. onClick={() => {
  431. navigateToFolder(dir.path);
  432. setSearchQuery('');
  433. }}
  434. className={`px-3 py-1 text-sm rounded-full transition-colors ${
  435. currentPath === dir.path
  436. ? 'bg-bambu-green text-white'
  437. : 'bg-bambu-dark-tertiary text-bambu-gray hover:text-white'
  438. }`}
  439. >
  440. {dir.label}
  441. </button>
  442. ))}
  443. <div className="flex-1" />
  444. <div className="relative">
  445. <Search className="absolute left-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray" />
  446. <input
  447. type="text"
  448. placeholder={t('printerFiles.filterPlaceholder')}
  449. value={searchQuery}
  450. onChange={(e) => setSearchQuery(e.target.value)}
  451. className="w-40 pl-8 pr-3 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none"
  452. />
  453. </div>
  454. <div className="relative flex items-center gap-1">
  455. <ArrowUpDown className="w-4 h-4 text-bambu-gray" />
  456. <select
  457. value={sortBy}
  458. onChange={(e) => setSortBy(e.target.value as SortOption)}
  459. className="appearance-none bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm py-1.5 pl-2 pr-6 focus:border-bambu-green focus:outline-none cursor-pointer"
  460. title="Sort files"
  461. aria-label="Sort files"
  462. >
  463. {SORT_OPTIONS.map((option) => (
  464. <option key={option.value} value={option.value}>
  465. {option.label}
  466. </option>
  467. ))}
  468. </select>
  469. </div>
  470. <Button
  471. variant="secondary"
  472. size="sm"
  473. onClick={() => refetch()}
  474. disabled={isLoading}
  475. >
  476. <RefreshCw className={`w-4 h-4 ${isLoading ? 'animate-spin' : ''}`} />
  477. </Button>
  478. </div>
  479. {/* Path breadcrumb */}
  480. <div className="flex items-center gap-2 px-4 py-2 bg-bambu-dark text-sm flex-shrink-0">
  481. <button
  482. onClick={navigateUp}
  483. disabled={currentPath === '/'}
  484. className="p-1 rounded hover:bg-bambu-dark-tertiary disabled:opacity-50 disabled:cursor-not-allowed"
  485. title="Go to parent folder"
  486. aria-label="Go to parent folder"
  487. >
  488. <ChevronLeft className="w-4 h-4" />
  489. </button>
  490. <span className="text-bambu-gray font-mono">{currentPath}</span>
  491. </div>
  492. {/* File list */}
  493. <div className="flex-1 overflow-y-auto p-2 min-h-0">
  494. {isLoading ? (
  495. <div className="flex items-center justify-center py-12">
  496. <Loader2 className="w-8 h-8 text-bambu-green animate-spin" />
  497. </div>
  498. ) : !data?.files?.length ? (
  499. <div className="text-center py-12 text-bambu-gray">
  500. No files in this directory
  501. </div>
  502. ) : (
  503. <div className="space-y-1">
  504. {/* Filter and sort: directories first, then files with selected sort */}
  505. {[...data.files]
  506. .filter((file) =>
  507. !searchQuery || file.name.toLowerCase().includes(searchQuery.toLowerCase())
  508. )
  509. .sort((a, b) => {
  510. // Directories always first
  511. if (a.is_directory && !b.is_directory) return -1;
  512. if (!a.is_directory && b.is_directory) return 1;
  513. // Apply selected sort within same type
  514. switch (sortBy) {
  515. case 'name-asc':
  516. return a.name.localeCompare(b.name);
  517. case 'name-desc':
  518. return b.name.localeCompare(a.name);
  519. case 'size-asc':
  520. return a.size - b.size;
  521. case 'size-desc':
  522. return b.size - a.size;
  523. case 'date-asc': {
  524. const aTime = a.mtime ? new Date(a.mtime).getTime() : 0;
  525. const bTime = b.mtime ? new Date(b.mtime).getTime() : 0;
  526. return aTime - bTime;
  527. }
  528. case 'date-desc': {
  529. const aTime = a.mtime ? new Date(a.mtime).getTime() : 0;
  530. const bTime = b.mtime ? new Date(b.mtime).getTime() : 0;
  531. return bTime - aTime;
  532. }
  533. default:
  534. return a.name.localeCompare(b.name);
  535. }
  536. })
  537. .map((file) => {
  538. const FileIcon = getFileIcon(file.name, file.is_directory);
  539. const isSelected = selectedFiles.has(file.path);
  540. return (
  541. <div
  542. key={file.path}
  543. className={`flex items-center gap-3 p-2 rounded-lg cursor-pointer transition-colors ${
  544. isSelected
  545. ? 'bg-bambu-green/20 border border-bambu-green/50'
  546. : 'hover:bg-bambu-dark-tertiary'
  547. }`}
  548. onClick={() => {
  549. if (file.is_directory) {
  550. navigateToFolder(file.path);
  551. }
  552. }}
  553. >
  554. {/* Checkbox for files only */}
  555. {!file.is_directory ? (
  556. <button
  557. onClick={(e) => toggleFileSelection(file.path, e)}
  558. className="flex-shrink-0 text-bambu-gray hover:text-white"
  559. >
  560. {isSelected ? (
  561. <CheckSquare className="w-5 h-5 text-bambu-green" />
  562. ) : (
  563. <Square className="w-5 h-5" />
  564. )}
  565. </button>
  566. ) : null}
  567. <FileIcon
  568. className={`w-5 h-5 flex-shrink-0 ${
  569. file.is_directory ? 'text-bambu-green' : 'text-bambu-gray'
  570. }`}
  571. />
  572. <span className="flex-1 text-white truncate">{file.name}</span>
  573. {!file.is_directory && (
  574. <div className="flex items-center gap-3">
  575. <span className="text-sm text-bambu-gray">
  576. {formatFileSize(file.size)}
  577. </span>
  578. {(file.name.toLowerCase().endsWith('.3mf') || file.name.toLowerCase().endsWith('.gcode') || file.name.toLowerCase().endsWith('.stl')) && (
  579. <button
  580. onClick={(e) => {
  581. e.stopPropagation();
  582. setViewerFile({ path: file.path, name: file.name });
  583. }}
  584. className="p-1 rounded hover:bg-bambu-dark text-bambu-gray hover:text-bambu-green"
  585. title="3D View"
  586. >
  587. <Box className="w-4 h-4" />
  588. </button>
  589. )}
  590. </div>
  591. )}
  592. {file.is_directory && (
  593. <ChevronLeft className="w-4 h-4 text-bambu-gray rotate-180" />
  594. )}
  595. </div>
  596. );
  597. })}
  598. </div>
  599. )}
  600. </div>
  601. {/* Action bar */}
  602. <div className="flex items-center justify-between p-4 border-t border-bambu-dark-tertiary bg-bambu-dark/50 flex-shrink-0">
  603. <div className="flex items-center gap-4">
  604. <div className="text-sm text-bambu-gray">
  605. {selectedFiles.size > 0
  606. ? `${selectedFiles.size} selected`
  607. : searchQuery
  608. ? `${data?.files?.filter(f => f.name.toLowerCase().includes(searchQuery.toLowerCase())).length || 0} of ${data?.files?.length || 0} items`
  609. : `${data?.files?.length || 0} items`
  610. }
  611. </div>
  612. {/* Select All / Deselect All */}
  613. {data?.files?.some(f => !f.is_directory) && (
  614. <div className="flex items-center gap-2">
  615. {selectedFiles.size > 0 ? (
  616. <button
  617. onClick={deselectAllFiles}
  618. className="flex items-center gap-1 text-xs text-bambu-gray hover:text-white transition-colors"
  619. >
  620. <MinusSquare className="w-4 h-4" />
  621. Deselect All
  622. </button>
  623. ) : (
  624. <button
  625. onClick={selectAllFiles}
  626. className="flex items-center gap-1 text-xs text-bambu-gray hover:text-white transition-colors"
  627. >
  628. <CheckSquare className="w-4 h-4" />
  629. Select All
  630. </button>
  631. )}
  632. </div>
  633. )}
  634. </div>
  635. <div className="flex gap-2">
  636. <Button
  637. variant="secondary"
  638. disabled={selectedFiles.size === 0 || downloadProgress !== null}
  639. onClick={handleDownload}
  640. >
  641. {downloadProgress ? (
  642. <>
  643. <Loader2 className="w-4 h-4 animate-spin" />
  644. {downloadProgress.current}/{downloadProgress.total}
  645. </>
  646. ) : (
  647. <>
  648. <Download className="w-4 h-4" />
  649. Download{selectedFiles.size > 1 ? ` (${selectedFiles.size})` : ''}
  650. </>
  651. )}
  652. </Button>
  653. <Button
  654. variant="secondary"
  655. disabled={selectedFiles.size === 0 || deleteMutation.isPending}
  656. onClick={handleDelete}
  657. className="text-red-400 hover:text-red-300"
  658. >
  659. {deleteMutation.isPending ? (
  660. <Loader2 className="w-4 h-4 animate-spin" />
  661. ) : (
  662. <Trash2 className="w-4 h-4" />
  663. )}
  664. {t('printerFiles.deleteButton')}{selectedFiles.size > 1 ? ` (${selectedFiles.size})` : ''}
  665. </Button>
  666. </div>
  667. </div>
  668. </div>
  669. {/* Delete Confirmation Modal */}
  670. {filesToDelete.length > 0 && (
  671. <ConfirmModal
  672. title={filesToDelete.length > 1 ? t('printerFiles.deleteFiles', { count: filesToDelete.length }) : t('fileManager.deleteFile')}
  673. message={
  674. filesToDelete.length > 1
  675. ? t('printerFiles.deleteFilesConfirm', { count: filesToDelete.length })
  676. : t('printerFiles.deleteFileConfirm', { name: filesToDelete[0].split('/').pop() })
  677. }
  678. confirmText={t('common.delete')}
  679. variant="danger"
  680. onConfirm={() => {
  681. deleteMutation.mutate(filesToDelete);
  682. }}
  683. onCancel={() => setFilesToDelete([])}
  684. />
  685. )}
  686. {viewerFile && (
  687. <PrinterFileViewerModal
  688. printerId={printerId}
  689. filePath={viewerFile.path}
  690. filename={viewerFile.name}
  691. onClose={() => setViewerFile(null)}
  692. />
  693. )}
  694. </div>
  695. );
  696. }