@macrostrat/data-sheet 4.0.0-dev3 → 4.0.1

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 (212) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/README.md +44 -0
  3. package/dist/actions/builtin.cjs +36 -5
  4. package/dist/actions/builtin.cjs.map +1 -1
  5. package/dist/actions/builtin.d.ts +3 -2
  6. package/dist/actions/builtin.js +36 -5
  7. package/dist/actions/builtin.js.map +1 -1
  8. package/dist/actions/column-controls.cjs +149 -0
  9. package/dist/actions/column-controls.cjs.map +1 -0
  10. package/dist/actions/column-controls.d.ts +11 -0
  11. package/dist/actions/column-controls.js +147 -0
  12. package/dist/actions/column-controls.js.map +1 -0
  13. package/dist/actions/column-filter.cjs +68 -0
  14. package/dist/actions/column-filter.cjs.map +1 -0
  15. package/dist/actions/column-filter.d.ts +11 -0
  16. package/dist/actions/column-filter.js +66 -0
  17. package/dist/actions/column-filter.js.map +1 -0
  18. package/dist/actions/edit-actions.cjs +42 -0
  19. package/dist/actions/edit-actions.cjs.map +1 -0
  20. package/dist/actions/edit-actions.d.ts +14 -0
  21. package/dist/actions/edit-actions.js +42 -0
  22. package/dist/actions/edit-actions.js.map +1 -0
  23. package/dist/actions/filters.cjs +39 -23
  24. package/dist/actions/filters.cjs.map +1 -1
  25. package/dist/actions/filters.d.ts +6 -3
  26. package/dist/actions/filters.js +41 -25
  27. package/dist/actions/filters.js.map +1 -1
  28. package/dist/actions/index.d.ts +4 -0
  29. package/dist/actions/selection.cjs +51 -32
  30. package/dist/actions/selection.cjs.map +1 -1
  31. package/dist/actions/selection.d.ts +3 -7
  32. package/dist/actions/selection.js +51 -32
  33. package/dist/actions/selection.js.map +1 -1
  34. package/dist/actions/table-controls.cjs +41 -0
  35. package/dist/actions/table-controls.cjs.map +1 -0
  36. package/dist/actions/table-controls.d.ts +4 -0
  37. package/dist/actions/table-controls.js +39 -0
  38. package/dist/actions/table-controls.js.map +1 -0
  39. package/dist/actions/toolbar.cjs +69 -10
  40. package/dist/actions/toolbar.cjs.map +1 -1
  41. package/dist/actions/toolbar.d.ts +9 -6
  42. package/dist/actions/toolbar.js +69 -10
  43. package/dist/actions/toolbar.js.map +1 -1
  44. package/dist/actions/toolbar.module.sass.cjs +12 -7
  45. package/dist/actions/toolbar.module.sass.cjs.map +1 -1
  46. package/dist/actions/toolbar.module.sass.js +12 -7
  47. package/dist/actions/toolbar.module.sass.js.map +1 -1
  48. package/dist/actions/types.d.ts +49 -4
  49. package/dist/cell-renderer.cjs +150 -14
  50. package/dist/cell-renderer.cjs.map +1 -1
  51. package/dist/cell-renderer.d.ts +1 -1
  52. package/dist/cell-renderer.js +151 -15
  53. package/dist/cell-renderer.js.map +1 -1
  54. package/dist/components/cell-detail.cjs +24 -0
  55. package/dist/components/cell-detail.cjs.map +1 -0
  56. package/dist/components/cell-detail.d.ts +12 -0
  57. package/dist/components/cell-detail.js +24 -0
  58. package/dist/components/cell-detail.js.map +1 -0
  59. package/dist/components/editor-popup.cjs +28 -24
  60. package/dist/components/editor-popup.cjs.map +1 -1
  61. package/dist/components/editor-popup.js +29 -25
  62. package/dist/components/editor-popup.js.map +1 -1
  63. package/dist/components/index.d.ts +2 -2
  64. package/dist/components/text-areas.cjs +51 -7
  65. package/dist/components/text-areas.cjs.map +1 -1
  66. package/dist/components/text-areas.d.ts +4 -1
  67. package/dist/components/text-areas.js +51 -7
  68. package/dist/components/text-areas.js.map +1 -1
  69. package/dist/components/view-info.cjs +46 -9
  70. package/dist/components/view-info.cjs.map +1 -1
  71. package/dist/components/view-info.d.ts +5 -1
  72. package/dist/components/view-info.js +47 -10
  73. package/dist/components/view-info.js.map +1 -1
  74. package/dist/core.cjs +167 -73
  75. package/dist/core.cjs.map +1 -1
  76. package/dist/core.d.ts +53 -6
  77. package/dist/core.js +169 -75
  78. package/dist/core.js.map +1 -1
  79. package/dist/data-sheet.css +72 -123
  80. package/dist/filters/operators.cjs +91 -0
  81. package/dist/filters/operators.cjs.map +1 -0
  82. package/dist/filters/operators.d.ts +17 -0
  83. package/dist/filters/operators.js +91 -0
  84. package/dist/filters/operators.js.map +1 -0
  85. package/dist/index.cjs +38 -10
  86. package/dist/index.cjs.map +1 -1
  87. package/dist/index.d.ts +2 -0
  88. package/dist/index.js +44 -16
  89. package/dist/index.js.map +1 -1
  90. package/dist/main.module.sass.cjs +13 -12
  91. package/dist/main.module.sass.cjs.map +1 -1
  92. package/dist/main.module.sass.js +13 -12
  93. package/dist/main.module.sass.js.map +1 -1
  94. package/dist/postgrest-table/data-loaders.cjs +255 -146
  95. package/dist/postgrest-table/data-loaders.cjs.map +1 -1
  96. package/dist/postgrest-table/data-loaders.d.ts +144 -46
  97. package/dist/postgrest-table/data-loaders.js +259 -150
  98. package/dist/postgrest-table/data-loaders.js.map +1 -1
  99. package/dist/postgrest-table/index.cjs +58 -169
  100. package/dist/postgrest-table/index.cjs.map +1 -1
  101. package/dist/postgrest-table/index.d.ts +5 -2
  102. package/dist/postgrest-table/index.js +63 -174
  103. package/dist/postgrest-table/index.js.map +1 -1
  104. package/dist/postgrest-table/loading-utils.cjs +0 -4
  105. package/dist/postgrest-table/loading-utils.cjs.map +1 -1
  106. package/dist/postgrest-table/loading-utils.js +1 -5
  107. package/dist/postgrest-table/loading-utils.js.map +1 -1
  108. package/dist/postgrest-table/main.module.sass.cjs +14 -13
  109. package/dist/postgrest-table/main.module.sass.cjs.map +1 -1
  110. package/dist/postgrest-table/main.module.sass.js +14 -13
  111. package/dist/postgrest-table/main.module.sass.js.map +1 -1
  112. package/dist/provider.cjs +68 -11
  113. package/dist/provider.cjs.map +1 -1
  114. package/dist/provider.d.ts +25 -0
  115. package/dist/provider.js +68 -11
  116. package/dist/provider.js.map +1 -1
  117. package/dist/renderers/column-header-cell.cjs +95 -75
  118. package/dist/renderers/column-header-cell.cjs.map +1 -1
  119. package/dist/renderers/column-header-cell.d.ts +3 -18
  120. package/dist/renderers/column-header-cell.js +98 -78
  121. package/dist/renderers/column-header-cell.js.map +1 -1
  122. package/dist/renderers/index.d.ts +0 -1
  123. package/dist/types.cjs +2 -0
  124. package/dist/types.cjs.map +1 -1
  125. package/dist/types.d.ts +84 -1
  126. package/dist/types.js +2 -0
  127. package/dist/types.js.map +1 -1
  128. package/dist/utils/column-spec.cjs.map +1 -1
  129. package/dist/utils/column-spec.d.ts +101 -1
  130. package/dist/utils/column-spec.js.map +1 -1
  131. package/dist/utils/debug-overlay.cjs +24 -0
  132. package/dist/utils/debug-overlay.cjs.map +1 -0
  133. package/dist/utils/debug-overlay.d.ts +4 -0
  134. package/dist/utils/debug-overlay.js +24 -0
  135. package/dist/utils/debug-overlay.js.map +1 -0
  136. package/dist/utils/index.cjs.map +1 -1
  137. package/dist/utils/index.d.ts +2 -0
  138. package/dist/utils/index.js.map +1 -1
  139. package/dist/utils/key-handling.cjs +52 -6
  140. package/dist/utils/key-handling.cjs.map +1 -1
  141. package/dist/utils/key-handling.js +52 -6
  142. package/dist/utils/key-handling.js.map +1 -1
  143. package/dist/utils/validation.cjs +41 -0
  144. package/dist/utils/validation.cjs.map +1 -0
  145. package/dist/utils/validation.d.ts +26 -0
  146. package/dist/utils/validation.js +41 -0
  147. package/dist/utils/validation.js.map +1 -0
  148. package/dist/zustand-store.cjs +119 -61
  149. package/dist/zustand-store.cjs.map +1 -1
  150. package/dist/zustand-store.js +121 -63
  151. package/dist/zustand-store.js.map +1 -1
  152. package/package.json +3 -3
  153. package/src/actions/builtin.ts +62 -7
  154. package/src/actions/column-controls.ts +185 -0
  155. package/src/actions/column-filter.ts +104 -0
  156. package/src/actions/edit-actions.ts +67 -0
  157. package/src/actions/filters.ts +54 -28
  158. package/src/actions/index.ts +4 -0
  159. package/src/actions/selection.ts +73 -54
  160. package/src/actions/table-controls.ts +46 -0
  161. package/src/actions/toolbar.module.sass +22 -2
  162. package/src/actions/toolbar.ts +113 -16
  163. package/src/actions/types.ts +49 -4
  164. package/src/cell-renderer.ts +211 -16
  165. package/src/components/cell-detail.ts +30 -0
  166. package/src/components/editor-popup.ts +39 -25
  167. package/src/components/index.ts +2 -2
  168. package/src/components/text-areas.ts +80 -16
  169. package/src/components/view-info.ts +63 -4
  170. package/src/core.ts +321 -103
  171. package/src/filters/operators.ts +127 -0
  172. package/src/index.ts +2 -0
  173. package/src/main.module.sass +6 -0
  174. package/src/postgrest-table/data-loaders.ts +459 -209
  175. package/src/postgrest-table/index.ts +81 -254
  176. package/src/postgrest-table/main.module.sass +11 -0
  177. package/src/provider.ts +109 -12
  178. package/src/renderers/column-header-cell.ts +60 -272
  179. package/src/renderers/index.ts +0 -1
  180. package/src/types.ts +76 -2
  181. package/src/utils/column-spec.ts +112 -1
  182. package/src/utils/debug-overlay.ts +22 -0
  183. package/src/utils/index.ts +2 -0
  184. package/src/utils/key-handling.ts +70 -6
  185. package/src/utils/validation.ts +72 -0
  186. package/src/zustand-store.ts +167 -60
  187. package/dist/components/actions.cjs +0 -12
  188. package/dist/components/actions.cjs.map +0 -1
  189. package/dist/components/actions.d.ts +0 -2
  190. package/dist/components/actions.js +0 -12
  191. package/dist/components/actions.js.map +0 -1
  192. package/dist/components/actions.module.sass.cjs +0 -12
  193. package/dist/components/actions.module.sass.cjs.map +0 -1
  194. package/dist/components/actions.module.sass.js +0 -11
  195. package/dist/components/actions.module.sass.js.map +0 -1
  196. package/dist/postgrest-table/lazy-loader-table.cjs +0 -27
  197. package/dist/postgrest-table/lazy-loader-table.cjs.map +0 -1
  198. package/dist/postgrest-table/lazy-loader-table.d.ts +0 -3
  199. package/dist/postgrest-table/lazy-loader-table.js +0 -27
  200. package/dist/postgrest-table/lazy-loader-table.js.map +0 -1
  201. package/dist/renderers/column-header-cell-postgrest.cjs +0 -216
  202. package/dist/renderers/column-header-cell-postgrest.cjs.map +0 -1
  203. package/dist/renderers/column-header-cell-postgrest.d.ts +0 -13
  204. package/dist/renderers/column-header-cell-postgrest.js +0 -216
  205. package/dist/renderers/column-header-cell-postgrest.js.map +0 -1
  206. package/dist/renderers/column-header-cell-postgrest.module.sass.cjs +0 -23
  207. package/dist/renderers/column-header-cell-postgrest.module.sass.cjs.map +0 -1
  208. package/dist/renderers/column-header-cell-postgrest.module.sass.js +0 -22
  209. package/dist/renderers/column-header-cell-postgrest.module.sass.js.map +0 -1
  210. package/src/components/actions.ts +0 -9
  211. package/src/postgrest-table/lazy-loader-table.ts +0 -25
  212. package/src/renderers/column-header-cell-postgrest.ts +0 -282
