2025-08-21 17:30:26 +01:00
|
|
|
import React, { createContext, useContext, useState, useRef, useCallback, useEffect, useMemo } from 'react';
|
|
|
|
import { FileMetadata } from '../types/file';
|
2025-08-20 16:51:55 +01:00
|
|
|
import { StoredFile, fileStorage } from '../services/fileStorage';
|
|
|
|
import { downloadFiles } from '../utils/downloadUtils';
|
2025-08-28 10:56:07 +01:00
|
|
|
import { FileId } from '../types/file';
|
2025-09-02 17:24:26 +01:00
|
|
|
import { getLatestVersions, groupFilesByOriginal, getVersionHistory } from '../utils/fileHistoryUtils';
|
2025-08-08 15:15:09 +01:00
|
|
|
|
|
|
|
// Type for the context value - now contains everything directly
|
|
|
|
interface FileManagerContextValue {
|
|
|
|
// State
|
|
|
|
activeSource: 'recent' | 'local' | 'drive';
|
2025-08-28 10:56:07 +01:00
|
|
|
selectedFileIds: FileId[];
|
2025-08-08 15:15:09 +01:00
|
|
|
searchTerm: string;
|
2025-08-21 17:30:26 +01:00
|
|
|
selectedFiles: FileMetadata[];
|
|
|
|
filteredFiles: FileMetadata[];
|
2025-08-11 09:16:16 +01:00
|
|
|
fileInputRef: React.RefObject<HTMLInputElement | null>;
|
2025-08-20 16:51:55 +01:00
|
|
|
selectedFilesSet: Set<string>;
|
2025-09-03 17:47:58 +01:00
|
|
|
expandedFileIds: Set<string>;
|
2025-09-02 17:24:26 +01:00
|
|
|
fileGroups: Map<string, FileMetadata[]>;
|
2025-08-11 09:16:16 +01:00
|
|
|
|
2025-08-08 15:15:09 +01:00
|
|
|
// Handlers
|
|
|
|
onSourceChange: (source: 'recent' | 'local' | 'drive') => void;
|
|
|
|
onLocalFileClick: () => void;
|
2025-08-21 17:30:26 +01:00
|
|
|
onFileSelect: (file: FileMetadata, index: number, shiftKey?: boolean) => void;
|
2025-08-08 15:15:09 +01:00
|
|
|
onFileRemove: (index: number) => void;
|
2025-08-21 17:30:26 +01:00
|
|
|
onFileDoubleClick: (file: FileMetadata) => void;
|
2025-08-08 15:15:09 +01:00
|
|
|
onOpenFiles: () => void;
|
|
|
|
onSearchChange: (value: string) => void;
|
|
|
|
onFileInputChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
|
2025-08-20 16:51:55 +01:00
|
|
|
onSelectAll: () => void;
|
|
|
|
onDeleteSelected: () => void;
|
|
|
|
onDownloadSelected: () => void;
|
2025-08-21 17:30:26 +01:00
|
|
|
onDownloadSingle: (file: FileMetadata) => void;
|
2025-09-03 17:47:58 +01:00
|
|
|
onToggleExpansion: (fileId: string) => void;
|
|
|
|
onAddToRecents: (file: FileMetadata) => void;
|
2025-09-02 17:24:26 +01:00
|
|
|
onNewFilesSelect: (files: File[]) => void;
|
2025-08-11 09:16:16 +01:00
|
|
|
|
2025-08-08 15:15:09 +01:00
|
|
|
// External props
|
2025-08-21 17:30:26 +01:00
|
|
|
recentFiles: FileMetadata[];
|
2025-08-08 15:15:09 +01:00
|
|
|
isFileSupported: (fileName: string) => boolean;
|
|
|
|
modalHeight: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the context
|
|
|
|
const FileManagerContext = createContext<FileManagerContextValue | null>(null);
|
|
|
|
|
|
|
|
// Provider component props
|
|
|
|
interface FileManagerProviderProps {
|
|
|
|
children: React.ReactNode;
|
2025-08-21 17:30:26 +01:00
|
|
|
recentFiles: FileMetadata[];
|
|
|
|
onFilesSelected: (files: FileMetadata[]) => void; // For selecting stored files
|
|
|
|
onNewFilesSelect: (files: File[]) => void; // For uploading new local files
|
2025-08-08 15:15:09 +01:00
|
|
|
onClose: () => void;
|
|
|
|
isFileSupported: (fileName: string) => boolean;
|
|
|
|
isOpen: boolean;
|
|
|
|
onFileRemove: (index: number) => void;
|
|
|
|
modalHeight: string;
|
|
|
|
refreshRecentFiles: () => Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const FileManagerProvider: React.FC<FileManagerProviderProps> = ({
|
|
|
|
children,
|
|
|
|
recentFiles,
|
|
|
|
onFilesSelected,
|
2025-08-21 17:30:26 +01:00
|
|
|
onNewFilesSelect,
|
2025-08-08 15:15:09 +01:00
|
|
|
onClose,
|
|
|
|
isFileSupported,
|
|
|
|
isOpen,
|
|
|
|
onFileRemove,
|
|
|
|
modalHeight,
|
|
|
|
refreshRecentFiles,
|
|
|
|
}) => {
|
|
|
|
const [activeSource, setActiveSource] = useState<'recent' | 'local' | 'drive'>('recent');
|
2025-08-28 10:56:07 +01:00
|
|
|
const [selectedFileIds, setSelectedFileIds] = useState<FileId[]>([]);
|
2025-08-08 15:15:09 +01:00
|
|
|
const [searchTerm, setSearchTerm] = useState('');
|
2025-08-20 16:51:55 +01:00
|
|
|
const [lastClickedIndex, setLastClickedIndex] = useState<number | null>(null);
|
2025-09-03 17:47:58 +01:00
|
|
|
const [expandedFileIds, setExpandedFileIds] = useState<Set<string>>(new Set());
|
2025-08-08 15:15:09 +01:00
|
|
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
2025-08-11 09:16:16 +01:00
|
|
|
|
2025-08-08 15:15:09 +01:00
|
|
|
// Track blob URLs for cleanup
|
|
|
|
const createdBlobUrls = useRef<Set<string>>(new Set());
|
|
|
|
|
|
|
|
// Computed values (with null safety)
|
2025-08-20 16:51:55 +01:00
|
|
|
const selectedFilesSet = new Set(selectedFileIds);
|
2025-08-28 10:56:07 +01:00
|
|
|
|
2025-09-02 17:24:26 +01:00
|
|
|
// Group files by original file ID for version management
|
|
|
|
const fileGroups = useMemo(() => {
|
|
|
|
if (!recentFiles || recentFiles.length === 0) return new Map();
|
|
|
|
|
|
|
|
// Convert FileMetadata to FileRecord-like objects for grouping utility
|
|
|
|
const recordsForGrouping = recentFiles.map(file => ({
|
|
|
|
...file,
|
|
|
|
originalFileId: file.originalFileId,
|
|
|
|
versionNumber: file.versionNumber || 0
|
|
|
|
}));
|
|
|
|
|
|
|
|
return groupFilesByOriginal(recordsForGrouping);
|
|
|
|
}, [recentFiles]);
|
|
|
|
|
2025-09-03 17:47:58 +01:00
|
|
|
// Get files to display with expansion logic
|
2025-09-02 17:24:26 +01:00
|
|
|
const displayFiles = useMemo(() => {
|
|
|
|
if (!recentFiles || recentFiles.length === 0) return [];
|
|
|
|
|
2025-09-03 17:47:58 +01:00
|
|
|
const recordsForGrouping = recentFiles.map(file => ({
|
|
|
|
...file,
|
|
|
|
originalFileId: file.originalFileId,
|
|
|
|
versionNumber: file.versionNumber || 0
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Get branch groups (leaf files with their lineage paths)
|
|
|
|
const branchGroups = groupFilesByOriginal(recordsForGrouping);
|
|
|
|
|
|
|
|
// Show only leaf files (end of branches) in main list
|
|
|
|
const expandedFiles = [];
|
|
|
|
for (const [leafFileId, lineagePath] of branchGroups) {
|
|
|
|
const leafFile = recentFiles.find(f => f.id === leafFileId);
|
|
|
|
if (!leafFile) continue;
|
|
|
|
|
|
|
|
// Add the leaf file (shown in main list)
|
|
|
|
expandedFiles.push(leafFile);
|
2025-09-02 17:24:26 +01:00
|
|
|
|
2025-09-03 17:47:58 +01:00
|
|
|
// If expanded, add the lineage history (except the leaf itself)
|
|
|
|
if (expandedFileIds.has(leafFileId)) {
|
|
|
|
const historyFiles = lineagePath
|
|
|
|
.filter((record: any) => record.id !== leafFileId)
|
|
|
|
.map((record: any) => recentFiles.find(f => f.id === record.id))
|
|
|
|
.filter((f): f is FileMetadata => f !== undefined);
|
|
|
|
expandedFiles.push(...historyFiles);
|
|
|
|
}
|
2025-09-02 17:24:26 +01:00
|
|
|
}
|
2025-09-03 17:47:58 +01:00
|
|
|
|
|
|
|
return expandedFiles;
|
|
|
|
}, [recentFiles, expandedFileIds, fileGroups]);
|
2025-09-02 17:24:26 +01:00
|
|
|
|
2025-08-28 10:56:07 +01:00
|
|
|
const selectedFiles = selectedFileIds.length === 0 ? [] :
|
2025-09-02 17:24:26 +01:00
|
|
|
displayFiles.filter(file => selectedFilesSet.has(file.id));
|
2025-08-28 10:56:07 +01:00
|
|
|
|
2025-09-02 17:24:26 +01:00
|
|
|
const filteredFiles = !searchTerm ? displayFiles :
|
|
|
|
displayFiles.filter(file =>
|
2025-08-20 16:51:55 +01:00
|
|
|
file.name.toLowerCase().includes(searchTerm.toLowerCase())
|
|
|
|
);
|
2025-08-08 15:15:09 +01:00
|
|
|
|
|
|
|
const handleSourceChange = useCallback((source: 'recent' | 'local' | 'drive') => {
|
|
|
|
setActiveSource(source);
|
|
|
|
if (source !== 'recent') {
|
|
|
|
setSelectedFileIds([]);
|
|
|
|
setSearchTerm('');
|
2025-08-20 16:51:55 +01:00
|
|
|
setLastClickedIndex(null);
|
2025-08-08 15:15:09 +01:00
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleLocalFileClick = useCallback(() => {
|
|
|
|
fileInputRef.current?.click();
|
|
|
|
}, []);
|
|
|
|
|
2025-08-21 17:30:26 +01:00
|
|
|
const handleFileSelect = useCallback((file: FileMetadata, currentIndex: number, shiftKey?: boolean) => {
|
|
|
|
const fileId = file.id;
|
2025-08-20 16:51:55 +01:00
|
|
|
if (!fileId) return;
|
2025-08-28 10:56:07 +01:00
|
|
|
|
2025-08-20 16:51:55 +01:00
|
|
|
if (shiftKey && lastClickedIndex !== null) {
|
|
|
|
// Range selection with shift-click
|
|
|
|
const startIndex = Math.min(lastClickedIndex, currentIndex);
|
|
|
|
const endIndex = Math.max(lastClickedIndex, currentIndex);
|
2025-08-28 10:56:07 +01:00
|
|
|
|
2025-08-20 16:51:55 +01:00
|
|
|
setSelectedFileIds(prev => {
|
|
|
|
const selectedSet = new Set(prev);
|
2025-08-28 10:56:07 +01:00
|
|
|
|
2025-08-20 16:51:55 +01:00
|
|
|
// Add all files in the range to selection
|
|
|
|
for (let i = startIndex; i <= endIndex; i++) {
|
2025-08-21 17:30:26 +01:00
|
|
|
const rangeFileId = filteredFiles[i]?.id;
|
2025-08-20 16:51:55 +01:00
|
|
|
if (rangeFileId) {
|
|
|
|
selectedSet.add(rangeFileId);
|
|
|
|
}
|
|
|
|
}
|
2025-08-28 10:56:07 +01:00
|
|
|
|
2025-08-20 16:51:55 +01:00
|
|
|
return Array.from(selectedSet);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Normal click behavior - optimized with Set for O(1) lookup
|
|
|
|
setSelectedFileIds(prev => {
|
|
|
|
const selectedSet = new Set(prev);
|
2025-08-28 10:56:07 +01:00
|
|
|
|
2025-08-20 16:51:55 +01:00
|
|
|
if (selectedSet.has(fileId)) {
|
|
|
|
selectedSet.delete(fileId);
|
2025-08-11 09:16:16 +01:00
|
|
|
} else {
|
2025-08-20 16:51:55 +01:00
|
|
|
selectedSet.add(fileId);
|
2025-08-11 09:16:16 +01:00
|
|
|
}
|
2025-08-28 10:56:07 +01:00
|
|
|
|
2025-08-20 16:51:55 +01:00
|
|
|
return Array.from(selectedSet);
|
|
|
|
});
|
2025-08-28 10:56:07 +01:00
|
|
|
|
2025-08-20 16:51:55 +01:00
|
|
|
// Update last clicked index for future range selections
|
|
|
|
setLastClickedIndex(currentIndex);
|
|
|
|
}
|
|
|
|
}, [filteredFiles, lastClickedIndex]);
|
2025-08-08 15:15:09 +01:00
|
|
|
|
|
|
|
const handleFileRemove = useCallback((index: number) => {
|
|
|
|
const fileToRemove = filteredFiles[index];
|
|
|
|
if (fileToRemove) {
|
|
|
|
setSelectedFileIds(prev => prev.filter(id => id !== fileToRemove.id));
|
|
|
|
}
|
|
|
|
onFileRemove(index);
|
|
|
|
}, [filteredFiles, onFileRemove]);
|
|
|
|
|
2025-08-21 17:30:26 +01:00
|
|
|
const handleFileDoubleClick = useCallback((file: FileMetadata) => {
|
2025-08-08 15:15:09 +01:00
|
|
|
if (isFileSupported(file.name)) {
|
|
|
|
onFilesSelected([file]);
|
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
}, [isFileSupported, onFilesSelected, onClose]);
|
|
|
|
|
|
|
|
const handleOpenFiles = useCallback(() => {
|
|
|
|
if (selectedFiles.length > 0) {
|
|
|
|
onFilesSelected(selectedFiles);
|
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
}, [selectedFiles, onFilesSelected, onClose]);
|
|
|
|
|
|
|
|
const handleSearchChange = useCallback((value: string) => {
|
|
|
|
setSearchTerm(value);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleFileInputChange = useCallback(async (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
const files = Array.from(event.target.files || []);
|
|
|
|
if (files.length > 0) {
|
|
|
|
try {
|
2025-08-21 17:30:26 +01:00
|
|
|
// For local file uploads, pass File objects directly to FileContext
|
|
|
|
onNewFilesSelect(files);
|
2025-08-08 15:15:09 +01:00
|
|
|
await refreshRecentFiles();
|
|
|
|
onClose();
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to process selected files:', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
event.target.value = '';
|
2025-08-21 17:30:26 +01:00
|
|
|
}, [onNewFilesSelect, refreshRecentFiles, onClose]);
|
2025-08-08 15:15:09 +01:00
|
|
|
|
2025-08-20 16:51:55 +01:00
|
|
|
const handleSelectAll = useCallback(() => {
|
|
|
|
const allFilesSelected = filteredFiles.length > 0 && selectedFileIds.length === filteredFiles.length;
|
|
|
|
if (allFilesSelected) {
|
|
|
|
// Deselect all
|
|
|
|
setSelectedFileIds([]);
|
|
|
|
setLastClickedIndex(null);
|
|
|
|
} else {
|
|
|
|
// Select all filtered files
|
2025-08-21 17:30:26 +01:00
|
|
|
setSelectedFileIds(filteredFiles.map(file => file.id).filter(Boolean));
|
2025-08-20 16:51:55 +01:00
|
|
|
setLastClickedIndex(null);
|
|
|
|
}
|
|
|
|
}, [filteredFiles, selectedFileIds]);
|
|
|
|
|
|
|
|
const handleDeleteSelected = useCallback(async () => {
|
|
|
|
if (selectedFileIds.length === 0) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Get files to delete based on current filtered view
|
2025-08-28 10:56:07 +01:00
|
|
|
const filesToDelete = filteredFiles.filter(file =>
|
2025-08-21 17:30:26 +01:00
|
|
|
selectedFileIds.includes(file.id)
|
2025-08-20 16:51:55 +01:00
|
|
|
);
|
|
|
|
|
2025-09-03 17:47:58 +01:00
|
|
|
// For each selected file, determine which files to delete based on branch logic
|
|
|
|
const fileIdsToDelete = new Set<string>();
|
|
|
|
|
2025-08-20 16:51:55 +01:00
|
|
|
for (const file of filesToDelete) {
|
2025-09-03 17:47:58 +01:00
|
|
|
// If this is a leaf file (main entry), delete its entire branch
|
|
|
|
const branchLineage = fileGroups.get(file.id) || [];
|
|
|
|
|
|
|
|
if (branchLineage.length > 0) {
|
|
|
|
// This is a leaf file with a lineage - check each file in the branch
|
|
|
|
for (const branchFile of branchLineage) {
|
|
|
|
// Check if this file is part of OTHER branches (shared between branches)
|
|
|
|
const isPartOfOtherBranches = Array.from(fileGroups.values()).some(otherLineage => {
|
|
|
|
// Check if this file appears in a different branch lineage
|
|
|
|
return otherLineage !== branchLineage &&
|
|
|
|
otherLineage.some((f: any) => f.id === branchFile.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isPartOfOtherBranches) {
|
|
|
|
// File is shared between branches - don't delete it
|
|
|
|
console.log(`Keeping shared file: ${branchFile.name} (part of other branches)`);
|
|
|
|
} else {
|
|
|
|
// File is exclusive to this branch - safe to delete
|
|
|
|
fileIdsToDelete.add(branchFile.id);
|
|
|
|
console.log(`Deleting branch-exclusive file: ${branchFile.name}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This is a standalone file or history file - just delete it
|
|
|
|
fileIdsToDelete.add(file.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete files from storage
|
|
|
|
for (const fileId of fileIdsToDelete) {
|
|
|
|
await fileStorage.deleteFile(fileId as FileId);
|
2025-08-20 16:51:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear selection
|
|
|
|
setSelectedFileIds([]);
|
|
|
|
|
|
|
|
// Refresh the file list
|
|
|
|
await refreshRecentFiles();
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to delete selected files:', error);
|
|
|
|
}
|
2025-09-03 17:47:58 +01:00
|
|
|
}, [selectedFileIds, filteredFiles, fileGroups, recentFiles, refreshRecentFiles]);
|
2025-08-20 16:51:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
const handleDownloadSelected = useCallback(async () => {
|
|
|
|
if (selectedFileIds.length === 0) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Get selected files
|
2025-08-28 10:56:07 +01:00
|
|
|
const selectedFilesToDownload = filteredFiles.filter(file =>
|
2025-08-21 17:30:26 +01:00
|
|
|
selectedFileIds.includes(file.id)
|
2025-08-20 16:51:55 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Use generic download utility
|
|
|
|
await downloadFiles(selectedFilesToDownload, {
|
|
|
|
zipFilename: `selected-files-${new Date().toISOString().slice(0, 19).replace(/[:-]/g, '')}.zip`
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to download selected files:', error);
|
|
|
|
}
|
|
|
|
}, [selectedFileIds, filteredFiles]);
|
|
|
|
|
2025-08-21 17:30:26 +01:00
|
|
|
const handleDownloadSingle = useCallback(async (file: FileMetadata) => {
|
2025-08-20 16:51:55 +01:00
|
|
|
try {
|
|
|
|
await downloadFiles([file]);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to download file:', error);
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2025-09-03 17:47:58 +01:00
|
|
|
const handleToggleExpansion = useCallback((fileId: string) => {
|
|
|
|
setExpandedFileIds(prev => {
|
|
|
|
const newSet = new Set(prev);
|
|
|
|
if (newSet.has(fileId)) {
|
|
|
|
newSet.delete(fileId);
|
|
|
|
} else {
|
|
|
|
newSet.add(fileId);
|
|
|
|
}
|
|
|
|
return newSet;
|
|
|
|
});
|
2025-09-02 17:24:26 +01:00
|
|
|
}, []);
|
|
|
|
|
2025-09-03 17:47:58 +01:00
|
|
|
const handleAddToRecents = useCallback(async (file: FileMetadata) => {
|
2025-09-02 17:24:26 +01:00
|
|
|
try {
|
2025-09-03 17:47:58 +01:00
|
|
|
console.log('Promoting to recents:', file.name, 'version:', file.versionNumber);
|
2025-09-02 17:24:26 +01:00
|
|
|
|
2025-09-03 17:47:58 +01:00
|
|
|
// Load the file from storage and create a copy with new ID and timestamp
|
2025-09-02 17:24:26 +01:00
|
|
|
const storedFile = await fileStorage.getFile(file.id);
|
2025-09-03 17:47:58 +01:00
|
|
|
if (storedFile) {
|
|
|
|
// Create new file with current timestamp to appear at top
|
|
|
|
const promotedFile = new File([storedFile.data], file.name, {
|
|
|
|
type: file.type,
|
|
|
|
lastModified: Date.now() // Current timestamp makes it appear at top
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add as new file through the normal flow (creates new ID)
|
|
|
|
onNewFilesSelect([promotedFile]);
|
|
|
|
|
|
|
|
console.log('Successfully promoted to recents:', file.name, 'v' + file.versionNumber);
|
2025-09-02 17:24:26 +01:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2025-09-03 17:47:58 +01:00
|
|
|
console.error('Failed to promote to recents:', error);
|
2025-09-02 17:24:26 +01:00
|
|
|
}
|
2025-09-03 17:47:58 +01:00
|
|
|
}, [onNewFilesSelect]);
|
2025-08-20 16:51:55 +01:00
|
|
|
|
2025-08-08 15:15:09 +01:00
|
|
|
// Cleanup blob URLs when component unmounts
|
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
|
|
|
// Clean up all created blob URLs
|
|
|
|
createdBlobUrls.current.forEach(url => {
|
|
|
|
URL.revokeObjectURL(url);
|
|
|
|
});
|
|
|
|
createdBlobUrls.current.clear();
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// Reset state when modal closes
|
|
|
|
useEffect(() => {
|
|
|
|
if (!isOpen) {
|
|
|
|
setActiveSource('recent');
|
|
|
|
setSelectedFileIds([]);
|
|
|
|
setSearchTerm('');
|
2025-08-20 16:51:55 +01:00
|
|
|
setLastClickedIndex(null);
|
2025-08-08 15:15:09 +01:00
|
|
|
}
|
|
|
|
}, [isOpen]);
|
|
|
|
|
2025-08-21 17:30:26 +01:00
|
|
|
const contextValue: FileManagerContextValue = useMemo(() => ({
|
2025-08-08 15:15:09 +01:00
|
|
|
// State
|
|
|
|
activeSource,
|
|
|
|
selectedFileIds,
|
|
|
|
searchTerm,
|
|
|
|
selectedFiles,
|
|
|
|
filteredFiles,
|
|
|
|
fileInputRef,
|
2025-08-20 16:51:55 +01:00
|
|
|
selectedFilesSet,
|
2025-09-03 17:47:58 +01:00
|
|
|
expandedFileIds,
|
2025-09-02 17:24:26 +01:00
|
|
|
fileGroups,
|
2025-08-11 09:16:16 +01:00
|
|
|
|
2025-08-08 15:15:09 +01:00
|
|
|
// Handlers
|
|
|
|
onSourceChange: handleSourceChange,
|
|
|
|
onLocalFileClick: handleLocalFileClick,
|
|
|
|
onFileSelect: handleFileSelect,
|
|
|
|
onFileRemove: handleFileRemove,
|
|
|
|
onFileDoubleClick: handleFileDoubleClick,
|
|
|
|
onOpenFiles: handleOpenFiles,
|
|
|
|
onSearchChange: handleSearchChange,
|
|
|
|
onFileInputChange: handleFileInputChange,
|
2025-08-20 16:51:55 +01:00
|
|
|
onSelectAll: handleSelectAll,
|
|
|
|
onDeleteSelected: handleDeleteSelected,
|
|
|
|
onDownloadSelected: handleDownloadSelected,
|
|
|
|
onDownloadSingle: handleDownloadSingle,
|
2025-09-03 17:47:58 +01:00
|
|
|
onToggleExpansion: handleToggleExpansion,
|
|
|
|
onAddToRecents: handleAddToRecents,
|
2025-09-02 17:24:26 +01:00
|
|
|
onNewFilesSelect,
|
2025-08-11 09:16:16 +01:00
|
|
|
|
2025-08-08 15:15:09 +01:00
|
|
|
// External props
|
|
|
|
recentFiles,
|
|
|
|
isFileSupported,
|
|
|
|
modalHeight,
|
2025-08-21 17:30:26 +01:00
|
|
|
}), [
|
|
|
|
activeSource,
|
|
|
|
selectedFileIds,
|
|
|
|
searchTerm,
|
|
|
|
selectedFiles,
|
|
|
|
filteredFiles,
|
|
|
|
fileInputRef,
|
2025-09-03 17:47:58 +01:00
|
|
|
expandedFileIds,
|
2025-09-02 17:24:26 +01:00
|
|
|
fileGroups,
|
2025-08-21 17:30:26 +01:00
|
|
|
handleSourceChange,
|
|
|
|
handleLocalFileClick,
|
|
|
|
handleFileSelect,
|
|
|
|
handleFileRemove,
|
|
|
|
handleFileDoubleClick,
|
|
|
|
handleOpenFiles,
|
|
|
|
handleSearchChange,
|
|
|
|
handleFileInputChange,
|
|
|
|
handleSelectAll,
|
|
|
|
handleDeleteSelected,
|
|
|
|
handleDownloadSelected,
|
2025-09-03 17:47:58 +01:00
|
|
|
handleToggleExpansion,
|
|
|
|
handleAddToRecents,
|
2025-09-02 17:24:26 +01:00
|
|
|
onNewFilesSelect,
|
2025-08-21 17:30:26 +01:00
|
|
|
recentFiles,
|
|
|
|
isFileSupported,
|
|
|
|
modalHeight,
|
|
|
|
]);
|
2025-08-08 15:15:09 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<FileManagerContext.Provider value={contextValue}>
|
|
|
|
{children}
|
|
|
|
</FileManagerContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Custom hook to use the context
|
|
|
|
export const useFileManagerContext = (): FileManagerContextValue => {
|
|
|
|
const context = useContext(FileManagerContext);
|
2025-08-11 09:16:16 +01:00
|
|
|
|
2025-08-08 15:15:09 +01:00
|
|
|
if (!context) {
|
|
|
|
throw new Error(
|
|
|
|
'useFileManagerContext must be used within a FileManagerProvider. ' +
|
|
|
|
'Make sure you wrap your component with <FileManagerProvider>.'
|
|
|
|
);
|
|
|
|
}
|
2025-08-11 09:16:16 +01:00
|
|
|
|
2025-08-08 15:15:09 +01:00
|
|
|
return context;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Export the context for advanced use cases
|
2025-08-11 09:16:16 +01:00
|
|
|
export { FileManagerContext };
|