derive-dashboards 0.1.1 → 0.3.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.
- package/README.md +5 -3
- package/dist/cache.mjs +2 -1
- package/dist/cli/add.mjs +130 -0
- package/dist/cli/cli.mjs +14 -12
- package/dist/cli/config.mjs +12 -1
- package/dist/cli/doctor.mjs +162 -0
- package/dist/cli/init.mjs +3 -2
- package/dist/cli/json.mjs +21 -0
- package/dist/cli/layers.mjs +33 -0
- package/dist/core/document/display.d.mts +63 -0
- package/dist/core/document/display.mjs +139 -0
- package/dist/core/document/layout.mjs +1 -1
- package/dist/core/document/view-layout.d.mts +14 -0
- package/dist/core/document/view-layout.mjs +157 -0
- package/dist/core/engine/filter.mjs +2 -2
- package/dist/core/engine/leaf.mjs +9 -4
- package/dist/core/engine/members.mjs +1 -1
- package/dist/core/engine/version.d.mts +5 -4
- package/dist/core/engine/version.mjs +5 -4
- package/dist/core/error/codes.d.mts +4 -1
- package/dist/core/error/codes.mjs +4 -1
- package/dist/core/index.d.mts +12 -8
- package/dist/core/index.mjs +11 -7
- package/dist/core/model/member.mjs +5 -2
- package/dist/core/model/types.d.mts +13 -13
- package/dist/core/model/validate/drill.mjs +6 -15
- package/dist/core/model/validate/fields.mjs +130 -0
- package/dist/core/model/validate/index.mjs +11 -3
- package/dist/core/model/validate/known-members.mjs +133 -0
- package/dist/core/model/validate/known-nested.mjs +85 -0
- package/dist/core/model/validate/known.mjs +94 -0
- package/dist/core/model/validate/members.mjs +7 -4
- package/dist/core/model/validate/source.mjs +8 -1
- package/dist/core/policy/bind-query.d.mts +12 -0
- package/dist/core/policy/bind-query.mjs +53 -0
- package/dist/core/query/alias.mjs +11 -1
- package/dist/core/query/drill/selection.d.mts +7 -2
- package/dist/core/query/drill/selection.mjs +18 -15
- package/dist/core/query/filter.d.mts +6 -0
- package/dist/core/query/picks.d.mts +8 -2
- package/dist/core/query/picks.mjs +11 -4
- package/dist/core/query/segments.mjs +2 -1
- package/dist/core/result/columns.d.mts +10 -1
- package/dist/core/result/columns.mjs +27 -5
- package/dist/core/result/time-axis.d.mts +23 -0
- package/dist/core/result/time-axis.mjs +72 -0
- package/dist/core/result/types.d.mts +36 -1
- package/dist/core/schema/aliases.d.mts +7 -0
- package/dist/core/schema/aliases.mjs +77 -0
- package/dist/core/schema/answer-parts.mjs +23 -1
- package/dist/core/schema/answers.d.mts +204 -0
- package/dist/core/schema/answers.mjs +2 -0
- package/dist/core/schema/index.d.mts +3 -2
- package/dist/core/schema/index.mjs +3 -2
- package/dist/core/schema/query.d.mts +81 -0
- package/dist/core/schema/query.mjs +9 -5
- package/dist/core/schema/tools.d.mts +2 -0
- package/dist/core/schema/tools.mjs +27 -1
- package/dist/core/time/bucket.mjs +8 -2
- package/dist/core/types/api.d.mts +7 -1
- package/dist/core/types/api.mjs +1 -1
- package/dist/dashboards.mjs +4 -0
- package/dist/drill-change.mjs +1 -1
- package/dist/drill-seal.mjs +1 -0
- package/dist/editing.mjs +2 -2
- package/dist/endpoints/dashboards.mjs +17 -2
- package/dist/endpoints/index.d.mts +15 -2
- package/dist/endpoints/meta.mjs +10 -1
- package/dist/execute.mjs +32 -17
- package/dist/handler.mjs +5 -1
- package/dist/index.mjs +6 -1
- package/dist/llms.txt +22 -0
- package/dist/mcp/format.mjs +42 -8
- package/dist/mcp/query.mjs +34 -4
- package/dist/postgres-store.mjs +1 -0
- package/dist/react/Canvas.mjs +28 -5
- package/dist/react/Cell.mjs +2 -1
- package/dist/react/Dashboard.d.mts +4 -0
- package/dist/react/Dashboard.mjs +27 -4
- package/dist/react/DashboardList.mjs +22 -22
- package/dist/react/DashboardListRow.mjs +49 -0
- package/dist/react/Discard.mjs +5 -2
- package/dist/react/Editing.mjs +19 -5
- package/dist/react/FilterBar.d.mts +7 -9
- package/dist/react/Grid.mjs +7 -19
- package/dist/react/Header.mjs +1 -1
- package/dist/react/History.mjs +5 -0
- package/dist/react/ReadFailed.mjs +23 -0
- package/dist/react/Spaces.mjs +15 -1
- package/dist/react/Tile.d.mts +2 -0
- package/dist/react/Tile.mjs +45 -10
- package/dist/react/_virtual/_rolldown/runtime.mjs +12 -0
- package/dist/react/arrange-steps.mjs +16 -13
- package/dist/react/arrange.mjs +10 -3
- package/dist/react/builder/Calcs.mjs +35 -11
- package/dist/react/builder/FilterEditor.mjs +196 -0
- package/dist/react/builder/Filters.mjs +172 -116
- package/dist/react/builder/Formula.mjs +82 -0
- package/dist/react/builder/Groups.mjs +1 -0
- package/dist/react/builder/Preview.mjs +1 -1
- package/dist/react/builder/RangeEditor.mjs +184 -0
- package/dist/react/builder/Sections.mjs +41 -6
- package/dist/react/builder/Time.mjs +138 -60
- package/dist/react/builder/Values.mjs +85 -0
- package/dist/react/builder/WidgetBuilder.mjs +50 -49
- package/dist/react/builder/base.d.mts +5 -0
- package/dist/react/builder/base.mjs +33 -8
- package/dist/react/builder/calc.mjs +15 -1
- package/dist/react/builder/escape.d.mts +1 -0
- package/dist/react/builder/escape.mjs +62 -0
- package/dist/react/builder/filter-tree.d.mts +9 -0
- package/dist/react/builder/filter-tree.mjs +186 -0
- package/dist/react/builder/formula-parse.d.mts +18 -0
- package/dist/react/builder/formula-parse.mjs +136 -0
- package/dist/react/builder/labels.d.mts +1 -1
- package/dist/react/builder/state.d.mts +2 -1
- package/dist/react/builder/state.mjs +23 -37
- package/dist/react/builder/useBuilder.d.mts +12 -1
- package/dist/react/builder/useBuilder.mjs +6 -3
- package/dist/react/builder/words.d.mts +59 -0
- package/dist/react/builder/words.mjs +73 -0
- package/dist/react/context.d.mts +15 -1
- package/dist/react/context.mjs +9 -3
- package/dist/react/drill/Bars.mjs +2 -2
- package/dist/react/drill/props.mjs +2 -1
- package/dist/react/drill/useDrill.d.mts +1 -1
- package/dist/react/drill/words.d.mts +3 -0
- package/dist/react/drill/words.mjs +2 -0
- package/dist/react/english.mjs +20 -5
- package/dist/react/filters/bar.d.mts +12 -0
- package/dist/react/filters/bar.mjs +135 -0
- package/dist/react/filters/calendar.mjs +193 -0
- package/dist/react/filters/chip.mjs +174 -0
- package/dist/react/filters/context.d.mts +22 -0
- package/dist/react/filters/context.mjs +78 -0
- package/dist/react/filters/dates.mjs +89 -0
- package/dist/react/filters/days.d.mts +36 -0
- package/dist/react/filters/days.mjs +87 -0
- package/dist/react/filters/field-step.mjs +81 -0
- package/dist/react/filters/hooks.d.mts +34 -0
- package/dist/react/filters/hooks.mjs +94 -0
- package/dist/react/filters/listbox.mjs +131 -0
- package/dist/react/filters/logic.d.mts +59 -0
- package/dist/react/filters/logic.mjs +109 -0
- package/dist/react/filters/menu-search.mjs +211 -0
- package/dist/react/filters/menu.mjs +85 -0
- package/dist/react/filters/number.mjs +127 -0
- package/dist/react/filters/numbers.d.mts +47 -0
- package/dist/react/filters/numbers.mjs +151 -0
- package/dist/react/filters/operator.mjs +104 -0
- package/dist/react/filters/phone.mjs +104 -0
- package/dist/react/filters/popover.d.mts +19 -0
- package/dist/react/filters/popover.mjs +153 -0
- package/dist/react/filters/rules.d.mts +8 -0
- package/dist/react/filters/rules.mjs +55 -0
- package/dist/react/filters/sheet.mjs +196 -0
- package/dist/react/filters/state.mjs +132 -0
- package/dist/react/filters/suggest.d.mts +47 -0
- package/dist/react/filters/suggest.mjs +97 -0
- package/dist/react/filters/values.mjs +166 -0
- package/dist/react/filters/words.d.mts +81 -0
- package/dist/react/filters/words.mjs +73 -0
- package/dist/react/format.mjs +11 -4
- package/dist/react/index.d.mts +15 -6
- package/dist/react/index.mjs +10 -4
- package/dist/react/messages.d.mts +34 -2
- package/dist/react/place.mjs +7 -3
- package/dist/react/renderers/index.d.mts +3 -21
- package/dist/react/renderers/index.mjs +68 -6
- package/dist/react/renderers/list.d.mts +2 -1
- package/dist/react/renderers/list.mjs +9 -12
- package/dist/react/renderers/props.d.mts +26 -0
- package/dist/react/scope.d.mts +4 -0
- package/dist/react/scope.mjs +19 -2
- package/dist/react/series-slots.d.mts +37 -0
- package/dist/react/series-slots.mjs +71 -0
- package/dist/react/series.d.mts +39 -9
- package/dist/react/series.mjs +144 -47
- package/dist/react/theme.css +930 -19
- package/dist/react/useBoard.d.mts +2 -0
- package/dist/react/useDashboard.d.mts +3 -0
- package/dist/react/useDashboard.mjs +6 -0
- package/dist/react/view-grid.mjs +16 -0
- package/dist/recharts/axes.mjs +28 -0
- package/dist/recharts/axis.mjs +84 -0
- package/dist/recharts/bar.d.mts +7 -0
- package/dist/recharts/bar.mjs +141 -0
- package/dist/recharts/charts.d.mts +4 -5
- package/dist/recharts/charts.mjs +2 -208
- package/dist/recharts/index.d.mts +3 -1
- package/dist/recharts/index.mjs +4 -1
- package/dist/recharts/line.d.mts +2 -0
- package/dist/recharts/line.mjs +112 -0
- package/dist/recharts/marks.mjs +47 -18
- package/dist/recharts/parts.mjs +103 -0
- package/dist/recharts/table.mjs +71 -0
- package/dist/records-read.mjs +1 -1
- package/dist/registry/filter-bar.json +26 -0
- package/dist/skills/derive-dashboards/SKILL.md +69 -0
- package/dist/store/apply.mjs +3 -1
- package/package.json +6 -2
- package/dist/react/FilterBar.mjs +0 -119
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { yAxes } from "./axes.mjs";
|
|
2
|
+
import { openPoint } from "./marks.mjs";
|
|
3
|
+
import { CHART, TOOLTIP, dashOf, legendText, runningLabel, seriesColors, tooltipFormat, tooltipLabel } from "./parts.mjs";
|
|
4
|
+
import { toSeries } from "./series.mjs";
|
|
5
|
+
import { Beside } from "./table.mjs";
|
|
6
|
+
import { useState } from "react";
|
|
7
|
+
import { CartesianGrid, Legend, Line, LineChart, ReferenceArea, ResponsiveContainer, Tooltip, XAxis } from "recharts";
|
|
8
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
// src/line.tsx
|
|
10
|
+
/** A line's dots: only the one the keyboard is on, ringed. */
|
|
11
|
+
const lineDot = (focused, key) => focused?.key === key ? (dot) => dot.index === focused.point ? /* @__PURE__ */ jsx("circle", {
|
|
12
|
+
className: "dd-chart-focus",
|
|
13
|
+
cx: dot.cx,
|
|
14
|
+
cy: dot.cy,
|
|
15
|
+
r: 5
|
|
16
|
+
}, dot.index) : /* @__PURE__ */ jsx("g", {}, dot.index) : false;
|
|
17
|
+
/** A click on a single line opens the point under it; with several, each opens from its own list. */
|
|
18
|
+
function lineClick(drill, drawn) {
|
|
19
|
+
const [only] = drawn.series;
|
|
20
|
+
return (state) => {
|
|
21
|
+
const point = Number(state.activeTooltipIndex);
|
|
22
|
+
if (only !== void 0 && drawn.series.length === 1 && Number.isInteger(point)) openPoint(drill, drawn, {
|
|
23
|
+
point,
|
|
24
|
+
series: only
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The bucket still running, shaded and saying so, so its low value reads as unfinished, not as
|
|
30
|
+
* a fall.
|
|
31
|
+
*/
|
|
32
|
+
function runningArea(props, points) {
|
|
33
|
+
const running = runningLabel(props, points);
|
|
34
|
+
const before = points.at(-2)?.x;
|
|
35
|
+
return running === void 0 || before === void 0 ? null : /* @__PURE__ */ jsx(ReferenceArea, {
|
|
36
|
+
yAxisId: "left",
|
|
37
|
+
x1: before,
|
|
38
|
+
x2: running,
|
|
39
|
+
fill: "var(--dd-line)",
|
|
40
|
+
fillOpacity: .35,
|
|
41
|
+
stroke: "none",
|
|
42
|
+
label: {
|
|
43
|
+
value: props.messages.soFar,
|
|
44
|
+
position: "insideTopRight",
|
|
45
|
+
fill: "var(--dd-muted)",
|
|
46
|
+
fontSize: 11
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function LineTile(props) {
|
|
51
|
+
const { answer, locale, drill } = props;
|
|
52
|
+
const [focused, setFocused] = useState(null);
|
|
53
|
+
const drawn = toSeries(answer, props, props.slots);
|
|
54
|
+
const { points, series } = drawn;
|
|
55
|
+
const painted = seriesColors(props, series);
|
|
56
|
+
const onClick = lineClick(drill, drawn);
|
|
57
|
+
const running = runningLabel(props, points);
|
|
58
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
59
|
+
className: "dd-chart",
|
|
60
|
+
"data-drillable": drill !== void 0,
|
|
61
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
62
|
+
className: "dd-chart-picture",
|
|
63
|
+
"aria-hidden": "true",
|
|
64
|
+
children: /* @__PURE__ */ jsx(ResponsiveContainer, { children: /* @__PURE__ */ jsxs(LineChart, {
|
|
65
|
+
...CHART,
|
|
66
|
+
data: points,
|
|
67
|
+
...drill === void 0 ? {} : { onClick },
|
|
68
|
+
children: [
|
|
69
|
+
/* @__PURE__ */ jsx(CartesianGrid, {
|
|
70
|
+
stroke: "var(--dd-line)",
|
|
71
|
+
vertical: false
|
|
72
|
+
}),
|
|
73
|
+
/* @__PURE__ */ jsx(XAxis, {
|
|
74
|
+
dataKey: "x",
|
|
75
|
+
tickLine: false,
|
|
76
|
+
axisLine: false,
|
|
77
|
+
stroke: "var(--dd-muted)",
|
|
78
|
+
minTickGap: 16
|
|
79
|
+
}),
|
|
80
|
+
yAxes(drawn, locale),
|
|
81
|
+
/* @__PURE__ */ jsx(Tooltip, {
|
|
82
|
+
...TOOLTIP,
|
|
83
|
+
formatter: tooltipFormat(props, drawn),
|
|
84
|
+
labelFormatter: tooltipLabel(props, running)
|
|
85
|
+
}),
|
|
86
|
+
runningArea(props, points),
|
|
87
|
+
series.length > 1 ? /* @__PURE__ */ jsx(Legend, {
|
|
88
|
+
itemSorter: null,
|
|
89
|
+
formatter: legendText
|
|
90
|
+
}) : null,
|
|
91
|
+
painted.map(({ series: s, color }, i) => /* @__PURE__ */ jsx(Line, {
|
|
92
|
+
dataKey: s.key,
|
|
93
|
+
name: s.label,
|
|
94
|
+
yAxisId: s.axis,
|
|
95
|
+
stroke: color,
|
|
96
|
+
strokeWidth: 2,
|
|
97
|
+
...dashOf(i),
|
|
98
|
+
legendType: "plainline",
|
|
99
|
+
dot: lineDot(focused, s.key),
|
|
100
|
+
isAnimationActive: false
|
|
101
|
+
}, s.key))
|
|
102
|
+
]
|
|
103
|
+
}) })
|
|
104
|
+
}), /* @__PURE__ */ jsx(Beside, {
|
|
105
|
+
drawn,
|
|
106
|
+
props,
|
|
107
|
+
kind: "line",
|
|
108
|
+
onFocusMark: setFocused
|
|
109
|
+
})]
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
export { LineTile };
|
package/dist/recharts/marks.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect, useRef } from "react";
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
2
|
import { jsx } from "react/jsx-runtime";
|
|
3
3
|
// src/marks.tsx
|
|
4
4
|
/** The line a series draws: its own column, or for a split chart the value it splits. */
|
|
@@ -40,44 +40,73 @@ function usePressing() {
|
|
|
40
40
|
globalThis.addEventListener("pointercancel", clear, { once: true });
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
|
-
/**
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
43
|
+
/** Where an arrow key moves along `count` marks from `at`; undefined for any other key. */
|
|
44
|
+
function nextMark(key, at, count) {
|
|
45
|
+
if (key === "ArrowRight" || key === "ArrowDown") return Math.min(at + 1, count - 1);
|
|
46
|
+
if (key === "ArrowLeft" || key === "ArrowUp") return Math.max(at - 1, 0);
|
|
47
|
+
if (key === "Home") return 0;
|
|
48
|
+
return key === "End" ? count - 1 : void 0;
|
|
49
|
+
}
|
|
50
|
+
/** The marks that open a row, in reading order: by point, then by series. */
|
|
51
|
+
function marksOf(drill, drawn, rows) {
|
|
52
|
+
return drawn.points.flatMap((_p, i) => drawn.series.flatMap((s) => {
|
|
53
53
|
const row = drawn.rowAt(i, s.key);
|
|
54
54
|
const props = row === void 0 || row >= rows ? void 0 : drill.props(row, entryOf(s));
|
|
55
|
-
|
|
55
|
+
if (props === void 0) return [];
|
|
56
|
+
const { role: _role, tabIndex: _tab, ...rest } = props;
|
|
57
|
+
return [{
|
|
56
58
|
key: `${String(i)}-${s.key}`,
|
|
57
59
|
point: i,
|
|
58
60
|
series: s.key,
|
|
59
|
-
props
|
|
61
|
+
props: rest
|
|
60
62
|
}];
|
|
61
63
|
}));
|
|
64
|
+
}
|
|
65
|
+
/** Arrow keys move focus along the list from the mark that has it, so the chart is one tab stop. */
|
|
66
|
+
function moveFocus(event) {
|
|
67
|
+
const buttons = [...event.currentTarget.querySelectorAll("button")];
|
|
68
|
+
const at = buttons.findIndex((b) => b === event.target);
|
|
69
|
+
const next = at < 0 ? void 0 : nextMark(event.key, at, buttons.length);
|
|
70
|
+
if (next === void 0) return;
|
|
71
|
+
event.preventDefault();
|
|
72
|
+
buttons[next]?.focus();
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Every mark that opens, as a list of buttons a keyboard reaches, so the chart opens without a
|
|
76
|
+
* pointer. The list is one tab stop: arrow keys, Home and End move along it, however many marks
|
|
77
|
+
* there are. It stays visually hidden and out of the chart's layout; the chart outlines the mark
|
|
78
|
+
* that has focus (`onFocusMark`). Each says its group and value, as a screen reader reads it.
|
|
79
|
+
* Nothing when drilling is off: the chart's data table carries its values then.
|
|
80
|
+
*/
|
|
81
|
+
function Marks(at) {
|
|
82
|
+
const clearAfterClick = usePressing();
|
|
83
|
+
const [current, setCurrent] = useState(0);
|
|
84
|
+
if (at.drill === void 0) return null;
|
|
85
|
+
const marks = marksOf(at.drill, at.drawn, at.rows);
|
|
62
86
|
if (marks.length === 0) return null;
|
|
87
|
+
const stop = Math.min(current, marks.length - 1);
|
|
63
88
|
return /* @__PURE__ */ jsx("ul", {
|
|
64
89
|
className: "dd-chart-marks",
|
|
65
|
-
|
|
90
|
+
"aria-label": at.label,
|
|
91
|
+
onKeyDown: moveFocus,
|
|
92
|
+
children: marks.map(({ key, point, series, props }, i) => /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsx("button", {
|
|
66
93
|
type: "button",
|
|
67
94
|
...props,
|
|
95
|
+
tabIndex: i === stop ? 0 : -1,
|
|
68
96
|
onFocus: () => {
|
|
69
|
-
|
|
97
|
+
setCurrent(i);
|
|
98
|
+
at.onFocusMark({
|
|
70
99
|
point,
|
|
71
100
|
key: series
|
|
72
101
|
});
|
|
73
102
|
},
|
|
74
103
|
onBlur: () => {
|
|
75
104
|
clearAfterClick(() => {
|
|
76
|
-
onFocusMark(null);
|
|
105
|
+
at.onFocusMark(null);
|
|
77
106
|
});
|
|
78
107
|
},
|
|
79
|
-
children: props["aria-label"]
|
|
108
|
+
children: String(props["aria-label"])
|
|
80
109
|
}) }, key))
|
|
81
110
|
});
|
|
82
111
|
}
|
|
83
|
-
export { Marks, openPoint };
|
|
112
|
+
export { Marks, nextMark, openPoint };
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { OTHER_KEY } from "./series.mjs";
|
|
2
|
+
import { cellText } from "./table.mjs";
|
|
3
|
+
import { chartColors, lastBucketOpen } from "derive-dashboards/react";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
import { displayOf } from "derive-dashboards/core";
|
|
6
|
+
// src/parts.tsx
|
|
7
|
+
/** Legend words in the text color: a series' hue marks its swatch, never its name. */
|
|
8
|
+
const legendText = (value) => /* @__PURE__ */ jsx("span", {
|
|
9
|
+
className: "dd-chart-legend",
|
|
10
|
+
children: value
|
|
11
|
+
});
|
|
12
|
+
/** A widget's own colour for a series, by its label (`display.colors`). */
|
|
13
|
+
const ownColor = (props, s) => displayOf("line", props.widget.display).colors?.[s.label];
|
|
14
|
+
/** The palette's colours, `--dd-series-1` to `--dd-series-6`. */
|
|
15
|
+
const PALETTE = 6;
|
|
16
|
+
/**
|
|
17
|
+
* Each series with its colour, in the series' order: the widget's own for it, else Other's quiet grey,
|
|
18
|
+
* else the palette's at the series' slot, which follows the series and not where it is drawn. In
|
|
19
|
+
* one chart two series share a colour only past the palette's six (`chartColors`).
|
|
20
|
+
*/
|
|
21
|
+
function seriesColors(props, series) {
|
|
22
|
+
const placed = series.map((s, at) => s.key === OTHER_KEY ? {} : { slot: s.slot ?? at });
|
|
23
|
+
const palette = chartColors(placed, PALETTE);
|
|
24
|
+
return series.map((s, at) => {
|
|
25
|
+
const place = palette[at];
|
|
26
|
+
const fallback = place === void 0 ? "var(--dd-other)" : `var(--dd-series-${place + 1})`;
|
|
27
|
+
return {
|
|
28
|
+
series: s,
|
|
29
|
+
color: ownColor(props, s) ?? fallback
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A tooltip's value: the answer's own cell behind the point, in its own series' column, so an
|
|
35
|
+
* exact amount reads exactly and each series keeps its unit. Only the pixels use the float.
|
|
36
|
+
*/
|
|
37
|
+
function tooltipFormat(props, drawn) {
|
|
38
|
+
return (value, _name, item) => {
|
|
39
|
+
const s = drawn.series.find((x) => x.key === item.dataKey);
|
|
40
|
+
if (s === void 0) return String(value);
|
|
41
|
+
const payload = item.payload;
|
|
42
|
+
const place = typeof payload === "object" && payload !== null && "#" in payload ? payload["#"] : void 0;
|
|
43
|
+
return cellText(props, drawn, {
|
|
44
|
+
place,
|
|
45
|
+
series: s,
|
|
46
|
+
value
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** Each line's own stroke, so series part by shape as well as by color. */
|
|
51
|
+
const DASHES = [
|
|
52
|
+
void 0,
|
|
53
|
+
"6 4",
|
|
54
|
+
"2 3",
|
|
55
|
+
"10 3 2 3"
|
|
56
|
+
];
|
|
57
|
+
/**
|
|
58
|
+
* The i-th line's dash, or none for the first: a solid line takes no dash at all. The dash goes
|
|
59
|
+
* by where the line is drawn, not by its series' slot: its job is to tell the lines of one chart
|
|
60
|
+
* apart without colour, and two slots four apart would share a dash.
|
|
61
|
+
*/
|
|
62
|
+
const dashOf = (i) => {
|
|
63
|
+
const dash = DASHES[i % DASHES.length];
|
|
64
|
+
return dash === void 0 ? {} : { strokeDasharray: dash };
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* A tooltip's title: the point's name, "so far" on a time bucket still running, and what a
|
|
68
|
+
* click does when the chart drills.
|
|
69
|
+
*/
|
|
70
|
+
const tooltipLabel = (props, running) => (label) => [
|
|
71
|
+
String(label),
|
|
72
|
+
running !== void 0 && String(label) === running ? props.messages.soFar : void 0,
|
|
73
|
+
props.drill === void 0 ? void 0 : props.messages.drillHint
|
|
74
|
+
].filter((part) => part !== void 0).join(" · ");
|
|
75
|
+
/** The x label of the last point when its time bucket is still running. */
|
|
76
|
+
const runningLabel = (props, points) => lastBucketOpen(props.answer) ? points.at(-1)?.x : void 0;
|
|
77
|
+
/** The tooltip in the theme's colors, light and dark; the hover band a soft wash, not a gray slab. */
|
|
78
|
+
const TOOLTIP = {
|
|
79
|
+
separator: ": ",
|
|
80
|
+
contentStyle: {
|
|
81
|
+
background: "var(--dd-surface)",
|
|
82
|
+
border: "1px solid var(--dd-line)",
|
|
83
|
+
borderRadius: "var(--dd-radius)",
|
|
84
|
+
color: "var(--dd-ink)"
|
|
85
|
+
},
|
|
86
|
+
labelStyle: { color: "var(--dd-muted)" },
|
|
87
|
+
itemStyle: { color: "var(--dd-ink)" },
|
|
88
|
+
cursor: {
|
|
89
|
+
fill: "var(--dd-line)",
|
|
90
|
+
fillOpacity: .4,
|
|
91
|
+
stroke: "var(--dd-line)"
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const CHART = {
|
|
95
|
+
accessibilityLayer: false,
|
|
96
|
+
margin: {
|
|
97
|
+
top: 4,
|
|
98
|
+
right: 8,
|
|
99
|
+
bottom: 0,
|
|
100
|
+
left: 0
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
export { CHART, TOOLTIP, dashOf, legendText, runningLabel, seriesColors, tooltipFormat, tooltipLabel };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { NotDrawn } from "./axes.mjs";
|
|
2
|
+
import { Marks } from "./marks.mjs";
|
|
3
|
+
import { formatCell } from "derive-dashboards/react";
|
|
4
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
// src/table.tsx
|
|
6
|
+
/**
|
|
7
|
+
* The answer's own cell behind a point of a series, formatted in its column, so an exact amount
|
|
8
|
+
* reads exactly and each series keeps its unit; the drawn number when no row stands behind it.
|
|
9
|
+
*/
|
|
10
|
+
function cellText(props, drawn, at) {
|
|
11
|
+
const { answer } = props;
|
|
12
|
+
const row = typeof at.place === "number" ? drawn.rowAt(at.place, at.series.key) : void 0;
|
|
13
|
+
const cell = row === void 0 ? void 0 : answer.rows[row]?.[answer.columns.indexOf(at.series.column)];
|
|
14
|
+
if (cell !== void 0) return formatCell(cell, at.series.column, props);
|
|
15
|
+
if (typeof at.value === "number") return formatCell(at.value, at.series.column, props);
|
|
16
|
+
const v = at.value;
|
|
17
|
+
return typeof v === "string" || typeof v === "boolean" || typeof v === "bigint" ? String(v) : "";
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The chart as a table, for screen readers: what a sighted reader gets from the picture and its
|
|
21
|
+
* tooltips, read with a screen reader's table keys. Visually hidden and out of the tab order, so
|
|
22
|
+
* a keyboard is never walked through every value; the picture itself is hidden from them.
|
|
23
|
+
*/
|
|
24
|
+
function ChartTable({ drawn, props, kind }) {
|
|
25
|
+
const { points, series } = drawn;
|
|
26
|
+
return /* @__PURE__ */ jsxs("table", {
|
|
27
|
+
className: "dd-sr-only",
|
|
28
|
+
children: [
|
|
29
|
+
/* @__PURE__ */ jsx("caption", { children: props.messages.chartTable({
|
|
30
|
+
title: props.widget.title,
|
|
31
|
+
kind,
|
|
32
|
+
points: points.length
|
|
33
|
+
}) }),
|
|
34
|
+
/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { children: [/* @__PURE__ */ jsx("td", {}), series.map((s) => /* @__PURE__ */ jsx("th", {
|
|
35
|
+
scope: "col",
|
|
36
|
+
children: s.label
|
|
37
|
+
}, s.key))] }) }),
|
|
38
|
+
/* @__PURE__ */ jsx("tbody", { children: points.map((point, place) => /* @__PURE__ */ jsxs("tr", { children: [/* @__PURE__ */ jsx("th", {
|
|
39
|
+
scope: "row",
|
|
40
|
+
children: point.x
|
|
41
|
+
}), series.map((s) => /* @__PURE__ */ jsx("td", { children: cellText(props, drawn, {
|
|
42
|
+
place,
|
|
43
|
+
series: s,
|
|
44
|
+
value: point[s.key]
|
|
45
|
+
}) }, s.key))] }, point.x)) })
|
|
46
|
+
]
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/** What sits beside every chart: its keyboard path, what it does not draw, and its data table. */
|
|
50
|
+
function Beside(at) {
|
|
51
|
+
const { drawn, props } = at;
|
|
52
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
53
|
+
/* @__PURE__ */ jsx(Marks, {
|
|
54
|
+
drill: props.drill,
|
|
55
|
+
drawn,
|
|
56
|
+
rows: props.answer.rows.length,
|
|
57
|
+
label: props.messages.chartPoints,
|
|
58
|
+
onFocusMark: at.onFocusMark
|
|
59
|
+
}),
|
|
60
|
+
/* @__PURE__ */ jsx(NotDrawn, {
|
|
61
|
+
drawn,
|
|
62
|
+
props
|
|
63
|
+
}),
|
|
64
|
+
/* @__PURE__ */ jsx(ChartTable, {
|
|
65
|
+
drawn,
|
|
66
|
+
props,
|
|
67
|
+
kind: at.kind
|
|
68
|
+
})
|
|
69
|
+
] });
|
|
70
|
+
}
|
|
71
|
+
export { Beside, cellText };
|
package/dist/records-read.mjs
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://ui.shadcn.com/schema/registry-item.json",
|
|
3
|
+
"name": "filter-bar",
|
|
4
|
+
"type": "registry:component",
|
|
5
|
+
"title": "Dashboard filter bar",
|
|
6
|
+
"description": "Linear-style filters for Derive Dashboards: one chip per filter, F to add, typed suggestions, date presets and ranges, number ranges, Undo. Reads field types, tones, buckets and units from your model.",
|
|
7
|
+
"dependencies": [
|
|
8
|
+
"derive-dashboards",
|
|
9
|
+
"cn",
|
|
10
|
+
"lucide-react",
|
|
11
|
+
"react-day-picker"
|
|
12
|
+
],
|
|
13
|
+
"registryDependencies": [
|
|
14
|
+
"button",
|
|
15
|
+
"popover",
|
|
16
|
+
"command",
|
|
17
|
+
"calendar"
|
|
18
|
+
],
|
|
19
|
+
"files": [
|
|
20
|
+
{
|
|
21
|
+
"path": "registry/filter-bar/filter-bar.tsx",
|
|
22
|
+
"type": "registry:component",
|
|
23
|
+
"content": "// Derive Dashboards filter bar, a shadcn/ui registry item:\n//\n// npx shadcn@latest add ./node_modules/derive-dashboards/dist/registry/filter-bar.json\n//\n// Then give it to the provider: <DashboardsProvider components={{ FilterBar: DashboardFilterBar }}>.\n// It reads each field's type, value tones, number buckets and unit from your model, so it needs no\n// configuration. `icons` and `renderMark` let you add your own. Built on Button, Popover, Command and\n// Calendar; the chip follows the Linear-style anatomy of bazza/ui's filters: field, operator,\n// values, remove.\n\"use client\";\n\nimport { cn } from \"cn\";\nimport {\n\ttype Control,\n\ttype DateRange,\n\tisDateRange,\n\tisValuePick,\n\ttype Picks,\n\ttype Primitive,\n} from \"derive-dashboards/core\";\nimport {\n\ttype FilterBarProps,\n\tfilterRules,\n\tuseControlValues,\n\tuseFilterFields,\n} from \"derive-dashboards/react\";\nimport {\n\tArrowLeft,\n\tCalendarDays,\n\tCheck,\n\tChevronDown,\n\tHash,\n\tListFilter,\n\ttype LucideIcon,\n\tRotateCcw,\n\tToggleLeft,\n\tX,\n} from \"lucide-react\";\nimport {\n\ttype ComponentProps,\n\ttype ComponentType,\n\tcreateContext,\n\ttype KeyboardEvent,\n\ttype ReactNode,\n\tuseContext,\n\tuseEffect,\n\tuseId,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n} from \"react\";\nimport type { DateRange as DayRange } from \"react-day-picker\";\nimport { Button } from \"@/components/ui/button\";\nimport { Calendar } from \"@/components/ui/calendar\";\nimport {\n\tCommand,\n\tCommandEmpty,\n\tCommandGroup,\n\tCommandInput,\n\tCommandItem,\n\tCommandList,\n\tCommandSeparator,\n} from \"@/components/ui/command\";\nimport { Popover, PopoverContent, PopoverTrigger } from \"@/components/ui/popover\";\n\n// The bar's rules (picks, dates, numbers, the menu's suggestions) come from the package, so this\n// bar behaves exactly like the built-in one; what is here is how it looks.\ntype FilterControl = filterRules.FilterControl;\ntype DateControl = filterRules.DateControl;\ntype ValuePick = filterRules.ValuePick;\ntype Fields = filterRules.Fields;\nconst {\n\taddDays,\n\tcanFlip,\n\ttakesMany,\n\tchangeText,\n\tchosenOf,\n\tdaysLabel,\n\tdaysPick,\n\tdefaultFormat,\n\tisNegative,\n\tisNumber,\n\tlistText,\n\tmatchingFields,\n\tnumberBounds,\n\tnumberDraft,\n\tnumberPresets,\n\tnumberSides,\n\tnumberText,\n\toffersNone,\n\topWord,\n\tpickedDays,\n\trangeLabel,\n\trelativeLabel,\n\tsuggest,\n\tsuggestionPick,\n\ttoggled,\n\tflipped,\n} = filterRules;\n/** The date range picked for a control, when one is. */\nconst rangeAt = (picks: Picks, id: string): DateRange | undefined => {\n\tconst p = picks[id];\n\treturn p !== undefined && isDateRange(p) ? p : undefined;\n};\nconst OP_TEXT = { isWord: \"is\", isNotWord: \"is not\", isAnyOf: \"is any of\", isNoneOf: \"is none of\" };\nconst opWords = (negative: boolean, many: boolean) => OP_TEXT[opWord(negative, many)];\nconst PROBLEM_TEXT = {\n\tnotNumbers: \"Enter numbers only.\",\n\tminOverMax: \"The minimum must be no more than the maximum.\",\n\toneBound: \"Enter a minimum or a maximum, not both.\",\n\tbothBounds: \"Enter both a minimum and a maximum.\",\n};\ntype Icon = LucideIcon | ComponentType<{ className?: string }>;\n\n/** What a host may add. Everything else comes from the model. */\nexport type DashboardFilterBarOptions = {\n\t/** An icon per control id; by default one per field type. */\n\ticons?: Readonly<Record<string, Icon>>;\n\t/**\n\t * Drawn before a value in lists and chips, such as a person's avatar. Return undefined to keep\n\t * the default: a dot in the value's tone from the model.\n\t */\n\trenderMark?: (control: Control, value: Primitive) => ReactNode;\n\t/** A value as words. By default yes or no for booleans, and the value with a capital first. */\n\tformatValue?: (control: Control, value: Primitive) => string | undefined;\n};\n\n/* ---------------------------------------------------------------- what the model says */\n\nconst Ctx = createContext<{\n\tfields: Fields;\n\toptions: DashboardFilterBarOptions;\n\ttimezone?: string | undefined;\n}>({\n\tfields: {},\n\toptions: {},\n});\nconst useBar = () => useContext(Ctx);\n\nfunction iconOf(control: Control, fields: Fields, icons?: Readonly<Record<string, Icon>>): Icon {\n\tconst own = icons?.[control.id];\n\tif (own !== undefined) return own;\n\tif (control.kind === \"date_range\") return CalendarDays;\n\tif (control.kind !== \"filter\") return ListFilter;\n\tif (isNumber(control, fields)) return Hash;\n\tif (fields[control.id]?.type === \"boolean\") return ToggleLeft;\n\treturn ListFilter;\n}\n\nconst TONE_CLASS: Record<string, string> = {\n\tpositive: \"bg-emerald-500\",\n\twarning: \"bg-amber-500\",\n\tnegative: \"bg-red-500\",\n\tneutral: \"bg-muted-foreground/50\",\n};\n\nfunction Kbd({ className, ...props }: ComponentProps<\"kbd\">) {\n\treturn (\n\t\t<kbd\n\t\t\tclassName={cn(\n\t\t\t\t\"pointer-events-none inline-flex h-4 min-w-4 select-none items-center justify-center rounded border bg-muted px-1 font-medium font-sans text-[10px] text-muted-foreground\",\n\t\t\t\tclassName,\n\t\t\t)}\n\t\t\t{...props}\n\t\t/>\n\t);\n}\n\n/* ---------------------------------------------------------------- words */\n\nfunction useFormat() {\n\tconst { options } = useBar();\n\tconst own = options.formatValue;\n\treturn own === undefined\n\t\t? defaultFormat\n\t\t: (control: Control, v: Primitive) => own(control, v) ?? defaultFormat(control, v);\n}\n/** Shown when no date is picked: each widget keeps the range it was built with. */\nconst NO_DATES = \"Each widget's own\";\n\nfunction useNarrow() {\n\tconst [narrow, setNarrow] = useState(\n\t\t() => globalThis.matchMedia?.(\"(max-width: 640px)\").matches ?? false,\n\t);\n\tuseEffect(() => {\n\t\tconst media = globalThis.matchMedia?.(\"(max-width: 640px)\");\n\t\tif (media === undefined) return;\n\t\tconst on = () => {\n\t\t\tsetNarrow(media.matches);\n\t\t};\n\t\tmedia.addEventListener(\"change\", on);\n\t\treturn () => {\n\t\t\tmedia.removeEventListener(\"change\", on);\n\t\t};\n\t}, []);\n\treturn narrow;\n}\n\n/* ---------------------------------------------------------------- the chip */\n\nconst SEGMENT =\n\t\"inline-flex items-center gap-1.5 px-2 outline-none hover:bg-accent focus-visible:bg-accent focus-visible:ring-2 focus-visible:ring-ring/50 focus-visible:ring-inset\";\n\n/**\n * One filter: field, operator, values and remove, each its own target, read as one group. It\n * sits inside its control's popover, whose picker stays mounted while the chip changes.\n */\nfunction ChipFrame({\n\ticon: Icon,\n\tlabel,\n\tsummary,\n\toperator,\n\tvalue,\n\tvalueLabel,\n\tonRemove,\n}: {\n\ticon: Icon;\n\tlabel: string;\n\tsummary: string;\n\toperator?: ReactNode;\n\tvalue: ReactNode;\n\tvalueLabel: string;\n\tonRemove?: () => void;\n}) {\n\treturn (\n\t\t<fieldset\n\t\t\taria-label={summary}\n\t\t\tclassName=\"m-0 inline-flex h-7 min-w-0 items-stretch overflow-hidden rounded-md border border-border bg-background p-0 text-[13px] shadow-xs\"\n\t\t>\n\t\t\t<PopoverTrigger\n\t\t\t\ttype=\"button\"\n\t\t\t\taria-label={`${label} filter`}\n\t\t\t\tclassName={cn(SEGMENT, \"text-muted-foreground\")}\n\t\t\t>\n\t\t\t\t<Icon className=\"size-3.5\" aria-hidden />\n\t\t\t\t<span className=\"font-medium text-foreground\">{label}</span>\n\t\t\t</PopoverTrigger>\n\t\t\t{operator}\n\t\t\t<PopoverTrigger\n\t\t\t\ttype=\"button\"\n\t\t\t\taria-label={valueLabel}\n\t\t\t\tclassName={cn(SEGMENT, \"border-border border-l font-medium\")}\n\t\t\t>\n\t\t\t\t{value}\n\t\t\t</PopoverTrigger>\n\t\t\t{onRemove === undefined ? null : (\n\t\t\t\t<button\n\t\t\t\t\ttype=\"button\"\n\t\t\t\t\taria-label={`Remove ${label} filter`}\n\t\t\t\t\tonClick={onRemove}\n\t\t\t\t\tclassName={cn(SEGMENT, \"border-border border-l px-1.5 text-muted-foreground\")}\n\t\t\t\t>\n\t\t\t\t\t<X className=\"size-3.5\" aria-hidden />\n\t\t\t\t</button>\n\t\t\t)}\n\t\t</fieldset>\n\t);\n}\n\n/** The operator as its own small menu: what it is now, and the other choice beside it. */\nfunction OperatorMenu({\n\tlabel,\n\tpick,\n\tonChange,\n}: {\n\tlabel: string;\n\tpick: ValuePick;\n\tonChange: (negative: boolean) => void;\n}) {\n\tconst [open, setOpen] = useState(false);\n\tconst negative = isNegative(pick.op);\n\tconst many = pick.values.length > 1;\n\tconst now = opWords(negative, many);\n\treturn (\n\t\t<Popover open={open} onOpenChange={setOpen}>\n\t\t\t<PopoverTrigger\n\t\t\t\ttype=\"button\"\n\t\t\t\taria-label={`${label} operator: ${now}. Change`}\n\t\t\t\tclassName={cn(\n\t\t\t\t\tSEGMENT,\n\t\t\t\t\t\"gap-0.5 border-border border-l text-muted-foreground hover:text-foreground\",\n\t\t\t\t)}\n\t\t\t>\n\t\t\t\t{now}\n\t\t\t\t<ChevronDown className=\"size-3 opacity-60\" aria-hidden />\n\t\t\t</PopoverTrigger>\n\t\t\t<PopoverContent className=\"dd-fb w-40 p-1\" role=\"menu\" aria-label={`${label} operator`}>\n\t\t\t\t{[false, true].map((neg) => (\n\t\t\t\t\t<button\n\t\t\t\t\t\tkey={String(neg)}\n\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\trole=\"menuitemradio\"\n\t\t\t\t\t\taria-checked={neg === negative}\n\t\t\t\t\t\tonClick={() => {\n\t\t\t\t\t\t\tonChange(neg);\n\t\t\t\t\t\t\tsetOpen(false);\n\t\t\t\t\t\t}}\n\t\t\t\t\t\tclassName=\"flex w-full items-center justify-between rounded-md px-2 py-1.5 text-left text-[13px] hover:bg-accent focus-visible:bg-accent focus-visible:outline-none\"\n\t\t\t\t\t>\n\t\t\t\t\t\t{opWords(neg, many)}\n\t\t\t\t\t\t{neg === negative ? <Check className=\"size-3.5\" aria-hidden /> : null}\n\t\t\t\t\t</button>\n\t\t\t\t))}\n\t\t\t</PopoverContent>\n\t\t</Popover>\n\t);\n}\n\n/** What sits before a value: the host's mark, else a dot in the value's tone from the model. */\nfunction Mark({ control, value }: { control: Control; value: Primitive }) {\n\tconst { fields, options } = useBar();\n\tconst own = options.renderMark?.(control, value);\n\tif (own !== undefined) return <>{own}</>;\n\tconst tones = fields[control.id]?.tones;\n\tif (tones === undefined) return null;\n\tconst tone = tones[String(value)] ?? \"neutral\";\n\treturn <span aria-hidden className={cn(\"size-2 shrink-0 rounded-full\", TONE_CLASS[tone])} />;\n}\n\nfunction ValuesSummary({ control, values }: { control: Control; values: readonly Primitive[] }) {\n\tconst format = useFormat();\n\tconst [first] = values;\n\tif (first === undefined) return null;\n\treturn (\n\t\t<>\n\t\t\t<span className=\"flex -space-x-0.5\">\n\t\t\t\t{values.slice(0, 3).map((v) => (\n\t\t\t\t\t<Mark key={String(v)} control={control} value={v} />\n\t\t\t\t))}\n\t\t\t</span>\n\t\t\t<span>{format(control, first)}</span>\n\t\t\t{values.length > 1 ? (\n\t\t\t\t<span className=\"text-muted-foreground\">+{String(values.length - 1)}</span>\n\t\t\t) : null}\n\t\t</>\n\t);\n}\n\n/* ---------------------------------------------------------------- pickers */\n\n/**\n * A searchable listbox of a control's values. Its options say whether each value is chosen\n * (`aria-selected`), the active row is separate (`aria-activedescendant`) and kept in view, the\n * order stays put while it is open, and results that no longer match what is typed are never\n * offered. A control's own list is searched by what it shows; a lookup on the server.\n */\nfunction ValueList({\n\tcontrol,\n\tchosen,\n\tmultiselect,\n\tonToggle,\n\tonClear,\n\tonBack,\n\theader,\n}: {\n\tcontrol: FilterControl;\n\tchosen: readonly Primitive[];\n\t/** Whether the control takes several values at once (else picking one replaces it). */\n\tmultiselect: boolean;\n\tonToggle: (value: Primitive) => void;\n\tonClear: () => void;\n\tonBack?: () => void;\n\theader?: ReactNode;\n}) {\n\tconst id = useId();\n\tconst format = useFormat();\n\tconst [search, setSearch] = useState(\"\");\n\tconst [debounced, setDebounced] = useState(\"\");\n\tuseEffect(() => {\n\t\tconst t = setTimeout(() => {\n\t\t\tsetDebounced(search);\n\t\t}, 150);\n\t\treturn () => {\n\t\t\tclearTimeout(t);\n\t\t};\n\t}, [search]);\n\tconst own = typeof control.values === \"object\";\n\tconst read = useControlValues(control, { search: own ? \"\" : debounced, limit: 50 });\n\tconst { values, loading } = read;\n\t// The values chosen when the list opened stay on top; later choices stay where they are.\n\tconst [pinned] = useState(() => chosen);\n\tconst needle = search.trim().toLowerCase();\n\tconst shown = useMemo(() => {\n\t\tconst none = offersNone(control);\n\t\tconst matches = (v: Primitive) =>\n\t\t\t(v !== null || none) &&\n\t\t\t(format(control, v).toLowerCase().includes(needle) ||\n\t\t\t\t(!own && String(v).toLowerCase().includes(needle)));\n\t\tconst rest = values.filter((v) => !pinned.includes(v));\n\t\tconst tail = none && !pinned.includes(null) && !rest.includes(null) ? [null] : [];\n\t\treturn [...pinned, ...rest, ...tail].filter((v) => matches(v));\n\t}, [values, pinned, needle, format, control, own]);\n\tconst pending = search !== debounced || loading;\n\tconst [active, setActive] = useState(0);\n\t// A new search starts at the first match (set while rendering, so no frame shows a stale row).\n\tconst [searched, setSearched] = useState(needle);\n\tif (searched !== needle) {\n\t\tsetSearched(needle);\n\t\tsetActive(0);\n\t}\n\tconst current = shown[Math.min(active, shown.length - 1)];\n\tconst optionId = (i: number) => `${id}-o${String(i)}`;\n\tconst onKey = (e: KeyboardEvent<HTMLInputElement>) => {\n\t\tif (e.nativeEvent.isComposing) return;\n\t\tif (e.key === \"ArrowDown\") {\n\t\t\te.preventDefault();\n\t\t\tsetActive((a) => Math.min(a + 1, shown.length - 1));\n\t\t} else if (e.key === \"ArrowUp\") {\n\t\t\te.preventDefault();\n\t\t\tsetActive((a) => Math.max(a - 1, 0));\n\t\t} else if (e.key === \"Enter\") {\n\t\t\te.preventDefault();\n\t\t\tif (current !== undefined) onToggle(current);\n\t\t} else if (e.key === \"Backspace\" && search === \"\" && onBack !== undefined) {\n\t\t\te.preventDefault();\n\t\t\tonBack();\n\t\t}\n\t};\n\treturn (\n\t\t<div className=\"flex flex-col text-[13px]\">\n\t\t\t{header}\n\t\t\t<div className=\"flex h-9 items-center gap-2 border-border border-b px-3\">\n\t\t\t\t<input\n\t\t\t\t\t// biome-ignore lint/a11y/noAutofocus: the picker opens to type into\n\t\t\t\t\tautoFocus\n\t\t\t\t\trole=\"combobox\"\n\t\t\t\t\taria-expanded\n\t\t\t\t\taria-controls={`${id}-list`}\n\t\t\t\t\taria-activedescendant={\n\t\t\t\t\t\tcurrent === undefined ? undefined : optionId(shown.indexOf(current))\n\t\t\t\t\t}\n\t\t\t\t\taria-label={`Search ${control.label.toLowerCase()}`}\n\t\t\t\t\tvalue={search}\n\t\t\t\t\tonChange={(e) => {\n\t\t\t\t\t\tsetSearch(e.target.value);\n\t\t\t\t\t}}\n\t\t\t\t\tonKeyDown={onKey}\n\t\t\t\t\tplaceholder={`Search ${control.label.toLowerCase()}…`}\n\t\t\t\t\tclassName=\"h-9 w-full bg-transparent outline-none placeholder:text-muted-foreground\"\n\t\t\t\t/>\n\t\t\t</div>\n\t\t\t<div\n\t\t\t\tid={`${id}-list`}\n\t\t\t\trole=\"listbox\"\n\t\t\t\taria-multiselectable={multiselect}\n\t\t\t\taria-label={`${control.label} values`}\n\t\t\t\taria-busy={pending}\n\t\t\t\tclassName=\"max-h-72 overflow-y-auto p-1\"\n\t\t\t>\n\t\t\t\t{shown.length === 0 ? (\n\t\t\t\t\t<div className=\"py-6 text-center text-muted-foreground\">\n\t\t\t\t\t\t{pending ? \"Searching…\" : `No ${control.label.toLowerCase()} matches “${search}”`}\n\t\t\t\t\t</div>\n\t\t\t\t) : (\n\t\t\t\t\tshown.map((v, i) => {\n\t\t\t\t\t\tconst on = chosen.includes(v);\n\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t// biome-ignore lint/a11y/useKeyWithClickEvents: the combobox input above drives the keyboard (aria-activedescendant)\n\t\t\t\t\t\t\t<div\n\t\t\t\t\t\t\t\tkey={String(v)}\n\t\t\t\t\t\t\t\tid={optionId(i)}\n\t\t\t\t\t\t\t\trole=\"option\"\n\t\t\t\t\t\t\t\taria-selected={on}\n\t\t\t\t\t\t\t\ttabIndex={-1}\n\t\t\t\t\t\t\t\tdata-active={i === active || undefined}\n\t\t\t\t\t\t\t\tref={(el) => {\n\t\t\t\t\t\t\t\t\tif (i === active) el?.scrollIntoView?.({ block: \"nearest\" });\n\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t\tonMouseMove={() => {\n\t\t\t\t\t\t\t\t\tsetActive(i);\n\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t\tonMouseDown={(e) => {\n\t\t\t\t\t\t\t\t\te.preventDefault();\n\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t\tonClick={() => {\n\t\t\t\t\t\t\t\t\tonToggle(v);\n\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t\tclassName=\"flex cursor-default select-none items-center gap-2 rounded-md px-2 py-1.5 data-[active]:bg-accent\"\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t<span\n\t\t\t\t\t\t\t\t\taria-hidden\n\t\t\t\t\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\t\t\t\t\"flex size-3.5 items-center justify-center border\",\n\t\t\t\t\t\t\t\t\t\tmultiselect ? \"rounded-[4px]\" : \"rounded-full\",\n\t\t\t\t\t\t\t\t\t\ton ? \"border-primary bg-primary text-primary-foreground\" : \"border-input\",\n\t\t\t\t\t\t\t\t\t)}\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t{on ? <Check className=\"size-2.5\" strokeWidth={3} /> : null}\n\t\t\t\t\t\t\t\t</span>\n\t\t\t\t\t\t\t\t<Mark control={control} value={v} />\n\t\t\t\t\t\t\t\t<span className=\"truncate\">{format(control, v)}</span>\n\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t);\n\t\t\t\t\t})\n\t\t\t\t)}\n\t\t\t</div>\n\t\t\t{read.failed ? (\n\t\t\t\t<div\n\t\t\t\t\trole=\"alert\"\n\t\t\t\t\tclassName=\"flex items-center justify-between border-border border-t px-3 py-2 text-[12px] text-destructive\"\n\t\t\t\t>\n\t\t\t\t\tCould not load the values.\n\t\t\t\t\t<button type=\"button\" className=\"underline\" onClick={read.retry}>\n\t\t\t\t\t\tRetry\n\t\t\t\t\t</button>\n\t\t\t\t</div>\n\t\t\t) : null}\n\t\t\t<div\n\t\t\t\tclassName=\"flex items-center justify-between border-border border-t px-3 py-2 text-[12px] text-muted-foreground\"\n\t\t\t\trole=\"status\"\n\t\t\t>\n\t\t\t\t<span>\n\t\t\t\t\t{pending && shown.length > 0\n\t\t\t\t\t\t? \"Searching…\"\n\t\t\t\t\t\t: chosen.length === 0\n\t\t\t\t\t\t\t? \"Any value\"\n\t\t\t\t\t\t\t: `${String(chosen.length)} selected`}\n\t\t\t\t</span>\n\t\t\t\t{chosen.length === 0 ? null : (\n\t\t\t\t\t<button type=\"button\" className=\"hover:text-foreground\" onClick={onClear}>\n\t\t\t\t\t\tClear\n\t\t\t\t\t</button>\n\t\t\t\t)}\n\t\t\t</div>\n\t\t</div>\n\t);\n}\n\nfunction PresetList({\n\tcontrol,\n\tpick,\n\tonPick,\n\textra,\n}: {\n\tcontrol: DateControl;\n\tpick: DateRange | undefined;\n\tonPick: (r?: DateRange) => void;\n\textra?: ReactNode;\n}) {\n\treturn (\n\t\t<div className=\"flex flex-col gap-0.5 p-1.5\">\n\t\t\t{control.presets.map((p) => {\n\t\t\t\tconst on = pick !== undefined && JSON.stringify(pick) === JSON.stringify(p);\n\t\t\t\treturn (\n\t\t\t\t\t<button\n\t\t\t\t\t\tkey={relativeLabel(p)}\n\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\taria-pressed={on}\n\t\t\t\t\t\tonClick={() => {\n\t\t\t\t\t\t\tonPick(p);\n\t\t\t\t\t\t}}\n\t\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\t\"flex items-center justify-between rounded-md px-2 py-1.5 text-left text-[13px] hover:bg-accent focus-visible:bg-accent focus-visible:outline-none\",\n\t\t\t\t\t\t\ton && \"bg-accent font-medium\",\n\t\t\t\t\t\t)}\n\t\t\t\t\t>\n\t\t\t\t\t\t{relativeLabel(p)}\n\t\t\t\t\t\t{on ? <Check className=\"size-3.5\" aria-hidden /> : null}\n\t\t\t\t\t</button>\n\t\t\t\t);\n\t\t\t})}\n\t\t\t<div className=\"my-1 h-px bg-border\" />\n\t\t\t<button\n\t\t\t\ttype=\"button\"\n\t\t\t\tonClick={() => {\n\t\t\t\t\tonPick();\n\t\t\t\t}}\n\t\t\t\tclassName=\"rounded-md px-2 py-1.5 text-left text-[13px] text-muted-foreground hover:bg-accent focus-visible:bg-accent focus-visible:outline-none\"\n\t\t\t>\n\t\t\t\t{NO_DATES}\n\t\t\t</button>\n\t\t\t{extra}\n\t\t</div>\n\t);\n}\n\n/** Presets on the left, a calendar on the right (one month on a phone), the chosen days below. */\nfunction DatePicker({\n\tcontrol,\n\tpick,\n\tonPick,\n}: {\n\tcontrol: DateControl;\n\tpick: DateRange | undefined;\n\tonPick: (r?: DateRange) => void;\n}) {\n\tconst narrow = useNarrow();\n\tconst { timezone } = useBar();\n\tconst zone = timezone === undefined ? \"the dashboard's time zone\" : timezone.replaceAll(\"_\", \" \");\n\tconst zoneLine = `Days in ${zone}`;\n\tconst [initial] = useState(() => pickedDays(pick));\n\tconst [days, setDays] = useState<DayRange | undefined>(initial);\n\tconst custom = days?.from !== undefined && days.to !== undefined;\n\treturn (\n\t\t<div className=\"flex flex-col sm:flex-row\">\n\t\t\t<div className=\"border-border sm:w-40 sm:border-r\">\n\t\t\t\t<PresetList control={control} pick={pick} onPick={onPick} />\n\t\t\t</div>\n\t\t\t<div className=\"flex flex-col\">\n\t\t\t\t<Calendar\n\t\t\t\t\tmode=\"range\"\n\t\t\t\t\tnumberOfMonths={narrow ? 1 : 2}\n\t\t\t\t\tselected={days}\n\t\t\t\t\tonSelect={setDays}\n\t\t\t\t\tdefaultMonth={initial?.from ?? addDays(new Date(), narrow ? 0 : -30)}\n\t\t\t\t\tdisabled={{ after: new Date() }}\n\t\t\t\t/>\n\t\t\t\t<div className=\"flex items-center justify-between gap-3 border-border border-t px-3 py-2 text-[12px] text-muted-foreground\">\n\t\t\t\t\t<span role=\"status\" className=\"flex flex-col\">\n\t\t\t\t\t\t<span>\n\t\t\t\t\t\t\t{custom && days.from && days.to\n\t\t\t\t\t\t\t\t? daysLabel(days.from, days.to)\n\t\t\t\t\t\t\t\t: pick === undefined\n\t\t\t\t\t\t\t\t\t? \"Pick a first and a last day\"\n\t\t\t\t\t\t\t\t\t: rangeLabel(pick)}\n\t\t\t\t\t\t</span>\n\t\t\t\t\t\t<span className=\"text-[11px]\">{zoneLine}</span>\n\t\t\t\t\t</span>\n\t\t\t\t\t<Button\n\t\t\t\t\t\tsize=\"sm\"\n\t\t\t\t\t\tdisabled={!custom}\n\t\t\t\t\t\tonClick={() => {\n\t\t\t\t\t\t\tif (days?.from === undefined || days.to === undefined) return;\n\t\t\t\t\t\t\tonPick(daysPick(days.from, days.to));\n\t\t\t\t\t\t}}\n\t\t\t\t\t>\n\t\t\t\t\t\tApply\n\t\t\t\t\t</Button>\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t</div>\n\t);\n}\n\n/** A number range: quick picks from the model's buckets, or your own bounds, checked. */\nfunction NumberPicker({\n\tcontrol,\n\tpick,\n\tonPick,\n}: {\n\tcontrol: FilterControl;\n\tpick: ValuePick | undefined;\n\tonPick: (p?: ValuePick) => void;\n}) {\n\tconst field = useBar().fields[control.id];\n\tconst spec = {\n\t\tunit: field?.unit ?? \"\",\n\t\tnoun: control.label.toLowerCase(),\n\t\tedges: field?.buckets ?? [],\n\t};\n\tconst quick = numberPresets(spec.edges, spec.unit, control.ops);\n\tconst [bounds] = useState(() => numberBounds(pick));\n\tconst [min, setMin] = useState(bounds.min);\n\tconst [max, setMax] = useState(bounds.max);\n\tconst draft = numberDraft(min, max, control.ops, pick?.op);\n\tconst sides = numberSides(control.ops, pick?.op);\n\tconst problem = draft.problem === undefined ? undefined : PROBLEM_TEXT[draft.problem];\n\tconst errorId = useId();\n\tconst apply = () => {\n\t\tif (problem === undefined) onPick(draft.pick);\n\t};\n\tconst input =\n\t\t\"h-8 w-full rounded-md border border-input bg-background px-2 text-[13px] outline-none focus-visible:ring-2 focus-visible:ring-ring/50 aria-[invalid=true]:border-destructive\";\n\treturn (\n\t\t<div className=\"flex flex-col gap-2 p-2\">\n\t\t\t<div className=\"flex flex-col gap-0.5\">\n\t\t\t\t{quick.map((q) => {\n\t\t\t\t\tconst on = JSON.stringify(q.pick) === JSON.stringify(pick);\n\t\t\t\t\treturn (\n\t\t\t\t\t\t<button\n\t\t\t\t\t\t\tkey={q.label}\n\t\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\t\taria-pressed={on}\n\t\t\t\t\t\t\tonClick={() => {\n\t\t\t\t\t\t\t\tonPick(q.pick);\n\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\t\t\"flex items-center justify-between rounded-md px-2 py-1.5 text-left text-[13px] hover:bg-accent focus-visible:bg-accent focus-visible:outline-none\",\n\t\t\t\t\t\t\t\ton && \"bg-accent font-medium\",\n\t\t\t\t\t\t\t)}\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{q.label}\n\t\t\t\t\t\t\t{on ? <Check className=\"size-3.5\" aria-hidden /> : null}\n\t\t\t\t\t\t</button>\n\t\t\t\t\t);\n\t\t\t\t})}\n\t\t\t</div>\n\t\t\t<div className=\"h-px bg-border\" />\n\t\t\t<div className=\"flex items-center gap-2 px-1 text-[12px] text-muted-foreground\">\n\t\t\t\t{sides.min || sides.exact ? (\n\t\t\t\t\t<input\n\t\t\t\t\t\tclassName={input}\n\t\t\t\t\t\tinputMode=\"decimal\"\n\t\t\t\t\t\taria-label={`${sides.exact ? \"Value\" : \"Minimum\"} ${spec.noun}`}\n\t\t\t\t\t\taria-invalid={problem !== undefined}\n\t\t\t\t\t\taria-describedby={problem === undefined ? undefined : errorId}\n\t\t\t\t\t\tplaceholder={sides.exact ? \"Value\" : \"Min\"}\n\t\t\t\t\t\tvalue={min}\n\t\t\t\t\t\tonChange={(e) => {\n\t\t\t\t\t\t\tsetMin(e.target.value);\n\t\t\t\t\t\t}}\n\t\t\t\t\t\tonKeyDown={(e) => {\n\t\t\t\t\t\t\tif (e.key === \"Enter\") apply();\n\t\t\t\t\t\t}}\n\t\t\t\t\t/>\n\t\t\t\t) : null}\n\t\t\t\t{sides.min && sides.max ? <span>to</span> : null}\n\t\t\t\t{sides.max ? (\n\t\t\t\t\t<input\n\t\t\t\t\t\tclassName={input}\n\t\t\t\t\t\tinputMode=\"decimal\"\n\t\t\t\t\t\taria-label={`Maximum ${spec.noun}`}\n\t\t\t\t\t\taria-invalid={problem !== undefined}\n\t\t\t\t\t\taria-describedby={problem === undefined ? undefined : errorId}\n\t\t\t\t\t\tplaceholder=\"Max\"\n\t\t\t\t\t\tvalue={max}\n\t\t\t\t\t\tonChange={(e) => {\n\t\t\t\t\t\t\tsetMax(e.target.value);\n\t\t\t\t\t\t}}\n\t\t\t\t\t\tonKeyDown={(e) => {\n\t\t\t\t\t\t\tif (e.key === \"Enter\") apply();\n\t\t\t\t\t\t}}\n\t\t\t\t\t/>\n\t\t\t\t) : null}\n\t\t\t\t<span>{spec.unit}</span>\n\t\t\t</div>\n\t\t\t{problem === undefined ? null : (\n\t\t\t\t<p id={errorId} role=\"alert\" className=\"px-1 text-[12px] text-destructive\">\n\t\t\t\t\t{problem}\n\t\t\t\t</p>\n\t\t\t)}\n\t\t\t<Button size=\"sm\" onClick={apply} disabled={problem !== undefined}>\n\t\t\t\tApply\n\t\t\t</Button>\n\t\t</div>\n\t);\n}\n\n/* ---------------------------------------------------------------- one control as a chip */\n\ntype OpenProps = { open: boolean; setOpen: (open: boolean) => void };\n\nfunction DateChip({\n\tcontrol,\n\tpick,\n\tonPick,\n\topen,\n\tsetOpen,\n}: {\n\tcontrol: DateControl;\n\tpick: DateRange | undefined;\n\tonPick: (r?: DateRange) => void;\n} & OpenProps) {\n\tconst text = pick === undefined ? NO_DATES : rangeLabel(pick);\n\treturn (\n\t\t<Popover open={open} onOpenChange={setOpen}>\n\t\t\t<ChipFrame\n\t\t\t\ticon={CalendarDays}\n\t\t\t\tlabel={control.label}\n\t\t\t\tsummary={`${control.label}: ${text}`}\n\t\t\t\tvalueLabel={`Change ${control.label} dates: ${text}`}\n\t\t\t\tvalue={\n\t\t\t\t\t<>\n\t\t\t\t\t\t<span className={pick === undefined ? \"font-normal text-muted-foreground\" : \"\"}>\n\t\t\t\t\t\t\t{text}\n\t\t\t\t\t\t</span>\n\t\t\t\t\t\t<ChevronDown className=\"size-3 text-muted-foreground\" aria-hidden />\n\t\t\t\t\t</>\n\t\t\t\t}\n\t\t\t\t{...(pick === undefined\n\t\t\t\t\t? {}\n\t\t\t\t\t: {\n\t\t\t\t\t\t\tonRemove: () => {\n\t\t\t\t\t\t\t\tonPick();\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t})}\n\t\t\t/>\n\t\t\t<PopoverContent className=\"dd-fb w-auto max-w-[calc(100vw-16px)] p-0\">\n\t\t\t\t<DatePicker\n\t\t\t\t\tcontrol={control}\n\t\t\t\t\tpick={pick}\n\t\t\t\t\tonPick={(r) => {\n\t\t\t\t\t\tonPick(r);\n\t\t\t\t\t\tsetOpen(false);\n\t\t\t\t\t}}\n\t\t\t\t/>\n\t\t\t</PopoverContent>\n\t\t</Popover>\n\t);\n}\n\n/**\n * A filter control: a quiet button while nothing is set, a chip once it is. Both sit in one\n * popover, so the picker keeps its search, order and place when the first value turns one into\n * the other.\n */\nfunction FilterChip({\n\tcontrol,\n\tpick,\n\tonPick,\n\topen,\n\tsetOpen,\n}: {\n\tcontrol: FilterControl;\n\tpick: ValuePick | undefined;\n\tonPick: (p?: ValuePick) => void;\n} & OpenProps) {\n\tconst { fields, options } = useBar();\n\tconst format = useFormat();\n\tconst Icon = iconOf(control, fields, options.icons);\n\tconst number = isNumber(control, fields);\n\tconst negative = pick !== undefined && isNegative(pick.op);\n\tconst chosen = chosenOf(pick);\n\tconst remove = () => {\n\t\tonPick();\n\t};\n\tlet trigger: ReactNode;\n\tif (pick === undefined)\n\t\ttrigger = (\n\t\t\t<PopoverTrigger\n\t\t\t\ttype=\"button\"\n\t\t\t\taria-label={`Filter by ${control.label}`}\n\t\t\t\t// On a phone the unset pinned filters live in the Filter menu, so the bar stays short.\n\t\t\t\tclassName=\"hidden h-7 items-center gap-1.5 rounded-md border border-border border-dashed px-2 text-[13px] text-foreground/70 outline-none hover:border-solid hover:bg-accent hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/50 sm:inline-flex\"\n\t\t\t>\n\t\t\t\t<Icon className=\"size-3.5\" aria-hidden />\n\t\t\t\t{control.label}\n\t\t\t</PopoverTrigger>\n\t\t);\n\telse if (number) {\n\t\tconst text = numberText(pick, fields[control.id]?.unit ?? \"\");\n\t\ttrigger = (\n\t\t\t<ChipFrame\n\t\t\t\ticon={Icon}\n\t\t\t\tlabel={control.label}\n\t\t\t\tsummary={`${control.label}: ${text}`}\n\t\t\t\tvalueLabel={`Change ${control.label}: ${text}`}\n\t\t\t\tvalue={text}\n\t\t\t\tonRemove={remove}\n\t\t\t/>\n\t\t);\n\t} else\n\t\ttrigger = (\n\t\t\t<ChipFrame\n\t\t\t\ticon={Icon}\n\t\t\t\tlabel={control.label}\n\t\t\t\tsummary={`${control.label} ${opWords(negative, chosen.length > 1)} ${listText(control, chosen, format)}`}\n\t\t\t\toperator={\n\t\t\t\t\t// Only where the control can say \"is not\"; else the operator is plain text.\n\t\t\t\t\tcanFlip(control, pick) ? (\n\t\t\t\t\t\t<OperatorMenu\n\t\t\t\t\t\t\tlabel={control.label}\n\t\t\t\t\t\t\tpick={pick}\n\t\t\t\t\t\t\tonChange={(neg) => {\n\t\t\t\t\t\t\t\tonPick(flipped(control, pick, neg));\n\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t/>\n\t\t\t\t\t) : (\n\t\t\t\t\t\t<span className=\"px-2 text-muted-foreground\">\n\t\t\t\t\t\t\t{opWords(negative, chosen.length > 1)}\n\t\t\t\t\t\t</span>\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tvalueLabel={`Change ${control.label} values: ${listText(control, chosen, format)}`}\n\t\t\t\tvalue={<ValuesSummary control={control} values={chosen} />}\n\t\t\t\tonRemove={remove}\n\t\t\t/>\n\t\t);\n\treturn (\n\t\t<Popover open={open} onOpenChange={setOpen}>\n\t\t\t{trigger}\n\t\t\t<PopoverContent className=\"dd-fb w-64 p-0\">\n\t\t\t\t{number ? (\n\t\t\t\t\t<NumberPicker\n\t\t\t\t\t\tcontrol={control}\n\t\t\t\t\t\tpick={pick}\n\t\t\t\t\t\tonPick={(p) => {\n\t\t\t\t\t\t\tonPick(p);\n\t\t\t\t\t\t\tsetOpen(false);\n\t\t\t\t\t\t}}\n\t\t\t\t\t/>\n\t\t\t\t) : (\n\t\t\t\t\t<ValueList\n\t\t\t\t\t\tcontrol={control}\n\t\t\t\t\t\tchosen={chosen}\n\t\t\t\t\t\tmultiselect={takesMany(control, negative)}\n\t\t\t\t\t\tonToggle={(v) => {\n\t\t\t\t\t\t\t// None stands alone: picking it replaces the values, and a value replaces it.\n\t\t\t\t\t\t\tonPick(toggled(control, pick, v));\n\t\t\t\t\t\t}}\n\t\t\t\t\t\tonClear={remove}\n\t\t\t\t\t/>\n\t\t\t\t)}\n\t\t\t</PopoverContent>\n\t\t</Popover>\n\t);\n}\n\n/* ---------------------------------------------------------------- + Filter, F */\n\n/** The longest typed word that is not the field's name, \"is\" or \"not\": what to ask a lookup for. */\nfunction lookupTerm(control: FilterControl, query: string): string {\n\tif (typeof control.values === \"object\") return \"\";\n\tconst label = control.label.toLowerCase().split(/\\s+/u);\n\treturn query\n\t\t.trim()\n\t\t.toLowerCase()\n\t\t.split(/\\s+/u)\n\t\t.filter((w) => w !== \"\" && w !== \"is\" && w !== \"not\" && !label.some((p) => p.startsWith(w)))\n\t\t.reduce((a, w) => (w.length > a.length ? w : a), \"\");\n}\n\n/** Reads one control's values into the menu's suggestions. One per control, so hooks stay fixed. */\nfunction Collect({\n\tcontrol,\n\tquery,\n\tonValues,\n}: {\n\tcontrol: FilterControl;\n\tquery: string;\n\tonValues: (id: string, values: readonly Primitive[], retry: (() => void) | null) => void;\n}) {\n\tconst { values, failed, retry } = useControlValues(control, {\n\t\tsearch: lookupTerm(control, query),\n\t});\n\tuseEffect(() => {\n\t\tonValues(control.id, values, failed ? retry : null);\n\t}, [control.id, values, failed, retry, onValues]);\n\treturn null;\n}\n\nfunction AddFilter({\n\tcontrols,\n\tpicks,\n\tsetPick,\n\topenDates,\n\topen,\n\tonOpenChange,\n}: {\n\tcontrols: readonly Control[];\n\tpicks: Picks;\n\tsetPick: (id: string, pick: Picks[string] | undefined) => void;\n\topenDates: () => void;\n\topen: boolean;\n\tonOpenChange: (open: boolean) => void;\n}) {\n\tconst [query, setQuery] = useState(\"\");\n\tconst [field, setField] = useState<Control | null>(null);\n\tconst filters = controls.filter((c): c is FilterControl => c.kind === \"filter\");\n\tconst date = controls.find((c): c is DateControl => c.kind === \"date_range\");\n\tconst { fields, options } = useBar();\n\tconst format = useFormat();\n\tconst [known, setKnown] = useState<Record<string, readonly Primitive[]>>({});\n\t// Fields whose values could not be read, with how to ask again: said in the menu with Retry.\n\tconst [failed, setFailed] = useState<Record<string, () => void>>({});\n\tconst onValues = useMemo(\n\t\t() => (id: string, values: readonly Primitive[], retry: (() => void) | null) => {\n\t\t\tsetKnown((k) =>\n\t\t\t\tJSON.stringify(k[id]) === JSON.stringify(values) ? k : { ...k, [id]: values },\n\t\t\t);\n\t\t\tsetFailed((f) => {\n\t\t\t\tif ((f[id] === undefined) === (retry === null)) return f;\n\t\t\t\tconst { [id]: _, ...rest } = f;\n\t\t\t\treturn retry === null ? rest : { ...rest, [id]: retry };\n\t\t\t});\n\t\t},\n\t\t[],\n\t);\n\t// Lookups are asked once typing settles, not on every key.\n\tconst [settled, setSettled] = useState(query);\n\tuseEffect(() => {\n\t\tconst t = setTimeout(() => {\n\t\t\tsetSettled(query);\n\t\t}, 150);\n\t\treturn () => {\n\t\t\tclearTimeout(t);\n\t\t};\n\t}, [query]);\n\tconst textFilters = filters.filter((c) => !isNumber(c, fields));\n\tconst suggestions = suggest({ filters: textFilters, date, known, query, format });\n\tconst close = () => {\n\t\tonOpenChange(false);\n\t\tsetField(null);\n\t\tsetQuery(\"\");\n\t};\n\tconst current = (id: string) => {\n\t\tconst p = picks[id];\n\t\treturn typeof p === \"object\" && p !== null && \"values\" in p ? p : undefined;\n\t};\n\tconst back = (\n\t\t<button\n\t\t\ttype=\"button\"\n\t\t\tonClick={() => {\n\t\t\t\tsetField(null);\n\t\t\t}}\n\t\t\tclassName=\"flex items-center gap-1.5 border-border border-b px-3 py-2 text-left text-[12px] text-muted-foreground hover:text-foreground\"\n\t\t>\n\t\t\t<ArrowLeft className=\"size-3\" aria-hidden /> {field?.label}\n\t\t</button>\n\t);\n\tlet body: ReactNode;\n\tif (field === null) {\n\t\tbody = (\n\t\t\t<Command shouldFilter={false} loop label=\"Add a filter\">\n\t\t\t\t<CommandInput\n\t\t\t\t\tvalue={query}\n\t\t\t\t\tonValueChange={setQuery}\n\t\t\t\t\tplaceholder=\"Filter by… try “stat rev” or “last 90”\"\n\t\t\t\t/>\n\t\t\t\t{Object.keys(failed).length === 0 ? null : (\n\t\t\t\t\t<div\n\t\t\t\t\t\trole=\"alert\"\n\t\t\t\t\t\tclassName=\"flex items-center justify-between border-border border-b px-3 py-2 text-[12px] text-destructive\"\n\t\t\t\t\t>\n\t\t\t\t\t\tSome values could not be loaded.\n\t\t\t\t\t\t<button\n\t\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\t\tclassName=\"underline\"\n\t\t\t\t\t\t\tonClick={() => {\n\t\t\t\t\t\t\t\tfor (const retry of Object.values(failed)) retry();\n\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\tRetry\n\t\t\t\t\t\t</button>\n\t\t\t\t\t</div>\n\t\t\t\t)}\n\t\t\t\t<CommandList>\n\t\t\t\t\t<CommandEmpty>No field or value matches.</CommandEmpty>\n\t\t\t\t\t{suggestions.length === 0 ? null : (\n\t\t\t\t\t\t<CommandGroup heading=\"Suggestions\">\n\t\t\t\t\t\t\t{suggestions.map((s) => {\n\t\t\t\t\t\t\t\tif (s.kind === \"date\")\n\t\t\t\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t\t\t\t<CommandItem\n\t\t\t\t\t\t\t\t\t\t\tkey={`date:${relativeLabel(s.range)}`}\n\t\t\t\t\t\t\t\t\t\t\tvalue={`date:${relativeLabel(s.range)}`}\n\t\t\t\t\t\t\t\t\t\t\tonSelect={() => {\n\t\t\t\t\t\t\t\t\t\t\t\tsetPick(s.control.id, s.range);\n\t\t\t\t\t\t\t\t\t\t\t\tclose();\n\t\t\t\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t\t\t<CalendarDays className=\"text-muted-foreground\" aria-hidden />\n\t\t\t\t\t\t\t\t\t\t\t<span className=\"text-muted-foreground\">{s.control.label}</span>\n\t\t\t\t\t\t\t\t\t\t\t<span className=\"font-medium\">{relativeLabel(s.range)}</span>\n\t\t\t\t\t\t\t\t\t\t</CommandItem>\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tconst Icon = iconOf(s.control, fields, options.icons);\n\t\t\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t\t\t<CommandItem\n\t\t\t\t\t\t\t\t\t\tkey={`${s.control.id}:${String(s.value)}`}\n\t\t\t\t\t\t\t\t\t\tvalue={`${s.control.id}:${String(s.value)}`}\n\t\t\t\t\t\t\t\t\t\tonSelect={() => {\n\t\t\t\t\t\t\t\t\t\t\tsetPick(s.control.id, suggestionPick(s, picks));\n\t\t\t\t\t\t\t\t\t\t\tclose();\n\t\t\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t\t<Icon className=\"text-muted-foreground\" aria-hidden />\n\t\t\t\t\t\t\t\t\t\t<span className=\"text-muted-foreground\">{s.control.label}</span>\n\t\t\t\t\t\t\t\t\t\t<span className=\"text-muted-foreground\">{s.negative ? \"is not\" : \"is\"}</span>\n\t\t\t\t\t\t\t\t\t\t<Mark control={s.control} value={s.value} />\n\t\t\t\t\t\t\t\t\t\t<span className=\"font-medium\">{format(s.control, s.value)}</span>\n\t\t\t\t\t\t\t\t\t</CommandItem>\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t})}\n\t\t\t\t\t\t</CommandGroup>\n\t\t\t\t\t)}\n\t\t\t\t\t{suggestions.length === 0 ? null : <CommandSeparator />}\n\t\t\t\t\t<CommandGroup heading=\"Fields\">\n\t\t\t\t\t\t{matchingFields(controls, query).map((c) => {\n\t\t\t\t\t\t\tconst Icon = iconOf(c, fields, options.icons);\n\t\t\t\t\t\t\tconst set = picks[c.id] !== undefined;\n\t\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t\t<CommandItem\n\t\t\t\t\t\t\t\t\tkey={c.id}\n\t\t\t\t\t\t\t\t\tvalue={c.id}\n\t\t\t\t\t\t\t\t\tonSelect={() => {\n\t\t\t\t\t\t\t\t\t\tsetField(c);\n\t\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t<Icon className=\"text-muted-foreground\" aria-hidden />\n\t\t\t\t\t\t\t\t\t{c.label}\n\t\t\t\t\t\t\t\t\t{set ? (\n\t\t\t\t\t\t\t\t\t\t<span className=\"ml-auto text-[11px] text-muted-foreground\">set</span>\n\t\t\t\t\t\t\t\t\t) : null}\n\t\t\t\t\t\t\t\t</CommandItem>\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t})}\n\t\t\t\t\t</CommandGroup>\n\t\t\t\t</CommandList>\n\t\t\t\t<div\n\t\t\t\t\taria-hidden\n\t\t\t\t\tclassName=\"flex items-center gap-3 border-border border-t px-3 py-2 text-[11px] text-muted-foreground\"\n\t\t\t\t>\n\t\t\t\t\t<span className=\"inline-flex items-center gap-1\">\n\t\t\t\t\t\t<Kbd>↵</Kbd> pick\n\t\t\t\t\t</span>\n\t\t\t\t\t<span className=\"inline-flex items-center gap-1\">\n\t\t\t\t\t\t<Kbd>⇧</Kbd>\n\t\t\t\t\t\t<Kbd>F</Kbd> remove last\n\t\t\t\t\t</span>\n\t\t\t\t\t<span className=\"inline-flex items-center gap-1\">\n\t\t\t\t\t\t<Kbd>esc</Kbd> close\n\t\t\t\t\t</span>\n\t\t\t\t</div>\n\t\t\t</Command>\n\t\t);\n\t} else if (field.kind === \"date_range\") {\n\t\tbody = (\n\t\t\t<div className=\"flex flex-col\">\n\t\t\t\t{back}\n\t\t\t\t<PresetList\n\t\t\t\t\tcontrol={field}\n\t\t\t\t\tpick={rangeAt(picks, field.id)}\n\t\t\t\t\tonPick={(r) => {\n\t\t\t\t\t\tsetPick(field.id, r);\n\t\t\t\t\t\tclose();\n\t\t\t\t\t}}\n\t\t\t\t\textra={\n\t\t\t\t\t\t<button\n\t\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\t\tonClick={() => {\n\t\t\t\t\t\t\t\tclose();\n\t\t\t\t\t\t\t\topenDates();\n\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\tclassName=\"rounded-md px-2 py-1.5 text-left text-[13px] hover:bg-accent focus-visible:bg-accent focus-visible:outline-none\"\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\tCustom range…\n\t\t\t\t\t\t</button>\n\t\t\t\t\t}\n\t\t\t\t/>\n\t\t\t</div>\n\t\t);\n\t} else if (field.kind === \"filter\" && isNumber(field, fields)) {\n\t\tbody = (\n\t\t\t<>\n\t\t\t\t{back}\n\t\t\t\t<NumberPicker\n\t\t\t\t\tcontrol={field}\n\t\t\t\t\tpick={current(field.id)}\n\t\t\t\t\tonPick={(p) => {\n\t\t\t\t\t\tsetPick(field.id, p);\n\t\t\t\t\t\tclose();\n\t\t\t\t\t}}\n\t\t\t\t/>\n\t\t\t</>\n\t\t);\n\t} else if (field.kind === \"filter\") {\n\t\tbody = (\n\t\t\t<ValueList\n\t\t\t\tcontrol={field}\n\t\t\t\tchosen={chosenOf(current(field.id))}\n\t\t\t\tmultiselect={takesMany(\n\t\t\t\t\tfield,\n\t\t\t\t\tcurrent(field.id) !== undefined && isNegative(current(field.id)?.op ?? \"equals\"),\n\t\t\t\t)}\n\t\t\t\theader={back}\n\t\t\t\tonBack={() => {\n\t\t\t\t\tsetField(null);\n\t\t\t\t}}\n\t\t\t\tonToggle={(v) => {\n\t\t\t\t\tsetPick(field.id, toggled(field, current(field.id), v));\n\t\t\t\t}}\n\t\t\t\tonClear={() => {\n\t\t\t\t\tsetPick(field.id, undefined);\n\t\t\t\t}}\n\t\t\t/>\n\t\t);\n\t}\n\treturn (\n\t\t<Popover\n\t\t\topen={open}\n\t\t\tonOpenChange={(o) => {\n\t\t\t\tif (o) onOpenChange(true);\n\t\t\t\telse close();\n\t\t\t}}\n\t\t>\n\t\t\t<PopoverTrigger\n\t\t\t\ttype=\"button\"\n\t\t\t\taria-label=\"Add a filter\"\n\t\t\t\taria-keyshortcuts=\"F\"\n\t\t\t\tclassName=\"inline-flex h-7 items-center gap-1.5 rounded-md px-2 text-[13px] text-muted-foreground outline-none hover:bg-accent hover:text-accent-foreground focus-visible:ring-2 focus-visible:ring-ring/50 [&_svg]:size-4\"\n\t\t\t>\n\t\t\t\t<ListFilter aria-hidden />\n\t\t\t\tFilter\n\t\t\t\t<Kbd aria-hidden>F</Kbd>\n\t\t\t</PopoverTrigger>\n\t\t\t<PopoverContent className=\"dd-fb w-72 p-0\">\n\t\t\t\t{textFilters.map((c) => (\n\t\t\t\t\t<Collect key={c.id} control={c} query={settled} onValues={onValues} />\n\t\t\t\t))}\n\t\t\t\t{body}\n\t\t\t</PopoverContent>\n\t\t</Popover>\n\t);\n}\n\n/* ---------------------------------------------------------------- the bar */\n\ntype Undo = { text: string; picks: Picks };\n\n/**\n * The dashboard's filter bar: one chip per filter, F to add one, Shift+F to remove the last,\n * Reset, Undo. Give it to the provider as `components={{ FilterBar: DashboardFilterBar }}`, or wrap\n * it to pass options.\n */\nexport function DashboardFilterBar({\n\tcontrols,\n\tpicks,\n\tonChange,\n\ttimezone,\n\t...options\n}: FilterBarProps & DashboardFilterBarOptions) {\n\tconst fields = useFilterFields(controls);\n\tconst [adding, setAdding] = useState(false);\n\t// One picker open at a time, kept open while its chip turns from empty to set.\n\tconst [openId, setOpenId] = useState<string | null>(null);\n\tconst [undo, setUndo] = useState<Undo | null>(null);\n\tconst order = useRef<string[]>([]);\n\t// A view that opened from a link says so until the viewer changes it.\n\tconst [fromLink] = useState(() =>\n\t\tObject.keys(picks).length === 0 ? null : JSON.stringify(picks),\n\t);\n\tuseEffect(() => {\n\t\tif (undo === null) return;\n\t\tconst t = setTimeout(() => {\n\t\t\tsetUndo(null);\n\t\t}, 8000);\n\t\treturn () => {\n\t\t\tclearTimeout(t);\n\t\t};\n\t}, [undo]);\n\tconst change = (next: Picks) => {\n\t\tconst text = changeText(controls, picks, next);\n\t\tsetUndo(text === undefined ? null : { text, picks });\n\t\tonChange(next);\n\t};\n\tconst setPick = (id: string, pick: Picks[string] | undefined) => {\n\t\tconst next: Picks = { ...picks };\n\t\tif (pick === undefined) delete next[id];\n\t\telse next[id] = pick;\n\t\torder.current = [...order.current.filter((x) => x !== id), ...(pick === undefined ? [] : [id])];\n\t\tchange(next);\n\t};\n\t// F opens the filter menu; Shift+F removes the filter set last.\n\tuseEffect(() => {\n\t\tconst onKey = (e: globalThis.KeyboardEvent) => {\n\t\t\tconst target = e.target instanceof HTMLElement ? e.target : null;\n\t\t\tif (target?.closest(\"input, textarea, select, [contenteditable], [role=dialog]\")) return;\n\t\t\tif (e.metaKey || e.ctrlKey || e.altKey || e.key.toLowerCase() !== \"f\") return;\n\t\t\te.preventDefault();\n\t\t\tif (e.shiftKey) {\n\t\t\t\tconst last =\n\t\t\t\t\torder.current.findLast((id) => picks[id] !== undefined) ?? Object.keys(picks).at(-1);\n\t\t\t\tif (last !== undefined) setPick(last, undefined);\n\t\t\t} else setAdding(true);\n\t\t};\n\t\tglobalThis.addEventListener(\"keydown\", onKey);\n\t\treturn () => {\n\t\t\tglobalThis.removeEventListener(\"keydown\", onKey);\n\t\t};\n\t});\n\tif (controls.length === 0) return null;\n\tconst date = controls.find((c): c is DateControl => c.kind === \"date_range\");\n\tconst filters = controls.filter((c): c is FilterControl => c.kind === \"filter\");\n\tconst datePick = date === undefined ? undefined : rangeAt(picks, date.id);\n\tconst pickOf = (id: string) => {\n\t\tconst p = picks[id];\n\t\treturn isValuePick(p) ? p : undefined;\n\t};\n\tconst any = Object.keys(picks).length > 0;\n\tconst linked = fromLink !== null && JSON.stringify(picks) === fromLink;\n\tconst openProps = (id: string) => ({\n\t\topen: openId === id,\n\t\tsetOpen: (o: boolean) => {\n\t\t\tsetOpenId(o ? id : null);\n\t\t},\n\t});\n\tconst bar = { fields, options, timezone };\n\treturn (\n\t\t<Ctx.Provider value={bar}>\n\t\t\t<div className=\"dd-fb pb-1\">\n\t\t\t\t<div className=\"flex flex-wrap items-center gap-1.5\" role=\"toolbar\" aria-label=\"Filters\">\n\t\t\t\t\t{date === undefined ? null : (\n\t\t\t\t\t\t<DateChip\n\t\t\t\t\t\t\tcontrol={date}\n\t\t\t\t\t\t\tpick={datePick}\n\t\t\t\t\t\t\t{...openProps(date.id)}\n\t\t\t\t\t\t\tonPick={(r) => {\n\t\t\t\t\t\t\t\tsetPick(date.id, r);\n\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t/>\n\t\t\t\t\t)}\n\t\t\t\t\t{filters.slice(0, -1).map((c) => (\n\t\t\t\t\t\t<FilterChip\n\t\t\t\t\t\t\tkey={c.id}\n\t\t\t\t\t\t\tcontrol={c}\n\t\t\t\t\t\t\tpick={pickOf(c.id)}\n\t\t\t\t\t\t\t{...openProps(c.id)}\n\t\t\t\t\t\t\tonPick={(p) => {\n\t\t\t\t\t\t\t\tsetPick(c.id, p);\n\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t/>\n\t\t\t\t\t))}\n\t\t\t\t\t{/* The last chip and Filter wrap together, so Filter never sits alone on a row. */}\n\t\t\t\t\t<span className=\"inline-flex items-center gap-1.5\">\n\t\t\t\t\t\t{filters.slice(-1).map((c) => (\n\t\t\t\t\t\t\t<FilterChip\n\t\t\t\t\t\t\t\tkey={c.id}\n\t\t\t\t\t\t\t\tcontrol={c}\n\t\t\t\t\t\t\t\tpick={pickOf(c.id)}\n\t\t\t\t\t\t\t\t{...openProps(c.id)}\n\t\t\t\t\t\t\t\tonPick={(p) => {\n\t\t\t\t\t\t\t\t\tsetPick(c.id, p);\n\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t))}\n\t\t\t\t\t\t<AddFilter\n\t\t\t\t\t\t\tcontrols={controls}\n\t\t\t\t\t\t\tpicks={picks}\n\t\t\t\t\t\t\tsetPick={setPick}\n\t\t\t\t\t\t\topenDates={() => {\n\t\t\t\t\t\t\t\tif (date !== undefined) setOpenId(date.id);\n\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\topen={adding}\n\t\t\t\t\t\t\tonOpenChange={setAdding}\n\t\t\t\t\t\t/>\n\t\t\t\t\t</span>\n\t\t\t\t\t{any ? (\n\t\t\t\t\t\t<div className=\"ml-auto flex items-center gap-1\">\n\t\t\t\t\t\t\t<span className=\"text-[12px] text-muted-foreground\">\n\t\t\t\t\t\t\t\t{linked ? \"From a link\" : \"Your view\"}\n\t\t\t\t\t\t\t</span>\n\t\t\t\t\t\t\t<Button\n\t\t\t\t\t\t\t\tvariant=\"ghost\"\n\t\t\t\t\t\t\t\tsize=\"sm\"\n\t\t\t\t\t\t\t\tonClick={() => {\n\t\t\t\t\t\t\t\t\torder.current = [];\n\t\t\t\t\t\t\t\t\tchange({});\n\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t<RotateCcw aria-hidden />\n\t\t\t\t\t\t\t\tReset\n\t\t\t\t\t\t\t</Button>\n\t\t\t\t\t\t</div>\n\t\t\t\t\t) : null}\n\t\t\t\t</div>\n\t\t\t\t<div\n\t\t\t\t\trole=\"status\"\n\t\t\t\t\taria-live=\"polite\"\n\t\t\t\t\tclassName=\"pointer-events-none fixed bottom-4 left-1/2 z-50 -translate-x-1/2\"\n\t\t\t\t>\n\t\t\t\t\t{undo === null ? null : (\n\t\t\t\t\t\t<div className=\"pointer-events-auto inline-flex animate-in items-center gap-3 rounded-md bg-foreground px-3 py-1.5 text-[13px] text-background shadow-lg\">\n\t\t\t\t\t\t\t{undo.text}\n\t\t\t\t\t\t\t<button\n\t\t\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\t\t\tclassName=\"font-semibold underline-offset-2 hover:underline\"\n\t\t\t\t\t\t\t\tonClick={() => {\n\t\t\t\t\t\t\t\t\tonChange(undo.picks);\n\t\t\t\t\t\t\t\t\tsetUndo(null);\n\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\tUndo\n\t\t\t\t\t\t\t</button>\n\t\t\t\t\t\t</div>\n\t\t\t\t\t)}\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t</Ctx.Provider>\n\t);\n}\n"
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: derive-dashboards
|
|
3
|
+
description: Add Derive Dashboards (embedded, editable dashboards over your own Postgres) to a TypeScript app, or change one that has it. Use when asked to add dashboards, analytics, charts or metrics pages to an app, to describe tables as a model, to mount the dashboards handler, to show or theme a dashboard or its filter bar in React, to let agents query dashboards over MCP, or to upgrade derive-dashboards.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Derive Dashboards
|
|
7
|
+
|
|
8
|
+
One npm package, `derive-dashboards`, imported by part: `derive-dashboards` (server),
|
|
9
|
+
`derive-dashboards/core` (model, types), `derive-dashboards/react`, `derive-dashboards/recharts`, and
|
|
10
|
+
the `derive-dashboards` CLI. Docs: https://github.com/derive-to/dashboards/tree/main/docs
|
|
11
|
+
(start with `quick-start.md`; `llms.txt` at the repo root indexes every page).
|
|
12
|
+
|
|
13
|
+
## Before you start
|
|
14
|
+
|
|
15
|
+
Check, and say what you found:
|
|
16
|
+
- Postgres (node-postgres `Pool`, or PGlite with a folder) is reachable. Other databases: a custom
|
|
17
|
+
data layer (`docs/custom-data-layer.md`).
|
|
18
|
+
- React 18.3 or 19 for the UI (the filter bar that `add` copies into the app needs React 19). Next.js: the provider and dashboards render from a `"use client"` file.
|
|
19
|
+
- How the app knows who is signed in: the handler needs a `viewer(request)` that returns the
|
|
20
|
+
person (with tenant values when the model has tenant keys) or `null`.
|
|
21
|
+
|
|
22
|
+
## Steps
|
|
23
|
+
|
|
24
|
+
1. Install: `npm install derive-dashboards` and, for the UI, `react recharts`.
|
|
25
|
+
2. Model: `npx derive-dashboards init` proposes `defineModel({...})` from the tables (add
|
|
26
|
+
`--tenant org=org_id` for a multi-tenant app; `--from dbt|cube <path>` imports existing metrics).
|
|
27
|
+
Read what it lists as left out or readable by the whole tenant, and confirm with the user.
|
|
28
|
+
Then `npx derive-dashboards check` must pass.
|
|
29
|
+
3. Server: `deriveDashboards({ model, viewer, access, connections: { main: connection },
|
|
30
|
+
dashboards, secret })` with `nodePostgres(pool)` for `connection` and `store`. Mount
|
|
31
|
+
`dashboards.handler` at `/api/dashboards/*` (one line per framework, like better-auth). Set
|
|
32
|
+
`secret` (`DASHBOARDS_SECRET`) in production. Editing needs the store's tables:
|
|
33
|
+
`npx derive-dashboards generate --orm drizzle --out db/dashboards.ts` (or `--orm sql`), or
|
|
34
|
+
`npx derive-dashboards create-tables` for an app with no schema tool.
|
|
35
|
+
4. A dashboard in code: a document of widgets, each with a stored query over the model, on a
|
|
36
|
+
12-column grid, plus `controls` for filters (`docs/filters.md`). Open it as `default:<key>`.
|
|
37
|
+
5. UI: import `derive-dashboards/react/theme.css`; `createDashboardsClient<typeof dashboards>()`
|
|
38
|
+
(import the server's type only); `<DashboardsProvider client renderers={RECHARTS_RENDERERS}
|
|
39
|
+
viewerKey={user.id}><Dashboard id="default:overview" /></DashboardsProvider>`.
|
|
40
|
+
6. `npx derive-dashboards doctor` prints each piece of the setup as `ok` or `fix` with its one
|
|
41
|
+
step: config, model, connection, store tables, `DASHBOARDS_SECRET`, React peers, shadcn base.
|
|
42
|
+
Fix every `fix` line before looking anywhere else.
|
|
43
|
+
7. Verify in a browser: the dashboard loads as a signed-in user, a filter changes the tiles, and
|
|
44
|
+
a second tenant sees only its own rows.
|
|
45
|
+
|
|
46
|
+
## Filters
|
|
47
|
+
|
|
48
|
+
Every dashboard draws a filter bar from its `controls`; nothing to install. Theme it with CSS
|
|
49
|
+
(`--dd-*` and `--dd-fb-*` variables, `.dd-fb-*` classes; the theme is in a cascade layer so the
|
|
50
|
+
app's CSS wins). Options: wrap `FilterBar` with `pinned`, `icons`, `renderMark`, `formatValue` and
|
|
51
|
+
pass it as `components={{ FilterBar }}`. Words: `messages.filterBar`. To own the code in a shadcn
|
|
52
|
+
app (Base UI or Radix), `npx derive-dashboards add filter-bar`.
|
|
53
|
+
|
|
54
|
+
## Agents
|
|
55
|
+
|
|
56
|
+
Two MCP tools let an agent ask questions and edit as itself: `docs/mcp.md`.
|
|
57
|
+
|
|
58
|
+
## Do not
|
|
59
|
+
|
|
60
|
+
- Do not import server code into the client bundle; import its type only.
|
|
61
|
+
- Do not bypass `viewer` or `access` to make a dashboard load; a refusal names the reason.
|
|
62
|
+
- Do not edit `dd_` tables by hand; use the store, `generate` or `create-tables`.
|
|
63
|
+
- Do not hand-write SQL for tiles; tiles are queries over the model, checked by the server.
|
|
64
|
+
|
|
65
|
+
## Upgrading
|
|
66
|
+
|
|
67
|
+
Read the changelog for the new version. Regenerate the store schema (`generate --orm … --out …`) and apply it with your migration tool, or run `create-tables`, which adds what a newer release needs.
|
|
68
|
+
An owned filter-bar copy: commit, `npx derive-dashboards add filter-bar --overwrite`, then put
|
|
69
|
+
the app's own changes back from `git diff`.
|
package/dist/store/apply.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { sha256 } from "../cache.mjs";
|
|
|
2
2
|
import { HttpError } from "../http.mjs";
|
|
3
3
|
import { writeCommands } from "./commands.mjs";
|
|
4
4
|
import { advanceDraft, draftOf, expectRevision, inTransaction, namedOf, now, openDraft } from "./rows.mjs";
|
|
5
|
-
import { canonical, reduceAll } from "derive-dashboards/core";
|
|
5
|
+
import { canonical, editedDisplayIssues, reduceAll } from "derive-dashboards/core";
|
|
6
6
|
// src/store/apply.ts
|
|
7
7
|
/** Applying edits to a draft: each batch all or nothing, each command once. */
|
|
8
8
|
const bodyOf = ({ commandId: _id, ...body }) => body;
|
|
@@ -68,6 +68,8 @@ function applyDraft(db, a) {
|
|
|
68
68
|
expectRevision(row, a.expectedRevision);
|
|
69
69
|
const outcome = reduceAll(current.doc, fresh.map((c) => bodyOf(c)));
|
|
70
70
|
if (!outcome.ok) throw new HttpError(400, "EDIT_REFUSED", outcome.issue.message, { issues: [outcome.issue] });
|
|
71
|
+
const wrong = editedDisplayIssues(current.doc, outcome.doc);
|
|
72
|
+
if (wrong[0] !== void 0) throw new HttpError(400, "EDIT_REFUSED", wrong[0].message, { issues: wrong });
|
|
71
73
|
const revision = current.revision + 1;
|
|
72
74
|
const at = now();
|
|
73
75
|
const first = Number(row.next_seq);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "derive-dashboards",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Open-source embedded dashboards for your product: the server, the engine, React components, charts and the CLI, in one package.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -87,7 +87,8 @@
|
|
|
87
87
|
"@derive-to/dashboards-core": "0.0.0"
|
|
88
88
|
},
|
|
89
89
|
"peerDependencies": {
|
|
90
|
-
"react": "^19.0.0",
|
|
90
|
+
"react": "^18.3.0 || ^19.0.0",
|
|
91
|
+
"react-dom": "^18.3.0 || ^19.0.0",
|
|
91
92
|
"recharts": "^3.0.0",
|
|
92
93
|
"vitest": "^4.0.0 || ^5.0.0"
|
|
93
94
|
},
|
|
@@ -95,6 +96,9 @@
|
|
|
95
96
|
"react": {
|
|
96
97
|
"optional": true
|
|
97
98
|
},
|
|
99
|
+
"react-dom": {
|
|
100
|
+
"optional": true
|
|
101
|
+
},
|
|
98
102
|
"recharts": {
|
|
99
103
|
"optional": true
|
|
100
104
|
},
|