@macrostrat/data-sheet 3.1.0 → 4.0.0-dev1

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 (184) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/dist/actions/builtin.cjs +56 -0
  3. package/dist/actions/builtin.cjs.map +1 -0
  4. package/dist/actions/builtin.d.ts +15 -0
  5. package/dist/actions/builtin.js +56 -0
  6. package/dist/actions/builtin.js.map +1 -0
  7. package/dist/actions/clipboard.cjs +250 -0
  8. package/dist/actions/clipboard.cjs.map +1 -0
  9. package/dist/actions/clipboard.d.ts +42 -0
  10. package/dist/actions/clipboard.js +250 -0
  11. package/dist/actions/clipboard.js.map +1 -0
  12. package/dist/actions/filters.cjs +123 -0
  13. package/dist/actions/filters.cjs.map +1 -0
  14. package/dist/actions/filters.d.ts +6 -0
  15. package/dist/actions/filters.js +123 -0
  16. package/dist/actions/filters.js.map +1 -0
  17. package/dist/actions/index.d.ts +6 -0
  18. package/dist/actions/selection.cjs +194 -0
  19. package/dist/actions/selection.cjs.map +1 -0
  20. package/dist/actions/selection.d.ts +40 -0
  21. package/dist/actions/selection.js +192 -0
  22. package/dist/actions/selection.js.map +1 -0
  23. package/dist/actions/toolbar.cjs +171 -0
  24. package/dist/actions/toolbar.cjs.map +1 -0
  25. package/dist/actions/toolbar.d.ts +10 -0
  26. package/dist/actions/toolbar.js +171 -0
  27. package/dist/actions/toolbar.js.map +1 -0
  28. package/dist/actions/toolbar.module.sass.cjs +22 -0
  29. package/dist/actions/toolbar.module.sass.cjs.map +1 -0
  30. package/dist/actions/toolbar.module.sass.js +20 -0
  31. package/dist/actions/toolbar.module.sass.js.map +1 -0
  32. package/dist/actions/types.d.ts +143 -0
  33. package/dist/cell-renderer.cjs +44 -41
  34. package/dist/cell-renderer.cjs.map +1 -1
  35. package/dist/cell-renderer.d.ts +1 -6
  36. package/dist/cell-renderer.js +45 -42
  37. package/dist/cell-renderer.js.map +1 -1
  38. package/dist/components/actions.module.sass.cjs +3 -3
  39. package/dist/components/actions.module.sass.js +2 -2
  40. package/dist/components/colors.cjs +20 -16
  41. package/dist/components/colors.cjs.map +1 -1
  42. package/dist/components/colors.d.ts +4 -3
  43. package/dist/components/colors.js +20 -16
  44. package/dist/components/colors.js.map +1 -1
  45. package/dist/components/editor-popup.cjs +4 -3
  46. package/dist/components/editor-popup.cjs.map +1 -1
  47. package/dist/components/editor-popup.js +4 -3
  48. package/dist/components/editor-popup.js.map +1 -1
  49. package/dist/components/index.d.ts +1 -0
  50. package/dist/components/main.module.sass.cjs +12 -12
  51. package/dist/components/main.module.sass.js +11 -11
  52. package/dist/components/text-areas.cjs +23 -18
  53. package/dist/components/text-areas.cjs.map +1 -1
  54. package/dist/components/text-areas.d.ts +8 -4
  55. package/dist/components/text-areas.js +20 -13
  56. package/dist/components/text-areas.js.map +1 -1
  57. package/dist/components/view-info.cjs +18 -0
  58. package/dist/components/view-info.cjs.map +1 -0
  59. package/dist/components/view-info.d.ts +1 -0
  60. package/dist/components/view-info.js +18 -0
  61. package/dist/components/view-info.js.map +1 -0
  62. package/dist/core.cjs +184 -156
  63. package/dist/core.cjs.map +1 -1
  64. package/dist/core.d.ts +14 -1
  65. package/dist/core.js +187 -157
  66. package/dist/core.js.map +1 -1
  67. package/dist/data-sheet.css +181 -43
  68. package/dist/index.cjs +43 -13
  69. package/dist/index.cjs.map +1 -1
  70. package/dist/index.d.ts +1 -0
  71. package/dist/index.js +43 -10
  72. package/dist/index.js.map +1 -1
  73. package/dist/main.module.sass.cjs +15 -14
  74. package/dist/main.module.sass.cjs.map +1 -1
  75. package/dist/main.module.sass.js +14 -13
  76. package/dist/main.module.sass.js.map +1 -1
  77. package/dist/notifications.cjs +19 -0
  78. package/dist/notifications.cjs.map +1 -0
  79. package/dist/notifications.d.ts +9 -0
  80. package/dist/notifications.js +19 -0
  81. package/dist/notifications.js.map +1 -0
  82. package/dist/postgrest-table/data-loaders.cjs +181 -104
  83. package/dist/postgrest-table/data-loaders.cjs.map +1 -1
  84. package/dist/postgrest-table/data-loaders.d.ts +52 -28
  85. package/dist/postgrest-table/data-loaders.js +181 -104
  86. package/dist/postgrest-table/data-loaders.js.map +1 -1
  87. package/dist/postgrest-table/index.cjs +205 -79
  88. package/dist/postgrest-table/index.cjs.map +1 -1
  89. package/dist/postgrest-table/index.d.ts +13 -16
  90. package/dist/postgrest-table/index.js +186 -58
  91. package/dist/postgrest-table/index.js.map +1 -1
  92. package/dist/postgrest-table/lazy-loader-table.cjs +18 -17
  93. package/dist/postgrest-table/lazy-loader-table.cjs.map +1 -1
  94. package/dist/postgrest-table/lazy-loader-table.d.ts +1 -1
  95. package/dist/postgrest-table/lazy-loader-table.js +17 -14
  96. package/dist/postgrest-table/lazy-loader-table.js.map +1 -1
  97. package/dist/postgrest-table/loading-utils.cjs +16 -0
  98. package/dist/postgrest-table/loading-utils.cjs.map +1 -0
  99. package/dist/postgrest-table/loading-utils.d.ts +13 -0
  100. package/dist/postgrest-table/loading-utils.js +16 -0
  101. package/dist/postgrest-table/loading-utils.js.map +1 -0
  102. package/dist/postgrest-table/main.module.sass.cjs +18 -13
  103. package/dist/postgrest-table/main.module.sass.cjs.map +1 -1
  104. package/dist/postgrest-table/main.module.sass.js +18 -13
  105. package/dist/postgrest-table/main.module.sass.js.map +1 -1
  106. package/dist/provider.cjs +58 -26
  107. package/dist/provider.cjs.map +1 -1
  108. package/dist/provider.js +57 -23
  109. package/dist/provider.js.map +1 -1
  110. package/dist/renderers/column-header-cell-postgrest.cjs +216 -0
  111. package/dist/renderers/column-header-cell-postgrest.cjs.map +1 -0
  112. package/dist/renderers/column-header-cell-postgrest.d.ts +13 -0
  113. package/dist/renderers/column-header-cell-postgrest.js +216 -0
  114. package/dist/renderers/column-header-cell-postgrest.js.map +1 -0
  115. package/dist/renderers/column-header-cell-postgrest.module.sass.cjs +23 -0
  116. package/dist/renderers/column-header-cell-postgrest.module.sass.cjs.map +1 -0
  117. package/dist/renderers/column-header-cell-postgrest.module.sass.js +22 -0
  118. package/dist/renderers/column-header-cell-postgrest.module.sass.js.map +1 -0
  119. package/dist/renderers/column-header-cell.cjs +103 -0
  120. package/dist/renderers/column-header-cell.cjs.map +1 -0
  121. package/dist/renderers/column-header-cell.d.ts +30 -0
  122. package/dist/renderers/column-header-cell.js +101 -0
  123. package/dist/renderers/column-header-cell.js.map +1 -0
  124. package/dist/renderers/index.d.ts +2 -0
  125. package/dist/table-updates/data-parameter.d.ts +14 -0
  126. package/dist/table-updates/filter.d.ts +16 -0
  127. package/dist/table-updates/index.d.ts +19 -0
  128. package/dist/types.cjs +9 -0
  129. package/dist/types.cjs.map +1 -0
  130. package/dist/types.d.ts +58 -4
  131. package/dist/types.js +9 -0
  132. package/dist/types.js.map +1 -0
  133. package/dist/utils/column-spec.cjs +9 -1
  134. package/dist/utils/column-spec.cjs.map +1 -1
  135. package/dist/utils/column-spec.d.ts +21 -0
  136. package/dist/utils/column-spec.js +9 -1
  137. package/dist/utils/column-spec.js.map +1 -1
  138. package/dist/utils/key-handling.cjs +141 -52
  139. package/dist/utils/key-handling.cjs.map +1 -1
  140. package/dist/utils/key-handling.d.ts +2 -3
  141. package/dist/utils/key-handling.js +142 -53
  142. package/dist/utils/key-handling.js.map +1 -1
  143. package/dist/zustand-store.cjs +216 -51
  144. package/dist/zustand-store.cjs.map +1 -1
  145. package/dist/zustand-store.js +197 -32
  146. package/dist/zustand-store.js.map +1 -1
  147. package/package.json +3 -3
  148. package/src/actions/builtin.ts +65 -0
  149. package/src/actions/clipboard.ts +350 -0
  150. package/src/actions/filters.ts +150 -0
  151. package/src/actions/index.ts +6 -0
  152. package/src/actions/selection.ts +271 -0
  153. package/src/actions/toolbar.module.sass +30 -0
  154. package/src/actions/toolbar.ts +212 -0
  155. package/src/actions/types.ts +159 -0
  156. package/src/cell-renderer.ts +55 -51
  157. package/src/components/colors.ts +22 -19
  158. package/src/components/editor-popup.ts +3 -2
  159. package/src/components/index.ts +1 -0
  160. package/src/components/main.module.sass +1 -0
  161. package/src/components/text-areas.ts +34 -19
  162. package/src/components/view-info.ts +17 -0
  163. package/src/core.ts +243 -170
  164. package/src/index.ts +1 -0
  165. package/src/main.module.sass +14 -2
  166. package/src/notifications.ts +22 -0
  167. package/src/postgrest-table/data-loaders.ts +300 -201
  168. package/src/postgrest-table/index.ts +258 -84
  169. package/src/postgrest-table/lazy-loader-table.ts +18 -17
  170. package/src/postgrest-table/loading-utils.ts +49 -0
  171. package/src/postgrest-table/main.module.sass +16 -0
  172. package/src/provider.ts +63 -20
  173. package/src/renderers/column-header-cell-postgrest.module.sass +71 -0
  174. package/src/renderers/column-header-cell-postgrest.ts +282 -0
  175. package/src/renderers/column-header-cell.ts +388 -0
  176. package/src/renderers/index.ts +2 -0
  177. package/src/table-updates/data-parameter.ts +44 -0
  178. package/src/table-updates/filter.ts +124 -0
  179. package/src/table-updates/index.ts +239 -0
  180. package/src/types.ts +58 -3
  181. package/src/utils/column-spec.ts +47 -0
  182. package/src/utils/key-handling.ts +181 -72
  183. package/src/zustand-store.ts +241 -36
  184. package/dist/provider.d.ts +0 -15