@@ -1,44 +1,34 @@
1
- import {
2
- Button,
3
- InputGroup,
4
- OverlayToaster,
5
- Tag as BPTag,
6
- } from "@blueprintjs/core";
1
+ import { InputGroup, OverlayToaster } from "@blueprintjs/core";
7
2
  import h from "./main.module.sass";
8
- import { DataSheet, getRowsToDelete } from "../core";
3
+ import { DataSheet } from "../core";
9
4
  import { LithologyTag, Tag, TagSize } from "@macrostrat/data-components";
10
5
  import {
11
- ColumnSortEntry,
12
- PostgrestColumnFilter,
6
+ createPostgRESTProvider,
7
+ dataRefreshTokenAtom,
13
8
  PostgrestFilter,
14
- PostgrestFilterOperator,
15
9
  PostgrestOrder,
16
- standardizeFilter,
17
- usePostgRESTLazyLoader,
18
10
  } from "./data-loaders";
19
- import { useCallback, useMemo, useState } from "react";
20
- import {
21
- ErrorBoundary,
22
- ToasterContext,
23
- useToaster,
24
- } from "@macrostrat/ui-components";
25
- import { Spec } from "immutability-helper";
11
+ import { useCallback, useEffect, useMemo, useRef } from "react";
12
+ import { ErrorBoundary, ToasterContext } from "@macrostrat/ui-components";
26
13
  import { PostgrestFilterBuilder } from "@supabase/postgrest-js";
