@macrostrat/data-sheet 3.0.3 → 3.1.0

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.
Files changed (80) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/dist/cell-renderer.cjs +162 -0
  3. package/dist/cell-renderer.cjs.map +1 -0
  4. package/dist/cell-renderer.d.ts +8 -0
  5. package/dist/cell-renderer.js +162 -0
  6. package/dist/cell-renderer.js.map +1 -0
  7. package/dist/components/actions.cjs +2 -6
  8. package/dist/components/actions.cjs.map +1 -1
  9. package/dist/components/actions.js +1 -3
  10. package/dist/components/actions.js.map +1 -1
  11. package/dist/components/colors.cjs +4 -7
  12. package/dist/components/colors.cjs.map +1 -1
  13. package/dist/components/colors.js +1 -3
  14. package/dist/components/colors.js.map +1 -1
  15. package/dist/components/editor-popup.cjs +15 -19
  16. package/dist/components/editor-popup.cjs.map +1 -1
  17. package/dist/components/editor-popup.d.ts +1 -1
  18. package/dist/components/editor-popup.js +10 -12
  19. package/dist/components/editor-popup.js.map +1 -1
  20. package/dist/core.cjs +67 -207
  21. package/dist/core.cjs.map +1 -1
  22. package/dist/core.d.ts +4 -9
  23. package/dist/core.js +48 -185
  24. package/dist/core.js.map +1 -1
  25. package/dist/data-sheet.css +9 -9
  26. package/dist/index.cjs +16 -4
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.ts +2 -0
  29. package/dist/index.js +17 -8
  30. package/dist/index.js.map +1 -1
  31. package/dist/postgrest-table/data-loaders.cjs +109 -0
  32. package/dist/postgrest-table/data-loaders.cjs.map +1 -1
  33. package/dist/postgrest-table/data-loaders.d.ts +6 -0
  34. package/dist/postgrest-table/data-loaders.js +111 -2
  35. package/dist/postgrest-table/data-loaders.js.map +1 -1
  36. package/dist/postgrest-table/index.cjs +1 -0
  37. package/dist/postgrest-table/index.cjs.map +1 -1
  38. package/dist/postgrest-table/index.d.ts +2 -1
  39. package/dist/postgrest-table/index.js +2 -0
  40. package/dist/postgrest-table/index.js.map +1 -1
  41. package/dist/postgrest-table/lazy-loader-table.cjs +26 -0
  42. package/dist/postgrest-table/lazy-loader-table.cjs.map +1 -0
  43. package/dist/postgrest-table/lazy-loader-table.d.ts +3 -0
  44. package/dist/postgrest-table/lazy-loader-table.js +24 -0
  45. package/dist/postgrest-table/lazy-loader-table.js.map +1 -0
  46. package/dist/provider.cjs +59 -336
  47. package/dist/provider.cjs.map +1 -1
  48. package/dist/provider.d.ts +11 -65
  49. package/dist/provider.js +58 -336
  50. package/dist/provider.js.map +1 -1
  51. package/dist/types.d.ts +54 -0
  52. package/dist/utils/column-spec.cjs +6 -1
  53. package/dist/utils/column-spec.cjs.map +1 -1
  54. package/dist/utils/column-spec.js +6 -1
  55. package/dist/utils/column-spec.js.map +1 -1
  56. package/dist/utils/key-handling.cjs +26 -1
  57. package/dist/utils/key-handling.cjs.map +1 -1
  58. package/dist/utils/key-handling.d.ts +3 -2
  59. package/dist/utils/key-handling.js +27 -2
  60. package/dist/utils/key-handling.js.map +1 -1
  61. package/dist/zustand-store.cjs +320 -0
  62. package/dist/zustand-store.cjs.map +1 -0
  63. package/dist/zustand-store.d.ts +6 -0
  64. package/dist/zustand-store.js +318 -0
  65. package/dist/zustand-store.js.map +1 -0
  66. package/package.json +7 -6
  67. package/src/cell-renderer.ts +217 -0
  68. package/src/components/actions.ts +1 -3
  69. package/src/components/colors.ts +1 -4
  70. package/src/components/editor-popup.ts +9 -12
  71. package/src/core.ts +82 -254
  72. package/src/index.ts +3 -0
  73. package/src/postgrest-table/data-loaders.ts +151 -1
  74. package/src/postgrest-table/index.ts +2 -1
  75. package/src/postgrest-table/lazy-loader-table.ts +24 -0
  76. package/src/provider.ts +66 -483
  77. package/src/types.ts +66 -0
  78. package/src/utils/column-spec.ts +6 -1
  79. package/src/utils/key-handling.ts +31 -5
  80. package/src/zustand-store.ts +377 -0
