@macrostrat/data-sheet 4.1.1 → 4.2.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 (303) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/dist/actions/builtin.cjs +1 -1
  3. package/dist/actions/builtin.cjs.map +1 -1
  4. package/dist/actions/builtin.d.ts +0 -2
  5. package/dist/actions/builtin.js +1 -1
  6. package/dist/actions/builtin.js.map +1 -1
  7. package/dist/actions/clipboard.cjs +1 -1
  8. package/dist/actions/clipboard.cjs.map +1 -1
  9. package/dist/actions/clipboard.d.ts +3 -2
  10. package/dist/actions/clipboard.js +1 -1
  11. package/dist/actions/clipboard.js.map +1 -1
  12. package/dist/actions/column-controls.cjs +7 -196
  13. package/dist/actions/column-controls.cjs.map +1 -1
  14. package/dist/actions/column-controls.d.ts +1 -1
  15. package/dist/actions/column-controls.js +5 -194
  16. package/dist/actions/column-controls.js.map +1 -1
  17. package/dist/actions/column-filter.cjs +24 -73
  18. package/dist/actions/column-filter.cjs.map +1 -1
  19. package/dist/actions/column-filter.d.ts +6 -4
  20. package/dist/actions/column-filter.js +25 -74
  21. package/dist/actions/column-filter.js.map +1 -1
  22. package/dist/actions/context.cjs +147 -0
  23. package/dist/actions/context.cjs.map +1 -0
  24. package/dist/actions/context.d.ts +93 -0
  25. package/dist/actions/context.js +145 -0
  26. package/dist/actions/context.js.map +1 -0
  27. package/dist/actions/edit-actions.cjs.map +1 -1
  28. package/dist/actions/edit-actions.d.ts +3 -1
  29. package/dist/actions/edit-actions.js.map +1 -1
  30. package/dist/actions/index.d.ts +2 -3
  31. package/dist/actions/selection.cjs +70 -115
  32. package/dist/actions/selection.cjs.map +1 -1
  33. package/dist/actions/selection.d.ts +22 -12
  34. package/dist/actions/selection.js +71 -114
  35. package/dist/actions/selection.js.map +1 -1
  36. package/dist/actions/table-actions.cjs +41 -0
  37. package/dist/actions/table-actions.cjs.map +1 -0
  38. package/dist/actions/{table-controls.d.ts → table-actions.d.ts} +0 -4
  39. package/dist/actions/{table-controls.js → table-actions.js} +2 -23
  40. package/dist/actions/table-actions.js.map +1 -0
  41. package/dist/actions/types.d.ts +11 -89
  42. package/dist/components/cell-detail.cjs +6 -6
  43. package/dist/components/cell-detail.cjs.map +1 -1
  44. package/dist/components/cell-detail.js +1 -1
  45. package/dist/components/colors.cjs.map +1 -1
  46. package/dist/components/colors.js.map +1 -1
  47. package/dist/components/editor-popup.cjs +7 -7
  48. package/dist/components/editor-popup.cjs.map +1 -1
  49. package/dist/components/editor-popup.js +1 -1
  50. package/dist/components/index.d.ts +2 -1
  51. package/dist/components/indicators/filter-and-sort.cjs +378 -0
  52. package/dist/components/indicators/filter-and-sort.cjs.map +1 -0
  53. package/dist/components/indicators/filter-and-sort.d.ts +59 -0
  54. package/dist/components/indicators/filter-and-sort.js +376 -0
  55. package/dist/components/indicators/filter-and-sort.js.map +1 -0
  56. package/dist/components/indicators/filter-and-sort.module.sass.cjs +16 -0
  57. package/dist/components/indicators/filter-and-sort.module.sass.cjs.map +1 -0
  58. package/dist/components/indicators/filter-and-sort.module.sass.js +14 -0
  59. package/dist/components/indicators/filter-and-sort.module.sass.js.map +1 -0
  60. package/dist/components/indicators/index.d.ts +4 -0
  61. package/dist/components/indicators/selection.cjs +113 -0
  62. package/dist/components/indicators/selection.cjs.map +1 -0
  63. package/dist/components/indicators/selection.d.ts +3 -0
  64. package/dist/components/indicators/selection.js +111 -0
  65. package/dist/components/indicators/selection.js.map +1 -0
  66. package/dist/components/indicators/selection.module.sass.cjs +19 -0
  67. package/dist/components/indicators/selection.module.sass.cjs.map +1 -0
  68. package/dist/components/indicators/selection.module.sass.js +17 -0
  69. package/dist/components/indicators/selection.module.sass.js.map +1 -0
  70. package/dist/components/indicators/unified-filters.cjs +130 -0
  71. package/dist/components/indicators/unified-filters.cjs.map +1 -0
  72. package/dist/components/indicators/unified-filters.d.ts +10 -0
  73. package/dist/components/indicators/unified-filters.js +130 -0
  74. package/dist/components/indicators/unified-filters.js.map +1 -0
  75. package/dist/components/indicators/view-info.cjs +59 -0
  76. package/dist/components/indicators/view-info.cjs.map +1 -0
  77. package/dist/components/{view-info.d.ts → indicators/view-info.d.ts} +1 -0
  78. package/dist/components/{view-info.js → indicators/view-info.js} +17 -13
  79. package/dist/components/indicators/view-info.js.map +1 -0
  80. package/dist/components/text-areas.cjs +5 -5
  81. package/dist/components/text-areas.cjs.map +1 -1
  82. package/dist/components/text-areas.js +1 -1
  83. package/dist/components/toolbars/actions-toolbar.cjs +185 -0
  84. package/dist/components/toolbars/actions-toolbar.cjs.map +1 -0
  85. package/dist/{actions/toolbar.d.ts → components/toolbars/actions-toolbar.d.ts} +9 -5
  86. package/dist/{actions/toolbar.js → components/toolbars/actions-toolbar.js} +37 -112
  87. package/dist/components/toolbars/actions-toolbar.js.map +1 -0
  88. package/dist/components/toolbars/actions-toolbar.module.sass.cjs +29 -0
  89. package/dist/components/toolbars/actions-toolbar.module.sass.cjs.map +1 -0
  90. package/dist/components/toolbars/actions-toolbar.module.sass.js +27 -0
  91. package/dist/components/toolbars/actions-toolbar.module.sass.js.map +1 -0
  92. package/dist/components/toolbars/active-filters.cjs +120 -0
  93. package/dist/components/toolbars/active-filters.cjs.map +1 -0
  94. package/dist/{actions/filters.d.ts → components/toolbars/active-filters.d.ts} +3 -3
  95. package/dist/components/toolbars/active-filters.js +120 -0
  96. package/dist/components/toolbars/active-filters.js.map +1 -0
  97. package/dist/components/toolbars/index.d.ts +2 -0
  98. package/dist/data-panel.cjs +291 -0
  99. package/dist/data-panel.cjs.map +1 -0
  100. package/dist/data-panel.d.ts +110 -0
  101. package/dist/data-panel.js +289 -0
  102. package/dist/data-panel.js.map +1 -0
  103. package/dist/data-panel.module.sass.cjs +40 -0
  104. package/dist/data-panel.module.sass.cjs.map +1 -0
  105. package/dist/data-panel.module.sass.js +38 -0
  106. package/dist/data-panel.module.sass.js.map +1 -0
  107. package/dist/{core.cjs → data-sheet.cjs} +123 -190
  108. package/dist/data-sheet.cjs.map +1 -0
  109. package/dist/data-sheet.css +241 -13
  110. package/dist/data-sheet.d.ts +16 -0
  111. package/dist/{core.js → data-sheet.js} +92 -159
  112. package/dist/data-sheet.js.map +1 -0
  113. package/dist/data-view.cjs +47 -0
  114. package/dist/data-view.cjs.map +1 -0
  115. package/dist/data-view.d.ts +15 -0
  116. package/dist/data-view.js +45 -0
  117. package/dist/data-view.js.map +1 -0
  118. package/dist/filters/operators.cjs +19 -2
  119. package/dist/filters/operators.cjs.map +1 -1
  120. package/dist/filters/operators.d.ts +4 -3
  121. package/dist/filters/operators.js +19 -2
  122. package/dist/filters/operators.js.map +1 -1
  123. package/dist/index.cjs +106 -47
  124. package/dist/index.cjs.map +1 -1
  125. package/dist/index.d.ts +4 -3
  126. package/dist/index.js +76 -17
  127. package/dist/index.js.map +1 -1
  128. package/dist/notifications.cjs +4 -4
  129. package/dist/notifications.cjs.map +1 -1
  130. package/dist/notifications.js +1 -1
  131. package/dist/postgrest-table/data-loaders.cjs +128 -95
  132. package/dist/postgrest-table/data-loaders.cjs.map +1 -1
  133. package/dist/postgrest-table/data-loaders.d.ts +52 -82
  134. package/dist/postgrest-table/data-loaders.js +109 -76
  135. package/dist/postgrest-table/data-loaders.js.map +1 -1
  136. package/dist/postgrest-table/index.cjs +18 -17
  137. package/dist/postgrest-table/index.cjs.map +1 -1
  138. package/dist/postgrest-table/index.d.ts +4 -3
  139. package/dist/postgrest-table/index.js +9 -8
  140. package/dist/postgrest-table/index.js.map +1 -1
  141. package/dist/{utils → provider}/column-spec.cjs +23 -9
  142. package/dist/provider/column-spec.cjs.map +1 -0
  143. package/dist/{utils → provider}/column-spec.d.ts +73 -2
  144. package/dist/{utils → provider}/column-spec.js +24 -10
  145. package/dist/provider/column-spec.js.map +1 -0
  146. package/dist/provider/core.cjs +382 -0
  147. package/dist/provider/core.cjs.map +1 -0
  148. package/dist/provider/core.d.ts +74 -0
  149. package/dist/provider/core.js +380 -0
  150. package/dist/provider/core.js.map +1 -0
  151. package/dist/provider/index.d.ts +7 -0
  152. package/dist/provider/interactions.cjs +128 -0
  153. package/dist/provider/interactions.cjs.map +1 -0
  154. package/dist/provider/interactions.d.ts +39 -0
  155. package/dist/provider/interactions.js +128 -0
  156. package/dist/provider/interactions.js.map +1 -0
  157. package/dist/provider/selection.cjs +29 -0
  158. package/dist/provider/selection.cjs.map +1 -0
  159. package/dist/provider/selection.d.ts +9 -0
  160. package/dist/provider/selection.js +29 -0
  161. package/dist/provider/selection.js.map +1 -0
  162. package/dist/provider/table-data.cjs +92 -0
  163. package/dist/provider/table-data.cjs.map +1 -0
  164. package/dist/provider/table-data.d.ts +81 -0
  165. package/dist/provider/table-data.js +92 -0
  166. package/dist/provider/table-data.js.map +1 -0
  167. package/dist/provider/types.cjs +22 -0
  168. package/dist/provider/types.cjs.map +1 -0
  169. package/dist/provider/types.d.ts +256 -0
  170. package/dist/provider/types.js +22 -0
  171. package/dist/provider/types.js.map +1 -0
  172. package/dist/{zustand-store.cjs → provider/zustand-store.cjs} +36 -23
  173. package/dist/provider/zustand-store.cjs.map +1 -0
  174. package/dist/{zustand-store.js → provider/zustand-store.js} +21 -8
  175. package/dist/provider/zustand-store.js.map +1 -0
  176. package/dist/{cell-renderer.cjs → renderers/cell-renderer.cjs} +67 -34
  177. package/dist/renderers/cell-renderer.cjs.map +1 -0
  178. package/dist/{cell-renderer.d.ts → renderers/cell-renderer.d.ts} +1 -2
  179. package/dist/{cell-renderer.js → renderers/cell-renderer.js} +65 -32
  180. package/dist/renderers/cell-renderer.js.map +1 -0
  181. package/dist/renderers/column-header-cell.cjs +21 -13
  182. package/dist/renderers/column-header-cell.cjs.map +1 -1
  183. package/dist/renderers/column-header-cell.d.ts +3 -2
  184. package/dist/renderers/column-header-cell.js +20 -12
  185. package/dist/renderers/column-header-cell.js.map +1 -1
  186. package/dist/renderers/index.d.ts +1 -0
  187. package/dist/types.cjs +7 -19
  188. package/dist/types.cjs.map +1 -1
  189. package/dist/types.d.ts +117 -229
  190. package/dist/types.js +7 -19
  191. package/dist/types.js.map +1 -1
  192. package/dist/utils/atoms.cjs +24 -0
  193. package/dist/utils/atoms.cjs.map +1 -0
  194. package/dist/utils/atoms.d.ts +4 -0
  195. package/dist/utils/atoms.js +24 -0
  196. package/dist/utils/atoms.js.map +1 -0
  197. package/dist/utils/debug-overlay.cjs.map +1 -1
  198. package/dist/utils/debug-overlay.js.map +1 -1
  199. package/dist/utils/index.d.ts +2 -3
  200. package/dist/utils/key-handling.cjs +140 -139
  201. package/dist/utils/key-handling.cjs.map +1 -1
  202. package/dist/utils/key-handling.d.ts +4 -2
  203. package/dist/utils/key-handling.js +136 -135
  204. package/dist/utils/key-handling.js.map +1 -1
  205. package/dist/utils/validation.cjs +6 -1
  206. package/dist/utils/validation.cjs.map +1 -1
  207. package/dist/utils/validation.d.ts +1 -1
  208. package/dist/utils/validation.js +6 -1
  209. package/dist/utils/validation.js.map +1 -1
  210. package/package.json +4 -3
  211. package/src/actions/builtin.ts +1 -4
  212. package/src/actions/clipboard.ts +4 -3
  213. package/src/actions/column-controls.ts +12 -245
  214. package/src/actions/column-filter.ts +28 -92
  215. package/src/actions/context.ts +299 -0
  216. package/src/actions/edit-actions.ts +3 -1
  217. package/src/actions/index.ts +2 -3
  218. package/src/actions/selection.ts +109 -156
  219. package/src/actions/{table-controls.ts → table-actions.ts} +0 -24
  220. package/src/actions/types.ts +17 -101
  221. package/src/components/colors.ts +1 -1
  222. package/src/components/index.ts +2 -1
  223. package/src/components/indicators/filter-and-sort.module.sass +13 -0
  224. package/src/components/indicators/filter-and-sort.ts +478 -0
  225. package/src/components/indicators/index.ts +4 -0
  226. package/src/components/indicators/selection.module.sass +22 -0
  227. package/src/components/indicators/selection.ts +141 -0
  228. package/src/components/indicators/unified-filters.ts +161 -0
  229. package/src/components/{view-info.ts → indicators/view-info.ts} +16 -27
  230. package/src/{actions/toolbar.module.sass → components/toolbars/actions-toolbar.module.sass} +17 -4
  231. package/src/{actions/toolbar.ts → components/toolbars/actions-toolbar.ts} +49 -150
  232. package/src/components/toolbars/active-filters.ts +150 -0
  233. package/src/components/toolbars/index.ts +2 -0
  234. package/src/data-panel.module.sass +185 -0
  235. package/src/data-panel.ts +556 -0
  236. package/src/{core.ts → data-sheet.ts} +145 -330
  237. package/src/data-view.ts +70 -0
  238. package/src/filters/operators.ts +33 -3
  239. package/src/index.ts +4 -3
  240. package/src/postgrest-table/data-loaders.ts +244 -171
  241. package/src/postgrest-table/index.ts +10 -9
  242. package/src/{utils → provider}/column-spec.ts +30 -15
  243. package/src/provider/core.ts +585 -0
  244. package/src/provider/index.ts +7 -0
  245. package/src/provider/interactions.ts +161 -0
  246. package/src/provider/selection.ts +31 -0
  247. package/src/provider/table-data.ts +178 -0
  248. package/src/provider/types.ts +273 -0
  249. package/src/{zustand-store.ts → provider/zustand-store.ts} +35 -14
  250. package/src/{cell-renderer.ts → renderers/cell-renderer.ts} +140 -57
  251. package/src/renderers/column-header-cell.ts +21 -14
  252. package/src/renderers/index.ts +1 -0
  253. package/src/types.ts +137 -239
  254. package/src/utils/atoms.ts +28 -0
  255. package/src/utils/debug-overlay.ts +1 -1
  256. package/src/utils/index.ts +2 -8
  257. package/src/utils/key-handling.ts +170 -164
  258. package/src/utils/validation.ts +7 -2
  259. package/dist/actions/filters.cjs +0 -139
  260. package/dist/actions/filters.cjs.map +0 -1
  261. package/dist/actions/filters.js +0 -139
  262. package/dist/actions/filters.js.map +0 -1
  263. package/dist/actions/table-controls.cjs +0 -62
  264. package/dist/actions/table-controls.cjs.map +0 -1
  265. package/dist/actions/table-controls.js.map +0 -1
  266. package/dist/actions/toolbar.cjs +0 -260
  267. package/dist/actions/toolbar.cjs.map +0 -1
  268. package/dist/actions/toolbar.js.map +0 -1
  269. package/dist/actions/toolbar.module.sass.cjs +0 -27
  270. package/dist/actions/toolbar.module.sass.cjs.map +0 -1
  271. package/dist/actions/toolbar.module.sass.js +0 -25
  272. package/dist/actions/toolbar.module.sass.js.map +0 -1
  273. package/dist/cell-renderer.cjs.map +0 -1
  274. package/dist/cell-renderer.js.map +0 -1
  275. package/dist/components/view-info.cjs +0 -55
  276. package/dist/components/view-info.cjs.map +0 -1
  277. package/dist/components/view-info.js.map +0 -1
  278. package/dist/core.cjs.map +0 -1
  279. package/dist/core.d.ts +0 -117
  280. package/dist/core.js.map +0 -1
  281. package/dist/provider.cjs +0 -197
  282. package/dist/provider.cjs.map +0 -1
  283. package/dist/provider.d.ts +0 -26
  284. package/dist/provider.js +0 -195
  285. package/dist/provider.js.map +0 -1
  286. package/dist/table-updates/data-parameter.d.ts +0 -14
  287. package/dist/table-updates/filter.d.ts +0 -16
  288. package/dist/table-updates/index.d.ts +0 -19
  289. package/dist/utils/column-spec.cjs.map +0 -1
  290. package/dist/utils/column-spec.js.map +0 -1
  291. package/dist/utils/index.cjs +0 -9
  292. package/dist/utils/index.cjs.map +0 -1
  293. package/dist/utils/index.js +0 -9
  294. package/dist/utils/index.js.map +0 -1
  295. package/dist/zustand-store.cjs.map +0 -1
  296. package/dist/zustand-store.js.map +0 -1
  297. package/src/actions/filters.ts +0 -176
  298. package/src/components/actions.module.sass +0 -8
  299. package/src/provider.ts +0 -267
  300. package/src/table-updates/data-parameter.ts +0 -44
  301. package/src/table-updates/filter.ts +0 -124
  302. package/src/table-updates/index.ts +0 -239
  303. /package/dist/{zustand-store.d.ts → provider/zustand-store.d.ts} +0 -0