27
- import { ColorCell, InfoBar } from "../components";
14
+ import { ColorCell } from "../components";
28
15
  import { DataSheetProviderProps } from "../types.ts";
29
- import {
30
- type ColumnHeaderActions,
31
- ColumnHeaderRendererProps,
32
- OPERATOR_LABELS,
33
- renderPostgRESTColumnHeaderCell,
34
- } from "../renderers";
35
16
  import { atom } from "jotai";
36
17
  import { columnSpecAtom, ctx } from "../provider.ts";
37
- import { TableAction } from "../actions";
38
- import { RegionCardinality } from "@blueprintjs/table";
18
+ import { ALL_CARDINALITIES, TableAction } from "../actions";
39
19
 
40
20
  export * from "./data-loaders";
41
- export * from "./lazy-loader-table";
21
+
22
+ /** Full-text search across the table's text columns, expressed as a
23
+ * table-scoped action (rendered in the standard toolbar, not bespoke chrome). */
24
+ export const fullTextSearchAction: TableAction = {
25
+ id: "full-text-search",
26
+ name: "Search",
27
+ icon: "search",
28
+ targets: ALL_CARDINALITIES,
29
+ requiresEditable: false,
30
+ render: () => h(SearchAction),
31
+ };
42
32
 
43
33
  interface PostgRESTTableViewProps<
