derive-dashboards 0.1.1 → 0.2.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 (167) hide show
  1. package/README.md +5 -3
  2. package/dist/cli/add.mjs +130 -0
  3. package/dist/cli/cli.mjs +14 -12
  4. package/dist/cli/config.mjs +12 -1
  5. package/dist/cli/doctor.mjs +147 -0
  6. package/dist/cli/init.mjs +3 -2
  7. package/dist/cli/json.mjs +21 -0
  8. package/dist/cli/layers.mjs +33 -0
  9. package/dist/core/document/layout.mjs +1 -1
  10. package/dist/core/document/view-layout.d.mts +14 -0
  11. package/dist/core/document/view-layout.mjs +157 -0
  12. package/dist/core/engine/filter.mjs +2 -2
  13. package/dist/core/engine/leaf.mjs +9 -4
  14. package/dist/core/engine/members.mjs +1 -1
  15. package/dist/core/engine/version.d.mts +1 -1
  16. package/dist/core/engine/version.mjs +1 -1
  17. package/dist/core/error/codes.d.mts +2 -1
  18. package/dist/core/error/codes.mjs +2 -1
  19. package/dist/core/index.d.mts +5 -3
  20. package/dist/core/index.mjs +5 -3
  21. package/dist/core/model/member.mjs +5 -2
  22. package/dist/core/model/types.d.mts +13 -13
  23. package/dist/core/model/validate/members.mjs +7 -4
  24. package/dist/core/policy/bind-query.d.mts +12 -0
  25. package/dist/core/policy/bind-query.mjs +53 -0
  26. package/dist/core/query/filter.d.mts +6 -0
  27. package/dist/core/query/picks.d.mts +8 -2
  28. package/dist/core/query/picks.mjs +11 -4
  29. package/dist/core/query/segments.mjs +2 -1
  30. package/dist/core/result/columns.mjs +6 -3
  31. package/dist/core/schema/aliases.d.mts +7 -0
  32. package/dist/core/schema/aliases.mjs +77 -0
  33. package/dist/core/schema/answers.d.mts +1 -0
  34. package/dist/core/schema/answers.mjs +1 -0
  35. package/dist/core/schema/index.d.mts +3 -2
  36. package/dist/core/schema/index.mjs +3 -2
  37. package/dist/core/schema/query.d.mts +81 -0
  38. package/dist/core/schema/query.mjs +9 -5
  39. package/dist/core/types/api.d.mts +7 -1
  40. package/dist/core/types/api.mjs +1 -1
  41. package/dist/dashboards.mjs +4 -0
  42. package/dist/editing.mjs +2 -2
  43. package/dist/endpoints/dashboards.mjs +17 -2
  44. package/dist/endpoints/index.d.mts +12 -2
  45. package/dist/endpoints/meta.mjs +10 -1
  46. package/dist/execute.mjs +20 -11
  47. package/dist/handler.mjs +5 -1
  48. package/dist/llms.txt +22 -0
  49. package/dist/mcp/format.mjs +42 -8
  50. package/dist/mcp/query.mjs +34 -4
  51. package/dist/postgres-store.mjs +1 -0
  52. package/dist/react/Canvas.mjs +28 -5
  53. package/dist/react/Cell.mjs +2 -1
  54. package/dist/react/Dashboard.mjs +15 -3
  55. package/dist/react/DashboardList.mjs +22 -22
  56. package/dist/react/DashboardListRow.mjs +49 -0
  57. package/dist/react/Discard.mjs +5 -2
  58. package/dist/react/Editing.mjs +19 -5
  59. package/dist/react/FilterBar.d.mts +7 -9
  60. package/dist/react/Grid.mjs +7 -19
  61. package/dist/react/Header.mjs +1 -1
  62. package/dist/react/History.mjs +5 -0
  63. package/dist/react/ReadFailed.mjs +23 -0
  64. package/dist/react/Spaces.mjs +15 -1
  65. package/dist/react/Tile.d.mts +2 -0
  66. package/dist/react/Tile.mjs +35 -7
  67. package/dist/react/_virtual/_rolldown/runtime.mjs +12 -0
  68. package/dist/react/arrange-steps.mjs +16 -13
  69. package/dist/react/arrange.mjs +10 -3
  70. package/dist/react/builder/Calcs.mjs +35 -11
  71. package/dist/react/builder/FilterEditor.mjs +196 -0
  72. package/dist/react/builder/Filters.mjs +172 -116
  73. package/dist/react/builder/Formula.mjs +82 -0
  74. package/dist/react/builder/Groups.mjs +1 -0
  75. package/dist/react/builder/Preview.mjs +1 -1
  76. package/dist/react/builder/RangeEditor.mjs +184 -0
  77. package/dist/react/builder/Sections.mjs +41 -6
  78. package/dist/react/builder/Time.mjs +138 -60
  79. package/dist/react/builder/Values.mjs +85 -0
  80. package/dist/react/builder/WidgetBuilder.mjs +50 -49
  81. package/dist/react/builder/base.d.mts +5 -0
  82. package/dist/react/builder/base.mjs +33 -8
  83. package/dist/react/builder/calc.mjs +15 -1
  84. package/dist/react/builder/escape.d.mts +1 -0
  85. package/dist/react/builder/escape.mjs +62 -0
  86. package/dist/react/builder/filter-tree.d.mts +9 -0
  87. package/dist/react/builder/filter-tree.mjs +186 -0
  88. package/dist/react/builder/formula-parse.d.mts +18 -0
  89. package/dist/react/builder/formula-parse.mjs +136 -0
  90. package/dist/react/builder/labels.d.mts +1 -1
  91. package/dist/react/builder/state.d.mts +2 -1
  92. package/dist/react/builder/state.mjs +23 -37
  93. package/dist/react/builder/useBuilder.d.mts +12 -1
  94. package/dist/react/builder/useBuilder.mjs +6 -3
  95. package/dist/react/builder/words.d.mts +59 -0
  96. package/dist/react/builder/words.mjs +73 -0
  97. package/dist/react/context.d.mts +13 -0
  98. package/dist/react/context.mjs +9 -3
  99. package/dist/react/drill/useDrill.d.mts +1 -1
  100. package/dist/react/drill/words.d.mts +3 -0
  101. package/dist/react/drill/words.mjs +2 -0
  102. package/dist/react/english.mjs +20 -5
  103. package/dist/react/filters/bar.d.mts +12 -0
  104. package/dist/react/filters/bar.mjs +135 -0
  105. package/dist/react/filters/calendar.mjs +193 -0
  106. package/dist/react/filters/chip.mjs +174 -0
  107. package/dist/react/filters/context.d.mts +22 -0
  108. package/dist/react/filters/context.mjs +78 -0
  109. package/dist/react/filters/dates.mjs +89 -0
  110. package/dist/react/filters/days.d.mts +36 -0
  111. package/dist/react/filters/days.mjs +87 -0
  112. package/dist/react/filters/field-step.mjs +81 -0
  113. package/dist/react/filters/hooks.d.mts +34 -0
  114. package/dist/react/filters/hooks.mjs +94 -0
  115. package/dist/react/filters/listbox.mjs +131 -0
  116. package/dist/react/filters/logic.d.mts +59 -0
  117. package/dist/react/filters/logic.mjs +109 -0
  118. package/dist/react/filters/menu-search.mjs +211 -0
  119. package/dist/react/filters/menu.mjs +85 -0
  120. package/dist/react/filters/number.mjs +127 -0
  121. package/dist/react/filters/numbers.d.mts +47 -0
  122. package/dist/react/filters/numbers.mjs +151 -0
  123. package/dist/react/filters/operator.mjs +104 -0
  124. package/dist/react/filters/phone.mjs +104 -0
  125. package/dist/react/filters/popover.d.mts +19 -0
  126. package/dist/react/filters/popover.mjs +153 -0
  127. package/dist/react/filters/rules.d.mts +8 -0
  128. package/dist/react/filters/rules.mjs +55 -0
  129. package/dist/react/filters/sheet.mjs +196 -0
  130. package/dist/react/filters/state.mjs +132 -0
  131. package/dist/react/filters/suggest.d.mts +47 -0
  132. package/dist/react/filters/suggest.mjs +97 -0
  133. package/dist/react/filters/values.mjs +166 -0
  134. package/dist/react/filters/words.d.mts +81 -0
  135. package/dist/react/filters/words.mjs +73 -0
  136. package/dist/react/format.mjs +11 -4
  137. package/dist/react/index.d.mts +11 -5
  138. package/dist/react/index.mjs +8 -4
  139. package/dist/react/messages.d.mts +34 -2
  140. package/dist/react/place.mjs +7 -3
  141. package/dist/react/renderers/index.mjs +65 -3
  142. package/dist/react/renderers/list.mjs +6 -6
  143. package/dist/react/series.d.mts +25 -8
  144. package/dist/react/series.mjs +135 -39
  145. package/dist/react/theme.css +930 -19
  146. package/dist/react/useBoard.d.mts +2 -0
  147. package/dist/react/useDashboard.d.mts +3 -0
  148. package/dist/react/useDashboard.mjs +6 -0
  149. package/dist/react/view-grid.mjs +16 -0
  150. package/dist/recharts/axes.mjs +28 -0
  151. package/dist/recharts/axis.mjs +84 -0
  152. package/dist/recharts/bar.d.mts +7 -0
  153. package/dist/recharts/bar.mjs +140 -0
  154. package/dist/recharts/charts.d.mts +4 -5
  155. package/dist/recharts/charts.mjs +2 -208
  156. package/dist/recharts/index.d.mts +3 -1
  157. package/dist/recharts/index.mjs +4 -1
  158. package/dist/recharts/line.d.mts +2 -0
  159. package/dist/recharts/line.mjs +111 -0
  160. package/dist/recharts/marks.mjs +47 -18
  161. package/dist/recharts/parts.mjs +88 -0
  162. package/dist/recharts/table.mjs +71 -0
  163. package/dist/records-read.mjs +1 -1
  164. package/dist/registry/filter-bar.json +26 -0
  165. package/dist/skills/derive-dashboards/SKILL.md +69 -0
  166. package/package.json +6 -2
  167. package/dist/react/FilterBar.mjs +0 -119
