()
| 7 | import { DataLoadWithDuckDBResult } from '@/components/layout/Sidebar'; |
| 8 | |
| 9 | export function useLazyFileLoader() { |
| 10 | const [loadingFileId, setLoadingFileId] = useState<string | null>(null); |
| 11 | const [error, setError] = useState<string | null>(null); |
| 12 | |
| 13 | const { getNodeById, updateFileData, markFileAsLoaded } = useFolderStore(); |
| 14 | const { processFileStreaming, processFile } = useDirectFileImport(); |
| 15 | const { addFile: addToAppStore, setActiveFile } = useAppStore(); |
| 16 | |
| 17 | const loadFile = useCallback(async ( |
| 18 | node: FolderNode, |
| 19 | onDataLoad?: (result: DataLoadWithDuckDBResult) => void |
| 20 | ) => { |
| 21 | if (node.type !== 'file' || !node.fileData) { |
| 22 | setError('Invalid file node'); |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | // If file is already loaded, just switch to it |
| 27 | if (node.fileData.isLoaded && node.fileData.tableName) { |
| 28 | console.log(`[LazyLoader] File already loaded: ${node.name} (${node.fileData.tableName})`); |
| 29 | |
| 30 | // Find the file in app store and set it active |
| 31 | const appFiles = useAppStore.getState().files; |
| 32 | const existingFile = appFiles.find(f => f.fileName === node.name); |
| 33 | if (existingFile) { |
| 34 | setActiveFile(existingFile.id); |
| 35 | return true; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | setLoadingFileId(node.id); |
| 40 | setError(null); |
| 41 | |
| 42 | try { |
| 43 | console.log(`[LazyLoader] Loading file: ${node.name}`); |
| 44 | |
| 45 | let result: DataLoadWithDuckDBResult | undefined; |
| 46 | |
| 47 | // Check if we have a file handle for streaming |
| 48 | if (node.fileData.handle) { |
| 49 | // Try to get the file from handle |
| 50 | try { |
| 51 | const file = await node.fileData.handle.getFile(); |
| 52 | |
| 53 | // Check if file still exists and matches |
| 54 | if (file.name === node.name) { |
| 55 | console.log(`[LazyLoader] Using file handle for streaming import`); |
| 56 | result = await processFileStreaming(node.fileData.handle, file, onDataLoad); |
| 57 | } else { |
| 58 | throw new Error('File name mismatch'); |
| 59 | } |
| 60 | } catch (handleError) { |
| 61 | console.warn(`[LazyLoader] File handle failed, prompting user:`, handleError); |
| 62 | |
| 63 | // Prompt user to re-select the file |
| 64 | if ('showOpenFilePicker' in window) { |
| 65 | const fileHandleArray = await window.showOpenFilePicker({ |
| 66 | types: [ |
nothing calls this directly
no test coverage detected