derive-dashboards 0.12.1 → 0.13.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.
@@ -1,13 +1,15 @@
1
1
  import { useDashboards } from "../context.mjs";
2
- import { useEscapeToLeave } from "./escape.mjs";
3
2
  import { CalcSection } from "./Calcs.mjs";
3
+ import { useEscapeToLeave } from "./escape.mjs";
4
4
  import { ClearedOptions, DisplayOptions } from "./DisplayOptions.mjs";
5
5
  import { FilterSection } from "./Filters.mjs";
6
6
  import { GroupSection } from "./Groups.mjs";
7
+ import { KindNeeds } from "./KindNeeds.mjs";
7
8
  import { Preview as Preview$1 } from "./Preview.mjs";
8
9
  import { Chip, KeptNote, MeasureSection, OpenSection } from "./Sections.mjs";
9
10
  import { TimeSection } from "./Time.mjs";
10
11
  import { useBuilder } from "./useBuilder.mjs";
12
+ import { kindLine } from "./words.mjs";
11
13
  import { TITLE_MOST } from "derive-dashboards/core";
12
14
  import { useEffect, useRef, useState } from "react";
13
15
  import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
@@ -28,9 +30,10 @@ function Header({ b, onSave, onCancel, saveLabel, saving }) {
28
30
  const { messages } = useDashboards();
29
31
  const widget = b.widget;
30
32
  const title = useRef(null);
33
+ const editing = b.state.base !== void 0;
31
34
  useEffect(() => {
32
- title.current?.focus();
33
- }, []);
35
+ if (editing) title.current?.focus();
36
+ }, [editing]);
34
37
  return /* @__PURE__ */ jsxs("header", {
35
38
  className: "dd-builder-header",
36
39
  children: [
@@ -64,16 +67,19 @@ function Header({ b, onSave, onCancel, saveLabel, saving }) {
64
67
  ]
65
68
  });
66
69
  }