@@ -0,0 +1,184 @@
1
+ import { useDashboards } from "../context.mjs";
2
+ import { DAY_ONLY, addDays, fromIsoDay, isoDay } from "../filters/days.mjs";
3
+ import { useState } from "react";
4
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
5
+ // src/builder/RangeEditor.tsx
6
+ const UNITS = [
7
+ "day",
8
+ "week",
9
+ "month",
10
+ "quarter",
11
+ "year"
12
+ ];
13
+ /** A rolling range as it stands, or a starting one: the last 6 months so far. */
14
+ const rollingOf = (r) => "from" in r ? {
15
+ last: 6,
16
+ unit: "month",
17
+ includeCurrent: true
18
+ } : r;
19
+ /** A fixed range's first and last day included, or the last 30 days when it has none. */
20
+ function daysOf(r) {
21
+ if ("from" in r && DAY_ONLY.test(r.from) && DAY_ONLY.test(r.to)) return {
22
+ from: r.from,
23
+ last: isoDay(addDays(fromIsoDay(r.to), -1))
24
+ };
25
+ const today = /* @__PURE__ */ new Date();
26
+ return {
27
+ from: isoDay(addDays(today, -29)),
28
+ last: isoDay(today)
29
+ };
30
+ }
31
+ function UnitSelect(props) {
32
+ const { messages } = useDashboards();
33
+ return /* @__PURE__ */ jsx("select", {
34
+ className: "dd-control",
35
+ "aria-label": messages.unit,
36
+ value: props.unit,
37
+ onChange: (e) => {
38
+ const unit = UNITS.find((u) => u === e.target.value);
39
+ if (unit !== void 0) props.onPick(unit);
40
+ },
41
+ children: UNITS.map((u) => /* @__PURE__ */ jsx("option", {
42
+ value: u,
43
+ children: messages.units[u]
44
+ }, u))
45
+ });
46
+ }
47
+ function Rolling(props) {
48
+ const { messages } = useDashboards();
49
+ const r = props.range;
50
+ const unit = r.unit === "hour" ? "day" : r.unit;
51
+ const set = (change) => {
52
+ props.onChange({
53
+ ...r,
54
+ unit,
55
+ ...change
56
+ });
57
+ };
58
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("div", {
59
+ className: "dd-custom-row",
60
+ children: [
61
+ messages.last,
62
+ /* @__PURE__ */ jsx("input", {
63
+ type: "number",
64
+ className: "dd-control",
65
+ min: 1,
66
+ max: 999,
67
+ "aria-label": messages.last,
68
+ value: r.last,
69
+ onChange: (e) => {
70
+ const n = Math.trunc(Number(e.target.value));
71
+ if (n >= 1 && n <= 999) set({ last: n });
72
+ }
73
+ }),
74
+ /* @__PURE__ */ jsx(UnitSelect, {
75
+ unit,
76
+ onPick: (u) => {
77
+ set({ unit: u });
78
+ }
79
+ })
80
+ ]
81
+ }), /* @__PURE__ */ jsxs("label", {
82
+ className: "dd-check",
83
+ children: [/* @__PURE__ */ jsx("input", {
84
+ type: "checkbox",
85
+ checked: r.includeCurrent,
86
+ onChange: (e) => {
87
+ set({ includeCurrent: e.target.checked });
88
+ }
89
+ }), messages.withCurrent(unit)]
90
+ })] });
91
+ }
92
+ function Between(props) {
93
+ const { messages } = useDashboards();
94
+ const [days, setDays] = useState(() => daysOf(props.range));
95
+ const wrong = days.from !== "" && days.last !== "" && days.last < days.from;
96
+ const set = (next) => {
97
+ setDays(next);
98
+ if (DAY_ONLY.test(next.from) && DAY_ONLY.test(next.last) && next.last >= next.from) props.onChange({
99
+ from: next.from,
100
+ to: isoDay(addDays(fromIsoDay(next.last), 1))
101
+ });
102
+ };
103
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("div", {
104
+ className: "dd-custom-dates",
105
+ children: [
106
+ /* @__PURE__ */ jsx("span", {
107
+ className: "dd-form-label",
108
+ children: messages.fromDay
109
+ }),
110
+ /* @__PURE__ */ jsx("input", {
111
+ type: "date",
112
+ className: "dd-control",
113
+ "aria-label": messages.betweenDates,
114
+ value: days.from,
115
+ onChange: (e) => {
116
+ set({
117
+ ...days,
118
+ from: e.target.value
119
+ });
120
+ }
121
+ }),
122
+ /* @__PURE__ */ jsx("span", {
123
+ className: "dd-form-label",
124
+ children: messages.toDay
125
+ }),
126
+ /* @__PURE__ */ jsx("input", {
127
+ type: "date",
128
+ className: "dd-control",
129
+ "aria-label": messages.rangeTo,
130
+ "aria-invalid": wrong,
131
+ value: days.last,
132
+ onChange: (e) => {
133
+ set({
134
+ ...days,
135
+ last: e.target.value
136
+ });
137
+ }
138
+ })
139
+ ]
140
+ }), wrong ? /* @__PURE__ */ jsx("p", {
141
+ className: "dd-builder-error",
142
+ children: messages.endBeforeStart
143
+ }) : /* @__PURE__ */ jsx("p", {
144
+ className: "dd-builder-note",
145
+ children: messages.bothDaysIncluded
146
+ })] });
147
+ }
148
+ /** A range of the widget's own, inline: one that moves with today, or fixed days. */
149
+ function RangeEditor(props) {
150
+ const { messages } = useDashboards();
151
+ const [mode, setMode] = useState("from" in props.range ? "between" : "rolling");
152
+ const pick = (next) => {
153
+ setMode(next);
154
+ if (next === "rolling") props.onChange(rollingOf(props.range));
155
+ else {
156
+ const d = daysOf(props.range);
157
+ props.onChange({
158
+ from: d.from,
159
+ to: isoDay(addDays(fromIsoDay(d.last), 1))
160
+ });
161
+ }
162
+ };
163
+ return /* @__PURE__ */ jsxs("div", {
164
+ className: "dd-custom",
165
+ children: [/* @__PURE__ */ jsx("span", {
166
+ className: "dd-seg",
167
+ children: ["rolling", "between"].map((m) => /* @__PURE__ */ jsx("button", {
168
+ type: "button",
169
+ "aria-pressed": mode === m,
170
+ onClick: () => {
171
+ pick(m);
172
+ },
173
+ children: m === "rolling" ? messages.rolling : messages.betweenDates
174
+ }, m))
175
+ }), mode === "rolling" ? /* @__PURE__ */ jsx(Rolling, {
176
+ range: rollingOf(props.range),
177
+ onChange: props.onChange
178
+ }) : /* @__PURE__ */ jsx(Between, {
179
+ range: props.range,
180
+ onChange: props.onChange
181
+ })]
182
+ });
183
+ }
184
+ export { RangeEditor };
@@ -1,20 +1,44 @@
1
1
  import { useDashboards } from "../context.mjs";