package/src/core.ts CHANGED
@@ -5,25 +5,30 @@ import {
5
5
  InputGroup,
6
6
  Intent,
7
7
  } from "@blueprintjs/core";
8
- import { Cell, Column, Region, RowHeaderCell, Table } from "@blueprintjs/table";
8
+ import {
9
+ Column,
10
+ Region,
11
+ RegionCardinality,
12
+ RowHeaderCell,
13
+ Table,
14
+ TableProps,
15
+ } from "@blueprintjs/table";
9
16
  import "@blueprintjs/table/lib/css/table.css";
10
- import hyper from "@macrostrat/hyper";
11
17
  import update from "immutability-helper";
12
18
  import { ReactNode, useCallback, useEffect, useMemo, useState } from "react";
13
- import { EditorPopup, DataSheetAction } from "./components";
14
- import styles from "./main.module.sass";
19
+ import { DataSheetAction } from "./components";
20
+ import h from "./main.module.sass";
15
21
  import {
16
22
  DataSheetProvider,
17
- DataSheetProviderProps,
18
- DataSheetStore,
19
- singleFocusedCell,
23
+ storeAtom,
24
+ useAtomValue,
20
25
  useSelector,
21
26
  useStoreAPI,
22
- VisibleCells,
27
+ atom,
23
28
  } from "./provider";
24
- import { ColumnSpec } from "./utils";
25
-
26
- const h = hyper.styled(styles);
29
+ import { DataSheetProviderProps, VisibleCells } from "./types.ts";
30
+ import { basicCellRenderer } from "./cell-renderer.ts";
31
+ import { tableKeyHandlerAtom } from "./utils";
27
32
 
28
33
  // More on component templates: https://storybook.js.org/docs/react/writing-stories/introduction#using-args
29
34
 
@@ -36,7 +41,7 @@ export enum DataSheetDensity {
36
41
  LOW = "low",
37
42
  }
38
43
 