@@ -1,20 +1,28 @@
1
1
  /** Lazy loading of data from a PostgREST endpoint */
2
2
 
3
3
  import { useAsyncEffect } from "@macrostrat/ui-components";
4
- import { debounce, range } from "underscore";
4
+ import { debounce } from "underscore";
5
5
  import { useCallback, useEffect, useMemo, useRef } from "react";
6
6
  import update, { Spec } from "immutability-helper";
7
7
  import {
8
+ ClientServerOptions,
8
9
  PostgrestClient,
9
10
  PostgrestFilterBuilder,
10
11
  PostgrestQueryBuilder,
11
12
  } from "@supabase/postgrest-js";
12
13
  import { adjustArraySize, RowRegion } from "./loading-utils.ts";
13
- import { ctx, tableDataAtom, useSelector } from "../provider.ts";
14
+ import {
15
+ ctx,
16
+ dataProviderAtom,
17
+ dataRefreshTokenAtom,
18
+ FetchData,
19
+ FetchDataFilter,
20
+ tableDataAtom,
21
+ TableDataProvider,
22
+ useSelector,
23
+ } from "../provider";
14
24
  import { atom } from "jotai";
15
- import type { ColumnSort } from "../types.ts";
16
-
17
- export type FetchMode = "scroll" | "paged";
25
+ import { FetchDataOptions, FetchMode } from "../types.ts";
18
26
 
