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,211 @@
1
+ import { Icon } from "./chip.mjs";
2
+ import { useBar, useFormat, useRangeText, useSummary, useWords } from "./context.mjs";
3
+ import { useControlValues, useDebounced } from "./hooks.mjs";
4
+ import { Combobox } from "./listbox.mjs";
5
+ import { isNumber } from "./logic.mjs";
6
+ import { matchingFields, suggest, suggestionPick } from "./suggest.mjs";
7
+ import { Mark } from "./values.mjs";
8
+ import { useCallback, useEffect, useState } from "react";
9
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
10
+ // src/filters/menu-search.tsx
11
+ /** Reads one control's values into the menu's suggestions. One per control, so hooks stay fixed. */
12
+ function Collect({ control, query, onValues }) {
13
+ const { grammar } = useWords();
14
+ const { values, failed, retry } = useControlValues(control, { search: searchTerm(control, query, grammar) });
15
+ useEffect(() => {
16
+ onValues(control.id, values, failed ? retry : null);
17
+ }, [
18
+ control.id,
19
+ values,
20
+ failed,
21
+ retry,
22
+ onValues
23
+ ]);
24
+ return null;
25
+ }
26
+ /** Every text filter's values, read while the menu is open, for its typed suggestions. */
27
+ function Collectors({ controls, query, onValues }) {
28
+ const { fields } = useBar();
29
+ return controls.filter((c) => c.kind === "filter" && !isNumber(c, fields)).map((c) => /* @__PURE__ */ jsx(Collect, {
30
+ control: c,
31
+ query,
32
+ onValues
33
+ }, c.id));
34
+ }
35
+ /**
36
+ * What to ask the server for a lookup while someone types: the longest typed word that is not
37
+ * the field's name or grammar ("assignee not ada" asks Assignee for "ada"). Own lists need none.
38
+ */
39
+ function searchTerm(control, query, grammar) {
40
+ if (typeof control.values === "object") return "";
41
+ const label = control.label.toLowerCase().split(/\s+/u);
42
+ return query.trim().toLowerCase().split(/\s+/u).filter((w) => w !== "" && !grammar.not.includes(w) && !grammar.skip.includes(w)).filter((w) => !label.some((p) => p.startsWith(w))).reduce((a, w) => w.length > a.length ? w : a, "");
43
+ }
44
+ function useKnown() {
45
+ const [known, setKnown] = useState({});
46
+ const [failed, setFailed] = useState({});
47
+ const onValues = useCallback((id, values, retry) => {
48
+ setKnown((k) => JSON.stringify(k[id]) === JSON.stringify(values) ? k : {
49
+ ...k,
50
+ [id]: values
51
+ });
52
+ setFailed((f) => {
53
+ if (f[id] === void 0 === (retry === null)) return f;
54
+ const { [id]: _, ...rest } = f;
55
+ return retry === null ? rest : {
56
+ ...rest,
57
+ [id]: retry
58
+ };
59
+ });
60
+ }, []);
61
+ return {
62
+ known,
63
+ failed: Object.values(failed),
64
+ onValues
65
+ };
66
+ }
67
+ /** Some fields' values could not be read, so suggestions may be missing: say so, with Retry. */
68
+ function MenuFailed({ retries }) {
69
+ const words = useWords();
70
+ if (retries.length === 0) return null;
71
+ return /* @__PURE__ */ jsxs("div", {
72
+ className: "dd-fb-foot dd-fb-error",
73
+ role: "alert",
74
+ children: [/* @__PURE__ */ jsx("span", { children: words.valuesFailed }), /* @__PURE__ */ jsx("button", {
75
+ type: "button",
76
+ className: "dd-fb-link",
77
+ onClick: () => {
78
+ for (const retry of retries) retry();
79
+ },
80
+ children: words.retry
81
+ })]
82
+ });
83
+ }
84
+ function SuggestionRow({ s }) {
85
+ const words = useWords();
86
+ const format = useFormat();
87
+ const rangeText = useRangeText();
88
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
89
+ /* @__PURE__ */ jsx(Icon, { control: s.control }),
90
+ /* @__PURE__ */ jsx("span", {
91
+ className: "dd-fb-quiet",
92
+ children: s.control.label
93
+ }),
94
+ s.kind === "date" ? /* @__PURE__ */ jsx("span", {
95
+ className: "dd-fb-option-text",
96
+ children: rangeText(s.range)
97
+ }) : /* @__PURE__ */ jsxs(Fragment, { children: [
98
+ /* @__PURE__ */ jsx("span", {
99
+ className: "dd-fb-quiet",
100
+ children: s.negative ? words.isNotWord : words.isWord
101
+ }),
102
+ /* @__PURE__ */ jsx(Mark, {
103
+ control: s.control,
104
+ value: s.value
105
+ }),
106
+ /* @__PURE__ */ jsx("span", {
107
+ className: "dd-fb-option-text",
108
+ children: format(s.control, s.value)
109
+ })
110
+ ] })
111
+ ] });
112
+ }
113
+ function FieldRow({ control }) {
114
+ const said = useSummary()(control);
115
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
116
+ /* @__PURE__ */ jsx(Icon, { control }),
117
+ /* @__PURE__ */ jsx("span", {
118
+ className: "dd-fb-option-text",
119
+ children: control.label
120
+ }),
121
+ said === void 0 ? null : /* @__PURE__ */ jsx("span", {
122
+ className: "dd-fb-quiet dd-fb-end",
123
+ children: said
124
+ })
125
+ ] });
126
+ }
127
+ /** The menu's first step: typed suggestions, then the fields that match. */
128
+ function useMenuOptions(controls, query, known, steps) {
129
+ const words = useWords();
130
+ const format = useFormat();
131
+ const rangeText = useRangeText();
132
+ const { fields, picks, setPick } = useBar();
133
+ const filters = controls.filter((c) => c.kind === "filter");
134
+ const date = controls.find((c) => c.kind === "date_range");
135
+ const textFilters = filters.filter((c) => !isNumber(c, fields));
136
+ const grammar = words.grammar;
137
+ return [...suggest({
138
+ filters: textFilters,
139
+ date,
140
+ known,
141
+ query,
142
+ format,
143
+ dateText: rangeText,
144
+ grammar
145
+ }).map((s) => ({
146
+ id: s.kind === "date" ? `date:${JSON.stringify(s.range)}` : `${s.control.id}:${String(s.value)}`,
147
+ group: words.suggestions,
148
+ onPick: () => {
149
+ setPick(s.control.id, suggestionPick(s, picks));
150
+ steps.close();
151
+ },
152
+ content: /* @__PURE__ */ jsx(SuggestionRow, { s })
153
+ })), ...matchingFields(controls, query, grammar.dates).map((c) => ({
154
+ id: `field:${c.id}`,
155
+ group: words.fields,
156
+ onPick: () => {
157
+ steps.go(c);
158
+ },
159
+ content: /* @__PURE__ */ jsx(FieldRow, { control: c })
160
+ }))];
161
+ }
162
+ function Keys() {
163
+ const words = useWords();
164
+ return /* @__PURE__ */ jsxs("div", {
165
+ className: "dd-fb-keys",
166
+ "aria-hidden": true,
167
+ children: [
168
+ /* @__PURE__ */ jsxs("span", { children: [
169
+ /* @__PURE__ */ jsx("kbd", { children: "↵" }),
170
+ " ",
171
+ words.keys.pick
172
+ ] }),
173
+ /* @__PURE__ */ jsxs("span", { children: [
174
+ /* @__PURE__ */ jsx("kbd", { children: "⇧" }),
175
+ /* @__PURE__ */ jsx("kbd", { children: "F" }),
176
+ " ",
177
+ words.keys.removeLast
178
+ ] }),
179
+ /* @__PURE__ */ jsxs("span", { children: [
180
+ /* @__PURE__ */ jsx("kbd", { children: "esc" }),
181
+ " ",
182
+ words.keys.close
183
+ ] })
184
+ ]
185
+ });
186
+ }
187
+ function MenuSearch({ controls, steps }) {
188
+ const words = useWords();
189
+ const [query, setQuery] = useState("");
190
+ const { known, failed, onValues } = useKnown();
191
+ const searched = useDebounced(query, 150);
192
+ const options = useMenuOptions(controls, query, known, steps);
193
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
194
+ /* @__PURE__ */ jsx(Collectors, {
195
+ controls,
196
+ query: searched,
197
+ onValues
198
+ }),
199
+ /* @__PURE__ */ jsx(Combobox, {
200
+ label: words.filter,
201
+ placeholder: words.menuPlaceholder,
202
+ query,
203
+ onQuery: setQuery,
204
+ options,
205
+ empty: words.menuNoMatch
206
+ }),
207
+ /* @__PURE__ */ jsx(MenuFailed, { retries: failed }),
208
+ /* @__PURE__ */ jsx(Keys, {})
209
+ ] });
210
+ }
211
+ export { MenuSearch };
@@ -0,0 +1,85 @@
1
+ import { useWords } from "./context.mjs";
2
+ import { FieldStep } from "./field-step.mjs";
3
+ import { MenuSearch } from "./menu-search.mjs";
4
+ import { Popover } from "./popover.mjs";
5
+ import { useRef, useState } from "react";
6
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
7
+ // src/filters/menu.tsx
8
+ const focusStep = (node) => {
9
+ node?.querySelector("input, button:not(.dd-fb-back)")?.focus({ preventScroll: true });
10
+ };
11
+ function MenuButton({ anchor, open, hidden, onClick }) {
12
+ const words = useWords();
13
+ const more = hidden > 0 ? words.more(hidden) : void 0;
14
+ return /* @__PURE__ */ jsxs("button", {
15
+ ref: anchor,
16
+ type: "button",
17
+ className: "dd-fb-add",
18
+ "aria-haspopup": "dialog",
19
+ "aria-expanded": open,
20
+ "aria-keyshortcuts": "F",
21
+ "aria-label": more === void 0 ? words.filter : `${words.filter} (${more})`,
22
+ onClick,
23
+ children: [
24
+ /* @__PURE__ */ jsx("span", {
25
+ className: "dd-fb-add-icon",
26
+ "aria-hidden": true,
27
+ children: "+"
28
+ }),
29
+ words.filter,
30
+ more === void 0 ? null : /* @__PURE__ */ jsx("span", {
31
+ className: "dd-fb-count",
32
+ children: more
33
+ }),
34
+ /* @__PURE__ */ jsx("kbd", {
35
+ "aria-hidden": true,
36
+ children: "F"
37
+ })
38
+ ]
39
+ });
40
+ }
41
+ /**
42
+ * The menu behind Filter and the F key: type a field, a value or both ("stat rev", "last 90"),
43
+ * or pick a field to open its picker here. It holds every filter, set or not, so the bar can
44
+ * show only some.
45
+ */
46
+ function FilterMenu({ controls, open, setOpen, openChip, hidden }) {
47
+ const words = useWords();
48
+ const anchor = useRef(null);
49
+ const [field, setField] = useState(null);
50
+ const close = () => {
51
+ setOpen(false);
52
+ setField(null);
53
+ };
54
+ const steps = {
55
+ go: setField,
56
+ close,
57
+ openChip
58
+ };
59
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(MenuButton, {
60
+ anchor,
61
+ open,
62
+ hidden,
63
+ onClick: () => {
64
+ if (open) close();
65
+ else setOpen(true);
66
+ }
67
+ }), /* @__PURE__ */ jsx(Popover, {
68
+ anchor,
69
+ open,
70
+ onClose: close,
71
+ label: words.filter,
72
+ className: "dd-fb-menu-pop",
73
+ children: /* @__PURE__ */ jsx("div", {
74
+ ref: focusStep,
75
+ children: field === null ? /* @__PURE__ */ jsx(MenuSearch, {
76
+ controls,
77
+ steps
78
+ }) : /* @__PURE__ */ jsx(FieldStep, {
79
+ field,
80
+ steps
81
+ })
82
+ }, field?.id ?? "")
83
+ })] });
84
+ }
85
+ export { FilterMenu };
@@ -0,0 +1,127 @@
1
+ import { useBar, useWords } from "./context.mjs";
2
+ import { numberBounds, numberDraft, numberPresets, numberSides, unitText } from "./numbers.mjs";
3
+ import { useId, useState } from "react";
4
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
5
+ // src/filters/number.tsx
6
+ function QuickPicks({ edges, unit, ops, pick, onPick }) {
7
+ const words = useWords();
8
+ const quick = numberPresets(edges, unit, ops);
9
+ if (quick.length === 0) return null;
10
+ const u = unitText(unit);
11
+ const say = (q) => q.kind === "under" ? words.under(q.lo, u) : q.kind === "between" ? words.between(q.lo, q.hi, u) : words.orMore(q.hi, u);
12
+ return /* @__PURE__ */ jsx("div", {
13
+ className: "dd-fb-presets",
14
+ children: quick.map((q) => /* @__PURE__ */ jsx("button", {
15
+ type: "button",
16
+ className: "dd-fb-preset",
17
+ "aria-pressed": JSON.stringify(q.pick) === JSON.stringify(pick),
18
+ onClick: () => {
19
+ onPick(q.pick);
20
+ },
21
+ children: say(q)
22
+ }, q.kind))
23
+ });
24
+ }
25
+ function Bound({ label, value, onChange, onEnter, problemId }) {
26
+ return /* @__PURE__ */ jsx("input", {
27
+ className: "dd-fb-input",
28
+ inputMode: "decimal",
29
+ "aria-label": label,
30
+ "aria-invalid": problemId !== void 0 || void 0,
31
+ "aria-describedby": problemId,
32
+ placeholder: label,
33
+ value,
34
+ onChange: (e) => {
35
+ onChange(e.target.value);
36
+ },
37
+ onKeyDown: (e) => {
38
+ if (e.key === "Enter") onEnter();
39
+ }
40
+ });
41
+ }
42
+ /** The min and max boxes the control's operators allow, and the unit after them. */
43
+ function Bounds({ name, unit, ops, current, bounds, setBounds, apply, problemId }) {
44
+ const words = useWords();
45
+ const sides = numberSides(ops, current);
46
+ const box = (side) => /* @__PURE__ */ jsx(Bound, {
47
+ label: `${sides.exact ? words.value : words[side]} ${name}`,
48
+ value: bounds[side],
49
+ onChange: (v) => {
50
+ setBounds({
51
+ ...bounds,
52
+ [side]: v
53
+ });
54
+ },
55
+ onEnter: apply,
56
+ problemId
57
+ });
58
+ return /* @__PURE__ */ jsxs("div", {
59
+ className: "dd-fb-range",
60
+ children: [
61
+ sides.min || sides.exact ? box("min") : null,
62
+ sides.min && sides.max ? /* @__PURE__ */ jsx("span", { children: words.to }) : null,
63
+ sides.max ? box("max") : null,
64
+ unit === "" ? null : /* @__PURE__ */ jsx("span", { children: unit })
65
+ ]
66
+ });
67
+ }
68
+ /** What is wrong with the typed bounds, if anything, and Apply. */
69
+ function NumberFoot({ problem, errorId, apply }) {
70
+ const words = useWords();
71
+ return /* @__PURE__ */ jsxs(Fragment, { children: [problem === void 0 ? null : /* @__PURE__ */ jsx("p", {
72
+ id: errorId,
73
+ role: "alert",
74
+ className: "dd-fb-error",
75
+ children: words[problem]
76
+ }), /* @__PURE__ */ jsx("button", {
77
+ type: "button",
78
+ className: "dd-fb-primary",
79
+ disabled: problem !== void 0,
80
+ onClick: apply,
81
+ children: words.apply
82
+ })] });
83
+ }
84
+ /**
85
+ * A number range: quick picks from the model's buckets, or your own bounds, checked as you type.
86
+ * Only finite numbers apply, and only with operators the control allows; an edited bound keeps
87
+ * the comparison it had where the control allows it (a "> 2" changed to 3 stays strict).
88
+ */
89
+ function NumberPicker({ control, pick, onPick }) {
90
+ const field = useBar().fields[control.id];
91
+ const unit = field?.unit ?? "";
92
+ const [bounds, setBounds] = useState(() => numberBounds(pick));
93
+ const draft = numberDraft(bounds.min, bounds.max, control.ops, pick?.op);
94
+ const errorId = useId();
95
+ const apply = () => {
96
+ if (draft.problem !== void 0) return;
97
+ onPick(draft.pick);
98
+ };
99
+ return /* @__PURE__ */ jsxs("div", {
100
+ className: "dd-fb-number",
101
+ children: [
102
+ /* @__PURE__ */ jsx(QuickPicks, {
103
+ edges: field?.buckets ?? [],
104
+ unit,
105
+ ops: control.ops,
106
+ pick,
107
+ onPick
108
+ }),
109
+ /* @__PURE__ */ jsx(Bounds, {
110
+ name: control.label.toLowerCase(),
111
+ unit,
112
+ ops: control.ops,
113
+ current: pick?.op,
114
+ bounds,
115
+ setBounds,
116
+ apply,
117
+ problemId: draft.problem === void 0 ? void 0 : errorId
118
+ }),
119
+ /* @__PURE__ */ jsx(NumberFoot, {
120
+ problem: draft.problem,
121
+ errorId,
122
+ apply
123
+ })
124
+ ]
125
+ });
126
+ }
127
+ export { NumberPicker };
@@ -0,0 +1,47 @@
1
+ import { ValuePick } from "./logic.mjs";
2
+ import { Op } from "derive-dashboards/core";
3
+ export declare const unitText: (unit: string) => string;
4
+ /** A number pick as a chip says it: "2–8 h", "≥ 2 h", "> 2 h", "< 8 h", "≤ 8 h", "= 4 h". */
5
+ export declare function numberText(pick: ValuePick, unit: string): string;
6
+ /** A quick pick from the model's buckets: under the first edge, between them, from the last. */
7
+ export type NumberPreset = {
8
+ kind: "under" | "between" | "over";
9
+ lo: number;
10
+ hi: number;
11
+ pick: ValuePick;
12
+ /** In English; a bar shows its own words. */
13
+ label: string;
14
+ };
15
+ /** The quick picks a number field's buckets give, for the operators the control allows. */
16
+ export declare function numberPresets(edges: readonly number[], unit: string, ops?: readonly Op[]): NumberPreset[];
17
+ /** Why typed bounds are not a pick; each is a word in `FilterWords`. */
18
+ export type NumberProblem = "notNumbers" | "minOverMax" | "oneBound" | "bothBounds";
19
+ /**
20
+ * Which boxes a control's operators give: a minimum, a maximum, or one exact value. An exact
21
+ * value is the only box for an equals-only control, and the box for a pick that is already
22
+ * "= 4" (`current`), so editing it keeps it exact.
23
+ */
24
+ export declare function numberSides(ops: readonly Op[], current?: Op): {
25
+ min: boolean;
26
+ max: boolean;
27
+ exact: true;
28
+ } | {
29
+ min: boolean;
30
+ max: boolean;
31
+ exact: false;
32
+ };
33
+ /**
34
+ * Typed bounds as a pick (none when blank), or why they are not one. Only finite numbers count;
35
+ * the operator is always one the control lists: between for both, a one-sided comparison for
36
+ * one (the one it had, if any), equals for an exact value. `prefer` is the pick's operator before
37
+ * the edit; a pick read into the boxes and applied unchanged comes back as itself.
38
+ */
39
+ export declare function numberDraft(min: string, max: string, ops?: readonly Op[], prefer?: Op): {
40
+ pick?: ValuePick;
41
+ problem?: NumberProblem;
42
+ };
43
+ /** A number pick as the boxes show it: an exact value and a minimum both fill the first box. */
44
+ export declare function numberBounds(pick?: ValuePick): {
45
+ min: string;
46
+ max: string;
47
+ };
@@ -0,0 +1,151 @@
1
+ // src/filters/numbers.ts
2
+ const unitText = (unit) => unit === "" ? "" : ` ${unit}`;
3
+ const NUMBER_SIGN = {
4
+ equals: "=",
5
+ gte: "≥",
6
+ gt: ">",
7
+ lt: "<",
8
+ lte: "≤"
9
+ };
10
+ /** A number pick as a chip says it: "2–8 h", "≥ 2 h", "> 2 h", "< 8 h", "≤ 8 h", "= 4 h". */
11
+ function numberText(pick, unit) {
12
+ const [a, b] = pick.values;
13
+ const u = unitText(unit);
14
+ if (pick.op === "between") return `${String(a)}–${String(b)}${u}`;
15
+ return `${NUMBER_SIGN[pick.op] ?? pick.op} ${String(a)}${u}`;
16
+ }
17
+ /** The quick picks a number field's buckets give, for the operators the control allows. */
18
+ function numberPresets(edges, unit, ops = [
19
+ "lt",
20
+ "between",
21
+ "gte"
22
+ ]) {
23
+ const [lo, hi] = edges;
24
+ if (lo === void 0 || hi === void 0) return [];
25
+ const u = unitText(unit);
26
+ return [
27
+ {
28
+ kind: "under",
29
+ lo,
30
+ hi,
31
+ pick: {
32
+ op: "lt",
33
+ values: [lo]
34
+ },
35
+ label: `Under ${String(lo)}${u}`
36
+ },
37
+ {
38
+ kind: "between",
39
+ lo,
40
+ hi,
41
+ pick: {
42
+ op: "between",
43
+ values: [lo, hi]
44
+ },
45
+ label: `${String(lo)} to ${String(hi)}${u}`
46
+ },
47
+ {
48
+ kind: "over",
49
+ lo,
50
+ hi,
51
+ pick: {
52
+ op: "gte",
53
+ values: [hi]
54
+ },
55
+ label: `${String(hi)}${u} or more`
56
+ }
57
+ ].filter((p) => ops.includes(p.pick.op));
58
+ }
59
+ const typedNumber = (s) => {
60
+ if (s.trim() === "") return "blank";
61
+ const n = Number(s);
62
+ return Number.isFinite(n) ? n : "bad";
63
+ };
64
+ /** The first of these operators the control allows. */
65
+ const firstOf = (ops, wanted) => wanted.find((op) => op !== void 0 && ops.includes(op));
66
+ /** One typed bound as a pick: from the minimum, or under the maximum, keeping `prefer` (the
67
+ * operator it had) where the control allows it, so "> 2" edited to 3 stays strict. */
68
+ function oneSide(value, side, ops, prefer) {
69
+ const op = side === "min" ? firstOf(ops, [
70
+ prefer === "gt" || prefer === "gte" ? prefer : void 0,
71
+ "gte",
72
+ "gt"
73
+ ]) : firstOf(ops, [
74
+ prefer === "lt" || prefer === "lte" ? prefer : void 0,
75
+ "lt",
76
+ "lte"
77
+ ]);
78
+ return op === void 0 ? { problem: "bothBounds" } : { pick: {
79
+ op,
80
+ values: [value]
81
+ } };
82
+ }
83
+ /** Both bounds as a range, where the control has between. */
84
+ function range(a, b, ops) {
85
+ if (!ops.includes("between")) return { problem: "oneBound" };
86
+ return a > b ? { problem: "minOverMax" } : { pick: {
87
+ op: "between",
88
+ values: [a, b]
89
+ } };
90
+ }
91
+ /**
92
+ * Which boxes a control's operators give: a minimum, a maximum, or one exact value. An exact
93
+ * value is the only box for an equals-only control, and the box for a pick that is already
94
+ * "= 4" (`current`), so editing it keeps it exact.
95
+ */
96
+ function numberSides(ops, current) {
97
+ const min = ops.some((op) => op === "between" || op === "gte" || op === "gt");
98
+ const max = ops.some((op) => op === "between" || op === "lt" || op === "lte");
99
+ const exact = ops.includes("equals") && (current === "equals" || !min && !max);
100
+ return exact ? {
101
+ min: false,
102
+ max: false,
103
+ exact
104
+ } : {
105
+ min,
106
+ max,
107
+ exact
108
+ };
109
+ }
110
+ /**
111
+ * Typed bounds as a pick (none when blank), or why they are not one. Only finite numbers count;
112
+ * the operator is always one the control lists: between for both, a one-sided comparison for
113
+ * one (the one it had, if any), equals for an exact value. `prefer` is the pick's operator before
114
+ * the edit; a pick read into the boxes and applied unchanged comes back as itself.
115
+ */
116
+ function numberDraft(min, max, ops = [
117
+ "between",
118
+ "gte",
119
+ "lt"
120
+ ], prefer) {
121
+ const a = typedNumber(min);
122
+ const b = typedNumber(max);
123
+ if (a === "bad" || b === "bad") return { problem: "notNumbers" };
124
+ if (a !== "blank" && numberSides(ops, prefer).exact) return { pick: {
125
+ op: "equals",
126
+ values: [a]
127
+ } };
128
+ if (a !== "blank" && b !== "blank") return range(a, b, ops);
129
+ if (a !== "blank") return oneSide(a, "min", ops, prefer);
130
+ return b === "blank" ? {} : oneSide(b, "max", ops, prefer);
131
+ }
132
+ /** A number pick as the boxes show it: an exact value and a minimum both fill the first box. */
133
+ function numberBounds(pick) {
134
+ if (pick === void 0) return {
135
+ min: "",
136
+ max: ""
137
+ };
138
+ const [a = "", b = ""] = pick.values.map(String);
139
+ if (pick.op === "between") return {
140
+ min: a,
141
+ max: b
142
+ };
143
+ return pick.op === "gte" || pick.op === "gt" || pick.op === "equals" ? {
144
+ min: a,
145
+ max: ""
146
+ } : {
147
+ min: "",
148
+ max: a
149
+ };
150
+ }
151
+ export { numberBounds, numberDraft, numberPresets, numberSides, numberText, unitText };