@malloy-publisher/sdk 0.0.248 → 0.0.249

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 (98) hide show
  1. package/README.md +32 -17
  2. package/dist/ServerProvider-CcfeBpt3.cjs.js +1 -0
  3. package/dist/{ServerProvider-BxEh3nFI.es.js → ServerProvider-EqBlMMqx.es.js} +1056 -905
  4. package/dist/client/api.d.ts +297 -6
  5. package/dist/client/index.cjs.js +1 -1
  6. package/dist/client/index.es.js +74 -70
  7. package/dist/components/Dashboard/Dashboard.d.ts +49 -0
  8. package/dist/components/Dashboard/DashboardTile.d.ts +48 -0
  9. package/dist/components/Dashboard/index.d.ts +2 -0
  10. package/dist/components/Notebook/Notebook.d.ts +46 -8
  11. package/dist/components/Notebook/NotebookCell.d.ts +37 -3
  12. package/dist/components/Notebook/runPlan.d.ts +29 -0
  13. package/dist/components/Notebook/types.d.ts +10 -0
  14. package/dist/components/Package/ContentTypeIcon.d.ts +17 -1
  15. package/dist/components/Package/Package.d.ts +1 -4
  16. package/dist/components/RenderedResult/RenderedResult.d.ts +7 -1
  17. package/dist/components/RenderedResult/ResultContainer.d.ts +3 -1
  18. package/dist/components/ResultsDialog.d.ts +4 -1
  19. package/dist/components/ServerProvider.d.ts +2 -1
  20. package/dist/components/click_helper.d.ts +10 -1
  21. package/dist/components/drill/index.d.ts +3 -0
  22. package/dist/components/drill/markDrillableCells.d.ts +55 -0
  23. package/dist/components/drill/resolveDrill.d.ts +159 -0
  24. package/dist/components/drill/useDrill.d.ts +63 -0
  25. package/dist/components/given/GivenInput.d.ts +27 -3
  26. package/dist/components/given/GivensPanel.d.ts +36 -7
  27. package/dist/components/given/filterValue.d.ts +117 -0
  28. package/dist/components/given/index.d.ts +1 -1
  29. package/dist/components/given/paramCodec.d.ts +60 -0
  30. package/dist/components/index.d.ts +4 -1
  31. package/dist/components/styles.d.ts +23 -0
  32. package/dist/constants/docLinks.d.ts +6 -0
  33. package/dist/{core-BuvlzSIX.es.js → core-DFBwNhvb.es.js} +1 -1
  34. package/dist/{core-C86Rb7tJ.cjs.js → core-DZk7z7ko.cjs.js} +1 -1
  35. package/dist/hooks/givenValue.d.ts +10 -0
  36. package/dist/hooks/index.d.ts +3 -1
  37. package/dist/hooks/useGivensState.d.ts +64 -0
  38. package/dist/hooks/useSuggestOptions.d.ts +78 -0
  39. package/dist/{index-BdtAZ5k6.es.js → index-CLcRrkxd.es.js} +41221 -39894
  40. package/dist/index-CWQXhe0D.cjs.js +338 -0
  41. package/dist/index.cjs.js +1 -1
  42. package/dist/index.es.js +77 -62
  43. package/dist/theme/types.d.ts +9 -0
  44. package/package.json +3 -1
  45. package/src/components/Connections/AddConnectionDialog.tsx +10 -1
  46. package/src/components/Dashboard/Dashboard.tsx +395 -0
  47. package/src/components/Dashboard/DashboardTile.tsx +150 -0
  48. package/src/components/Dashboard/index.ts +2 -0
  49. package/src/components/Home/Home.tsx +114 -23
  50. package/src/components/Notebook/Notebook.tsx +444 -390
  51. package/src/components/Notebook/NotebookCell.tsx +117 -3
  52. package/src/components/Notebook/runPlan.spec.ts +152 -0
  53. package/src/components/Notebook/runPlan.ts +64 -0
  54. package/src/components/Notebook/types.ts +11 -0
  55. package/src/components/Package/ContentTypeIcon.spec.ts +88 -0
  56. package/src/components/Package/ContentTypeIcon.tsx +77 -6
  57. package/src/components/Package/Package.tsx +148 -38
  58. package/src/components/RenderedResult/RenderedResult.tsx +309 -19
  59. package/src/components/RenderedResult/ResultContainer.tsx +28 -1
  60. package/src/components/ResultsDialog.tsx +5 -0
  61. package/src/components/ServerProvider.tsx +2 -0
  62. package/src/components/click_helper.ts +14 -1
  63. package/src/components/drill/index.ts +25 -0
  64. package/src/components/drill/markDrillableCells.dom.spec.ts +310 -0
  65. package/src/components/drill/markDrillableCells.spec.ts +106 -0
  66. package/src/components/drill/markDrillableCells.ts +311 -0
  67. package/src/components/drill/resolveDrill.spec.ts +446 -0
  68. package/src/components/drill/resolveDrill.ts +372 -0
  69. package/src/components/drill/useDrill.tsx +200 -0
  70. package/src/components/given/GivenInput.spec.tsx +495 -15
  71. package/src/components/given/GivenInput.tsx +562 -30
  72. package/src/components/given/GivensPanel.spec.tsx +172 -0
  73. package/src/components/given/GivensPanel.tsx +139 -32
  74. package/src/components/given/filterValue.spec.ts +361 -0
  75. package/src/components/given/filterValue.ts +204 -0
  76. package/src/components/given/index.ts +5 -1
  77. package/src/components/given/paramCodec.spec.ts +854 -0
  78. package/src/components/given/paramCodec.ts +480 -0
  79. package/src/components/index.ts +4 -1
  80. package/src/components/styles.ts +27 -3
  81. package/src/constants/docLinks.spec.ts +51 -0
  82. package/src/constants/docLinks.ts +26 -0
  83. package/src/hooks/givenValue.ts +10 -0
  84. package/src/hooks/index.ts +6 -4
  85. package/src/hooks/useGivensState.spec.ts +446 -0
  86. package/src/hooks/useGivensState.ts +337 -0
  87. package/src/hooks/useSuggestOptions.spec.ts +109 -0
  88. package/src/hooks/useSuggestOptions.ts +260 -0
  89. package/src/theme/buildTableCssVars.ts +4 -0
  90. package/src/theme/resolveTheme.spec.ts +13 -0
  91. package/src/theme/resolveTheme.ts +3 -0
  92. package/src/theme/types.ts +9 -0
  93. package/test/README.md +16 -10
  94. package/dist/ServerProvider-Dg5S1qBU.cjs.js +0 -1
  95. package/dist/hooks/useGivensForm.d.ts +0 -35
  96. package/dist/index-MES4MGpQ.cjs.js +0 -309
  97. package/src/hooks/useGivensForm.spec.ts +0 -99
  98. package/src/hooks/useGivensForm.ts +0 -107
