@macrostrat/data-sheet 2.0.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 (79) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +19 -0
  3. package/dist/actions.5740e8e2.js +21 -0
  4. package/dist/actions.5740e8e2.js.map +1 -0
  5. package/dist/actions.module.6ec06797.css +11 -0
  6. package/dist/actions.module.6ec06797.css.map +1 -0
  7. package/dist/actions.module.ea96403b.js +16 -0
  8. package/dist/actions.module.ea96403b.js.map +1 -0
  9. package/dist/cell-renderers.de70f471.js +31 -0
  10. package/dist/cell-renderers.de70f471.js.map +1 -0
  11. package/dist/colors.41c3ed7a.js +83 -0
  12. package/dist/colors.41c3ed7a.js.map +1 -0
  13. package/dist/column-spec.4710da93.js +70 -0
  14. package/dist/column-spec.4710da93.js.map +1 -0
  15. package/dist/components.c3ad66e9.js +11 -0
  16. package/dist/components.c3ad66e9.js.map +1 -0
  17. package/dist/core.d8d80480.js +356 -0
  18. package/dist/core.d8d80480.js.map +1 -0
  19. package/dist/data-loaders.5302dedd.js +196 -0
  20. package/dist/data-loaders.5302dedd.js.map +1 -0
  21. package/dist/editor-popup.9259608f.js +68 -0
  22. package/dist/editor-popup.9259608f.js.map +1 -0
  23. package/dist/index.d.ts +202 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +9 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/main.module.846377cd.css +35 -0
  28. package/dist/main.module.846377cd.css.map +1 -0
  29. package/dist/main.module.914caa19.css +91 -0
  30. package/dist/main.module.914caa19.css.map +1 -0
  31. package/dist/main.module.93f91ad4.js +25 -0
  32. package/dist/main.module.93f91ad4.js.map +1 -0
  33. package/dist/main.module.dc8f6293.css +62 -0
  34. package/dist/main.module.dc8f6293.css.map +1 -0
  35. package/dist/main.module.de002429.js +43 -0
  36. package/dist/main.module.de002429.js.map +1 -0
  37. package/dist/main.module.ff717a08.js +37 -0
  38. package/dist/main.module.ff717a08.js.map +1 -0
  39. package/dist/postgrest-table.15ded3c1.js +173 -0
  40. package/dist/postgrest-table.15ded3c1.js.map +1 -0
  41. package/dist/provider.67e982b8.js +232 -0
  42. package/dist/provider.67e982b8.js.map +1 -0
  43. package/dist/text-areas.079e8b15.js +78 -0
  44. package/dist/text-areas.079e8b15.js.map +1 -0
  45. package/dist/utils.a37ed786.js +14 -0
  46. package/dist/utils.a37ed786.js.map +1 -0
  47. package/package.json +50 -0
  48. package/src/__archive/base.ts +55 -0
  49. package/src/__archive/components/column-utils.ts +126 -0
  50. package/src/__archive/components/index.ts +115 -0
  51. package/src/__archive/components/toolbar.ts +79 -0
  52. package/src/__archive/editors/index.ts +78 -0
  53. package/src/__archive/editors/styles.module.scss +10 -0
  54. package/src/__archive/enhancers.ts +36 -0
  55. package/src/__archive/index.ts +8 -0
  56. package/src/__archive/main.ts +180 -0
  57. package/src/__archive/provider.ts +179 -0
  58. package/src/__archive/styles.module.scss +56 -0
  59. package/src/__archive/test-sheet/attitude-sheet.old.styl +53 -0
  60. package/src/__archive/test-sheet/attitude-sheet.scss +60 -0
  61. package/src/__archive/test-sheet/index.ts +210 -0
  62. package/src/__archive/virtualized.ts +82 -0
  63. package/src/components/actions.module.sass +8 -0
  64. package/src/components/actions.ts +11 -0
  65. package/src/components/colors.ts +90 -0
  66. package/src/components/editor-popup.ts +69 -0
  67. package/src/components/index.ts +3 -0
  68. package/src/components/main.module.sass +35 -0
  69. package/src/components/text-areas.ts +88 -0
  70. package/src/core.ts +481 -0
  71. package/src/index.ts +2 -0
  72. package/src/main.module.sass +77 -0
  73. package/src/postgrest-table/cell-renderers.ts +24 -0
  74. package/src/postgrest-table/data-loaders.ts +287 -0
  75. package/src/postgrest-table/index.ts +172 -0
  76. package/src/postgrest-table/main.module.sass +52 -0
  77. package/src/provider.ts +282 -0
  78. package/src/utils/column-spec.ts +97 -0
  79. package/src/utils/index.ts +7 -0
