@malloy-publisher/sdk 0.2.7 → 0.3.1
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 +180 -204
- package/dist/DocumentStorageProvider-BeBD0uWc.cjs.js +134 -0
- package/dist/DocumentStorageProvider-tGvodI8f.es.js +24268 -0
- package/dist/RenderedResult-CPi6uQWq.cjs.js +115 -0
- package/dist/RenderedResult-mDE-4u8S.es.js +542 -0
- package/dist/{ServerProvider-xXW1HJOJ.cjs.js → ServerProvider-CH71GsoL.cjs.js} +1 -1
- package/dist/{ServerProvider-ksC5Su_x.es.js → ServerProvider-X2Ma0HRs.es.js} +324 -359
- package/dist/builder/index.cjs.js +105 -0
- package/dist/builder/index.d.ts +2 -0
- package/dist/builder/index.es.js +9561 -0
- package/dist/builder-entry.d.ts +8 -0
- package/dist/client/api.d.ts +13 -1
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +1 -1
- package/dist/components/ApiErrorDisplay.d.ts +1 -2
- package/dist/components/Dashboard/Dashboard.d.ts +23 -16
- package/dist/components/Dashboard/DashboardGrid.d.ts +67 -0
- package/dist/components/Dashboard/DashboardTile.d.ts +6 -1
- package/dist/components/Dashboard/ExploreDialog.d.ts +18 -0
- package/dist/components/Dashboard/RowsDialog.d.ts +47 -0
- package/dist/components/Dashboard/TileCard.d.ts +44 -0
- package/dist/components/Dashboard/index.d.ts +2 -0
- package/dist/components/Dashboard/promoteMeasureRow.d.ts +40 -0
- package/dist/components/Dashboard/telemetry.d.ts +43 -0
- package/dist/components/DashboardBuilder/AddTileDialog.d.ts +35 -0
- package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +29 -0
- package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +112 -0
- package/dist/components/DashboardBuilder/DashboardEditor.d.ts +32 -0
- package/dist/components/DashboardBuilder/DiffDialog.d.ts +15 -0
- package/dist/components/DashboardBuilder/DrillDialog.d.ts +15 -0
- package/dist/components/DashboardBuilder/FilterDialog.d.ts +54 -0
- package/dist/components/DashboardBuilder/FilterStrip.d.ts +17 -0
- package/dist/components/DashboardBuilder/SettingsPopover.d.ts +20 -0
- package/dist/components/DashboardBuilder/TileFrame.d.ts +42 -0
- package/dist/components/DashboardBuilder/TileMenu.d.ts +24 -0
- package/dist/components/DashboardBuilder/catalog.d.ts +62 -0
- package/dist/components/DashboardBuilder/controls.d.ts +157 -0
- package/dist/components/DashboardBuilder/diff.d.ts +35 -0
- package/dist/components/DashboardBuilder/document.d.ts +218 -0
- package/dist/components/DashboardBuilder/layout.d.ts +71 -0
- package/dist/components/DashboardBuilder/malloyText.d.ts +45 -0
- package/dist/components/DashboardBuilder/preview.d.ts +60 -0
- package/dist/components/DashboardBuilder/readDocument.d.ts +106 -0
- package/dist/components/DashboardBuilder/sortable.d.ts +51 -0
- package/dist/components/DashboardBuilder/spliceDocument.d.ts +44 -0
- package/dist/components/DashboardBuilder/testing/fixtures.d.ts +10 -0
- package/dist/components/DashboardBuilder/useBuilderShortcuts.d.ts +22 -0
- package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +63 -0
- package/dist/components/DashboardBuilder/useDraft.d.ts +13 -0
- package/dist/components/DashboardBuilder/useFilterForm.d.ts +65 -0
- package/dist/components/DashboardBuilder/useTileReorder.d.ts +22 -0
- package/dist/components/DashboardBuilder/useTileResize.d.ts +29 -0
- package/dist/components/DocumentStorage/BrowserDocumentStorage.d.ts +14 -0
- package/dist/components/DocumentStorage/DocumentStorage.d.ts +58 -0
- package/dist/components/DocumentStorage/DocumentStorageProvider.d.ts +25 -0
- package/dist/components/DocumentStorage/index.d.ts +3 -0
- package/dist/components/FloatingIconButton.d.ts +16 -0
- package/dist/components/Model/index.d.ts +0 -1
- package/dist/components/Prose.d.ts +46 -0
- package/dist/components/RenderedResult/RenderedResult.d.ts +12 -0
- package/dist/components/RenderedResult/ResultContainer.d.ts +7 -1
- package/dist/components/RenderedResult/ResultPanel.d.ts +30 -0
- package/dist/components/RenderedResult/index.d.ts +2 -0
- package/dist/components/RenderedResult/resultSizing.d.ts +183 -0
- package/dist/components/drill/index.d.ts +2 -1
- package/dist/components/drill/resolveDrill.d.ts +22 -0
- package/dist/components/drill/useDrill.d.ts +11 -2
- package/dist/components/drill/useDrillSelf.d.ts +46 -0
- package/dist/components/given/filterValue.d.ts +63 -0
- package/dist/components/given/index.d.ts +1 -0
- package/dist/components/given/useGivenUrlParams.d.ts +39 -0
- package/dist/components/index.d.ts +3 -3
- package/dist/components/styles.d.ts +3 -7
- package/dist/core-8NVBCLZh.cjs.js +148 -0
- package/dist/{core-BY7vB5gN.es.js → core-B8oMW4sm.es.js} +3540 -2335
- package/dist/hooks/index.d.ts +3 -1
- package/dist/hooks/useDocumentControls.d.ts +70 -0
- package/dist/hooks/useQueryResult.d.ts +40 -0
- package/dist/hooks/useSuggestOptions.d.ts +28 -12
- package/dist/index.cjs.js +34 -1
- package/dist/index.es.js +21487 -79
- package/dist/sdk.css +1 -1
- package/dist/styles.css +0 -1
- package/dist/utils/malloyLiteral.d.ts +11 -0
- package/package.json +25 -14
- package/src/builder-entry.ts +55 -0
- package/src/components/ApiErrorDisplay.tsx +1 -1
- package/src/components/Connections/EditConnectionDialog.tsx +96 -86
- package/src/components/Dashboard/Dashboard.spec.tsx +7 -10
- package/src/components/Dashboard/Dashboard.tsx +184 -229
- package/src/components/Dashboard/DashboardGrid.tsx +124 -0
- package/src/components/Dashboard/DashboardTile.spec.tsx +30 -6
- package/src/components/Dashboard/DashboardTile.tsx +67 -101
- package/src/components/Dashboard/ExploreDialog.tsx +84 -0
- package/src/components/Dashboard/RowsDialog.spec.ts +42 -0
- package/src/components/Dashboard/RowsDialog.tsx +234 -0
- package/src/components/Dashboard/TileCard.tsx +120 -0
- package/src/components/Dashboard/index.ts +2 -0
- package/src/components/Dashboard/promoteMeasureRow.spec.ts +207 -0
- package/src/components/Dashboard/promoteMeasureRow.ts +160 -0
- package/src/components/Dashboard/telemetry.ts +44 -0
- package/src/components/Dashboard/tileGridColumn.spec.ts +1 -1
- package/src/components/DashboardBuilder/AddTileDialog.tsx +249 -0
- package/src/components/DashboardBuilder/BuilderToolbar.tsx +270 -0
- package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +858 -0
- package/src/components/DashboardBuilder/DashboardBuilder.tsx +653 -0
- package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +211 -0
- package/src/components/DashboardBuilder/DashboardEditor.tsx +493 -0
- package/src/components/DashboardBuilder/DiffDialog.tsx +129 -0
- package/src/components/DashboardBuilder/DrillDialog.tsx +281 -0
- package/src/components/DashboardBuilder/FilterDialog.tsx +581 -0
- package/src/components/DashboardBuilder/FilterStrip.tsx +125 -0
- package/src/components/DashboardBuilder/SettingsPopover.tsx +168 -0
- package/src/components/DashboardBuilder/TileFrame.tsx +380 -0
- package/src/components/DashboardBuilder/TileMenu.tsx +188 -0
- package/src/components/DashboardBuilder/catalog.spec.ts +215 -0
- package/src/components/DashboardBuilder/catalog.ts +225 -0
- package/src/components/DashboardBuilder/controls.spec.ts +243 -0
- package/src/components/DashboardBuilder/controls.ts +398 -0
- package/src/components/DashboardBuilder/diff.spec.ts +44 -0
- package/src/components/DashboardBuilder/diff.ts +93 -0
- package/src/components/DashboardBuilder/document.ts +209 -0
- package/src/components/DashboardBuilder/keepRowStructure.spec.ts +84 -0
- package/src/components/DashboardBuilder/layout.ts +143 -0
- package/src/components/DashboardBuilder/malloyText.spec.ts +84 -0
- package/src/components/DashboardBuilder/malloyText.ts +151 -0
- package/src/components/DashboardBuilder/moveIntoGap.spec.ts +90 -0
- package/src/components/DashboardBuilder/preview.spec.ts +149 -0
- package/src/components/DashboardBuilder/preview.ts +172 -0
- package/src/components/DashboardBuilder/readDocument.spec.ts +375 -0
- package/src/components/DashboardBuilder/readDocument.ts +467 -0
- package/src/components/DashboardBuilder/sortable.tsx +151 -0
- package/src/components/DashboardBuilder/spliceDocument.spec.ts +1108 -0
- package/src/components/DashboardBuilder/spliceDocument.ts +1098 -0
- package/src/components/DashboardBuilder/testing/fixtures.ts +48 -0
- package/src/components/DashboardBuilder/useBuilderShortcuts.ts +88 -0
- package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +255 -0
- package/src/components/DashboardBuilder/useDashboardEditor.ts +193 -0
- package/src/components/DashboardBuilder/useDraft.ts +43 -0
- package/src/components/DashboardBuilder/useFilterForm.spec.tsx +144 -0
- package/src/components/DashboardBuilder/useFilterForm.ts +352 -0
- package/src/components/DashboardBuilder/useTileReorder.spec.tsx +97 -0
- package/src/components/DashboardBuilder/useTileReorder.ts +115 -0
- package/src/components/DashboardBuilder/useTileResize.spec.tsx +121 -0
- package/src/components/DashboardBuilder/useTileResize.ts +110 -0
- package/src/components/DashboardBuilder/withGaps.spec.ts +96 -0
- package/src/components/DocumentStorage/BrowserDocumentStorage.spec.ts +81 -0
- package/src/components/DocumentStorage/BrowserDocumentStorage.ts +105 -0
- package/src/components/DocumentStorage/DocumentStorage.ts +73 -0
- package/src/components/DocumentStorage/DocumentStorageProvider.tsx +58 -0
- package/src/components/DocumentStorage/index.ts +16 -0
- package/src/components/Environment/About.tsx +4 -36
- package/src/components/FloatingIconButton.tsx +41 -0
- package/src/components/Model/Model.tsx +6 -23
- package/src/components/Model/ModelCell.tsx +34 -72
- package/src/components/Model/index.ts +0 -1
- package/src/components/Notebook/Notebook.tsx +27 -156
- package/src/components/Notebook/NotebookCell.tsx +37 -191
- package/src/components/Prose.spec.tsx +85 -0
- package/src/components/Prose.tsx +179 -0
- package/src/components/QueryResult/QueryResult.tsx +15 -43
- package/src/components/RenderedResult/RenderedResult.tsx +86 -76
- package/src/components/RenderedResult/ResultContainer.tsx +25 -36
- package/src/components/RenderedResult/ResultPanel.spec.tsx +49 -0
- package/src/components/RenderedResult/ResultPanel.tsx +73 -0
- package/src/components/RenderedResult/index.ts +16 -0
- package/src/components/RenderedResult/resultSizing.spec.ts +249 -0
- package/src/components/RenderedResult/resultSizing.ts +308 -0
- package/src/components/ResultsDialog.tsx +2 -1
- package/src/components/drill/index.ts +6 -0
- package/src/components/drill/resolveDrill.ts +22 -1
- package/src/components/drill/useDrill.spec.tsx +94 -0
- package/src/components/drill/useDrill.tsx +52 -12
- package/src/components/drill/useDrillSelf.spec.tsx +64 -0
- package/src/components/drill/useDrillSelf.ts +110 -0
- package/src/components/given/GivenInput.spec.tsx +196 -2
- package/src/components/given/GivenInput.tsx +300 -25
- package/src/components/given/GivensPanel.tsx +1 -1
- package/src/components/given/filterValue.spec.ts +112 -0
- package/src/components/given/filterValue.ts +188 -0
- package/src/components/given/index.ts +4 -0
- package/src/components/given/useGivenUrlParams.spec.tsx +74 -0
- package/src/components/given/useGivenUrlParams.ts +87 -0
- package/src/components/index.ts +11 -3
- package/src/components/styles.ts +0 -37
- package/src/hooks/index.ts +13 -1
- package/src/hooks/useDocumentControls.ts +170 -0
- package/src/hooks/useGivensState.spec.ts +158 -0
- package/src/hooks/useGivensState.ts +114 -23
- package/src/hooks/useQueryResult.ts +82 -0
- package/src/hooks/useSuggestOptions.dom.spec.ts +60 -1
- package/src/hooks/useSuggestOptions.ts +94 -51
- package/src/utils/malloyLiteral.spec.ts +43 -0
- package/src/utils/malloyLiteral.ts +76 -0
- package/vite.config.ts +5 -1
- package/dist/components/AnalyzePackageButton.d.ts +0 -6
- package/dist/components/Workbook/BrowserWorkbookStorage.d.ts +0 -9
- package/dist/components/Workbook/EditableMalloyCell.d.ts +0 -11
- package/dist/components/Workbook/ModelPicker.d.ts +0 -11
- package/dist/components/Workbook/MutableCell.d.ts +0 -17
- package/dist/components/Workbook/Workbook.d.ts +0 -7
- package/dist/components/Workbook/WorkbookList.d.ts +0 -7
- package/dist/components/Workbook/WorkbookManager.d.ts +0 -84
- package/dist/components/Workbook/WorkbookStorage.d.ts +0 -17
- package/dist/components/Workbook/WorkbookStorageProvider.d.ts +0 -12
- package/dist/components/Workbook/index.d.ts +0 -7
- package/dist/core-CWKmWEg2.cjs.js +0 -148
- package/dist/index-B0WyZAUA.es.js +0 -66412
- package/dist/index-kxVD1RjY.cjs.js +0 -356
- package/dist/markdown-editor.css +0 -341
- package/src/components/AnalyzePackageButton.tsx +0 -319
- package/src/components/Workbook/BrowserWorkbookStorage.ts +0 -64
- package/src/components/Workbook/EditableMalloyCell.tsx +0 -49
- package/src/components/Workbook/ModelPicker.tsx +0 -134
- package/src/components/Workbook/MutableCell.tsx +0 -458
- package/src/components/Workbook/Workbook.tsx +0 -501
- package/src/components/Workbook/WorkbookList.tsx +0 -110
- package/src/components/Workbook/WorkbookManager.ts +0 -214
- package/src/components/Workbook/WorkbookStorage.ts +0 -35
- package/src/components/Workbook/WorkbookStorageProvider.tsx +0 -40
- package/src/components/Workbook/index.ts +0 -10
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { act, renderHook } from "@testing-library/react";
|
|
5
|
+
import { describe, expect, it, mock } from "bun:test";
|
|
6
|
+
import type { CatalogField } from "./catalog";
|
|
7
|
+
import { controlsOf, type BuilderControl, type MappingRow } from "./controls";
|
|
8
|
+
import type { DashboardDocument, DashboardTile, LocalGiven } from "./document";
|
|
9
|
+
import { openDocument } from "./testing/fixtures";
|
|
10
|
+
import { useFilterForm } from "./useFilterForm";
|
|
11
|
+
|
|
12
|
+
const SOURCE = `##! experimental.givens
|
|
13
|
+
## artifact { title="T" tiles=["a -> by_cat", "a -> by_brand", "orders -> by_region"] }
|
|
14
|
+
import { orders } from "../m.malloy"
|
|
15
|
+
|
|
16
|
+
given: CATEGORY :: filter<string> is f''
|
|
17
|
+
|
|
18
|
+
source: a is orders extend {
|
|
19
|
+
view: by_cat is by_category + { where: cat ~ $CATEGORY }
|
|
20
|
+
view: by_brand is by_brand_view
|
|
21
|
+
}`;
|
|
22
|
+
|
|
23
|
+
const FIELDS: CatalogField[] = [
|
|
24
|
+
{ name: "cat", kind: "dimension", type: "string_type" },
|
|
25
|
+
{ name: "brand", kind: "dimension", type: "string_type" },
|
|
26
|
+
{ name: "amount", kind: "dimension", type: "number_type" },
|
|
27
|
+
{ name: "created_at", kind: "dimension", type: "date_type" },
|
|
28
|
+
];
|
|
29
|
+
const fieldsFor = (tile: DashboardTile) =>
|
|
30
|
+
tile.source === "a" ? FIELDS : undefined;
|
|
31
|
+
|
|
32
|
+
const form = async (
|
|
33
|
+
document: DashboardDocument,
|
|
34
|
+
control?: BuilderControl,
|
|
35
|
+
available: BuilderControl[] = [],
|
|
36
|
+
) => {
|
|
37
|
+
const onApply = mock(
|
|
38
|
+
(_given: string, _rows: MappingRow[], _declare?: LocalGiven) => {},
|
|
39
|
+
);
|
|
40
|
+
const view = renderHook(() =>
|
|
41
|
+
useFilterForm({
|
|
42
|
+
open: true,
|
|
43
|
+
document,
|
|
44
|
+
control,
|
|
45
|
+
available,
|
|
46
|
+
fieldsFor,
|
|
47
|
+
onApply,
|
|
48
|
+
}),
|
|
49
|
+
);
|
|
50
|
+
return { view, onApply };
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
describe("useFilterForm: a new control", () => {
|
|
54
|
+
it("starts on every tile that can take a binding, and names itself after its field", async () => {
|
|
55
|
+
const { view } = await form(await openDocument(SOURCE));
|
|
56
|
+
const { current } = view.result;
|
|
57
|
+
expect(current.editing).toBe(false);
|
|
58
|
+
expect(current.rows.map((row) => row.include)).toEqual([
|
|
59
|
+
true,
|
|
60
|
+
true,
|
|
61
|
+
false,
|
|
62
|
+
]);
|
|
63
|
+
expect(current.bindableCount).toBe(2);
|
|
64
|
+
expect(current.canApply).toBe(false);
|
|
65
|
+
|
|
66
|
+
act(() => view.result.current.pickField("brand"));
|
|
67
|
+
expect(view.result.current.newName).toBe("BRAND");
|
|
68
|
+
expect(view.result.current.canApply).toBe(true);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("follows the field's type to the kind of control, and the comparison it needs", async () => {
|
|
72
|
+
const { view } = await form(await openDocument(SOURCE));
|
|
73
|
+
// A number range is still a filter expression, compared with `~`.
|
|
74
|
+
act(() => view.result.current.pickField("amount"));
|
|
75
|
+
expect(view.result.current.kind).toBe("number");
|
|
76
|
+
expect(view.result.current.valueTyped).toBe(false);
|
|
77
|
+
// A date is a value, and a value needs a comparison.
|
|
78
|
+
act(() => view.result.current.pickField("created_at"));
|
|
79
|
+
expect(view.result.current.kind).toBe("date");
|
|
80
|
+
expect(view.result.current.valueTyped).toBe(true);
|
|
81
|
+
expect(view.result.current.commonOp).toBe(">=");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("holds Apply while a ticked tile cannot take the field, and says which", async () => {
|
|
85
|
+
const { view } = await form(await openDocument(SOURCE));
|
|
86
|
+
act(() => view.result.current.pickField("nope"));
|
|
87
|
+
expect(view.result.current.canApply).toBe(false);
|
|
88
|
+
expect(view.result.current.commonProblem).toContain("Not a field of");
|
|
89
|
+
act(() => view.result.current.setAll(false));
|
|
90
|
+
act(() => view.result.current.setRow(1, { include: true }));
|
|
91
|
+
expect(view.result.current.included).toBe(1);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("declares the control it applies, suggesting over the ticked tile's source", async () => {
|
|
95
|
+
const { view, onApply } = await form(await openDocument(SOURCE));
|
|
96
|
+
act(() => view.result.current.pickField("brand"));
|
|
97
|
+
act(() => view.result.current.setLabel("Brand name"));
|
|
98
|
+
act(() => view.result.current.apply());
|
|
99
|
+
expect(onApply).toHaveBeenCalledTimes(1);
|
|
100
|
+
const [given, rows, declared] = onApply.mock.calls[0];
|
|
101
|
+
expect(given).toBe("BRAND");
|
|
102
|
+
expect(rows.filter((row) => row.include).map((row) => row.field)).toEqual(
|
|
103
|
+
["brand", "brand"],
|
|
104
|
+
);
|
|
105
|
+
expect(declared?.label).toBe("Brand name");
|
|
106
|
+
expect(declared?.suggest?.source).toBe("orders");
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe("useFilterForm: an existing control", () => {
|
|
111
|
+
it("opens on what the control binds now, and unticking a row unbinds it", async () => {
|
|
112
|
+
const document = await openDocument(SOURCE);
|
|
113
|
+
const [category] = controlsOf(document);
|
|
114
|
+
const { view, onApply } = await form(document, category);
|
|
115
|
+
expect(view.result.current.editing).toBe(true);
|
|
116
|
+
expect(view.result.current.rows[0]).toMatchObject({
|
|
117
|
+
include: true,
|
|
118
|
+
field: "cat",
|
|
119
|
+
});
|
|
120
|
+
expect(view.result.current.rows[1].include).toBe(false);
|
|
121
|
+
act(() => view.result.current.setRow(0, { include: false }));
|
|
122
|
+
act(() => view.result.current.apply());
|
|
123
|
+
const [, rows, declared] = onApply.mock.calls[0];
|
|
124
|
+
expect(rows.every((row) => !row.include)).toBe(true);
|
|
125
|
+
// The declaration is left as it is when only the bindings changed.
|
|
126
|
+
expect(declared).toBeUndefined();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("opens per tile when the bound tiles disagree on the field", async () => {
|
|
130
|
+
const source = SOURCE.replace(
|
|
131
|
+
"view: by_brand is by_brand_view",
|
|
132
|
+
"view: by_brand is by_brand_view + { where: brand ~ $CATEGORY }",
|
|
133
|
+
);
|
|
134
|
+
const document = await openDocument(source);
|
|
135
|
+
const [category] = controlsOf(document);
|
|
136
|
+
const { view } = await form(document, category);
|
|
137
|
+
expect(view.result.current.perTile).toBe(true);
|
|
138
|
+
expect(view.result.current.rows.map((row) => row.field)).toEqual([
|
|
139
|
+
"cat",
|
|
140
|
+
"brand",
|
|
141
|
+
"category",
|
|
142
|
+
]);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
5
|
+
import type { CatalogField } from "./catalog";
|
|
6
|
+
import {
|
|
7
|
+
acceptsField,
|
|
8
|
+
canBind,
|
|
9
|
+
defaultOperator,
|
|
10
|
+
givenNameFor,
|
|
11
|
+
kindForFieldType,
|
|
12
|
+
mappingOf,
|
|
13
|
+
newLocalGiven,
|
|
14
|
+
typeLabel,
|
|
15
|
+
type BuilderControl,
|
|
16
|
+
type ControlKind,
|
|
17
|
+
type MappingRow,
|
|
18
|
+
} from "./controls";
|
|
19
|
+
import type { DashboardDocument, DashboardTile, LocalGiven } from "./document";
|
|
20
|
+
|
|
21
|
+
type Source = { kind: "existing"; name: string } | { kind: "new" };
|
|
22
|
+
|
|
23
|
+
/** `STATUS` → `Status`: the label a new control gets when none is typed. */
|
|
24
|
+
export const titleCase = (name: string) =>
|
|
25
|
+
name.charAt(0) + name.slice(1).toLowerCase().replace(/_/g, " ");
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The filter window's state and every derivation it renders from: what the
|
|
29
|
+
* control is (new here, or one the model offers), which tiles it binds and on
|
|
30
|
+
* which field, what stands in the way of applying, and the payload an Apply
|
|
31
|
+
* hands back. No MUI in here, so it can be exercised without a dialog.
|
|
32
|
+
*/
|
|
33
|
+
export function useFilterForm({
|
|
34
|
+
open,
|
|
35
|
+
document,
|
|
36
|
+
control,
|
|
37
|
+
available,
|
|
38
|
+
fieldsFor,
|
|
39
|
+
onApply,
|
|
40
|
+
}: {
|
|
41
|
+
open: boolean;
|
|
42
|
+
document: DashboardDocument;
|
|
43
|
+
control: BuilderControl | undefined;
|
|
44
|
+
available: readonly BuilderControl[];
|
|
45
|
+
fieldsFor?: (tile: DashboardTile) => readonly CatalogField[] | undefined;
|
|
46
|
+
onApply: (given: string, rows: MappingRow[], declare?: LocalGiven) => void;
|
|
47
|
+
}) {
|
|
48
|
+
// What the window SHOWS: the control while open, and the same control while
|
|
49
|
+
// it fades out. The builder drops `control` the instant the window closes,
|
|
50
|
+
// and without this the title flipped to "Add a filter" for the length of
|
|
51
|
+
// the exit transition.
|
|
52
|
+
const shownRef = useRef(control);
|
|
53
|
+
if (open) shownRef.current = control;
|
|
54
|
+
const shown = open ? control : shownRef.current;
|
|
55
|
+
const editing = shown !== undefined;
|
|
56
|
+
|
|
57
|
+
const [source, setSource] = useState<Source>({ kind: "new" });
|
|
58
|
+
const [label, setLabel] = useState("");
|
|
59
|
+
const [kind, setKind] = useState<ControlKind>("select");
|
|
60
|
+
const [dateDefault, setDateDefault] = useState("");
|
|
61
|
+
// The field every ticked tile filters on, and the rows themselves. The
|
|
62
|
+
// field is the common case and, until "Adjust per tile" is pressed, the ONE
|
|
63
|
+
// that is applied: a row's own field only counts once the author has asked
|
|
64
|
+
// to set them one by one. That is what keeps a fallback name a row happened
|
|
65
|
+
// to carry from being written when the box above it was empty.
|
|
66
|
+
const [field, setField] = useState("");
|
|
67
|
+
const [rows, setRows] = useState<MappingRow[]>([]);
|
|
68
|
+
const [perTile, setPerTile] = useState(false);
|
|
69
|
+
|
|
70
|
+
/** The model source a tile's extension is built on, which its fields live on. */
|
|
71
|
+
const baseOf = (tile: DashboardTile) =>
|
|
72
|
+
document.sources.find((source) => source.name === tile.source)?.base ??
|
|
73
|
+
tile.source;
|
|
74
|
+
|
|
75
|
+
// Reset on open, on what is true now.
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (!open) return;
|
|
78
|
+
if (control) {
|
|
79
|
+
setSource({ kind: "existing", name: control.name });
|
|
80
|
+
setLabel(control.label ?? "");
|
|
81
|
+
const mapped = mappingOf(document, control);
|
|
82
|
+
setRows(mapped);
|
|
83
|
+
// One field when every bound tile agrees; otherwise the rows already
|
|
84
|
+
// differ and the per-tile view is the honest one to open on. A control
|
|
85
|
+
// bound nowhere starts from what its own suggest names, or empty — and
|
|
86
|
+
// empty has to be filled before it can apply.
|
|
87
|
+
const bound = mapped.filter((row) => row.include);
|
|
88
|
+
const fieldsBound = new Set(bound.map((row) => row.field));
|
|
89
|
+
setField(
|
|
90
|
+
fieldsBound.size === 1 ? bound[0].field : (control.field ?? ""),
|
|
91
|
+
);
|
|
92
|
+
setPerTile(fieldsBound.size > 1);
|
|
93
|
+
} else {
|
|
94
|
+
setSource({ kind: "new" });
|
|
95
|
+
setLabel("");
|
|
96
|
+
setKind("select");
|
|
97
|
+
setField("");
|
|
98
|
+
setDateDefault(new Date().toISOString().slice(0, 10));
|
|
99
|
+
setRows(
|
|
100
|
+
document.tiles.map((tile) => ({
|
|
101
|
+
include: canBind(tile),
|
|
102
|
+
field: "",
|
|
103
|
+
})),
|
|
104
|
+
);
|
|
105
|
+
setPerTile(false);
|
|
106
|
+
}
|
|
107
|
+
}, [open, control, document]);
|
|
108
|
+
|
|
109
|
+
// A new control's name follows its field, distinct from anything declared.
|
|
110
|
+
const taken = useMemo(
|
|
111
|
+
() => [
|
|
112
|
+
...(document.localGivens ?? []).map((g) => g.name),
|
|
113
|
+
...available.map((g) => g.name),
|
|
114
|
+
],
|
|
115
|
+
[document, available],
|
|
116
|
+
);
|
|
117
|
+
const newName = useMemo(
|
|
118
|
+
() => (field.trim() ? givenNameFor(field.trim(), taken) : ""),
|
|
119
|
+
[field, taken],
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
// The given being mapped, with the type that decides how each row compares.
|
|
123
|
+
const target: { name: string; type?: string } | undefined =
|
|
124
|
+
source.kind === "existing"
|
|
125
|
+
? (control ?? available.find((g) => g.name === source.name))
|
|
126
|
+
: newName
|
|
127
|
+
? {
|
|
128
|
+
name: newName,
|
|
129
|
+
type: newLocalGiven({ name: newName, label, kind, field }).type,
|
|
130
|
+
}
|
|
131
|
+
: undefined;
|
|
132
|
+
// A `date` or `number` given is a value, not a filter expression: it needs
|
|
133
|
+
// a comparison, and `~` is not one it accepts.
|
|
134
|
+
const valueTyped =
|
|
135
|
+
target?.type !== undefined && !target.type.startsWith("filter<");
|
|
136
|
+
const commonOp =
|
|
137
|
+
rows.find((row) => row.include)?.op ?? defaultOperator(target?.type);
|
|
138
|
+
|
|
139
|
+
const setRow = (index: number, patch: Partial<MappingRow>) =>
|
|
140
|
+
setRows((previous) =>
|
|
141
|
+
previous.map((row, i) => (i === index ? { ...row, ...patch } : row)),
|
|
142
|
+
);
|
|
143
|
+
const setCommonOp = (next: string) =>
|
|
144
|
+
setRows((previous) => previous.map((row) => ({ ...row, op: next })));
|
|
145
|
+
/** Rows follow a given's type: the comparison it needs, or none. */
|
|
146
|
+
const retype = (type: string | undefined) => {
|
|
147
|
+
const op = defaultOperator(type);
|
|
148
|
+
setRows((previous) =>
|
|
149
|
+
previous.map(({ op: _was, ...row }) => (op ? { ...row, op } : row)),
|
|
150
|
+
);
|
|
151
|
+
};
|
|
152
|
+
const pickExisting = (given: BuilderControl) => {
|
|
153
|
+
setSource({ kind: "existing", name: given.name });
|
|
154
|
+
setField(given.field ?? "");
|
|
155
|
+
retype(given.type);
|
|
156
|
+
};
|
|
157
|
+
const pickKind = (next: ControlKind) => {
|
|
158
|
+
setKind(next);
|
|
159
|
+
retype(newLocalGiven({ name: "X", label, kind: next, field }).type);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const bindable = document.tiles.map(canBind);
|
|
163
|
+
const bindableCount = bindable.filter(Boolean).length;
|
|
164
|
+
const included = rows.filter((row, i) => row.include && bindable[i]).length;
|
|
165
|
+
// The tiles a common field has to fit: the ticked, bindable ones — or every
|
|
166
|
+
// bindable one while nothing is ticked yet, so the picker has something to
|
|
167
|
+
// offer before the first tick.
|
|
168
|
+
const ticked = document.tiles.filter(
|
|
169
|
+
(_, i) => (rows[i]?.include ?? false) && bindable[i],
|
|
170
|
+
);
|
|
171
|
+
const pool =
|
|
172
|
+
ticked.length > 0 ? ticked : document.tiles.filter((_, i) => bindable[i]);
|
|
173
|
+
// What the common picker offers: every field any tile in the pool can take,
|
|
174
|
+
// once by name. Undefined when no tile has a list, which is "accept anything".
|
|
175
|
+
const commonFields = useMemo(() => {
|
|
176
|
+
const byName = new Map<string, CatalogField>();
|
|
177
|
+
let any = false;
|
|
178
|
+
for (const tile of pool) {
|
|
179
|
+
const list = fieldsFor?.(tile);
|
|
180
|
+
if (!list) continue;
|
|
181
|
+
any = true;
|
|
182
|
+
for (const field of list)
|
|
183
|
+
if (!byName.has(field.name)) byName.set(field.name, field);
|
|
184
|
+
}
|
|
185
|
+
return any ? [...byName.values()] : undefined;
|
|
186
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- pool is derived from rows and tiles
|
|
187
|
+
}, [rows, document.tiles, fieldsFor]);
|
|
188
|
+
// Where a NEW control's picker reads its options: the source of the first
|
|
189
|
+
// ticked tile that has the field — a `suggest` has to resolve in this file,
|
|
190
|
+
// and a tile's own base always does. A dashboard over imported sources
|
|
191
|
+
// alone has no extension, so the tile's source is the base itself.
|
|
192
|
+
const suggestSource = (() => {
|
|
193
|
+
const name = field.trim();
|
|
194
|
+
for (const tile of ticked) {
|
|
195
|
+
const list = fieldsFor?.(tile);
|
|
196
|
+
if (!list || list.some((f) => f.name === name)) return baseOf(tile);
|
|
197
|
+
}
|
|
198
|
+
return ticked[0] ? baseOf(ticked[0]) : document.sources[0]?.base;
|
|
199
|
+
})();
|
|
200
|
+
const setAll = (include: boolean) =>
|
|
201
|
+
setRows((previous) =>
|
|
202
|
+
previous.map((row, i) => ({
|
|
203
|
+
...row,
|
|
204
|
+
include: include && bindable[i],
|
|
205
|
+
})),
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
// The rows as they will be APPLIED: the common field on every row until
|
|
209
|
+
// the author has asked to set them one by one.
|
|
210
|
+
const effective = useMemo(
|
|
211
|
+
() => (perTile ? rows : rows.map((row) => ({ ...row, field }))),
|
|
212
|
+
[perTile, rows, field],
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
// Validation, per TILE, where its source's fields are known. Unknown is a
|
|
216
|
+
// name that tile's source does not have; empty is no name at all. Either on
|
|
217
|
+
// a ticked tile holds Apply, and is marked where it stands.
|
|
218
|
+
const typeOf = (name: string) => {
|
|
219
|
+
for (const tile of pool) {
|
|
220
|
+
const found = fieldsFor?.(tile)?.find((f) => f.name === name.trim());
|
|
221
|
+
if (found?.type) return found.type;
|
|
222
|
+
}
|
|
223
|
+
return undefined;
|
|
224
|
+
};
|
|
225
|
+
// What this given can compare: the picker offers only these, and a name
|
|
226
|
+
// typed past the list is held to the same rule.
|
|
227
|
+
const accepts = (candidate: CatalogField) =>
|
|
228
|
+
acceptsField(target?.type, candidate.type);
|
|
229
|
+
const problemWith = (
|
|
230
|
+
name: string,
|
|
231
|
+
tile: DashboardTile | undefined,
|
|
232
|
+
): string | undefined => {
|
|
233
|
+
if (name.trim() === "") return "Pick the field this filter compares.";
|
|
234
|
+
const list = tile ? fieldsFor?.(tile) : undefined;
|
|
235
|
+
if (!list) return undefined;
|
|
236
|
+
const found = list.find((f) => f.name === name.trim());
|
|
237
|
+
if (!found)
|
|
238
|
+
return `Not a field of ${tile ? baseOf(tile) : "the tiles' source"}.`;
|
|
239
|
+
if (found.type && !acceptsField(target?.type, found.type))
|
|
240
|
+
return `${name.trim()} is ${typeLabel(found.type)}; this filter compares ${typeLabel(
|
|
241
|
+
target?.type?.replace(/^filter<(.+)>$/, "$1"),
|
|
242
|
+
)}.`;
|
|
243
|
+
return undefined;
|
|
244
|
+
};
|
|
245
|
+
// A NEW control follows the field it is given: pick a number and it becomes
|
|
246
|
+
// a number range, a date and it becomes a date picker. Explicit kind changes
|
|
247
|
+
// still win afterwards; this only moves a kind the field cannot take.
|
|
248
|
+
const pickField = (next: string) => {
|
|
249
|
+
setField(next);
|
|
250
|
+
if (editing || source.kind !== "new") return;
|
|
251
|
+
const fieldType = typeOf(next);
|
|
252
|
+
const kindType = newLocalGiven({
|
|
253
|
+
name: "X",
|
|
254
|
+
label,
|
|
255
|
+
kind,
|
|
256
|
+
field: next,
|
|
257
|
+
}).type;
|
|
258
|
+
if (fieldType && !acceptsField(kindType, fieldType))
|
|
259
|
+
pickKind(kindForFieldType(fieldType));
|
|
260
|
+
};
|
|
261
|
+
const rowProblems = effective.map((row, i) =>
|
|
262
|
+
row.include && bindable[i]
|
|
263
|
+
? problemWith(row.field, document.tiles[i])
|
|
264
|
+
: undefined,
|
|
265
|
+
);
|
|
266
|
+
const fieldsResolve = rowProblems.every((problem) => problem === undefined);
|
|
267
|
+
// In the common case one box speaks for every row, so its message is the
|
|
268
|
+
// rows' message; the box is only marked once a tile is ticked to bind.
|
|
269
|
+
// Every ticked tile has to take it; the first that cannot says why.
|
|
270
|
+
const commonProblem =
|
|
271
|
+
!perTile && included > 0
|
|
272
|
+
? ticked.map((tile) => problemWith(field, tile)).find(Boolean)
|
|
273
|
+
: undefined;
|
|
274
|
+
|
|
275
|
+
const canApply =
|
|
276
|
+
target !== undefined &&
|
|
277
|
+
fieldsResolve &&
|
|
278
|
+
(source.kind === "existing" || (field.trim() !== "" && newName !== ""));
|
|
279
|
+
|
|
280
|
+
const apply = () => {
|
|
281
|
+
if (!target || !canApply) return;
|
|
282
|
+
if (source.kind === "new") {
|
|
283
|
+
onApply(
|
|
284
|
+
target.name,
|
|
285
|
+
effective,
|
|
286
|
+
newLocalGiven({
|
|
287
|
+
name: target.name,
|
|
288
|
+
// Untyped, the label is what the placeholder promised — `Status`
|
|
289
|
+
// for `$STATUS` — not the shouted name.
|
|
290
|
+
label: label.trim() || titleCase(target.name),
|
|
291
|
+
kind,
|
|
292
|
+
field: field.trim(),
|
|
293
|
+
...(suggestSource ? { source: suggestSource } : {}),
|
|
294
|
+
...(dateDefault ? { dateDefault } : {}),
|
|
295
|
+
}),
|
|
296
|
+
);
|
|
297
|
+
} else if (control?.origin === "dashboard" && control.local) {
|
|
298
|
+
// Retag in place when the label changed; the declaration is otherwise
|
|
299
|
+
// the one already in the file.
|
|
300
|
+
const trimmed = label.trim();
|
|
301
|
+
const changed = trimmed !== (control.local.label ?? "");
|
|
302
|
+
onApply(
|
|
303
|
+
target.name,
|
|
304
|
+
effective,
|
|
305
|
+
changed
|
|
306
|
+
? { ...control.local, ...(trimmed ? { label: trimmed } : {}) }
|
|
307
|
+
: undefined,
|
|
308
|
+
);
|
|
309
|
+
} else {
|
|
310
|
+
onApply(target.name, effective);
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
const fromModel = editing && shown.origin === "model";
|
|
315
|
+
|
|
316
|
+
return {
|
|
317
|
+
editing,
|
|
318
|
+
shown,
|
|
319
|
+
fromModel,
|
|
320
|
+
source,
|
|
321
|
+
setSource,
|
|
322
|
+
label,
|
|
323
|
+
setLabel,
|
|
324
|
+
kind,
|
|
325
|
+
dateDefault,
|
|
326
|
+
setDateDefault,
|
|
327
|
+
field,
|
|
328
|
+
rows,
|
|
329
|
+
setRows,
|
|
330
|
+
perTile,
|
|
331
|
+
setPerTile,
|
|
332
|
+
newName,
|
|
333
|
+
target,
|
|
334
|
+
valueTyped,
|
|
335
|
+
commonOp,
|
|
336
|
+
setRow,
|
|
337
|
+
setCommonOp,
|
|
338
|
+
pickExisting,
|
|
339
|
+
pickKind,
|
|
340
|
+
bindable,
|
|
341
|
+
bindableCount,
|
|
342
|
+
included,
|
|
343
|
+
commonFields,
|
|
344
|
+
setAll,
|
|
345
|
+
accepts,
|
|
346
|
+
pickField,
|
|
347
|
+
rowProblems,
|
|
348
|
+
commonProblem,
|
|
349
|
+
canApply,
|
|
350
|
+
apply,
|
|
351
|
+
};
|
|
352
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { act, renderHook } from "@testing-library/react";
|
|
5
|
+
import { describe, expect, it, mock } from "bun:test";
|
|
6
|
+
import type { DragEndEvent, DragOverEvent } from "@dnd-kit/react";
|
|
7
|
+
import { tileKey, type DashboardTile } from "./document";
|
|
8
|
+
import { GAP_TYPE } from "./sortable";
|
|
9
|
+
import { useTileReorder } from "./useTileReorder";
|
|
10
|
+
|
|
11
|
+
const tile = (
|
|
12
|
+
name: string,
|
|
13
|
+
extra: Partial<DashboardTile> = {},
|
|
14
|
+
): DashboardTile => ({
|
|
15
|
+
name,
|
|
16
|
+
source: "s",
|
|
17
|
+
declaration: { kind: "reference", from: name },
|
|
18
|
+
colspan: 6,
|
|
19
|
+
...extra,
|
|
20
|
+
});
|
|
21
|
+
// Two rows of two: c starts the second row.
|
|
22
|
+
const tiles = [tile("a"), tile("b"), tile("c", { break: true }), tile("d")];
|
|
23
|
+
|
|
24
|
+
/** A report that the tile `id` is over the gap after `after`. */
|
|
25
|
+
const overGap = (id: string, after: string) =>
|
|
26
|
+
({
|
|
27
|
+
operation: {
|
|
28
|
+
source: { id },
|
|
29
|
+
target: { type: GAP_TYPE, data: { after } },
|
|
30
|
+
},
|
|
31
|
+
}) as unknown as DragOverEvent;
|
|
32
|
+
|
|
33
|
+
const ended = (id: string, canceled = false) =>
|
|
34
|
+
({ canceled, operation: { source: { id } } }) as unknown as DragEndEvent;
|
|
35
|
+
|
|
36
|
+
const mount = () => {
|
|
37
|
+
const commit = mock((_next: DashboardTile[]) => {});
|
|
38
|
+
const onLanded = mock((_index: number) => {});
|
|
39
|
+
const view = renderHook(() => useTileReorder({ tiles, commit, onLanded }));
|
|
40
|
+
return { view, commit, onLanded };
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
describe("useTileReorder", () => {
|
|
44
|
+
it("previews a drop into a row's gap, and writes the order once on release", () => {
|
|
45
|
+
const { view, commit, onLanded } = mount();
|
|
46
|
+
act(() => view.result.current.onDragStart());
|
|
47
|
+
expect(view.result.current.dragging).toBe(true);
|
|
48
|
+
|
|
49
|
+
// d, dragged up into the gap after b: it joins the first row, and c
|
|
50
|
+
// keeps starting the second.
|
|
51
|
+
act(() =>
|
|
52
|
+
view.result.current.onDragOver(
|
|
53
|
+
overGap(tileKey(tiles[3]), tileKey(tiles[1])),
|
|
54
|
+
),
|
|
55
|
+
);
|
|
56
|
+
expect(view.result.current.preview?.map((t) => t.name)).toEqual([
|
|
57
|
+
"a",
|
|
58
|
+
"b",
|
|
59
|
+
"d",
|
|
60
|
+
"c",
|
|
61
|
+
]);
|
|
62
|
+
expect(view.result.current.preview?.map((t) => t.break ?? false)).toEqual(
|
|
63
|
+
[false, false, false, true],
|
|
64
|
+
);
|
|
65
|
+
expect(commit).not.toHaveBeenCalled();
|
|
66
|
+
|
|
67
|
+
act(() => view.result.current.onDragEnd(ended(tileKey(tiles[3]))));
|
|
68
|
+
expect(commit).toHaveBeenCalledTimes(1);
|
|
69
|
+
expect(commit.mock.calls[0][0].map((t) => t.name)).toEqual([
|
|
70
|
+
"a",
|
|
71
|
+
"b",
|
|
72
|
+
"d",
|
|
73
|
+
"c",
|
|
74
|
+
]);
|
|
75
|
+
expect(onLanded).toHaveBeenCalledWith(2);
|
|
76
|
+
expect(view.result.current.dragging).toBe(false);
|
|
77
|
+
expect(view.result.current.preview).toBeUndefined();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("ignores the gap right after the tile in hand, and writes nothing on cancel", () => {
|
|
81
|
+
const { view, commit } = mount();
|
|
82
|
+
act(() => view.result.current.onDragStart());
|
|
83
|
+
act(() =>
|
|
84
|
+
view.result.current.onDragOver(
|
|
85
|
+
overGap(tileKey(tiles[1]), tileKey(tiles[1])),
|
|
86
|
+
),
|
|
87
|
+
);
|
|
88
|
+
expect(view.result.current.preview).toBeUndefined();
|
|
89
|
+
act(() =>
|
|
90
|
+
view.result.current.onDragOver(
|
|
91
|
+
overGap(tileKey(tiles[3]), tileKey(tiles[1])),
|
|
92
|
+
),
|
|
93
|
+
);
|
|
94
|
+
act(() => view.result.current.onDragEnd(ended(tileKey(tiles[3]), true)));
|
|
95
|
+
expect(commit).not.toHaveBeenCalled();
|
|
96
|
+
});
|
|
97
|
+
});
|