19
27
  interface LazyLoaderStateCore<T> {
20
28
  loading: boolean;
@@ -39,7 +47,8 @@ export interface PostgrestOrder<T> {
39
47
  nullsFirst?: boolean;
40
48
  }
41
49
 
42
- /** Operators available for server-side column filtering via PostgREST. */
50
+ /** Operators available for server-side column filtering via PostgREST.
51
+ * `cs`/`ov` target array columns (contains / overlaps). */
43
52
  export type PostgrestFilterOperator =
44
53
  | "eq"
45
54
  | "neq"
@@ -49,7 +58,9 @@ export type PostgrestFilterOperator =
49
58
  | "lt"
50
59
  | "gte"
51
60
  | "lte"
52
- | "is";
61
+ | "is"
62
+ | "cs"
63
+ | "ov";
53
64
 
54
65
  /** A single column sort entry. */
55
66
  export interface ColumnSortEntry {
@@ -65,6 +76,7 @@ export interface PostgrestColumnFilter {
65
76
  }
66
77
 
67
78
  export interface PostgrestFilter {
79
+ id?: string;
68
80
  type: "filter";
69
81
  apply(
70
82
  req: PostgrestFilterBuilder<any, any, any, any>,
@@ -88,6 +100,16 @@ export function applyColumnFilters(
88
100
  ) {
89
101
  for (const cf of filters) {
90
102
  if (cf.value === "" || cf.value == null) continue;
103
+ // Array-column operators need a list value and postgrest-js's dedicated
104
+ // builders (which emit the `{…}` array literal), not a scalar `.filter`.
105
+ if (cf.operator === "cs") {
106
+ req = req.contains(cf.key, splitFilterList(cf.value));
107
+ continue;
108
+ }
109
+ if (cf.operator === "ov") {
110
+ req = req.overlaps(cf.key, splitFilterList(cf.value));
111
+ continue;
112
+ }
91
113
  const val =
92
114
  cf.operator === "like" || cf.operator === "ilike"
93
115
  ? `*${cf.value}*`
@@ -97,6 +119,15 @@ export function applyColumnFilters(
97
119
  return req;
98
120
  }
99
121
 
122
+ /** Split a comma-separated filter value into trimmed, non-empty tokens (the set
123
+ * for `cs`/`ov`); a single value yields a one-element list. */
124
+ function splitFilterList(value: any): string[] {
125
+ return String(value)
126
+ .split(",")
127
+ .map((s) => s.trim())
128
+ .filter((s) => s !== "");
129
+ }
130
+
100
131
  type LazyLoaderAction<T> =
101
132
  | { type: "start-loading" }
102
133
  | {
@@ -126,7 +157,7 @@ function lazyLoadingReducer<T>(
126
157
  return {
127
158
  ...state,
128
159
  loading: false,
129
- data: update(state.data, action.changes),
160
+ data: update(state.data, action.changes as any),
130
161
  };
131
162
  case "loaded":
132
163
  let data = adjustArraySize(state.data, action.totalSize);
@@ -191,12 +222,69 @@ interface QueryConfig extends SortAndFilterOptions {
191
222
  count?: "exact" | "estimated";
192
223
  limit?: number;
193
224
  identityKey: string;
225
+ /** The identity column's default ordering (used when nothing in `order`
226
+ * already sorts it). A string ⇒ ascending; pass a `PostgrestOrder` to set the
227
+ * direction (e.g. `source_id` descending). */
228
+ identityOrder?: PostgrestOrder<any>;
194
229
  lastLoadedRow?: any;
195
230
  lastLoadedRowIndex: number;
196
231
  }
197
232
 
233
+ /** Format a value for a PostgREST logic-tree condition. Reserved characters
234
+ * (`, . : ( )`), spaces, quotes, and backslashes require the value to be
235
+ * double-quoted, with `"` and `\` backslash-escaped inside — otherwise a map
236
+ * name containing a comma would break the `or=(...)` parse. */
237
+ function formatKeysetValue(v: any): string {
238
+ const s = String(v);
239
+ if (s === "" || /[,.:()"\\ ]/.test(s)) {
240
+ return `"${s.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
241
+ }
242
+ return s;
243
+ }
244
+
245
+ /** An equality condition for the cursor prefix (`col.eq.v`, or `col.is.null`). */
246
+ function keysetEq(key: string, v: any): string {
247
+ return v == null ? `${key}.is.null` : `${key}.eq.${formatKeysetValue(v)}`;
248
+ }
249
+
250
+ /**
251
+ * Build the compound keyset predicate (the body of an `or=(...)`) selecting
252
+ * rows strictly after `lastRow` under `clauses` (lexicographic, last clause is
253
+ * the identity tiebreaker):
254
+ *
255
+ * (c1 OP1 v1)
256
+ * OR (c1 = v1 AND c2 OP2 v2)
257
+ * OR (c1 = v1 AND c2 = v2 AND c3 OP3 v3) …
258
+ *
259
+ * `OPi` is `gt` for an ascending clause, `lt` for descending. Nulls-last: a
260
+ * null pivot value has no strictly-ordered non-null successor at its level, so
261
+ * that term is skipped and the tie defers to deeper clauses (the identity key
262
+ * is non-null, so at least its term is always emitted). Returns `null` if no
263
+ * term applies. (Known limit: rows whose *sort* column is null aren't picked up
264
+ * across the non-null→null boundary — an edge for nullable sort columns; the
265
+ * non-null identity tiebreaker keeps pagination from stalling.)
266
+ */
267
+ function buildKeysetPredicate(
268
+ clauses: PostgrestOrder<any>[],
269
+ lastRow: any,
270
+ ): string | null {
271
+ const terms: string[] = [];
272
+ for (let i = 0; i < clauses.length; i++) {
273
+ const v = lastRow[clauses[i].key];
274
+ if (v == null) continue;
275
+ const conds: string[] = [];
276
+ for (let j = 0; j < i; j++) {
277
+ conds.push(keysetEq(clauses[j].key, lastRow[clauses[j].key]));
278
+ }
279
+ const op = (clauses[i].ascending ?? true) ? "gt" : "lt";
280
+ conds.push(`${clauses[i].key}.${op}.${formatKeysetValue(v)}`);
281
+ terms.push(conds.length === 1 ? conds[0] : `and(${conds.join(",")})`);
282
+ }
283
+ return terms.length > 0 ? terms.join(",") : null;
284
+ }
285
+
198
286
  function buildQuery<T>(
199
- client: PostgrestQueryBuilder<T, any, any>,
287
+ client: PostgrestQueryBuilder<any, any, any>,
200
288
  config: QueryConfig,
201
289
  ) {
202
290
  const { columns = "*", count, lastLoadedRowIndex } = config;
@@ -218,21 +306,27 @@ function buildQuery<T>(
218
306
 
219
307
  const orderClauses = buildPostgrestOrderClauses(
220
308
  config.order ?? [],
221
- config.identityKey,
309
+ config.identityOrder ?? config.identityKey,
222
310
  );
223
311
 
224
312
  let hasOrdering = false;
225
313
  for (const clause of orderClauses) {
226
314
  query = query.order(clause.key, clause);
227
- if (config.lastLoadedRow != null) {
228
- const op = (clause.ascending ?? true) ? "gt" : "lt";
229
- const value = config.lastLoadedRow[clause.key];
230
- query = query[op](clause.key, value);
231
- }
232
315
  hasOrdering = true;
233
316
  }
234
317
 
235
- let offset = null;
318
+ // Keyset cursor: rows strictly after the last loaded row under the active
319
+ // ordering. This MUST be a compound predicate, not an independent `gt`/`lt`
320
+ // per column: with a leading low-cardinality sort (e.g. `state`), a plain
321
+ // `state.gt.X AND source_id.lt.Y` drops every row sharing the cursor's
322
+ // `state` (`state.gt.X` is false for them). The lexicographic form keeps the
323
+ // tie broken by the next column, ending on the non-null identity key.
324
+ if (config.lastLoadedRow != null && hasOrdering) {
325
+ const pred = buildKeysetPredicate(orderClauses, config.lastLoadedRow);
326
+ if (pred != null) query = query.or(pred);
327
+ }
328
+
329
+ let offset: number | null = null;
236
330
  if (!hasOrdering && lastLoadedRowIndex > 0) {
237
331
  // We need to load based on offsets
238
332
  offset = lastLoadedRowIndex + 1;
@@ -247,7 +341,6 @@ function buildQuery<T>(
247
341
  }
248
342
  }
249
343
 
250
- console.log("query", query.url.search);
251
344
  return query;
252
345
  }
253
346
 
@@ -271,10 +364,6 @@ const lazyLoaderCoreStateAtom = atom<LazyLoaderStateCore<any>>({
271
364
  /** Current page index (0-based) for paged fetch mode. */
272
365
  export const chunkPageAtom = atom(0);
273
366
 
274
- /** Bump this to force the active chunk loader to reset and re-fetch from
275
- * scratch — e.g. after a mutation (save/delete) that invalidates loaded rows. */
276
- export const dataRefreshTokenAtom = atom(0);
277
-
278
367
  /** Footer state for the optional bottom-of-table indicator: rows loaded, the
279
368
  * source total when known, load status, and — in paged mode — the current
280
369
  * page and total pages. */
@@ -313,6 +402,85 @@ export const tableFooterAtom = atom<TableFooterInfo>((get) => {
313
402
  };
314
403
  });
315
404
 
405
+ /** Auto-load this many pages per burst, then pause (null ⇒ unbounded). Set by
406
+ * a consumer (e.g. `DataPanel`'s `autoLoadPages` prop). */
407
+ export const autoLoadPagesAtom = atom<number | null>(null);
408
+
409
+ /** Row count at the last manual "load more" — the point auto-load resumes
410
+ * counting a fresh burst from. */
411
+ export const resumeBaselineAtom = atom(0);
412
+
413
+ /** Live loading controls, read from the store rather than passed down a
414
+ * boundary contract. Any component inside the provider can call
415
+ * `useLoadControls()` — a footer, a toolbar button, a status line — without the
416
+ * renderer wiring a specific prop shape to it. */
417
+ export interface LoadControls {
418
+ /** Load the next page and resume auto-loading (clears a paused state). */
419
+ loadMore: () => void;
420
+ /** A fetch is in flight. */
421
+ loading: boolean;
422
+ /** More rows remain to load. */
423
+ hasMore: boolean;
424
+ /** Rows loaded so far. */
425
+ loaded: number;
426
+ /** Source total when known, else `null`. */
427
+ total: number | null;
428
+ /** Auto-load is paused at the `autoLoadPages` checkpoint. */
429
+ paused: boolean;
430
+ /** Auto-load is currently permitted (`hasMore && !loading && !paused`) —
431
+ * used to gate an infinite-scroll sentinel. */
432
+ canLoadMore: boolean;
433
+ /** Advance one page *without* resuming a paused auto-load (the sentinel
434
+ * uses this; `loadMore` is the manual, un-pausing variant). */
435
+ advance: () => void;
436
+ }
437
+
438
+ /** Store-managed load controls (see `LoadControls`). Reads the loader's footer
439
+ * state, the loaded rows, and the pause atoms; `loadMore`/`advance` set the
440
+ * visible region so the loader fetches the next chunk. */
441
+ export function useLoadControls(): LoadControls {
442
+ const footer = ctx.useValue(tableFooterAtom);
443
+ const data = ctx.useValue(tableDataAtom) ?? [];
444
+ const { isLocalProvider } = ctx.useValue(dataProviderAtom);
445
+ const resumeBaseline = ctx.useValue(resumeBaselineAtom);
446
+ const autoLoadPages = ctx.useValue(autoLoadPagesAtom);
447
+ const setResumeBaseline = ctx.useSet(resumeBaselineAtom);
448
+ const setVisibleRegion = ctx.useSet(visibleRegionAtom);
449
+
450
+ let loadedCount = 0;
451
+ for (const row of data) if (row != null) loadedCount++;
452
+ const pageSize = footer.pageSize || 100;
453
+ const dataLength = data.length;
454
+ const hasMore = !isLocalProvider && loadedCount < dataLength;
455
+ const paused =
456
+ autoLoadPages != null &&
457
+ loadedCount - resumeBaseline >= autoLoadPages * pageSize;
458
+ const canLoadMore = hasMore && !footer.loading && !paused;
459
+
460
+ const advance = useCallback(() => {
461
+ setVisibleRegion({
462
+ rowIndexStart: 0,
463
+ rowIndexEnd: Math.min(loadedCount + pageSize, dataLength),
464
+ });
465
+ }, [setVisibleRegion, loadedCount, pageSize, dataLength]);
466
+
467
+ const loadMore = useCallback(() => {
468
+ setResumeBaseline(loadedCount);
469
+ advance();
470
+ }, [setResumeBaseline, loadedCount, advance]);
471
+
472
+ return {
473
+ loadMore,
474
+ loading: footer.loading,
475
+ hasMore,
476
+ loaded: footer.loaded,
477
+ total: footer.total,
478
+ paused,
479
+ canLoadMore,
480
+ advance,
481
+ };
482
+ }
483
+
316
484
  export interface ViewInfo {
317
485
  visibleRegion: RowRegion;
318
486
  totalCount: number | null;
@@ -379,30 +547,45 @@ function buildPostgrestOrderClauses(
379
547
  sorts: (ColumnSortEntry | PostgrestOrder<any>)[],
380
548
  identityOrder: PostgrestOrder<any> | string,
381
549
  ): PostgrestOrder<any>[] {
382
- // Clause to build ordering for a Postgrest table.
383
- // There must be an ordering by identity key, and it must be last...
550
+ // Build the ORDER BY for a PostgREST query. Two invariants:
551
+ // 1. Each column appears at most once. Keyset pagination adds a `gt`/`lt`
552
+ // cursor predicate per order clause, so a duplicated column (e.g. the
553
+ // identity key present in both a base order and an active sort, with
554
+ // opposite directions) would emit contradictory `col=lt.X&col=gt.X`
555
+ // bounds that match nothing — an infinite empty-result reload. First
556
+ // occurrence wins (active sorts should be passed ahead of base order).
557
+ // 2. The identity key is ordered last (a stable tiebreaker for the cursor),
558
+ // appended with its default direction only if no clause already sorts it.
384
559
  const identitySort =
385
560
  typeof identityOrder == "string"
386
561
  ? { key: identityOrder, ascending: true }
387
562
  : identityOrder;
388
563
  const identityKey = identitySort.key;
389
- const clauses: PostgrestOrder<any>[] = sorts.map((sort) => {
390
- return {
564
+
565
+ const seen = new Set<string>();
566
+ const clauses: PostgrestOrder<any>[] = [];
567
+ for (const sort of sorts) {
568
+ if (seen.has(sort.key)) continue;
569
+ seen.add(sort.key);
570
+ clauses.push({
391
571
  key: sort.key,
392
572
  ascending: sort.ascending,
393
573
  nullsFirst: "nullsFirst" in sort ? sort.nullsFirst : false,
394
- };
395
- });
396
- // sort the clauses so the identity clause is last
397
- const hasIdentityClause = clauses.some((d) => d.key === identityKey);
398
- if (hasIdentityClause) {
399
- clauses.sort((a, b) => {
400
- if (a.key === identityKey) return 1;
401
- if (b.key === identityKey) return -1;
402
- return 0;
403
574
  });
575
+ }
576
+
577
+ if (seen.has(identityKey)) {
578
+ // Move the (already-deduped) identity clause to the end, keeping the
579
+ // direction the active sort chose.
580
+ const idx = clauses.findIndex((c) => c.key === identityKey);
581
+ const [idClause] = clauses.splice(idx, 1);
582
+ clauses.push(idClause);
404
583
  } else {
405
- clauses.push(identitySort);
584
+ clauses.push({
585
+ key: identitySort.key,
586
+ ascending: identitySort.ascending ?? true,
587
+ nullsFirst: (identitySort as any).nullsFirst ?? false,
588
+ });
406
589
  }
407
590
  return clauses;
408
591
  }
@@ -429,140 +612,6 @@ export function useScrollHandler() {
429
612
  );
430
613
  }
431
614
 
432
- /** The active view state (a filter's id / column / config), passed to a
433
- * `fetchChunk`. A server provider translates `columnKey` + `state` into a
434
- * query; a local (in-memory) provider applies `predicate` directly. Both come
435
- * from the same `TableFilter`, so one filter definition serves either target. */
436
- export interface FetchDataFilter {
437
- id: string;
438
- columnKey?: string;
439
- state: any;
440
- /** Client-side row predicate (from the `TableFilter`). Server providers
441
- * ignore it and translate `columnKey` + `state` instead. */
442
- predicate?: (row: any, state: any) => boolean;
443
- }
444
-
445
- /** Parameters passed to a `fetchChunk` implementation for one window. */
446
- export interface FetchDataParams {
447
- /** Row offset of the requested chunk (chunk-aligned). */
448
- offset: number;
449
- /** Maximum rows to return. */
450
- limit: number;
451
- /** Aborts when the request is superseded (view change / unmount). */
452
- signal: AbortSignal;
453
- /** Active sorts, in priority order. */
454
- sorts: ColumnSort[];
455
- /** Active filters (id + column + config). */
456
- filters: FetchDataFilter[];
457
- /** In scroll mode, the already-loaded row immediately before this chunk
458
- * (and its data-array index), or `null` at the start / in paged mode. Keyset
459
- * sources can page from this cursor (e.g. `WHERE key > cursor`) instead of a
460
- * slow `OFFSET`; offset-based sources can ignore it. */
461
- cursor?: { row: any; index: number } | null;
462
- }
463
-
464
- /** Result of a `fetchChunk` call. `totalCount` reports the source length when
465
- * known (drives sparse-array pre-sizing and a proportional scrollbar); omit it
466
- * for unknown-length sources (the array grows as chunks arrive). */
467
- export interface FetchDataResult<T = any> {
468
- rows: T[];
469
- totalCount?: number | null;
470
- }
471
-
472
- export type FetchData<T = any> = (
473
- params: FetchDataParams,
474
- ) => Promise<FetchDataResult<T>>;
475
-
476
- /**
477
- * A table's data source, addressed uniformly whether it's an in-memory array or
478
- * a remote backend. `fetchChunk` returns a window with the active sorts/filters
479
- * already applied (in memory by the local provider, in SQL by a server one);
480
- * `identity` addresses a row for edits/mutations — stable across re-sorts,
481
- * unlike an array index. The mutation ops are present only for editable /
482
- * persisting sources. The in-memory case (`createLocalProvider`) is just the
483
- * degenerate implementation, so local and server tables share one path.
484
- */
485
- export interface TableDataProvider<T = any> {
486
- fetchData(params: FetchDataParams): Promise<FetchDataResult<T>>;
487
- identity(row: T): string | number;
488
- saveRows?(rows: T[]): Promise<void>;
489
- deleteRows?(ids: Array<string | number>): Promise<void>;
490
- insertRow?(row: Partial<T>): Promise<void>;
491
- }
492
-
493
- /**
494
- * The degenerate provider: an in-memory array. `fetchChunk` applies the active
495
- * filter predicates and sorts over the array and returns the requested slice
496
- * plus the (fully-known) total — the same view engine used for client-side
497
- * tables, expressed as a provider.
498
- */
499
- export function createLocalProvider<T = any>(
500
- data: T[],
501
- options: { identity?: (row: T) => string | number } = {},
502
- ): TableDataProvider<T> {
503
- const identity: (row: T) => string | number =
504
- options.identity ?? defaultLocalIdentity;
505
- return {
506
- identity,
507
- async fetchData({ offset, limit, sorts, filters }) {
508
- let rows = data;
509
- if (filters != null && filters.length > 0) {
510
- rows = rows.filter((row) =>
511
- filters.every((f) =>
512
- f.predicate != null ? f.predicate(row, f.state) : true,
513
- ),
514
- );
515
- }
516
- if (sorts != null && sorts.length > 0) {
517
- rows = [...rows].sort(compareRowsBySorts(sorts));
518
- }
519
- return {
520
- rows: rows.slice(offset, offset + limit),
521
- totalCount: rows.length,
522
- };
523
- },
524
- };
525
- }
526
-
527
- // Stable synthetic identity for in-memory rows lacking a natural `id`. Keyed by
528
- // object reference (a local data array keeps the same row objects across
529
- // re-sorts), so edits survive re-ordering even without an id field.
530
- const _syntheticRowIds = new WeakMap<object, string>();
531
- let _syntheticRowCounter = 0;
532
- function defaultLocalIdentity(row: any): string | number | undefined {
533
- if (row == null) return undefined;
534
- if (row.id != null) return row.id;
535
- if (typeof row !== "object") return row;
536
- let id = _syntheticRowIds.get(row);
537
- if (id == null) {
538
- id = `__row_${_syntheticRowCounter++}`;
539
- _syntheticRowIds.set(row, id);
540
- }
541
- return id;
542
- }
543
-
544
- /** Multi-key row comparator (priority order; nulls first when ascending),
545
- * shared by the local provider and any other in-memory sort. */
546
- export function compareRowsBySorts(sorts: ColumnSort[]) {
547
- return (a: any, b: any): number => {
548
- for (const sort of sorts) {
549
- const av = a?.[sort.key];
550
- const bv = b?.[sort.key];
551
- if (av == null && bv == null) continue;
552
- if (av == null) return sort.ascending ? -1 : 1;
553
- if (bv == null) return sort.ascending ? 1 : -1;
554
- const cmp = av < bv ? -1 : av > bv ? 1 : 0;
555
- if (cmp !== 0) return sort.ascending ? cmp : -cmp;
556
- }
557
- return 0;
558
- };
559
- }
560
-
561
- export interface FetchDataOptions {
562
- pageSize?: number;
563
- fetchMode?: FetchMode;
564
- }
565
-
566
615
  /**
567
616
  * A generic, backend-agnostic windowed data source. Give it a `fetchChunk`
568
617
  * function and it drives the sheet's lazy loading: it loads the chunk covering
@@ -580,6 +629,9 @@ export function useDataLoader<T = any>(
580
629
  ) {
581
630
  const { pageSize = 100, fetchMode = "scroll" } = options;
582
631
  const [state, dispatch] = useLazyLoaderReducer();
632
+
633
+ const { isLocalProvider, provider } = ctx.useValue(dataProviderAtom);
634
+
583
635
  const visibleRegion = ctx.useValue(visibleRegionAtom);
584
636
  const page = ctx.useValue(chunkPageAtom);
585
637
  const setPage = ctx.useSet(chunkPageAtom);
@@ -724,6 +776,11 @@ export function createPostgRESTFetchChunk<T = any>(config: {
724
776
  endpoint: string;
725
777
  table: string;
726
778
  identityKey: string;
779
+ /** Default ordering direction of the identity key (applied when no active
780
+ * sort targets it). `false` ⇒ the table defaults to identity-descending
781
+ * (e.g. newest `source_id` first) without a redundant `baseOrder` entry that
782
+ * would otherwise collide with the identity tiebreaker. Defaults to `true`. */
783
+ identityAscending?: boolean;
727
784
  columns?: string | string[];
728
785
  /** Order applied before the active column sorts (identity key appended last). */
729
786
  baseOrder?: PostgrestOrder<any>[];
@@ -739,9 +796,12 @@ export function createPostgRESTFetchChunk<T = any>(config: {
739
796
  return async ({ limit, signal, sorts, filters, cursor }) => {
740
797
  const client = new PostgrestClient(config.endpoint).from(config.table);
741
798
 
799
+ // Active sorts first so they win the by-key dedupe in
800
+ // `buildPostgrestOrderClauses` (a user sort on the identity column
801
+ // overrides its default direction rather than colliding with it).
742
802
  const order: PostgrestOrder<any>[] = [
743
- ...(config.baseOrder ?? []),
744
803
  ...sorts.map((s) => ({ key: s.key, ascending: s.ascending })),
804
+ ...(config.baseOrder ?? []),
745
805
  ];
746
806
 
747
807
  const pgFilters: PostgrestFilter[] = [];
@@ -760,6 +820,10 @@ export function createPostgRESTFetchChunk<T = any>(config: {
760
820
  count: cursor == null ? "exact" : undefined,
761
821
  limit,
762
822
  identityKey: config.identityKey,
823
+ identityOrder: {
824
+ key: config.identityKey,
825
+ ascending: config.identityAscending ?? true,
826
+ },
763
827
  order,
764
828
  filters: pgFilters,
765
829
  lastLoadedRow: cursor?.row ?? null,
@@ -784,6 +848,9 @@ export function createPostgRESTProvider<T = any>(config: {
784
848
  endpoint: string;
785
849
  table: string;
786
850
  identityKey: string;
851
+ /** Default ordering direction of the identity key (see
852
+ * `createPostgRESTFetchChunk`). `false` ⇒ identity-descending by default. */
853
+ identityAscending?: boolean;
787
854
  columns?: string | string[];
788
855
  baseOrder?: PostgrestOrder<any>[];
789
856
  baseFilter?: (
@@ -825,7 +892,13 @@ function indexOfFirstNullInRegion(
825
892
  data: any[],
826
893
  region: RowRegion,
827
894
  ): number | null {
828
- for (let i = region.rowIndexStart; i < region.rowIndexEnd; i++) {
895
+ // Clamp to the array length: a visible region can outlast the data it was
896
+ // set against (e.g. a keyset page hits the end of a filtered set and the
897
+ // array shrinks below a region end set before the shrink). Scanning past
898
+ // `data.length` reads `undefined` as a fillable null and re-fetches the same
899
+ // now-out-of-range offset forever — the tail-of-filtered-set reload loop.
900
+ const end = Math.min(region.rowIndexEnd, data.length);
901
+ for (let i = region.rowIndexStart; i < end; i++) {
829
902
  if (data[i] == null) {
830
903
  return i;
831
904
  }
@@ -1,20 +1,19 @@
1
1
  import { InputGroup, OverlayToaster } from "@blueprintjs/core";
2
2
  import h from "./main.module.sass";
3
- import { DataSheet } from "../core";
3
+ import { DataSheet } from "../data-sheet.ts";
4
4
  import { LithologyTag, Tag, TagSize } from "@macrostrat/data-components";
5
5
  import {
6
6
  createPostgRESTProvider,
7
- dataRefreshTokenAtom,
8
7
  PostgrestFilter,
9
8
  PostgrestOrder,
10
9
  } from "./data-loaders";
11
- import { useCallback, useEffect, useMemo, useRef } from "react";
10
+ import { ReactNode, useCallback, useEffect, useMemo, useRef } from "react";
12
11
  import { ErrorBoundary, ToasterContext } from "@macrostrat/ui-components";
13
12
  import { PostgrestFilterBuilder } from "@supabase/postgrest-js";
14
13
  import { ColorCell } from "../components";
15
- import { DataSheetProviderProps } from "../types.ts";
14
+ import { DataSheetProviderProps } from "../provider/types.ts";
16
15
  import { atom } from "jotai";
17
- import { columnSpecAtom, ctx } from "../provider.ts";
16
+ import { columnSpecAtom, ctx, dataRefreshTokenAtom } from "../provider";
18
17
  import { ALL_CARDINALITIES, TableAction } from "../actions";
19
18
 
20
19
  export * from "./data-loaders";
@@ -47,7 +46,9 @@ interface PostgRESTTableViewProps<
47
46
  ): PostgrestFilterBuilder<T, any, any, any>;
48
47
  }
49
48
 
50
- export function PostgRESTTableView<T>(props: PostgRESTTableViewProps<T>) {
49
+ export function PostgRESTTableView<T extends object>(
50
+ props: PostgRESTTableViewProps<T>,
51
+ ) {
51
52
  return h(
52
53
  ErrorBoundary,
53
54
  h(ctx.Provider, h(ToasterContext, h(_PostgRESTTableView, props))),
@@ -104,10 +105,10 @@ function _PostgRESTTableView<T>({
104
105
  editable = false,
105
106
  enableFullTableSearch = false,
106
107
  actions,
107
- identityKey = null,
108
+ identityKey,
108
109
  filter: userFilter,
109
110
  ...rest
110
- }: PostgRESTTableViewProps<T>) {
111
+ }: PostgRESTTableViewProps<any>) {
111
112
  // Boundary of Jotai store
112
113
  ctx.useSync(enableFullTextSearchAtom, enableFullTableSearch);
113
114
 
@@ -197,7 +198,7 @@ export function notifyOnError(toaster: OverlayToaster, error: any) {
197
198
  console.error(error);
198
199
  const { message, status, code, details } = error;
199
200
 
200
- let errorDetails = null;
201
+ let errorDetails: ReactNode = null;
201
202
 
202
203
  if (details != null) {
203
204
  if (typeof details === "string") {