(targetFileId?: string)
| 34 | * Follows the same pattern as useQueryExecution for consistency |
| 35 | */ |
| 36 | export const useDataPreview = (targetFileId?: string): DataPreviewResult => { |
| 37 | const activeFile = useAppStore(selectActiveFile); |
| 38 | const { executePaginatedQuery, getObjectType } = useDuckDBStore(); |
| 39 | |
| 40 | const { |
| 41 | getFileState, |
| 42 | updateFileState, |
| 43 | initializeFileState |
| 44 | } = useDataPreviewStore(); |
| 45 | |
| 46 | // Local loading state for page changes |
| 47 | const [isChangingPage, setIsChangingPage] = useState(false); |
| 48 | |
| 49 | // Use provided fileId or fall back to active file |
| 50 | const fileId = targetFileId || activeFile?.id || ''; |
| 51 | const fileState = getFileState(fileId) || { |
| 52 | currentPage: 1, |
| 53 | rowsPerPage: 1000, |
| 54 | totalRows: 0, |
| 55 | totalPages: 0, |
| 56 | data: null, |
| 57 | columns: null, |
| 58 | isLoading: false, |
| 59 | isCountLoading: false, |
| 60 | error: null, |
| 61 | lastFetchTime: 0, |
| 62 | }; |
| 63 | |
| 64 | // Get current file based on fileId parameter |
| 65 | const currentFile = targetFileId |
| 66 | ? useAppStore.getState().files.find(f => f.id === targetFileId) |
| 67 | : activeFile; |
| 68 | |
| 69 | // Initialize file state when file changes |
| 70 | useEffect(() => { |
| 71 | if (fileId) { |
| 72 | initializeFileState(fileId); |
| 73 | } |
| 74 | }, [fileId, initializeFileState]); |
| 75 | |
| 76 | /** |
| 77 | * Load initial data for the target file |
| 78 | */ |
| 79 | const loadInitialData = useCallback(async () => { |
| 80 | if (!currentFile?.tableName || !fileId) return; |
| 81 | |
| 82 | const existingState = getFileState(fileId); |
| 83 | |
| 84 | // If we already have data cached and it's recent (< 5 minutes), use it |
| 85 | if (existingState?.data && existingState.lastFetchTime > Date.now() - 5 * 60 * 1000) { |
| 86 | console.log('[useDataPreview] Using cached data for file:', currentFile.fileName); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | try { |
| 91 | updateFileState(fileId, { |
| 92 | isLoading: true, |
| 93 | error: null |
no test coverage detected