mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-08-26 14:19:24 +00:00
File editor tweaks
This commit is contained in:
parent
a0aec7e0d1
commit
ba1a7b269e
@ -43,7 +43,7 @@ const FileManager: React.FC<FileManagerProps> = ({ selectedTool }) => {
|
|||||||
|
|
||||||
const handleFilesSelected = useCallback(async (files: FileMetadata[]) => {
|
const handleFilesSelected = useCallback(async (files: FileMetadata[]) => {
|
||||||
try {
|
try {
|
||||||
// NEW: Use stored files flow that preserves original IDs
|
// Use stored files flow that preserves original IDs
|
||||||
const filesWithMetadata = await Promise.all(
|
const filesWithMetadata = await Promise.all(
|
||||||
files.map(async (metadata) => ({
|
files.map(async (metadata) => ({
|
||||||
file: await convertToFile(metadata),
|
file: await convertToFile(metadata),
|
||||||
|
@ -7,6 +7,7 @@ import { Dropzone } from '@mantine/dropzone';
|
|||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import UploadFileIcon from '@mui/icons-material/UploadFile';
|
import UploadFileIcon from '@mui/icons-material/UploadFile';
|
||||||
import { useToolFileSelection, useFileState, useFileManagement, useFileActions } from '../../contexts/FileContext';
|
import { useToolFileSelection, useFileState, useFileManagement, useFileActions } from '../../contexts/FileContext';
|
||||||
|
import { useNavigationActions } from '../../contexts/NavigationContext';
|
||||||
import { FileOperation } from '../../types/fileContext';
|
import { FileOperation } from '../../types/fileContext';
|
||||||
import { fileStorage } from '../../services/fileStorage';
|
import { fileStorage } from '../../services/fileStorage';
|
||||||
import { generateThumbnailForFile } from '../../utils/thumbnailUtils';
|
import { generateThumbnailForFile } from '../../utils/thumbnailUtils';
|
||||||
@ -55,28 +56,20 @@ const FileEditor = ({
|
|||||||
|
|
||||||
// Get the real context actions
|
// Get the real context actions
|
||||||
const { actions } = useFileActions();
|
const { actions } = useFileActions();
|
||||||
|
const { actions: navActions } = useNavigationActions();
|
||||||
|
|
||||||
// Create a stable ref to access current selected files and actions without dependency
|
// Proper context-based file selection updater
|
||||||
const selectedFileIdsRef = useRef<string[]>([]);
|
|
||||||
const actionsRef = useRef(actions);
|
|
||||||
selectedFileIdsRef.current = selectedFileIds;
|
|
||||||
actionsRef.current = actions;
|
|
||||||
|
|
||||||
// Wrapper for context file selection updates (stable)
|
|
||||||
const setContextSelectedFiles = useCallback((fileIds: string[] | ((prev: string[]) => string[])) => {
|
const setContextSelectedFiles = useCallback((fileIds: string[] | ((prev: string[]) => string[])) => {
|
||||||
if (typeof fileIds === 'function') {
|
if (typeof fileIds === 'function') {
|
||||||
// Handle callback pattern - get current state from ref
|
// Handle callback pattern with current state
|
||||||
const result = fileIds(selectedFileIdsRef.current);
|
const result = fileIds(selectedFileIds);
|
||||||
actionsRef.current.setSelectedFiles(result);
|
actions.setSelectedFiles(result);
|
||||||
} else {
|
} else {
|
||||||
// Handle direct array pattern
|
// Handle direct array pattern
|
||||||
actionsRef.current.setSelectedFiles(fileIds);
|
actions.setSelectedFiles(fileIds);
|
||||||
}
|
}
|
||||||
}, []); // No dependencies at all - completely stable
|
}, [selectedFileIds, actions.setSelectedFiles]);
|
||||||
|
|
||||||
const setCurrentView = (mode: any) => {
|
|
||||||
// Will be handled by parent component actions
|
|
||||||
};
|
|
||||||
|
|
||||||
// Get tool file selection context (replaces FileSelectionContext)
|
// Get tool file selection context (replaces FileSelectionContext)
|
||||||
const {
|
const {
|
||||||
@ -286,8 +279,9 @@ const FileEditor = ({
|
|||||||
const closeAllFiles = useCallback(() => {
|
const closeAllFiles = useCallback(() => {
|
||||||
if (activeFileRecords.length === 0) return;
|
if (activeFileRecords.length === 0) return;
|
||||||
|
|
||||||
|
|
||||||
// Remove all files from context but keep in storage
|
// Remove all files from context but keep in storage
|
||||||
|
const allFileIds = activeFileRecords.map(record => record.id);
|
||||||
|
removeFiles(allFileIds, false); // false = keep in storage
|
||||||
|
|
||||||
// Clear selections
|
// Clear selections
|
||||||
setContextSelectedFiles([]);
|
setContextSelectedFiles([]);
|
||||||
@ -440,9 +434,9 @@ const FileEditor = ({
|
|||||||
if (record) {
|
if (record) {
|
||||||
// Set the file as selected in context and switch to viewer for preview
|
// Set the file as selected in context and switch to viewer for preview
|
||||||
setContextSelectedFiles([fileId]);
|
setContextSelectedFiles([fileId]);
|
||||||
setCurrentView('viewer');
|
navActions.setMode('viewer');
|
||||||
}
|
}
|
||||||
}, [activeFileRecords, setContextSelectedFiles, setCurrentView]);
|
}, [activeFileRecords, setContextSelectedFiles, navActions.setMode]);
|
||||||
|
|
||||||
const handleMergeFromHere = useCallback((fileId: string) => {
|
const handleMergeFromHere = useCallback((fileId: string) => {
|
||||||
const startIndex = activeFileRecords.findIndex(r => r.id === fileId);
|
const startIndex = activeFileRecords.findIndex(r => r.id === fileId);
|
||||||
@ -495,10 +489,14 @@ const FileEditor = ({
|
|||||||
|
|
||||||
<Box p="md" pt="xl">
|
<Box p="md" pt="xl">
|
||||||
<Group mb="md">
|
<Group mb="md">
|
||||||
{showBulkActions && !toolMode && (
|
{toolMode && (
|
||||||
<>
|
<>
|
||||||
<Button onClick={selectAll} variant="light">Select All</Button>
|
<Button onClick={selectAll} variant="light">Select All</Button>
|
||||||
<Button onClick={deselectAll} variant="light">Deselect All</Button>
|
<Button onClick={deselectAll} variant="light">Deselect All</Button>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
{showBulkActions && !toolMode && (
|
||||||
|
<>
|
||||||
<Button onClick={closeAllFiles} variant="light" color="orange">
|
<Button onClick={closeAllFiles} variant="light" color="orange">
|
||||||
Close All
|
Close All
|
||||||
</Button>
|
</Button>
|
||||||
|
@ -16,7 +16,7 @@ export const useFileHandler = () => {
|
|||||||
await actions.addFiles(files);
|
await actions.addFiles(files);
|
||||||
}, [actions.addFiles]);
|
}, [actions.addFiles]);
|
||||||
|
|
||||||
// NEW: Add stored files preserving their original IDs to prevent session duplicates
|
// Add stored files preserving their original IDs to prevent session duplicates
|
||||||
const addStoredFiles = useCallback(async (filesWithMetadata: Array<{ file: File; originalId: string; metadata: FileMetadata }>) => {
|
const addStoredFiles = useCallback(async (filesWithMetadata: Array<{ file: File; originalId: string; metadata: FileMetadata }>) => {
|
||||||
// Filter out files that already exist with the same ID (exact match)
|
// Filter out files that already exist with the same ID (exact match)
|
||||||
const newFiles = filesWithMetadata.filter(({ originalId }) => {
|
const newFiles = filesWithMetadata.filter(({ originalId }) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user