2
+ import { keptAside } from "./base.mjs";
2
3
  import { measureOptions } from "./options.mjs";
3
4
  import { Picker } from "./Picker.mjs";
4
- import { useState } from "react";
5
+ import { createContext, useContext, useState } from "react";
5
6
  import { jsx, jsxs } from "react/jsx-runtime";
6
7
  // src/builder/Sections.tsx
7
- /** A titled part of the builder's panel, with its add link. */
8
+ /** Which section of the panel is open: one at a time, the others folded to a line. */
9
+ const OpenSection = createContext(void 0);
10
+ /**
11
+ * A titled part of the builder's panel, with its add link. Inside an `OpenSection`, only the open
12
+ * one shows its parts; the rest are one line each, saying what they hold.
13
+ */
8
14
  function Section(props) {
9
15
  const { messages } = useDashboards();
16
+ const fold = useContext(OpenSection);
17
+ const open = fold === void 0 || fold.open === props.title;
18
+ const { onAdd } = props;
10
19
  return /* @__PURE__ */ jsxs("section", {
11
20
  className: "dd-builder-section",
12
- children: [/* @__PURE__ */ jsxs("header", { children: [/* @__PURE__ */ jsx("h4", { children: props.title }), props.onAdd === void 0 ? null : /* @__PURE__ */ jsx("button", {
21
+ "data-open": open,
22
+ children: [/* @__PURE__ */ jsxs("header", { children: [fold === void 0 ? /* @__PURE__ */ jsx("h4", { children: props.title }) : /* @__PURE__ */ jsxs("button", {
23
+ type: "button",
24
+ className: "dd-section-toggle",
25
+ "aria-expanded": open,
26
+ onClick: () => {
27
+ fold.setOpen(open ? void 0 : props.title);
28
+ },
29
+ children: [/* @__PURE__ */ jsx("h4", { children: props.title }), open || props.summary === void 0 ? null : /* @__PURE__ */ jsx("span", {
30
+ className: "dd-section-summary",
31
+ children: props.summary
32
+ })]
33
+ }), onAdd === void 0 ? null : /* @__PURE__ */ jsx("button", {
13
34
  type: "button",
14
35
  className: "dd-link",
15
- onClick: props.onAdd,
36
+ onClick: () => {
37
+ fold?.setOpen(props.title);
38
+ onAdd();
39
+ },
16
40
  children: messages.add
17
- })] }), props.children]
41
+ })] }), open ? props.children : null]
18
42
  });
19
43
  }