@@ -0,0 +1,287 @@
1
+ /** Lazy loading of data from a PostgREST endpoint */
2
+
3
+ import { useAsyncEffect } from "@macrostrat/ui-components";
4
+ import { debounce } from "underscore";
5
+ import { useCallback, useMemo, useReducer } from "react";
6
+ import update, { Spec } from "immutability-helper";
7
+
8
+ interface ChunkIndex {
9
+ startRow: number;
10
+ endRow: number;
11
+ lastValue: any;
12
+ }
13
+
14
+ import { PostgrestClient, PostgrestQueryBuilder } from "@supabase/postgrest-js";
15
+
16
+ interface LazyLoaderState<T> {
17
+ data: (T | null)[];
18
+ loading: boolean;
19
+ error: Error | null;
20
+ visibleRegion: RowRegion;
21
+ initialized: boolean;
22
+ }
23
+
24
+ type LazyLoaderAction<T> =
25
+ | { type: "start-loading" }
26
+ | { type: "loaded"; data: T[]; offset: number; totalSize: number }
27
+ | { type: "error"; error: Error }
28
+ | { type: "set-visible"; region: RowRegion }
29
+ | { type: "update-data"; changes: Spec<T[]> };
30
+
31
+ function adjustArraySize<T>(arr: T[], newSize: number) {
32
+ if (newSize == null || arr.length === newSize) {
33
+ return arr;
34
+ } else if (arr.length > newSize) {
35
+ // Trim the array
36
+ arr = arr.slice(0, newSize);
37
+ }
38
+ return [...arr, ...Array(newSize - arr.length).fill(null)];
39
+ }
40
+
41
+ function lazyLoadingReducer<T>(
42
+ state: LazyLoaderState<T>,
43
+ action: LazyLoaderAction<T>
44
+ ): LazyLoaderState<T> {
45
+ console.log(action);
46
+ switch (action.type) {
47
+ case "start-loading":
48
+ return {
49
+ ...state,
50
+ loading: true,
51
+ initialized: true,
52
+ };
53
+ case "set-visible":
54
+ return {
55
+ ...state,
56
+ visibleRegion: action.region,
57
+ };
58
+ case "update-data":
59
+ return {
60
+ ...state,
61
+ loading: false,
62
+ data: update(state.data, action.changes),
63
+ };
64
+ case "loaded":
65
+ let data = adjustArraySize(state.data, action.totalSize);
66
+ data = [
67
+ ...data.slice(0, action.offset),
68
+ ...action.data,
69
+ ...data.slice(action.offset + action.data.length),
70
+ ];
71
+
72
+ return {
73
+ ...state,
74
+ data,
75
+ loading: false,
76
+ };
77
+ case "error":
78
+ return {
79
+ ...state,
80
+ error: action.error,
81
+ loading: false,
82
+ };
83
+ default:
84
+ return state;
85
+ }
86
+ }
87
+
88
+ interface RowRegion {
89
+ rowIndexStart: number;
90
+ rowIndexEnd: number;
91
+ }
92
+
93
+ enum LoadDirection {
94
+ "up",
95
+ "down",
96
+ }
97
+
98
+ function overlapsNulls(data: any[], region: RowRegion) {
99
+ for (let i = region.rowIndexStart; i < region.rowIndexEnd; i++) {
100
+ if (data[i] == null) {
101
+ return true;
102
+ }
103
+ }
104
+ return false;
105
+ }
106
+
107
+ function distanceToNextNonEmptyRow(
108
+ data: any[],
109
+ start: number,
110
+ direction: LoadDirection,
111
+ limit: number
112
+ ): number {
113
+ let i = start;
114
+ while (i < data.length && i > 0 && limit > 0) {
115
+ if (data[i] != null) {
116
+ return i;
117
+ }
118
+ i += direction === LoadDirection.down ? 1 : -1;
119
+ limit -= 1;
120
+ }
121
+ return i;
122
+ }
123
+
124
+ interface QueryConfig {
125
+ columns?: string;
126
+ count?: "exact" | "estimated";
127
+ limit?: number;
128
+ offset?: number;
129
+ order?: { key: string; ascending: boolean };
130
+ after?: any;
131
+ }
132
+
133
+ function buildQuery<T>(
134
+ client: PostgrestQueryBuilder<T, any>,
135
+ config: QueryConfig
136
+ ) {
137
+ const { columns = "*", count } = config;
138
+ const opts = { count };
139
+
140
+ let query = client.select(columns, opts);
141
+
142
+ if (config.order != null) {
143
+ query = query.order(config.order.key, {
144
+ ascending: config.order.ascending,
145
+ });
146
+ if (config.after != null) {
147
+ const op = config.order.ascending ? "gt" : "lt";
148
+ query = query[op](config.order.key, config.after);
149
+ }
150
+ }
151
+ if (config.limit != null) {
152
+ if (config.offset != null) {
153
+ query = query.range(config.offset, config.offset + config.limit - 1);
154
+ console.log(`Random seek from ${config.offset}, this will be slow`);
155
+ } else {
156
+ query = query.limit(config.limit);
157
+ }
158
+ }
159
+ return query;
160
+ }
161
+
162
+ function _loadMoreData<T>(
163
+ client: PostgrestQueryBuilder<T, any>,
164
+ config: QueryConfig & { chunkSize: number },
165
+ state: LazyLoaderState<T>,
166
+ dispatch: any
167
+ ) {
168
+ const rowIndex = indexOfFirstNullInRegion(state.data, state.visibleRegion);
169
+ if (state.loading || rowIndex == null) {
170
+ if (state.initialized) {
171
+ return;
172
+ }
173
+ }
174
+
175
+ const { chunkSize = 100, ...rest } = config;
176
+
177
+ const sortKey = config.order?.key ?? "id";
178
+
179
+ let cfg: QueryConfig = {
180
+ ...rest,
181
+ limit: chunkSize,
182
+ offset: null,
183
+ };
184
+
185
+ // Allows random seeking
186
+ const isInitialQuery = !state.initialized;
187
+ if (isInitialQuery) {
188
+ cfg.count = "exact";
189
+ }
190
+
191
+ // This only works for forward queries
192
+ if (!isInitialQuery) {
193
+ cfg.after = state.data[rowIndex - 1]?.[sortKey];
194
+ if (cfg.after == null) {
195
+ cfg.offset = rowIndex;
196
+ }
197
+ }
198
+
199
+ dispatch({ type: "start-loading" });
200
+
201
+ const query = buildQuery(client, cfg);
202
+
203
+ query.then((res) => {
204
+ const { data, count } = res;
205
+ dispatch({
206
+ type: "loaded",
207
+ data,
208
+ offset: rowIndex,
209
+ totalSize: count,
210
+ });
211
+ });
212
+ }
213
+
214
+ // Ensure only one data load is in progress at a time
215
+ const loadMoreData = debounce(_loadMoreData, 100);
216
+
217
+ type LazyLoaderOptions = Omit<QueryConfig, "count" | "offset" | "limit"> & {
218
+ chunkSize?: number;
219
+ sortKey?: string;
220
+ };
221
+
222
+ export function usePostgRESTLazyLoader(
223
+ endpoint: string,
224
+ table: string,
225
+ config: LazyLoaderOptions = {}
226
+ ) {
227
+ const initialState: LazyLoaderState<any> = {
228
+ data: [],
229
+ loading: false,
230
+ error: null,
231
+ visibleRegion: { rowIndexStart: 0, rowIndexEnd: 0 },
232
+ initialized: false,
233
+ };
234
+
235
+ const client = useMemo(() => {
236
+ return new PostgrestClient(endpoint).from(table);
237
+ }, [endpoint, table]);
238
+
239
+ const [state, dispatch] = useReducer(lazyLoadingReducer, initialState);
240
+ const { data, loading } = state;
241
+
242
+ useAsyncEffect(async () => {
243
+ loadMoreData(client, config, state, dispatch);
244
+ }, [
245
+ data,
246
+ state.visibleRegion.rowIndexStart,
247
+ state.visibleRegion.rowIndexEnd,
248
+ ]);
249
+
250
+ const onScroll = useCallback(
251
+ debounce((visibleCells: RowRegion) => {
252
+ dispatch({
253
+ type: "set-visible",
254
+ region: visibleCells,
255
+ });
256
+ }, 500),
257
+ [dispatch]
258
+ );
259
+
260
+ return {
261
+ data,
262
+ loading,
263
+ onScroll,
264
+ dispatch,
265
+ client,
266
+ };
267
+ }
268
+
269
+ function getRowIndexToLoadFrom<T>(
270
+ data: (T | null)[],
271
+ visibleRegion: RowRegion,
272
+ chunkSize: number
273
+ ) {
274
+ return indexOfFirstNullInRegion(data, visibleRegion);
275
+ }
276
+
277
+ function indexOfFirstNullInRegion(
278
+ data: any[],
279
+ region: RowRegion
280
+ ): number | null {
281
+ for (let i = region.rowIndexStart; i < region.rowIndexEnd; i++) {
282
+ if (data[i] == null) {
283
+ return i;
284
+ }
285
+ }
286
+ return null;
287
+ }
@@ -0,0 +1,172 @@
1
+ import { OverlayToaster, Tag } from "@blueprintjs/core";
2
+ import hyper from "@macrostrat/hyper";
3
+ import styles from "./main.module.sass";
4
+ import { DataSheet, ColorCell } from "../core"; //getRowsToDelete
5
+ import { LithologyTag } from "./cell-renderers";
6
+ import { usePostgRESTLazyLoader } from "./data-loaders";
7
+ import { Spinner } from "@blueprintjs/core";
8
+
9
+ export * from "./data-loaders";
10
+ import { useCallback, useRef } from "react";
11
+ import { ErrorBoundary } from "@macrostrat/ui-components";
12
+ import { Spec } from "immutability-helper";
13
+ import { DataSheetProviderProps } from "../provider";
14
+
15
+ const h = hyper.styled(styles);
16
+
17
+ interface PostgRESTTableViewProps<T> extends DataSheetProviderProps<T> {
18
+ endpoint: string;
19
+ table: string;
20
+ columnOptions?: any;
21
+ order?: any;
22
+ columns?: string;
23
+ editable?: boolean;
24
+ }
25
+
26
+ export function PostgRESTTableView<T>(props: PostgRESTTableViewProps<T>) {
27
+ return h(ErrorBoundary, h(_PostgRESTTableView, props));
28
+ }
29
+
30
+ const successResponses = [200, 201];
31
+
32
+ function _PostgRESTTableView<T>({
33
+ endpoint,
34
+ table,
35
+ columnOptions,
36
+ order,
37
+ columns,
38
+ editable = false,
39
+ ...rest
40
+ }: PostgRESTTableViewProps<T>) {
41
+ const { data, onScroll, dispatch, client } = usePostgRESTLazyLoader(
42
+ endpoint,
43
+ table,
44
+ {
45
+ order,
46
+ columns,
47
+ }
48
+ );
49
+
50
+ const toasterRef = useRef(null);
51
+
52
+ const finishResponse = useCallback(
53
+ (promisedResult, changes) => {
54
+ promisedResult
55
+ .then((res) => {
56
+ if (!successResponses.includes(res.status)) {
57
+ // Throw an error with the status code
58
+ let err = new Error(res.error.message);
59
+ err["status"] = res.status;
60
+ throw err;
61
+ }
62
+
63
+ // Merge new data with old data
64
+ dispatch({ type: "update-data", changes });
65
+ })
66
+ .catch((err: Error) => {
67
+ const status = err["status"];
68
+ toasterRef.current?.show({
69
+ message: h([
70
+ h.if(status != null)([h("code", status), " "]),
71
+ err.message,
72
+ ]),
73
+ intent: "danger",
74
+ });
75
+ });
76
+ },
77
+ [dispatch]
78
+ );
79
+
80
+ if (data == null) {
81
+ return h(Spinner);
82
+ }
83
+
84
+ return h("div.data-sheet-outer", [
85
+ h(OverlayToaster, { usePortal: false, ref: toasterRef }),
86
+ h(DataSheet, {
87
+ ...rest,
88
+ data,
89
+ columnSpecOptions: columnOptions ?? {},
90
+ editable,
91
+ onVisibleCellsChange: onScroll,
92
+ onDeleteRows(selection) {
93
+ if (!editable) return;
94
+
95
+ const rowIndices = console.log(rowIndices); //getRowsToDelete(selection);
96
+
97
+ const ids = rowIndices.map((i) => data[i].id);
98
+
99
+ dispatch({ type: "start-loading" });
100
+
101
+ const query = client.delete().in("id", ids);
102
+
103
+ finishResponse(query, { $delete: Array.from(rowIndices.keys()) });
104
+ },
105
+ onSaveData(updates, data) {
106
+ if (!editable) return;
107
+
108
+ // Augment updates with primary key
109
+
110
+ let changes: Spec<any[]> = {};
111
+ let updateRows: any[] = [];
112
+ for (let [key, update] of Object.entries(updates)) {
113
+ const value = { ...data[key], ...update };
114
+ updateRows.push(value);
115
+ changes[key] = { $set: value };
116
+ }
117
+
118
+ dispatch({ type: "start-loading" });
119
+
120
+ // Save data
121
+ const query = client.upsert(updateRows, { defaultToNull: false });
122
+
123
+ finishResponse(query, changes);
124
+ },
125
+ }),
126
+ ]);
127
+ }
128
+
129
+ export function LongTextViewer({ value, onChange }) {
130
+ return h("div.long-text", value);
131
+ }
132
+
133
+ export function IntervalCell({ value, children, ...rest }) {
134
+ return h(ColorCell, { value: value?.color, ...rest }, value?.name);
135
+ }
136
+
137
+ export function lithologyRenderer(value) {
138
+ return h("span.liths", [
139
+ addJoiner(value?.map((d) => h(LithologyTag, { data: d }))),
140
+ ]);
141
+ }
142
+
143
+ export function ExpandedLithologies({ value, onChange }) {
144
+ if (value == null) return h("div.basis-panel", "No lithologies");
145
+ return h("div.basis-panel", [
146
+ h("table", [
147
+ h("thead", h("tr", [h("th", "Lithology"), h("th", "Source")])),
148
+ h(
149
+ "tbody",
150
+ value.map((d) => {
151
+ return h("tr", { key: d.id }, [
152
+ h("td", h(LithologyTag, { data: d })),
153
+ h(
154
+ "td.basis-col",
155
+ d.basis_col?.map((d) => {
156
+ return h(Tag, { minimal: true, key: d }, [
157
+ h("span.tag-header", "Column"),
158
+ " ",
159
+ h("code", d),
160
+ ]);
161
+ })
162
+ ),
163
+ ]);
164
+ })
165
+ ),
166
+ ]),
167
+ ]);
168
+ }
169
+
170
+ function addJoiner(arr) {
171
+ return arr?.reduce((acc, curr) => [acc, " ", curr]);
172
+ }
@@ -0,0 +1,52 @@
1
+ .lithology
2
+ font-weight: bold
3
+ display: inline-block
4
+ padding: 0 0.5em
5
+ border-radius: 0.5em
6
+
7
+ .inline-lithology .lithology
8
+ line-height: 1.2em
9
+
10
+ .basis-panel
11
+ :global(.bp5-tag)
12
+ margin: 0 0.5em 0.5em 0
13
+
14
+ td
15
+ vertical-align: top
16
+
17
+ th
18
+ text-align: left
19
+ font-weight: 400
20
+ font-size: 0.9em
21
+ font-style: italic
22
+ color: var(--secondary-color)
23
+
24
+ .basis-panel, .long-text
25
+ max-width: 20em
26
+ padding: 8px
27
+ max-height: 30em
28
+ overflow-y: scroll
29
+ pointer-events: all
30
+
31
+ .description
32
+ font-style: italic
33
+
34
+
35
+ .tag-header
36
+ font-weight: bold
37
+ text-transform: small-caps
38
+ font-size: 0.8em
39
+ color: var(--secondary-color)
40
+
41
+ .basis-col
42
+ max-width: 15em
43
+
44
+ .liths :global(.bp5-tag)
45
+ padding: 0px 4px
46
+ min-height: 16px
47
+
48
+ .data-sheet-outer
49
+ position: relative
50
+ flex: 1
51
+ display: flex
52
+ min-height: 0