@@ -0,0 +1,480 @@
1
+ /**
2
+ * Between a given's value and the two wire forms it takes: a URL query
3
+ * parameter, and the `givens` map on a query request.
4
+ *
5
+ * Given state is URL-addressable so a filtered view is a link you can send,
6
+ * and so a drill can arrive somewhere with its filter already applied. A URL
7
+ * carries strings, while a given is typed, so a value makes a round trip
8
+ * through here on the way out to the address bar and back.
9
+ *
10
+ * The declared type is what decides the reading: `"100"` is the number 100 for
11
+ * a `number` given and the string `"100"` for a `filter<number>`, whose values
12
+ * are filter syntax rather than numbers.
13
+ */
14
+
15
+ import type { GivenValue } from "../../hooks/givenValue";
16
+ import { isFilterType } from "./filterValue";
17
+
18
+ /**
19
+ * A given's value as a query-parameter string, or undefined when there is
20
+ * nothing to put in the URL (an unset given, which the server fills from the
21
+ * model default).
22
+ *
23
+ * An empty string is a real value, not an absence: for a `filter<…>` it is the
24
+ * empty filter, i.e. "All", which is meaningfully different from leaving the
25
+ * declared default in place.
26
+ */
27
+ export function givenToParam(
28
+ value: GivenValue | undefined,
29
+ // Required, not optional: the only thing this decides is a Date's spelling,
30
+ // and a caller that forgot it would silently truncate a timestamp to its
31
+ // date, which is the defect this parameter exists to fix. Better to fail to
32
+ // compile than to lose the time of day again.
33
+ type: string | undefined,
34
+ ): string | undefined {
35
+ if (value === undefined || value === null) return undefined;
36
+ // Written in the same spelling the request body uses, so the URL keeps a
37
+ // timestamp's time of day. Encoding every Date as `YYYY-MM-DD` truncated a
38
+ // `timestamp` or `timestamptz` given the moment it round-tripped through the
39
+ // address bar, and the query then re-ran at midnight against a different
40
+ // instant than the reader had chosen.
41
+ if (value instanceof Date) return dateToRequest(value, type);
42
+ // Refused, not joined. `GivenValue` used to admit `string[]` and this joined
43
+ // on `,` with no escaping, which this codec cannot undo: `paramToGiven`
44
+ // never returns an array for any type, so the list came back as one string,
45
+ // and a value CONTAINING a comma ("Ben & Jerry, Inc") could not even be
46
+ // split back to the right count. The type no longer promises arrays; this
47
+ // stays as a guard for a JavaScript caller the types cannot reach, where
48
+ // dropping the parameter is at least visible as "unset" rather than sending
49
+ // a value the reader never chose.
50
+ if (Array.isArray(value)) return undefined;
51
+ return String(value);
52
+ }
53
+
54
+ /** The three calendar fields a picked day is read from, as dayjs exposes them. */
55
+ export interface PickedDay {
56
+ year(): number;
57
+ /** Zero-based, as `Date` and dayjs both have it. */
58
+ month(): number;
59
+ date(): number;
60
+ }
61
+
62
+ /**
63
+ * The instant to store for a calendar day the user picked in a date control.
64
+ *
65
+ * Built in UTC from the day's own fields, NOT by taking the instant off the
66
+ * picker. MUI's adapter resolves its default timezone to LOCAL here (this SDK
67
+ * loads dayjs's `utc` plugin but not `timezone`), so the value it hands back is
68
+ * local midnight-ish at the reference time-of-day, while both the read side of
69
+ * the control and {@link dateToRequest} take the UTC day. Those two readings
70
+ * differ by a day for any offset that crosses midnight: in Sydney, clicking
71
+ * 5 January stored 4 January and the field then redrew as the 4th, so the click
72
+ * appeared to jump backwards.
73
+ */
74
+ export function pickedDayToUtc(day: PickedDay, previous?: Date): Date {
75
+ // The time of day is carried over from the value being replaced, because a
76
+ // DatePicker offers no way to re-enter it. Zeroing it instead silently moved
77
+ // a `timestamp` given's boundary back to midnight every time the user
78
+ // adjusted the day, which is a different filter, not a re-spelling of the
79
+ // same one. Nothing to carry (a fresh pick, or a `date` given) means
80
+ // midnight, which is what a bare calendar day means anyway.
81
+ return new Date(
82
+ Date.UTC(
83
+ day.year(),
84
+ day.month(),
85
+ day.date(),
86
+ previous?.getUTCHours() ?? 0,
87
+ previous?.getUTCMinutes() ?? 0,
88
+ previous?.getUTCSeconds() ?? 0,
89
+ previous?.getUTCMilliseconds() ?? 0,
90
+ ),
91
+ );
92
+ }
93
+
94
+ /**
95
+ * The spellings {@link parseTemporal} accepts: an ISO-shaped civil datetime with
96
+ * an optional zone suffix. Deliberately strict, and deliberately the only thing
97
+ * that decides.
98
+ */
99
+ const TEMPORAL =
100
+ /^(\d{4})-(\d{1,2})-(\d{1,2})(?:[T\s]+(\d{1,2}):(\d{2})(?::(\d{2}))?(?:\.(\d{1,9}))?)?\s*(Z|[+-]\d{2}:?\d{2}|[+-]\d{2}|UTC|GMT)?$/i;
101
+
102
+ /**
103
+ * A value with its offset `+` put back, if the address bar ate one.
104
+ *
105
+ * A `+` in a query string means a SPACE, so an offset survives only when the
106
+ * writer percent-encoded it. `URLSearchParams` hands us `2024-01-05T10:30:00
107
+ * 05:00` for a hand-pasted `+05:00`, and refusing that made the parser work for
108
+ * the western half of the world and not the eastern: `-05:00` arrived intact
109
+ * and `+05:00` did not, with nothing on screen to say the URL had eaten it
110
+ * rather than the grammar rejecting it.
111
+ *
112
+ * Applied only when a full datetime precedes the group, so nothing else can
113
+ * match, and reached only by the three temporal types (`date`, `timestamp`,
114
+ * `timestamptz`), so it cannot touch a `string` or `filter<…>` value. Kept
115
+ * apart from `parseTemporal` so a REFUSED value is still reported back as it was
116
+ * typed, rather than in the mangled space form the URL delivered.
117
+ */
118
+ function restoreUrlPlus(raw: string): string {
119
+ const trimmed = raw.trim();
120
+ // Whitespace and nothing else: hand back `raw` untouched. Trimming it yields
121
+ // `""`, which has already passed the empty-means-unset check above, so the
122
+ // given was sent as an empty date while its control read as unset.
123
+ //
124
+ // Only that case. An earlier form of this guard returned `raw` whenever no
125
+ // `+` was restored, which stopped trimming ordinary values too: ` 2024-01-05`
126
+ // was then refused while ` 2024-01-05T10:30:00 05:00` still parsed, since the
127
+ // restoring path trimmed and the non-restoring path no longer did. Same
128
+ // leading space, opposite answers, decided by whether the URL had eaten a `+`.
129
+ if (trimmed === "") return raw;
130
+ return trimmed.replace(
131
+ /^(\d{4}-\d{1,2}-\d{1,2}[T\s]+\d{1,2}:\d{2}(?::\d{2})?(?:\.\d{1,9})?)\s+(\d{2}:?\d{2}|\d{2})$/,
132
+ "$1+$2",
133
+ );
134
+ }
135
+
136
+ /**
137
+ * A date or datetime parameter as an instant, or undefined when it is not one.
138
+ *
139
+ * The zone is parsed HERE rather than handed to `new Date`, which is the point
140
+ * of this function and took four attempts to reach. Outside the exact ISO
141
+ * grammar `new Date` is implementation-defined, so delegating to it made the
142
+ * answer depend on the reader's browser. Measured on this machine:
143
+ * `new Date("2024-01-05 10:30:00 UTC")`, which is what BigQuery prints for a
144
+ * TIMESTAMP, gives the right instant in V8 and in Bun and **Invalid Date in
145
+ * Safari's JavaScriptCore**. A unit suite running under Bun cannot see that, so
146
+ * a green test bench would have shipped it.
147
+ *
148
+ * Everything the earlier attempts got wrong is a case here:
149
+ *
150
+ * - **No zone means UTC**, built from the value's own parts. `new Date` reads an
151
+ * offset-less datetime as LOCAL, so one shared link resolved to a different
152
+ * instant per reader and was rewritten to the shifted one on the next report.
153
+ * - **The parts are checked, not just parsed.** `Date.UTC` normalises rather
154
+ * than refusing, so `2024-13-45` arrived as 2025-02-14, silently repairing a
155
+ * mistyped link to a date nobody chose.
156
+ * - **A meridiem is not a zone.** A gate of `\s[A-Z]{2,5}$` matches ` AM`, which
157
+ * put `Dec 25 2024 10:30 AM` back to being read as local time.
158
+ * - **Two-digit offsets count.** `+00` and `-05` are what Postgres and DuckDB
159
+ * print for a TIMESTAMPTZ, and demanding four digits rejected the default
160
+ * output of the two databases Publisher ships with.
161
+ *
162
+ * A spelling this grammar does not cover (`Dec 25 2024`, `12/25/2024`) is passed
163
+ * through, and the server names the given in its error. Narrower than what V8
164
+ * alone would take, on purpose: an answer that depends on the browser is worse
165
+ * than a refusal that does not.
166
+ */
167
+ function parseTemporal(raw: string, calendarDay = false): Date | undefined {
168
+ // A `+` in a query string means a SPACE, so an offset survives the address
169
+ // bar only when the writer percent-encoded it. `URLSearchParams` hands us
170
+ // `2024-01-05T10:30:00 05:00` for a hand-pasted `+05:00`, and refusing that
171
+ // made the parser work for the western half of the world and not the
172
+ // eastern: `-05:00` arrived intact and `+05:00` did not, with nothing on
173
+ // screen to say the URL had eaten it rather than the grammar. Read back as
174
+ // `+` only when a full datetime precedes it, so nothing else can match.
175
+ const text = restoreUrlPlus(raw);
176
+ if (text === "") return undefined;
177
+
178
+ const m = TEMPORAL.exec(text);
179
+ if (!m) return undefined;
180
+ const [, y, mo, d, h, mi, sec, frac, zone] = m;
181
+
182
+ const parts = {
183
+ year: Number(y),
184
+ month: Number(mo),
185
+ day: Number(d),
186
+ hour: Number(h ?? 0),
187
+ minute: Number(mi ?? 0),
188
+ second: Number(sec ?? 0),
189
+ // Truncated to milliseconds, which is all a `Date` holds. Accepting more
190
+ // digits than that is the point: warehouses print microseconds as a
191
+ // matter of course, and capping the pattern at three meant a perfectly
192
+ // ordinary `...:00.123456` failed to parse at all.
193
+ milli: Number((frac ?? "").slice(0, 3).padEnd(3, "0") || 0),
194
+ };
195
+
196
+ const civil = Date.UTC(
197
+ parts.year,
198
+ parts.month - 1,
199
+ parts.day,
200
+ parts.hour,
201
+ parts.minute,
202
+ parts.second,
203
+ parts.milli,
204
+ );
205
+ // Read the parts back off the result. `Date.UTC` normalises rather than
206
+ // refusing, so `2024-13-45` became 2025-02-14 and, because the value is
207
+ // reported back outward, a mistyped link was silently REPAIRED to a date the
208
+ // reader never typed.
209
+ const built = new Date(civil);
210
+ const survived =
211
+ built.getUTCFullYear() === parts.year &&
212
+ built.getUTCMonth() === parts.month - 1 &&
213
+ built.getUTCDate() === parts.day &&
214
+ built.getUTCHours() === parts.hour &&
215
+ built.getUTCMinutes() === parts.minute &&
216
+ built.getUTCSeconds() === parts.second;
217
+ if (!survived) return undefined;
218
+
219
+ // A zone qualifies a time of day, so it is meaningless on a bare date and
220
+ // that combination is refused rather than resolved: `2024-01-05+05`
221
+ // otherwise became 2024-01-04T19:00Z, reported back as the 4th, a day
222
+ // earlier than what was written and silently rewritten into the URL.
223
+ //
224
+ // This catches the literal spelling, NOT the one a URL delivers. A pasted
225
+ // `2024-01-05+05:00` arrives as `2024-01-05 05:00`, which is a well-formed
226
+ // time of day and is read as one; `restoreUrlPlus` deliberately does not
227
+ // fire without a full datetime in front, because there is nothing to
228
+ // distinguish the two readings and guessing wrong is worse than either.
229
+ if (zone !== undefined && h === undefined) return undefined;
230
+
231
+ const offset = zoneOffsetMinutes(zone);
232
+ if (offset === undefined) return undefined;
233
+ const instant = new Date(civil - offset * 60_000);
234
+
235
+ // For a `date` given, and only for a value that reached here CARRYING A TIME
236
+ // (a bare date with a zone was already refused above, for every type), refuse
237
+ // a zone just when applying it MOVES THE DAY. The qualifier is spelled out
238
+ // rather than left implied by position: stating this rule without it reads
239
+ // as "a `date` accepts `Z`", which is false for a bare date.
240
+ //
241
+ // Refusing every zone was the previous attempt and it was far too broad: it
242
+ // rejected `Z`, `UTC` and `+00:00`, so `2024-01-05T00:00:00.000Z`, which is
243
+ // exactly what `Date#toISOString()` emits and therefore what any host or
244
+ // link generator produces, stopped resolving at all. What the day-shift
245
+ // defect actually needs is this comparison, and nothing wider.
246
+ if (
247
+ calendarDay &&
248
+ zone !== undefined &&
249
+ instant.getUTCDate() !== parts.day
250
+ ) {
251
+ return undefined;
252
+ }
253
+ return instant;
254
+ }
255
+
256
+ /**
257
+ * Minutes east of UTC for a matched zone suffix; absent means the value is
258
+ * naive, so UTC. Undefined for a suffix that is shaped like an offset but is
259
+ * not one.
260
+ *
261
+ * The range check is the same principle as the parts-survived check above:
262
+ * refuse rather than silently produce a different instant. Without it `+99:99`
263
+ * was read as 99 hours and 99 minutes, so `2024-01-05T10:30:00+99:99` resolved
264
+ * to 1 January, four days off, with nothing to say so. Real offsets run from
265
+ * -12:00 to +14:00 (Kiribati is the outer edge) and their minutes are always
266
+ * under 60.
267
+ */
268
+ function zoneOffsetMinutes(zone: string | undefined): number | undefined {
269
+ if (zone === undefined) return 0;
270
+ const z = zone.toUpperCase();
271
+ if (z === "Z" || z === "UTC" || z === "GMT") return 0;
272
+ const m = /^([+-])(\d{2}):?(\d{2})?$/.exec(z);
273
+ if (!m) return undefined;
274
+ const mins = Number(m[3] ?? 0);
275
+ if (mins > 59) return undefined;
276
+ const minutes = Number(m[2]) * 60 + mins;
277
+ // Not symmetric, whatever the tidier `Math.abs` version would suggest. The
278
+ // real range runs -12:00 (Baker Island) to +14:00 (Kiribati), so a single
279
+ // ±14:00 bound admitted -13:00 and -14:00, which exist nowhere.
280
+ const east = m[1] !== "-";
281
+ if (minutes > (east ? 14 * 60 : 12 * 60)) return undefined;
282
+ return east ? minutes : -minutes;
283
+ }
284
+
285
+ /**
286
+ * Whether `""` is a value the user can mean for this type, rather than "unset".
287
+ *
288
+ * A DENYLIST, to agree with `paramToGiven`'s `default` arm. As an allowlist it
289
+ * named the three types that keep `""` and treated everything else as unset,
290
+ * while the switch below passes any unrecognised type through untouched: a
291
+ * deliberate `""` on a type neither knew about was written to the URL and read
292
+ * back as unset. Naming the types that cannot hold an empty value keeps the two
293
+ * in step as new types arrive.
294
+ */
295
+ function isEmptyStringMeaningful(type: string | undefined): boolean {
296
+ return !EMPTY_MEANS_UNSET.has(type ?? "");
297
+ }
298
+
299
+ /** Types with no empty value, so `?X=` on one of them means "unset". */
300
+ const EMPTY_MEANS_UNSET = new Set([
301
+ "boolean",
302
+ "number",
303
+ "date",
304
+ "timestamp",
305
+ "timestamptz",
306
+ ]);
307
+
308
+ /** Read a query-parameter string back as a value of the declared type. */
309
+ export function paramToGiven(
310
+ type: string | undefined,
311
+ raw: string,
312
+ ): GivenValue {
313
+ // An empty parameter means UNSET for every type that has no empty value,
314
+ // so it is dropped here rather than carried as `""`. `?FLAG=` was reaching
315
+ // the request as `{"FLAG": ""}`, which no boolean, number or date can be,
316
+ // while the control showed the model default or an empty box: the screen
317
+ // and the request disagreed, and nothing on screen said so. `string` and
318
+ // `filter<...>` are deliberately excluded, because there the empty string is
319
+ // a value a user can mean, and typing it is how they ask for it.
320
+ if (raw === "" && !isEmptyStringMeaningful(type)) return null;
321
+
322
+ // A filter's value IS a string of filter syntax, whatever it filters on, so
323
+ // it never gets coerced to the inner type.
324
+ if (isFilterType(type)) return raw;
325
+
326
+ switch (type) {
327
+ case "number": {
328
+ // `Number("")` is 0, not NaN, so an empty parameter would arrive as a
329
+ // deliberate zero and filter accordingly. Treated as unparseable
330
+ // instead, which is what the pass-through below is for.
331
+ const parsed = raw.trim() === "" ? NaN : Number(raw);
332
+ // A URL is user-editable, so a value that is not a number is passed
333
+ // through rather than turned into NaN; the server rejects it with a
334
+ // message naming the given, which beats a silently empty control.
335
+ return Number.isFinite(parsed) ? parsed : raw;
336
+ }
337
+ case "boolean":
338
+ // Passed through when it is neither, the way the number and date cases
339
+ // deliberately do: a URL is hand-editable, and coercing `?ACTIVE=yes`
340
+ // to `false` sends the OPPOSITE of what was typed with no complaint.
341
+ // The raw text gets an error naming the given instead.
342
+ if (raw === "true") return true;
343
+ if (raw === "false") return false;
344
+ return raw;
345
+ case "date":
346
+ case "timestamp":
347
+ case "timestamptz": {
348
+ // A URL is hand-editable, so a value that is not a date is passed
349
+ // through rather than becoming an Invalid Date; the server rejects it
350
+ // with a message naming the given.
351
+ //
352
+ // A `date` refuses a zone that would MOVE THE DAY, which is why the
353
+ // type reaches here at all. A date is a calendar day, so resolving
354
+ // `2024-01-05T00:30:00+05:30` lands on the 4th, and because the value
355
+ // is reported back outward the reader's address bar was then rewritten
356
+ // to a day they never wrote. On a value that CARRIES A TIME, a zone
357
+ // leaving the day alone reads normally, `Z` and `+00:00` included:
358
+ // refusing every zone was tried and was far too broad, since it
359
+ // rejected `Date#toISOString()` output. A zone on a BARE date is a
360
+ // separate rule and is always refused, for every temporal type, since
361
+ // an offset has no time of day to qualify. Both comparisons live in
362
+ // `parseTemporal`.
363
+ // The RESTORED text on refusal, not `raw`: otherwise a value the URL
364
+ // mangled is reported straight back out in its mangled form, so the
365
+ // reader's address bar loses the `+` they typed.
366
+ const text = restoreUrlPlus(raw);
367
+ return parseTemporal(text, type === "date") ?? text;
368
+ }
369
+ default:
370
+ // No `array<…>` case. `malloyGivenToApi` renders a non-filter given
371
+ // as the bare `type.type`, so `"array<…>"` is a spelling the server
372
+ // cannot produce, and Malloy emits only scalar parameter types for
373
+ // givens anyway. The branch that used to be here split on `,` with no
374
+ // escaping, so it was also the one place a value could be corrupted
375
+ // on the URL round trip.
376
+ return raw;
377
+ }
378
+ }
379
+
380
+ /**
381
+ * The whole control row as query parameters, dropping unset givens so a
382
+ * default-state dashboard has a clean URL.
383
+ */
384
+ export function givensToParams(
385
+ values: ReadonlyMap<string, GivenValue>,
386
+ declaredTypes: ReadonlyMap<string, string | undefined>,
387
+ ): Record<string, string> {
388
+ const params: Record<string, string> = {};
389
+ for (const [name, value] of values) {
390
+ const encoded = givenToParam(value, declaredTypes.get(name));
391
+ if (encoded !== undefined) params[name] = encoded;
392
+ }
393
+ return params;
394
+ }
395
+
396
+ /**
397
+ * A `Date` in the spelling the server accepts for the given's declared type.
398
+ *
399
+ * The three time types take three different forms, and each rejects the other
400
+ * two, so this cannot be one blanket `toISOString()`:
401
+ *
402
+ * - `date` wants a bare `YYYY-MM-DD`, and rejects anything longer.
403
+ * - `timestamp` is naive: an offset or a trailing `Z` is rejected outright,
404
+ * with a message pointing at `timestamptz`.
405
+ * - `timestamptz` is the instant, and is the only one that takes the full ISO
406
+ * string.
407
+ *
408
+ * Everything is read in UTC, matching the controls, which pick dates through
409
+ * dayjs's UTC plugin.
410
+ */
411
+ function dateToRequest(value: Date, type: string | undefined): string {
412
+ const iso = value.toISOString();
413
+ switch (type) {
414
+ case "timestamptz":
415
+ return iso;
416
+ case "timestamp":
417
+ // Same instant with the zone marker dropped, which is what "naive"
418
+ // means here.
419
+ return iso.slice(0, -1);
420
+ default:
421
+ return iso.slice(0, 10);
422
+ }
423
+ }
424
+
425
+ /**
426
+ * Control values as the `givens` map a query request takes.
427
+ *
428
+ * Unlike the URL form this keeps types: a `number` given goes over as a
429
+ * number, because the server type-checks each value against its declaration.
430
+ * Only `Date` needs converting, since JSON has no date, and converting it
431
+ * needs the declared type (see {@link dateToRequest}).
432
+ *
433
+ * `only` narrows the map to a named subset, which is how a composite tile is
434
+ * run with just the givens it references instead of the whole control row.
435
+ */
436
+ export function givensToRequest(
437
+ values: ReadonlyMap<string, GivenValue>,
438
+ declaredTypes: ReadonlyMap<string, string | undefined>,
439
+ only?: readonly string[],
440
+ ): Record<string, unknown> {
441
+ const names = only ? new Set(only) : undefined;
442
+ const request: Record<string, unknown> = {};
443
+ for (const [name, value] of values) {
444
+ if (names && !names.has(name)) continue;
445
+ if (value === null || value === undefined) continue;
446
+ // Arrays are refused HERE too, not just in `givenToParam`. `GivenValue`
447
+ // no longer admits one, but the two functions are the URL side and the
448
+ // REQUEST side of the same rule, and fixing only the URL side left an
449
+ // array from a JavaScript caller filtering every query while being
450
+ // invisible in the address bar: the two disagreed about the same value,
451
+ // which is the drift this pair exists to avoid.
452
+ if (Array.isArray(value)) continue;
453
+ request[name] =
454
+ value instanceof Date
455
+ ? dateToRequest(value, declaredTypes.get(name))
456
+ : value;
457
+ }
458
+ return request;
459
+ }
460
+
461
+ /**
462
+ * Read query parameters into control values, keeping only the names the
463
+ * dashboard actually declares.
464
+ *
465
+ * A URL can name anything; binding a given the model does not surface fails the
466
+ * query outright, so an unrecognized parameter is ignored rather than passed
467
+ * along. That also means an unrelated query parameter on the page's URL (a
468
+ * tracking tag, say) cannot break the dashboard.
469
+ */
470
+ export function paramsToGivens(
471
+ params: Record<string, string>,
472
+ declaredTypes: ReadonlyMap<string, string | undefined>,
473
+ ): Map<string, GivenValue> {
474
+ const values = new Map<string, GivenValue>();
475
+ for (const [name, raw] of Object.entries(params)) {
476
+ if (!declaredTypes.has(name)) continue;
477
+ values.set(name, paramToGiven(declaredTypes.get(name), raw));
478
+ }
479
+ return values;
480
+ }
@@ -1,8 +1,11 @@
1
1
  export { AnalyzePackageButton } from "./AnalyzePackageButton";
