@smallwebco/tinypivot-react 1.0.60 → 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.cjs CHANGED
@@ -187,6 +187,40 @@ function useAIAnalyst(options) {
187
187
  console.warn("Failed to fetch schema:", err);
188
188
  }
189
189
  }, []);
190
+ const fetchSampleData = (0, import_react.useCallback)(async (dataSource) => {
191
+ if (!configRef.current.endpoint)
192
+ return;
193
+ try {
194
+ const sql = `SELECT * FROM ${dataSource.table} LIMIT 100`;
195
+ const response = await fetch(configRef.current.endpoint, {
196
+ method: "POST",
197
+ headers: { "Content-Type": "application/json" },
198
+ body: JSON.stringify({
199
+ action: "query",
200
+ sql,
201
+ table: dataSource.table
202
+ })
203
+ });
204
+ if (!response.ok) {
205
+ throw new Error(`Failed to fetch sample data: ${response.statusText}`);
206
+ }
207
+ const result = await response.json();
208
+ if (result.error) {
209
+ throw new Error(result.error);
210
+ }
211
+ if (result.data && result.data.length > 0) {
212
+ setLastLoadedData(result.data);
213
+ onDataLoaded?.({
214
+ data: result.data,
215
+ query: sql,
216
+ dataSourceId: dataSource.id,
217
+ rowCount: result.data.length
218
+ });
219
+ }
220
+ } catch (err) {
221
+ console.warn("Failed to fetch sample data:", err);
222
+ }
223
+ }, [onDataLoaded]);
190
224
  const selectDataSource = (0, import_react.useCallback)(async (dataSourceId) => {
191
225
  const dataSource = effectiveDataSources.find((ds) => ds.id === dataSourceId);
192
226
  if (!dataSource) {
@@ -241,8 +275,9 @@ What would you like to know about this data?`
241
275
  }
242
276
  } else if (configRef.current.endpoint) {
243
277
  await fetchSchema(dataSource);
278
+ await fetchSampleData(dataSource);
244
279
  }
245
- }, [effectiveDataSources, fetchSchema, onConversationUpdate, onDataLoaded]);
280
+ }, [effectiveDataSources, fetchSchema, fetchSampleData, onConversationUpdate, onDataLoaded]);
246
281
  const callAIEndpoint = (0, import_react.useCallback)(async (userInput, currentConversation, currentSchemas, currentDataSources) => {
247
282
  if (!configRef.current.endpoint) {
248
283
  throw new Error("No endpoint configured. Set `endpoint` in AI analyst config.");