20
44
  /** One entry: a grip when it can move, its label and a quiet detail, and remove. */
@@ -63,6 +87,7 @@ function MeasureSection({ b }) {
63
87
  } };
64
88
  return /* @__PURE__ */ jsxs(Section, {
65
89
  title: messages.measure,
90
+ summary: (query?.measures ?? []).map((m) => b.labelOf(m.measure)).join(", "),
66
91
  ...add,
67
92
  children: [(query?.measures ?? []).map((m) => /* @__PURE__ */ jsx(Entry, {
68
93
  label: b.labelOf(m.measure),
@@ -82,4 +107,14 @@ function MeasureSection({ b }) {
82
107
  }) : null]
83
108
  });
84
109
  }
85
- export { Chip, Entry, MeasureSection, Section };
110
+ /** One plain line: what this widget has that the builder keeps but does not edit. */
111
+ function KeptNote({ b }) {
112
+ const { messages } = useDashboards();
113
+ const kept = keptAside(b.state);
114
+ if (!kept.display && kept.queries.length === 0 && kept.fields.length === 0) return null;
115
+ return /* @__PURE__ */ jsx("p", {
116
+ className: "dd-builder-note",
117
+ children: messages.keptAside(kept)
118
+ });
119
+ }
120
+ export { Chip, Entry, KeptNote, MeasureSection, OpenSection, Section };
@@ -1,36 +1,54 @@
1
1
  import { useDashboards } from "../context.mjs";
2
+ import { rangeLabel } from "../filters/days.mjs";
2
3
  import { fieldOptions } from "./options.mjs";
3
4
  import { Picker } from "./Picker.mjs";
4
- import { Chip, Entry, Section } from "./Sections.mjs";
5
+ import { RangeEditor } from "./RangeEditor.mjs";
6
+ import { Entry, Section } from "./Sections.mjs";
5
7
  import { useState } from "react";
6
8
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
7
9
  // src/builder/Time.tsx
10
+ /** The quick ranges, each with the grain it reads best at. */
8
11
  const RANGES = [
9
12
  {
10
- last: 7,
11
- unit: "day",
12
- includeCurrent: true
13
+ range: {
14
+ last: 7,
15
+ unit: "day",
16
+ includeCurrent: true
17
+ },
18
+ grain: "day"
13
19
  },
14
20
  {
15
- last: 30,
16
- unit: "day",
17
- includeCurrent: true
21
+ range: {
22
+ last: 30,
23
+ unit: "day",
24
+ includeCurrent: true
25
+ },
26
+ grain: "day"
18
27
  },
19
28
  {
20
- last: 90,
21
- unit: "day",
22
- includeCurrent: true
29
+ range: {
30
+ last: 90,
31
+ unit: "day",
32
+ includeCurrent: true
33
+ },
34
+ grain: "week"
23
35
  },
24
36
  {
25
- last: 1,
26
- unit: "year",
27
- includeCurrent: true
37
+ range: {
38
+ last: 12,
39
+ unit: "month",
40
+ includeCurrent: true
41
+ },
42
+ grain: "month"
28
43
  }
29
44
  ];
30
45
  const GRAINS = [
46
+ "hour",
31
47
  "day",
32
48
  "week",
33
- "month"
49
+ "month",
50
+ "quarter",
51
+ "year"
34
52
  ];
35
53
  const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
36
54
  /** The time field, changed by searching the source's other time fields. */
@@ -42,66 +60,126 @@ function TimeField({ b, time }) {
42
60
  setPicking(false);
43
61
  };
44
62
  const others = fieldOptions(b.meta, source, true).filter((o) => o.id !== time.dimension);
45
- return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("button", {
46
- type: "button",
47
- className: "dd-entry-label",
48
- disabled: others.length === 0,
49
- onClick: () => {
50
- setPicking(true);
51
- },
52
- children: /* @__PURE__ */ jsx("b", { children: b.labelOf(time.dimension) })
53
- }), picking ? /* @__PURE__ */ jsx(Picker, {
54
- options: others,
55
- placeholder: messages.searchFields,
56
- autoFocus: true,
57
- onPick: (dimension) => {
58
- b.setTime({ dimension });
59
- close();
60
- },
61
- onClose: close
62
- }) : null] });
63
+ if (others.length === 0) return null;
64
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("span", {
65
+ className: "dd-form-label",
66
+ children: messages.timeField
67
+ }), /* @__PURE__ */ jsxs("div", {
68
+ className: "dd-form-field",
69
+ children: [/* @__PURE__ */ jsx("button", {
70
+ type: "button",
71
+ className: "dd-control",
72
+ "aria-label": messages.timeField,
73
+ onClick: () => {
74
+ setPicking(true);
75
+ },
76
+ children: b.labelOf(time.dimension)
77
+ }), picking ? /* @__PURE__ */ jsx(Picker, {
78
+ options: others,
79
+ placeholder: messages.searchFields,
80
+ autoFocus: true,
81
+ onPick: (dimension) => {
82
+ b.setTime({ dimension });
83
+ close();
84
+ },
85
+ onClose: close
86
+ }) : null]
87
+ })] });
63
88
  }
