@smallwebco/tinypivot-react 1.0.59 → 1.0.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -143,6 +143,40 @@ function useAIAnalyst(options) {
143
143
  console.warn("Failed to fetch schema:", err);
144
144
  }
145
145
  }, []);
146
+ const fetchSampleData = useCallback(async (dataSource) => {
147
+ if (!configRef.current.endpoint)
148
+ return;
149
+ try {
150
+ const sql = `SELECT * FROM ${dataSource.table} LIMIT 100`;
151
+ const response = await fetch(configRef.current.endpoint, {
152
+ method: "POST",
153
+ headers: { "Content-Type": "application/json" },
154
+ body: JSON.stringify({
155
+ action: "query",
156
+ sql,
157
+ table: dataSource.table
158
+ })
159
+ });
160
+ if (!response.ok) {
161
+ throw new Error(`Failed to fetch sample data: ${response.statusText}`);
162
+ }
163
+ const result = await response.json();
164
+ if (result.error) {
165
+ throw new Error(result.error);
166
+ }
167
+ if (result.data && result.data.length > 0) {
168
+ setLastLoadedData(result.data);
169
+ onDataLoaded?.({
170
+ data: result.data,
171
+ query: sql,
172
+ dataSourceId: dataSource.id,
173
+ rowCount: result.data.length
174
+ });
175
+ }
176
+ } catch (err) {
177
+ console.warn("Failed to fetch sample data:", err);
178
+ }
179
+ }, [onDataLoaded]);
146
180
  const selectDataSource = useCallback(async (dataSourceId) => {
147
181
  const dataSource = effectiveDataSources.find((ds) => ds.id === dataSourceId);
148
182
  if (!dataSource) {
@@ -197,8 +231,9 @@ What would you like to know about this data?`
197
231
  }
198
232
  } else if (configRef.current.endpoint) {
199
233
  await fetchSchema(dataSource);
234
+ await fetchSampleData(dataSource);
200
235
  }
201
- }, [effectiveDataSources, fetchSchema, onConversationUpdate, onDataLoaded]);
236
+ }, [effectiveDataSources, fetchSchema, fetchSampleData, onConversationUpdate, onDataLoaded]);
202
237
  const callAIEndpoint = useCallback(async (userInput, currentConversation, currentSchemas, currentDataSources) => {
203
238
  if (!configRef.current.endpoint) {
204
239
  throw new Error("No endpoint configured. Set `endpoint` in AI analyst config.");