@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.
- package/CHANGELOG.md +5 -0
- package/dist/actions/builtin.cjs +56 -0
- package/dist/actions/builtin.cjs.map +1 -0
- package/dist/actions/builtin.d.ts +15 -0
- package/dist/actions/builtin.js +56 -0
- package/dist/actions/builtin.js.map +1 -0
- package/dist/actions/clipboard.cjs +250 -0
- package/dist/actions/clipboard.cjs.map +1 -0
- package/dist/actions/clipboard.d.ts +42 -0
- package/dist/actions/clipboard.js +250 -0
- package/dist/actions/clipboard.js.map +1 -0
- package/dist/actions/filters.cjs +123 -0
- package/dist/actions/filters.cjs.map +1 -0
- package/dist/actions/filters.d.ts +6 -0
- package/dist/actions/filters.js +123 -0
- package/dist/actions/filters.js.map +1 -0
- package/dist/actions/index.d.ts +6 -0
- package/dist/actions/selection.cjs +194 -0
- package/dist/actions/selection.cjs.map +1 -0
- package/dist/actions/selection.d.ts +40 -0
- package/dist/actions/selection.js +192 -0
- package/dist/actions/selection.js.map +1 -0
- package/dist/actions/toolbar.cjs +171 -0
- package/dist/actions/toolbar.cjs.map +1 -0
- package/dist/actions/toolbar.d.ts +10 -0
- package/dist/actions/toolbar.js +171 -0
- package/dist/actions/toolbar.js.map +1 -0
- package/dist/actions/toolbar.module.sass.cjs +22 -0
- package/dist/actions/toolbar.module.sass.cjs.map +1 -0
- package/dist/actions/toolbar.module.sass.js +20 -0
- package/dist/actions/toolbar.module.sass.js.map +1 -0
- package/dist/actions/types.d.ts +143 -0
- package/dist/cell-renderer.cjs +44 -41
- package/dist/cell-renderer.cjs.map +1 -1
- package/dist/cell-renderer.d.ts +1 -6
- package/dist/cell-renderer.js +45 -42
- package/dist/cell-renderer.js.map +1 -1
- package/dist/components/actions.module.sass.cjs +3 -3
- package/dist/components/actions.module.sass.js +2 -2
- package/dist/components/colors.cjs +20 -16
- package/dist/components/colors.cjs.map +1 -1
- package/dist/components/colors.d.ts +4 -3
- package/dist/components/colors.js +20 -16
- package/dist/components/colors.js.map +1 -1
- package/dist/components/editor-popup.cjs +4 -3
- package/dist/components/editor-popup.cjs.map +1 -1
- package/dist/components/editor-popup.js +4 -3
- package/dist/components/editor-popup.js.map +1 -1
- package/dist/components/index.d.ts +1 -0
- package/dist/components/main.module.sass.cjs +12 -12
- package/dist/components/main.module.sass.js +11 -11
- package/dist/components/text-areas.cjs +23 -18
- package/dist/components/text-areas.cjs.map +1 -1
- package/dist/components/text-areas.d.ts +8 -4
- package/dist/components/text-areas.js +20 -13
- package/dist/components/text-areas.js.map +1 -1
- package/dist/components/view-info.cjs +18 -0
- package/dist/components/view-info.cjs.map +1 -0
- package/dist/components/view-info.d.ts +1 -0
- package/dist/components/view-info.js +18 -0
- package/dist/components/view-info.js.map +1 -0
- package/dist/core.cjs +184 -156
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.ts +14 -1
- package/dist/core.js +187 -157
- package/dist/core.js.map +1 -1
- package/dist/data-sheet.css +181 -43
- package/dist/index.cjs +43 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +43 -10
- package/dist/index.js.map +1 -1
- package/dist/main.module.sass.cjs +15 -14
- package/dist/main.module.sass.cjs.map +1 -1
- package/dist/main.module.sass.js +14 -13
- package/dist/main.module.sass.js.map +1 -1
- package/dist/notifications.cjs +19 -0
- package/dist/notifications.cjs.map +1 -0
- package/dist/notifications.d.ts +9 -0
- package/dist/notifications.js +19 -0
- package/dist/notifications.js.map +1 -0
- package/dist/postgrest-table/data-loaders.cjs +181 -104
- package/dist/postgrest-table/data-loaders.cjs.map +1 -1
- package/dist/postgrest-table/data-loaders.d.ts +52 -28
- package/dist/postgrest-table/data-loaders.js +181 -104
- package/dist/postgrest-table/data-loaders.js.map +1 -1
- package/dist/postgrest-table/index.cjs +205 -79
- package/dist/postgrest-table/index.cjs.map +1 -1
- package/dist/postgrest-table/index.d.ts +13 -16
- package/dist/postgrest-table/index.js +186 -58
- package/dist/postgrest-table/index.js.map +1 -1
- package/dist/postgrest-table/lazy-loader-table.cjs +18 -17
- package/dist/postgrest-table/lazy-loader-table.cjs.map +1 -1
- package/dist/postgrest-table/lazy-loader-table.d.ts +1 -1
- package/dist/postgrest-table/lazy-loader-table.js +17 -14
- package/dist/postgrest-table/lazy-loader-table.js.map +1 -1
- package/dist/postgrest-table/loading-utils.cjs +16 -0
- package/dist/postgrest-table/loading-utils.cjs.map +1 -0
- package/dist/postgrest-table/loading-utils.d.ts +13 -0
- package/dist/postgrest-table/loading-utils.js +16 -0
- package/dist/postgrest-table/loading-utils.js.map +1 -0
- package/dist/postgrest-table/main.module.sass.cjs +18 -13
- package/dist/postgrest-table/main.module.sass.cjs.map +1 -1
- package/dist/postgrest-table/main.module.sass.js +18 -13
- package/dist/postgrest-table/main.module.sass.js.map +1 -1
- package/dist/provider.cjs +58 -26
- package/dist/provider.cjs.map +1 -1
- package/dist/provider.js +57 -23
- package/dist/provider.js.map +1 -1
- package/dist/renderers/column-header-cell-postgrest.cjs +216 -0
- package/dist/renderers/column-header-cell-postgrest.cjs.map +1 -0
- package/dist/renderers/column-header-cell-postgrest.d.ts +13 -0
- package/dist/renderers/column-header-cell-postgrest.js +216 -0
- package/dist/renderers/column-header-cell-postgrest.js.map +1 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.cjs +23 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.cjs.map +1 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.js +22 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.js.map +1 -0
- package/dist/renderers/column-header-cell.cjs +103 -0
- package/dist/renderers/column-header-cell.cjs.map +1 -0
- package/dist/renderers/column-header-cell.d.ts +30 -0
- package/dist/renderers/column-header-cell.js +101 -0
- package/dist/renderers/column-header-cell.js.map +1 -0
- package/dist/renderers/index.d.ts +2 -0
- package/dist/table-updates/data-parameter.d.ts +14 -0
- package/dist/table-updates/filter.d.ts +16 -0
- package/dist/table-updates/index.d.ts +19 -0
- package/dist/types.cjs +9 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.ts +58 -4
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/column-spec.cjs +9 -1
- package/dist/utils/column-spec.cjs.map +1 -1
- package/dist/utils/column-spec.d.ts +21 -0
- package/dist/utils/column-spec.js +9 -1
- package/dist/utils/column-spec.js.map +1 -1
- package/dist/utils/key-handling.cjs +141 -52
- package/dist/utils/key-handling.cjs.map +1 -1
- package/dist/utils/key-handling.d.ts +2 -3
- package/dist/utils/key-handling.js +142 -53
- package/dist/utils/key-handling.js.map +1 -1
- package/dist/zustand-store.cjs +216 -51
- package/dist/zustand-store.cjs.map +1 -1
- package/dist/zustand-store.js +197 -32
- package/dist/zustand-store.js.map +1 -1
- package/package.json +3 -3
- package/src/actions/builtin.ts +65 -0
- package/src/actions/clipboard.ts +350 -0
- package/src/actions/filters.ts +150 -0
- package/src/actions/index.ts +6 -0
- package/src/actions/selection.ts +271 -0
- package/src/actions/toolbar.module.sass +30 -0
- package/src/actions/toolbar.ts +212 -0
- package/src/actions/types.ts +159 -0
- package/src/cell-renderer.ts +55 -51
- package/src/components/colors.ts +22 -19
- package/src/components/editor-popup.ts +3 -2
- package/src/components/index.ts +1 -0
- package/src/components/main.module.sass +1 -0
- package/src/components/text-areas.ts +34 -19
- package/src/components/view-info.ts +17 -0
- package/src/core.ts +243 -170
- package/src/index.ts +1 -0
- package/src/main.module.sass +14 -2
- package/src/notifications.ts +22 -0
- package/src/postgrest-table/data-loaders.ts +300 -201
- package/src/postgrest-table/index.ts +258 -84
- package/src/postgrest-table/lazy-loader-table.ts +18 -17
- package/src/postgrest-table/loading-utils.ts +49 -0
- package/src/postgrest-table/main.module.sass +16 -0
- package/src/provider.ts +63 -20
- package/src/renderers/column-header-cell-postgrest.module.sass +71 -0
- package/src/renderers/column-header-cell-postgrest.ts +282 -0
- package/src/renderers/column-header-cell.ts +388 -0
- package/src/renderers/index.ts +2 -0
- package/src/table-updates/data-parameter.ts +44 -0
- package/src/table-updates/filter.ts +124 -0
- package/src/table-updates/index.ts +239 -0
- package/src/types.ts +58 -3
- package/src/utils/column-spec.ts +47 -0
- package/src/utils/key-handling.ts +181 -72
- package/src/zustand-store.ts +241 -36
- package/dist/provider.d.ts +0 -15
|
@@ -2,58 +2,105 @@
|
|
|
2
2
|
|
|
3
3
|
import { useAsyncEffect } from "@macrostrat/ui-components";
|
|
4
4
|
import { debounce, range } from "underscore";
|
|
5
|
-
import { useCallback,
|
|
5
|
+
import { useCallback, useEffect, useMemo, useRef } from "react";
|
|
6
6
|
import update, { Spec } from "immutability-helper";
|
|
7
|
-
|
|
8
|
-
interface ChunkIndex {
|
|
9
|
-
startRow: number;
|
|
10
|
-
endRow: number;
|
|
11
|
-
lastValue: any;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
7
|
import {
|
|
15
8
|
PostgrestClient,
|
|
16
|
-
PostgrestQueryBuilder,
|
|
17
9
|
PostgrestFilterBuilder,
|
|
10
|
+
PostgrestQueryBuilder,
|
|
18
11
|
} from "@supabase/postgrest-js";
|
|
12
|
+
import { adjustArraySize, RowRegion, sleep } from "./loading-utils.ts";
|
|
13
|
+
import { ctx, tableDataAtom } from "../provider.ts";
|
|
14
|
+
import { atom } from "jotai";
|
|
15
|
+
import h from "./main.module.sass";
|
|
19
16
|
|
|
20
|
-
interface
|
|
21
|
-
data: (T | null)[];
|
|
17
|
+
interface LazyLoaderStateCore<T> {
|
|
22
18
|
loading: boolean;
|
|
23
19
|
error: Error | null;
|
|
24
|
-
visibleRegion: RowRegion;
|
|
25
20
|
initialized: boolean;
|
|
26
21
|
}
|
|
27
22
|
|
|
23
|
+
interface LazyLoaderState<T> extends LazyLoaderStateCore<T> {
|
|
24
|
+
data: (T | null)[];
|
|
25
|
+
visibleRegion: RowRegion;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
28
|
export interface PostgrestOrder<T> {
|
|
29
29
|
key: string;
|
|
30
30
|
ascending?: boolean;
|
|
31
31
|
nullsFirst?: boolean;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
/** Operators available for server-side column filtering via PostgREST. */
|
|
35
|
+
export type PostgrestFilterOperator =
|
|
36
|
+
| "eq"
|
|
37
|
+
| "neq"
|
|
38
|
+
| "like"
|
|
39
|
+
| "ilike"
|
|
40
|
+
| "gt"
|
|
41
|
+
| "lt"
|
|
42
|
+
| "gte"
|
|
43
|
+
| "lte"
|
|
44
|
+
| "is";
|
|
45
|
+
|
|
46
|
+
/** A single column sort entry. */
|
|
47
|
+
export interface ColumnSortEntry {
|
|
48
|
+
key: string;
|
|
49
|
+
ascending: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A single column filter entry. */
|
|
53
|
+
export interface PostgrestColumnFilter {
|
|
54
|
+
key: string;
|
|
55
|
+
operator: PostgrestFilterOperator;
|
|
56
|
+
value: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface PostgrestFilter {
|
|
60
|
+
type: "filter";
|
|
61
|
+
apply(
|
|
62
|
+
req: PostgrestFilterBuilder<any, any, any, any>,
|
|
63
|
+
): PostgrestFilterBuilder<any, any, any, any>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function standardizeFilter(
|
|
67
|
+
columnFilter: PostgrestColumnFilter,
|
|
68
|
+
): PostgrestFilter {
|
|
69
|
+
return {
|
|
70
|
+
type: "filter",
|
|
71
|
+
apply(req) {
|
|
72
|
+
return applyColumnFilters(req, [columnFilter]);
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function applyColumnFilters(
|
|
78
|
+
req: PostgrestFilterBuilder<any, any, any, any>,
|
|
79
|
+
filters: PostgrestColumnFilter[],
|
|
80
|
+
) {
|
|
81
|
+
for (const cf of filters) {
|
|
82
|
+
if (cf.value === "" || cf.value == null) continue;
|
|
83
|
+
const val =
|
|
84
|
+
cf.operator === "like" || cf.operator === "ilike"
|
|
85
|
+
? `*${cf.value}*`
|
|
86
|
+
: cf.value;
|
|
87
|
+
req = req.filter(cf.key, cf.operator, val);
|
|
88
|
+
}
|
|
89
|
+
return req;
|
|
90
|
+
}
|
|
91
|
+
|
|
34
92
|
type LazyLoaderAction<T> =
|
|
35
93
|
| { type: "start-loading" }
|
|
36
94
|
| { type: "loaded"; data: T[]; offset: number; totalSize: number }
|
|
37
95
|
| { type: "error"; error: Error }
|
|
38
|
-
| { type: "set-visible"; region: RowRegion }
|
|
39
96
|
| { type: "update-data"; changes: Spec<T[]> }
|
|
40
|
-
| { type: "reset" }
|
|
41
|
-
|
|
42
|
-
function adjustArraySize<T>(arr: T[], newSize: number) {
|
|
43
|
-
if (newSize == null || arr.length === newSize) {
|
|
44
|
-
return arr;
|
|
45
|
-
} else if (arr.length > newSize) {
|
|
46
|
-
// Trim the array
|
|
47
|
-
arr = arr.slice(0, newSize);
|
|
48
|
-
}
|
|
49
|
-
return [...arr, ...Array(newSize - arr.length).fill(null)];
|
|
50
|
-
}
|
|
97
|
+
| { type: "reset" }
|
|
98
|
+
| { type: "start-reload" };
|
|
51
99
|
|
|
52
100
|
function lazyLoadingReducer<T>(
|
|
53
101
|
state: LazyLoaderState<T>,
|
|
54
102
|
action: LazyLoaderAction<T>,
|
|
55
103
|
): LazyLoaderState<T> {
|
|
56
|
-
console.log(action);
|
|
57
104
|
switch (action.type) {
|
|
58
105
|
case "start-loading":
|
|
59
106
|
return {
|
|
@@ -61,11 +108,6 @@ function lazyLoadingReducer<T>(
|
|
|
61
108
|
loading: true,
|
|
62
109
|
initialized: true,
|
|
63
110
|
};
|
|
64
|
-
case "set-visible":
|
|
65
|
-
return {
|
|
66
|
-
...state,
|
|
67
|
-
visibleRegion: action.region,
|
|
68
|
-
};
|
|
69
111
|
case "update-data":
|
|
70
112
|
return {
|
|
71
113
|
...state,
|
|
@@ -74,24 +116,32 @@ function lazyLoadingReducer<T>(
|
|
|
74
116
|
};
|
|
75
117
|
case "loaded":
|
|
76
118
|
let data = adjustArraySize(state.data, action.totalSize);
|
|
119
|
+
const newData = action.data ?? [];
|
|
120
|
+
const offset = action.offset ?? 0;
|
|
77
121
|
data = [
|
|
78
|
-
...data.slice(0,
|
|
79
|
-
...
|
|
80
|
-
...data.slice(
|
|
122
|
+
...data.slice(0, offset),
|
|
123
|
+
...newData,
|
|
124
|
+
...data.slice(offset + newData.length),
|
|
81
125
|
];
|
|
82
|
-
|
|
83
126
|
return {
|
|
84
127
|
...state,
|
|
85
128
|
data,
|
|
86
129
|
loading: false,
|
|
87
130
|
};
|
|
88
|
-
|
|
131
|
+
case "start-reload":
|
|
132
|
+
// Preserve the table dimensions but show ghost/skeleton cells
|
|
133
|
+
return {
|
|
134
|
+
...state,
|
|
135
|
+
data: new Array(state.data.length).fill(null),
|
|
136
|
+
loading: true,
|
|
137
|
+
initialized: false,
|
|
138
|
+
};
|
|
89
139
|
case "reset":
|
|
90
140
|
return {
|
|
141
|
+
visibleRegion: defaultVisibleRegion,
|
|
91
142
|
data: [],
|
|
92
143
|
loading: false,
|
|
93
144
|
error: null,
|
|
94
|
-
visibleRegion: { rowIndexStart: 0, rowIndexEnd: 0 },
|
|
95
145
|
initialized: false,
|
|
96
146
|
};
|
|
97
147
|
case "error":
|
|
@@ -105,64 +155,35 @@ function lazyLoadingReducer<T>(
|
|
|
105
155
|
}
|
|
106
156
|
}
|
|
107
157
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
"up",
|
|
115
|
-
"down",
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function overlapsNulls(data: any[], region: RowRegion) {
|
|
119
|
-
for (let i = region.rowIndexStart; i < region.rowIndexEnd; i++) {
|
|
120
|
-
if (data[i] == null) {
|
|
121
|
-
return true;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
return false;
|
|
125
|
-
}
|
|
158
|
+
type SortAndFilterOptions = {
|
|
159
|
+
/** Column-level sort state, managed externally. When provided, overrides
|
|
160
|
+
* the `order` prop for query building. */
|
|
161
|
+
order?: PostgrestOrder<any>[];
|
|
162
|
+
filters?: PostgrestFilter[];
|
|
163
|
+
};
|
|
126
164
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
): number {
|
|
133
|
-
let i = start;
|
|
134
|
-
while (i < data.length && i > 0 && limit > 0) {
|
|
135
|
-
if (data[i] != null) {
|
|
136
|
-
return i;
|
|
137
|
-
}
|
|
138
|
-
i += direction === LoadDirection.down ? 1 : -1;
|
|
139
|
-
limit -= 1;
|
|
140
|
-
}
|
|
141
|
-
return i;
|
|
142
|
-
}
|
|
165
|
+
type LazyLoaderOptions = Omit<QueryConfig, "count" | "offset" | "limit"> &
|
|
166
|
+
SortAndFilterOptions & {
|
|
167
|
+
chunkSize?: number;
|
|
168
|
+
sortKey?: string;
|
|
169
|
+
};
|
|
143
170
|
|
|
144
|
-
interface QueryConfig {
|
|
171
|
+
interface QueryConfig extends SortAndFilterOptions {
|
|
145
172
|
columns?: string | string[];
|
|
146
173
|
count?: "exact" | "estimated";
|
|
147
174
|
limit?: number;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
fullTextSearch?: string;
|
|
152
|
-
filter?: (
|
|
153
|
-
query: PostgrestFilterBuilder<any, any, any>,
|
|
154
|
-
) => PostgrestFilterBuilder<any, any, any>;
|
|
175
|
+
identityKey: string;
|
|
176
|
+
lastLoadedRow?: any;
|
|
177
|
+
lastLoadedRowIndex: number;
|
|
155
178
|
}
|
|
156
179
|
|
|
157
180
|
function buildQuery<T>(
|
|
158
|
-
client: PostgrestQueryBuilder<T, any>,
|
|
181
|
+
client: PostgrestQueryBuilder<T, any, any>,
|
|
159
182
|
config: QueryConfig,
|
|
160
183
|
) {
|
|
161
|
-
const { columns = "*", count } = config;
|
|
184
|
+
const { columns = "*", count, lastLoadedRowIndex } = config;
|
|
162
185
|
const opts = { count };
|
|
163
186
|
|
|
164
|
-
console.log("config", config);
|
|
165
|
-
|
|
166
187
|
let cols: string;
|
|
167
188
|
if (Array.isArray(columns)) {
|
|
168
189
|
cols = columns.join(", ");
|
|
@@ -172,51 +193,182 @@ function buildQuery<T>(
|
|
|
172
193
|
|
|
173
194
|
let query = client.select(cols, opts);
|
|
174
195
|
|
|
175
|
-
|
|
176
|
-
|
|
196
|
+
const filters = config.filters ?? [];
|
|
197
|
+
for (const filter of filters) {
|
|
198
|
+
query = filter.apply(query);
|
|
177
199
|
}
|
|
178
|
-
console.log("query", query.url.search);
|
|
179
200
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
201
|
+
const orderClauses = buildPostgrestOrderClauses(
|
|
202
|
+
config.order ?? [],
|
|
203
|
+
config.identityKey,
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
let hasOrdering = false;
|
|
207
|
+
for (const clause of orderClauses) {
|
|
208
|
+
query = query.order(clause.key, clause);
|
|
209
|
+
if (config.lastLoadedRow != null) {
|
|
210
|
+
const op = (clause.ascending ?? true) ? "gt" : "lt";
|
|
211
|
+
const value = config.lastLoadedRow[clause.key];
|
|
212
|
+
query = query[op](clause.key, value);
|
|
186
213
|
}
|
|
214
|
+
hasOrdering = true;
|
|
187
215
|
}
|
|
216
|
+
|
|
217
|
+
let offset = null;
|
|
218
|
+
if (!hasOrdering && lastLoadedRowIndex > 0) {
|
|
219
|
+
// We need to load based on offsets
|
|
220
|
+
offset = lastLoadedRowIndex + 1;
|
|
221
|
+
}
|
|
222
|
+
|
|
188
223
|
if (config.limit != null) {
|
|
189
|
-
if (
|
|
190
|
-
query = query.range(
|
|
191
|
-
console.log(`Random seek from ${
|
|
224
|
+
if (offset != null) {
|
|
225
|
+
query = query.range(offset, offset + config.limit - 1);
|
|
226
|
+
console.log(`Random seek from ${offset}, this will be slow`);
|
|
192
227
|
} else {
|
|
193
228
|
query = query.limit(config.limit);
|
|
194
229
|
}
|
|
195
230
|
}
|
|
231
|
+
|
|
232
|
+
console.log("query", query.url.search);
|
|
196
233
|
return query;
|
|
197
234
|
}
|
|
198
235
|
|
|
199
|
-
|
|
236
|
+
const defaultVisibleRegion: RowRegion = {
|
|
237
|
+
rowIndexStart: 0,
|
|
238
|
+
rowIndexEnd: 0,
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
/** Atom to house the current visible region of the table */
|
|
242
|
+
const visibleRegionAtom = atom<RowRegion>(defaultVisibleRegion);
|
|
243
|
+
|
|
244
|
+
const lazyLoaderCoreStateAtom = atom<LazyLoaderCoreState<any>>({
|
|
245
|
+
loading: false,
|
|
246
|
+
error: null,
|
|
247
|
+
initialized: false,
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
export interface ViewInfo {
|
|
251
|
+
visibleRegion: RowRegion;
|
|
252
|
+
totalCount: number | null;
|
|
253
|
+
loading: boolean;
|
|
254
|
+
error: Error | null;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export const viewInfoAtom = atom<ViewInfo>((get) => {
|
|
258
|
+
const state = get(lazyLoaderStateAtom);
|
|
259
|
+
return {
|
|
260
|
+
visibleRegion: state.visibleRegion,
|
|
261
|
+
loading: state.loading,
|
|
262
|
+
error: state.error,
|
|
263
|
+
totalCount: state.data.length,
|
|
264
|
+
};
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
/** Temporary passthrough atom to allow visible region to be separated from the rest
|
|
268
|
+
* of the lazy loader state
|
|
269
|
+
*/
|
|
270
|
+
const lazyLoaderStateAtom = atom(
|
|
271
|
+
(get): LazyLoaderState<any> => {
|
|
272
|
+
const core = get(lazyLoaderCoreStateAtom);
|
|
273
|
+
const data = get(tableDataAtom);
|
|
274
|
+
const visibleRegion = get(visibleRegionAtom);
|
|
275
|
+
return {
|
|
276
|
+
...core,
|
|
277
|
+
data,
|
|
278
|
+
visibleRegion,
|
|
279
|
+
};
|
|
280
|
+
},
|
|
281
|
+
(get, set, update: any) => {
|
|
282
|
+
// Split the state between several places
|
|
283
|
+
if (typeof update === "function") {
|
|
284
|
+
update = update(get(lazyLoaderStateAtom));
|
|
285
|
+
}
|
|
286
|
+
const { visibleRegion, data, ...rest } = update;
|
|
287
|
+
if (visibleRegion != null) {
|
|
288
|
+
set(visibleRegionAtom, visibleRegion);
|
|
289
|
+
}
|
|
290
|
+
if (data != null && data.length > 0) {
|
|
291
|
+
set(tableDataAtom, data);
|
|
292
|
+
}
|
|
293
|
+
if (Object.keys(rest).length > 1) {
|
|
294
|
+
set(lazyLoaderCoreStateAtom, (v) => ({ ...v, ...rest }));
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
function useLazyLoaderReducer() {
|
|
300
|
+
const [state, setState] = ctx.use(lazyLoaderStateAtom);
|
|
301
|
+
const dispatch = useCallback(
|
|
302
|
+
(action: LazyLoaderAction<any>) =>
|
|
303
|
+
setState((prev) => lazyLoadingReducer(prev, action)),
|
|
304
|
+
[setState],
|
|
305
|
+
);
|
|
306
|
+
return [state, dispatch] as const;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function buildPostgrestOrderClauses(
|
|
310
|
+
sorts: (ColumnSortEntry | PostgrestOrder<any>)[],
|
|
311
|
+
identityOrder: PostgrestOrder<any> | string,
|
|
312
|
+
): PostgrestOrder<any>[] {
|
|
313
|
+
// Clause to build ordering for a Postgrest table.
|
|
314
|
+
// There must be an ordering by identity key, and it must be last...
|
|
315
|
+
const identitySort =
|
|
316
|
+
typeof identityOrder == "string"
|
|
317
|
+
? { key: identityOrder, ascending: true }
|
|
318
|
+
: identityOrder;
|
|
319
|
+
const identityKey = identitySort.key;
|
|
320
|
+
const clauses: PostgrestOrder<any>[] = sorts.map((sort) => {
|
|
321
|
+
return {
|
|
322
|
+
key: sort.key,
|
|
323
|
+
ascending: sort.ascending,
|
|
324
|
+
nullsFirst: "nullsFirst" in sort ? sort.nullsFirst : false,
|
|
325
|
+
};
|
|
326
|
+
});
|
|
327
|
+
// sort the clauses so the identity clause is last
|
|
328
|
+
const hasIdentityClause = clauses.some((d) => d.key === identityKey);
|
|
329
|
+
if (hasIdentityClause) {
|
|
330
|
+
clauses.sort((a, b) => {
|
|
331
|
+
if (a.key === identityKey) return 1;
|
|
332
|
+
if (b.key === identityKey) return -1;
|
|
333
|
+
return 0;
|
|
334
|
+
});
|
|
335
|
+
} else {
|
|
336
|
+
clauses.push(identitySort);
|
|
337
|
+
}
|
|
338
|
+
return clauses;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function _loadMorePostgRESTData<T>(
|
|
200
342
|
client: PostgrestQueryBuilder<T, any>,
|
|
201
|
-
config: QueryConfig & { chunkSize
|
|
343
|
+
config: QueryConfig & { chunkSize?: number },
|
|
202
344
|
state: LazyLoaderState<T>,
|
|
203
345
|
dispatch: any,
|
|
204
346
|
) {
|
|
205
|
-
|
|
347
|
+
let rowIndex = indexOfFirstNullInRegion(state.data, state.visibleRegion);
|
|
206
348
|
if (state.loading || rowIndex == null) {
|
|
207
349
|
if (state.initialized) {
|
|
208
350
|
return;
|
|
209
351
|
}
|
|
352
|
+
// For initial/reload queries, default to loading from the start
|
|
353
|
+
rowIndex = rowIndex ?? 0;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const lastLoadedRowIndex: number = rowIndex - 1;
|
|
357
|
+
let lastLoadedRow = null;
|
|
358
|
+
if (lastLoadedRowIndex >= 0) {
|
|
359
|
+
lastLoadedRow = state.data[lastLoadedRowIndex];
|
|
210
360
|
}
|
|
211
361
|
|
|
212
362
|
const { chunkSize = 100, ...rest } = config;
|
|
213
363
|
|
|
364
|
+
// Determine the primary sort key from column sorts or the order prop
|
|
214
365
|
const sortKey = config.order?.key ?? "id";
|
|
215
366
|
|
|
216
367
|
let cfg: QueryConfig = {
|
|
217
368
|
...rest,
|
|
218
369
|
limit: chunkSize,
|
|
219
|
-
|
|
370
|
+
lastLoadedRow,
|
|
371
|
+
lastLoadedRowIndex,
|
|
220
372
|
};
|
|
221
373
|
|
|
222
374
|
// Allows random seeking
|
|
@@ -225,14 +377,6 @@ function _loadMoreData<T>(
|
|
|
225
377
|
cfg.count = "exact";
|
|
226
378
|
}
|
|
227
379
|
|
|
228
|
-
// This only works for forward queries
|
|
229
|
-
if (!isInitialQuery) {
|
|
230
|
-
cfg.after = state.data[rowIndex - 1]?.[sortKey];
|
|
231
|
-
if (cfg.after == null) {
|
|
232
|
-
cfg.offset = rowIndex;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
380
|
dispatch({ type: "start-loading" });
|
|
237
381
|
|
|
238
382
|
const query = buildQuery(client, cfg);
|
|
@@ -250,48 +394,16 @@ function _loadMoreData<T>(
|
|
|
250
394
|
}
|
|
251
395
|
|
|
252
396
|
// Ensure only one data load is in progress at a time
|
|
253
|
-
const
|
|
254
|
-
|
|
255
|
-
type LazyLoaderOptions = Omit<QueryConfig, "count" | "offset" | "limit"> & {
|
|
256
|
-
chunkSize?: number;
|
|
257
|
-
sortKey?: string;
|
|
258
|
-
filter?: (
|
|
259
|
-
query: PostgrestFilterBuilder<any, any, any>,
|
|
260
|
-
) => PostgrestFilterBuilder<any, any, any>;
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
export function usePostgRESTLazyLoader(
|
|
264
|
-
endpoint: string,
|
|
265
|
-
table: string,
|
|
266
|
-
config: LazyLoaderOptions = {},
|
|
267
|
-
) {
|
|
268
|
-
const initialState: LazyLoaderState<any> = {
|
|
269
|
-
data: [],
|
|
270
|
-
loading: false,
|
|
271
|
-
error: null,
|
|
272
|
-
visibleRegion: { rowIndexStart: 0, rowIndexEnd: 0 },
|
|
273
|
-
initialized: false,
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
const client = useMemo(() => {
|
|
277
|
-
return new PostgrestClient(endpoint).from(table);
|
|
278
|
-
}, [endpoint, table]);
|
|
279
|
-
|
|
280
|
-
const [state, dispatch] = useReducer(lazyLoadingReducer, initialState);
|
|
281
|
-
const { data, loading } = state;
|
|
282
|
-
|
|
283
|
-
useAsyncEffect(async () => {
|
|
284
|
-
loadMoreData(client, config, state, dispatch);
|
|
285
|
-
}, [
|
|
286
|
-
data,
|
|
287
|
-
state.visibleRegion.rowIndexStart,
|
|
288
|
-
state.visibleRegion.rowIndexEnd,
|
|
289
|
-
]);
|
|
397
|
+
const loadMorePostgRESTData = debounce(_loadMorePostgRESTData, 100);
|
|
290
398
|
|
|
399
|
+
export function useScrollHandler() {
|
|
400
|
+
/** A standardized approach to holding onto the scroll position for the table */
|
|
291
401
|
// Reference to hold onto the scroll position
|
|
292
|
-
const ref = useRef(null);
|
|
402
|
+
const ref = useRef<RowRegion>(null);
|
|
293
403
|
|
|
294
|
-
const
|
|
404
|
+
const setVisibleRegion = ctx.useSet(visibleRegionAtom);
|
|
405
|
+
|
|
406
|
+
return useCallback(
|
|
295
407
|
debounce((visibleCells: RowRegion) => {
|
|
296
408
|
if (
|
|
297
409
|
visibleCells.rowIndexEnd == ref.current?.rowIndexEnd &&
|
|
@@ -300,32 +412,55 @@ export function usePostgRESTLazyLoader(
|
|
|
300
412
|
return;
|
|
301
413
|
}
|
|
302
414
|
console.log("Visible cells changed", visibleCells);
|
|
303
|
-
|
|
304
|
-
type: "set-visible",
|
|
305
|
-
region: visibleCells,
|
|
306
|
-
});
|
|
415
|
+
setVisibleRegion(visibleCells);
|
|
307
416
|
ref.current = visibleCells;
|
|
308
417
|
}, 500),
|
|
309
|
-
[
|
|
418
|
+
[setVisibleRegion],
|
|
310
419
|
);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
export function usePostgRESTLazyLoader(
|
|
423
|
+
endpoint: string,
|
|
424
|
+
table: string,
|
|
425
|
+
config: LazyLoaderOptions = {},
|
|
426
|
+
) {
|
|
427
|
+
const getClient = useCallback(() => {
|
|
428
|
+
return new PostgrestClient(endpoint).from(table);
|
|
429
|
+
}, [endpoint, table]);
|
|
430
|
+
|
|
431
|
+
const [state, dispatch] = useLazyLoaderReducer();
|
|
432
|
+
const { data, loading } = state;
|
|
433
|
+
|
|
434
|
+
// Reset data whenever sort/filter configuration changes so we re-fetch
|
|
435
|
+
const sortFilterKey = useMemo(
|
|
436
|
+
() =>
|
|
437
|
+
JSON.stringify({
|
|
438
|
+
s: config.order,
|
|
439
|
+
v: config.filters,
|
|
440
|
+
i: config.identityKey,
|
|
441
|
+
}),
|
|
442
|
+
[config.order, config.filters, config.identityKey],
|
|
443
|
+
);
|
|
444
|
+
|
|
445
|
+
const visibleRegion = ctx.useValue(visibleRegionAtom);
|
|
446
|
+
|
|
447
|
+
useEffect(() => {
|
|
448
|
+
dispatch({ type: "reset" });
|
|
449
|
+
}, [sortFilterKey]);
|
|
450
|
+
|
|
451
|
+
useAsyncEffect(async () => {
|
|
452
|
+
const client = getClient();
|
|
453
|
+
loadMorePostgRESTData(client, config, state, dispatch);
|
|
454
|
+
}, [data, visibleRegion, sortFilterKey]);
|
|
311
455
|
|
|
312
456
|
return {
|
|
313
457
|
data,
|
|
314
458
|
loading,
|
|
315
|
-
onScroll,
|
|
316
459
|
dispatch,
|
|
317
|
-
|
|
460
|
+
getClient,
|
|
318
461
|
};
|
|
319
462
|
}
|
|
320
463
|
|
|
321
|
-
function getRowIndexToLoadFrom<T>(
|
|
322
|
-
data: (T | null)[],
|
|
323
|
-
visibleRegion: RowRegion,
|
|
324
|
-
chunkSize: number,
|
|
325
|
-
) {
|
|
326
|
-
return indexOfFirstNullInRegion(data, visibleRegion);
|
|
327
|
-
}
|
|
328
|
-
|
|
329
464
|
function indexOfFirstNullInRegion(
|
|
330
465
|
data: any[],
|
|
331
466
|
region: RowRegion,
|
|
@@ -339,50 +474,18 @@ function indexOfFirstNullInRegion(
|
|
|
339
474
|
}
|
|
340
475
|
|
|
341
476
|
export function useTestLazyLoader(config: LazyLoaderOptions = {}) {
|
|
342
|
-
const
|
|
343
|
-
data: [],
|
|
344
|
-
loading: false,
|
|
345
|
-
error: null,
|
|
346
|
-
visibleRegion: { rowIndexStart: 0, rowIndexEnd: 0 },
|
|
347
|
-
initialized: false,
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
const [state, dispatch] = useReducer(lazyLoadingReducer, initialState);
|
|
477
|
+
const [state, dispatch] = useLazyLoaderReducer();
|
|
351
478
|
const { data, loading } = state;
|
|
352
479
|
|
|
480
|
+
const visibleRegion = ctx.useValue(visibleRegionAtom);
|
|
481
|
+
|
|
353
482
|
useAsyncEffect(async () => {
|
|
354
483
|
testDataLoader(config, state, dispatch);
|
|
355
|
-
}, [
|
|
356
|
-
data,
|
|
357
|
-
state.visibleRegion.rowIndexStart,
|
|
358
|
-
state.visibleRegion.rowIndexEnd,
|
|
359
|
-
]);
|
|
360
|
-
|
|
361
|
-
// Reference to hold onto the scroll position
|
|
362
|
-
const ref = useRef(null);
|
|
363
|
-
|
|
364
|
-
const onScroll = useCallback(
|
|
365
|
-
debounce((visibleCells: RowRegion) => {
|
|
366
|
-
if (
|
|
367
|
-
visibleCells.rowIndexEnd == ref.current?.rowIndexEnd &&
|
|
368
|
-
visibleCells.rowIndexStart == ref.current?.rowIndexStart
|
|
369
|
-
) {
|
|
370
|
-
return;
|
|
371
|
-
}
|
|
372
|
-
console.log("Visible cells changed", visibleCells);
|
|
373
|
-
dispatch({
|
|
374
|
-
type: "set-visible",
|
|
375
|
-
region: visibleCells,
|
|
376
|
-
});
|
|
377
|
-
ref.current = visibleCells;
|
|
378
|
-
}, 500),
|
|
379
|
-
[dispatch],
|
|
380
|
-
);
|
|
484
|
+
}, [data, visibleRegion]);
|
|
381
485
|
|
|
382
486
|
return {
|
|
383
487
|
data,
|
|
384
488
|
loading,
|
|
385
|
-
onScroll,
|
|
386
489
|
dispatch,
|
|
387
490
|
};
|
|
388
491
|
}
|
|
@@ -471,10 +574,6 @@ const testQueryFunc = async (
|
|
|
471
574
|
};
|
|
472
575
|
};
|
|
473
576
|
|
|
474
|
-
async function sleep(ms: number) {
|
|
475
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
476
|
-
}
|
|
477
|
-
|
|
478
577
|
function buildSyntheticData(offset: number, count: number | null) {
|
|
479
578
|
console.log(
|
|
480
579
|
`Getting data at indices between ${offset} and ${offset + count}`,
|
|
@@ -483,7 +582,7 @@ function buildSyntheticData(offset: number, count: number | null) {
|
|
|
483
582
|
const id = i + 1;
|
|
484
583
|
return {
|
|
485
584
|
id,
|
|
486
|
-
name: `
|
|
585
|
+
name: `row ${id} has some long text content`,
|
|
487
586
|
};
|
|
488
587
|
});
|
|
489
588
|
}
|