64
- /** A row of chips where one is on. */
65
- function ChipRow(props) {
66
- return /* @__PURE__ */ jsx("div", {
67
- className: "dd-chips",
68
- children: props.items.map((t) => /* @__PURE__ */ jsx(Chip, {
69
- on: props.on(t),
70
- label: props.label(t),
89
+ /**
90
+ * The quick ranges and Custom, which edits a range of the widget's own below. When time is
91
+ * broken down, a quick range also sets the grain it reads best at; a grain picked after it stays.
92
+ */
93
+ function QuickRanges(props) {
94
+ const { messages } = useDashboards();
95
+ const { b, time } = props;
96
+ const [open, setOpen] = useState(false);
97
+ const preset = RANGES.findIndex((r) => same(time.range, r.range));
98
+ const custom = open || preset === -1;
99
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("fieldset", {
100
+ className: "dd-seg dd-seg-fill",
101
+ "aria-label": messages.timeRange,
102
+ children: [RANGES.map((r, i) => /* @__PURE__ */ jsx("button", {
103
+ type: "button",
104
+ title: messages.range(r.range),
105
+ "aria-label": messages.range(r.range),
106
+ "aria-pressed": !custom && preset === i,
71
107
  onClick: () => {
72
- props.pick(t);
73
- }
74
- }, props.label(t)))
108
+ setOpen(false);
109
+ const grain = time.grain === void 0 ? {} : { grain: r.grain };
110
+ b.setTime({
111
+ range: r.range,
112
+ ...grain
113
+ });
114
+ },
115
+ children: messages.shortRange(r.range)
116
+ }, messages.range(r.range))), /* @__PURE__ */ jsx("button", {
117
+ type: "button",
118
+ "aria-pressed": custom,
119
+ onClick: () => {
120
+ setOpen(true);
121
+ },
122
+ children: messages.customRange
123
+ })]
124
+ }), custom ? /* @__PURE__ */ jsx(RangeEditor, {
125
+ range: time.range,
126
+ onChange: (next) => {
127
+ b.setTime({ range: next });
128
+ }
129
+ }) : null] });
130
+ }
131
+ /** How time splits: not at all, or by a grain. */
132
+ function GrainSelect({ b, time }) {
133
+ const { messages, locale } = useDashboards();
134
+ const title = (w) => w.charAt(0).toLocaleUpperCase(locale) + w.slice(1);
135
+ return /* @__PURE__ */ jsx("select", {
136
+ className: "dd-control",
137
+ "aria-label": messages.breakdown,
138
+ value: time.grain ?? "total",
139
+ onChange: (e) => {
140
+ const grain = ["total", ...GRAINS].find((g) => g === e.target.value);
141
+ if (grain !== void 0) b.setTime({ grain });
142
+ },
143
+ children: ["total", ...GRAINS].map((g) => /* @__PURE__ */ jsx("option", {
144
+ value: g,
145
+ children: title(g === "total" ? messages.allTime : messages.grains[g])
146
+ }, g))
75
147
  });
76
148
  }