2
- export { useRouterClickHandler } from "./click_helper";
2
+ export { useRouterClickHandler, type NavigationClick } from "./click_helper";
3
+ export * from "./Dashboard";
3
4
  export * from "./DataAppViewer";
5
+ export * from "./drill";
4
6
  export * from "./Environment";
5
7
  export * from "./filter";
8
+ export * from "./given";
6
9
  export * from "./Home";
7
10
  export * from "./Loading";
8
11
  export * from "./Materializations";
@@ -6,9 +6,33 @@ import { Card, CardContent, CardMedia, styled } from "@mui/material";
6
6
  * the hex values inline so the brand can be retuned in one place.
7
7
  */
8
8
  export const MALLOY_BRAND = {
9
- teal: "#14b3cb", // light wing of the M (governed reports)
10
- orange: "#e47404", // right wing of the M (semantic models)
11
- darkBlue: "#1474a4", // deep shadow of the M (package data)
9
+ teal: "#14b3cb", // light wing of the M
10
+ orange: "#e47404", // right wing of the M
11
+ darkBlue: "#1474a4", // deep shadow of the M
12
+ } as const;
13
+
14
+ /**
15
+ * Accents that extend the logo's three, for the places that need more colors
16
+ * than the brand supplies. The package page gives every kind of content its
17
+ * own, and three kinds became six.
18
+ *
19
+ * Their own family rather than borrowings from the chart series
20
+ * (`theme/defaults.ts`), because the two have opposite constraints. A series
21
+ * color fills a large area and can be light; these sit behind a white icon as a
22
+ * small solid backplate, where the icon has to stay legible. Measured against
23
+ * white: violet 5.5:1, magenta 4.6:1, moss 3.9:1, so all three clear 3:1, the
24
+ * WCAG minimum for a graphical object. The nearest series hues do not: `#aacd85`
25
+ * sage is 1.8:1, `#ec72b8` pink 2.7:1, `#b87ced` violet 2.9:1.
26
+ *
27
+ * The bar is stated for these three only. `MALLOY_BRAND` above is inherited from
28
+ * the logo and teal is 2.5:1, which is below it, so the row it backs has a
29
+ * white-on-teal glyph that does not meet the same standard. Pre-existing, not
30
+ * introduced here, and it is a brand decision rather than a component one.
31
+ */
32
+ export const MALLOY_ACCENT = {
33
+ violet: "#7c4dcc",
34
+ magenta: "#c2478f",
35
+ moss: "#5c8f3f",
12
36
  } as const;
