The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
gdgt-1377 deselect and hide from bulk view tables not matching selection (#68469)
Jakub Chodorowicz383f6ba968cf
diff --git a/e2e/support/helpers/e2e-data-studio-helpers.ts b/e2e/support/helpers/e2e-data-studio-helpers.tsindex 1c4eb26e9bc..2efac53c22b 100644--- a/e2e/support/helpers/e2e-data-studio-helpers.ts+++ b/e2e/support/helpers/e2e-data-studio-helpers.ts@@ -2,6 +2,7 @@ import type { MeasureId, SegmentId, TableId } from "metabase-types/api";import { codeMirrorHelpers } from "./e2e-codemirror-helpers";import { popover } from "./e2e-ui-elements-helpers";+const { H } = cy;const libraryPage = () => cy.findByTestId("library-page");const newSnippetPage = () => cy.findByTestId("new-snippet-page");@@ -112,6 +113,10 @@ export const DataStudio = {.findByTestId("table-description-section").findByPlaceholderText("No description"),},+ openFilterPopover: () => {+ cy.findByRole("button", { name: "Filter" }).click();+ H.popover();+ },},Library: {visit: () => {diff --git a/e2e/support/helpers/e2e-datamodel-helpers.ts b/e2e/support/helpers/e2e-datamodel-helpers.tsindex bf4c26cc47a..736004e3c01 100644--- a/e2e/support/helpers/e2e-datamodel-helpers.ts+++ b/e2e/support/helpers/e2e-datamodel-helpers.ts@@ -5,6 +5,8 @@ import type {TableId,} from "metabase-types/api";+import { popover } from "./e2e-ui-elements-helpers";+export const DataModel = {visit,visitDataStudio,@@ -15,14 +17,19 @@ export const DataModel = {get: getTablePicker,getDatabase: getTablePickerDatabase,getDatabaseToggle: getTablePickerDatabaseToggle,+ getDatabaseCheckbox,getDatabases: getTablePickerDatabases,getSchemas: getTablePickerSchemas,getSchema: getTablePickerSchema,getSchemaToggle: getTablePickerSchemaToggle,+ getSchemaCheckbox,getTables: getTablePickerTables,getTable: getTablePickerTable,getSearchInput: getTablePickerSearchInput,getFilterForm: getTablePickerFilter,+ openFilterPopover,+ selectFilterOption,+ applyFilters,},TableSection: {get: getTableSection,@@ -664,3 +671,26 @@ function getMeasureEditorDependenciesTab() {function getMeasureRevisionHistory() {return cy.findByTestId("measure-revision-history-page");}++export function openFilterPopover() {+ cy.findByRole("button", { name: "Filter" }).click();+ popover();+}++export function selectFilterOption(fieldLabel: string, optionLabel: string) {+ cy.findByRole("textbox", { name: fieldLabel }).click();+ popover().contains(optionLabel).click();+}++export function applyFilters() {+ cy.findByRole("button", { name: "Apply" }).click();+ cy.wait("@listTables");+}++export function getDatabaseCheckbox(databaseName: string) {+ return getTablePickerDatabase(databaseName).find('input[type="checkbox"]');+}++export function getSchemaCheckbox(schemaName: string) {+ return getTablePickerSchema(schemaName).find('input[type="checkbox"]');+}diff --git a/e2e/test/scenarios/data-studio/data-model/datamodel-data-studio-search.cy.spec.ts b/e2e/test/scenarios/data-studio/data-model/datamodel-data-studio-search.cy.spec.tsindex 7afeef9651a..0ec5e5f8d76 100644--- a/e2e/test/scenarios/data-studio/data-model/datamodel-data-studio-search.cy.spec.ts+++ b/e2e/test/scenarios/data-studio/data-model/datamodel-data-studio-search.cy.spec.ts@@ -11,6 +11,7 @@ describe("Search", () => {H.activateToken("bleeding-edge");H.resetTestTable({ type: "postgres", table: "multi_schema" });H.resyncDatabase({ dbId: WRITABLE_DB_ID });+ cy.intercept("GET", "/api/table?*").as("listTables");});it("should support prefix-based search", () => {@@ -124,24 +125,19 @@ describe("Search", () => {const sampleDatabaseName = "Sample Database";const domesticSchema = "Domestic";- const getDatabaseCheckbox = (name: string) =>- TablePicker.getDatabase(name).find('input[type="checkbox"]');- const getSchemaCheckbox = (schemaName: string) =>- TablePicker.getSchema(schemaName).find('input[type="checkbox"]');-- getDatabaseCheckbox(sampleDatabaseName).click();+ TablePicker.getDatabaseCheckbox(sampleDatabaseName).click();cy.findByRole("heading", { name: /2 tables selected/i }).should("be.visible",);- getDatabaseCheckbox(postgres).click();+ TablePicker.getDatabaseCheckbox(postgres).click();cy.findByRole("heading", { name: /4 tables selected/i }).should("be.visible",);- getSchemaCheckbox(domesticSchema).click();+ TablePicker.getSchemaCheckbox(domesticSchema).click();cy.findByRole("heading", { name: /3 tables selected/i }).should("be.visible",);- getDatabaseCheckbox(postgres).click();+ TablePicker.getDatabaseCheckbox(postgres).click();cy.findByRole("heading", { name: /4 tables selected/i }).should("be.visible",);@@ -172,4 +168,44 @@ describe("Search", () => {TablePicker.getTables().should("have.length", 0);TablePicker.getDatabases().should("have.length", 2);});++ it("should deselect and hide tables that are not in the search results", () => {+ H.DataModel.visitDataStudio();++ TablePicker.getTables().should("have.length", 0);+ TablePicker.getSearchInput().type("a");+ TablePicker.getTables().should("have.length", 4);++ ["Writable Postgres12", "Sample Database"].forEach((database) => {+ TablePicker.getDatabaseCheckbox(database).click();+ });+ cy.findByRole("heading", { name: /4 tables selected/i }).should(+ "be.visible",+ );+ TablePicker.getTables()+ .find('input[type="checkbox"]:checked')+ .should("have.length", 4);++ TablePicker.openFilterPopover();+ cy.findByTestId("table-picker-filter").within(() => {+ TablePicker.selectFilterOption("Visibility type", "Bronze");+ TablePicker.applyFilters();+ });++ TablePicker.getTables()+ .find('input[type="checkbox"]:checked')+ .should("have.length", 0);++ TablePicker.getDatabaseCheckbox("Writable Postgres12").click();++ cy.findByRole("heading", { name: /2 tables selected/i }).should(+ "be.visible",+ );++ TablePicker.selectFilterOption("Visibility type", "Gold");+ TablePicker.get().findByText("No tables found").should("be.visible");+ cy.findByRole("heading", { name: /2 tables selected/i }).should(+ "not.exist",+ );+ });});diff --git a/e2e/test/scenarios/data-studio/data-model/datamodel-data-studio.cy.spec.ts b/e2e/test/scenarios/data-studio/data-model/datamodel-data-studio.cy.spec.tsindex 084a3ca4296..27e2b78bc23 100644--- a/e2e/test/scenarios/data-studio/data-model/datamodel-data-studio.cy.spec.ts+++ b/e2e/test/scenarios/data-studio/data-model/datamodel-data-studio.cy.spec.ts@@ -562,15 +562,15 @@ describe("scenarios > data studio > datamodel", () => {H.DataModel.visitDataStudio();- openFilterPopover();+ TablePicker.openFilterPopover();cy.log("Filter popover should close on click outside");H.DataModel.TablePicker.getSearchInput().click();H.DataModel.TablePicker.getFilterForm().should("not.exist");- openFilterPopover();- selectFilterOption("Visibility type", "Gold");- applyFilters();+ TablePicker.openFilterPopover();+ TablePicker.selectFilterOption("Visibility type", "Gold");+ TablePicker.applyFilters();H.expectUnstructuredSnowplowEvent({event: "data_studio_table_picker_filters_applied",});@@ -600,9 +600,9 @@ describe("scenarios > data studio > datamodel", () => {H.DataModel.visitDataStudio();- openFilterPopover();- selectFilterOption("Owner", "Unspecified");- applyFilters();+ TablePicker.openFilterPopover();+ TablePicker.selectFilterOption("Owner", "Unspecified");+ TablePicker.applyFilters();cy.get<TableId>("@unownedTableId").then(expectTableVisible);cy.get<TableId>("@ownedTableId").then(expectTableNotVisible);@@ -627,11 +627,11 @@ describe("scenarios > data studio > datamodel", () => {H.DataModel.visitDataStudio();- openFilterPopover();+ TablePicker.openFilterPopover();cy.get<string>("@ownerName").then((ownerName) => {selectOwnerByName(ownerName);});- applyFilters();+ TablePicker.applyFilters();cy.get<TableId>("@ownedTableId").then(expectTableVisible);cy.get<TableId>("@unownedTableId").then(expectTableNotVisible);@@ -653,9 +653,9 @@ describe("scenarios > data studio > datamodel", () => {H.DataModel.visitDataStudio();- openFilterPopover();+ TablePicker.openFilterPopover();selectOwnerByEmail(OWNER_EMAIL);- applyFilters();+ TablePicker.applyFilters();cy.get<TableId>("@emailOwnedTableId").then(expectTableVisible);cy.get<TableId>("@otherTableId").then(expectTableNotVisible);@@ -676,9 +676,9 @@ describe("scenarios > data studio > datamodel", () => {H.DataModel.visitDataStudio();- openFilterPopover();- selectFilterOption("Source", "Uploaded data");- applyFilters();+ TablePicker.openFilterPopover();+ TablePicker.selectFilterOption("Source", "Uploaded data");+ TablePicker.applyFilters();cy.get<TableId>("@uploadedTableId").then(expectTableVisible);cy.get<TableId>("@ingestedTableId").then(expectTableNotVisible);@@ -711,9 +711,9 @@ describe("scenarios > data studio > datamodel", () => {H.DataModel.visitDataStudio();- openFilterPopover();+ TablePicker.openFilterPopover();toggleUnusedFilter(true);- applyFilters();+ TablePicker.applyFilters();cy.get<TableId>("@unusedTableId").then(expectTableVisible);cy.get<TableId>("@usedTableId").then(expectTableNotVisible);@@ -3925,21 +3925,6 @@ type TableLookup = {name?: string;};-function openFilterPopover() {- cy.findByRole("button", { name: "Filter" }).click();- H.popover();-}--function applyFilters() {- cy.findByRole("button", { name: "Apply" }).click();- cy.wait("@listTables");-}--function selectFilterOption(fieldLabel: string, optionLabel: string) {- cy.findByRole("textbox", { name: fieldLabel }).click();- H.popover().contains(optionLabel).click();-}-function selectOwnerByName(ownerLabel: string) {cy.findByRole("textbox", { name: "Owner" }).click();H.popover().contains(ownerLabel).click();diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/components/SearchNew.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/components/SearchNew.tsxindex 1c7466cb93d..307938b45bd 100644--- a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/components/SearchNew.tsx+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/components/SearchNew.tsx@@ -98,7 +98,8 @@ export function SearchNew({filters,onChange,}: SearchNewProps) {- const { resetSelection } = useSelection();+ const { resetSelection, filterSelectedTables } = useSelection();+const routeParams = parseRouteParams(params);const {data: tables,@@ -148,6 +149,10 @@ export function SearchNew({[filteredTables],);+ useEffect(() => {+ filterSelectedTables(filteredTables.map((table) => table.id));+ }, [filteredTables, filterSelectedTables]);+useEffect(() => {resetSelection();}, [query, filters, resetSelection]);diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TableSection/TableAttributesEditBulk.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TableSection/TableAttributesEditBulk.tsxindex fadeba4e43d..22b8466a11a 100644--- a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TableSection/TableAttributesEditBulk.tsx+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TableSection/TableAttributesEditBulk.tsx@@ -1,4 +1,5 @@import { useEffect, useState } from "react";+import { usePrevious } from "react-use";import { t } from "ttag";import { useSelector } from "metabase/lib/redux";@@ -61,6 +62,9 @@ export function TableAttributesEditBulk({const [entityType, setEntityType] = useState<string | null>(null);const [userId, setUserId] = useState<UserId | "unknown" | null>(null);const [modalType, setModalType] = useState<TableModalType>();+ const previousTables = usePrevious(selectedTables);+ const previousSchemas = usePrevious(selectedSchemas);+ const previousDatabases = usePrevious(selectedDatabases);const hasOnlyTablesSelected =selectedTables.size > 0 &&@@ -143,12 +147,30 @@ export function TableAttributesEditBulk({};useEffect(() => {- setDataLayer(null);- setDataSource(null);- setEmail(null);- setEntityType(null);- setUserId(null);- }, [selectedTables, selectedSchemas, selectedDatabases]);+ if (!previousTables || !previousSchemas || !previousDatabases) {+ return;+ }++ const shouldReset =+ !isSubsetOf(selectedTables, previousTables) ||+ !isSubsetOf(selectedSchemas, previousSchemas) ||+ !isSubsetOf(selectedDatabases, previousDatabases);++ if (shouldReset) {+ setDataLayer(null);+ setDataSource(null);+ setEmail(null);+ setEntityType(null);+ setUserId(null);+ }+ }, [+ previousDatabases,+ previousSchemas,+ previousTables,+ selectedDatabases,+ selectedSchemas,+ selectedTables,+ ]);return (<>@@ -323,3 +345,7 @@ export function TableAttributesEditBulk({</>);}++function isSubsetOf<T>(subset: Set<T>, superset: Set<T>): boolean {+ return Array.from(subset).every((element) => superset.has(element));+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TableSection/TableAttributesEditBulk.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TableSection/TableAttributesEditBulk.unit.spec.tsxindex 9ffdee19420..9d618ee6615 100644--- a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TableSection/TableAttributesEditBulk.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TableSection/TableAttributesEditBulk.unit.spec.tsx@@ -21,9 +21,11 @@ import { TableAttributesEditBulk } from "./TableAttributesEditBulk";// Component that allows controlling selection for testingfunction SelectionController({initialTables,+ nextTables,onSelectionChange,}: {initialTables: Set<TableId>;+ nextTables?: Set<TableId>;onSelectionChange: () => void;}) {const { setSelectedTables } = useSelection();@@ -38,7 +40,7 @@ function SelectionController({data-testid="change-selection"onClick={() => {// Change selection to a different table- setSelectedTables(new Set([999]));+ setSelectedTables(nextTables ?? new Set([999]));onSelectionChange();}}>@@ -49,8 +51,10 @@ function SelectionController({function TestWrapper({initialTables = new Set([1]),+ nextTables,}: {initialTables?: Set<TableId>;+ nextTables?: Set<TableId>;}) {const [_selectionChanged, setSelectionChanged] = useState(false);@@ -59,6 +63,7 @@ function TestWrapper({<TableAttributesEditBulk hasLibrary canPublish onUpdate={_.noop} /><SelectionControllerinitialTables={initialTables}+ nextTables={nextTables}onSelectionChange={() => setSelectionChanged(true)}/></SelectionProvider>@@ -67,12 +72,14 @@ function TestWrapper({type SetupOpts = {initialTables?: Set<TableId>;+ nextTables?: Set<TableId>;isAdmin?: boolean;isDataAnalyst?: boolean;};function setup({initialTables = new Set([1]),+ nextTables,isAdmin = false,isDataAnalyst = false,}: SetupOpts = {}) {@@ -93,15 +100,18 @@ function setup({},});- renderWithProviders(<TestWrapper initialTables={initialTables} />, {- withRouter: false,- storeInitialState: {- currentUser: createMockUser({- is_superuser: isAdmin,- is_data_analyst: isDataAnalyst,- }),+ renderWithProviders(+ <TestWrapper initialTables={initialTables} nextTables={nextTables} />,+ {+ withRouter: false,+ storeInitialState: {+ currentUser: createMockUser({+ is_superuser: isAdmin,+ is_data_analyst: isDataAnalyst,+ }),+ },},- });+ );}describe("TableAttributesEditBulk", () => {@@ -150,4 +160,33 @@ describe("TableAttributesEditBulk", () => {expect(screen.getByRole("textbox", { name: "Owner" })).toHaveValue("");});});++ it("should keep form state when selection is a subset", async () => {+ setup({+ initialTables: new Set([1, 2]),+ nextTables: new Set([1]),+ });+ const userName = "Testy Tableton";++ await waitFor(() => {+ expect(screen.getByText(/tables selected/i)).toBeInTheDocument();+ });++ await userEvent.click(screen.getByRole("textbox", { name: "Owner" }));+ await userEvent.click(screen.getByText(userName));+ await waitFor(() => {+ expect(screen.getByRole("textbox", { name: "Owner" })).toHaveValue(+ userName,+ );+ });++ const changeSelectionButton = screen.getByTestId("change-selection");+ await userEvent.click(changeSelectionButton);++ await waitFor(() => {+ expect(screen.getByRole("textbox", { name: "Owner" })).toHaveValue(+ userName,+ );+ });+ });});diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/pages/DataModel/contexts/SelectionContext.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/pages/DataModel/contexts/SelectionContext.tsxindex 333630d3e29..04c7d863c30 100644--- a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/pages/DataModel/contexts/SelectionContext.tsx+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/pages/DataModel/contexts/SelectionContext.tsx@@ -22,6 +22,7 @@ interface SelectionContextValue {selectedItemsCount: number;hasOnlyOneTableSelected: boolean;hasSelectedMoreThanOneTable: boolean;+ filterSelectedTables: (tables: TableId[]) => void;}const SelectionContext = createContext<SelectionContextValue | null>(null);@@ -41,6 +42,13 @@ export function SelectionProvider({ children }: { children: ReactNode }) {setSelectedDatabases(new Set());}, []);+ const filterSelectedTables = useCallback((tables: TableId[]) => {+ setSelectedTables(+ (oldTables) =>+ new Set([...oldTables].filter((tableId) => tables.includes(tableId))),+ );+ }, []);+const hasSelectedItems =selectedTables.size > 0 ||selectedSchemas.size > 0 ||@@ -71,6 +79,7 @@ export function SelectionProvider({ children }: { children: ReactNode }) {selectedItemsCount,hasOnlyOneTableSelected,hasSelectedMoreThanOneTable,+ filterSelectedTables,}}>{children}