@malloy-publisher/sdk 0.9.12 → 0.9.13
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/README.md +5 -7
- package/dist/{RenderedResult-CDiXJAh9.es.js → RenderedResult-DnBtf-cI.es.js} +2 -2
- package/dist/{RenderedResult-De51QvsZ.cjs.js → RenderedResult-DtQtNV0d.cjs.js} +1 -1
- package/dist/ServerProvider-BIFPCzTD.cjs.js +1 -0
- package/dist/{ServerProvider-RvKSbvnL.es.js → ServerProvider-BV7IoDMR.es.js} +805 -606
- package/dist/builder/index.cjs.js +72 -72
- package/dist/builder/index.es.js +4156 -4219
- package/dist/builder-entry.d.ts +1 -0
- package/dist/client/api.d.ts +178 -0
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +41 -37
- package/dist/components/Report/telemetry.d.ts +0 -5
- package/dist/components/ReportBuilder/BuilderBar.d.ts +1 -1
- package/dist/components/ReportBuilder/BuilderToolbar.d.ts +14 -10
- package/dist/components/ReportBuilder/EditorSurface.d.ts +1 -2
- package/dist/components/ReportBuilder/ReportBuilder.d.ts +3 -10
- package/dist/components/ReportBuilder/ReportEditor.d.ts +2 -9
- package/dist/components/ReportBuilder/SaveStatus.d.ts +16 -0
- package/dist/components/ReportBuilder/TileFrame.d.ts +9 -2
- package/dist/components/ReportBuilder/builderControls.d.ts +14 -6
- package/dist/components/ReportBuilder/useBuilderSession.d.ts +1 -1
- package/dist/components/ReportBuilder/useReportDraft.d.ts +6 -2
- package/dist/components/given/GivenInput.d.ts +18 -1
- package/dist/components/given/GivensPanel.d.ts +12 -1
- package/dist/components/given/useOptionSearch.d.ts +30 -0
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/useDocumentControls.d.ts +5 -0
- package/dist/hooks/useGivensState.d.ts +7 -0
- package/dist/hooks/useSuggestOptions.d.ts +60 -4
- package/dist/index.cjs.js +1 -1
- package/dist/index.es.js +5 -5
- package/dist/{useCompiledDocument-Bf4RSfB6.es.js → useCompiledDocument-B4QF1mIl.es.js} +6912 -6759
- package/dist/useCompiledDocument-D0OAMejE.cjs.js +142 -0
- package/package.json +1 -1
- package/src/builder-entry.ts +1 -0
- package/src/components/Report/ReportPage.tsx +5 -1
- package/src/components/Report/telemetry.ts +0 -6
- package/src/components/ReportBuilder/AddTileDialog.tsx +2 -2
- package/src/components/ReportBuilder/BuilderBar.tsx +5 -256
- package/src/components/ReportBuilder/BuilderHeader.tsx +9 -3
- package/src/components/ReportBuilder/BuilderToolbar.tsx +15 -38
- package/src/components/ReportBuilder/EditorSurface.tsx +0 -3
- package/src/components/ReportBuilder/FilterDialog.tsx +1 -1
- package/src/components/ReportBuilder/ReportBuilder.controls.spec.tsx +210 -24
- package/src/components/ReportBuilder/ReportBuilder.tsx +43 -85
- package/src/components/ReportBuilder/ReportEditor.package.spec.tsx +0 -36
- package/src/components/ReportBuilder/ReportEditor.tsx +2 -13
- package/src/components/ReportBuilder/SaveStatus.tsx +271 -0
- package/src/components/ReportBuilder/TileFrame.spec.ts +34 -0
- package/src/components/ReportBuilder/TileFrame.tsx +10 -3
- package/src/components/ReportBuilder/builderControls.ts +39 -13
- package/src/components/ReportBuilder/emptyStart.spec.ts +1 -1
- package/src/components/ReportBuilder/useReportDraft.ts +38 -4
- package/src/components/given/GivenInput.tsx +219 -104
- package/src/components/given/GivensPanel.spec.tsx +19 -0
- package/src/components/given/GivensPanel.tsx +31 -1
- package/src/components/given/useOptionSearch.spec.tsx +218 -0
- package/src/components/given/useOptionSearch.ts +100 -0
- package/src/hooks/index.ts +5 -1
- package/src/hooks/useDocumentControls.ts +12 -1
- package/src/hooks/useGivensState.spec.ts +54 -0
- package/src/hooks/useGivensState.ts +21 -1
- package/src/hooks/useSuggestOptions.dom.spec.ts +125 -4
- package/src/hooks/useSuggestOptions.spec.ts +73 -2
- package/src/hooks/useSuggestOptions.ts +234 -62
- package/dist/ServerProvider-DKrBcXL7.cjs.js +0 -1
- package/dist/useCompiledDocument-XsQz85Ri.cjs.js +0 -141
- package/src/components/ReportBuilder/ReportBuilder.layoutSwitch.spec.tsx +0 -145
|
@@ -4,8 +4,11 @@
|
|
|
4
4
|
import { describe, expect, it } from "bun:test";
|
|
5
5
|
import {
|
|
6
6
|
buildSuggestQuery,
|
|
7
|
+
malloyStringLiteral,
|
|
8
|
+
readOptionPage,
|
|
7
9
|
readOptionValues,
|
|
8
10
|
SUGGEST_OPTION_LIMIT,
|
|
11
|
+
SUGGEST_SEARCH_LIMIT,
|
|
9
12
|
} from "./useSuggestOptions";
|
|
10
13
|
|
|
11
14
|
describe("readOptionValues", () => {
|
|
@@ -91,7 +94,8 @@ describe("the generated suggest query", () => {
|
|
|
91
94
|
"run: order_items -> {\n" +
|
|
92
95
|
" group_by: products.category\n" +
|
|
93
96
|
" order_by: category asc\n" +
|
|
94
|
-
|
|
97
|
+
// One past the limit, so a list that was cut off is known to be.
|
|
98
|
+
` limit: ${SUGGEST_OPTION_LIMIT + 1}\n` +
|
|
95
99
|
"}",
|
|
96
100
|
);
|
|
97
101
|
});
|
|
@@ -106,7 +110,74 @@ describe("the generated suggest query", () => {
|
|
|
106
110
|
// Otherwise the server's default row cap truncates in whatever order the
|
|
107
111
|
// warehouse returned, silently and differently each time.
|
|
108
112
|
expect(buildSuggestQuery("products", "category")).toContain(
|
|
109
|
-
`limit: ${SUGGEST_OPTION_LIMIT}`,
|
|
113
|
+
`limit: ${SUGGEST_OPTION_LIMIT + 1}`,
|
|
110
114
|
);
|
|
111
115
|
});
|
|
112
116
|
});
|
|
117
|
+
|
|
118
|
+
describe("a search of a cut-off list", () => {
|
|
119
|
+
it("matches ignoring case, as a plain substring, with a limit and the same order", () => {
|
|
120
|
+
expect(buildSuggestQuery("recalls", "maker.name", "General Motors")).toBe(
|
|
121
|
+
"run: recalls -> {\n" +
|
|
122
|
+
" group_by: maker.name\n" +
|
|
123
|
+
" where: strpos(lower(maker.name::string), 'general motors') > 0\n" +
|
|
124
|
+
" order_by: name asc\n" +
|
|
125
|
+
` limit: ${SUGGEST_SEARCH_LIMIT + 1}\n` +
|
|
126
|
+
"}",
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe("malloyStringLiteral: typed text can never leave the literal", () => {
|
|
132
|
+
it("escapes a single quote", () => {
|
|
133
|
+
expect(malloyStringLiteral("Levi's")).toBe("'Levi\\'s'");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("escapes a backslash before the quote, so `\\'` cannot close it", () => {
|
|
137
|
+
// Escaped quote first would turn `\'` into `\\'`: an escaped backslash
|
|
138
|
+
// followed by a bare quote, which ends the literal.
|
|
139
|
+
expect(malloyStringLiteral("a\\'b")).toBe("'a\\\\\\'b'");
|
|
140
|
+
expect(malloyStringLiteral("back\\slash")).toBe("'back\\\\slash'");
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("leaves regular-expression and LIKE metacharacters as text", () => {
|
|
144
|
+
// Matched with strpos, not a pattern, so none of these means anything.
|
|
145
|
+
const text = ".*+?^$()[]{}|%_";
|
|
146
|
+
expect(malloyStringLiteral(text)).toBe(`'${text}'`);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("drops control characters, which a literal cannot hold", () => {
|
|
150
|
+
expect(malloyStringLiteral("a\nb\tc\u0000d")).toBe("'abcd'");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("keeps an attempt to close the literal and add Malloy inside it", () => {
|
|
154
|
+
const attack = "x') > 0 or (1 = 1";
|
|
155
|
+
const literal = malloyStringLiteral(attack);
|
|
156
|
+
expect(literal).toBe("'x\\') > 0 or (1 = 1'");
|
|
157
|
+
// One literal: the only unescaped quotes are its own two.
|
|
158
|
+
expect(literal.replace(/\\\\|\\'/g, "").match(/'/g)).toHaveLength(2);
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
describe("readOptionPage: was the list cut off?", () => {
|
|
163
|
+
const rows = (n: number) =>
|
|
164
|
+
JSON.stringify(Array.from({ length: n }, (_, i) => ({ v: `v${i}` })));
|
|
165
|
+
|
|
166
|
+
it("is, when a generated query returns the extra row, and keeps only the limit", () => {
|
|
167
|
+
const page = readOptionPage(rows(4), "v", { limit: 3 });
|
|
168
|
+
expect(page).toEqual({ values: ["v0", "v1", "v2"], truncated: true });
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("is not, at exactly the limit", () => {
|
|
172
|
+
expect(readOptionPage(rows(3), "v", { limit: 3 }).truncated).toBe(false);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("is, for a named query that fills the server's cap", () => {
|
|
176
|
+
expect(
|
|
177
|
+
readOptionPage(rows(3), "v", { limit: 3, cutOffAt: 3 }).truncated,
|
|
178
|
+
).toBe(true);
|
|
179
|
+
expect(
|
|
180
|
+
readOptionPage(rows(2), "v", { limit: 3, cutOffAt: 3 }).truncated,
|
|
181
|
+
).toBe(false);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
|
|
4
4
|
import { useQueries } from "@tanstack/react-query";
|
|
5
|
-
import { useMemo } from "react";
|
|
5
|
+
import { useMemo, useRef } from "react";
|
|
6
6
|
import type { Given } from "../client";
|
|
7
7
|
import { withPreamble } from "../components/Dashboard/textSource";
|
|
8
8
|
import {
|
|
@@ -19,12 +19,43 @@ import type { GivenValue, HostGivenValue } from "./givenValue";
|
|
|
19
19
|
* is also 1000 but caps silently and in whatever order the warehouse returns.
|
|
20
20
|
* An explicit limit with an explicit order makes a truncated list the same list
|
|
21
21
|
* every time, and makes the cap something a reader of this file can see.
|
|
22
|
-
*
|
|
23
|
-
* `
|
|
24
|
-
*
|
|
22
|
+
* The query asks for one row more than this, so a list that was cut off is
|
|
23
|
+
* known to be (`truncated`), not guessed from a count that happens to land on
|
|
24
|
+
* the limit. Past the limit, typing in the picker searches the whole dimension
|
|
25
|
+
* on the server instead of filtering the first thousand: see
|
|
26
|
+
* {@link OptionSearch}.
|
|
25
27
|
*/
|
|
26
28
|
export const SUGGEST_OPTION_LIMIT = 1000;
|
|
27
29
|
|
|
30
|
+
/** How many matches a search of the server returns: enough to scan, and the hint says to keep typing past it. */
|
|
31
|
+
export const SUGGEST_SEARCH_LIMIT = 200;
|
|
32
|
+
|
|
33
|
+
/** One fetch of options: the values, and whether there were more than it holds. */
|
|
34
|
+
export interface OptionPage {
|
|
35
|
+
values: string[];
|
|
36
|
+
/** More rows matched than the page holds, so values past it are not offered. */
|
|
37
|
+
truncated: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Search a truncated option list on the server: for a control whose suggest
|
|
42
|
+
* came back cut off at {@link SUGGEST_OPTION_LIMIT}, so filtering in the
|
|
43
|
+
* browser would only ever find values in the first thousand.
|
|
44
|
+
*
|
|
45
|
+
* `key` identifies the suggest (its report, version, source, dimension, the
|
|
46
|
+
* givens it carries and a text source's definitions); a caller caches each
|
|
47
|
+
* search under it plus the text searched for. `run` sends the same suggest with
|
|
48
|
+
* a case-insensitive "contains" on the dimension.
|
|
49
|
+
*
|
|
50
|
+
* Only a suggest over a dimension has one: a named `suggest { query=… }` is a
|
|
51
|
+
* whole query, with nowhere to add a condition, so its list is filtered in the
|
|
52
|
+
* browser and says it was cut off.
|
|
53
|
+
*/
|
|
54
|
+
export interface OptionSearch {
|
|
55
|
+
key: readonly unknown[];
|
|
56
|
+
run: (text: string, signal?: AbortSignal) => Promise<OptionPage>;
|
|
57
|
+
}
|
|
58
|
+
|
|
28
59
|
/**
|
|
29
60
|
* The name a `group_by` gives its output column, which is the last segment of a
|
|
30
61
|
* field path.
|
|
@@ -48,16 +79,58 @@ function outputFieldName(dimension: string): string {
|
|
|
48
79
|
* itself, and this text has already been wrong once: `order_by` takes an output
|
|
49
80
|
* field name, not a path, so a joined dimension failed the query outright.
|
|
50
81
|
*/
|
|
51
|
-
export function buildSuggestQuery(
|
|
82
|
+
export function buildSuggestQuery(
|
|
83
|
+
source: string,
|
|
84
|
+
dimension: string,
|
|
85
|
+
/** Text the values must contain, ignoring case: a search of a truncated list. */
|
|
86
|
+
search?: string,
|
|
87
|
+
): string {
|
|
88
|
+
// One row past the limit, so a list that was cut off says so.
|
|
89
|
+
const limit =
|
|
90
|
+
(search === undefined ? SUGGEST_OPTION_LIMIT : SUGGEST_SEARCH_LIMIT) + 1;
|
|
52
91
|
return (
|
|
53
92
|
`run: ${source} -> {\n` +
|
|
54
93
|
` group_by: ${dimension}\n` +
|
|
94
|
+
(search === undefined
|
|
95
|
+
? ""
|
|
96
|
+
: ` where: ${containsCondition(dimension, search)}\n`) +
|
|
55
97
|
` order_by: ${outputFieldName(dimension)} asc\n` +
|
|
56
|
-
` limit: ${
|
|
98
|
+
` limit: ${limit}\n` +
|
|
57
99
|
`}`
|
|
58
100
|
);
|
|
59
101
|
}
|
|
60
102
|
|
|
103
|
+
/**
|
|
104
|
+
* "The dimension contains this text, ignoring case", in Malloy every dialect
|
|
105
|
+
* compiles: `strpos` and `lower` are standard functions each dialect maps to
|
|
106
|
+
* its own SQL (`STRPOS`, `POSITION`, `LOCATE`, `CHARINDEX`). Not a regular
|
|
107
|
+
* expression, whose inline `(?i)` Snowflake does not read, and not `LIKE`,
|
|
108
|
+
* whose wildcards (`%`, `_`) a value can contain and whose escape character
|
|
109
|
+
* differs by database: the text is a plain string literal, matched as written.
|
|
110
|
+
*
|
|
111
|
+
* Cast to string so a numeric or date dimension searches its text too. The text
|
|
112
|
+
* is lowercased here, where it is known, and the column in SQL.
|
|
113
|
+
*/
|
|
114
|
+
function containsCondition(dimension: string, search: string): string {
|
|
115
|
+
return `strpos(lower(${dimension}::string), ${malloyStringLiteral(search.toLowerCase())}) > 0`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Text as a Malloy string literal, whatever it holds: a backslash and a
|
|
120
|
+
* single quote escaped, in that order, so neither can end the literal early.
|
|
121
|
+
* Control characters are dropped: a search box has no use for them, and a raw
|
|
122
|
+
* newline is not legal inside a literal. Nothing else is special inside
|
|
123
|
+
* single quotes, so `%`, `_`, `{`, `$` and the rest stay literal text.
|
|
124
|
+
*/
|
|
125
|
+
export function malloyStringLiteral(text: string): string {
|
|
126
|
+
const escaped = text
|
|
127
|
+
// eslint-disable-next-line no-control-regex
|
|
128
|
+
.replace(/[\u0000-\u001f\u007f]/g, "")
|
|
129
|
+
.replace(/\\/g, "\\\\")
|
|
130
|
+
.replace(/'/g, "\\'");
|
|
131
|
+
return `'${escaped}'`;
|
|
132
|
+
}
|
|
133
|
+
|
|
61
134
|
/**
|
|
62
135
|
* Resolve the option lists behind `select` / `multiselect` controls.
|
|
63
136
|
*
|
|
@@ -155,6 +228,14 @@ export function useSuggestOptions(
|
|
|
155
228
|
* an empty picker has no way to tell which they are looking at.
|
|
156
229
|
*/
|
|
157
230
|
failed: Set<string>;
|
|
231
|
+
/**
|
|
232
|
+
* Givens whose list was cut off: more values exist than were fetched. For
|
|
233
|
+
* a suggest over a dimension, `search` can find the rest; for a named
|
|
234
|
+
* query it cannot, and the picker says the list is partial.
|
|
235
|
+
*/
|
|
236
|
+
truncated: Set<string>;
|
|
237
|
+
/** Server search, per given, for a truncated suggest over a dimension. */
|
|
238
|
+
search: Map<string, OptionSearch>;
|
|
158
239
|
} {
|
|
159
240
|
const { apiClients } = useServer();
|
|
160
241
|
|
|
@@ -173,68 +254,93 @@ export function useSuggestOptions(
|
|
|
173
254
|
[specs],
|
|
174
255
|
);
|
|
175
256
|
|
|
257
|
+
// What identifies each suggest, and how to run it with or without a search:
|
|
258
|
+
// one definition, so the list and its searches always send the same query
|
|
259
|
+
// and carry the same givens.
|
|
260
|
+
const suggests = suggestable.map((spec) => {
|
|
261
|
+
const givens = suggestRequestGivens(spec, applied);
|
|
262
|
+
const suggest = spec.suggest ?? {};
|
|
263
|
+
const identity = [
|
|
264
|
+
environmentName,
|
|
265
|
+
packageName,
|
|
266
|
+
versionId,
|
|
267
|
+
modelPath,
|
|
268
|
+
spec.name,
|
|
269
|
+
suggest.query,
|
|
270
|
+
suggest.source,
|
|
271
|
+
suggest.dimension,
|
|
272
|
+
// Only the givens the suggest carries, so a change to any OTHER
|
|
273
|
+
// control leaves the cached list alone.
|
|
274
|
+
givens === undefined ? null : JSON.stringify(givens),
|
|
275
|
+
// Only a text source keys on its definitions, so no other key changes shape.
|
|
276
|
+
...(preamble === undefined ? [] : [preamble]),
|
|
277
|
+
];
|
|
278
|
+
const run = async (
|
|
279
|
+
search: string | undefined,
|
|
280
|
+
signal?: AbortSignal,
|
|
281
|
+
): Promise<OptionPage> => {
|
|
282
|
+
const named = suggest.query !== undefined;
|
|
283
|
+
const response = await apiClients.models.executeQueryModel(
|
|
284
|
+
environmentName,
|
|
285
|
+
packageName,
|
|
286
|
+
modelPath as string,
|
|
287
|
+
named && preamble === undefined
|
|
288
|
+
? {
|
|
289
|
+
queryName: suggest.query,
|
|
290
|
+
compactJson: true,
|
|
291
|
+
versionId,
|
|
292
|
+
givens,
|
|
293
|
+
}
|
|
294
|
+
: {
|
|
295
|
+
query: withPreamble(
|
|
296
|
+
preamble ?? "",
|
|
297
|
+
named
|
|
298
|
+
? `run: ${suggest.query}`
|
|
299
|
+
: buildSuggestQuery(
|
|
300
|
+
suggest.source as string,
|
|
301
|
+
suggest.dimension as string,
|
|
302
|
+
search,
|
|
303
|
+
),
|
|
304
|
+
),
|
|
305
|
+
compactJson: true,
|
|
306
|
+
versionId,
|
|
307
|
+
givens,
|
|
308
|
+
},
|
|
309
|
+
undefined,
|
|
310
|
+
{ signal },
|
|
311
|
+
);
|
|
312
|
+
return readOptionPage(
|
|
313
|
+
response.data.result,
|
|
314
|
+
suggest.dimension === undefined
|
|
315
|
+
? undefined
|
|
316
|
+
: outputFieldName(suggest.dimension),
|
|
317
|
+
// A generated query asked for one row past its limit. A named
|
|
318
|
+
// query's own limit is unknown: a full server cap is the best sign
|
|
319
|
+
// it was cut off.
|
|
320
|
+
named
|
|
321
|
+
? { limit: SUGGEST_OPTION_LIMIT, cutOffAt: SUGGEST_OPTION_LIMIT }
|
|
322
|
+
: {
|
|
323
|
+
limit:
|
|
324
|
+
search === undefined
|
|
325
|
+
? SUGGEST_OPTION_LIMIT
|
|
326
|
+
: SUGGEST_SEARCH_LIMIT,
|
|
327
|
+
},
|
|
328
|
+
);
|
|
329
|
+
};
|
|
330
|
+
return { spec, identity, run, searchable: suggest.query === undefined };
|
|
331
|
+
});
|
|
332
|
+
|
|
176
333
|
const results = useQueries({
|
|
177
|
-
queries:
|
|
178
|
-
const givens = suggestRequestGivens(spec, applied);
|
|
334
|
+
queries: suggests.map(({ identity, run }) => {
|
|
179
335
|
return {
|
|
180
|
-
queryKey: [
|
|
181
|
-
"givenSuggest",
|
|
182
|
-
environmentName,
|
|
183
|
-
packageName,
|
|
184
|
-
versionId,
|
|
185
|
-
modelPath,
|
|
186
|
-
spec.name,
|
|
187
|
-
spec.suggest?.query,
|
|
188
|
-
spec.suggest?.source,
|
|
189
|
-
spec.suggest?.dimension,
|
|
190
|
-
// Only the givens the suggest carries, so a change to any OTHER
|
|
191
|
-
// control leaves the cached list alone.
|
|
192
|
-
givens === undefined ? null : JSON.stringify(givens),
|
|
193
|
-
// Only a text source keys on its definitions, so no other key changes shape.
|
|
194
|
-
...(preamble === undefined ? [] : [preamble]),
|
|
195
|
-
],
|
|
336
|
+
queryKey: ["givenSuggest", ...identity],
|
|
196
337
|
enabled: modelPath !== undefined,
|
|
197
338
|
// Option lists change with the data, not with the filters, so they
|
|
198
339
|
// are cached well past a single control interaction.
|
|
199
340
|
staleTime: 5 * 60 * 1000,
|
|
200
341
|
refetchOnWindowFocus: false,
|
|
201
|
-
queryFn:
|
|
202
|
-
|
|
203
|
-
const response = await apiClients.models.executeQueryModel(
|
|
204
|
-
environmentName,
|
|
205
|
-
packageName,
|
|
206
|
-
modelPath as string,
|
|
207
|
-
suggest.query !== undefined && preamble === undefined
|
|
208
|
-
? {
|
|
209
|
-
queryName: suggest.query,
|
|
210
|
-
compactJson: true,
|
|
211
|
-
versionId,
|
|
212
|
-
givens,
|
|
213
|
-
}
|
|
214
|
-
: {
|
|
215
|
-
query: withPreamble(
|
|
216
|
-
preamble ?? "",
|
|
217
|
-
suggest.query !== undefined
|
|
218
|
-
? `run: ${suggest.query}`
|
|
219
|
-
: buildSuggestQuery(
|
|
220
|
-
suggest.source as string,
|
|
221
|
-
suggest.dimension as string,
|
|
222
|
-
),
|
|
223
|
-
),
|
|
224
|
-
compactJson: true,
|
|
225
|
-
versionId,
|
|
226
|
-
givens,
|
|
227
|
-
},
|
|
228
|
-
undefined,
|
|
229
|
-
{ signal },
|
|
230
|
-
);
|
|
231
|
-
return readOptionValues(
|
|
232
|
-
response.data.result,
|
|
233
|
-
suggest.dimension === undefined
|
|
234
|
-
? undefined
|
|
235
|
-
: outputFieldName(suggest.dimension),
|
|
236
|
-
);
|
|
237
|
-
},
|
|
342
|
+
queryFn: ({ signal }: { signal: AbortSignal }) =>
|
|
343
|
+
run(undefined, signal),
|
|
238
344
|
};
|
|
239
345
|
}),
|
|
240
346
|
});
|
|
@@ -243,7 +349,7 @@ export function useSuggestOptions(
|
|
|
243
349
|
const byName = new Map<string, string[]>();
|
|
244
350
|
suggestable.forEach((spec, index) => {
|
|
245
351
|
const value = results[index]?.data;
|
|
246
|
-
if (spec.name && value) byName.set(spec.name, value);
|
|
352
|
+
if (spec.name && value) byName.set(spec.name, value.values);
|
|
247
353
|
});
|
|
248
354
|
return byName;
|
|
249
355
|
// `results` is a fresh array each render; its data identity is what
|
|
@@ -267,10 +373,64 @@ export function useSuggestOptions(
|
|
|
267
373
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
268
374
|
}, [suggestable, results.map((result) => result.isError).join("|")]);
|
|
269
375
|
|
|
376
|
+
// Same reasoning as `options`: compared by what each list says, not by the
|
|
377
|
+
// fresh wrapper objects.
|
|
378
|
+
const truncatedKey = results
|
|
379
|
+
.map((result) => !!result.data?.truncated)
|
|
380
|
+
.join("|");
|
|
381
|
+
const truncated = useMemo(() => {
|
|
382
|
+
const names = new Set<string>();
|
|
383
|
+
suggestable.forEach((spec, index) => {
|
|
384
|
+
if (spec.name && results[index]?.data?.truncated) names.add(spec.name);
|
|
385
|
+
});
|
|
386
|
+
return names;
|
|
387
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
388
|
+
}, [suggestable, truncatedKey]);
|
|
389
|
+
|
|
390
|
+
// A handle per truncated suggest over a dimension. Rebuilt only when what
|
|
391
|
+
// identifies a suggest changes, so a picker searching with one does not
|
|
392
|
+
// start over on every render.
|
|
393
|
+
const identities = JSON.stringify(suggests.map((s) => s.identity));
|
|
394
|
+
const latestRuns = useRef(suggests);
|
|
395
|
+
latestRuns.current = suggests;
|
|
396
|
+
const search = useMemo(() => {
|
|
397
|
+
const byName = new Map<string, OptionSearch>();
|
|
398
|
+
suggests.forEach(({ spec, identity, searchable }, index) => {
|
|
399
|
+
if (!spec.name || !searchable || !truncated.has(spec.name)) return;
|
|
400
|
+
byName.set(spec.name, {
|
|
401
|
+
key: ["givenSuggestSearch", ...identity],
|
|
402
|
+
run: (text, signal) => latestRuns.current[index].run(text, signal),
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
return byName;
|
|
406
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
407
|
+
}, [identities, truncated]);
|
|
408
|
+
|
|
270
409
|
return {
|
|
271
410
|
options,
|
|
272
411
|
isLoading: results.some((result) => result.isLoading),
|
|
273
412
|
failed,
|
|
413
|
+
truncated,
|
|
414
|
+
search,
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* One fetch's options and whether it was cut off. `limit` is what the page
|
|
420
|
+
* holds; a generated query asked for one row more, so a row past it means
|
|
421
|
+
* more exist. `cutOffAt` instead marks a page as cut off once it reaches that
|
|
422
|
+
* many rows, for a named query whose own limit is unknown.
|
|
423
|
+
*/
|
|
424
|
+
export function readOptionPage(
|
|
425
|
+
result: string | undefined,
|
|
426
|
+
dimension: string | undefined,
|
|
427
|
+
{ limit, cutOffAt }: { limit: number; cutOffAt?: number },
|
|
428
|
+
): OptionPage {
|
|
429
|
+
const rows = readRows(result);
|
|
430
|
+
return {
|
|
431
|
+
values: optionValues(rows.slice(0, limit), dimension),
|
|
432
|
+
truncated:
|
|
433
|
+
cutOffAt === undefined ? rows.length > limit : rows.length >= cutOffAt,
|
|
274
434
|
};
|
|
275
435
|
}
|
|
276
436
|
|
|
@@ -291,6 +451,11 @@ export function readOptionValues(
|
|
|
291
451
|
result: string | undefined,
|
|
292
452
|
dimension: string | undefined,
|
|
293
453
|
): string[] {
|
|
454
|
+
return optionValues(readRows(result), dimension);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** A compact-JSON result as rows, or a thrown error when it is not one. */
|
|
458
|
+
function readRows(result: string | undefined): unknown[] {
|
|
294
459
|
// Absent is tolerated: `result` is optional in the API contract, so a
|
|
295
460
|
// missing one is a shape the server is allowed to send and "no values" is
|
|
296
461
|
// the honest reading of it.
|
|
@@ -314,7 +479,14 @@ export function readOptionValues(
|
|
|
314
479
|
"Suggest query returned JSON that is not a list of rows.",
|
|
315
480
|
);
|
|
316
481
|
}
|
|
482
|
+
return rows;
|
|
483
|
+
}
|
|
317
484
|
|
|
485
|
+
/** The option strings in some rows: see {@link readOptionValues}. */
|
|
486
|
+
function optionValues(
|
|
487
|
+
rows: unknown[],
|
|
488
|
+
dimension: string | undefined,
|
|
489
|
+
): string[] {
|
|
318
490
|
const values: string[] = [];
|
|
319
491
|
const seen = new Set<string>();
|
|
320
492
|
for (const row of rows) {
|