13
37
 
14
38
  /**
@@ -0,0 +1,51 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import { readdirSync } from "fs";
3
+ import { join } from "path";
4
+ import { DOC_LINKS } from "./docLinks";
5
+
6
+ const REPO_ROOT = join(import.meta.dir, "..", "..", "..", "..");
7
+ const REPO_DOCS_PREFIX =
8
+ "https://github.com/malloydata/publisher/blob/main/docs/";
9
+
10
+ /**
11
+ * Every DOC_LINKS entry pointing into this repo names a file that is actually
12
+ * here. These are rendered as links on the Console home page, so a doc renamed
13
+ * or moved without updating the constant ships a 404 to every reader, and
14
+ * nothing else in the build would notice: the value is a string, and a string
15
+ * is as valid wrong as right.
16
+ *
17
+ * What this pins, stated precisely, because the obvious reading is wider than
18
+ * the check: the target exists **in this working tree**. It cannot pin that the
19
+ * URL resolves today, because the URL names `blob/main` and a doc added on a
20
+ * branch is here and not on `main` until that branch merges. That gap is a
21
+ * merge-ordering question, handled by sequencing the pull request, not by a
22
+ * test. What this does catch is the durable half: a rename, a deletion, or a
23
+ * typo, from the moment it happens.
24
+ *
25
+ * Compared against exact basenames rather than with `existsSync`, because macOS
26
+ * is case-insensitive: `dashboards.md` mistyped as `Dashboards.md` passes an
27
+ * existence check on the author's machine and 404s on GitHub, which is
28
+ * case-sensitive. Linux CI would catch it, but only after the author had seen
29
+ * green.
30
+ */
31
+ describe("DOC_LINKS", () => {
32
+ const repoDocs = Object.entries(DOC_LINKS).filter(([, url]) =>
33
+ url.startsWith(REPO_DOCS_PREFIX),
34
+ );
35
+
36
+ // Guards the filter itself. Were the prefix to drift from the constant, every
37
+ // case below would vanish and the suite would still be green: zero passing
38
+ // assertions reads identically to zero broken links.
39
+ it("covers the in-repo docs", () => {
40
+ expect(repoDocs.length).toBeGreaterThan(0);
41
+ expect(repoDocs.length).toBe(
42
+ Object.values(DOC_LINKS).filter((url) => url.includes("/blob/"))
43
+ .length,
44
+ );
45
+ });
46
+
47
+ it.each(repoDocs)("%s resolves to a file in docs/", (_key, url) => {
48
+ const relative = url.slice(REPO_DOCS_PREFIX.length);
49
+ expect(readdirSync(join(REPO_ROOT, "docs"))).toContain(relative);
50
+ });
51
+ });
@@ -4,6 +4,20 @@
4
4
 