77
149
  /** Over time: the time field, no breakdown or a grain, the range, and any comparison. */
78
150
  function TimeSection({ b }) {
79
- const { messages } = useDashboards();
151
+ const { messages, locale } = useDashboards();
80
152
  const time = b.state.query?.time;
81
153
  if (time === void 0) return null;
82
- const grains = ["total", ...GRAINS];
154
+ const summary = [
155
+ b.labelOf(time.dimension),
156
+ time.grain === void 0 ? messages.allTime : messages.grains[time.grain],
157
+ rangeLabel(time.range, locale)
158
+ ].join(" · ");
83
159
  return /* @__PURE__ */ jsxs(Section, {
84
160
  title: messages.overTime,
161
+ summary,
85
162
  children: [
86
- /* @__PURE__ */ jsx(TimeField, {
163
+ /* @__PURE__ */ jsx(QuickRanges, {
87
164
  b,
88
165
  time
89
166
  }),
90
- /* @__PURE__ */ jsx(ChipRow, {
91
- items: grains,
92
- label: (g) => g === "total" ? messages.allTime : messages.grains[g],
93
- on: (g) => (time.grain ?? "total") === g,
94
- pick: (grain) => {
95
- b.setTime({ grain });
96
- }
97
- }),
98
- /* @__PURE__ */ jsx(ChipRow, {
99
- items: RANGES,
100
- label: (r) => messages.range(r),
101
- on: (r) => same(time.range, r),
102
- pick: (range) => {
103
- b.setTime({ range });
104
- }
167
+ /* @__PURE__ */ jsxs("div", {
168
+ className: "dd-form",
169
+ children: [
170
+ /* @__PURE__ */ jsx("span", {
171
+ className: "dd-form-label",
172
+ children: messages.breakdown
173
+ }),
174
+ /* @__PURE__ */ jsx(GrainSelect, {
175
+ b,
176
+ time
177
+ }),
178
+ /* @__PURE__ */ jsx(TimeField, {
179
+ b,
180
+ time
181
+ })
182
+ ]
105
183
  }),
106
184
  b.state.removed === void 0 ? null : /* @__PURE__ */ jsx("p", {
107
185
  className: "dd-muted",
@@ -0,0 +1,85 @@
1
+ import { useDashboards } from "../context.mjs";
2
+ import { useDebounced } from "../debounce.mjs";
3
+ import { Chip } from "./Sections.mjs";
4
+ import { useState } from "react";
5
+ import useSWR from "swr";
6
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
7
+ // src/builder/Values.tsx
8
+ /** A field's values as chips, the chosen ones pressed. */
9
+ function ValueChips(props) {
10
+ const { messages } = useDashboards();
11
+ return /* @__PURE__ */ jsx("div", {
12
+ className: "dd-chips",
13
+ children: props.values.map((v) => /* @__PURE__ */ jsx(Chip, {
14
+ on: props.chosen.includes(v),
15
+ label: v === null ? messages.empty : String(v),
16
+ onClick: () => {
17
+ props.onToggle(v);
18
+ }
19
+ }, String(v)))
20
+ });
21
+ }
22
+ /** What the value list is doing when it has nothing to show: loading, failed (with Retry), or empty. */
23
+ function ValuesState(props) {
24
+ const { messages } = useDashboards();
25
+ if (props.failed) return /* @__PURE__ */ jsxs("p", {
26
+ className: "dd-builder-error",
27
+ role: "alert",
28
+ children: [
29
+ messages.readFailed,
30
+ " ",
31
+ /* @__PURE__ */ jsx("button", {
32
+ type: "button",
33
+ className: "dd-button dd-quiet",
34
+ onClick: props.retry,
35
+ children: messages.drillRetry
36
+ })
37
+ ]
38
+ });
39
+ if (props.loading) return /* @__PURE__ */ jsx("p", {
40
+ className: "dd-builder-note",
41
+ children: messages.loading
42
+ });
43
+ return props.empty ? /* @__PURE__ */ jsx("p", {
44
+ className: "dd-builder-note",
45
+ children: messages.noValues
46
+ }) : null;
47
+ }
48
+ /** A field's values to pick from: the ones this viewer can see, narrowed as they type. */
49
+ function ValuePicker(props) {
50
+ const { client, messages } = useDashboards();
51
+ const [search, setSearch] = useState("");
52
+ const sent = useDebounced(search, 250);
53
+ const values = useSWR([
54
+ "dd-member-values",
55
+ props.member,
56
+ sent
57
+ ], () => client.memberValues({
58
+ member: props.member,
59
+ limit: 50,
60
+ ...sent === "" ? {} : { search: sent }
61
+ }));
62
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
63
+ /* @__PURE__ */ jsx("input", {
64
+ className: "dd-search",
65
+ type: "search",
66
+ placeholder: messages.searchValues,
67
+ value: search,
68
+ onChange: (event) => {
69
+ setSearch(event.target.value);
70
+ }
71
+ }),
72
+ /* @__PURE__ */ jsx(ValueChips, {
73
+ values: values.data?.values ?? [],
74
+ chosen: props.chosen,
75
+ onToggle: props.onToggle
76
+ }),
77
+ /* @__PURE__ */ jsx(ValuesState, {
78
+ loading: values.isLoading,
79
+ failed: values.error !== void 0,
80
+ empty: values.data?.values.length === 0,
81
+ retry: () => void values.mutate()
82
+ })
83
+ ] });
84
+ }
85
+ export { ValuePicker };