67
- /** The chart kinds the provider can draw, over the preview. */
70
+ /**
71
+ * The chart kinds the provider can draw, over the preview, from the first screen on: what can
72
+ * be made is in sight before a measure is picked, and a kind picked first is kept.
73
+ */
68
74
  function Kinds({ b }) {
69
75
  const { messages, renderers } = useDashboards();
70
- if (b.state.query === void 0) return null;
71
76
  return /* @__PURE__ */ jsx("fieldset", {
72
77
  className: "dd-chips dd-kinds",
73
78
  "aria-label": messages.chartKinds,
74
79
  children: kindsOf(renderers).map((k) => /* @__PURE__ */ jsx(Chip, {
75
80
  on: b.state.kind === k,
76
81
  label: messages.kinds[k] ?? k,
82
+ title: kindLine(messages, k),
77
83
  onClick: () => {
78
84
  b.setKind(k);
79
85
  }
@@ -150,12 +156,22 @@ function OptionsSection({ b }) {
150
156
  function WidgetBuilder({ initial, backLabel, controls, error, ...actions }) {
151
157
  const b = useBuilder(initial);
152
158
  const { messages } = useDashboards();
153
- const [open, setOpen] = useState(initial === void 0 ? messages.measure : void 0);
159
+ const [fold, setFold] = useState({
160
+ open: initial === void 0 ? messages.measure : void 0,
161
+ search: false
162
+ });
163
+ const setOpen = (open, search = false) => {
164
+ setFold({
165
+ open,
166
+ search
167
+ });
168
+ };
154
169
  const root = useRef(null);
155
170
  useEscapeToLeave(root, JSON.stringify(b.widget), actions.onCancel);
156
171
  return /* @__PURE__ */ jsxs("div", {
157
172
  className: "dd-builder",
158
173
  ref: root,
174
+ "data-empty": b.state.query === void 0,
159
175
  children: [
160
176
  backLabel === void 0 ? null : /* @__PURE__ */ jsxs("button", {
161
177
  type: "button",
@@ -172,39 +188,41 @@ function WidgetBuilder({ initial, backLabel, controls, error, ...actions }) {
172
188
  role: "alert",
173
189
  children: error
174
190
  }),
175
- /* @__PURE__ */ jsxs("div", {
191
+ /* @__PURE__ */ jsx("div", {
176
192
  className: "dd-builder-body",
177
- children: [/* @__PURE__ */ jsxs("div", {
178
- className: "dd-builder-panel",
179
- children: [/* @__PURE__ */ jsxs(OpenSection.Provider, {
180
- value: {
181
- open,
182
- setOpen
183
- },
193
+ children: /* @__PURE__ */ jsxs(OpenSection.Provider, {
194
+ value: {
195
+ ...fold,
196
+ setOpen
197
+ },
198
+ children: [/* @__PURE__ */ jsxs("div", {
199
+ className: "dd-builder-panel",
184
200
  children: [
185
201
  /* @__PURE__ */ jsx(MeasureSection, { b }),
186
202
  /* @__PURE__ */ jsx(TimeSection, { b }),
187
203
  /* @__PURE__ */ jsx(GroupSection, { b }),
188
204
  /* @__PURE__ */ jsx(FilterSection, { b }),
189
- /* @__PURE__ */ jsx(CalcSection, { b })
205
+ /* @__PURE__ */ jsx(CalcSection, { b }),
206
+ /* @__PURE__ */ jsx(OptionsSection, { b })
207
+ ]
208
+ }), /* @__PURE__ */ jsxs("div", {
209
+ className: "dd-builder-main",
210
+ children: [
211
+ /* @__PURE__ */ jsx(Kinds, { b }),
212
+ /* @__PURE__ */ jsx(KindNeeds, { b }),
213
+ /* @__PURE__ */ jsx(DashboardNote, {
214
+ b,
215
+ controls
216
+ }),
217
+ /* @__PURE__ */ jsx(KeptNote, { b }),
218
+ /* @__PURE__ */ jsx(ClearedOptions, { b }),
219
+ /* @__PURE__ */ jsx(Preview$1, {
220
+ b,
221
+ data: true
222
+ })
190
223
  ]
191
- }), /* @__PURE__ */ jsx(OptionsSection, { b })]
192
- }), /* @__PURE__ */ jsxs("div", {
193
- className: "dd-builder-main",
194
- children: [
195
- /* @__PURE__ */ jsx(Kinds, { b }),
196
- /* @__PURE__ */ jsx(DashboardNote, {
197
- b,
198
- controls
199
- }),
200
- /* @__PURE__ */ jsx(KeptNote, { b }),
201
- /* @__PURE__ */ jsx(ClearedOptions, { b }),
202
- /* @__PURE__ */ jsx(Preview$1, {
203
- b,
204
- data: true
205
- })
206
- ]
207
- })]
224
+ })]
225
+ })
208
226
  })
209
227
  ]
210
228
  });
@@ -1,4 +1,5 @@
1
1
  import { keepsOthers, pruned } from "./display.mjs";
2
+ import { timedFor } from "./kind-needs.mjs";
2
3
  import { fittingTitle, sourceIn } from "./labels.mjs";
3
4
  import { displayOf, parseMemberId } from "derive-dashboards/core";
4
5
  // src/builder/base.ts
@@ -156,11 +157,26 @@ function defaultTime(meta, name) {
156
157
  range: DEFAULT_RANGE
157
158
  };
158
159
  }
159
- function setKind(state, kind) {
160
+ /**
161
+ * A query started under a kind the person already picked: a kind that runs over time is given
162
+ * its dates at once (`timedFor`), as when it is picked for a query already there.
163
+ */
164
+ function startQuery(meta, state, query) {
165
+ return withQuery(meta, state, state.kindSet ? timedFor(state.kind, query) : query);
166
+ }
167
+ /**
168
+ * Picks the kind, before a measure or after. A kind that runs over time gives a lone value its
169
+ * dates (`timedFor`), so a line is a line as soon as it is picked.
170
+ */
171
+ function setKind(meta, state, kind) {
172
+ const query = state.query === void 0 ? void 0 : timedFor(kind, state.query);
173
+ const title = state.titleSet ? state.title : fittingTitle(meta, query);
160
174
  const next = {
161
175
  ...state,
162
176
  kind,
163
- kindSet: true
177
+ kindSet: true,
178
+ query,
179
+ title
164
180
  };
165
181
  return fitDisplay(state.query, kind === state.kind ? next : fitWhole(next));
166
182
  }
@@ -196,4 +212,4 @@ function widgetOf(state) {
196
212
  ...Object.keys(display).length === 0 ? {} : { display }
197
213
  };
198
214
  }
199
- export { DEFAULT_RANGE, cleared, defaultTime, emptyBuilder, fitWhole, freshId, keptAside, lastPart, refersTo, setKind, setTitle, widgetOf, withQuery };
215
+ export { DEFAULT_RANGE, cleared, defaultTime, emptyBuilder, fitWhole, freshId, keptAside, lastPart, refersTo, setKind, setTitle, startQuery, widgetOf, withQuery };
@@ -0,0 +1,8 @@
1
+ import { StoredQuery } from "derive-dashboards/core";
2
+ /**
3
+ * What a kind still asks of a query before it draws what its name says: dates to run along
4
+ * (`time`), the whole period where it has no time axis (`whole`), a field whose values it
5
+ * compares (`split`), a second field to cross the first with (`cross`), or steps (`steps`: a
6
+ * second value, or a field whose values are the steps).
7
+ */
8
+ export type Need = "time" | "whole" | "split" | "cross" | "steps";
@@ -0,0 +1,64 @@
1
+ import { grainFor } from "./ranges.mjs";
2
+ // src/builder/kind-needs.ts
3
+ /** The kinds that draw a value over time. */
4
+ const OVER_TIME = /* @__PURE__ */ new Set([
5
+ "line",
6
+ "area",
7
+ "multiples"
8
+ ]);
9
+ /** The kinds that compare the values of a field, or periods: one value alone is one bar or one row. */
10
+ const COMPARES = /* @__PURE__ */ new Set(["bar", "list"]);
11
+ /** What of a query a kind draws from. */
12
+ function shapeOf(query) {
13
+ return {
14
+ groups: (query.dimensions ?? []).length,
15
+ values: query.measures.length + (query.calc ?? []).length,
16
+ timed: query.time?.grain !== void 0,
17
+ dated: query.time !== void 0
18
+ };
19
+ }
20
+ /** A value with nothing to set it beside: no field, no periods. */
21
+ const lone = (shape) => shape.groups === 0 && !shape.timed;
22
+ /** What a kind that runs over time asks for: its dates, a field where it has none, or a series for each small chart. */
23
+ function overTimeNeed(kind, shape) {
24
+ if (!shape.timed) {
25
+ if (shape.dated) return "time";
26
+ return shape.groups === 0 ? "split" : void 0;
27
+ }
28
+ return kind === "multiples" && shape.groups === 0 && shape.values < 2 ? "split" : void 0;
29
+ }
30
+ /** What a funnel asks for: the whole period, since it has no time axis, then steps. */
31
+ function funnelNeed(shape) {
32
+ if (shape.timed) return "whole";
33
+ return shape.groups === 0 && shape.values < 2 ? "steps" : void 0;
34
+ }
35
+ /** What the other kinds ask for; none for a number, a table, or a kind of the host's own. */
36
+ function otherNeed(kind, shape) {
37
+ if (COMPARES.has(kind)) return lone(shape) ? "split" : void 0;
38
+ if (kind === "heatmap") return shape.groups + Number(shape.timed) < 2 ? "cross" : void 0;
39
+ return kind === "funnel" ? funnelNeed(shape) : void 0;
40
+ }
41
+ /** What the kind still asks of the query; none when it has what it draws from, or there is no query yet. */
42
+ function needOf(kind, query) {
43
+ if (query === void 0) return void 0;
44
+ const shape = shapeOf(query);
45
+ return OVER_TIME.has(kind) ? overTimeNeed(kind, shape) : otherNeed(kind, shape);
46
+ }
47
+ /**
48
+ * The query with a grain where its kind runs over time and it holds a lone value with dates:
49
+ * the grain its range reads best at. Asked when a person picks the kind, and when a query
50
+ * starts under a kind already picked; never after, so a person who then asks for the whole
51
+ * period keeps it. A query with a field is left as built: its kind then says it asks for dates.
52
+ */
53
+ function timedFor(kind, query) {
54
+ const time = query.time;
55
+ if (!OVER_TIME.has(kind) || time === void 0 || !lone(shapeOf(query))) return query;
56
+ return {
57
+ ...query,
58
+ time: {
59
+ ...time,
60
+ grain: grainFor(time.range)
61
+ }
62
+ };
63
+ }
64
+ export { needOf, timedFor };
@@ -0,0 +1,68 @@
1
+ // src/builder/ranges.ts
2
+ /** The quick ranges, each with the grain it reads best at. */
3
+ const RANGES = [
4
+ {
5
+ range: {
6
+ last: 7,
7
+ unit: "day",
8
+ includeCurrent: true
9
+ },
10
+ grain: "day"
11
+ },
12
+ {
13
+ range: {
14
+ last: 30,
15
+ unit: "day",
16
+ includeCurrent: true
17
+ },
18
+ grain: "day"
19
+ },
20
+ {
21
+ range: {
22
+ last: 90,
23
+ unit: "day",
24
+ includeCurrent: true
25
+ },
26
+ grain: "week"
27
+ },
28
+ {
29
+ range: {
30
+ last: 12,
31
+ unit: "month",
32
+ includeCurrent: true
33
+ },
34
+ grain: "month"
35
+ }
36
+ ];
37
+ /** A unit in days, near enough to tell a week of buckets from a year of them. */
38
+ const DAYS = {
39
+ hour: 1 / 24,
40
+ day: 1,
41
+ week: 7,
42
+ month: 30,
43
+ quarter: 90,
44
+ year: 360
45
+ };
46
+ const DAY_MS = 864e5;
47
+ /** How many days a range spans, near enough to choose a grain by. */
48
+ function daysOf(range) {
49
+ if ("last" in range) return range.last * DAYS[range.unit];
50
+ return (Date.parse(range.to) - Date.parse(range.from)) / DAY_MS;
51
+ }
52
+ /** The longest range, in days, each grain is the best reading of; a year reads anything longer. */
53
+ const UP_TO = [
54
+ [2, "hour"],
55
+ [45, "day"],
56
+ [200, "week"],
57
+ [1100, "month"],
58
+ [3700, "quarter"]
59
+ ];
60
+ /**
61
+ * The grain a range reads best at when it is first drawn over time: the finest that draws a
62
+ * few dozen buckets at most. A quick range's own grain (`RANGES`) is what this gives for it.
63
+ */
64
+ function grainFor(range) {
65
+ const days = daysOf(range);
66
+ return UP_TO.find(([most]) => days <= most)?.[1] ?? "year";
67
+ }
68
+ export { RANGES, grainFor };
@@ -1,4 +1,4 @@
1
- import { DEFAULT_RANGE, cleared, defaultTime, fitWhole, freshId, lastPart, refersTo, withQuery } from "./base.mjs";
1
+ import { DEFAULT_RANGE, cleared, defaultTime, fitWhole, freshId, lastPart, refersTo, startQuery, withQuery } from "./base.mjs";
2
2
  import { setOption, toggleEntry } from "./display.mjs";
3
3
  import { addCondition, removeNode, setMode } from "./filter-tree.mjs";
4
4
  import { formulaRefs, parseMemberId } from "derive-dashboards/core";
@@ -12,7 +12,7 @@ function addMeasure(meta, state, measure) {
12
12
  const current = state.query;
13
13
  if (current === void 0 || current.source !== name) {
14
14
  const time = defaultTime(meta, name);
15
- return withQuery(meta, state, {
15
+ return startQuery(meta, state, {
16
16
  kind: "aggregate",
17
17
  source: name,
18
18
  measures: [{
@@ -44,9 +44,7 @@ function useBuilder(initial) {
44
44
  setTitle: on(setTitle),
45
45
  setDisplay: on(setDisplay),
46
46
  toggleDisplay: on(toggleDisplay),
47
- setKind: (kind) => {
48
- setState((s) => setKind(s, kind));
49
- },
47
+ setKind: on(setKind),
50
48
  labelOf: (member) => labelOf(meta, member),
51
49
  /** An entry of the query by its id: a measure's label, a group's field, or a calc's name. */
52
50
  entryLabel: (id) => entryLabelOf(meta, state.query, id),
@@ -1,5 +1,6 @@
1
1
  import { GroupMode } from "./filter-tree.mjs";
2
2
  import { FormulaProblem } from "./formula-parse.mjs";
3
+ import { Need } from "./kind-needs.mjs";
3
4
  import { DisplayOptionName, Grain, Op } from "derive-dashboards/core";
4
5
  /** The units a rolling range counts in. */
5
6
  export type RangeUnit = Exclude<Grain, "hour">;
@@ -72,4 +73,17 @@ export type BuilderMessages = {
72
73
  nothingToSet: string;
73
74
  /** Said in Options after a change cleared display options that no longer fit, by their names. */
74
75
  optionsCleared: (names: readonly string[]) => string;
76
+ /**
77
+ * What each chart kind is, in a line: its chip's title, and under its sketch before a measure
78
+ * is picked. A kind left out, as a renderer of the host's own is, says nothing.
79
+ */
80
+ kindSays: Readonly<Record<string, string>>;
81
+ /**
82
+ * Over the preview, what the picked kind still asks of the widget before it draws what its
83
+ * name says: "A heatmap crosses two fields: one down, one across." What would give it is
84
+ * offered after it, one press each.
85
+ */
86
+ kindNeeds: (kind: string, need: Need) => string;
87
+ /** After the fields offered for a kind's need: opens Group by, to search every field. */
88
+ moreFields: string;
75
89
  };
@@ -1,4 +1,21 @@
1
1
  // src/builder/words.ts
2
+ /** What a kind asks for, where the kind has words of its own for it. */
3
+ const ENGLISH_NEEDS = {
4
+ "line.time": "A line runs over time.",
5
+ "area.time": "An area runs over time.",
6
+ "multiples.time": "Small multiples run over time.",
7
+ "bar.split": "A bar chart compares the values of a field, or periods.",
8
+ "list.split": "A list has a row for each value of a field, or each period.",
9
+ "multiples.split": "Small multiples draw a chart for each value of a field."
10
+ };
11
+ /** What any kind asks for, said without its name. */
12
+ const ENGLISH_NEED = {
13
+ time: "This kind runs over time.",
14
+ whole: "A funnel has no time axis: it draws the whole period.",
15
+ split: "This kind compares the values of a field.",
16
+ cross: "A heatmap crosses two fields: one down, one across.",
17
+ steps: "A funnel draws steps: add another measure, or group by a field."
18
+ };
2
19
  const ENGLISH_OPS = {
3
20
  equals: "is",
4
21
  in: "is",
@@ -116,6 +133,21 @@ const ENGLISH_BUILDER = {
116
133
  optionAutomatic: "Automatic",
117
134
  optionNone: "None",
118
135
  nothingToSet: "Nothing to set for this kind",
119
- optionsCleared: (names) => `Cleared ${names.join(", ")}: ${names.length === 1 ? "it no longer fits" : "they no longer fit"} this tile.`
136
+ optionsCleared: (names) => `Cleared ${names.join(", ")}: ${names.length === 1 ? "it no longer fits" : "they no longer fit"} this tile.`,
137
+ kindSays: {
138
+ number: "One figure, large, with how it moved.",
139
+ line: "A value over time.",
140
+ bar: "The values of a field, or periods, side by side.",
141
+ table: "Rows and columns of figures.",
142
+ list: "A row for each value of a field, with its details.",
143
+ area: "A value over time, its parts stacked.",
144
+ multiples: "A small chart over time for each part.",
145
+ heatmap: "Two fields crossed, each cell shaded by its value.",
146
+ funnel: "Steps in order, each with what it keeps of the one before."
147
+ },
148
+ kindNeeds: (kind, need) => ENGLISH_NEEDS[`${kind}.${need}`] ?? ENGLISH_NEED[need],
149
+ moreFields: "More fields"
120
150
  };
121
- export { ENGLISH_BUILDER };
151
+ /** A kind's line: the host's, else the English one, so a host gives only the kinds it translates. */
152
+ const kindLine = (messages, kind) => messages.kindSays[kind] ?? ENGLISH_BUILDER.kindSays[kind];
153
+ export { ENGLISH_BUILDER, kindLine };
@@ -114,7 +114,10 @@ function drawnWith(measured, cell, at) {
114
114
  if (cell === null) return void 0;
115
115
  return at.total && measured?.dot === void 0 ? void 0 : measured;
116
116
  }
117
- /** A value cell's number: with its bar, or alone (in a card, as an element of its own, to sit opposite its name). */
117
+ /**
118
+ * A value cell's number: with its bar, or alone. An element of its own either way: the theme
119
+ * underlines it under the pointer where its cell opens, and a card sets it opposite its name.
120
+ */
118
121
  function figureOf(text, cell, at, how) {
119
122
  if (how?.bar === true) return /* @__PURE__ */ jsx(Bar, {
120
123
  text,
@@ -124,10 +127,10 @@ function figureOf(text, cell, at, how) {
124
127
  ...at
125
128
  }
126
129
  });
127
- return at.cards ? /* @__PURE__ */ jsx("span", {
130
+ return /* @__PURE__ */ jsx("span", {
128
131
  className: "dd-table-value",
129
132
  children: text
130
- }) : text;
133
+ });
131
134
  }
132
135
  const ARROW = {
133
136
  up: "↑",