5
5
  const DOCS_BASE = "https://docs.malloydata.dev/documentation";
6
6
  const PUBLISHING_BASE = `${DOCS_BASE}/user_guides/publishing`;
7
+ // Publisher's own reference docs, which live in the repo rather than on the
8
+ // docs site. Newer features are documented here first, and for some of them
9
+ // this is the only write-up there is.
10
+ //
11
+ // Pinned to `main`, so a link is live the moment its doc merges and dead until
12
+ // then. `docLinks.spec.ts` checks each target exists in this repo, which catches
13
+ // a rename or a deletion but cannot catch a doc that is here and not yet on
14
+ // `main`; adding a key for a doc still on a branch is a sequencing decision, not
15
+ // something the build can see.
16
+ //
17
+ // One of these is in that state: `choosing-a-surface.md` ships in this change,
18
+ // so its link goes live the moment this merges. The other five are on `main`
19
+ // already, `dashboards.md` as of the dashboards slice landing.
20
+ const REPO_DOCS = "https://github.com/malloydata/publisher/blob/main/docs";
7
21
 
8
22
  export const DOC_LINKS = {
9
23
  // Documentation landing page.
@@ -14,4 +28,16 @@ export const DOC_LINKS = {
14
28
  explorer: `${PUBLISHING_BASE}/explorer`,
15
29
  // Connect Claude and other AI assistants over MCP.
16
30
  mcpAgents: `${PUBLISHING_BASE}/mcp_agents`,
31
+ // Notebooks, dashboards, and HTML data apps, and how to pick between them.
32
+ surfaces: `${REPO_DOCS}/choosing-a-surface.md`,
33
+ // Dashboards declared as tags on a Malloy query.
34
+ dashboards: `${REPO_DOCS}/dashboards.md`,
35
+ // No-build HTML pages served from a package's public/ directory.
36
+ dataApps: `${REPO_DOCS}/html-data-apps.md`,
37
+ // Runtime parameters: filter controls, row-level access, source gates.
38
+ givens: `${REPO_DOCS}/givens.md`,
39
+ // Persisting an expensive source into a table, on demand or on a cron.
40
+ materialization: `${REPO_DOCS}/materialization.md`,
41
+ // Database and query-engine connections.
42
+ connections: `${REPO_DOCS}/connections.md`,
17
43
  } as const;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * UI-side value for a given. Mirrors the JS shapes the server accepts for
3
+ * `givens` runtime values, plus `Date` (serialized before send: see
4
+ * `givensToRequest`).
5
+ *
6
+ * Its own module because every control, codec, and hook in the givens path
7
+ * speaks it, and it should not have to be imported from whichever hook happens
8
+ * to be canonical this week.
9
+ */
10
+ export type GivenValue = string | number | boolean | Date | null;
@@ -34,8 +34,10 @@ export {
34
34
 
35
35
  // Givens hooks and types
36
36
  export { useModelGivens } from "./useModelGivens";
37
+ export { type GivenValue } from "./givenValue";
37
38
  export {
38
- useGivensForm,
39
- type GivenValue,
40
- type UseGivensFormResult,
41
- } from "./useGivensForm";
39
+ useGivensState,
40
+ type UseGivensStateOptions,
41
+ type UseGivensStateResult,
42
+ } from "./useGivensState";
43
+ export { useSuggestOptions } from "./useSuggestOptions";