@surfaces-ui/components 0.0.0-stage → 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/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +48 -2
- package/dist/appComponents.d.ts +86 -0
- package/dist/appComponents.js +555 -0
- package/dist/appComponents.js.map +1 -0
- package/dist/chart/BulletGraph.d.ts +44 -0
- package/dist/chart/BulletGraph.js +40 -0
- package/dist/chart/BulletGraph.js.map +1 -0
- package/dist/chart/Chart.d.ts +177 -0
- package/dist/chart/Chart.js +279 -0
- package/dist/chart/Chart.js.map +1 -0
- package/dist/chart/Sparkline.d.ts +35 -0
- package/dist/chart/Sparkline.js +37 -0
- package/dist/chart/Sparkline.js.map +1 -0
- package/dist/chart/advice.d.ts +28 -0
- package/dist/chart/advice.js +39 -0
- package/dist/chart/advice.js.map +1 -0
- package/dist/chart/option.d.ts +90 -0
- package/dist/chart/option.js +252 -0
- package/dist/chart/option.js.map +1 -0
- package/dist/controls/Combobox.d.ts +39 -0
- package/dist/controls/Combobox.js +75 -0
- package/dist/controls/Combobox.js.map +1 -0
- package/dist/controls/ConfirmDialog.d.ts +35 -0
- package/dist/controls/ConfirmDialog.js +84 -0
- package/dist/controls/ConfirmDialog.js.map +1 -0
- package/dist/controls/ItemControls.d.ts +115 -0
- package/dist/controls/ItemControls.js +109 -0
- package/dist/controls/ItemControls.js.map +1 -0
- package/dist/controls/Popover.d.ts +31 -0
- package/dist/controls/Popover.js +114 -0
- package/dist/controls/Popover.js.map +1 -0
- package/dist/controls/ResizeHandle.d.ts +48 -0
- package/dist/controls/ResizeHandle.js +91 -0
- package/dist/controls/ResizeHandle.js.map +1 -0
- package/dist/controls/SegmentedControl.d.ts +27 -0
- package/dist/controls/SegmentedControl.js +17 -0
- package/dist/controls/SegmentedControl.js.map +1 -0
- package/dist/controls/StatusBadge.d.ts +17 -0
- package/dist/controls/StatusBadge.js +10 -0
- package/dist/controls/StatusBadge.js.map +1 -0
- package/dist/controls/context.d.ts +17 -0
- package/dist/controls/context.js +2 -0
- package/dist/controls/context.js.map +1 -0
- package/dist/controls/placement.d.ts +43 -0
- package/dist/controls/placement.js +19 -0
- package/dist/controls/placement.js.map +1 -0
- package/dist/controls/resize.d.ts +23 -0
- package/dist/controls/resize.js +49 -0
- package/dist/controls/resize.js.map +1 -0
- package/dist/data/DataSourceProvider.d.ts +11 -0
- package/dist/data/DataSourceProvider.js +21 -0
- package/dist/data/DataSourceProvider.js.map +1 -0
- package/dist/data/export.d.ts +33 -0
- package/dist/data/export.js +77 -0
- package/dist/data/export.js.map +1 -0
- package/dist/data/format.d.ts +44 -0
- package/dist/data/format.js +127 -0
- package/dist/data/format.js.map +1 -0
- package/dist/data/messages.d.ts +13 -0
- package/dist/data/messages.js +2 -0
- package/dist/data/messages.js.map +1 -0
- package/dist/data/useRowWindow.d.ts +70 -0
- package/dist/data/useRowWindow.js +175 -0
- package/dist/data/useRowWindow.js.map +1 -0
- package/dist/discovery/AskData.d.ts +22 -0
- package/dist/discovery/AskData.js +48 -0
- package/dist/discovery/AskData.js.map +1 -0
- package/dist/discovery/ContextBuilder.d.ts +49 -0
- package/dist/discovery/ContextBuilder.js +167 -0
- package/dist/discovery/ContextBuilder.js.map +1 -0
- package/dist/discovery/DataSearch.d.ts +27 -0
- package/dist/discovery/DataSearch.js +73 -0
- package/dist/discovery/DataSearch.js.map +1 -0
- package/dist/discovery/ObjectDetail.d.ts +38 -0
- package/dist/discovery/ObjectDetail.js +129 -0
- package/dist/discovery/ObjectDetail.js.map +1 -0
- package/dist/discovery/RelationshipGraph.d.ts +19 -0
- package/dist/discovery/RelationshipGraph.js +64 -0
- package/dist/discovery/RelationshipGraph.js.map +1 -0
- package/dist/discovery/shared.d.ts +38 -0
- package/dist/discovery/shared.js +64 -0
- package/dist/discovery/shared.js.map +1 -0
- package/dist/editor/SqlEditor.d.ts +41 -0
- package/dist/editor/SqlEditor.js +181 -0
- package/dist/editor/SqlEditor.js.map +1 -0
- package/dist/explore/Explorer.d.ts +110 -0
- package/dist/explore/Explorer.js +471 -0
- package/dist/explore/Explorer.js.map +1 -0
- package/dist/explore/ModelLineage.d.ts +22 -0
- package/dist/explore/ModelLineage.js +70 -0
- package/dist/explore/ModelLineage.js.map +1 -0
- package/dist/explore/ReportBuilder.d.ts +39 -0
- package/dist/explore/ReportBuilder.js +98 -0
- package/dist/explore/ReportBuilder.js.map +1 -0
- package/dist/explore/SemanticModelEditor.d.ts +60 -0
- package/dist/explore/SemanticModelEditor.js +220 -0
- package/dist/explore/SemanticModelEditor.js.map +1 -0
- package/dist/explore/SemanticRows.d.ts +42 -0
- package/dist/explore/SemanticRows.js +117 -0
- package/dist/explore/SemanticRows.js.map +1 -0
- package/dist/explore/SemanticsProvider.d.ts +31 -0
- package/dist/explore/SemanticsProvider.js +72 -0
- package/dist/explore/SemanticsProvider.js.map +1 -0
- package/dist/explore/model.d.ts +151 -0
- package/dist/explore/model.js +344 -0
- package/dist/explore/model.js.map +1 -0
- package/dist/explore/report.d.ts +60 -0
- package/dist/explore/report.js +92 -0
- package/dist/explore/report.js.map +1 -0
- package/dist/explore/semantic-proposal.d.ts +44 -0
- package/dist/explore/semantic-proposal.js +67 -0
- package/dist/explore/semantic-proposal.js.map +1 -0
- package/dist/explore/semantic.d.ts +44 -0
- package/dist/explore/semantic.js +154 -0
- package/dist/explore/semantic.js.map +1 -0
- package/dist/files/FileBrowser.d.ts +42 -0
- package/dist/files/FileBrowser.js +314 -0
- package/dist/files/FileBrowser.js.map +1 -0
- package/dist/files/files.d.ts +65 -0
- package/dist/files/files.js +165 -0
- package/dist/files/files.js.map +1 -0
- package/dist/filter/FilterBar.d.ts +49 -0
- package/dist/filter/FilterBar.js +153 -0
- package/dist/filter/FilterBar.js.map +1 -0
- package/dist/filter/QueryFilters.d.ts +25 -0
- package/dist/filter/QueryFilters.js +58 -0
- package/dist/filter/QueryFilters.js.map +1 -0
- package/dist/form/Form.d.ts +76 -0
- package/dist/form/Form.js +131 -0
- package/dist/form/Form.js.map +1 -0
- package/dist/graph/NodeGraph.d.ts +30 -0
- package/dist/graph/NodeGraph.js +86 -0
- package/dist/graph/NodeGraph.js.map +1 -0
- package/dist/graph/model.d.ts +68 -0
- package/dist/graph/model.js +122 -0
- package/dist/graph/model.js.map +1 -0
- package/dist/grid/DataGrid.d.ts +98 -0
- package/dist/grid/DataGrid.js +399 -0
- package/dist/grid/DataGrid.js.map +1 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.js +86 -0
- package/dist/index.js.map +1 -0
- package/dist/locale/SurfacesText.d.ts +17 -0
- package/dist/locale/SurfacesText.js +17 -0
- package/dist/locale/SurfacesText.js.map +1 -0
- package/dist/locale/locale.d.ts +305 -0
- package/dist/locale/locale.js +359 -0
- package/dist/locale/locale.js.map +1 -0
- package/dist/menu/ContextMenu.d.ts +31 -0
- package/dist/menu/ContextMenu.js +95 -0
- package/dist/menu/ContextMenu.js.map +1 -0
- package/dist/metric/Metric.d.ts +33 -0
- package/dist/metric/Metric.js +63 -0
- package/dist/metric/Metric.js.map +1 -0
- package/dist/metric/useMeasure.d.ts +23 -0
- package/dist/metric/useMeasure.js +43 -0
- package/dist/metric/useMeasure.js.map +1 -0
- package/dist/model/ModelDiagram.d.ts +142 -0
- package/dist/model/ModelDiagram.js +699 -0
- package/dist/model/ModelDiagram.js.map +1 -0
- package/dist/panzoom/usePanZoom.d.ts +117 -0
- package/dist/panzoom/usePanZoom.js +313 -0
- package/dist/panzoom/usePanZoom.js.map +1 -0
- package/dist/pipeline/DatasetReview.d.ts +48 -0
- package/dist/pipeline/DatasetReview.js +157 -0
- package/dist/pipeline/DatasetReview.js.map +1 -0
- package/dist/pipeline/IngestWizard.d.ts +51 -0
- package/dist/pipeline/IngestWizard.js +79 -0
- package/dist/pipeline/IngestWizard.js.map +1 -0
- package/dist/pipeline/Pipeline.d.ts +171 -0
- package/dist/pipeline/Pipeline.js +203 -0
- package/dist/pipeline/Pipeline.js.map +1 -0
- package/dist/pipeline/PipelineView.d.ts +63 -0
- package/dist/pipeline/PipelineView.js +239 -0
- package/dist/pipeline/PipelineView.js.map +1 -0
- package/dist/pivot/PivotTable.d.ts +94 -0
- package/dist/pivot/PivotTable.js +171 -0
- package/dist/pivot/PivotTable.js.map +1 -0
- package/dist/plan/QueryPlan.d.ts +29 -0
- package/dist/plan/QueryPlan.js +79 -0
- package/dist/plan/QueryPlan.js.map +1 -0
- package/dist/result/QueryResult.d.ts +51 -0
- package/dist/result/QueryResult.js +255 -0
- package/dist/result/QueryResult.js.map +1 -0
- package/dist/review/ActionReceipt.d.ts +52 -0
- package/dist/review/ActionReceipt.js +32 -0
- package/dist/review/ActionReceipt.js.map +1 -0
- package/dist/review/PiiReview.d.ts +37 -0
- package/dist/review/PiiReview.js +29 -0
- package/dist/review/PiiReview.js.map +1 -0
- package/dist/review/PlanReview.d.ts +42 -0
- package/dist/review/PlanReview.js +37 -0
- package/dist/review/PlanReview.js.map +1 -0
- package/dist/review/StepFlow.d.ts +25 -0
- package/dist/review/StepFlow.js +21 -0
- package/dist/review/StepFlow.js.map +1 -0
- package/dist/review/pii.d.ts +21 -0
- package/dist/review/pii.js +67 -0
- package/dist/review/pii.js.map +1 -0
- package/dist/rows/RowList.d.ts +41 -0
- package/dist/rows/RowList.js +71 -0
- package/dist/rows/RowList.js.map +1 -0
- package/dist/schema/ObjectBrowser.d.ts +78 -0
- package/dist/schema/ObjectBrowser.js +467 -0
- package/dist/schema/ObjectBrowser.js.map +1 -0
- package/dist/schema/ObjectInfo.d.ts +25 -0
- package/dist/schema/ObjectInfo.js +69 -0
- package/dist/schema/ObjectInfo.js.map +1 -0
- package/dist/schema/SchemaExplorer.d.ts +32 -0
- package/dist/schema/SchemaExplorer.js +175 -0
- package/dist/schema/SchemaExplorer.js.map +1 -0
- package/dist/schema/refs.d.ts +32 -0
- package/dist/schema/refs.js +65 -0
- package/dist/schema/refs.js.map +1 -0
- package/dist/site/Site.d.ts +191 -0
- package/dist/site/Site.js +138 -0
- package/dist/site/Site.js.map +1 -0
- package/dist/source/ConnectionBuilder.d.ts +54 -0
- package/dist/source/ConnectionBuilder.js +115 -0
- package/dist/source/ConnectionBuilder.js.map +1 -0
- package/dist/source/SourceList.d.ts +17 -0
- package/dist/source/SourceList.js +71 -0
- package/dist/source/SourceList.js.map +1 -0
- package/dist/styles.css +9882 -0
- package/dist/styles.layer.css +9886 -0
- package/dist/table/DataTable.d.ts +72 -0
- package/dist/table/DataTable.js +68 -0
- package/dist/table/DataTable.js.map +1 -0
- package/dist/table/Pagination.d.ts +20 -0
- package/dist/table/Pagination.js +16 -0
- package/dist/table/Pagination.js.map +1 -0
- package/dist/table/SelectionBar.d.ts +43 -0
- package/dist/table/SelectionBar.js +18 -0
- package/dist/table/SelectionBar.js.map +1 -0
- package/dist/text/Markdown.d.ts +71 -0
- package/dist/text/Markdown.js +187 -0
- package/dist/text/Markdown.js.map +1 -0
- package/dist/text/highlight.d.ts +23 -0
- package/dist/text/highlight.js +196 -0
- package/dist/text/highlight.js.map +1 -0
- package/dist/theme/ThemePicker.d.ts +16 -0
- package/dist/theme/ThemePicker.js +49 -0
- package/dist/theme/ThemePicker.js.map +1 -0
- package/dist/theme/ThemeProvider.d.ts +43 -0
- package/dist/theme/ThemeProvider.js +107 -0
- package/dist/theme/ThemeProvider.js.map +1 -0
- package/dist/workspace/NavBar.d.ts +23 -0
- package/dist/workspace/NavBar.js +171 -0
- package/dist/workspace/NavBar.js.map +1 -0
- package/dist/workspace/SideNav.d.ts +61 -0
- package/dist/workspace/SideNav.js +149 -0
- package/dist/workspace/SideNav.js.map +1 -0
- package/dist/workspace/Workspace.d.ts +50 -0
- package/dist/workspace/Workspace.js +243 -0
- package/dist/workspace/Workspace.js.map +1 -0
- package/dist/workspace/icons.d.ts +34 -0
- package/dist/workspace/icons.js +37 -0
- package/dist/workspace/icons.js.map +1 -0
- package/dist/workspace/layout.d.ts +74 -0
- package/dist/workspace/layout.js +276 -0
- package/dist/workspace/layout.js.map +1 -0
- package/package.json +85 -3
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { collect } from "@surfaces-ui/data";
|
|
3
|
+
import { useCallback, useEffect, useId, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { Chart } from "../chart/Chart.js";
|
|
5
|
+
import { ControlIcon, ItemControls } from "../controls/ItemControls.js";
|
|
6
|
+
import { filtersExcept, filtersFromSpec } from "@surfaces-ui/data";
|
|
7
|
+
import { useDataSource } from "../data/DataSourceProvider.js";
|
|
8
|
+
import { formatCell, numericValue } from "../data/format.js";
|
|
9
|
+
import { messageOf } from "../data/useRowWindow.js";
|
|
10
|
+
import { QueryFilters } from "../filter/QueryFilters.js";
|
|
11
|
+
import { useContextMenu } from "../menu/ContextMenu.js";
|
|
12
|
+
import { addToShelf, emptyExplore, exploreChart, exploreQuery, movePill, normalizeExplore, pillColumn, pillId, pillNumber, pillProblem, pillsFromFields, suggestedView, removeFromShelf, setChartSettings, setMeasureStyle, setPill, suggestDerived, swapProblem, } from "./model.js";
|
|
13
|
+
import { canPromote, mergeModelPills, pillsWithModel } from "./semantic.js";
|
|
14
|
+
import { ModelLineage } from "./ModelLineage.js";
|
|
15
|
+
const SHELVES = [
|
|
16
|
+
{ id: "rows", label: "Break down by", takes: ["attribute"], hint: "An attribute: one bar, point or row for each of its values." },
|
|
17
|
+
{ id: "split", label: "Split by", takes: ["attribute"], hint: "An attribute whose values become separate series." },
|
|
18
|
+
{ id: "values", label: "Measure", takes: ["metric", "derived"], hint: "Metrics and calculations: what is counted or added up." },
|
|
19
|
+
];
|
|
20
|
+
const GLYPH = { attribute: "Abc", metric: "Σ", derived: "ƒ" };
|
|
21
|
+
/** How long a finger holds a pill before it drags it rather than scrolling the page. Shorter than a long press, which opens the pill's menu. */
|
|
22
|
+
export const TOUCH_HOLD_MS = 300;
|
|
23
|
+
const KIND_WORD = { attribute: "attribute", metric: "metric", derived: "calculation" };
|
|
24
|
+
const AGGREGATES = [
|
|
25
|
+
{ value: "sum", label: "Sum" },
|
|
26
|
+
{ value: "avg", label: "Average" },
|
|
27
|
+
{ value: "count", label: "Count" },
|
|
28
|
+
{ value: "count_distinct", label: "Count of distinct values" },
|
|
29
|
+
{ value: "min", label: "Smallest" },
|
|
30
|
+
{ value: "max", label: "Largest" },
|
|
31
|
+
{ value: "median", label: "Median" },
|
|
32
|
+
];
|
|
33
|
+
const GRAINS = ["day", "week", "month", "quarter", "year"];
|
|
34
|
+
const FORMATS = ["number", "compact", "currency", "percent"];
|
|
35
|
+
/** The grouped result of what is on the shelves, asked of the engine again whenever the shelves, the pills or the filters change. */
|
|
36
|
+
function useExploreRows(dataSource, sourceId, sql, state, limit) {
|
|
37
|
+
const key = JSON.stringify([sourceId, sql, state.rows, state.split, state.values, state.filters, state.fields, limit]);
|
|
38
|
+
const [result, setResult] = useState({ key: "" });
|
|
39
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: `key` stands for the query.
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
const controller = new AbortController();
|
|
42
|
+
let query;
|
|
43
|
+
try {
|
|
44
|
+
query = exploreQuery(state, sql, limit);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
setResult({ key, error: messageOf(error) });
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
collect(dataSource.execute({ sourceId, query }, { signal: controller.signal }))
|
|
51
|
+
.then((found) => {
|
|
52
|
+
if (controller.signal.aborted)
|
|
53
|
+
return;
|
|
54
|
+
const types = Object.fromEntries(found.table.schema.fields.map((f) => [f.name, f.type]));
|
|
55
|
+
setResult({ key, rows: found.table.toArray().map((row) => row.toJSON()), types });
|
|
56
|
+
})
|
|
57
|
+
.catch((error) => !controller.signal.aborted && setResult({ key, error: messageOf(error) }));
|
|
58
|
+
return () => controller.abort();
|
|
59
|
+
}, [dataSource, key]);
|
|
60
|
+
return result.key === key ? result : {};
|
|
61
|
+
}
|
|
62
|
+
const shelfPills = (state) => [...state.rows, ...(state.split ? [state.split] : []), ...state.values].map((id) => state.fields.find((p) => p.id === id)).filter((p) => !!p);
|
|
63
|
+
/** The grouped result of what is on the shelves, as a small table. The engine summarises; at most `limit` rows are asked for. */
|
|
64
|
+
function ExploreTable({ dataSource, sourceId, sql, state, limit = 200 }) {
|
|
65
|
+
const result = useExploreRows(dataSource, sourceId, sql, state, limit);
|
|
66
|
+
const columns = shelfPills(state);
|
|
67
|
+
if (result.error)
|
|
68
|
+
return (_jsx("p", { className: "sf-filter-problem", role: "alert", children: result.error }));
|
|
69
|
+
if (!result.rows)
|
|
70
|
+
return _jsx("p", { className: "sf-explore-hint", children: "Working it out\u2026" });
|
|
71
|
+
const cell = (pill, row) => {
|
|
72
|
+
const value = row[pillColumn(pill)];
|
|
73
|
+
const type = result.types?.[pillColumn(pill)];
|
|
74
|
+
// A decimal arrives as its digits without the point; its type says where the point goes.
|
|
75
|
+
if (pill.kind !== "attribute")
|
|
76
|
+
return pillNumber(type ? numericValue(value, type) : value, pill.format);
|
|
77
|
+
return value == null ? "–" : type ? formatCell(value, type).text : String(value);
|
|
78
|
+
};
|
|
79
|
+
return (_jsxs("div", { className: "sf-explore-table", tabIndex: 0, role: "region", "aria-label": "The numbers behind the view", children: [_jsxs("table", { children: [_jsx("thead", { children: _jsx("tr", { children: columns.map((pill) => (_jsx("th", { scope: "col", "data-number": pill.kind !== "attribute" || undefined, children: pill.label }, pill.id))) }) }), _jsx("tbody", { children: result.rows.map((row, i) => (_jsx("tr", { children: columns.map((pill) => (_jsx("td", { "data-number": pill.kind !== "attribute" || undefined, children: cell(pill, row) }, pill.id))) }, i))) })] }), result.rows.length >= limit && _jsxs("p", { className: "sf-explore-hint", children: ["The first ", limit, " rows, in the order shown. Add a filter or a coarser breakdown to see fewer."] })] }));
|
|
80
|
+
}
|
|
81
|
+
/** Values with nothing to break them down by: one number each, worked out by the engine, shown as headline figures. */
|
|
82
|
+
function ExploreNumbers({ dataSource, sourceId, sql, state }) {
|
|
83
|
+
const result = useExploreRows(dataSource, sourceId, sql, { ...state, rows: [], split: undefined }, 1);
|
|
84
|
+
const values = state.values.map((id) => state.fields.find((p) => p.id === id)).filter((p) => !!p);
|
|
85
|
+
if (result.error)
|
|
86
|
+
return (_jsx("p", { className: "sf-filter-problem", role: "alert", children: result.error }));
|
|
87
|
+
return (_jsx("dl", { className: "sf-explore-numbers", children: values.map((pill) => (_jsxs("div", { children: [_jsx("dt", { children: pill.label }), _jsx("dd", { children: result.rows ? pillNumber(result.types?.[pill.id] ? numericValue(result.rows[0]?.[pill.id], result.types[pill.id]) : result.rows[0]?.[pill.id], pill.format) : "…" })] }, pill.id))) }));
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* What a set of shelves draws: a chart when there is something to break
|
|
91
|
+
* down by, the numbers behind it as a table, or headline figures when there
|
|
92
|
+
* is nothing to break down by. One tile of a report, or the explorer's own
|
|
93
|
+
* result. Everything is worked out by the engine.
|
|
94
|
+
*/
|
|
95
|
+
export function ExploreView(props) {
|
|
96
|
+
const { sourceId, sql, state, height = 300, label, onPointClick } = props;
|
|
97
|
+
const dataSource = useDataSource(props.dataSource);
|
|
98
|
+
const chart = useMemo(() => exploreChart(state), [state]);
|
|
99
|
+
const clickable = !!onPointClick && !!chart && !chart.xGrain;
|
|
100
|
+
const [filters, problem] = useMemo(() => {
|
|
101
|
+
try {
|
|
102
|
+
return [filtersFromSpec(clickable ? filtersExcept(state.filters, chart.x) : state.filters), undefined];
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
return [[], messageOf(error)];
|
|
106
|
+
}
|
|
107
|
+
}, [state.filters, clickable, chart]);
|
|
108
|
+
if (state.values.length === 0)
|
|
109
|
+
return null;
|
|
110
|
+
if (problem)
|
|
111
|
+
return (_jsx("p", { className: "sf-filter-problem", role: "alert", children: problem }));
|
|
112
|
+
const shown = props.view ?? state.view ?? "chart";
|
|
113
|
+
if (!chart)
|
|
114
|
+
return _jsx(ExploreNumbers, { dataSource: dataSource, sourceId: sourceId, sql: sql, state: state });
|
|
115
|
+
if (shown === "table")
|
|
116
|
+
return _jsx(ExploreTable, { dataSource: dataSource, sourceId: sourceId, sql: sql, state: state });
|
|
117
|
+
return (_jsx(Chart, { dataSource: dataSource, sourceId: sourceId, sql: sql, filters: filters, type: chart.type, x: chart.x, xGrain: chart.xGrain, splitBy: chart.splitBy, y: chart.y, xTitle: chart.xTitle, yTitle: chart.yTitle, yRightTitle: chart.yRightTitle, yFormat: chart.yFormat, horizontal: chart.horizontal, height: height, label: label, onPointClick: clickable ? (point) => point.x != null && onPointClick(chart.x, point.x) : undefined }, JSON.stringify(chart)));
|
|
118
|
+
}
|
|
119
|
+
/** What the shelves say, in words: "Refund rate by Category, split by Channel". */
|
|
120
|
+
export function exploreReading(state) {
|
|
121
|
+
const label = (id) => state.fields.find((p) => p.id === id)?.label;
|
|
122
|
+
return [
|
|
123
|
+
state.values.map(label).filter(Boolean).join(" and "),
|
|
124
|
+
state.rows.length > 0 ? `by ${state.rows.map(label).filter(Boolean).join(", then ")}` : "",
|
|
125
|
+
state.split ? `split by ${label(state.split)}` : "",
|
|
126
|
+
]
|
|
127
|
+
.filter(Boolean)
|
|
128
|
+
.join(" ");
|
|
129
|
+
}
|
|
130
|
+
/** The form a pill is made or changed in. Nothing is saved until it is valid. Used by `Explorer` and `SemanticModelEditor`, so a field is edited the same way in both. */
|
|
131
|
+
export function PillEditor({ pill, all, columns, isNew, modelLabel, onSave, onCancel, onRemove, onPromote, }) {
|
|
132
|
+
const id = useId();
|
|
133
|
+
const [draft, setDraft] = useState(pill);
|
|
134
|
+
const others = all.filter((p) => p.id !== pill.id);
|
|
135
|
+
const named = isNew ? { ...draft, id: pillId(draft.label || KIND_WORD[draft.kind], others.map((p) => p.id)) } : draft;
|
|
136
|
+
const problem = pillProblem(named, [...others, named], columns.map((c) => c.name));
|
|
137
|
+
const metrics = all.filter((p) => p.kind === "metric");
|
|
138
|
+
const field = columns.find((c) => c.name === draft.column);
|
|
139
|
+
const temporal = !!field && /Timestamp|Date/.test(String(field.type));
|
|
140
|
+
// Calculations that name this metric follow any change to it; say so before it is saved.
|
|
141
|
+
const dependents = isNew || pill.kind !== "metric" ? [] : all.filter((p) => p.kind === "derived" && new RegExp(`\\b${pill.id}\\b`).test(p.expression ?? ""));
|
|
142
|
+
const changesMeaning = dependents.length > 0 && (draft.aggregate !== pill.aggregate || draft.column !== pill.column);
|
|
143
|
+
const submit = (event) => {
|
|
144
|
+
event.preventDefault();
|
|
145
|
+
if (!problem)
|
|
146
|
+
onSave(named);
|
|
147
|
+
};
|
|
148
|
+
return (_jsxs("form", { className: "sf-explore-editor", onSubmit: submit, "aria-label": isNew ? `New ${KIND_WORD[draft.kind]}` : `Edit ${pill.label}`, children: [_jsxs("p", { className: "sf-explore-editor-title", children: [_jsx("span", { className: "sf-pill-glyph", "aria-hidden": "true", children: GLYPH[draft.kind] }), isNew ? `New ${KIND_WORD[draft.kind]}` : `Edit ${KIND_WORD[draft.kind]}`, !isNew && pill.by && _jsxs("span", { className: "sf-explore-by", children: [" \u00B7 made by ", pill.by] })] }), modelLabel && _jsxs("p", { className: "sf-explore-hint", children: ["Defined in the ", modelLabel, " model. A change here stays in this view until it is added to the model again."] }), _jsx("label", { htmlFor: `${id}-label`, children: "Label" }), _jsx("input", { id: `${id}-label`, className: "sf-input", value: draft.label, autoFocus: true, onChange: (e) => setDraft({ ...draft, label: e.target.value }) }), draft.kind !== "derived" && (_jsxs(_Fragment, { children: [_jsx("label", { htmlFor: `${id}-column`, children: "Column" }), _jsxs("select", { id: `${id}-column`, className: "sf-input", value: draft.column ?? "", onChange: (e) => setDraft({ ...draft, column: e.target.value || undefined }), children: [draft.kind === "metric" && _jsx("option", { value: "", children: "(rows)" }), columns.map((c) => (_jsx("option", { value: c.name, children: c.name }, c.name)))] })] })), draft.kind === "attribute" && temporal && (_jsxs(_Fragment, { children: [_jsx("label", { htmlFor: `${id}-grain`, children: "Grouped by" }), _jsx("select", { id: `${id}-grain`, className: "sf-input", value: draft.grain ?? "month", onChange: (e) => setDraft({ ...draft, grain: e.target.value }), children: GRAINS.map((g) => (_jsx("option", { value: g, children: g }, g))) })] })), draft.kind === "metric" && (_jsxs(_Fragment, { children: [_jsx("label", { htmlFor: `${id}-aggregate`, children: "Summarised as" }), _jsx("select", { id: `${id}-aggregate`, className: "sf-input", value: draft.aggregate ?? "sum", onChange: (e) => setDraft({ ...draft, aggregate: e.target.value }), children: AGGREGATES.map((a) => (_jsx("option", { value: a.value, children: a.label }, a.value))) })] })), draft.kind === "derived" && (_jsxs(_Fragment, { children: [_jsx("label", { htmlFor: `${id}-expression`, children: "Calculation" }), _jsx("textarea", { id: `${id}-expression`, className: "sf-input sf-explore-expression", rows: 2, spellCheck: false, value: draft.expression ?? "", "aria-describedby": `${id}-names`, placeholder: "refund_amount / line_amount", onChange: (e) => setDraft({ ...draft, expression: e.target.value }) }), _jsx("p", { id: `${id}-names`, className: "sf-explore-hint", children: "Metrics, numbers, + \u2212 \u00D7 \u00F7 and parentheses. Worked out by the database after summarising. Press a metric to add it:" }), _jsx("div", { className: "sf-explore-names", children: metrics.map((m) => (_jsxs("button", { type: "button", className: "sf-pill", "data-kind": "metric", title: m.label, onClick: () => setDraft({ ...draft, expression: `${draft.expression ?? ""}${draft.expression && !/[\s(]$/.test(draft.expression) ? " " : ""}${m.id}` }), children: [_jsx("span", { className: "sf-pill-glyph", "aria-hidden": "true", children: "\u03A3" }), m.id] }, m.id))) })] })), draft.kind !== "attribute" && (_jsxs(_Fragment, { children: [_jsx("label", { htmlFor: `${id}-format`, children: "Shown as" }), _jsx("select", { id: `${id}-format`, className: "sf-input", value: draft.format ?? "number", onChange: (e) => setDraft({ ...draft, format: e.target.value }), children: FORMATS.map((f) => (_jsx("option", { value: f, children: f }, f))) })] })), _jsx("label", { htmlFor: `${id}-description`, children: "What it means (optional)" }), _jsx("input", { id: `${id}-description`, className: "sf-input", value: draft.description ?? "", onChange: (e) => setDraft({ ...draft, description: e.target.value || undefined }) }), changesMeaning && (_jsxs("p", { className: "sf-explore-warning", role: "status", children: [dependents.length === 1 ? "A calculation uses" : `${dependents.length} calculations use`, " this metric and will change with it: ", dependents.map((p) => p.label).join(", "), ". To keep ", dependents.length === 1 ? "it" : "them", " as ", dependents.length === 1 ? "it is" : "they are", ", cancel and duplicate this metric instead."] })), problem && (_jsx("p", { className: "sf-filter-problem", role: "alert", children: problem })), _jsxs("div", { className: "sf-explore-editor-actions", children: [_jsx("button", { type: "submit", className: "sf-button", "data-variant": "primary", disabled: !!problem, children: isNew ? "Add" : "Save" }), _jsx("button", { type: "button", className: "sf-button", onClick: onCancel, children: "Cancel" }), onPromote && (_jsx("button", { type: "button", className: "sf-button", onClick: onPromote, children: "Add to the model" })), onRemove && (_jsx("button", { type: "button", className: "sf-button", "data-variant": "danger", onClick: onRemove, children: "Remove" }))] })] }));
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Exploring a query with pills. Every column becomes a pill generated on the
|
|
152
|
+
* spot: an attribute to break down by or a metric to measure. Calculations
|
|
153
|
+
* over metrics (a rate, a margin) are pills too, suggested, written by an
|
|
154
|
+
* agent or made by hand, and every pill can be edited. Putting pills on the
|
|
155
|
+
* shelves draws a chart and the numbers behind it, worked out by the
|
|
156
|
+
* database. The shelves and the pills are plain data, so an agent can set
|
|
157
|
+
* exactly what a person can drag, and sees what a person did.
|
|
158
|
+
*/
|
|
159
|
+
export function Explorer(props) {
|
|
160
|
+
const { onChange, onAsk, editor = "you", chartHeight = 300, model } = props;
|
|
161
|
+
const sourceId = props.sourceId ?? model?.sourceId ?? "";
|
|
162
|
+
const sql = props.sql ?? model?.sql ?? "";
|
|
163
|
+
const dataSource = useDataSource(props.dataSource);
|
|
164
|
+
const menu = useContextMenu();
|
|
165
|
+
const uid = useId();
|
|
166
|
+
const [own, setOwn] = useState(props.defaultValue ?? emptyExplore);
|
|
167
|
+
const state = useMemo(() => normalizeExplore(props.value ?? own), [props.value, own]);
|
|
168
|
+
// As given, before anything is taken off the shelves: shelves can name pills that are only generated once the columns are known.
|
|
169
|
+
const latest = useRef(props.value ?? own);
|
|
170
|
+
latest.current = props.value ?? own;
|
|
171
|
+
const modelRef = useRef(model);
|
|
172
|
+
modelRef.current = model;
|
|
173
|
+
const set = useCallback((next) => {
|
|
174
|
+
const clean = normalizeExplore(next);
|
|
175
|
+
setOwn(clean);
|
|
176
|
+
onChange?.(clean);
|
|
177
|
+
}, [onChange]);
|
|
178
|
+
const [columns, setColumns] = useState([]);
|
|
179
|
+
const [problem, setProblem] = useState();
|
|
180
|
+
const [note, setNote] = useState("");
|
|
181
|
+
const [editing, setEditing] = useState();
|
|
182
|
+
const [search, setSearch] = useState("");
|
|
183
|
+
const context = useRef(props.onContext);
|
|
184
|
+
context.current = props.onContext;
|
|
185
|
+
/** Says what happened to the person, and tells the host the same as context for an agent. */
|
|
186
|
+
const say = (kind, summary, subject) => {
|
|
187
|
+
setNote(summary);
|
|
188
|
+
context.current?.({ kind, summary, ...(subject ? { subject } : {}) });
|
|
189
|
+
};
|
|
190
|
+
const [ask, setAsk] = useState("");
|
|
191
|
+
// Calculations and filters start folded unless there are some already; then each stays as a person leaves it.
|
|
192
|
+
const [calculationsOpen, setCalculationsOpen] = useState(() => (props.value ?? props.defaultValue)?.fields?.some((p) => p.kind === "derived") ?? false);
|
|
193
|
+
const [filtersOpen, setFiltersOpen] = useState(() => ((props.value ?? props.defaultValue)?.filters?.length ?? 0) > 0);
|
|
194
|
+
const shown = state.view ?? "chart";
|
|
195
|
+
// The query's columns: what a pill can be made from. With no pills given, they are generated from these.
|
|
196
|
+
useEffect(() => {
|
|
197
|
+
const controller = new AbortController();
|
|
198
|
+
setProblem(undefined);
|
|
199
|
+
collect(dataSource.execute({ sourceId, query: { baseSql: sql, page: { limit: 1 } } }, { signal: controller.signal }))
|
|
200
|
+
.then((result) => {
|
|
201
|
+
if (controller.signal.aborted)
|
|
202
|
+
return;
|
|
203
|
+
const found = result.table.schema.fields;
|
|
204
|
+
setColumns(found);
|
|
205
|
+
if (latest.current.fields.length === 0) {
|
|
206
|
+
const fields = modelRef.current ? pillsWithModel(modelRef.current, found) : pillsFromFields(found);
|
|
207
|
+
const empty = latest.current.rows.length === 0 && latest.current.values.length === 0 && !latest.current.split;
|
|
208
|
+
set({ ...latest.current, fields, ...(props.startWithView && empty ? suggestedView(fields) : {}) });
|
|
209
|
+
const own = fields.filter((p) => p.model).length;
|
|
210
|
+
say("fields.generated", own > 0 ? `${own} fields from the ${modelRef.current?.label || modelRef.current?.name} model, and ${fields.length - own} generated from the other columns.` : `${fields.length} fields generated from the columns. Edit any of them, or add your own.`, { fields: fields.map((p) => ({ id: p.id, kind: p.kind, label: p.label, ...(p.model ? { model: p.model } : {}) })) });
|
|
211
|
+
}
|
|
212
|
+
})
|
|
213
|
+
.catch((error) => !controller.signal.aborted && setProblem(messageOf(error)));
|
|
214
|
+
return () => controller.abort();
|
|
215
|
+
}, [dataSource, sourceId, sql]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
216
|
+
// A model that changes (a field added to it here or elsewhere) brings its pills along; pills changed by hand stay as they are.
|
|
217
|
+
useEffect(() => {
|
|
218
|
+
if (!model || latest.current.fields.length === 0)
|
|
219
|
+
return;
|
|
220
|
+
const merged = mergeModelPills(latest.current.fields, model);
|
|
221
|
+
if (JSON.stringify(merged) !== JSON.stringify(latest.current.fields))
|
|
222
|
+
set({ ...latest.current, fields: merged });
|
|
223
|
+
}, [model]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
224
|
+
const promote = async (target) => {
|
|
225
|
+
if (!props.onPromote)
|
|
226
|
+
return;
|
|
227
|
+
try {
|
|
228
|
+
const said = await props.onPromote(target, state.fields);
|
|
229
|
+
say("field.promoted", said || `${target.label} offered to the model.`, { field: target, ...(model ? { model: model.name } : {}) });
|
|
230
|
+
}
|
|
231
|
+
catch (error) {
|
|
232
|
+
setNote(messageOf(error));
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
const offer = (target) => !!props.onPromote && canPromote(target, model);
|
|
236
|
+
const pill = (id) => state.fields.find((p) => p.id === id);
|
|
237
|
+
const onShelf = (id) => (state.rows.includes(id) ? "rows" : state.split === id ? "split" : state.values.includes(id) ? "values" : undefined);
|
|
238
|
+
const put = (id, shelf, at) => {
|
|
239
|
+
const target = pill(id);
|
|
240
|
+
if (!target)
|
|
241
|
+
return;
|
|
242
|
+
const where = shelf ?? (target.kind === "attribute" ? "rows" : "values");
|
|
243
|
+
const name = SHELVES.find((s) => s.id === where).label;
|
|
244
|
+
if (!SHELVES.find((s) => s.id === where).takes.includes(target.kind))
|
|
245
|
+
return setNote(`${target.label} is ${target.kind === "attribute" ? "an attribute" : `a ${KIND_WORD[target.kind]}`}, so it cannot go on “${name}”.`);
|
|
246
|
+
const was = onShelf(id);
|
|
247
|
+
const next = addToShelf(state, id, where, at);
|
|
248
|
+
set(next);
|
|
249
|
+
const list = where === "split" ? [] : next[where];
|
|
250
|
+
const place = list.indexOf(id) + 1;
|
|
251
|
+
say("shelf.changed", was === where && list.length > 1 ? `${target.label} moved to place ${place} of ${list.length} on “${name}”.` : `${target.label} added to “${name}”.`, { ...(was === where ? { moved: target.id } : { added: target.id }), shelf: where, rows: next.rows, split: next.split ?? null, values: next.values });
|
|
252
|
+
};
|
|
253
|
+
const shift = (target, by) => {
|
|
254
|
+
const shelf = onShelf(target.id);
|
|
255
|
+
if (shelf !== "rows" && shelf !== "values")
|
|
256
|
+
return;
|
|
257
|
+
const next = movePill(state, target.id, by);
|
|
258
|
+
if (next === state)
|
|
259
|
+
return setNote(`${target.label} is already ${by < 0 ? "first" : "last"} on “${SHELVES.find((s) => s.id === shelf).label}”.`);
|
|
260
|
+
set(next);
|
|
261
|
+
say("shelf.changed", `${target.label} moved to place ${next[shelf].indexOf(target.id) + 1} of ${next[shelf].length} on “${SHELVES.find((s) => s.id === shelf).label}”.`, { moved: target.id, shelf, rows: next.rows, split: next.split ?? null, values: next.values });
|
|
262
|
+
};
|
|
263
|
+
const take = (id, shelf) => {
|
|
264
|
+
const next = removeFromShelf(state, id, shelf);
|
|
265
|
+
set(next);
|
|
266
|
+
say("shelf.changed", `${pill(id)?.label ?? id} removed from “${SHELVES.find((s) => s.id === shelf).label}”.`, { removed: id, shelf, rows: next.rows, split: next.split ?? null, values: next.values });
|
|
267
|
+
};
|
|
268
|
+
const removeField = (target) => {
|
|
269
|
+
set(setPill(state, target.id, undefined));
|
|
270
|
+
say("field.removed", `${target.label} removed.`, { field: target });
|
|
271
|
+
};
|
|
272
|
+
const CHART_WORD = { bar: "bars", line: "a line", area: "an area" };
|
|
273
|
+
const chartTypeNow = state.chart?.type ?? (pill(state.rows[0] ?? "")?.grain ? "line" : "bar");
|
|
274
|
+
const setType = (type) => {
|
|
275
|
+
set({ ...setChartSettings(state, { type }), view: "chart" });
|
|
276
|
+
say("chart.changed", `Drawn as ${CHART_WORD[type]}.`, { chart: { type } });
|
|
277
|
+
};
|
|
278
|
+
const showTable = () => {
|
|
279
|
+
set({ ...state, view: "table" });
|
|
280
|
+
say("view.changed", "Shown as a table.", { view: "table" });
|
|
281
|
+
};
|
|
282
|
+
const cannotSwap = swapProblem(state);
|
|
283
|
+
const swapped = !!state.chart?.swap && !cannotSwap;
|
|
284
|
+
const swap = () => {
|
|
285
|
+
const next = !swapped;
|
|
286
|
+
set(setChartSettings(state, { swap: next }));
|
|
287
|
+
say("chart.changed", next ? "Axes swapped: the bars run across, the breakdown down the side." : "Axes back: the bars stand up, the breakdown along the bottom.", { chart: { swap: next } });
|
|
288
|
+
};
|
|
289
|
+
const styleOf = (id) => state.chart?.measures?.[id] ?? {};
|
|
290
|
+
const restyle = (target, style) => {
|
|
291
|
+
set(setMeasureStyle(state, target.id, style));
|
|
292
|
+
const said = style.type ? `${target.label} drawn as ${CHART_WORD[style.type]}.` : style.axis === "right" ? `${target.label} measured on its own scale, at the right.` : `${target.label} measured on the left scale.`;
|
|
293
|
+
say("chart.changed", said, { measure: target.id, ...style });
|
|
294
|
+
};
|
|
295
|
+
// Two scales when some measures, but not all, are on the right (all on the right is drawn as one scale, on the left).
|
|
296
|
+
const onRight = state.values.filter((id) => styleOf(id).axis === "right").length;
|
|
297
|
+
const twoScales = onRight > 0 && onRight < state.values.length;
|
|
298
|
+
// Which side's scale a measure is read against, marked on its pill when there are two.
|
|
299
|
+
const sideOf = (id) => (twoScales ? (styleOf(id).axis === "right" ? "right" : "left") : undefined);
|
|
300
|
+
// What each shelf puts on the chart, said on the shelf itself.
|
|
301
|
+
const axisOf = { rows: swapped ? "Y axis" : "X axis", values: swapped ? "X axis" : twoScales ? "Y axes" : "Y axis", split: "Colour" };
|
|
302
|
+
const blank = (kind) => ({ id: "", kind, label: "", by: editor, ...(kind === "metric" ? { aggregate: "sum", column: columns.find((c) => /Int|Float|Decimal/.test(String(c.type)))?.name } : kind === "attribute" ? { column: columns[0]?.name } : { expression: "", format: "number" }) });
|
|
303
|
+
const menuFor = (target) => [
|
|
304
|
+
...(target.kind === "attribute"
|
|
305
|
+
? [
|
|
306
|
+
{ label: "Break down by this", onSelect: () => put(target.id, "rows"), hint: "Enter" },
|
|
307
|
+
{ label: "Split by this", onSelect: () => put(target.id, "split"), hint: "S" },
|
|
308
|
+
]
|
|
309
|
+
: [{ label: "Measure this", onSelect: () => put(target.id, "values"), hint: "Enter" }]),
|
|
310
|
+
...(onShelf(target.id) ? [{ label: "Take off the shelves", onSelect: () => take(target.id, onShelf(target.id)), hint: "Delete" }] : []),
|
|
311
|
+
...((onShelf(target.id) === "rows" || onShelf(target.id) === "values") && state[onShelf(target.id)].length > 1
|
|
312
|
+
? [
|
|
313
|
+
{ label: "Move earlier", onSelect: () => shift(target, -1), hint: "Alt+←", disabled: state[onShelf(target.id)][0] === target.id },
|
|
314
|
+
{ label: "Move later", onSelect: () => shift(target, 1), hint: "Alt+→", disabled: state[onShelf(target.id)].at(-1) === target.id },
|
|
315
|
+
]
|
|
316
|
+
: []),
|
|
317
|
+
{ separator: true },
|
|
318
|
+
{ label: "Edit", onSelect: () => setEditing({ pill: target, isNew: false }), hint: "E" },
|
|
319
|
+
{ label: "Remove the field", onSelect: () => removeField(target), hint: "Shift+Delete" },
|
|
320
|
+
{ label: "Duplicate", onSelect: () => setEditing({ pill: { ...target, label: `${target.label} copy`, by: editor, model: undefined }, isNew: true }), hint: "D" },
|
|
321
|
+
...(offer(target) ? [{ label: model && [...model.dimensions, ...model.measures, ...model.metrics].some((f) => f.name === target.id) ? "Update it in the model" : "Add to the model", onSelect: () => void promote(target), hint: "M" }] : []),
|
|
322
|
+
];
|
|
323
|
+
// Dragging a pill: pointer events, so a finger and a pen work as a mouse does. A press that does not move is a click.
|
|
324
|
+
// A finger has to hold still on a pill for a moment before it drags it (`TOUCH_HOLD_MS`): a finger that moves at once is scrolling the page,
|
|
325
|
+
// which matters on a phone, where the pills fill the screen. A pen and a mouse drag at once.
|
|
326
|
+
const [ghost, setGhost] = useState();
|
|
327
|
+
const press = (target, from) => (event) => {
|
|
328
|
+
if (event.button !== 0)
|
|
329
|
+
return;
|
|
330
|
+
const start = { x: event.clientX, y: event.clientY };
|
|
331
|
+
let dragging = false;
|
|
332
|
+
let held = event.pointerType !== "touch";
|
|
333
|
+
const shelfAt = (x, y) => document.elementFromPoint(x, y)?.closest("[data-shelf]")?.dataset.shelf;
|
|
334
|
+
// Where among the shelf's other pills the pointer is: before the first whose middle it has not passed, reading line by line.
|
|
335
|
+
const placeAt = (shelf, x, y) => {
|
|
336
|
+
if (shelf === "split")
|
|
337
|
+
return undefined;
|
|
338
|
+
const wraps = Array.from(document.querySelectorAll(`[data-shelf="${shelf}"] [data-pill]`)).filter((w) => w.dataset.pill !== target.id);
|
|
339
|
+
const index = wraps.findIndex((w) => {
|
|
340
|
+
const r = w.getBoundingClientRect();
|
|
341
|
+
return y < r.top || (y <= r.bottom && x < r.left + r.width / 2);
|
|
342
|
+
});
|
|
343
|
+
return index < 0 ? wraps.length : index;
|
|
344
|
+
};
|
|
345
|
+
const hold = held
|
|
346
|
+
? undefined
|
|
347
|
+
: window.setTimeout(() => {
|
|
348
|
+
held = true;
|
|
349
|
+
// Lifted where the finger is, so the person sees it is now theirs to move.
|
|
350
|
+
setGhost({ label: target.label, kind: target.kind, x: start.x, y: start.y, over: shelfAt(start.x, start.y) });
|
|
351
|
+
}, TOUCH_HOLD_MS);
|
|
352
|
+
// Once a finger holds a pill, the page must not scroll under it.
|
|
353
|
+
const still = (e) => held && e.cancelable && e.preventDefault();
|
|
354
|
+
const move = (e) => {
|
|
355
|
+
const far = Math.hypot(e.clientX - start.x, e.clientY - start.y) >= 6;
|
|
356
|
+
if (!held)
|
|
357
|
+
return far ? void stop(e) : undefined;
|
|
358
|
+
if (!dragging && !far)
|
|
359
|
+
return;
|
|
360
|
+
dragging = true;
|
|
361
|
+
const over = shelfAt(e.clientX, e.clientY);
|
|
362
|
+
setGhost({ label: target.label, kind: target.kind, x: e.clientX, y: e.clientY, over, id: target.id, ...(over ? { place: placeAt(over, e.clientX, e.clientY) } : {}) });
|
|
363
|
+
};
|
|
364
|
+
const stop = (e) => {
|
|
365
|
+
window.clearTimeout(hold);
|
|
366
|
+
window.removeEventListener("pointermove", move);
|
|
367
|
+
window.removeEventListener("pointerup", up);
|
|
368
|
+
window.removeEventListener("pointercancel", stop);
|
|
369
|
+
window.removeEventListener("contextmenu", stop);
|
|
370
|
+
window.removeEventListener("touchmove", still);
|
|
371
|
+
window.removeEventListener("keydown", cancel);
|
|
372
|
+
setGhost(undefined);
|
|
373
|
+
return e;
|
|
374
|
+
};
|
|
375
|
+
const up = (e) => {
|
|
376
|
+
stop(e);
|
|
377
|
+
if (!dragging)
|
|
378
|
+
return;
|
|
379
|
+
// The click that follows a drag must not also act.
|
|
380
|
+
// Only the click the browser sends with this pointerup: when none comes (a drop outside any common ancestor), the next real click must still work.
|
|
381
|
+
const swallow = (click) => click.stopPropagation();
|
|
382
|
+
window.addEventListener("click", swallow, { capture: true, once: true });
|
|
383
|
+
setTimeout(() => window.removeEventListener("click", swallow, { capture: true }), 0);
|
|
384
|
+
const shelf = shelfAt(e.clientX, e.clientY);
|
|
385
|
+
if (shelf)
|
|
386
|
+
put(target.id, shelf, placeAt(shelf, e.clientX, e.clientY));
|
|
387
|
+
else if (from)
|
|
388
|
+
take(target.id, from);
|
|
389
|
+
};
|
|
390
|
+
const cancel = (e) => e.key === "Escape" && stop(e);
|
|
391
|
+
window.addEventListener("pointermove", move);
|
|
392
|
+
window.addEventListener("pointerup", up);
|
|
393
|
+
// The browser took the touch for a scroll, or a long press opened the pill's menu: no drag.
|
|
394
|
+
window.addEventListener("pointercancel", stop);
|
|
395
|
+
window.addEventListener("contextmenu", stop);
|
|
396
|
+
window.addEventListener("touchmove", still, { passive: false });
|
|
397
|
+
window.addEventListener("keydown", cancel);
|
|
398
|
+
};
|
|
399
|
+
const pillKeys = (target) => (event) => {
|
|
400
|
+
if (event.key === "s" && target.kind === "attribute")
|
|
401
|
+
put(target.id, "split");
|
|
402
|
+
else if (event.key === "e")
|
|
403
|
+
setEditing({ pill: target, isNew: false });
|
|
404
|
+
else if (event.key === "d")
|
|
405
|
+
setEditing({ pill: { ...target, label: `${target.label} copy`, by: editor, model: undefined }, isNew: true });
|
|
406
|
+
else if (event.key === "m" && offer(target))
|
|
407
|
+
void promote(target);
|
|
408
|
+
else if (event.altKey && (event.key === "ArrowLeft" || event.key === "ArrowUp"))
|
|
409
|
+
shift(target, -1);
|
|
410
|
+
else if (event.altKey && (event.key === "ArrowRight" || event.key === "ArrowDown"))
|
|
411
|
+
shift(target, 1);
|
|
412
|
+
else if ((event.key === "Delete" || event.key === "Backspace") && event.shiftKey)
|
|
413
|
+
removeField(target);
|
|
414
|
+
else if ((event.key === "Delete" || event.key === "Backspace") && onShelf(target.id))
|
|
415
|
+
take(target.id, onShelf(target.id));
|
|
416
|
+
else
|
|
417
|
+
return;
|
|
418
|
+
event.preventDefault();
|
|
419
|
+
};
|
|
420
|
+
/** While dragging over a shelf: true for the pill the dragged one would land before. */
|
|
421
|
+
const insertBefore = (shelf, id) => {
|
|
422
|
+
if (!ghost || ghost.place === undefined || shelf === "split")
|
|
423
|
+
return false;
|
|
424
|
+
const others = state[shelf].filter((item) => item !== ghost.id);
|
|
425
|
+
return others[ghost.place] === id;
|
|
426
|
+
};
|
|
427
|
+
const drawPill = (target, shelf) => {
|
|
428
|
+
const bad = pillProblem(target, state.fields, columns.length > 0 ? columns.map((c) => c.name) : undefined);
|
|
429
|
+
const side = shelf === "values" && chart && shown === "chart" ? sideOf(target.id) : undefined;
|
|
430
|
+
const tag = shelf ? undefined : target.model ? "model" : target.by && target.by !== "the columns" ? (target.by === "suggested from the columns" ? "suggested" : target.by) : undefined;
|
|
431
|
+
return (_jsxs("span", { className: "sf-pill-wrap", "data-pill": shelf ? target.id : undefined, "data-insert": shelf && ghost?.over === shelf && ghost.id !== target.id && insertBefore(shelf, target.id) ? "before" : undefined, "data-kind": target.kind, "data-on": (!shelf && onShelf(target.id)) || undefined, "data-bad": bad ? "true" : undefined, children: [_jsxs("button", { type: "button", className: "sf-pill", "data-kind": target.kind, title: [target.description, bad, target.kind === "derived" ? target.expression : undefined, target.model ? `From the ${model?.label || target.model} model` : undefined, target.by ? `Made by ${target.by}` : undefined].filter(Boolean).join(" · "), "aria-keyshortcuts": `Enter S E D${offer(target) ? " M" : ""} Delete Shift+Delete${shelf === "rows" || shelf === "values" ? " Alt+ArrowLeft Alt+ArrowRight" : ""} Shift+F10`, onPointerDown: press(target, shelf), onClick: () => (shelf ? setEditing({ pill: target, isNew: false }) : put(target.id)), onKeyDown: pillKeys(target), onContextMenu: (event) => menu.open(event, `${target.label} actions`, menuFor(target)), children: [_jsx("span", { className: "sf-pill-glyph", "aria-hidden": "true", children: GLYPH[target.kind] }), _jsxs("span", { className: "sf-pill-label", children: [target.label, " ", _jsx("span", { className: "sf-visually-hidden", children: `(${KIND_WORD[target.kind]}${tag ? `, ${tag === "model" ? "from the model" : `by ${tag}`}` : ""}${side ? `, ${side} scale` : ""}${shelf ? "" : onShelf(target.id) ? ", in use" : ""}${bad ? `, problem: ${bad}` : ""})` })] }), side && (_jsx("span", { className: "sf-pill-side", "data-side": side, "aria-hidden": "true", children: side })), tag && (_jsx("span", { className: "sf-pill-by", "data-model": target.model ? "true" : undefined, "aria-hidden": "true", children: tag }))] }), shelf ? (_jsx("button", { type: "button", className: "sf-pill-x", "aria-label": `Take ${target.label} off “${SHELVES.find((s) => s.id === shelf).label}”`, onClick: () => take(target.id, shelf), children: _jsx(ControlIcon, { icon: "close" }) })) : (_jsxs(_Fragment, { children: [_jsx("button", { type: "button", className: "sf-pill-x", "data-control": "edit", "aria-label": `Edit ${target.label}`, onClick: () => setEditing({ pill: target, isNew: false }), children: _jsx(ControlIcon, { icon: "edit" }) }), onShelf(target.id) && (_jsx("button", { type: "button", className: "sf-pill-x", "aria-label": `Take ${target.label} off “${SHELVES.find((s) => s.id === onShelf(target.id)).label}”`, onClick: () => take(target.id, onShelf(target.id)), children: _jsx(ControlIcon, { icon: "close" }) }))] }))] }, target.id));
|
|
432
|
+
};
|
|
433
|
+
const words = search.trim().toLowerCase();
|
|
434
|
+
const group = (kind) => state.fields.filter((p) => p.kind === kind && (!words || p.label.toLowerCase().includes(words) || p.id.includes(words)));
|
|
435
|
+
const chart = useMemo(() => exploreChart(state), [state]);
|
|
436
|
+
const reading = exploreReading(state);
|
|
437
|
+
const suggest = () => {
|
|
438
|
+
const found = suggestDerived(state.fields);
|
|
439
|
+
if (found.length === 0)
|
|
440
|
+
return setNote("Nothing more to suggest from these columns. Make a calculation of your own with “New calculation”.");
|
|
441
|
+
set({ ...state, fields: [...state.fields, ...found] });
|
|
442
|
+
say("fields.suggested", `${found.length} ${found.length === 1 ? "calculation" : "calculations"} suggested from the column names: ${found.map((p) => p.label).join(", ")}. They are guesses to keep, edit or remove.`, { fields: found });
|
|
443
|
+
};
|
|
444
|
+
return (_jsxs("div", { className: ["sf-explore", props.className].filter(Boolean).join(" "), children: [model?.dataset && _jsx(ModelLineage, { model: model, compact: true }), _jsxs("div", { className: "sf-explore-frame", children: [_jsxs("aside", { className: "sf-explore-fields", "aria-label": "Fields", children: [_jsx("input", { className: "sf-input", type: "search", placeholder: "Find a field", "aria-label": "Find a field", value: search, onChange: (e) => setSearch(e.target.value) }), ["attribute", "metric"].map((kind) => (_jsxs("section", { "aria-label": kind === "attribute" ? "Attributes" : "Metrics", children: [_jsxs("h4", { className: "sf-explore-heading", children: [kind === "attribute" ? "Attributes" : kind === "metric" ? "Metrics" : "Calculations", _jsx("button", { type: "button", className: "sf-explore-add", "aria-label": `New ${KIND_WORD[kind]}`, onClick: () => setEditing({ pill: blank(kind), isNew: true }), children: _jsx(ControlIcon, { icon: "add" }) })] }), _jsxs("div", { className: "sf-explore-pills", children: [group(kind).map((p) => drawPill(p)), group(kind).length === 0 && _jsx("p", { className: "sf-explore-hint", children: words ? "None match." : "None." })] })] }, kind))), _jsxs("details", { className: "sf-explore-more", open: calculationsOpen || (!!words && group("derived").length > 0), onToggle: (e) => setCalculationsOpen(e.currentTarget.open), children: [_jsxs("summary", { children: ["Calculations", state.fields.some((p) => p.kind === "derived") && _jsx("span", { className: "sf-explore-count", children: state.fields.filter((p) => p.kind === "derived").length })] }), _jsxs("section", { "aria-label": "Calculations", children: [_jsxs("h4", { className: "sf-explore-heading", children: ["Calculations", _jsx("button", { type: "button", className: "sf-explore-add", "aria-label": `New ${KIND_WORD.derived}`, onClick: () => setEditing({ pill: blank("derived"), isNew: true }), children: _jsx(ControlIcon, { icon: "add" }) })] }), _jsxs("div", { className: "sf-explore-pills", children: [group("derived").map((p) => drawPill(p)), group("derived").length === 0 && _jsx("p", { className: "sf-explore-hint", children: words ? "None match." : "None yet. A calculation is arithmetic over metrics: a rate, a margin, an average." })] })] }), _jsx("button", { type: "button", className: "sf-button", onClick: suggest, children: "Suggest calculations" })] }), editing && (_jsx(PillEditor, { pill: editing.pill, all: state.fields, columns: columns, isNew: editing.isNew, modelLabel: editing.pill.model ? model?.label || editing.pill.model : undefined, onPromote: !editing.isNew && offer(editing.pill) ? () => (void promote(editing.pill), setEditing(undefined)) : undefined, onCancel: () => setEditing(undefined), onSave: (next) => {
|
|
445
|
+
// Changed by hand, it is no longer the model's definition until it is added again.
|
|
446
|
+
const changed = { ...next, by: editor, model: undefined };
|
|
447
|
+
set(setPill(state, editing.isNew ? changed.id : editing.pill.id, changed));
|
|
448
|
+
say(editing.isNew ? "field.added" : "field.changed", `${changed.label} ${editing.isNew ? "added" : "saved"}.`, { field: changed, ...(editing.isNew ? {} : { was: editing.pill }) });
|
|
449
|
+
setEditing(undefined);
|
|
450
|
+
}, onRemove: editing.isNew
|
|
451
|
+
? undefined
|
|
452
|
+
: () => {
|
|
453
|
+
set(setPill(state, editing.pill.id, undefined));
|
|
454
|
+
say("field.removed", `${editing.pill.label} removed.`, { field: editing.pill });
|
|
455
|
+
setEditing(undefined);
|
|
456
|
+
} }, `${editing.pill.id}-${editing.isNew}`))] }), _jsxs("div", { className: "sf-explore-main", children: [onAsk && (_jsxs("form", { className: "sf-explore-ask", onSubmit: (event) => {
|
|
457
|
+
event.preventDefault();
|
|
458
|
+
if (!ask.trim())
|
|
459
|
+
return;
|
|
460
|
+
onAsk(ask.trim(), state);
|
|
461
|
+
say("asked", `Asked: “${ask.trim()}”.`, { text: ask.trim() });
|
|
462
|
+
setAsk("");
|
|
463
|
+
}, children: [_jsx("input", { className: "sf-input", "aria-label": "Ask for a field or a view", placeholder: "Ask for a field or a view: refund rate by region, split by channel", value: ask, onChange: (e) => setAsk(e.target.value) }), _jsx("button", { type: "submit", className: "sf-button", disabled: !ask.trim(), children: "Ask" })] })), _jsx("div", { className: "sf-explore-shelves", children: SHELVES.map((shelf) => {
|
|
464
|
+
const ids = shelf.id === "split" ? (state.split ? [state.split] : []) : state[shelf.id];
|
|
465
|
+
return (_jsxs("div", { className: "sf-explore-shelf", "data-shelf": shelf.id, "data-over": ghost?.over === shelf.id || undefined, "data-accepts": ghost ? shelf.takes.includes(ghost.kind) : undefined, role: "group", "aria-label": shelf.label, children: [_jsx("span", { className: "sf-explore-shelf-name", title: shelf.hint, children: shelf.label }), chart && shown === "chart" && (shelf.id !== "split" || state.values.length === 1) && (_jsx("span", { className: "sf-explore-axis", "data-axis": shelf.id === "split" ? "colour" : axisOf[shelf.id] === "X axis" ? "x" : "y", title: shelf.id === "values" && twoScales ? "Each measure's pill says which side's scale it is read against." : undefined, children: axisOf[shelf.id] })), ids.map((id) => pill(id)).filter((p) => !!p).map((p) => drawPill(p, shelf.id)), ids.length === 0 && _jsx("span", { className: "sf-explore-hint", children: ghost ? (shelf.takes.includes(ghost.kind) ? "Drop here" : "Not for this field") : shelf.id === "values" ? "Press a metric" : shelf.id === "rows" ? "Press an attribute" : "Optional" })] }, shelf.id));
|
|
466
|
+
}) }), chart && (_jsxs("div", { className: "sf-explore-chart-settings", children: [_jsxs("div", { className: "sf-segmented sf-result-views", role: "group", "aria-label": "Draw as", children: [["bar", "line", "area"].map((type) => {
|
|
467
|
+
const on = shown === "chart" && chartTypeNow === type;
|
|
468
|
+
return (_jsx("button", { type: "button", className: "sf-segment", "data-checked": on || undefined, "aria-pressed": on, onClick: () => setType(type), children: type === "bar" ? "Bars" : type === "line" ? "Line" : "Area" }, type));
|
|
469
|
+
}), _jsx("button", { type: "button", className: "sf-segment", "data-checked": shown === "table" || undefined, "aria-pressed": shown === "table", onClick: showTable, children: "Table" })] }), shown === "chart" && (_jsxs(_Fragment, { children: [_jsx("button", { type: "button", className: "sf-button", "aria-pressed": swapped, "aria-disabled": !!cannotSwap || undefined, title: cannotSwap, "aria-describedby": cannotSwap ? `${uid}-swap` : undefined, onClick: () => !cannotSwap && swap(), children: "Swap axes" }), cannotSwap && (_jsx("span", { id: `${uid}-swap`, className: "sf-explore-hint", children: cannotSwap })), state.values.length > 1 && (_jsx("ul", { className: "sf-explore-measures", "aria-label": "Each measure", children: state.values.map((id) => pill(id)).filter((p) => !!p).map((p) => (_jsxs("li", { children: [_jsx("span", { className: "sf-explore-measure-name", children: p.label }), _jsxs("select", { className: "sf-input", "aria-label": `Draw ${p.label} as`, value: styleOf(p.id).type ?? chartTypeNow, onChange: (e) => restyle(p, { type: e.target.value }), children: [_jsx("option", { value: "bar", children: "Bars" }), _jsx("option", { value: "line", children: "Line" }), _jsx("option", { value: "area", children: "Area" })] }), _jsxs("select", { className: "sf-input", "aria-label": `Scale for ${p.label}`, value: styleOf(p.id).axis ?? "left", onChange: (e) => restyle(p, { axis: e.target.value }), children: [_jsx("option", { value: "left", children: "Left scale" }), _jsx("option", { value: "right", children: "Right scale" })] })] }, p.id))) }))] }))] })), _jsxs("details", { className: "sf-explore-more", open: filtersOpen, onToggle: (e) => setFiltersOpen(e.currentTarget.open), children: [_jsxs("summary", { children: ["Filters", state.filters.length > 0 && _jsx("span", { className: "sf-explore-count", children: state.filters.length })] }), _jsx(QueryFilters, { sourceId: sourceId, sql: sql, filters: state.filters, onChange: (next) => set({ ...state, filters: next }), onContext: props.onContext, dataSource: dataSource })] }), problem && (_jsx("p", { className: "sf-filter-problem", role: "alert", children: problem })), state.values.length === 0 ? (_jsx("p", { className: "sf-explore-empty", children: "Press a metric to measure it, then an attribute to break it down. Each press, drag or edit asks the database again; nothing is worked out in this page." })) : (_jsxs("div", { className: "sf-explore-result sf-controls-host", "data-controls": "corner", children: [props.controls?.length ? _jsx(ItemControls, { label: `${reading || "View"} controls`, controls: props.controls }) : null, _jsx("p", { className: "sf-explore-reading", children: reading }), !chart && _jsx("p", { className: "sf-explore-hint", children: "With nothing to break down by there is one number each. Press an attribute for a chart." }), _jsx(ExploreView, { dataSource: dataSource, sourceId: sourceId, sql: sql, state: state, view: shown, height: chartHeight, label: reading }), props.actions && _jsx("div", { className: "sf-explore-actions", children: props.actions })] })), _jsx("p", { className: "sf-explore-note", role: "status", children: note })] })] }), ghost && (_jsxs("span", { className: "sf-pill sf-pill-ghost", "data-kind": ghost.kind, "aria-hidden": "true", style: { left: ghost.x, top: ghost.y }, children: [_jsx("span", { className: "sf-pill-glyph", children: GLYPH[ghost.kind] }), ghost.label] })), menu.element] }));
|
|
470
|
+
}
|
|
471
|
+
//# sourceMappingURL=Explorer.js.map
|