()
| 64 | } |
| 65 | |
| 66 | export const useFileAwareQuery = (): UseFileAwareQueryReturn => { |
| 67 | // === STATE === |
| 68 | const [query, setQuery] = useState<string>(""); |
| 69 | const [savedQuery, setSavedQuery] = useState<string>(""); // Track what's saved |
| 70 | const [isDirty, setIsDirty] = useState<boolean>(false); // Simple dirty tracking |
| 71 | |
| 72 | // === EXTERNAL STATE === |
| 73 | const canExecuteQueries = useDuckDBStore(selectCanExecuteQueries); |
| 74 | const hasUserTables = useDuckDBStore(selectHasUserTables); |
| 75 | const hasSampleTable = useDuckDBStore(selectHasSampleTable); |
| 76 | const sampleTableName = useDuckDBStore(selectSampleTableName); |
| 77 | const activeFile = useAppStore(selectActiveFile); |
| 78 | const updateFile = useAppStore((state) => state.updateFile); |
| 79 | const tableName = useAppStore(selectTableName); |
| 80 | |
| 81 | // === UTILITY FUNCTIONS === |
| 82 | |
| 83 | /** |
| 84 | * Creates default SQL state for a new file |
| 85 | */ |
| 86 | const createDefaultSqlState = useCallback((fileName: string, tableName: string): FileSqlState => { |
| 87 | const defaultQuery = `-- Query data from ${fileName || 'your file'} |
| 88 | -- Modify this query to explore your data |
| 89 | SELECT * FROM "${tableName}" LIMIT 100;`; |
| 90 | |
| 91 | return { |
| 92 | query: defaultQuery, |
| 93 | history: [], |
| 94 | lastExecutedAt: undefined, |
| 95 | }; |
| 96 | }, []); |
| 97 | |
| 98 | /** |
| 99 | * Creates default sample table query when no files are loaded |
| 100 | */ |
| 101 | const createSampleTableQuery = useCallback((): string => { |
| 102 | if (hasSampleTable) { |
| 103 | return `-- Sample employee data is available for testing |
| 104 | -- Import your own files to query your data |
| 105 | SELECT * |
| 106 | FROM "${sampleTableName}" |
| 107 | LIMIT 10;`; |
| 108 | } |
| 109 | return `-- Import your own CSV, JSON, or Parquet files to get started |
| 110 | -- Your data will appear here once loaded`; |
| 111 | }, [hasSampleTable, sampleTableName]); |
| 112 | |
| 113 | // === IMPERATIVE API === |
| 114 | |
| 115 | /** |
| 116 | * Save current query to the active file's state |
| 117 | */ |
| 118 | const saveCurrentQuery = useCallback((): void => { |
| 119 | if (!activeFile || !query.trim()) return; |
| 120 | |
| 121 | const currentSqlState = activeFile.sqlState || createDefaultSqlState(activeFile.fileName, tableName || 'unknown'); |
| 122 | |
| 123 | // Only save if different |
no outgoing calls
no test coverage detected