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