derive-dashboards 0.1.0 → 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.
- package/README.md +8 -5
- package/dist/adapter/sql-adapter.mjs +4 -4
- package/dist/body.mjs +11 -1
- package/dist/cache.d.mts +1 -1
- package/dist/cli/add.mjs +130 -0
- package/dist/cli/check.mjs +14 -1
- package/dist/cli/cli.mjs +14 -12
- package/dist/cli/config.mjs +12 -1
- package/dist/cli/doctor.mjs +147 -0
- package/dist/cli/import/dbt-schema.mjs +2 -2
- package/dist/cli/import/index.mjs +2 -2
- package/dist/cli/init.mjs +3 -2
- package/dist/cli/json.mjs +21 -0
- package/dist/cli/layers.mjs +33 -0
- package/dist/core/conformance/cases.d.mts +3 -0
- package/dist/core/conformance/cases.mjs +20 -4
- package/dist/core/conformance/check.mjs +6 -3
- package/dist/core/conformance/drill-cases.mjs +120 -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/document/view.d.mts +26 -7
- package/dist/core/document/view.mjs +42 -11
- package/dist/core/engine/filter.mjs +2 -2
- package/dist/core/engine/index.d.mts +14 -2
- package/dist/core/engine/index.mjs +12 -1
- package/dist/core/engine/leaf.mjs +9 -4
- package/dist/core/engine/members.mjs +2 -2
- package/dist/core/engine/postgres.mjs +1 -1
- package/dist/core/engine/record-layout.d.mts +11 -0
- package/dist/core/engine/record-layout.mjs +7 -7
- package/dist/core/engine/records.d.mts +3 -2
- package/dist/core/engine/records.mjs +2 -1
- package/dist/core/engine/segmented.mjs +5 -2
- package/dist/core/engine/sql-records.mjs +2 -1
- package/dist/core/engine/statement.mjs +37 -3
- package/dist/core/engine/types.d.mts +3 -0
- package/dist/core/engine/version.d.mts +1 -1
- package/dist/core/engine/version.mjs +1 -1
- package/dist/core/error/codes.d.mts +33 -1
- package/dist/core/error/codes.mjs +30 -2
- package/dist/core/index.d.mts +13 -7
- package/dist/core/index.mjs +9 -5
- package/dist/core/model/catalog/check.mjs +1 -1
- package/dist/core/model/hints.d.mts +36 -0
- package/dist/core/model/member.d.mts +4 -2
- package/dist/core/model/member.mjs +19 -3
- package/dist/core/model/types.d.mts +21 -19
- package/dist/core/model/validate/drill.mjs +70 -0
- package/dist/core/model/validate/index.mjs +3 -0
- package/dist/core/model/validate/members.mjs +7 -4
- package/dist/core/model/validate/shape.mjs +150 -0
- package/dist/core/model/validate/source.mjs +5 -3
- 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 +7 -1
- package/dist/core/query/drill/compose.d.mts +22 -0
- package/dist/core/query/drill/compose.mjs +85 -0
- package/dist/core/query/drill/fields.d.mts +21 -0
- package/dist/core/query/drill/fields.mjs +91 -0
- package/dist/core/query/drill/internal.d.mts +11 -0
- package/dist/core/query/drill/items.d.mts +29 -0
- package/dist/core/query/drill/items.mjs +98 -0
- package/dist/core/query/drill/line.d.mts +31 -0
- package/dist/core/query/drill/line.mjs +183 -0
- package/dist/core/query/drill/links.d.mts +33 -0
- package/dist/core/query/drill/links.mjs +31 -0
- package/dist/core/query/drill/operand.mjs +7 -0
- package/dist/core/query/drill/scopes.d.mts +22 -0
- package/dist/core/query/drill/scopes.mjs +58 -0
- package/dist/core/query/drill/selection.d.mts +37 -0
- package/dist/core/query/drill/selection.mjs +144 -0
- package/dist/core/query/drill/steps.d.mts +14 -0
- package/dist/core/query/drill/steps.mjs +35 -0
- package/dist/core/query/drill/synthetic-expr.mjs +85 -0
- package/dist/core/query/drill/synthetic.d.mts +22 -0
- package/dist/core/query/drill/synthetic.mjs +165 -0
- package/dist/core/query/drill/time.d.mts +11 -0
- package/dist/core/query/drill/time.mjs +71 -0
- package/dist/core/query/drill/types.d.mts +155 -0
- package/dist/core/query/drill/types.mjs +4 -0
- package/dist/core/query/drill/views.d.mts +62 -0
- package/dist/core/query/drill/views.mjs +257 -0
- 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.d.mts +12 -2
- package/dist/core/query/segments.mjs +5 -2
- package/dist/core/result/columns.mjs +20 -4
- package/dist/core/result/types.d.mts +5 -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 +117 -0
- package/dist/core/schema/answers.d.mts +2109 -128
- package/dist/core/schema/answers.mjs +11 -113
- package/dist/core/schema/commands.d.mts +2 -2
- package/dist/core/schema/document.d.mts +1 -1
- package/dist/core/schema/drill-answers.mjs +179 -0
- package/dist/core/schema/drill.d.mts +260 -0
- package/dist/core/schema/drill.mjs +107 -0
- package/dist/core/schema/index.d.mts +6 -3
- package/dist/core/schema/index.mjs +6 -3
- package/dist/core/schema/picks.d.mts +35 -0
- package/dist/core/schema/picks.mjs +23 -0
- package/dist/core/schema/query.d.mts +82 -1
- package/dist/core/schema/query.mjs +9 -5
- package/dist/core/schema/tools.mjs +1 -1
- package/dist/core/schema/view.d.mts +0 -35
- package/dist/core/schema/view.mjs +19 -24
- package/dist/core/store/schema.d.mts +5 -0
- package/dist/core/store/schema.mjs +10 -0
- package/dist/core/types/api.d.mts +21 -1
- package/dist/core/types/api.mjs +1 -1
- package/dist/core/types/drill-api.d.mts +210 -0
- package/dist/core/types/options.d.mts +21 -0
- package/dist/core/types/viewer.d.mts +5 -0
- package/dist/cursor-seal.d.mts +8 -0
- package/dist/cursor-seal.mjs +98 -0
- package/dist/dashboard-id.mjs +1 -1
- package/dist/dashboards.mjs +4 -0
- package/dist/drill-access.mjs +93 -0
- package/dist/drill-change.mjs +76 -0
- package/dist/drill-crumbs.mjs +113 -0
- package/dist/drill-distinct.mjs +130 -0
- package/dist/drill-explain.mjs +64 -0
- package/dist/drill-identity.mjs +35 -0
- package/dist/drill-item-id.mjs +35 -0
- package/dist/drill-items.mjs +132 -0
- package/dist/drill-open.mjs +169 -0
- package/dist/drill-seal.mjs +170 -0
- package/dist/drill-secret.mjs +14 -0
- package/dist/drill-share.mjs +53 -0
- package/dist/drill-summary.mjs +158 -0
- package/dist/drill-targets.mjs +53 -0
- package/dist/drill-totals.mjs +8 -0
- package/dist/drill-views.mjs +175 -0
- package/dist/drill.mjs +157 -0
- package/dist/editing.mjs +3 -3
- package/dist/endpoints/dashboards.mjs +28 -5
- package/dist/endpoints/drill.mjs +55 -0
- package/dist/endpoints/index.d.mts +245 -4
- package/dist/endpoints/index.mjs +5 -1
- package/dist/endpoints/meta.mjs +10 -1
- package/dist/endpoints/query.mjs +26 -4
- package/dist/execute.d.mts +19 -1
- package/dist/execute.mjs +34 -27
- package/dist/handler.mjs +5 -1
- package/dist/index.d.mts +3 -1
- package/dist/index.mjs +29 -2
- package/dist/llms.txt +22 -0
- package/dist/mcp/drill.mjs +61 -0
- package/dist/mcp/format.mjs +69 -11
- package/dist/mcp/index.d.mts +15 -4
- package/dist/mcp/index.mjs +13 -7
- package/dist/mcp/query.d.mts +7 -2
- package/dist/mcp/query.mjs +40 -6
- package/dist/node-postgres.d.mts +8 -4
- package/dist/node-postgres.mjs +29 -6
- package/dist/owner.mjs +3 -3
- package/dist/postgres-connection.d.mts +8 -2
- package/dist/postgres-connection.mjs +2 -1
- 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 +10 -5
- package/dist/react/Dashboard.mjs +62 -22
- package/dist/react/DashboardList.mjs +52 -24
- package/dist/react/DashboardListRow.mjs +49 -0
- package/dist/react/Discard.mjs +12 -4
- package/dist/react/Editing.mjs +19 -5
- package/dist/react/FilterBar.d.mts +7 -9
- package/dist/react/Grid.mjs +19 -24
- package/dist/react/Header.mjs +10 -1
- package/dist/react/History.mjs +5 -0
- package/dist/react/Parts.mjs +6 -2
- package/dist/react/ReadFailed.mjs +23 -0
- package/dist/react/ReviewPanel.mjs +13 -2
- package/dist/react/RowBody.d.mts +19 -0
- package/dist/react/RowBody.mjs +55 -0
- package/dist/react/Spaces.mjs +15 -1
- package/dist/react/Tile.d.mts +3 -1
- package/dist/react/Tile.mjs +84 -32
- 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/client.d.mts +2 -0
- package/dist/react/client.mjs +1 -0
- package/dist/react/context.d.mts +28 -1
- package/dist/react/context.mjs +25 -4
- package/dist/react/drill/Bars.mjs +179 -0
- package/dist/react/drill/Controls.mjs +173 -0
- package/dist/react/drill/DashboardTile.d.mts +18 -0
- package/dist/react/drill/DashboardTile.mjs +107 -0
- package/dist/react/drill/DrillPanel.d.mts +31 -0
- package/dist/react/drill/DrillPanel.mjs +104 -0
- package/dist/react/drill/DrillTabs.mjs +52 -0
- package/dist/react/drill/Header.mjs +131 -0
- package/dist/react/drill/Items.d.mts +22 -0
- package/dist/react/drill/Items.mjs +164 -0
- package/dist/react/drill/ItemsFoot.mjs +60 -0
- package/dist/react/drill/Notes.mjs +58 -0
- package/dist/react/drill/PanelBody.mjs +74 -0
- package/dist/react/drill/PanelStates.mjs +37 -0
- package/dist/react/drill/board.d.mts +18 -0
- package/dist/react/drill/board.mjs +91 -0
- package/dist/react/drill/counts.mjs +14 -0
- package/dist/react/drill/host.mjs +26 -0
- package/dist/react/drill/item-parts.d.mts +5 -0
- package/dist/react/drill/item-parts.mjs +76 -0
- package/dist/react/drill/item-type.d.mts +26 -0
- package/dist/react/drill/item-type.mjs +7 -0
- package/dist/react/drill/lazy.d.mts +6 -0
- package/dist/react/drill/lazy.mjs +17 -0
- package/dist/react/drill/pages.mjs +48 -0
- package/dist/react/drill/panel-focus.mjs +51 -0
- package/dist/react/drill/props.d.mts +25 -0
- package/dist/react/drill/props.mjs +72 -0
- package/dist/react/drill/state.d.mts +18 -0
- package/dist/react/drill/state.mjs +68 -0
- package/dist/react/drill/useDrill.d.mts +46 -0
- package/dist/react/drill/useDrill.mjs +194 -0
- package/dist/react/drill/words.d.mts +101 -0
- package/dist/react/drill/words.mjs +79 -0
- package/dist/react/english.mjs +29 -6
- 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.d.mts +3 -3
- package/dist/react/format.mjs +45 -16
- package/dist/react/index.d.mts +34 -5
- package/dist/react/index.mjs +15 -4
- package/dist/react/messages.d.mts +47 -2
- package/dist/react/place.mjs +7 -3
- package/dist/react/renderers/index.d.mts +10 -2
- package/dist/react/renderers/index.mjs +116 -14
- package/dist/react/renderers/list.d.mts +22 -0
- package/dist/react/renderers/list.mjs +118 -0
- package/dist/react/review-words.mjs +1 -1
- package/dist/react/review.mjs +2 -2
- package/dist/react/scope.mjs +2 -7
- package/dist/react/series.d.mts +54 -0
- package/dist/react/series.mjs +191 -0
- package/dist/react/theme.css +2734 -1296
- package/dist/react/theme.css.d.ts +1 -0
- package/dist/react/tile-store.mjs +15 -3
- package/dist/react/useBoard.d.mts +7 -0
- package/dist/react/useBoard.mjs +4 -1
- package/dist/react/useDashboard.d.mts +4 -0
- package/dist/react/useDashboard.mjs +8 -1
- package/dist/react/useTiles.mjs +50 -15
- package/dist/react/view-grid.mjs +16 -0
- package/dist/react/view.d.mts +9 -0
- package/dist/react/view.mjs +22 -10
- package/dist/reading.d.mts +2 -0
- package/dist/reading.mjs +9 -1
- 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 +140 -0
- package/dist/recharts/charts.d.mts +4 -6
- package/dist/recharts/charts.mjs +2 -114
- package/dist/recharts/index.d.mts +5 -3
- package/dist/recharts/index.mjs +6 -3
- package/dist/recharts/line.d.mts +2 -0
- package/dist/recharts/line.mjs +111 -0
- package/dist/recharts/marks.mjs +112 -0
- package/dist/recharts/parts.mjs +88 -0
- package/dist/recharts/series.d.mts +2 -22
- package/dist/recharts/series.mjs +1 -64
- package/dist/recharts/table.mjs +71 -0
- package/dist/records-read.mjs +44 -0
- package/dist/registry/filter-bar.json +26 -0
- package/dist/skills/derive-dashboards/SKILL.md +69 -0
- package/dist/store/apply.mjs +5 -5
- package/dist/store/commands.mjs +4 -4
- package/dist/store/discard.mjs +7 -7
- package/dist/store/drafts.mjs +5 -5
- package/dist/store/editor.mjs +1 -1
- package/dist/store/publish.mjs +12 -12
- package/dist/store/reader.mjs +41 -22
- package/dist/store/review.mjs +16 -16
- package/dist/store/rows.mjs +20 -20
- package/dist/store/undo.mjs +3 -3
- package/dist/store/unpublished.mjs +4 -4
- package/dist/store/versions.mjs +14 -14
- package/dist/testing/adapter-suite.mjs +4 -4
- package/dist/tiles.mjs +37 -3
- package/package.json +12 -10
- package/dist/react/FilterBar.mjs +0 -119
- package/dist/react/Records.d.mts +0 -12
- package/dist/react/Records.mjs +0 -97
- package/src/adapter/sql-adapter.ts +0 -95
- package/src/adapter/statement.ts +0 -185
- package/src/authorize.ts +0 -73
- package/src/body.ts +0 -169
- package/src/bounded.ts +0 -43
- package/src/cache.ts +0 -192
- package/src/checks.ts +0 -170
- package/src/custom-source.ts +0 -59
- package/src/dashboard-id.ts +0 -36
- package/src/dashboards.ts +0 -132
- package/src/editing.ts +0 -148
- package/src/endpoints/access.ts +0 -41
- package/src/endpoints/dashboards.ts +0 -92
- package/src/endpoints/drafts.ts +0 -138
- package/src/endpoints/index.ts +0 -21
- package/src/endpoints/meta.ts +0 -137
- package/src/endpoints/query.ts +0 -86
- package/src/endpoints/read.ts +0 -92
- package/src/endpoints/review.ts +0 -89
- package/src/endpoints/values.ts +0 -62
- package/src/endpoints/versions.ts +0 -104
- package/src/execute.ts +0 -207
- package/src/handler.ts +0 -232
- package/src/http.ts +0 -66
- package/src/index.ts +0 -163
- package/src/introspect.ts +0 -205
- package/src/mcp/api.ts +0 -59
- package/src/mcp/format.ts +0 -67
- package/src/mcp/index.ts +0 -151
- package/src/mcp/query.ts +0 -153
- package/src/mcp/resolve.ts +0 -114
- package/src/node-postgres.ts +0 -81
- package/src/owner.ts +0 -36
- package/src/postgres-connection.ts +0 -143
- package/src/postgres-store.ts +0 -82
- package/src/principal.ts +0 -39
- package/src/reading.ts +0 -56
- package/src/request-rows.ts +0 -23
- package/src/request.ts +0 -22
- package/src/storage.ts +0 -63
- package/src/store/apply.ts +0 -129
- package/src/store/commands.ts +0 -75
- package/src/store/conflicts.ts +0 -98
- package/src/store/create-tables.ts +0 -104
- package/src/store/discard.ts +0 -42
- package/src/store/drafts.ts +0 -133
- package/src/store/editor.ts +0 -28
- package/src/store/log.ts +0 -28
- package/src/store/publish.ts +0 -199
- package/src/store/publisher.ts +0 -21
- package/src/store/reader.ts +0 -139
- package/src/store/review.ts +0 -239
- package/src/store/rows.ts +0 -250
- package/src/store/tenant.ts +0 -28
- package/src/store/undo.ts +0 -56
- package/src/store/unpublished.ts +0 -33
- package/src/store/versions.ts +0 -106
- package/src/store/writer.ts +0 -19
- package/src/testing/adapter-suite.ts +0 -239
- package/src/testing/index.ts +0 -2
- package/src/tile-doc.ts +0 -46
- package/src/tiles.ts +0 -120
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import "./logic.mjs";
|
|
2
|
+
import { ComponentType, ReactNode } from "react";
|
|
3
|
+
import { Control, DateRange, Picks, Primitive } from "derive-dashboards/core";
|
|
4
|
+
/** An icon: any component taking a className, such as one from lucide-react. */
|
|
5
|
+
export type FilterIcon = ComponentType<{
|
|
6
|
+
className?: string;
|
|
7
|
+
"aria-hidden"?: boolean;
|
|
8
|
+
}>;
|
|
9
|
+
/** What a host may add to the filter bar. Everything else comes from the model. */
|
|
10
|
+
export type FilterBarOptions = {
|
|
11
|
+
/** An icon per control id. None by default. */
|
|
12
|
+
icons?: Readonly<Record<string, FilterIcon>>;
|
|
13
|
+
/**
|
|
14
|
+
* Drawn before a value in lists and chips, such as a person's avatar. Return undefined to keep
|
|
15
|
+
* the default: a dot in the value's tone from the model.
|
|
16
|
+
*/
|
|
17
|
+
renderMark?: (control: Control, value: Primitive) => ReactNode;
|
|
18
|
+
/** A value as words; return undefined to keep the default ("None", yes or no, a capital first). */
|
|
19
|
+
formatValue?: (control: Control, value: Primitive) => string | undefined;
|
|
20
|
+
/** How many unset filters show as chips before the rest wait in the Filter menu. Default 4. */
|
|
21
|
+
pinned?: number;
|
|
22
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { useDashboards } from "../context.mjs";
|
|
2
|
+
import { rangeLabel } from "./days.mjs";
|
|
3
|
+
import { chosenOf, defaultFormat, isNumber, listText, pickOpWord } from "./logic.mjs";
|
|
4
|
+
import { numberText } from "./numbers.mjs";
|
|
5
|
+
import { ENGLISH_FILTERS } from "./words.mjs";
|
|
6
|
+
import { createContext, useContext } from "react";
|
|
7
|
+
import { isDateRange, isValuePick } from "derive-dashboards/core";
|
|
8
|
+
// src/filters/context.ts
|
|
9
|
+
const BarContext = createContext({
|
|
10
|
+
fields: {},
|
|
11
|
+
options: {},
|
|
12
|
+
picks: {},
|
|
13
|
+
setPick: () => {}
|
|
14
|
+
});
|
|
15
|
+
function useBar() {
|
|
16
|
+
return useContext(BarContext);
|
|
17
|
+
}
|
|
18
|
+
const valuePickOf = (picks, id) => {
|
|
19
|
+
const pick = picks[id];
|
|
20
|
+
return isValuePick(pick) ? pick : void 0;
|
|
21
|
+
};
|
|
22
|
+
const rangeOf = (picks, id) => {
|
|
23
|
+
const pick = picks[id];
|
|
24
|
+
return pick !== void 0 && isDateRange(pick) ? pick : void 0;
|
|
25
|
+
};
|
|
26
|
+
/** The bar's words: English, with any the host passed as `messages.filterBar`. */
|
|
27
|
+
function useWords() {
|
|
28
|
+
const { messages } = useDashboards();
|
|
29
|
+
const own = messages.filterBar;
|
|
30
|
+
return own === void 0 ? ENGLISH_FILTERS : {
|
|
31
|
+
...ENGLISH_FILTERS,
|
|
32
|
+
...own
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** The locale dates are written in: the provider's, else the viewer's. */
|
|
36
|
+
function useLocale() {
|
|
37
|
+
return useDashboards().locale;
|
|
38
|
+
}
|
|
39
|
+
/** A value as words: the host's `formatValue`, else None, Yes or No in the bar's words, else the default. */
|
|
40
|
+
function useFormat() {
|
|
41
|
+
const { options } = useBar();
|
|
42
|
+
const words = useWords();
|
|
43
|
+
const own = options.formatValue;
|
|
44
|
+
const base = (control, value) => {
|
|
45
|
+
if (value === null) return words.none;
|
|
46
|
+
if (typeof value === "boolean") return value ? words.yes : words.no;
|
|
47
|
+
return defaultFormat(control, value);
|
|
48
|
+
};
|
|
49
|
+
return own === void 0 ? base : (control, value) => own(control, value) ?? base(control, value);
|
|
50
|
+
}
|
|
51
|
+
/** A date range as words: a preset in the host's `messages.range`, days and times as dates. */
|
|
52
|
+
function useRangeText() {
|
|
53
|
+
const { messages, locale } = useDashboards();
|
|
54
|
+
return (range) => "from" in range ? rangeLabel(range, locale) : messages.range(range);
|
|
55
|
+
}
|
|
56
|
+
/** A pick's operator in the host's words; `negative` names the other choice. */
|
|
57
|
+
function useOpText() {
|
|
58
|
+
const words = useWords();
|
|
59
|
+
return (pick, negative) => words[pickOpWord(pick, negative)];
|
|
60
|
+
}
|
|
61
|
+
/** What a control's pick says, in words ("is any of Review, Done", "2–8 h", "Last 7 days"). */
|
|
62
|
+
function useSummary() {
|
|
63
|
+
const { fields, picks } = useBar();
|
|
64
|
+
const format = useFormat();
|
|
65
|
+
const rangeText = useRangeText();
|
|
66
|
+
const opText = useOpText();
|
|
67
|
+
return (control) => {
|
|
68
|
+
if (control.kind === "date_range") {
|
|
69
|
+
const r = rangeOf(picks, control.id);
|
|
70
|
+
return r === void 0 ? void 0 : rangeText(r);
|
|
71
|
+
}
|
|
72
|
+
const pick = control.kind === "filter" ? valuePickOf(picks, control.id) : void 0;
|
|
73
|
+
if (pick === void 0 || control.kind !== "filter") return;
|
|
74
|
+
if (isNumber(control, fields)) return numberText(pick, fields[control.id]?.unit ?? "");
|
|
75
|
+
return `${opText(pick)} ${listText(control, chosenOf(pick), format)}`;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export { BarContext, rangeOf, useBar, useFormat, useLocale, useOpText, useRangeText, useSummary, useWords, valuePickOf };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Calendar } from "./calendar.mjs";
|
|
2
|
+
import { useBar, useRangeText, useWords } from "./context.mjs";
|
|
3
|
+
import { daysLabel, daysPick, pickedDays } from "./days.mjs";
|
|
4
|
+
import { useState } from "react";
|
|
5
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
// src/filters/dates.tsx
|
|
7
|
+
/** The date control's presets, then "each widget's own". */
|
|
8
|
+
function PresetList({ control, pick, onPick, extra }) {
|
|
9
|
+
const words = useWords();
|
|
10
|
+
const rangeText = useRangeText();
|
|
11
|
+
const now = pick === void 0 ? void 0 : JSON.stringify(pick);
|
|
12
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
13
|
+
className: "dd-fb-presets dd-fb-presets-dates",
|
|
14
|
+
children: [
|
|
15
|
+
control.presets.map((p) => /* @__PURE__ */ jsx("button", {
|
|
16
|
+
type: "button",
|
|
17
|
+
className: "dd-fb-preset",
|
|
18
|
+
"aria-pressed": now === JSON.stringify(p),
|
|
19
|
+
onClick: () => {
|
|
20
|
+
onPick(p);
|
|
21
|
+
},
|
|
22
|
+
children: rangeText(p)
|
|
23
|
+
}, JSON.stringify(p))),
|
|
24
|
+
/* @__PURE__ */ jsx("button", {
|
|
25
|
+
type: "button",
|
|
26
|
+
className: "dd-fb-preset dd-fb-quiet",
|
|
27
|
+
"aria-pressed": pick === void 0,
|
|
28
|
+
onClick: () => {
|
|
29
|
+
onPick();
|
|
30
|
+
},
|
|
31
|
+
children: words.widgetDates
|
|
32
|
+
}),
|
|
33
|
+
extra
|
|
34
|
+
]
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/** The days chosen so far and the zone they are read in, with Apply. */
|
|
38
|
+
function DaysFoot({ days, pick, onApply }) {
|
|
39
|
+
const words = useWords();
|
|
40
|
+
const rangeText = useRangeText();
|
|
41
|
+
const { timezone } = useBar();
|
|
42
|
+
const zone = timezone === void 0 ? words.dashboardZone : timezone.replaceAll("_", " ");
|
|
43
|
+
const { from, to } = days;
|
|
44
|
+
const chosen = from !== void 0 && to !== void 0 ? daysLabel(from, to) : pick === void 0 ? words.pickDays : rangeText(pick);
|
|
45
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
46
|
+
className: "dd-fb-foot",
|
|
47
|
+
children: [/* @__PURE__ */ jsxs("span", {
|
|
48
|
+
role: "status",
|
|
49
|
+
className: "dd-fb-stack",
|
|
50
|
+
children: [/* @__PURE__ */ jsx("span", { children: chosen }), /* @__PURE__ */ jsx("span", {
|
|
51
|
+
className: "dd-fb-quiet",
|
|
52
|
+
children: words.daysIn(zone)
|
|
53
|
+
})]
|
|
54
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
55
|
+
type: "button",
|
|
56
|
+
className: "dd-fb-primary",
|
|
57
|
+
disabled: from === void 0 || to === void 0,
|
|
58
|
+
onClick: () => {
|
|
59
|
+
if (from !== void 0 && to !== void 0) onApply(daysPick(from, to));
|
|
60
|
+
},
|
|
61
|
+
children: words.apply
|
|
62
|
+
})]
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/** Presets beside a calendar; the chosen days and the zone they are read in, below. */
|
|
66
|
+
function DatePicker({ control, pick, onPick, narrow }) {
|
|
67
|
+
const [initial] = useState(() => pickedDays(pick));
|
|
68
|
+
const [days, setDays] = useState(initial ?? {});
|
|
69
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
70
|
+
className: "dd-fb-dates",
|
|
71
|
+
children: [/* @__PURE__ */ jsx(PresetList, {
|
|
72
|
+
control,
|
|
73
|
+
pick,
|
|
74
|
+
onPick
|
|
75
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
76
|
+
className: "dd-fb-dates-custom",
|
|
77
|
+
children: [/* @__PURE__ */ jsx(Calendar, {
|
|
78
|
+
initial,
|
|
79
|
+
narrow,
|
|
80
|
+
onChange: setDays
|
|
81
|
+
}), /* @__PURE__ */ jsx(DaysFoot, {
|
|
82
|
+
days,
|
|
83
|
+
pick,
|
|
84
|
+
onApply: onPick
|
|
85
|
+
})]
|
|
86
|
+
})]
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
export { DatePicker, PresetList };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { RelativeRange as RelativeRange$1 } from "./logic.mjs";
|
|
2
|
+
import { DateRange } from "derive-dashboards/core";
|
|
3
|
+
/** A date format for a locale (the viewer's when none), made once. */
|
|
4
|
+
export declare function dateFormat(locale: string | undefined, options: Intl.DateTimeFormatOptions): Intl.DateTimeFormat;
|
|
5
|
+
export declare const DAY_ONLY: RegExp;
|
|
6
|
+
/** A local date as a plain day, "2026-10-01". */
|
|
7
|
+
export declare const isoDay: (d: Date) => string;
|
|
8
|
+
/** A plain day ("2026-10-01") as a local date at noon, so no zone can move it. */
|
|
9
|
+
export declare const fromIsoDay: (s: string) => Date;
|
|
10
|
+
export declare const addDays: (d: Date, n: number) => Date;
|
|
11
|
+
export declare const sameDay: (a: Date, b: Date) => boolean;
|
|
12
|
+
/** Monday is 0, Sunday 6. */
|
|
13
|
+
export declare const weekday: (d: Date) => number;
|
|
14
|
+
export declare const monthStart: (d: Date, by?: number) => Date;
|
|
15
|
+
export declare const monthEnd: (d: Date) => Date;
|
|
16
|
+
/** A relative range in words: "This month" runs to now, "Last month" is the whole one before. */
|
|
17
|
+
export declare function relativeLabel(r: RelativeRange$1): string;
|
|
18
|
+
/** An explicit range of plain days, the excluded end shown as the last day included. */
|
|
19
|
+
export declare function explicitDays(r: Extract<DateRange, {
|
|
20
|
+
from: string;
|
|
21
|
+
}>): {
|
|
22
|
+
first: Date;
|
|
23
|
+
last: Date;
|
|
24
|
+
};
|
|
25
|
+
/** The days of a pick that a calendar can show: plain days only, else none. */
|
|
26
|
+
export declare function pickedDays(r: DateRange | undefined): {
|
|
27
|
+
from: Date;
|
|
28
|
+
to: Date;
|
|
29
|
+
} | undefined;
|
|
30
|
+
export declare const daysLabel: (first: Date, last: Date, locale?: string) => string;
|
|
31
|
+
/** A range in words. Plain days read as days; times with an offset are instants, in the viewer's clock. */
|
|
32
|
+
export declare function rangeLabel(r: DateRange, locale?: string): string;
|
|
33
|
+
/** Today as a plain day in a time zone (the viewer's when none), at noon so no zone moves it. */
|
|
34
|
+
export declare function todayIn(zone: string | undefined, now?: Date): Date;
|
|
35
|
+
/** The days a calendar pick sends: plain days, the end excluded, read by the server in the dashboard's zone. */
|
|
36
|
+
export declare const daysPick: (first: Date, last: Date) => DateRange;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// src/filters/days.ts
|
|
2
|
+
const formats = /* @__PURE__ */ new Map();
|
|
3
|
+
/** A date format for a locale (the viewer's when none), made once. */
|
|
4
|
+
function dateFormat(locale, options) {
|
|
5
|
+
const key = `${locale ?? ""}|${JSON.stringify(options)}`;
|
|
6
|
+
let f = formats.get(key);
|
|
7
|
+
if (f === void 0) {
|
|
8
|
+
f = new Intl.DateTimeFormat(locale, options);
|
|
9
|
+
formats.set(key, f);
|
|
10
|
+
}
|
|
11
|
+
return f;
|
|
12
|
+
}
|
|
13
|
+
const DAY_ONLY = /^\d{4}-\d{2}-\d{2}$/u;
|
|
14
|
+
/** A local date as a plain day, "2026-10-01". */
|
|
15
|
+
const isoDay = (d) => `${String(d.getFullYear())}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
|
16
|
+
/** A plain day ("2026-10-01") as a local date at noon, so no zone can move it. */
|
|
17
|
+
const fromIsoDay = (s) => {
|
|
18
|
+
const [y, m, d] = s.slice(0, 10).split("-").map(Number);
|
|
19
|
+
return new Date(y ?? 1970, (m ?? 1) - 1, d ?? 1, 12);
|
|
20
|
+
};
|
|
21
|
+
const addDays = (d, n) => new Date(d.getFullYear(), d.getMonth(), d.getDate() + n, 12);
|
|
22
|
+
const sameDay = (a, b) => isoDay(a) === isoDay(b);
|
|
23
|
+
/** Monday is 0, Sunday 6. */
|
|
24
|
+
const weekday = (d) => (d.getDay() + 6) % 7;
|
|
25
|
+
const monthStart = (d, by = 0) => new Date(d.getFullYear(), d.getMonth() + by, 1, 12);
|
|
26
|
+
const monthEnd = (d) => new Date(d.getFullYear(), d.getMonth() + 1, 0, 12);
|
|
27
|
+
/** A relative range in words: "This month" runs to now, "Last month" is the whole one before. */
|
|
28
|
+
function relativeLabel(r) {
|
|
29
|
+
const units = `${String(r.last)} ${r.unit}s`;
|
|
30
|
+
if (r.includeCurrent) return r.last === 1 ? r.unit === "day" ? "Today" : `This ${r.unit}` : `Last ${units}`;
|
|
31
|
+
if (r.last === 1) return r.unit === "day" ? "Yesterday" : `Last ${r.unit}`;
|
|
32
|
+
return `Previous ${units}`;
|
|
33
|
+
}
|
|
34
|
+
/** An explicit range of plain days, the excluded end shown as the last day included. */
|
|
35
|
+
function explicitDays(r) {
|
|
36
|
+
return {
|
|
37
|
+
first: fromIsoDay(r.from),
|
|
38
|
+
last: addDays(fromIsoDay(r.to), -1)
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** The days of a pick that a calendar can show: plain days only, else none. */
|
|
42
|
+
function pickedDays(r) {
|
|
43
|
+
if (r === void 0 || !("from" in r) || !DAY_ONLY.test(r.from) || !DAY_ONLY.test(r.to)) return void 0;
|
|
44
|
+
const { first, last } = explicitDays(r);
|
|
45
|
+
return {
|
|
46
|
+
from: first,
|
|
47
|
+
to: last
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const daysLabel = (first, last, locale) => `${dateFormat(locale, {
|
|
51
|
+
month: "short",
|
|
52
|
+
day: "numeric"
|
|
53
|
+
}).format(first)} – ${dateFormat(locale, {
|
|
54
|
+
month: "short",
|
|
55
|
+
day: "numeric",
|
|
56
|
+
year: "numeric"
|
|
57
|
+
}).format(last)}`;
|
|
58
|
+
/** A range in words. Plain days read as days; times with an offset are instants, in the viewer's clock. */
|
|
59
|
+
function rangeLabel(r, locale) {
|
|
60
|
+
if (!("from" in r)) return relativeLabel(r);
|
|
61
|
+
const days = pickedDays(r);
|
|
62
|
+
if (days !== void 0) return daysLabel(days.from, days.to, locale);
|
|
63
|
+
const instant = dateFormat(locale, {
|
|
64
|
+
dateStyle: "medium",
|
|
65
|
+
timeStyle: "short"
|
|
66
|
+
});
|
|
67
|
+
return `${instant.format(new Date(r.from))} – ${instant.format(new Date(r.to))}`;
|
|
68
|
+
}
|
|
69
|
+
/** Today as a plain day in a time zone (the viewer's when none), at noon so no zone moves it. */
|
|
70
|
+
function todayIn(zone, now = /* @__PURE__ */ new Date()) {
|
|
71
|
+
try {
|
|
72
|
+
return fromIsoDay(dateFormat("en-CA", {
|
|
73
|
+
timeZone: zone,
|
|
74
|
+
year: "numeric",
|
|
75
|
+
month: "2-digit",
|
|
76
|
+
day: "2-digit"
|
|
77
|
+
}).format(now));
|
|
78
|
+
} catch {
|
|
79
|
+
return fromIsoDay(isoDay(now));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** The days a calendar pick sends: plain days, the end excluded, read by the server in the dashboard's zone. */
|
|
83
|
+
const daysPick = (first, last) => ({
|
|
84
|
+
from: isoDay(first),
|
|
85
|
+
to: isoDay(addDays(last, 1))
|
|
86
|
+
});
|
|
87
|
+
export { DAY_ONLY, addDays, dateFormat, daysLabel, daysPick, explicitDays, fromIsoDay, isoDay, monthEnd, monthStart, pickedDays, rangeLabel, relativeLabel, sameDay, todayIn, weekday };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { rangeOf, useBar, useWords, valuePickOf } from "./context.mjs";
|
|
2
|
+
import { PresetList } from "./dates.mjs";
|
|
3
|
+
import { isNumber, toggled } from "./logic.mjs";
|
|
4
|
+
import { NumberPicker } from "./number.mjs";
|
|
5
|
+
import { ValueList } from "./values.mjs";
|
|
6
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
// src/filters/field-step.tsx
|
|
8
|
+
function Back({ label, steps }) {
|
|
9
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
10
|
+
type: "button",
|
|
11
|
+
className: "dd-fb-back",
|
|
12
|
+
onClick: () => {
|
|
13
|
+
steps.go(null);
|
|
14
|
+
},
|
|
15
|
+
children: [
|
|
16
|
+
/* @__PURE__ */ jsx("span", {
|
|
17
|
+
"aria-hidden": true,
|
|
18
|
+
children: "←"
|
|
19
|
+
}),
|
|
20
|
+
" ",
|
|
21
|
+
label
|
|
22
|
+
]
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/** "Custom range…": closes the menu and opens the date chip, where the calendar is. */
|
|
26
|
+
function CustomRange({ id, steps }) {
|
|
27
|
+
const words = useWords();
|
|
28
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
29
|
+
type: "button",
|
|
30
|
+
className: "dd-fb-preset",
|
|
31
|
+
onClick: () => {
|
|
32
|
+
steps.close();
|
|
33
|
+
steps.openChip(id);
|
|
34
|
+
},
|
|
35
|
+
children: [words.customRange, "…"]
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/** The menu's second step: one field's picker, with a way back. */
|
|
39
|
+
function FieldStep({ field, steps }) {
|
|
40
|
+
const { fields, picks, setPick } = useBar();
|
|
41
|
+
const back = /* @__PURE__ */ jsx(Back, {
|
|
42
|
+
label: field.label,
|
|
43
|
+
steps
|
|
44
|
+
});
|
|
45
|
+
const done = (pick) => {
|
|
46
|
+
setPick(field.id, pick);
|
|
47
|
+
steps.close();
|
|
48
|
+
};
|
|
49
|
+
if (field.kind === "date_range") return /* @__PURE__ */ jsxs(Fragment, { children: [back, /* @__PURE__ */ jsx(PresetList, {
|
|
50
|
+
control: field,
|
|
51
|
+
pick: rangeOf(picks, field.id),
|
|
52
|
+
onPick: done,
|
|
53
|
+
extra: /* @__PURE__ */ jsx(CustomRange, {
|
|
54
|
+
id: field.id,
|
|
55
|
+
steps
|
|
56
|
+
})
|
|
57
|
+
})] });
|
|
58
|
+
if (field.kind !== "filter") return back;
|
|
59
|
+
const pick = valuePickOf(picks, field.id);
|
|
60
|
+
if (isNumber(field, fields)) return /* @__PURE__ */ jsxs(Fragment, { children: [back, /* @__PURE__ */ jsx(NumberPicker, {
|
|
61
|
+
control: field,
|
|
62
|
+
pick,
|
|
63
|
+
onPick: done
|
|
64
|
+
})] });
|
|
65
|
+
return /* @__PURE__ */ jsx(ValueList, {
|
|
66
|
+
control: field,
|
|
67
|
+
pick,
|
|
68
|
+
before: back,
|
|
69
|
+
onBack: () => {
|
|
70
|
+
steps.go(null);
|
|
71
|
+
},
|
|
72
|
+
onToggle: (v) => {
|
|
73
|
+
setPick(field.id, toggled(field, pick, v));
|
|
74
|
+
if (!field.multi) steps.close();
|
|
75
|
+
},
|
|
76
|
+
onClear: () => {
|
|
77
|
+
setPick(field.id, void 0);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
export { FieldStep };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Control, MetaMember, MetaResponse, Picks, Primitive } from "derive-dashboards/core";
|
|
2
|
+
export type FilterBarProps = {
|
|
3
|
+
controls: readonly Control[];
|
|
4
|
+
picks: Picks;
|
|
5
|
+
onChange: (picks: Picks) => void;
|
|
6
|
+
/** The dashboard's own time zone when it sets one; calendar days are read in it. */
|
|
7
|
+
timezone?: string | undefined;
|
|
8
|
+
};
|
|
9
|
+
/** A control's values as a bar lists them: what is known, whether a read is under way or failed. */
|
|
10
|
+
export type ValuesState = {
|
|
11
|
+
values: readonly Primitive[];
|
|
12
|
+
loading: boolean;
|
|
13
|
+
/** The last read failed (earlier values may still be shown); `retry` asks again. */
|
|
14
|
+
failed: boolean;
|
|
15
|
+
retry: () => void;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* A filter control's values: its own list, or for "lookup" the values its member has that this
|
|
19
|
+
* viewer can see, narrowed by `search` on the server. For any filter bar, the built-in one or yours.
|
|
20
|
+
*/
|
|
21
|
+
export declare function useControlValues(control: Extract<Control, {
|
|
22
|
+
kind: "filter";
|
|
23
|
+
}>, options?: {
|
|
24
|
+
search?: string;
|
|
25
|
+
limit?: number;
|
|
26
|
+
}): ValuesState;
|
|
27
|
+
/** A member in the viewer's meta: its own source's dimension, or one a join reaches. */
|
|
28
|
+
export declare function fieldOf(meta: MetaResponse | undefined, member: string): MetaMember | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* What the model says about each filter control's field: its type (a text list, a number, a yes
|
|
31
|
+
* or no), value tones, number buckets and unit. A filter bar draws the right picker from it, with
|
|
32
|
+
* nothing configured by the host. Undefined while loading, or for a viewer the meta is not open to.
|
|
33
|
+
*/
|
|
34
|
+
export declare function useFilterFields(controls: readonly Control[]): Readonly<Record<string, MetaMember | undefined>>;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { useDashboards } from "../context.mjs";
|
|
2
|
+
import { ScopeContext } from "../scope.mjs";
|
|
3
|
+
import { useContext, useEffect, useMemo, useState } from "react";
|
|
4
|
+
import useSWR from "swr";
|
|
5
|
+
// src/filters/hooks.ts
|
|
6
|
+
const LOOKUP_LIMIT = 24;
|
|
7
|
+
const noop = () => {};
|
|
8
|
+
const NONE = [];
|
|
9
|
+
/** A control's own list, searched; the same array while nothing changed, for effects. */
|
|
10
|
+
function useOwnValues(own, search) {
|
|
11
|
+
return useMemo(() => {
|
|
12
|
+
if (typeof own !== "object") return null;
|
|
13
|
+
const needle = search.toLowerCase();
|
|
14
|
+
return needle === "" ? own : own.filter((v) => String(v).toLowerCase().includes(needle));
|
|
15
|
+
}, [own, search]);
|
|
16
|
+
}
|
|
17
|
+
/** A lookup control's values from the server, searched there; none for a control's own list. */
|
|
18
|
+
function useLookup(control, search, limit) {
|
|
19
|
+
const { client } = useDashboards();
|
|
20
|
+
const scope = useContext(ScopeContext);
|
|
21
|
+
const draft = scope?.draft === true;
|
|
22
|
+
const key = typeof control.values === "object" || scope === null ? null : [
|
|
23
|
+
"dd-values",
|
|
24
|
+
scope.dashboardId,
|
|
25
|
+
control.id,
|
|
26
|
+
draft,
|
|
27
|
+
search,
|
|
28
|
+
limit
|
|
29
|
+
];
|
|
30
|
+
return useSWR(key, () => client.controlValues({
|
|
31
|
+
dashboardId: scope?.dashboardId ?? "",
|
|
32
|
+
controlId: control.id,
|
|
33
|
+
limit,
|
|
34
|
+
...search === "" ? {} : { search },
|
|
35
|
+
...draft ? { draft: true } : {}
|
|
36
|
+
}), { keepPreviousData: true });
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A filter control's values: its own list, or for "lookup" the values its member has that this
|
|
40
|
+
* viewer can see, narrowed by `search` on the server. For any filter bar, the built-in one or yours.
|
|
41
|
+
*/
|
|
42
|
+
function useControlValues(control, options = {}) {
|
|
43
|
+
const search = options.search ?? "";
|
|
44
|
+
const fetched = useLookup(control, search, options.limit ?? LOOKUP_LIMIT);
|
|
45
|
+
const ownValues = useOwnValues(control.values, search);
|
|
46
|
+
if (ownValues !== null) return {
|
|
47
|
+
values: ownValues,
|
|
48
|
+
loading: false,
|
|
49
|
+
failed: false,
|
|
50
|
+
retry: noop
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
values: fetched.data?.values ?? NONE,
|
|
54
|
+
loading: fetched.isLoading,
|
|
55
|
+
failed: fetched.error !== void 0,
|
|
56
|
+
retry: () => {
|
|
57
|
+
fetched.mutate();
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/** A member in the viewer's meta: its own source's dimension, or one a join reaches. */
|
|
62
|
+
function fieldOf(meta, member) {
|
|
63
|
+
const [sourceName, a, b] = member.split(".");
|
|
64
|
+
const source = meta?.sources.find((s) => s.name === sourceName);
|
|
65
|
+
if (source === void 0 || a === void 0) return void 0;
|
|
66
|
+
if (b === void 0) return source.dimensions.find((d) => d.id === member);
|
|
67
|
+
const join = source.joins.find((j) => j.name === a);
|
|
68
|
+
const target = meta?.sources.find((s) => s.name === join?.source);
|
|
69
|
+
return target?.dimensions.find((d) => d.id === `${target.name}.${b}`);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* What the model says about each filter control's field: its type (a text list, a number, a yes
|
|
73
|
+
* or no), value tones, number buckets and unit. A filter bar draws the right picker from it, with
|
|
74
|
+
* nothing configured by the host. Undefined while loading, or for a viewer the meta is not open to.
|
|
75
|
+
*/
|
|
76
|
+
function useFilterFields(controls) {
|
|
77
|
+
const { client } = useDashboards();
|
|
78
|
+
const { data } = useSWR(["dd-meta"], () => client.meta());
|
|
79
|
+
return Object.fromEntries(controls.map((c) => [c.id, c.kind === "filter" ? fieldOf(data, c.member) : void 0]));
|
|
80
|
+
}
|
|
81
|
+
/** A value that settles: it follows `value` once it has not changed for `ms`. */
|
|
82
|
+
function useDebounced(value, ms) {
|
|
83
|
+
const [out, setOut] = useState(value);
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
const t = setTimeout(() => {
|
|
86
|
+
setOut(value);
|
|
87
|
+
}, ms);
|
|
88
|
+
return () => {
|
|
89
|
+
clearTimeout(t);
|
|
90
|
+
};
|
|
91
|
+
}, [value, ms]);
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
export { fieldOf, useControlValues, useDebounced, useFilterFields };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { useId, useState } from "react";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
// src/filters/listbox.tsx
|
|
4
|
+
/** Where each key moves the active row, given the row now and how many there are. */
|
|
5
|
+
const MOVES = {
|
|
6
|
+
ArrowDown: (at, last) => Math.min(at + 1, last),
|
|
7
|
+
ArrowUp: (at) => Math.max(at - 1, 0)
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* The row the keyboard is on, apart from the chosen ones (as ARIA's listbox pattern asks). A new
|
|
11
|
+
* search starts at the first row, set while rendering, so no frame shows a stale row.
|
|
12
|
+
*/
|
|
13
|
+
function useActive(query, count) {
|
|
14
|
+
const [active, setActive] = useState(0);
|
|
15
|
+
const [searched, setSearched] = useState(query);
|
|
16
|
+
if (searched !== query) {
|
|
17
|
+
setSearched(query);
|
|
18
|
+
setActive(0);
|
|
19
|
+
}
|
|
20
|
+
return [Math.min(active, count - 1), setActive];
|
|
21
|
+
}
|
|
22
|
+
function Row({ id, option, active, multiselect, onHover }) {
|
|
23
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
24
|
+
id,
|
|
25
|
+
role: "option",
|
|
26
|
+
"aria-selected": option.selected ?? (multiselect ? false : active),
|
|
27
|
+
"data-active": active || void 0,
|
|
28
|
+
tabIndex: -1,
|
|
29
|
+
className: "dd-fb-option",
|
|
30
|
+
onMouseMove: () => {
|
|
31
|
+
if (!active) onHover();
|
|
32
|
+
},
|
|
33
|
+
onMouseDown: (e) => {
|
|
34
|
+
e.preventDefault();
|
|
35
|
+
},
|
|
36
|
+
ref: (el) => {
|
|
37
|
+
if (active) el?.scrollIntoView?.({ block: "nearest" });
|
|
38
|
+
},
|
|
39
|
+
onClick: option.onPick,
|
|
40
|
+
children: [option.selected === void 0 ? null : /* @__PURE__ */ jsx("span", {
|
|
41
|
+
className: "dd-fb-check",
|
|
42
|
+
"data-single": !multiselect || void 0,
|
|
43
|
+
"data-on": option.selected || void 0,
|
|
44
|
+
"aria-hidden": true
|
|
45
|
+
}), option.content]
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function Rows({ id, options, at, multiselect, setActive }) {
|
|
49
|
+
return options.map((o, i) => /* @__PURE__ */ jsxs("div", {
|
|
50
|
+
role: "presentation",
|
|
51
|
+
children: [o.group !== void 0 && o.group !== options[i - 1]?.group ? /* @__PURE__ */ jsx("div", {
|
|
52
|
+
className: "dd-fb-group",
|
|
53
|
+
"aria-hidden": true,
|
|
54
|
+
children: o.group
|
|
55
|
+
}) : null, /* @__PURE__ */ jsx(Row, {
|
|
56
|
+
id: `${id}-r${String(i)}`,
|
|
57
|
+
option: o,
|
|
58
|
+
active: i === at,
|
|
59
|
+
multiselect,
|
|
60
|
+
onHover: () => {
|
|
61
|
+
setActive(i);
|
|
62
|
+
}
|
|
63
|
+
})]
|
|
64
|
+
}, o.id));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* A search box over a list, the one keyboard pattern of the bar: type to search, arrows to
|
|
68
|
+
* move, Enter to pick, Backspace on an empty box to go back.
|
|
69
|
+
*/
|
|
70
|
+
/** The search box's keys: arrows move; Enter picks; Backspace on an empty box goes back. */
|
|
71
|
+
function keysFor(p, at, setActive) {
|
|
72
|
+
return (e) => {
|
|
73
|
+
if (e.nativeEvent.isComposing) return;
|
|
74
|
+
const move = MOVES[e.key];
|
|
75
|
+
if (move !== void 0 && p.options.length > 0) {
|
|
76
|
+
e.preventDefault();
|
|
77
|
+
setActive(move(at, p.options.length - 1));
|
|
78
|
+
} else if (e.key === "Enter") {
|
|
79
|
+
e.preventDefault();
|
|
80
|
+
p.options[at]?.onPick();
|
|
81
|
+
} else if (e.key === "Backspace" && p.query === "" && p.onBack !== void 0) {
|
|
82
|
+
e.preventDefault();
|
|
83
|
+
p.onBack();
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function Combobox(props) {
|
|
88
|
+
const { label, query, options, multiselect = false, busy = false } = props;
|
|
89
|
+
const id = useId();
|
|
90
|
+
const [at, setActive] = useActive(query, options.length);
|
|
91
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
92
|
+
className: "dd-fb-combobox",
|
|
93
|
+
children: [
|
|
94
|
+
props.before,
|
|
95
|
+
/* @__PURE__ */ jsx("input", {
|
|
96
|
+
className: "dd-fb-search",
|
|
97
|
+
role: "combobox",
|
|
98
|
+
"aria-expanded": true,
|
|
99
|
+
"aria-controls": `${id}-list`,
|
|
100
|
+
"aria-activedescendant": options[at] === void 0 ? void 0 : `${id}-r${String(at)}`,
|
|
101
|
+
"aria-autocomplete": "list",
|
|
102
|
+
"aria-label": label,
|
|
103
|
+
placeholder: props.placeholder,
|
|
104
|
+
value: query,
|
|
105
|
+
onChange: (e) => {
|
|
106
|
+
props.onQuery(e.target.value);
|
|
107
|
+
},
|
|
108
|
+
onKeyDown: keysFor(props, at, setActive)
|
|
109
|
+
}),
|
|
110
|
+
/* @__PURE__ */ jsx("div", {
|
|
111
|
+
id: `${id}-list`,
|
|
112
|
+
role: "listbox",
|
|
113
|
+
"aria-label": label,
|
|
114
|
+
"aria-multiselectable": multiselect || void 0,
|
|
115
|
+
"aria-busy": busy || void 0,
|
|
116
|
+
className: "dd-fb-list",
|
|
117
|
+
children: options.length === 0 ? /* @__PURE__ */ jsx("div", {
|
|
118
|
+
className: "dd-fb-empty",
|
|
119
|
+
children: props.empty
|
|
120
|
+
}) : /* @__PURE__ */ jsx(Rows, {
|
|
121
|
+
id,
|
|
122
|
+
options,
|
|
123
|
+
at,
|
|
124
|
+
multiselect,
|
|
125
|
+
setActive
|
|
126
|
+
})
|
|
127
|
+
})
|
|
128
|
+
]
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
export { Combobox };
|