FileManagerModal.tsx 29 KB

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