import { useState, useRef, useCallback, useMemo, useEffect, type DragEvent } from 'react';
import { useSearchParams } from 'react-router-dom';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import {
FolderOpen,
Loader2,
Plus,
Upload,
Trash2,
Download,
MoreVertical,
ChevronRight,
FolderPlus,
FileBox,
Clock,
HardDrive,
File,
MoveRight,
CheckSquare,
Square,
LayoutGrid,
List,
Search,
SortAsc,
SortDesc,
AlertTriangle,
Filter,
X,
CheckCircle,
XCircle,
Link2,
Unlink,
Archive as ArchiveIcon,
Briefcase,
Printer,
Pencil,
Play,
} from 'lucide-react';
import { api } from '../api/client';
import type {
LibraryFolderTree,
LibraryFileListItem,
LibraryFolderCreate,
LibraryFolderUpdate,
AppSettings,
Archive,
} from '../api/client';
import { Button } from '../components/Button';
import { ConfirmModal } from '../components/ConfirmModal';
import { PrintModal } from '../components/PrintModal';
import { useToast } from '../contexts/ToastContext';
type SortField = 'name' | 'date' | 'size' | 'type' | 'prints';
type SortDirection = 'asc' | 'desc';
// Utility to format file size
function formatFileSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
}
// Utility to format duration
function formatDuration(seconds: number | null): string {
if (!seconds) return '-';
const hours = Math.floor(seconds / 3600);
const mins = Math.floor((seconds % 3600) / 60);
if (hours > 0) return `${hours}h ${mins}m`;
return `${mins}m`;
}
// New Folder Modal
interface NewFolderModalProps {
parentId: number | null;
onClose: () => void;
onSave: (data: LibraryFolderCreate) => void;
isLoading: boolean;
}
function NewFolderModal({ parentId, onClose, onSave, isLoading }: NewFolderModalProps) {
const [name, setName] = useState('');
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
onSave({ name: name.trim(), parent_id: parentId });
};
return (
);
}
// Rename Modal
interface RenameModalProps {
type: 'file' | 'folder';
currentName: string;
onClose: () => void;
onSave: (newName: string) => void;
isLoading: boolean;
}
function RenameModal({ type, currentName, onClose, onSave, isLoading }: RenameModalProps) {
const [name, setName] = useState(currentName);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (name.trim() && name.trim() !== currentName) {
onSave(name.trim());
}
};
return (
Rename {type === 'file' ? 'File' : 'Folder'}
);
}
// Move Files Modal
interface MoveFilesModalProps {
folders: LibraryFolderTree[];
selectedFiles: number[];
currentFolderId: number | null;
onClose: () => void;
onMove: (folderId: number | null) => void;
isLoading: boolean;
}
function MoveFilesModal({ folders, selectedFiles, currentFolderId, onClose, onMove, isLoading }: MoveFilesModalProps) {
const [targetFolder, setTargetFolder] = useState(null);
const flattenFolders = (items: LibraryFolderTree[], depth = 0): { id: number | null; name: string; depth: number }[] => {
const result: { id: number | null; name: string; depth: number }[] = [];
for (const item of items) {
result.push({ id: item.id, name: item.name, depth });
if (item.children.length > 0) {
result.push(...flattenFolders(item.children, depth + 1));
}
}
return result;
};
const flatFolders = [{ id: null, name: 'Root (No Folder)', depth: 0 }, ...flattenFolders(folders)];
return (
Move {selectedFiles.length} File(s)
{flatFolders.map((folder) => (
setTargetFolder(folder.id)}
disabled={folder.id === currentFolderId}
className={`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${
targetFolder === folder.id
? 'bg-bambu-green/20 text-bambu-green'
: folder.id === currentFolderId
? 'opacity-50 cursor-not-allowed text-bambu-gray'
: 'hover:bg-bambu-dark text-white'
}`}
style={{ paddingLeft: `${12 + folder.depth * 16}px` }}
>
{folder.name}
{folder.id === currentFolderId && (current) }
))}
Cancel
onMove(targetFolder)} disabled={isLoading}>
{isLoading ? : 'Move'}
);
}
// Link Folder Modal
interface LinkFolderModalProps {
folder: LibraryFolderTree;
onClose: () => void;
onLink: (update: LibraryFolderUpdate) => void;
isLoading: boolean;
}
function LinkFolderModal({ folder, onClose, onLink, isLoading }: LinkFolderModalProps) {
const [linkType, setLinkType] = useState<'project' | 'archive'>('project');
const [selectedId, setSelectedId] = useState(
folder.project_id || folder.archive_id || null
);
// Initialize linkType based on existing link
useState(() => {
if (folder.archive_id) setLinkType('archive');
});
const { data: projects } = useQuery({
queryKey: ['projects'],
queryFn: () => api.getProjects(),
});
const { data: archives } = useQuery({
queryKey: ['archives-for-link'],
queryFn: () => api.getArchives(undefined, undefined, 100),
});
const handleSave = () => {
if (linkType === 'project') {
onLink({
project_id: selectedId,
archive_id: 0, // Unlink archive
});
} else {
onLink({
project_id: 0, // Unlink project
archive_id: selectedId,
});
}
};
const handleUnlink = () => {
onLink({
project_id: 0,
archive_id: 0,
});
};
const isLinked = folder.project_id || folder.archive_id;
return (
Link Folder
Link "{folder.name} " to a project or archive for quick access.
{/* Link type selector */}
{ setLinkType('project'); setSelectedId(null); }}
className={`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg border transition-colors ${
linkType === 'project'
? 'border-bambu-green bg-bambu-green/10 text-bambu-green'
: 'border-bambu-dark-tertiary text-bambu-gray hover:text-white'
}`}
>
Project
{ setLinkType('archive'); setSelectedId(null); }}
className={`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg border transition-colors ${
linkType === 'archive'
? 'border-bambu-green bg-bambu-green/10 text-bambu-green'
: 'border-bambu-dark-tertiary text-bambu-gray hover:text-white'
}`}
>
Archive
{/* Selection list */}
{linkType === 'project' ? (
projects && projects.length > 0 ? (
projects.map((project) => (
setSelectedId(project.id)}
className={`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${
selectedId === project.id
? 'bg-bambu-green/20 text-bambu-green'
: 'hover:bg-bambu-dark-tertiary text-white'
}`}
>
{project.name}
))
) : (
No projects found
)
) : (
archives && archives.length > 0 ? (
archives.map((archive: Archive) => (
setSelectedId(archive.id)}
className={`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${
selectedId === archive.id
? 'bg-bambu-green/20 text-bambu-green'
: 'hover:bg-bambu-dark-tertiary text-white'
}`}
>
{archive.print_name || archive.filename}
))
) : (
No archives found
)
)}
{isLinked && (
Unlink
)}
Cancel
{isLoading ? : 'Link'}
);
}
// Upload Modal with Drag & Drop
interface UploadModalProps {
folderId: number | null;
onClose: () => void;
onUploadComplete: () => void;
}
interface UploadFile {
file: File;
status: 'pending' | 'uploading' | 'success' | 'error';
error?: string;
isZip?: boolean;
extractedCount?: number;
}
function UploadModal({ folderId, onClose, onUploadComplete }: UploadModalProps) {
const [files, setFiles] = useState([]);
const [isDragging, setIsDragging] = useState(false);
const [isUploading, setIsUploading] = useState(false);
const [preserveZipStructure, setPreserveZipStructure] = useState(true);
const [createFolderFromZip, setCreateFolderFromZip] = useState(false);
const fileInputRef = useRef(null);
const handleDragOver = (e: DragEvent) => {
e.preventDefault();
setIsDragging(true);
};
const handleDragLeave = (e: DragEvent) => {
e.preventDefault();
setIsDragging(false);
};
const handleDrop = (e: DragEvent) => {
e.preventDefault();
setIsDragging(false);
const droppedFiles = Array.from(e.dataTransfer.files);
addFiles(droppedFiles);
};
const handleFileSelect = (e: React.ChangeEvent) => {
if (e.target.files) {
addFiles(Array.from(e.target.files));
}
};
const addFiles = (newFiles: File[]) => {
const uploadFiles: UploadFile[] = newFiles.map((file) => ({
file,
status: 'pending',
isZip: file.name.toLowerCase().endsWith('.zip'),
}));
setFiles((prev) => [...prev, ...uploadFiles]);
};
const removeFile = (index: number) => {
setFiles((prev) => prev.filter((_, i) => i !== index));
};
const hasZipFiles = files.some((f) => f.isZip && f.status === 'pending');
const handleUpload = async () => {
if (files.length === 0) return;
setIsUploading(true);
for (let i = 0; i < files.length; i++) {
if (files[i].status !== 'pending') continue;
setFiles((prev) =>
prev.map((f, idx) => (idx === i ? { ...f, status: 'uploading' } : f))
);
try {
if (files[i].isZip) {
// Extract ZIP file
const result = await api.extractZipFile(files[i].file, folderId, preserveZipStructure, createFolderFromZip);
setFiles((prev) =>
prev.map((f, idx) =>
idx === i
? {
...f,
status: result.errors.length > 0 && result.extracted === 0 ? 'error' : 'success',
extractedCount: result.extracted,
error: result.errors.length > 0 ? `${result.errors.length} files failed` : undefined,
}
: f
)
);
} else {
// Regular file upload
await api.uploadLibraryFile(files[i].file, folderId);
setFiles((prev) =>
prev.map((f, idx) => (idx === i ? { ...f, status: 'success' } : f))
);
}
} catch (err) {
setFiles((prev) =>
prev.map((f, idx) =>
idx === i
? { ...f, status: 'error', error: err instanceof Error ? err.message : 'Upload failed' }
: f
)
);
}
}
setIsUploading(false);
onUploadComplete();
// Auto-close modal after upload completes
onClose();
};
const pendingCount = files.filter((f) => f.status === 'pending').length;
const successCount = files.filter((f) => f.status === 'success').length;
const errorCount = files.filter((f) => f.status === 'error').length;
const allDone = files.length > 0 && pendingCount === 0 && !isUploading;
return (
Upload Files
{/* Drop Zone */}
fileInputRef.current?.click()}
className={`border-2 border-dashed rounded-lg p-8 text-center cursor-pointer transition-colors ${
isDragging
? 'border-bambu-green bg-bambu-green/10'
: 'border-bambu-dark-tertiary hover:border-bambu-green/50'
}`}
>
{isDragging ? 'Drop files here' : 'Drag & drop files here'}
or click to browse
All file types supported. ZIP files will be extracted.
{/* ZIP Options */}
{hasZipFiles && (
)}
{/* File List */}
{files.length > 0 && (
{files.map((uploadFile, index) => (
{uploadFile.isZip ? (
) : (
)}
{uploadFile.file.name}
{(uploadFile.file.size / 1024 / 1024).toFixed(2)} MB
{uploadFile.isZip && uploadFile.status === 'pending' && (
• Will be extracted
)}
{uploadFile.extractedCount !== undefined && (
• {uploadFile.extractedCount} files extracted
)}
{uploadFile.status === 'pending' && (
removeFile(index)}
className="p-1 hover:bg-bambu-dark-tertiary rounded"
>
)}
{uploadFile.status === 'uploading' && (
)}
{uploadFile.status === 'success' && (
)}
{uploadFile.status === 'error' && (
)}
))}
)}
{/* Summary */}
{allDone && (
Upload complete: {successCount} succeeded
{errorCount > 0 && , {errorCount} failed }
)}
{allDone ? 'Close' : 'Cancel'}
{!allDone && (
{isUploading ? (
<>
Uploading...
>
) : (
<>
Upload {pendingCount > 0 ? `(${pendingCount})` : ''}
>
)}
)}
);
}
// Folder Tree Item
interface FolderTreeItemProps {
folder: LibraryFolderTree;
selectedFolderId: number | null;
onSelect: (id: number | null) => void;
onDelete: (id: number) => void;
onLink: (folder: LibraryFolderTree) => void;
onRename: (folder: LibraryFolderTree) => void;
depth?: number;
wrapNames?: boolean;
}
function FolderTreeItem({ folder, selectedFolderId, onSelect, onDelete, onLink, onRename, depth = 0, wrapNames = false }: FolderTreeItemProps) {
const [expanded, setExpanded] = useState(true);
const [showActions, setShowActions] = useState(false);
const hasChildren = folder.children.length > 0;
const isLinked = folder.project_id || folder.archive_id;
return (
onSelect(folder.id)}
>
{hasChildren ? (
{
e.stopPropagation();
setExpanded(!expanded);
}}
className="p-0.5 hover:bg-bambu-dark-tertiary rounded"
>
) : (
)}
{folder.name}
{/* Link indicator - clickable to change link */}
{isLinked && (
{ e.stopPropagation(); onLink(folder); }}
className="flex-shrink-0 flex items-center gap-1 text-xs px-1.5 py-0.5 rounded bg-blue-500/20 text-blue-400 hover:bg-blue-500/30 transition-colors"
title={`${folder.project_name ? `Project: ${folder.project_name}` : `Archive: ${folder.archive_name}`} (click to change)`}
>
{folder.project_name ? (
) : (
)}
)}
{folder.file_count > 0 && (
{folder.file_count}
)}
{/* Quick link button - always visible for unlinked folders */}
{!isLinked && (
{ e.stopPropagation(); onLink(folder); }}
className="flex-shrink-0 p-1 rounded hover:bg-bambu-dark-tertiary"
title="Link to project or archive"
>
)}
e.stopPropagation()}>
setShowActions(!showActions)}
className="p-1 rounded hover:bg-bambu-dark-tertiary"
>
{showActions && (
<>
setShowActions(false)} />
{ onRename(folder); setShowActions(false); }}
>
Rename
{ onLink(folder); setShowActions(false); }}
>
{isLinked ? 'Change Link...' : 'Link to...'}
{ onDelete(folder.id); setShowActions(false); }}
>
Delete
>
)}
{hasChildren && expanded && (
{folder.children.map((child) => (
))}
)}
);
}
// Helper to check if a file is sliced (printable)
function isSlicedFilename(filename: string): boolean {
const lower = filename.toLowerCase();
return lower.endsWith('.gcode') || lower.includes('.gcode.');
}
// File Card
interface FileCardProps {
file: LibraryFileListItem;
isSelected: boolean;
onSelect: (id: number) => void;
onDelete: (id: number) => void;
onDownload: (id: number) => void;
onAddToQueue?: (id: number) => void;
onPrint?: (file: LibraryFileListItem) => void;
onRename?: (file: LibraryFileListItem) => void;
}
function FileCard({ file, isSelected, onSelect, onDelete, onDownload, onAddToQueue, onPrint, onRename }: FileCardProps) {
const [showActions, setShowActions] = useState(false);
return (
onSelect(file.id)}
>
{/* Thumbnail */}
{file.thumbnail_path ? (
) : (
)}
{/* File type badge */}
{file.file_type.toUpperCase()}
{/* Info */}
{file.print_name || file.filename}
{formatFileSize(file.file_size)}
{file.print_time_seconds && (
{formatDuration(file.print_time_seconds)}
)}
{file.print_count > 0 && (
Printed {file.print_count}x
)}
{/* Actions - always visible on mobile, hover on desktop */}
e.stopPropagation()}>
setShowActions(!showActions)}
className="p-1.5 rounded bg-bambu-dark-secondary/90 hover:bg-bambu-dark-tertiary"
>
{showActions && (
<>
setShowActions(false)} />
{onPrint && isSlicedFilename(file.filename) && (
{ onPrint(file); setShowActions(false); }}
>
Print
)}
{onAddToQueue && isSlicedFilename(file.filename) && (
{ onAddToQueue(file.id); setShowActions(false); }}
>
Add to Queue
)}
{ onDownload(file.id); setShowActions(false); }}
>
Download
{onRename && (
{ onRename(file); setShowActions(false); }}
>
Rename
)}
{ onDelete(file.id); setShowActions(false); }}
>
Delete
>
)}
{/* Selection checkbox - always visible on mobile, hover on desktop */}
);
}
export function FileManagerPage() {
const queryClient = useQueryClient();
const { showToast } = useToast();
const [searchParams] = useSearchParams();
// Read folder ID from URL query parameter
const folderIdFromUrl = searchParams.get('folder');
const initialFolderId = folderIdFromUrl ? parseInt(folderIdFromUrl, 10) : null;
// State
const [selectedFolderId, setSelectedFolderId] = useState
(initialFolderId);
const [selectedFiles, setSelectedFiles] = useState([]);
const [showNewFolderModal, setShowNewFolderModal] = useState(false);
const [showMoveModal, setShowMoveModal] = useState(false);
const [showUploadModal, setShowUploadModal] = useState(false);
const [linkFolder, setLinkFolder] = useState(null);
const [deleteConfirm, setDeleteConfirm] = useState<{ type: 'file' | 'folder' | 'bulk'; id: number; count?: number } | null>(null);
const [printFile, setPrintFile] = useState(null);
const [printMultiFile, setPrintMultiFile] = useState(null);
const [renameItem, setRenameItem] = useState<{ type: 'file' | 'folder'; id: number; name: string } | null>(null);
const [viewMode, setViewMode] = useState<'grid' | 'list'>(() => {
return (localStorage.getItem('library-view-mode') as 'grid' | 'list') || 'grid';
});
const [wrapFolderNames, setWrapFolderNames] = useState(() => {
return localStorage.getItem('library-wrap-folders') === 'true';
});
// Resizable sidebar state
const [sidebarWidth, setSidebarWidth] = useState(() => {
const saved = localStorage.getItem('library-sidebar-width');
return saved ? parseInt(saved, 10) : 256; // Default w-64 = 256px
});
const [isResizing, setIsResizing] = useState(false);
const sidebarRef = useRef(null);
// Handle sidebar resize
useEffect(() => {
if (!isResizing) return;
// Prevent text selection during resize
document.body.style.userSelect = 'none';
document.body.style.cursor = 'col-resize';
const handleMouseMove = (e: MouseEvent) => {
if (!sidebarRef.current) return;
const containerRect = sidebarRef.current.parentElement?.getBoundingClientRect();
if (!containerRect) return;
// Calculate new width based on mouse position relative to container
const newWidth = e.clientX - containerRect.left;
// Clamp between 200px and 500px
const clampedWidth = Math.min(500, Math.max(200, newWidth));
setSidebarWidth(clampedWidth);
};
const handleMouseUp = () => {
setIsResizing(false);
document.body.style.userSelect = '';
document.body.style.cursor = '';
// Save to localStorage
localStorage.setItem('library-sidebar-width', String(sidebarWidth));
};
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
return () => {
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleMouseUp);
document.body.style.userSelect = '';
document.body.style.cursor = '';
};
}, [isResizing, sidebarWidth]);
// Filter and sort state
const [searchQuery, setSearchQuery] = useState('');
const [filterType, setFilterType] = useState('all');
const [sortField, setSortField] = useState('date');
const [sortDirection, setSortDirection] = useState('desc');
// Update selectedFolderId when URL parameter changes (e.g., navigating from Project or Archive page)
useEffect(() => {
const folderParam = searchParams.get('folder');
if (folderParam) {
const newFolderId = parseInt(folderParam, 10);
setSelectedFolderId(newFolderId);
}
}, [searchParams]);
// Queries
const { data: settings } = useQuery({
queryKey: ['settings'],
queryFn: () => api.getSettings() as Promise,
});
const { data: folders, isLoading: foldersLoading } = useQuery({
queryKey: ['library-folders'],
queryFn: () => api.getLibraryFolders(),
});
const { data: files, isLoading: filesLoading } = useQuery({
queryKey: ['library-files', selectedFolderId],
queryFn: () => api.getLibraryFiles(selectedFolderId, selectedFolderId === null),
});
const { data: stats } = useQuery({
queryKey: ['library-stats'],
queryFn: () => api.getLibraryStats(),
});
// Get unique file types for filter dropdown
const fileTypes = useMemo(() => {
if (!files) return [];
const types = new Set(files.map((f) => f.file_type));
return Array.from(types).sort();
}, [files]);
// Filter and sort files
const filteredAndSortedFiles = useMemo(() => {
if (!files) return [];
let result = [...files];
// Apply search filter
if (searchQuery.trim()) {
const query = searchQuery.toLowerCase();
result = result.filter(
(f) =>
f.filename.toLowerCase().includes(query) ||
(f.print_name && f.print_name.toLowerCase().includes(query))
);
}
// Apply type filter
if (filterType !== 'all') {
result = result.filter((f) => f.file_type === filterType);
}
// Apply sorting
result.sort((a, b) => {
let comparison = 0;
switch (sortField) {
case 'name':
comparison = (a.print_name || a.filename).localeCompare(b.print_name || b.filename);
break;
case 'date':
comparison = new Date(a.created_at).getTime() - new Date(b.created_at).getTime();
break;
case 'size':
comparison = a.file_size - b.file_size;
break;
case 'type':
comparison = a.file_type.localeCompare(b.file_type);
break;
case 'prints':
comparison = a.print_count - b.print_count;
break;
}
return sortDirection === 'asc' ? comparison : -comparison;
});
return result;
}, [files, searchQuery, filterType, sortField, sortDirection]);
// Check if disk space is low
const isDiskSpaceLow = useMemo(() => {
if (!stats || !settings) return false;
const thresholdBytes = (settings.library_disk_warning_gb || 5) * 1024 * 1024 * 1024;
return stats.disk_free_bytes < thresholdBytes;
}, [stats, settings]);
// Mutations
const createFolderMutation = useMutation({
mutationFn: (data: LibraryFolderCreate) => api.createLibraryFolder(data),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['library-folders'] });
setShowNewFolderModal(false);
showToast('Folder created', 'success');
},
onError: (error: Error) => showToast(error.message, 'error'),
});
const deleteFolderMutation = useMutation({
mutationFn: (id: number) => api.deleteLibraryFolder(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['library-folders'] });
queryClient.invalidateQueries({ queryKey: ['library-files'] });
queryClient.invalidateQueries({ queryKey: ['library-stats'] });
if (selectedFolderId === deleteConfirm?.id) {
setSelectedFolderId(null);
}
setDeleteConfirm(null);
showToast('Folder deleted', 'success');
},
onError: (error: Error) => {
setDeleteConfirm(null);
showToast(error.message, 'error');
},
});
const deleteFileMutation = useMutation({
mutationFn: (id: number) => api.deleteLibraryFile(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['library-files'] });
queryClient.invalidateQueries({ queryKey: ['library-folders'] });
queryClient.invalidateQueries({ queryKey: ['library-stats'] });
setSelectedFiles((prev) => prev.filter((id) => id !== deleteConfirm?.id));
setDeleteConfirm(null);
showToast('File deleted', 'success');
},
onError: (error: Error) => {
setDeleteConfirm(null);
showToast(error.message, 'error');
},
});
const bulkDeleteMutation = useMutation({
mutationFn: (fileIds: number[]) => api.bulkDeleteLibrary(fileIds, []),
onSuccess: (_, fileIds) => {
queryClient.invalidateQueries({ queryKey: ['library-files'] });
queryClient.invalidateQueries({ queryKey: ['library-folders'] });
queryClient.invalidateQueries({ queryKey: ['library-stats'] });
showToast(`Deleted ${fileIds.length} files`, 'success');
setSelectedFiles([]);
setDeleteConfirm(null);
},
onError: (error: Error) => {
setDeleteConfirm(null);
showToast(error.message, 'error');
},
});
const moveFilesMutation = useMutation({
mutationFn: ({ fileIds, folderId }: { fileIds: number[]; folderId: number | null }) =>
api.moveLibraryFiles(fileIds, folderId),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['library-files'] });
queryClient.invalidateQueries({ queryKey: ['library-folders'] });
setSelectedFiles([]);
setShowMoveModal(false);
showToast('Files moved', 'success');
},
onError: (error: Error) => showToast(error.message, 'error'),
});
const updateFolderMutation = useMutation({
mutationFn: ({ id, data }: { id: number; data: LibraryFolderUpdate }) =>
api.updateLibraryFolder(id, data),
onSuccess: (_, variables) => {
queryClient.invalidateQueries({ queryKey: ['library-folders'] });
// Invalidate project/archive folder queries so other pages see the update
queryClient.invalidateQueries({ queryKey: ['project-folders'] });
queryClient.invalidateQueries({ queryKey: ['archive-folders'] });
setLinkFolder(null);
const isUnlink = variables.data.project_id === 0 && variables.data.archive_id === 0;
showToast(isUnlink ? 'Folder unlinked' : 'Folder linked', 'success');
},
onError: (error: Error) => showToast(error.message, 'error'),
});
const addToQueueMutation = useMutation({
mutationFn: (fileIds: number[]) => api.addLibraryFilesToQueue(fileIds),
onSuccess: (result) => {
queryClient.invalidateQueries({ queryKey: ['library-files'] });
queryClient.invalidateQueries({ queryKey: ['queue'] });
queryClient.invalidateQueries({ queryKey: ['archives'] }); // Archives are created when adding to queue
setSelectedFiles([]);
if (result.added.length > 0 && result.errors.length === 0) {
showToast(
`Added ${result.added.length} file${result.added.length > 1 ? 's' : ''} to queue`,
'success'
);
} else if (result.added.length > 0 && result.errors.length > 0) {
showToast(
`Added ${result.added.length} file${result.added.length > 1 ? 's' : ''}, ${result.errors.length} failed`,
'success'
);
} else {
showToast(`Failed to add files: ${result.errors[0]?.error || 'Unknown error'}`, 'error');
}
},
onError: (error: Error) => showToast(error.message, 'error'),
});
const renameFileMutation = useMutation({
mutationFn: ({ id, filename }: { id: number; filename: string }) =>
api.updateLibraryFile(id, { filename }),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['library-files'] });
setRenameItem(null);
showToast('File renamed', 'success');
},
onError: (error: Error) => {
setRenameItem(null);
showToast(error.message, 'error');
},
});
const renameFolderMutation = useMutation({
mutationFn: ({ id, name }: { id: number; name: string }) =>
api.updateLibraryFolder(id, { name }),
onSuccess: () => {
// Invalidate both folders and files - files may display folder info
queryClient.invalidateQueries({ queryKey: ['library-folders'] });
queryClient.invalidateQueries({ queryKey: ['library-files'] });
setRenameItem(null);
showToast('Folder renamed', 'success');
},
onError: (error: Error) => {
setRenameItem(null);
showToast(error.message, 'error');
},
});
// Helper to check if a file is sliced (printable)
const isSlicedFile = useCallback((filename: string) => {
const lower = filename.toLowerCase();
return lower.endsWith('.gcode') || lower.includes('.gcode.');
}, []);
// Get sliced files from selection
const selectedSlicedFiles = useMemo(() => {
if (!files) return [];
return files.filter(f => selectedFiles.includes(f.id) && isSlicedFile(f.filename));
}, [files, selectedFiles, isSlicedFile]);
// Handlers
const handleFileSelect = useCallback((id: number) => {
// Always toggle selection (multi-select by default)
setSelectedFiles((prev) => {
return prev.includes(id) ? prev.filter((x) => x !== id) : [...prev, id];
});
}, []);
const handleSelectAll = useCallback(() => {
if (filteredAndSortedFiles.length > 0) {
setSelectedFiles(filteredAndSortedFiles.map((f) => f.id));
}
}, [filteredAndSortedFiles]);
const handleDeselectAll = useCallback(() => {
setSelectedFiles([]);
}, []);
const handleUploadComplete = () => {
queryClient.invalidateQueries({ queryKey: ['library-files'] });
queryClient.invalidateQueries({ queryKey: ['library-folders'] });
queryClient.invalidateQueries({ queryKey: ['library-stats'] });
};
const handleDownload = (id: number) => {
window.open(api.getLibraryFileDownloadUrl(id), '_blank');
};
const handleDeleteConfirm = () => {
if (!deleteConfirm) return;
if (deleteConfirm.type === 'file') {
deleteFileMutation.mutate(deleteConfirm.id);
} else if (deleteConfirm.type === 'folder') {
deleteFolderMutation.mutate(deleteConfirm.id);
} else if (deleteConfirm.type === 'bulk') {
bulkDeleteMutation.mutate(selectedFiles);
}
};
const isDeleting = deleteFolderMutation.isPending || deleteFileMutation.isPending || bulkDeleteMutation.isPending;
const handleViewModeChange = (mode: 'grid' | 'list') => {
setViewMode(mode);
localStorage.setItem('library-view-mode', mode);
};
const isLoading = foldersLoading || filesLoading;
return (
{/* Header */}
File Manager
Organize and manage your print files
{/* View mode toggle */}
handleViewModeChange('grid')}
className={`p-1.5 rounded transition-colors ${
viewMode === 'grid' ? 'bg-bambu-card text-white' : 'text-bambu-gray hover:text-white'
}`}
title="Grid view"
>
handleViewModeChange('list')}
className={`p-1.5 rounded transition-colors ${
viewMode === 'list' ? 'bg-bambu-card text-white' : 'text-bambu-gray hover:text-white'
}`}
title="List view"
>
setShowNewFolderModal(true)}>
New Folder
setShowUploadModal(true)}>
Upload
{/* Disk space warning */}
{isDiskSpaceLow && stats && settings && (
Low disk space warning
Only {formatFileSize(stats.disk_free_bytes)} free of {formatFileSize(stats.disk_total_bytes)} total.
Threshold is set to {settings.library_disk_warning_gb} GB in settings.
)}
{/* Stats bar */}
{stats && (
Files:
{stats.total_files}
Folders:
{stats.total_folders}
Size:
{formatFileSize(stats.total_size_bytes)}
Free:
{formatFileSize(stats.disk_free_bytes)}
)}
{/* Main content */}
{/* Folder sidebar - resizable */}
{/* Resize handle - drag to resize, double-click to reset */}
{
e.preventDefault();
setIsResizing(true);
}}
onDoubleClick={() => {
setSidebarWidth(256); // Reset to default w-64
localStorage.setItem('library-sidebar-width', '256');
}}
title="Drag to resize, double-click to reset"
>
{/* Grip dots */}
Folders
{
const newValue = !wrapFolderNames;
setWrapFolderNames(newValue);
localStorage.setItem('library-wrap-folders', String(newValue));
}}
className={`text-xs px-1.5 py-0.5 rounded transition-colors ${
wrapFolderNames
? 'bg-bambu-green/20 text-bambu-green'
: 'text-bambu-gray hover:text-white hover:bg-bambu-dark'
}`}
title={wrapFolderNames ? 'Disable text wrapping' : 'Enable text wrapping'}
>
Wrap
{/* All Files (root) */}
setSelectedFolderId(null)}
>
All Files
{/* Folder tree */}
{folders?.map((folder) => (
setDeleteConfirm({ type: 'folder', id })}
onLink={setLinkFolder}
onRename={(f) => setRenameItem({ type: 'folder', id: f.id, name: f.name })}
wrapNames={wrapFolderNames}
/>
))}
{/* Files area */}
{/* Search, Filter, Sort toolbar */}
{files && files.length > 0 && (
{/* Search */}
setSearchQuery(e.target.value)}
className="w-full pl-9 pr-3 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green"
/>
{/* Type filter */}
setFilterType(e.target.value)}
className="bg-bambu-dark border border-bambu-dark-tertiary rounded px-2 py-1.5 text-sm text-white focus:outline-none focus:border-bambu-green"
>
All types
{fileTypes.map((type) => (
{type.toUpperCase()}
))}
{/* Sort */}
setSortField(e.target.value as SortField)}
className="bg-bambu-dark border border-bambu-dark-tertiary rounded px-2 py-1.5 text-sm text-white focus:outline-none focus:border-bambu-green"
>
Date
Name
Size
Type
Prints
setSortDirection((d) => (d === 'asc' ? 'desc' : 'asc'))}
className="p-1.5 rounded bg-bambu-dark border border-bambu-dark-tertiary hover:border-bambu-green transition-colors"
title={sortDirection === 'asc' ? 'Ascending' : 'Descending'}
>
{sortDirection === 'asc' ? (
) : (
)}
{/* Results count */}
{(searchQuery || filterType !== 'all') && (
{filteredAndSortedFiles.length} of {files.length} files
)}
)}
{/* Selection toolbar */}
{filteredAndSortedFiles.length > 0 && (
{/* Select all / Deselect all */}
{selectedFiles.length === filteredAndSortedFiles.length && selectedFiles.length > 0 ? (
Deselect All
) : (
Select All
)}
{selectedFiles.length > 0 && (
<>
{selectedFiles.length} selected
{selectedSlicedFiles.length === 1 && (
setPrintMultiFile(selectedSlicedFiles[0])}
>
Print
)}
{selectedSlicedFiles.length > 0 && (
addToQueueMutation.mutate(selectedSlicedFiles.map(f => f.id))}
disabled={addToQueueMutation.isPending}
>
{addToQueueMutation.isPending ? 'Adding...' : `Add to Queue${selectedSlicedFiles.length < selectedFiles.length ? ` (${selectedSlicedFiles.length})` : ''}`}
)}
setShowMoveModal(true)}
>
Move
{
if (selectedFiles.length === 1) {
setDeleteConfirm({ type: 'file', id: selectedFiles[0] });
} else {
setDeleteConfirm({ type: 'bulk', id: 0, count: selectedFiles.length });
}
}}
>
Delete
Clear
>
)}
)}
{/* File grid/list */}
{isLoading ? (
) : files?.length === 0 ? (
{selectedFolderId !== null ? 'Folder is empty' : 'No files yet'}
{selectedFolderId !== null
? 'Upload files or move files into this folder to get started.'
: 'Upload files to start organizing your print-related files.'}
setShowUploadModal(true)}>
Upload Files
) : filteredAndSortedFiles.length === 0 ? (
No matching files
No files match your current search or filter criteria.
{ setSearchQuery(''); setFilterType('all'); }}>
Clear filters
) : viewMode === 'grid' ? (
{filteredAndSortedFiles.map((file) => (
setDeleteConfirm({ type: 'file', id })}
onDownload={handleDownload}
onAddToQueue={(id) => addToQueueMutation.mutate([id])}
onPrint={setPrintFile}
onRename={(f) => setRenameItem({ type: 'file', id: f.id, name: f.filename })}
/>
))}
) : (
{/* List header */}
{/* List rows */}
{filteredAndSortedFiles.map((file) => (
handleFileSelect(file.id)}
>
{/* Checkbox */}
{selectedFiles.includes(file.id) &&
}
{/* Name with thumbnail */}
{file.thumbnail_path ? (
) : (
)}
{/* Hover preview */}
{file.thumbnail_path && (
)}
{file.print_name || file.filename}
{/* Type */}
{file.file_type.toUpperCase()}
{/* Size */}
{formatFileSize(file.file_size)}
{/* Prints */}
{file.print_count > 0 ? `${file.print_count}x` : '-'}
{/* Actions */}
e.stopPropagation()}>
{isSlicedFilename(file.filename) && (
<>
setPrintFile(file)}
className="p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-bambu-green transition-colors"
title="Print"
>
addToQueueMutation.mutate([file.id])}
className="p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors"
title="Add to Queue"
disabled={addToQueueMutation.isPending}
>
>
)}
handleDownload(file.id)}
className="p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors"
title="Download"
>
setRenameItem({ type: 'file', id: file.id, name: file.filename })}
className="p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors"
title="Rename"
>
setDeleteConfirm({ type: 'file', id: file.id })}
className="p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-red-400 transition-colors"
title="Delete"
>
))}
)}
{/* Modals */}
{showNewFolderModal && (
setShowNewFolderModal(false)}
onSave={(data) => createFolderMutation.mutate(data)}
isLoading={createFolderMutation.isPending}
/>
)}
{showMoveModal && folders && (
setShowMoveModal(false)}
onMove={(folderId) => moveFilesMutation.mutate({ fileIds: selectedFiles, folderId })}
isLoading={moveFilesMutation.isPending}
/>
)}
{showUploadModal && (
setShowUploadModal(false)}
onUploadComplete={handleUploadComplete}
/>
)}
{linkFolder && (
setLinkFolder(null)}
onLink={(data) => updateFolderMutation.mutate({ id: linkFolder.id, data })}
isLoading={updateFolderMutation.isPending}
/>
)}
{deleteConfirm && (
setDeleteConfirm(null)}
/>
)}
{printFile && (
setPrintFile(null)}
onSuccess={() => {
setPrintFile(null);
queryClient.invalidateQueries({ queryKey: ['library-files'] });
queryClient.invalidateQueries({ queryKey: ['archives'] });
}}
/>
)}
{printMultiFile && (
setPrintMultiFile(null)}
onSuccess={() => {
setPrintMultiFile(null);
setSelectedFiles([]);
queryClient.invalidateQueries({ queryKey: ['library-files'] });
queryClient.invalidateQueries({ queryKey: ['archives'] });
}}
/>
)}
{renameItem && (
setRenameItem(null)}
onSave={(newName) => {
if (renameItem.type === 'file') {
renameFileMutation.mutate({ id: renameItem.id, filename: newName });
} else {
renameFolderMutation.mutate({ id: renameItem.id, name: newName });
}
}}
isLoading={renameFileMutation.isPending || renameFolderMutation.isPending}
/>
)}
);
}