39
- interface DataSheetInternalProps<T> {
44
+ interface DataSheetInternalProps<T> extends TableProps {
40
45
  onVisibleCellsChange?: (visibleCells: VisibleCells) => void;
41
46
  onSaveData?: (updatedData: any[], data: T[]) => void;
42
47
  onUpdateData?: (updatedData: any[], data: T[]) => void;
@@ -71,7 +76,7 @@ export function DataSheet<T>(props: DataSheetProps<T>) {
71
76
  {
72
77
  data,
73
78
  columnSpec,
74
- columnSpecOptions: columnSpecOptions,
79
+ columnSpecOptions,
75
80
  enableColumnReordering,
76
81
  defaultColumnWidth,
77
82
  editable,
@@ -87,9 +92,13 @@ export function DataSheet<T>(props: DataSheetProps<T>) {
87
92
  );
88
93
  }
89
94
 
95
+ const deletedRowHeaderStyle = {
96
+ opacity: 0.5,
97
+ textDecoration: "line-through",
98
+ };
99
+
90
100
  function _DataSheet<T>({
91
101
  onVisibleCellsChange,
92
- enableColumnReordering,
93
102
  onSaveData,
94
103
  onUpdateData,
95
104
  onDeleteRows,
@@ -98,6 +107,8 @@ function _DataSheet<T>({
98
107
  enableFocusedCell,
99
108
  autoFocusEditor = true,
100
109
  density = DataSheetDensity.HIGH,
110
+ selectionModes,
111
+ ...rest
101
112
  }: DataSheetInternalProps<T>) {
102
113
  /**
103
114
  * @param data: The data to be displayed in the table
@@ -181,7 +192,7 @@ function _DataSheet<T>({
181
192
  [selectedRegions],
182
193
  );
183
194
 
184
- const columnWidths = useSelector((state) => state.columnWidths);
195
+ const columnWidths = useAtomValue(columnWidthsAtom);
185
196
 
186
197
  const numRows = Math.max(updatedData.length, data.length);
187
198
 
@@ -207,25 +218,25 @@ function _DataSheet<T>({
207
218
  };
208
219
  }
209
220
 
210
- const children = useMemo(
211
- () =>
212
- columnSpec.map((col, colIndex) => {
213
- return h(Column, {
214
- name: col.name,
215
- cellRenderer: (rowIndex) => {
216
- const state = storeAPI.getState();
217
- return basicCellRenderer<T>(
218
- rowIndex,
219
- colIndex,
220
- col,
221
- state,
222
- autoFocusEditor,
223
- );
224
- },
225
- });
226
- }),
227
- [columnSpec, storeAPI, autoFocusEditor],
228
- );
221
+ const onColumnsReordered = useSelector((state) => state.onColumnsReordered);
222
+
223
+ const children = useMemo(() => {
224
+ return columnSpec.map((col, colIndex) => {
225
+ return h(Column, {
226
+ name: col.name,
227
+ cellRenderer: (rowIndex) => {
228
+ const state = storeAPI.getState();
229
+ return basicCellRenderer<T>(
230
+ rowIndex,
231
+ colIndex,
232
+ col,
233
+ state,
234
+ autoFocusEditor,
235
+ );
236
+ },
237
+ });
238
+ });
239
+ }, [columnSpec, storeAPI, autoFocusEditor]);
229
240
 
230
241
  const onColumnWidthChanged = useSelector(
231
242
  (state) => state.onColumnWidthChanged,
@@ -233,14 +244,10 @@ function _DataSheet<T>({
233
244
 
234
245
  const rowHeaderCellRenderer = useCallback(
235
246
  (rowIndex: number) => {
236
- let style = null;
237
- if (deletedRows.has(rowIndex)) {
238
- style = {
239
- opacity: 0.5,
240
- textDecoration: "line-through",
241
- };
242
- }
247
+ const style = deletedRows.has(rowIndex) ? deletedRowHeaderStyle : null;
248
+
243
249
  return h(RowHeaderCell, {
250
+ enableRowReordering: false,
244
251
  index: rowIndex,
245
252
  name: `${rowIndex + 1}`,
246
253
  style,
@@ -249,9 +256,17 @@ function _DataSheet<T>({
249
256
  [deletedRows],
250
257
  );
251
258
 
252
- const onKeyDown = useSelector((state) => state.tableKeyHandler);
259
+ const onKeyDown = useAtomValue(tableKeyHandlerAtom);
253
260
 
254
- if (data == null) return null;
261
+ let _selectionModes = selectionModes;
262
+ if (
263
+ editable &&
264
+ _selectionModes != null &&
265
+ !_selectionModes.includes(RegionCardinality.CELLS)
266
+ ) {
267
+ _selectionModes = [..._selectionModes, RegionCardinality.CELLS];
268
+ // Ensure selection mode includes "cells"
269
+ }
255
270
 
256
271
  return h("div.data-sheet-container", { className, style }, [
257
272
  h.if(editable)(DataSheetEditToolbar, {
@@ -263,15 +278,13 @@ function _DataSheet<T>({
263
278
  "div.data-sheet-holder",
264
279
  { onKeyDown },
265
280
  h(
266
- // @ts-expect-error
267
281
  Table,
268
282
  {
269
283
  ref,
270
284
  numRows,
271
285
  className: "data-sheet",
272
286
  enableFocusedCell,
273
- enableColumnReordering,
274
- //onColumnsReordered,
287
+ onColumnsReordered,
275
288
  focusedCell,
276
289
  selectedRegions,
277
290
  defaultRowHeight: rowHeight,
@@ -279,6 +292,8 @@ function _DataSheet<T>({
279
292
  columnWidths,
280
293
  onColumnWidthChanged,
281
294
  onSelection,
295
+ enableRowReordering: false,
296
+ enableRowResizing: false,
282
297
  // The cell renderer is memoized internally based on these data dependencies
283
298
  cellRendererDependencies: [
284
299
  data,
@@ -289,6 +304,8 @@ function _DataSheet<T>({
289
304
  ],
290
305
  onVisibleCellsChange: _onVisibleCellsChange,
291
306
  rowHeaderCellRenderer,
307
+ selectionModes: _selectionModes,
308
+ ...rest,
292
309
  },
293
310
  children,
294
311
  ),
@@ -296,210 +313,34 @@ function _DataSheet<T>({
296
313
  ]);
297
314
  }
298
315
 
299
- function basicCellRenderer<T>(
300
- rowIndex: number,
301
- colIndex: number,
302
- columnSpec: ColumnSpec,
303
- state: DataSheetStore<T>,
304
- autoFocusEditor = true,
305
- ): any {
306
- const data = state.data;
307
- const updatedData = state.updatedData;
308
-
309
- const isDeleted = state.deletedRows.has(rowIndex);
310
-
311
- const row = data[rowIndex] ?? updatedData[rowIndex];
312
- const loading = row == null;
313
- const col = columnSpec;
314
-
315
- const focusedCell = state.focusedCell;
316
- const _topLeftCell = state.topLeftCell;
317
- const onCellEdited = state.onCellEdited;
318
- const clearSelection = state.clearSelection;
319
-
320
- const value = updatedData[rowIndex]?.[col.key] ?? data[rowIndex]?.[col.key];
321
- const _renderedValue = col.valueRenderer?.(value) ?? value;
322
-
323
- let style = col.style ?? {};
324
- if (isDeleted) {
325
- style = {
326
- ...style,
327
- opacity: 0.5,
328
- textDecoration: "line-through",
329
- };
330
- }
331
-
332
- const focused =
333
- focusedCell?.col === colIndex && focusedCell?.row === rowIndex;
334
-
335
- const editable = (col.editable ?? state.editable) && !isDeleted;
336
-
337
- // Top left cell of a ranged selection
338
- const topLeft =
339
- _topLeftCell?.col === colIndex && _topLeftCell?.row === rowIndex;
340
-
341
- if (!editable && state.editable) {
342
- // If the cell is not editable but the sheet is editable, we want to differentiate
343
- // between editable and non-editable cells.
344
- style.color = "var(--secondary-color)";
345
- }
316
+ /** Atoms for efficient sub-selection of state */
346
317
 
347
- const edited = updatedData[rowIndex]?.[col.key] != null;
348
- let intent = edited ? "success" : undefined;
349
- if (isDeleted) {
350
- intent = "danger";
351
- }
318
+ const deletedRowsAtom = atom((get) => get(storeAtom).deletedRows);
319
+ const updatedDataAtom = atom((get) => get(storeAtom).updatedData);
352
320
 
353
- const _Cell = col.cellComponent ?? BaseCell;
321
+ const hasUpdatesAtom = atom((get) => {
322
+ // Readable atom to indicate whether there are any updates in the updatedData array
323
+ const deletedRows = get(deletedRowsAtom);
324
+ const updatedData = get(updatedDataAtom);
325
+ return updatedData.length > 0 || deletedRows.size > 0;
326
+ });
354
327
 
355
- let inlineEditor = editable ? (col.inlineEditor ?? true) : false;
328
+ const columnSpecAtom = atom((get) => get(storeAtom).columnSpec);
329
+ const columnWidthsIndexAtom = atom((get) => get(storeAtom).columnWidthsIndex);
330
+ const defaultColumnWidthAtom = atom((get) => get(storeAtom).defaultColumnWidth);
356
331
 
357
- if (!topLeft) {
358
- // This should be the case for every cell except the focused one
359
- return h(
360
- _Cell,
361
- {
362
- intent,
363
- loading,
364
- value,
365
- style,
366
- isDeleted,
367
- },
368
- _renderedValue,
369
- );
370
- }
371
-
372
- // The rest is for the top-left cell of a selection or the focused cell
373
-
374
- // Hidden input to capture key events
375
- let hiddenInput = h("input.hidden-input", {
376
- autoFocus: true,
377
- onKeyDown: state.editorKeyHandler,
378
- });
379
-
380
- let cellContents: ReactNode = _renderedValue;
381
-
382
- let _dataEditor = null;
383
- let className = null;
384
-
385
- if (col.dataEditor != null) {
386
- _dataEditor = h(
387
- EditorPopup,
388
- {
389
- autoFocus: autoFocusEditor,
390
- valueViewer: _renderedValue,
391
- },
392
- [
393
- h(col.dataEditor, {
394
- value,
395
- editable,
396
- onChange(value) {
397
- if (!editable) return;
398
- state.onSelectionEdited(value);
399
- },
400
- }),
401
- ],
402
- );
403
- }
404
-
405
- if (!editable) {
406
- // Most cells are not focused and don't need to be editable.
407
- // This will be the rendering logic for almost all cells
408
-
409
- if (_dataEditor != null) {
410
- cellContents = _dataEditor;
411
- }
412
-
413
- return h(
414
- _Cell,
415
- {
416
- intent,
417
- value,
418
- style,
419
- },
420
- cellContents,
421
- );
422
- // Could probably put the hidden input elsewhere,
423
- }
424
-
425
- /* The remaining logic covers cells that are focused and editable */
426
-
427
- const onChange = (e) => {
428
- if (!editable) return;
429
- if (value === e.target.value) return;
430
- onCellEdited(rowIndex, col.key, e.target.value);
431
- };
432
-
433
- const isSingleCellSelection = singleFocusedCell(state.selection) != null;
434
-
435
- let _inlineEditor: ReactNode = null;
436
- if (typeof inlineEditor == "boolean") {
437
- let _value = value;
438
- if (
439
- typeof _renderedValue === "string" ||
440
- typeof _renderedValue === "number" ||
441
- _renderedValue == null
442
- ) {
443
- _value = _renderedValue;
444
- }
445
- _inlineEditor = h("input.main-editor", {
446
- value: _value ?? "",
447
- autoFocus: autoFocusEditor,
448
- onChange,
449
- onKeyDown: state.editorKeyHandler,
450
- });
451
- } else {
452
- // If inlineEditor is a ReactNode, we use it directly
453
- _inlineEditor = inlineEditor as ReactNode;
454
- }
455
-
456
- if (_dataEditor != null) {
457
- className = "editor-cell";
458
- cellContents = _dataEditor;
459
- hiddenInput = null;
460
- } else if (_inlineEditor != null) {
461
- cellContents = _inlineEditor;
462
- className = "input-cell";
463
- hiddenInput = null;
464
- }
465
-
466
- // Hidden html input
467
- return h(
468
- _Cell,
469
- {
470
- intent,
471
- value,
472
- className,
473
- style,
474
- //truncated: false,
475
- },
476
- [
477
- cellContents,
478
- h.if(editable && isSingleCellSelection)(DragHandle, {
479
- focusedCell,
480
- }),
481
- hiddenInput,
482
- ],
332
+ const columnWidthsAtom = atom((get) => {
333
+ const ix = get(columnWidthsIndexAtom);
334
+ return get(columnSpecAtom).map(
335
+ (col) => ix.get(col.key) ?? col.width ?? get(defaultColumnWidthAtom),
483
336
  );
484
- }
485
-
486
- function DragHandle({ focusedCell }) {
487
- // TODO: we might want to drag multiple columns in some cases
488
- // This should be on the last cell of a selection
489
- const onDragValue = useSelector((state) => state.onDragValue);
490
-
491
- return h("div.corner-drag-handle", {
492
- onMouseDown(e) {
493
- onDragValue(focusedCell);
494
- e.preventDefault();
495
- },
496
- });
497
- }
337
+ });
498
338
 
499
339
  function DataSheetEditToolbar({ onSaveData, onDeleteRows }) {
500
340
  const selection = useSelector((state) => state.selection);
501
341
  const resetChanges = useSelector((state) => state.resetChanges);
502
- const hasUpdates = useSelector((state) => state.hasUpdates);
342
+
343
+ const hasUpdates = useAtomValue(hasUpdatesAtom);
503
344
 
504
345
  return h("div.data-sheet-toolbar", [
505
346
  h(ButtonGroup, { minimal: true }, [
@@ -580,19 +421,6 @@ function AddRowButton() {
580
421
  );
581
422
  }
582
423
 
583
- export function BaseCell({ children, value, ...rest }) {
584
- return h(
585
- Cell,
586
- {
587
- interactive: true,
588
- ...rest,
589
- },
590
- children,
591
- );
592
- }
593
-
594
- export { Cell };
595
-
596
424
  export function getRowsToDelete(selection) {
597
425
  let rowIndices: number[] = [];
598
426
  for (const sel of selection) {
package/src/index.ts CHANGED
@@ -3,3 +3,6 @@ export * from "./postgrest-table";
3
3
  export * from "./components";
4
4
  export * from "./provider";
5
5
  export * from "./utils";
6
+ export * from "./types";
7
+
8
+ export { Cell } from "@blueprintjs/table";
@@ -1,7 +1,7 @@
1
1
  /** Lazy loading of data from a PostgREST endpoint */
2
2
 
3
3
  import { useAsyncEffect } from "@macrostrat/ui-components";
4
- import { debounce } from "underscore";
4
+ import { debounce, range } from "underscore";
5
5
  import { useCallback, useMemo, useReducer, useRef, useEffect } from "react";
6
6
  import update, { Spec } from "immutability-helper";
7
7
 
@@ -337,3 +337,153 @@ function indexOfFirstNullInRegion(
337
337
  }
338
338
  return null;
339
339
  }
340
+
341
+ export function useTestLazyLoader(config: LazyLoaderOptions = {}) {
342
+ const initialState: LazyLoaderState<any> = {
343
+ data: [],
344
+ loading: false,
345
+ error: null,
346
+ visibleRegion: { rowIndexStart: 0, rowIndexEnd: 0 },
347
+ initialized: false,
348
+ };
349
+
350
+ const [state, dispatch] = useReducer(lazyLoadingReducer, initialState);
351
+ const { data, loading } = state;
352
+
353
+ useAsyncEffect(async () => {
354
+ testDataLoader(config, state, dispatch);
355
+ }, [
356
+ data,
357
+ state.visibleRegion.rowIndexStart,
358
+ state.visibleRegion.rowIndexEnd,
359
+ ]);
360
+
361
+ // Reference to hold onto the scroll position
362
+ const ref = useRef(null);
363
+
364
+ const onScroll = useCallback(
365
+ debounce((visibleCells: RowRegion) => {
366
+ if (
367
+ visibleCells.rowIndexEnd == ref.current?.rowIndexEnd &&
368
+ visibleCells.rowIndexStart == ref.current?.rowIndexStart
369
+ ) {
370
+ return;
371
+ }
372
+ console.log("Visible cells changed", visibleCells);
373
+ dispatch({
374
+ type: "set-visible",
375
+ region: visibleCells,
376
+ });
377
+ ref.current = visibleCells;
378
+ }, 500),
379
+ [dispatch],
380
+ );
381
+
382
+ return {
383
+ data,
384
+ loading,
385
+ onScroll,
386
+ dispatch,
387
+ };
388
+ }
389
+
390
+ function testDataLoader(
391
+ config: { chunkSize: number },
392
+ state: LazyLoaderState<any>,
393
+ dispatch,
394
+ ) {
395
+ const sortKey = "id";
396
+ const rowIndex = indexOfFirstNullInRegion(state.data, state.visibleRegion);
397
+ if (state.loading || rowIndex == null) {
398
+ if (state.initialized) {
399
+ return;
400
+ }
401
+ }
402
+
403
+ const chunkSize = 10;
404
+
405
+ let cfg: QueryConfig = {
406
+ //...rest,
407
+ limit: chunkSize,
408
+ offset: null,
409
+ };
410
+
411
+ // Allows random seeking
412
+ const isInitialQuery = !state.initialized;
413
+ if (isInitialQuery) {
414
+ cfg.count = "exact";
415
+ }
416
+
417
+ // This only works for forward queries
418
+ if (!isInitialQuery) {
419
+ cfg.after = state.data[rowIndex - 1]?.[sortKey];
420
+ if (cfg.after == null) {
421
+ cfg.offset = rowIndex;
422
+ }
423
+ }
424
+
425
+ dispatch({ type: "start-loading" });
426
+
427
+ const query = testQueryFunc(cfg);
428
+ query.then((res) => {
429
+ console.log(res);
430
+ const { data, count } = res;
431
+ dispatch({
432
+ type: "loaded",
433
+ data,
434
+ offset: rowIndex,
435
+ totalSize: count,
436
+ });
437
+ });
438
+ }
439
+
440
+ interface QueryParams {
441
+ count: "exact" | "estimated";
442
+ limit: number;
443
+ offset?: number;
444
+ after?: number;
445
+ }
446
+
447
+ const testQueryFunc = async (
448
+ cfg: QueryParams,
449
+ ): Promise<{
450
+ data: any[];
451
+ count: number | null;
452
+ }> => {
453
+ console.log(cfg);
454
+ let offset = null;
455
+ if (cfg.after) {
456
+ offset = cfg.after;
457
+ } else if (cfg.offset) {
458
+ offset = cfg.offset;
459
+ }
460
+
461
+ let count = cfg.limit;
462
+ const totalCount = 50000;
463
+ count = Math.min(count, totalCount - offset);
464
+
465
+ // wait for a little bit before returning
466
+ await sleep(100);
467
+
468
+ return {
469
+ data: buildSyntheticData(offset, count),
470
+ count: cfg.count != null ? 50000 : undefined,
471
+ };
472
+ };
473
+
474
+ async function sleep(ms: number) {
475
+ return new Promise((resolve) => setTimeout(resolve, ms));
476
+ }
477
+
478
+ function buildSyntheticData(offset: number, count: number | null) {
479
+ console.log(
480
+ `Getting data at indices between ${offset} and ${offset + count}`,
481
+ );
482
+ return range(offset, offset + count).map((i) => {
483
+ const id = i + 1;
484
+ return {
485
+ id,
486
+ name: `This is some long text content in row ${id}`,
487
+ };
488
+ });
489
+ }
@@ -7,6 +7,7 @@ import { PostgrestOrder, usePostgRESTLazyLoader } from "./data-loaders";
7
7
  import { Spinner, InputGroup } from "@blueprintjs/core";
8
8
 
9
9
  export * from "./data-loaders";
10
+ export * from "./lazy-loader-table";
10
11
  import { useCallback, useState, useRef } from "react";
11
12
  import {
12
13
  ErrorBoundary,
@@ -15,7 +16,6 @@ import {
15
16
  useToaster,
16
17
  } from "@macrostrat/ui-components";
17
18
  import { Spec } from "immutability-helper";
18
- import { DataSheetProviderProps } from "../provider";
19
19
  import { PostgrestFilterBuilder } from "@supabase/postgrest-js";
20
20
  import type {
21
21
  GenericFunction,
@@ -23,6 +23,7 @@ import type {
23
23
  GenericView,
24
24
  } from "@supabase/postgrest-js/dist/cjs/index";
25
25
  import { ColorCell } from "../components";
26
+ import { DataSheetProviderProps } from "../types.ts";
26
27
 
27
28
  const h = hyper.styled(styles);
28
29
 
@@ -0,0 +1,24 @@
1
+ import hyper from "@macrostrat/hyper";
2
+ import styles from "./main.module.sass";
3
+ import { DataSheet } from "../core";
4
+ import { useTestLazyLoader } from "./data-loaders";
5
+ import { Spinner } from "@blueprintjs/core";
6
+
7
+ const h = hyper.styled(styles);
8
+
9
+ export function TestLazyLoaderTableView<T>({ ...rest }) {
10
+ const { data, onScroll } = useTestLazyLoader();
11
+
12
+ if (data == null) {
13
+ return h(Spinner);
14
+ }
15
+
16
+ return h("div.data-sheet-outer", [
17
+ h(DataSheet, {
18
+ ...rest,
19
+ data,
20
+ editable: false,
21
+ onVisibleCellsChange: onScroll,
22
+ }),
23
+ ]);
24
+ }