()
| 34 | }; |
| 35 | |
| 36 | export const useFileImport = () => { |
| 37 | const { t } = useTranslation(); |
| 38 | const analytics = useAnalytics(); |
| 39 | const { showSuccess, showError } = useNotifications(); |
| 40 | |
| 41 | // Stores |
| 42 | const { addFile, addFileToWorkspace } = useAppStore(); |
| 43 | const { addFile: addFileToFolder, draftFolderId, expandFolder } = useFolderStore(); |
| 44 | |
| 45 | // Import hooks |
| 46 | const { processFile, processFileStreaming, isProcessing } = useDirectFileImport(); |
| 47 | |
| 48 | // Create workspace file from File object |
| 49 | const createWorkspaceFile = useCallback((file: File, handle?: FileSystemFileHandle): WorkspaceFile => { |
| 50 | const fileType = file.name.split('.').pop()?.toLowerCase() || 'txt'; |
| 51 | const workspaceFileType = fileType === 'xlsx' || fileType === 'xls' ? 'excel' : fileType as WorkspaceFile['type']; |
| 52 | |
| 53 | return { |
| 54 | id: `file-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`, |
| 55 | name: file.name, |
| 56 | type: workspaceFileType, |
| 57 | size: file.size, |
| 58 | lastModified: file.lastModified, |
| 59 | handle: handle, // May be undefined for drag/drop or fallback |
| 60 | }; |
| 61 | }, []); |
| 62 | |
| 63 | // Add file to all stores (unified logic) with duplicate detection |
| 64 | const addToAllStores = useCallback(( |
| 65 | file: File, |
| 66 | result: DataLoadWithDuckDBResult, |
| 67 | handle?: FileSystemFileHandle, |
| 68 | targetFolderId?: string |
| 69 | ) => { |
| 70 | // Check if file is already open in app store |
| 71 | const { files, setActiveFile, workspaceFiles } = useAppStore.getState(); |
| 72 | const existingFile = files.find(f => f.fileName === file.name); |
| 73 | |
| 74 | if (existingFile) { |
| 75 | console.log('[UnifiedFileImport] File already open, switching to existing tab:', file.name); |
| 76 | setActiveFile(existingFile.id); |
| 77 | |
| 78 | // Check if we should add to target folder (for drag & drop to specific folders) |
| 79 | if (targetFolderId) { |
| 80 | const fileType = file.name.split('.').pop()?.toLowerCase() || 'txt'; |
| 81 | const { getChildren } = useFolderStore.getState(); |
| 82 | const folderChildren = getChildren(targetFolderId); |
| 83 | const existingInFolder = folderChildren.find(node => |
| 84 | node.type === 'file' && |
| 85 | node.name === file.name |
| 86 | ); |
| 87 | |
| 88 | if (!existingInFolder) { |
| 89 | console.log('[UnifiedFileImport] Adding existing file to target folder:', targetFolderId); |
| 90 | addFileToFolder(file, { |
| 91 | handle, |
| 92 | size: file.size, |
| 93 | lastModified: file.lastModified, |
no test coverage detected