44
34
  T extends object,
@@ -51,7 +41,7 @@ interface PostgRESTTableViewProps<
51
41
  editable?: boolean;
52
42
  identityKey?: string;
53
43
  enableFullTableSearch?: boolean;
54
- dataSheetActions?: any;
44
+ actions?: TableAction<T>[];
55
45
  filter(
56
46
  query: PostgrestFilterBuilder<T, any, any, any>,
57
47
  ): PostgrestFilterBuilder<T, any, any, any>;
@@ -113,35 +103,26 @@ function _PostgRESTTableView<T>({
113
103
  columns,
114
104
  editable = false,
115
105
  enableFullTableSearch = false,
116
- dataSheetActions,
106
+ actions,
117
107
  identityKey = null,
108
+ filter: userFilter,
118
109
  ...rest
119
110
  }: PostgRESTTableViewProps<T>) {
120
111
  // Boundary of Jotai store
121
112
  ctx.useSync(enableFullTextSearchAtom, enableFullTableSearch);
122
113
 
123
- // Server-side column sort/filter state
124
- const [columnSorts, setColumnSorts] = useState<ColumnSortEntry[]>([]);
125
- const [columnFilters, setColumnFilters] = useState<PostgrestColumnFilter[]>(
126
- [],
127
- );
128
-
129
- /** TODO: remove "full-text search" from filter options */
130
- const ftsFilter = buildFilter(ctx.useValue(fullTextSearchFilterConfigAtom));
131
- let filters = columnFilters.map(standardizeFilter);
132
- if (ftsFilter != null) {
133
- filters.push(ftsFilter);
134
- }
114
+ const ftsConfig = ctx.useValue(fullTextSearchFilterConfigAtom);
135
115
 
136
- const _order = [];
116
+ // Base ordering from the `order` prop (active column sorts are added by the
117
+ // fetchChunk from the shared store, per-column identity appended last).
118
+ const _order: PostgrestOrder<any>[] = [];
137
119
  if (typeof order === "object" && "key" in order) {
138
- _order.push(order);
120
+ _order.push(order as PostgrestOrder<any>);
139
121
  } else if (Array.isArray(order)) {
140
122
  _order.push(...order);
141
123
  }
142
- _order.push(...columnSorts);
143
124
 
144
- // Infer identity key
125
+ // Infer identity key from the order prop when not given explicitly.
145
126
  let _identityKey = identityKey;
146
127
  if (_identityKey == null) {
147
128
  if (_order.length == 0) {
@@ -151,168 +132,66 @@ function _PostgRESTTableView<T>({
151
132
  }
152
133
  }
153
134
 
154
- const { data, dispatch, getClient } = usePostgRESTLazyLoader(
155
- endpoint,
156
- table,
157
- {
158
- order: _order,
159
- columns,
160
- filters,
161
- identityKey: _identityKey,
162
- },
163
- );
164
-
165
- const toaster = useToaster();
166
-
167
- const finishResponse = useCallback(
168
- (promisedResult, changes) => {
169
- wrapWithErrorHandling(toaster, promisedResult).then((res) => {
170
- if (res == null) {
171
- throw new Error("Could not complete action");
172
- } else {
173
- // Merge new data with old data
174
- console.log("Updating data", changes);
175
- dispatch({ type: "update-data", changes });
176
- }
177
- });
178
- },
179
- [dispatch, toaster],
180
- );
181
-
182
- // Column header actions for sort/filter
183
- const columnHeaderActions: ColumnHeaderActions = useMemo(
184
- () => ({
185
- onSetSort(key: string, ascending: boolean | null) {
186
- // Single-column sort: replace any existing sort
187
- if (ascending == null) {
188
- setColumnSorts([]);
189
- } else {
190
- setColumnSorts([{ key, ascending }]);
191
- }
192
- },
193
- onSetFilter(
194
- key: string,
195
- operator: PostgrestFilterOperator | null,
196
- value: string,
197
- ) {
198
- setColumnFilters((prev) => {
199
- const without = prev.filter((f) => f.key !== key);
200
- if (operator == null || value === "") return without;
201
- return [...without, { key, operator, value }];
202
- });
203
- },
204
- onClearColumn(key: string) {
205
- setColumnSorts((prev) => prev.filter((s) => s.key !== key));
206
- setColumnFilters((prev) => prev.filter((f) => f.key !== key));
207
- },
208
- }),
209
- [],
210
- );
211
-
212
- // Column header cell renderer using sort/filter state
213
- const columnHeaderCellRenderer = useCallback(
214
- (props: ColumnHeaderRendererProps) => {
215
- const { col, colIndex } = props;
216
- const activeSort = columnSorts.find((s) => s.key === col.key);
217
- const activeFilter = columnFilters.find((f) => f.key === col.key);
218
- return renderPostgRESTColumnHeaderCell({
219
- col,
220
- colIndex,
221
- activeSort,
222
- activeFilter,
223
- actions: columnHeaderActions,
224
- });
135
+ const bumpRefresh = ctx.useSet(dataRefreshTokenAtom);
136
+
137
+ // Base query transform: the `filter` prop + full-text search. Column filters
138
+ // come from the shared store (as operator `columnFilter`s) via the loader's
139
+ // `params.filters`, so they aren't applied here.
140
+ const baseFilter = useCallback(
141
+ (q: PostgrestFilterBuilder<any, any, any, any>) => {
142
+ let query = q;
143
+ if (userFilter != null) query = userFilter(query as any) as any;
144
+ const ff = buildFilter(ftsConfig);
145
+ if (ff != null) query = ff.apply(query);
146
+ return query;
225
147
  },
226
- [columnSorts, columnFilters, columnHeaderActions],
148
+ [userFilter, ftsConfig],
227
149
  );
228
150
 
229
- const hasActiveFilters = columnFilters.length > 0;
230
- const hasActiveSort = columnSorts.length > 0;
231
-
232
- return h(
233
- DataSheet,
234
- {
235
- ...rest,
236
- dataSheetActions: enableFullTableSearch
237
- ? h(SearchAction)
238
- : dataSheetActions,
239
- columnSpecOptions: columnOptions,
240
- editable,
241
- columnHeaderCellRenderer,
242
- onDeleteRows(selection) {
243
- if (!editable) return;
244
-
245
- const rowIndices = getRowsToDelete(selection);
246
-
247
- const ids = rowIndices.map((i) => data[i][identityKey]);
248
-
249
- dispatch({ type: "start-loading" });
250
-
251
- const client = getClient();
252
- let query = client.delete().in(identityKey, ids);
253
- finishResponse(query, { $delete: Array.from(rowIndices.keys()) });
254
- },
255
- onSaveData(updates, data) {
256
- if (!editable) return;
257
-
258
- // Augment updates with primary key
259
-
260
- let changes: Spec<any[]> = {};
261
- let updateRows: any[] = [];
262
- for (let [key, update] of Object.entries(updates)) {
263
- const value = { ...data[key], ...update };
264
- updateRows.push(value);
265
- changes[key] = { $set: value };
266
- }
267
-
268
- dispatch({ type: "start-loading" });
269
- const client = getClient();
270
- // Save data
271
- let query = client.upsert(updateRows, { defaultToNull: false });
272
- finishResponse(query, changes);
273
- },
274
- },
275
- [
276
- h.if(hasActiveFilters || hasActiveSort)(ServerFilterBar, {
277
- columnSorts,
278
- columnFilters,
279
- onClearFilter(key: string) {
280
- columnHeaderActions.onClearColumn(key);
281
- },
282
- onClearAll() {
283
- setColumnSorts([]);
284
- setColumnFilters([]);
285
- },
151
+ // The whole read+persist contract as one `TableDataProvider`, passed to the
152
+ // DataSheet `provider` prop: `fetchData` (keyset-paginated, threading store
153
+ // sorts/filters), `identity`, and `saveRows`/`deleteRows` (upsert/delete).
154
+ // Save (batch) and delete both flow through it — no `onSave`/`onDeleteRows`.
155
+ const orderKey = JSON.stringify(_order);
156
+ const provider = useMemo(
157
+ () =>
158
+ createPostgRESTProvider({
159
+ endpoint,
160
+ table,
161
+ columns,
162
+ identityKey: _identityKey!,
163
+ baseOrder: _order,
164
+ baseFilter,
286
165
  }),
287
- ],
166
+ // eslint-disable-next-line react-hooks/exhaustive-deps
167
+ [endpoint, table, columns, _identityKey, orderKey, baseFilter],
288
168
  );
289
- }
290
169
 
291
- const saveDataAction: TableAction = {
292
- id: "save-data",
293
- name: "Save changes",
294
- icon: "floppy-disk",
295
- intent: "success",
296
- requiresEditable: true,
297
- targets: [RegionCardinality.FULL_TABLE],
298
- async run(ctx) {
299
- const data = ctx.data;
300
- const updates = ctx.updatedData;
301
-
302
- let changes: Spec<any[]> = {};
303
- let updateRows: any[] = [];
304
- for (let [key, update] of Object.entries(updates)) {
305
- const value = { ...data[key], ...update };
306
- updateRows.push(value);
307
- changes[key] = { $set: value };
170
+ // Full-text search re-runs the query; the loader re-fetches on store
171
+ // sort/filter changes on its own, so only FTS needs a nudge here.
172
+ const firstRun = useRef(true);
173
+ useEffect(() => {
174
+ if (firstRun.current) {
175
+ firstRun.current = false;
176
+ return;
308
177
  }
309
-
310
- dispatch({ type: "start-loading" });
311
- const client = getClient();
312
- // Save data
313
- let query = client.upsert(updateRows, { defaultToNull: false });
314
- },
315
- };
178
+ bumpRefresh((v) => v + 1);
179
+ // eslint-disable-next-line react-hooks/exhaustive-deps
180
+ }, [ftsConfig]);
181
+
182
+ // Full-text search joins the standard actions toolbar (no bespoke chrome).
183
+ const mergedActions = enableFullTableSearch
184
+ ? [fullTextSearchAction, ...(actions ?? [])]
185
+ : actions;
186
+
187
+ return h(DataSheet, {
188
+ ...rest,
189
+ provider,
190
+ actions: mergedActions,
191
+ columnSpecOptions: columnOptions,
192
+ editable,
193
+ });
194
+ }
316
195
 
317
196
  export function notifyOnError(toaster: OverlayToaster, error: any) {
318
197
  console.error(error);
@@ -416,55 +295,3 @@ export function SearchAction() {
416
295
  },
417
296
  });
418
297
  }
419
-
420
- /** Bar showing active server-side sort and filter state as removable tags. */
421
- function ServerFilterBar({
422
- columnSorts,
423
- columnFilters,
424
- onClearFilter,
425
- onClearAll,
426
- }: {
427
- columnSorts: ColumnSortEntry[];
428
- columnFilters: PostgrestColumnFilter[];
429
- onClearFilter: (key: string) => void;
430
- onClearAll: () => void;
431
- }) {
432
- return h("div.server-filter-bar", [
433
- columnSorts.map((s) =>
434
- h(
435
- BPTag,
436
- {
437
- key: `sort-${s.key}`,
438
- icon: s.ascending ? "sort-asc" : "sort-desc",
439
- intent: "primary",
440
- onRemove: () => onClearFilter(s.key),
441
- minimal: true,
442
- },
443
- `${s.key}: ${s.ascending ? "A→Z" : "Z→A"}`,
444
- ),
445
- ),
446
- columnFilters.map((f) =>
447
- h(
448
- BPTag,
449
- {
450
- key: `filter-${f.key}`,
451
- icon: "filter",
452
- intent: "warning",
453
- onRemove: () => onClearFilter(f.key),
454
- minimal: true,
455
- },
456
- `${f.key} ${OPERATOR_LABELS[f.operator] ?? f.operator} ${f.value}`,
457
- ),
458
- ),
459
- h(
460
- Button,
461
- {
462
- minimal: true,
463
- small: true,
464
- icon: "cross",
465
- onClick: onClearAll,
466
- },
467
- "Clear all",
468
- ),
469
- ]);
470
- }
@@ -65,6 +65,17 @@
65
65
  @extend .toolbar
66
66
  order: 10
67
67
 
68
+ .load-progress
69
+ order: 11
70
+ display: flex
71
+ flex-direction: row
72
+ align-items: center
73
+ gap: 0.4em
74
+ padding: 2px 4px
75
+ font-size: 11px
76
+ opacity: 0.75
77
+ font-variant-numeric: tabular-nums
78
+
68
79
  .tag-cell-content
69
80
  line-height: 1
70
81
 
package/src/provider.ts CHANGED
@@ -8,6 +8,8 @@ import {
8
8
  DataSheetProviderProps,
9
9
  DataSheetState,
10
10
  DataSheetStore,
11
+ DS_ROW_ID,
12
+ TableElementStatus,
11
13
  } from "./types.ts";
12
14
  import { createZustandStore } from "./zustand-store.ts";
13
15
  import { atomWithStore } from "jotai-zustand";
@@ -129,25 +131,120 @@ export const tableDataAtom = atom(
129
131
  },
130
132
  (get, set, newData: any[]) => {
131
133
  set(storeAtom, (state: DataSheetState<any>): DataSheetState<any> => {
132
- console.log("Updating table data", newData);
134
+ let next: DataSheetState<any> = { ...state, data: newData };
133
135
  if (
134
136
  state.data.length == 0 &&
135
137
  newData.length > 0 &&
136
138
  state.columnSpec.length == 0
137
139
  ) {
138
- console.log("Generating column spec from data");
139
- /** We haven't yet generated the column spec, and we need to do so. TODO: we may be able to forestall this with loading state */
140
- return {
141
- ...state,
142
- columnSpec: generateColumnSpec(newData, state.columnSpecOptions),
143
- data: newData,
144
- };
140
+ // We haven't yet generated the column spec, and we need to do so.
141
+ next.columnSpec = generateColumnSpec(newData, state.columnSpecOptions);
145
142
  }
146
143
 
147
- return {
148
- ...state,
149
- data: newData,
150
- };
144
+ // Re-attach the edit overlay by identity. The loader replaces `data` with
145
+ // re-ordered windows on sort/filter, so an index-keyed overlay would
146
+ // smear; keying by identity keeps edits (and deletes) on their rows, holds
147
+ // edits for not-yet-loaded rows, and re-appends locally-added rows (which
148
+ // aren't in the provider's data) after it. Skipped when the consumer
149
+ // controls the overlay (they own reconciliation).
150
+ if (!state.controlledOverlay) {
151
+ const remap = remapOverlayByIdentity(state, newData);
152
+ next.data = remap.data;
153
+ next.updatedData = remap.updatedData;
154
+ next.rowStatus = remap.rowStatus;
155
+ next.pendingOverlayById = remap.pending;
156
+ }
157
+
158
+ return next;
151
159
  });
152
160
  },
153
161
  );
162
+
163
+ type OverlayEntry = { edit?: any; status?: any };
164
+
165
+ /** Row identity used by the overlay: an in-table-added row's synthetic id
166
+ * (stable across its edits), else the provider/consumer `identity`. */
167
+ function overlayIdentity(
168
+ state: DataSheetState<any>,
169
+ row: any,
170
+ ): string | number | null | undefined {
171
+ if (row == null) return undefined;
172
+ const added = row[DS_ROW_ID];
173
+ if (added != null) return added;
174
+ return state.identity(row);
175
+ }
176
+
177
+ /** Re-project the (index-keyed) edit overlay from the old `data` order onto a
178
+ * new one, matching rows by identity. Edits/statuses whose row is absent from
179
+ * `newData` are held in `pending` and re-applied when the row loads; rows added
180
+ * in-table (status `ADDED`) are re-appended after the provider data (they have
181
+ * no provider row). Starts from the prior pending set so edits survive multiple
182
+ * re-sorts. Returns the reconstructed data (provider rows + re-appended added
183
+ * rows) alongside the re-aligned overlay. */
184
+ function remapOverlayByIdentity(
185
+ state: DataSheetState<any>,
186
+ newData: any[],
187
+ ): {
188
+ data: any[];
189
+ updatedData: any[];
190
+ rowStatus: any[];
191
+ pending: Map<string | number, OverlayEntry>;
192
+ } {
193
+ const { data: oldData, updatedData, rowStatus } = state;
194
+
195
+ // Collect current overlay entries by identity, seeded with prior pending.
196
+ const byId = new Map<string | number, OverlayEntry>(state.pendingOverlayById);
197
+ const n = Math.max(
198
+ oldData.length,
199
+ updatedData?.length ?? 0,
200
+ rowStatus?.length ?? 0,
201
+ );
202
+ for (let i = 0; i < n; i++) {
203
+ const row = oldData[i];
204
+ if (row == null) continue;
205
+ const id = overlayIdentity(state, row);
206
+ if (id == null) continue;
207
+ const edit = updatedData?.[i];
208
+ const status = rowStatus?.[i];
209
+ if (edit != null || status != null) byId.set(id, { edit, status });
210
+ }
211
+
212
+ // Project onto the new data order.
213
+ const resultData = newData.slice();
214
+ const nextUpdated: any[] = [];
215
+ const nextStatus: any[] = [];
216
+ const attached = new Set<string | number>();
217
+ for (let j = 0; j < resultData.length; j++) {
218
+ const row = resultData[j];
219
+ if (row == null) continue;
220
+ const id = overlayIdentity(state, row);
221
+ if (id == null) continue;
222
+ const entry = byId.get(id);
223
+ if (entry == null) continue;
224
+ if (entry.edit != null) nextUpdated[j] = entry.edit;
225
+ if (entry.status != null) nextStatus[j] = entry.status;
226
+ attached.add(id);
227
+ }
228
+
229
+ // Re-append in-table-added rows (not present in the provider data), then hold
230
+ // anything still unattached (rows not currently loaded) as pending.
231
+ const pending = new Map<string | number, OverlayEntry>();
232
+ for (const [id, entry] of byId) {
233
+ if (attached.has(id)) continue;
234
+ if (entry.status === TableElementStatus.ADDED) {
235
+ const idx = resultData.length;
236
+ resultData.push(entry.edit ?? {});
237
+ nextUpdated[idx] = entry.edit;
238
+ nextStatus[idx] = entry.status;
239
+ } else {
240
+ pending.set(id, entry);
241
+ }
242
+ }
243
+
244
+ return {
245
+ data: resultData,
246
+ updatedData: nextUpdated,
247
+ rowStatus: nextStatus,
248
+ pending,
249
+ };
250
+ }