2025-07-16 17:53:50 +01:00
|
|
|
import { useState, useCallback } from 'react';
|
2025-08-19 21:29:37 +01:00
|
|
|
import { useIndexedDB } from '../contexts/IndexedDBContext';
|
2025-08-14 18:07:18 +01:00
|
|
|
import { FileWithUrl, FileMetadata } from '../types/file';
|
2025-08-08 15:15:09 +01:00
|
|
|
import { generateThumbnailForFile } from '../utils/thumbnailUtils';
|
2025-07-16 17:53:50 +01:00
|
|
|
|
|
|
|
export const useFileManager = () => {
|
|
|
|
const [loading, setLoading] = useState(false);
|
2025-08-19 21:29:37 +01:00
|
|
|
const indexedDB = useIndexedDB();
|
2025-07-16 17:53:50 +01:00
|
|
|
|
2025-08-14 18:07:18 +01:00
|
|
|
const convertToFile = useCallback(async (fileMetadata: FileMetadata): Promise<File> => {
|
2025-08-19 21:29:37 +01:00
|
|
|
if (!indexedDB) {
|
|
|
|
throw new Error('IndexedDB context not available');
|
2025-07-16 17:53:50 +01:00
|
|
|
}
|
2025-08-19 21:29:37 +01:00
|
|
|
|
|
|
|
// Try ID first (preferred)
|
|
|
|
if (fileMetadata.id) {
|
|
|
|
const file = await indexedDB.loadFile(fileMetadata.id);
|
|
|
|
if (file) {
|
|
|
|
return file;
|
|
|
|
}
|
2025-07-16 17:53:50 +01:00
|
|
|
}
|
2025-08-19 21:29:37 +01:00
|
|
|
throw new Error(`File not found in storage: ${fileMetadata.name} (ID: ${fileMetadata.id})`);
|
|
|
|
}, [indexedDB]);
|
2025-07-16 17:53:50 +01:00
|
|
|
|
2025-08-14 18:07:18 +01:00
|
|
|
const loadRecentFiles = useCallback(async (): Promise<FileMetadata[]> => {
|
2025-07-16 17:53:50 +01:00
|
|
|
setLoading(true);
|
|
|
|
try {
|
2025-08-19 21:29:37 +01:00
|
|
|
if (!indexedDB) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2025-08-14 18:07:18 +01:00
|
|
|
// Get metadata only (no file data) for performance
|
2025-08-19 21:29:37 +01:00
|
|
|
const storedFileMetadata = await indexedDB.loadAllMetadata();
|
2025-08-14 18:07:18 +01:00
|
|
|
const sortedFiles = storedFileMetadata.sort((a, b) => (b.lastModified || 0) - (a.lastModified || 0));
|
|
|
|
|
2025-08-19 21:29:37 +01:00
|
|
|
// Already in correct FileMetadata format
|
|
|
|
return sortedFiles;
|
2025-07-16 17:53:50 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to load recent files:', error);
|
|
|
|
return [];
|
|
|
|
} finally {
|
|
|
|
setLoading(false);
|
|
|
|
}
|
2025-08-19 21:29:37 +01:00
|
|
|
}, [indexedDB]);
|
2025-07-16 17:53:50 +01:00
|
|
|
|
2025-08-14 18:07:18 +01:00
|
|
|
const handleRemoveFile = useCallback(async (index: number, files: FileMetadata[], setFiles: (files: FileMetadata[]) => void) => {
|
2025-07-16 17:53:50 +01:00
|
|
|
const file = files[index];
|
2025-08-14 18:07:18 +01:00
|
|
|
if (!file.id) {
|
|
|
|
throw new Error('File ID is required for removal');
|
|
|
|
}
|
2025-08-19 21:29:37 +01:00
|
|
|
if (!indexedDB) {
|
|
|
|
throw new Error('IndexedDB context not available');
|
|
|
|
}
|
2025-07-16 17:53:50 +01:00
|
|
|
try {
|
2025-08-19 21:29:37 +01:00
|
|
|
await indexedDB.deleteFile(file.id);
|
2025-07-16 17:53:50 +01:00
|
|
|
setFiles(files.filter((_, i) => i !== index));
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to remove file:', error);
|
|
|
|
throw error;
|
|
|
|
}
|
2025-08-19 21:29:37 +01:00
|
|
|
}, [indexedDB]);
|
2025-07-16 17:53:50 +01:00
|
|
|
|
2025-08-14 18:07:18 +01:00
|
|
|
const storeFile = useCallback(async (file: File, fileId: string) => {
|
2025-08-19 21:29:37 +01:00
|
|
|
if (!indexedDB) {
|
|
|
|
throw new Error('IndexedDB context not available');
|
|
|
|
}
|
2025-07-16 17:53:50 +01:00
|
|
|
try {
|
2025-08-19 21:29:37 +01:00
|
|
|
// Store file with provided UUID from FileContext (thumbnail generated internally)
|
|
|
|
const metadata = await indexedDB.saveFile(file, fileId);
|
2025-08-11 16:40:38 +01:00
|
|
|
|
2025-08-19 21:29:37 +01:00
|
|
|
// Convert file to ArrayBuffer for StoredFile interface compatibility
|
|
|
|
const arrayBuffer = await file.arrayBuffer();
|
|
|
|
|
|
|
|
// Return StoredFile format for compatibility with old API
|
|
|
|
return {
|
|
|
|
id: fileId,
|
|
|
|
name: file.name,
|
|
|
|
type: file.type,
|
|
|
|
size: file.size,
|
|
|
|
lastModified: file.lastModified,
|
|
|
|
data: arrayBuffer,
|
|
|
|
thumbnail: metadata.thumbnail
|
|
|
|
};
|
2025-07-16 17:53:50 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to store file:', error);
|
|
|
|
throw error;
|
|
|
|
}
|
2025-08-19 21:29:37 +01:00
|
|
|
}, [indexedDB]);
|
2025-07-16 17:53:50 +01:00
|
|
|
|
|
|
|
const createFileSelectionHandlers = useCallback((
|
|
|
|
selectedFiles: string[],
|
|
|
|
setSelectedFiles: (files: string[]) => void
|
|
|
|
) => {
|
|
|
|
const toggleSelection = (fileId: string) => {
|
|
|
|
setSelectedFiles(
|
|
|
|
selectedFiles.includes(fileId)
|
|
|
|
? selectedFiles.filter(id => id !== fileId)
|
|
|
|
: [...selectedFiles, fileId]
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const clearSelection = () => {
|
|
|
|
setSelectedFiles([]);
|
|
|
|
};
|
|
|
|
|
2025-08-14 21:47:02 +01:00
|
|
|
const selectMultipleFiles = async (files: FileMetadata[], onFilesSelect: (files: File[]) => void, onStoredFilesSelect?: (filesWithMetadata: Array<{ file: File; originalId: string; metadata: FileMetadata }>) => void) => {
|
2025-07-16 17:53:50 +01:00
|
|
|
if (selectedFiles.length === 0) return;
|
|
|
|
|
|
|
|
try {
|
2025-08-14 18:07:18 +01:00
|
|
|
// Filter by UUID and convert to File objects
|
|
|
|
const selectedFileObjects = files.filter(f => selectedFiles.includes(f.id));
|
2025-08-14 21:47:02 +01:00
|
|
|
|
|
|
|
if (onStoredFilesSelect) {
|
|
|
|
// NEW: Use stored files flow that preserves IDs
|
|
|
|
const filesWithMetadata = await Promise.all(
|
|
|
|
selectedFileObjects.map(async (metadata) => ({
|
|
|
|
file: await convertToFile(metadata),
|
|
|
|
originalId: metadata.id,
|
|
|
|
metadata
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
onStoredFilesSelect(filesWithMetadata);
|
|
|
|
} else {
|
|
|
|
// LEGACY: Old flow that generates new UUIDs (for backward compatibility)
|
|
|
|
const filePromises = selectedFileObjects.map(convertToFile);
|
|
|
|
const convertedFiles = await Promise.all(filePromises);
|
|
|
|
onFilesSelect(convertedFiles); // FileContext will assign new UUIDs
|
|
|
|
}
|
|
|
|
|
2025-07-16 17:53:50 +01:00
|
|
|
clearSelection();
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to load selected files:', error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
toggleSelection,
|
|
|
|
clearSelection,
|
|
|
|
selectMultipleFiles
|
|
|
|
};
|
|
|
|
}, [convertToFile]);
|
|
|
|
|
2025-08-08 15:15:09 +01:00
|
|
|
const touchFile = useCallback(async (id: string) => {
|
2025-08-19 21:29:37 +01:00
|
|
|
if (!indexedDB) {
|
|
|
|
console.warn('IndexedDB context not available for touch operation');
|
|
|
|
return;
|
|
|
|
}
|
2025-08-08 15:15:09 +01:00
|
|
|
try {
|
2025-08-19 21:29:37 +01:00
|
|
|
// Update access time - this will be handled by the cache in IndexedDBContext
|
|
|
|
// when the file is loaded, so we can just load it briefly to "touch" it
|
|
|
|
await indexedDB.loadFile(id);
|
2025-08-08 15:15:09 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to touch file:', error);
|
|
|
|
}
|
2025-08-19 21:29:37 +01:00
|
|
|
}, [indexedDB]);
|
2025-08-08 15:15:09 +01:00
|
|
|
|
2025-07-16 17:53:50 +01:00
|
|
|
return {
|
|
|
|
loading,
|
|
|
|
convertToFile,
|
|
|
|
loadRecentFiles,
|
|
|
|
handleRemoveFile,
|
|
|
|
storeFile,
|
2025-08-08 15:15:09 +01:00
|
|
|
touchFile,
|
2025-07-16 17:53:50 +01:00
|
|
|
createFileSelectionHandlers
|
|
|
|
};
|
2025-08-11 16:40:38 +01:00
|
|
|
};
|