@@ -0,0 +1,388 @@
1
+ /**
2
+ * Column header with client-side sort and filter controls.
3
+ *
4
+ * Mirrors the PostgREST column header UX but operates on in-memory data.
5
+ * Sort state is stored in the DataSheet zustand store; filtering uses the
6
+ * existing `setFilter` / `removeFilter` mechanism with auto-generated
7
+ * predicate filters for simple text search.
8
+ */
9
+ import h from "@macrostrat/hyper";
10
+ import { ColumnHeaderCell } from "@blueprintjs/table";
11
+ import {
12
+ Button,
13
+ ButtonGroup,
14
+ Icon,
15
+ InputGroup,
16
+ Menu,
17
+ Tag as BPTag,
18
+ } from "@blueprintjs/core";
19
+ import { useCallback, useRef } from "react";
20
+ import { useSelector, useStoreAPI } from "../provider";
21
+ import type { ColumnSpec } from "../utils/column-spec";
22
+ import type { ColumnSort, TableFilter } from "../actions/types";
23
+ import type {
24
+ PostgrestColumnFilter,
25
+ ColumnSortEntry,
26
+ } from "../postgrest-table";
27
+
28
+ export interface ColumnActionsConfig {
29
+ activeSort?: ColumnSortEntry | null;
30
+ activeFilter?: PostgrestColumnFilter | null;
31
+ }
32
+
33
+ export interface ColumnHeaderRendererProps extends ColumnActionsConfig {
34
+ col: ColumnSpec;
35
+ colIndex: number;
36
+ //actions: ColumnHeaderActions;
37
+ }
38
+
39
+ // ---- Auto-generated column filter helpers ----
40
+
41
+ /** Build a simple text-contains filter for a column. */
42
+ function buildAutoFilter(col: ColumnSpec): TableFilter {
43
+ return {
44
+ id: autoFilterId(col.key),
45
+ name: col.name,
46
+ columnKey: col.key,
47
+ icon: "filter",
48
+ defaultState: { search: "" },
49
+ predicate(row, state) {
50
+ if (!state?.search) return true;
51
+ const val = String(row[col.key] ?? "").toLowerCase();
52
+ return val.includes(state.search.toLowerCase());
53
+ },
54
+ };
55
+ }
56
+
57
+ /** Stable filter-ID prefix for auto-generated column filters. */
58
+ export function autoFilterId(key: string) {
59
+ return `__col_${key}`;
60
+ }
61
+
62
+ // ---- Column header cell ----
63
+
64
+ /** Client-side column header cell with sort/filter controls.
65
+ * Reads sort state from the store and manages column filters via
66
+ * the existing `setFilter`/`removeFilter` store methods. */
67
+ export function renderColumnHeaderCell({
68
+ col,
69
+ colIndex,
70
+ activeSort,
71
+ activeFilter,
72
+ }: ColumnHeaderRendererProps) {
73
+ return h(ColumnHeaderCell, { name: col.name });
74
+
75
+ const isSortable = col.sortable === true;
76
+ const isFilterable =
77
+ col.filterable === true || typeof col.filterable === "object";
78
+
79
+ if (!isSortable && !isFilterable) {
80
+ return h(ColumnHeaderCell, { name: col.name });
81
+ }
82
+
83
+ const hasFilterActive =
84
+ activeFilter != null &&
85
+ activeFilter.state?.search !== "" &&
86
+ activeFilter.state?.search != null;
87
+
88
+ const hasSortActive = activeSort != null;
89
+
90
+ return h(ColumnHeaderCell, {
91
+ name: col.name,
92
+ nameRenderer: () =>
93
+ h(ColumnHeaderName, { col, hasSortActive, hasFilterActive, activeSort }),
94
+ menuRenderer: () =>
95
+ h(ClientColumnActionsMenu, {
96
+ col,
97
+ isSortable,
98
+ isFilterable,
99
+ activeSort,
100
+ activeFilter,
101
+ }),
102
+ });
103
+ }
104
+
105
+ /** Column name with active sort/filter indicator icons. */
106
+ function ColumnHeaderName({ col, hasSortActive, hasFilterActive, activeSort }) {
107
+ return h(
108
+ "div",
109
+ { style: { display: "flex", alignItems: "center", width: "100%" } },
110
+ [
111
+ h(
112
+ "span",
113
+ {
114
+ style: {
115
+ flex: 1,
116
+ overflow: "hidden",
117
+ textOverflow: "ellipsis",
118
+ whiteSpace: "nowrap",
119
+ },
120
+ },
121
+ col.name,
122
+ ),
123
+ h.if(hasSortActive || hasFilterActive)(
124
+ "span",
125
+ {
126
+ style: {
127
+ display: "flex",
128
+ alignItems: "center",
129
+ gap: "2px",
130
+ marginLeft: "4px",
131
+ flexShrink: 0,
132
+ },
133
+ },
134
+ [
135
+ h.if(hasSortActive)(Icon, {
136
+ icon: activeSort?.ascending ? "sort-asc" : "sort-desc",
137
+ size: 12,
138
+ style: { color: "var(--intent-primary, #2965cc)" },
139
+ }),
140
+ h.if(hasFilterActive)(Icon, {
141
+ icon: "filter",
142
+ size: 12,
143
+ style: { color: "var(--intent-warning, #d99e0b)" },
144
+ }),
145
+ ],
146
+ ),
147
+ ],
148
+ );
149
+ }
150
+
151
+ interface ClientColumnActionsMenuProps extends ColumnActionsConfig {
152
+ col: ColumnSpec;
153
+ isSortable: boolean;
154
+ isFilterable: boolean;
155
+ }
156
+
157
+ /** Menu content rendered inside ColumnHeaderCell's built-in dropdown. */
158
+ function ClientColumnActionsMenu({
159
+ col,
160
+ isSortable,
161
+ isFilterable,
162
+ activeSort,
163
+ activeFilter,
164
+ }) {
165
+ const storeAPI = useStoreAPI();
166
+ const filterInputRef = useRef<HTMLInputElement>(null);
167
+
168
+ const applyFilter = useCallback(() => {
169
+ const val = filterInputRef.current?.value ?? "";
170
+ const store = storeAPI.getState();
171
+ if (val.trim() === "") {
172
+ store.removeFilter(autoFilterId(col.key));
173
+ } else {
174
+ const filter = buildAutoFilter(col);
175
+ store.setFilter(filter.id, filter, { search: val });
176
+ }
177
+ }, [col, storeAPI]);
178
+
179
+ const hasAnyActive =
180
+ activeSort != null ||
181
+ (activeFilter != null &&
182
+ activeFilter.state?.search !== "" &&
183
+ activeFilter.state?.search != null);
184
+
185
+ return h(Menu, { className: "column-actions-menu" }, [
186
+ h("div", { style: { padding: "12px", minWidth: "220px" } }, [
187
+ h.if(isSortable)("div", { style: { marginBottom: "12px" } }, [
188
+ h("div", { style: sortFilterLabelStyle }, "Sort"),
189
+ h("div", { style: { display: "flex", gap: "4px" } }, [
190
+ h(ButtonGroup, { minimal: true }, [
191
+ h(
192
+ Button,
193
+ {
194
+ icon: "sort-asc",
195
+ active: activeSort?.ascending === true,
196
+ intent: activeSort?.ascending === true ? "primary" : "none",
197
+ onClick() {
198
+ const store = storeAPI.getState();
199
+ if (activeSort?.ascending === true) {
200
+ store.setColumnSort(col.key, null);
201
+ } else {
202
+ store.setColumnSort(col.key, true);
203
+ }
204
+ },
205
+ },
206
+ "A→Z",
207
+ ),
208
+ h(
209
+ Button,
210
+ {
211
+ icon: "sort-desc",
212
+ active: activeSort?.ascending === false,
213
+ intent: activeSort?.ascending === false ? "primary" : "none",
214
+ onClick() {
215
+ const store = storeAPI.getState();
216
+ if (activeSort?.ascending === false) {
217
+ store.setColumnSort(col.key, null);
218
+ } else {
219
+ store.setColumnSort(col.key, false);
220
+ }
221
+ },
222
+ },
223
+ "Z→A",
224
+ ),
225
+ ]),
226
+ ]),
227
+ ]),
228
+ h.if(isFilterable)("div", { style: { marginBottom: "12px" } }, [
229
+ h("div", { style: sortFilterLabelStyle }, "Filter"),
230
+ h(
231
+ "div",
232
+ { style: { display: "flex", gap: "4px", alignItems: "center" } },
233
+ [
234
+ h(InputGroup, {
235
+ className: "filter-value-input",
236
+ small: true,
237
+ placeholder: "Search…",
238
+ defaultValue: activeFilterEntry?.state?.search ?? "",
239
+ inputRef: filterInputRef,
240
+ onKeyDown(e) {
241
+ if (e.key === "Enter") {
242
+ applyFilter();
243
+ }
244
+ },
245
+ rightElement: h(Button, {
246
+ icon: "arrow-right",
247
+ minimal: true,
248
+ small: true,
249
+ onClick: applyFilter,
250
+ }),
251
+ }),
252
+ ],
253
+ ),
254
+ ]),
255
+ h.if(hasAnyActive)(
256
+ "div",
257
+ {
258
+ style: {
259
+ display: "flex",
260
+ justifyContent: "flex-end",
261
+ paddingTop: "4px",
262
+ borderTop: "1px solid var(--divider-color, rgba(16, 22, 26, 0.15))",
263
+ },
264
+ },
265
+ [
266
+ h(
267
+ Button,
268
+ {
269
+ small: true,
270
+ minimal: true,
271
+ intent: "danger",
272
+ icon: "cross",
273
+ onClick() {
274
+ const store = storeAPI.getState();
275
+ store.setColumnSort(col.key, null);
276
+ store.removeFilter(autoFilterId(col.key));
277
+ if (filterInputRef.current) {
278
+ filterInputRef.current.value = "";
279
+ }
280
+ },
281
+ },
282
+ "Clear all",
283
+ ),
284
+ ],
285
+ ),
286
+ ]),
287
+ ]);
288
+ }
289
+
290
+ const sortFilterLabelStyle = {
291
+ fontWeight: 600,
292
+ fontSize: "11px",
293
+ textTransform: "uppercase" as const,
294
+ letterSpacing: "0.5px",
295
+ color: "var(--secondary-color, #5c7080)",
296
+ marginBottom: "6px",
297
+ };
298
+
299
+ // ---- Sort/filter state bar ----
300
+
301
+ /** Bar showing active client-side sort and filter state as removable tags.
302
+ * Rendered automatically by `_DataSheet` when sorts or column filters
303
+ * are active. */
304
+ export function SortFilterBar() {
305
+ const columnSorts = useSelector((state) => state.columnSorts);
306
+ const activeFilters = useSelector((state) => state.activeFilters);
307
+ const storeAPI = useStoreAPI();
308
+
309
+ // Collect auto-generated column filters
310
+ const columnFilterEntries: { key: string; search: string }[] = [];
311
+ for (const [id, entry] of activeFilters) {
312
+ if (id.startsWith("__col_") && entry.state?.search) {
313
+ columnFilterEntries.push({
314
+ key: id.replace("__col_", ""),
315
+ search: entry.state.search,
316
+ });
317
+ }
318
+ }
319
+
320
+ const hasAnything = columnSorts.length > 0 || columnFilterEntries.length > 0;
321
+ if (!hasAnything) return null;
322
+
323
+ return h(
324
+ "div",
325
+ {
326
+ style: {
327
+ display: "flex",
328
+ flexDirection: "row",
329
+ flexWrap: "wrap",
330
+ gap: "4px",
331
+ marginBottom: "4px",
332
+ alignItems: "center",
333
+ },
334
+ },
335
+ [
336
+ columnSorts.map((s) =>
337
+ h(
338
+ BPTag,
339
+ {
340
+ key: `sort-${s.key}`,
341
+ icon: s.ascending ? "sort-asc" : "sort-desc",
342
+ intent: "primary",
343
+ onRemove() {
344
+ storeAPI.getState().setColumnSort(s.key, null);
345
+ },
346
+ minimal: true,
347
+ },
348
+ `${s.key}: ${s.ascending ? "A→Z" : "Z→A"}`,
349
+ ),
350
+ ),
351
+ columnFilterEntries.map((f) =>
352
+ h(
353
+ BPTag,
354
+ {
355
+ key: `filter-${f.key}`,
356
+ icon: "filter",
357
+ intent: "warning",
358
+ onRemove() {
359
+ storeAPI.getState().removeFilter(autoFilterId(f.key));
360
+ },
361
+ minimal: true,
362
+ },
363
+ `${f.key} contains "${f.search}"`,
364
+ ),
365
+ ),
366
+ h(
367
+ Button,
368
+ {
369
+ minimal: true,
370
+ small: true,
371
+ icon: "cross",
372
+ onClick() {
373
+ const store = storeAPI.getState();
374
+ // Clear all column sorts
375
+ store.clearColumnSorts();
376
+ // Clear auto-generated column filters
377
+ for (const [id] of activeFilters) {
378
+ if (id.startsWith("__col_")) {
379
+ store.removeFilter(id);
380
+ }
381
+ }
382
+ },
383
+ },
384
+ "Clear all",
385
+ ),
386
+ ],
387
+ );
388
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./column-header-cell";
2
+ export * from "./column-header-cell-postgrest";
@@ -0,0 +1,44 @@
1
+ import { Filter } from "./filter";
2
+
3
+ interface DataParameters {
4
+ group?: string;
5
+ select: {
6
+ page?: string;
7
+ pageSize?: string;
8
+ };
9
+ filter: {
10
+ [key: string]: Filter; // Used for filters
11
+ };
12
+ }
13
+
14
+ const cloneDataParameters = (
15
+ dataParameters: DataParameters
16
+ ): DataParameters => {
17
+ return {
18
+ group: dataParameters?.group,
19
+ select: { ...dataParameters?.select },
20
+ filter: Object.entries(dataParameters?.filter).reduce(
21
+ (acc, [key, value]) => {
22
+ acc[key] = value.clone();
23
+ return acc;
24
+ },
25
+ {}
26
+ ),
27
+ };
28
+ };
29
+
30
+ /*
31
+ * Check if a column is active in the data parameters
32
+ */
33
+ export const isColumnActive = (
34
+ dataParameters: DataParameters,
35
+ column: string
36
+ ) => {
37
+ return (
38
+ (column in dataParameters.filter &&
39
+ dataParameters.filter[column].is_valid()) ||
40
+ column == dataParameters?.group
41
+ );
42
+ };
43
+
44
+ export { cloneDataParameters, DataParameters };
@@ -0,0 +1,124 @@
1
+ import { ColumnOperators } from "../tables/defs";
2
+
3
+ export class Filter {
4
+ readonly column_name: string;
5
+ readonly operator: ColumnOperators | undefined;
6
+ readonly value: string | boolean | number | null;
7
+
8
+ constructor(
9
+ column_name: string,
10
+ operator: ColumnOperators | undefined,
11
+ value: string | boolean | number | null
12
+ ) {
13
+ this.column_name = column_name;
14
+ this.operator = operator;
15
+ this.value = value;
16
+ }
17
+
18
+ get formattedValue() {
19
+ switch (this.operator) {
20
+ case "in":
21
+ return `(${this.value})`;
22
+ default:
23
+ return this.value;
24
+ }
25
+ }
26
+
27
+ get urlValue() {
28
+ return `${this.operator}.${this.formattedValue}`;
29
+ }
30
+
31
+ clone = () => {
32
+ return new Filter(this.column_name, this.operator, this.value);
33
+ };
34
+
35
+ passes = (data: Record<string, string | boolean | number | null>) => {
36
+ const filterValue = data[this.column_name];
37
+ switch (this.operator) {
38
+ case "eq":
39
+ return filterValue == this.value;
40
+ case "lt":
41
+ return filterValue < this.value;
42
+ case "le":
43
+ return filterValue <= this.value;
44
+ case "gt":
45
+ return filterValue > this.value;
46
+ case "ge":
47
+ return filterValue >= this.value;
48
+ case "ne":
49
+ return filterValue != this.value;
50
+ case "is_distinct_from":
51
+ return filterValue != this.value;
52
+ case "is_not_distinct_from":
53
+ return filterValue == this.value;
54
+ case "like":
55
+ if (typeof filterValue != "string" || typeof this.value != "string") {
56
+ return false;
57
+ }
58
+ return filterValue.includes(this.value);
59
+ case "in":
60
+ if (typeof filterValue != "string" || typeof this.value != "string") {
61
+ return false;
62
+ }
63
+ return this.value.includes(filterValue);
64
+ case "is":
65
+ return filterValue == this.value;
66
+ default:
67
+ return false;
68
+ }
69
+ };
70
+
71
+ is_valid = () => {
72
+ if (this.operator == undefined) {
73
+ return false;
74
+ }
75
+ return true;
76
+ };
77
+
78
+ to_array = () => {
79
+ return [this.column_name, this.operator + "." + this.formattedValue];
80
+ };
81
+ }
82
+
83
+ /*
84
+ * Convert url and filter to a parameterized URL
85
+ */
86
+ export const addFilterToURL = (url: URL, filter: Filter) => {
87
+ if (filter.is_valid()) {
88
+ const [columnName, filterValue] = filter.to_array();
89
+ url.searchParams.append(columnName, filterValue);
90
+ }
91
+ return url;
92
+ };
93
+
94
+ /*
95
+ * Sort the filters by column name and stringify
96
+ */
97
+ export const createFiltersKey = (filters: Filter[]) => {
98
+ const sortFilterEntries = (a: Filter, b: Filter) => {
99
+ return a.column_name.localeCompare(b.column_name);
100
+ };
101
+
102
+ return filters
103
+ .filter((filter) => filter.is_valid())
104
+ .sort(sortFilterEntries)
105
+ .map((filter) => filter.urlValue)
106
+ .join("&");
107
+ };
108
+
109
+ export const rowPassesFilters = (
110
+ row: Record<string, string | boolean | number | null>,
111
+ filters: Filter[] | undefined
112
+ ) => {
113
+ for (const filter of filters) {
114
+ // If the filter is invalid skip it
115
+ if (!filter.is_valid()) {
116
+ continue;
117
+ }
118
+
119
+ if (!filter.passes(row)) {
120
+ return false;
121
+ }
122
+ }
123
+ return true;
124
+ };