Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

should show library (#66343)

Nick Fitzpatrickcbe07addba8c
diff --git a/e2e/support/test-library-data.ts b/e2e/support/test-library-data.ts
index 5a6408b31dc..47ae872ac2c 100644
--- a/e2e/support/test-library-data.ts
+++ b/e2e/support/test-library-data.ts
@@ -68,3 +68,26 @@ export function createLibraryWithItems() {
},
);
}
+
+export function createLibraryWithModel() {
+ return H.createLibrary().then(
+ (response: Cypress.Response<LibraryResponse>) => {
+ const body = response.body;
+ const modelsCollection = body.effective_children?.find(
+ (child) => child.name === "Data",
+ );
+
+ return H.createQuestion(TRUSTED_ORDERS_MODEL, {
+ wrapId: true,
+ idAlias: "trustedOrdersModelId",
+ }).then(() =>
+ cy.get("@trustedOrdersModelId").then((modelId) =>
+ cy.request("PUT", `/api/card/${modelId}`, {
+ type: "model",
+ collection_id: modelsCollection?.id,
+ }),
+ ),
+ );
+ },
+ );
+}
diff --git a/e2e/test/scenarios/data-studio/modeling-library.cy.spec.ts b/e2e/test/scenarios/data-studio/modeling-library.cy.spec.ts
index 203f9c45f41..f3fa5ec6868 100644
--- a/e2e/test/scenarios/data-studio/modeling-library.cy.spec.ts
+++ b/e2e/test/scenarios/data-studio/modeling-library.cy.spec.ts
@@ -1,3 +1,14 @@
+import { SAMPLE_DB_ID } from "e2e/support/cypress_data";
+import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
+import {
+ ALL_USERS_GROUP_ID,
+ ORDERS_MODEL_ID,
+} from "e2e/support/cypress_sample_instance_data";
+import {
+ createLibraryWithItems,
+ createLibraryWithModel,
+} from "e2e/support/test-library-data";
+
const { H } = cy;
describe("scenarios > data studio > modeling > library", () => {
@@ -52,4 +63,103 @@ describe("scenarios > data studio > modeling > library", () => {
cy.findByText("No models yet").should("be.visible");
});
});
+
+ it("should be available in the data picker", () => {
+ createLibraryWithItems();
+
+ H.startNewQuestion();
+ H.miniPickerBrowseAll().click();
+
+ H.entityPickerModalItem(0, "Library").click();
+ H.entityPickerModalItem(1, "Data").click();
+ H.entityPickerModalItem(2, "Trusted Orders Model").click();
+
+ cy.log("Ensure that the we can build the path from a value");
+
+ cy.button(/Trusted Orders Model/).click();
+ H.miniPickerHeader().click();
+ H.miniPickerBrowseAll().click();
+
+ H.entityPickerModalItem(0, "Library").should(
+ "have.attr",
+ "data-active",
+ "true",
+ );
+ H.entityPickerModalItem(1, "Data").should(
+ "have.attr",
+ "data-active",
+ "true",
+ );
+ H.entityPickerModalItem(2, "Trusted Orders Model").should(
+ "have.attr",
+ "data-active",
+ "true",
+ );
+ });
+
+ it("should let you move models and metrics into the library, even when empty", () => {
+ H.createLibrary();
+
+ H.visitModel(ORDERS_MODEL_ID);
+ H.openQuestionActions("Duplicate");
+ H.modal().findByTestId("dashboard-and-collection-picker-button").click();
+
+ H.entityPickerModalTab("Collections").click();
+ H.entityPickerModalItem(0, "Library").click();
+ H.entityPickerModalItem(1, "Metrics").should(
+ "have.attr",
+ "data-disabled",
+ "true",
+ );
+ H.entityPickerModalItem(1, "Data").click();
+ H.entityPickerModal().button("Select this collection").click();
+ H.modal().button("Duplicate").click();
+ });
+
+ it("should show the library collection even if only 1 child collection has items", () => {
+ createLibraryWithModel();
+
+ H.startNewQuestion();
+ H.miniPickerBrowseAll().click();
+
+ H.entityPickerModalItem(0, "Library").click();
+ H.entityPickerModalItem(1, "Data").click();
+ H.entityPickerModalItem(2, "Trusted Orders Model").should("exist");
+ });
+
+ it("should be available in the question picker modal", () => {
+ createLibraryWithItems();
+
+ const library = () => H.entityPickerModalLevel(0).findByText("Library");
+
+ cy.visit(
+ `/admin/permissions/data/group/${ALL_USERS_GROUP_ID}/database/${SAMPLE_DB_ID}/schema/PUBLIC/${SAMPLE_DATABASE.ACCOUNTS_ID}/segmented`,
+ );
+
+ H.modal()
+ .findByText(/Use a saved question/)
+ .click();
+ H.modal()
+ .button(/Select a question/)
+ .click();
+
+ H.entityPickerModal().within(() => {
+ // Questions tab should not show the library, because the library can only contain models and metrics
+ H.entityPickerModalTab("Questions").click();
+ library().should("not.exist");
+ H.entityPickerModalItem(0, "Our analytics").should("exist");
+
+ // Question picker should show library in models tab
+ H.entityPickerModalTab("Models").click();
+ H.entityPickerModalItem(0, "Our analytics").should("exist");
+ library().click();
+ H.entityPickerModalItem(1, "Metrics").should(
+ "have.attr",
+ "data-disabled",
+ "true",
+ );
+ H.entityPickerModalItem(1, "Data").click();
+ H.entityPickerModalItem(2, "Trusted Orders Model").should("exist");
+ });
+ });
});
diff --git a/e2e/test/scenarios/data-studio/transforms.cy.spec.ts b/e2e/test/scenarios/data-studio/transforms.cy.spec.ts
index 84dd0a0a34f..48e2a4bfa50 100644
--- a/e2e/test/scenarios/data-studio/transforms.cy.spec.ts
+++ b/e2e/test/scenarios/data-studio/transforms.cy.spec.ts
@@ -4,6 +4,7 @@ import dedent from "ts-dedent";
import { SAMPLE_DB_ID, WRITABLE_DB_ID } from "e2e/support/cypress_data";
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
+import { createLibraryWithItems } from "e2e/support/test-library-data";
import type {
CardType,
PythonTransformTableAliases,
@@ -86,6 +87,31 @@ describe("scenarios > admin > transforms", () => {
});
});
+ it("should not show you the library in the mini picker when building transforms (uxw-2403)", () => {
+ createLibraryWithItems();
+
+ visitTransformListPage();
+ getTransformsSidebar().button("Create a transform").click();
+ H.popover().findByText("Query builder").click();
+ H.miniPicker().within(() => {
+ cy.findByText(DB_NAME).should("exist");
+ cy.findByText("Our analytics").should("exist");
+ cy.findByText("Browse all").should("exist");
+ cy.findByText("Data").should("not.exist");
+ });
+
+ cy.findByRole("link", { name: "Exit data studio" }).click();
+ H.modal().button("Discard changes").click();
+ H.newButton("Question").click();
+
+ H.miniPicker().within(() => {
+ cy.findByText("Our analytics").should("not.exist");
+ cy.findByText("Browse all").should("exist");
+ cy.findByText("Data").click();
+ cy.findByText("Trusted Orders Model").should("exist");
+ });
+ });
+
it("should be able to create and run a SQL transform", () => {
cy.log("create a new transform");
visitTransformListPage();
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/utils.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/utils.ts
index 6f3defa9538..715cbfe827a 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/utils.ts
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/utils.ts
@@ -1,8 +1,6 @@
import { useMemo } from "react";
-import { match } from "ts-pattern";
import { skipToken, useListCollectionItemsQuery } from "metabase/api";
-import type { MiniPickerCollectionItem } from "metabase/common/components/Pickers/MiniPicker/types";
import type { LibraryCollectionType } from "metabase/plugins";
import { getIsEmbeddingIframe } from "metabase/selectors/embed";
import { getUserIsAdmin } from "metabase/selectors/user";
@@ -84,37 +82,10 @@ export const useGetLibraryCollection = ({
}: { skip?: boolean } = {}) => {
const { data: libraryCollection, isLoading: isLoadingCollection } =
useGetLibraryCollectionQuery(undefined, { skip });
- const hasStuff = Boolean(
- libraryCollection &&
- (libraryCollection?.below?.length || libraryCollection?.here?.length),
- );
- const { data: libraryItems, isLoading: isLoadingItems } =
- useListCollectionItemsQuery(
- libraryCollection && hasStuff ? { id: libraryCollection.id } : skipToken,
- );
-
- const subcollectionsWithStuff =
- libraryItems?.data.filter(
- (item) =>
- item.model === "collection" &&
- (item.here?.length || item.below?.length),
- ) ?? [];
-
- const showableLibrary = match({ subcollectionsWithStuff, hasStuff })
- .when(
- // if there's only one subcollection with stuff, we want to go straight into it
- ({ subcollectionsWithStuff }) => subcollectionsWithStuff?.length === 1,
- () => subcollectionsWithStuff[0],
- )
- .with(
- { hasStuff: true },
- () => libraryCollection as MiniPickerCollectionItem,
- )
- .otherwise(() => undefined);
return {
- isLoading: isLoadingCollection || isLoadingItems,
- data: showableLibrary,
+ isLoading: isLoadingCollection,
+ data: libraryCollection,
};
};
diff --git a/enterprise/frontend/src/metabase-enterprise/sandboxes/components/EditSandboxingModal/EditSandboxingModal.tsx b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/EditSandboxingModal/EditSandboxingModal.tsx
index 70eb816cc5b..035557ce213 100644
--- a/enterprise/frontend/src/metabase-enterprise/sandboxes/components/EditSandboxingModal/EditSandboxingModal.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/EditSandboxingModal/EditSandboxingModal.tsx
@@ -37,6 +37,8 @@ import {
DataAttributeMappingEditor,
} from "../AttributeMappingEditor";
+import { shouldDisableItem } from "./utils";
+
// eslint-disable-next-line ttag/no-module-declaration -- see metabase#55045
const ERROR_MESSAGE = t`An error occurred.`;
@@ -219,6 +221,7 @@ const EditSandboxingModal = ({
hideModal();
}}
onClose={hideModal}
+ shouldDisableItem={shouldDisableItem}
/>
)}
</div>
diff --git a/enterprise/frontend/src/metabase-enterprise/sandboxes/components/EditSandboxingModal/utils.ts b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/EditSandboxingModal/utils.ts
new file mode 100644
index 00000000000..1813130c417
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/EditSandboxingModal/utils.ts
@@ -0,0 +1,29 @@
+import _ from "underscore";
+
+import type {
+ QuestionPickerItem,
+ QuestionPickerModel,
+} from "metabase/common/components/Pickers/QuestionPicker";
+
+export const shouldDisableItem = (
+ item: QuestionPickerItem,
+ models?: QuestionPickerModel[],
+) =>
+ !(
+ isRootCollection(item) ||
+ isPersonalCollection(item) ||
+ isCollectionWithModels(item, models) ||
+ item.model === "dataset" ||
+ item.model === "card"
+ );
+
+const isRootCollection = (item: QuestionPickerItem) => item.id === "root";
+const isPersonalCollection = (item: QuestionPickerItem) =>
+ item.model === "collection" && item.is_personal && item.location === "/";
+const isCollectionWithModels = (
+ item: QuestionPickerItem,
+ models: QuestionPickerModel[] = [],
+) =>
+ (item.model === "collection" || item.model === "dashboard") &&
+ _.intersection([...(item.below || []), ...(item.here || [])], models).length >
+ 0;
diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformEditor/utils.ts b/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformEditor/utils.ts
index 90904114b4f..e0802af9148 100644
--- a/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformEditor/utils.ts
+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformEditor/utils.ts
@@ -37,5 +37,6 @@ export function getEditorOptions(databases: Database[]): QueryEditorUiOptions {
return false;
},
+ shouldShowLibrary: false,
};
}
diff --git a/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker.tsx b/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/MiniPicker.tsx
similarity index 85%
rename from frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker.tsx
rename to frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/MiniPicker.tsx
index 9e9a717d054..2d62df0fd38 100644
--- a/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker.tsx
+++ b/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/MiniPicker.tsx
@@ -3,18 +3,17 @@ import { useCallback, useEffect, useMemo } from "react";
import { PLUGIN_DATA_STUDIO } from "metabase/plugins";
import { Box, Menu } from "metabase/ui";
-import { useLogRecentItem } from "../../../EntityPicker/hooks/use-log-recent-item";
-import type { DataPickerValue } from "../../DataPicker";
-import { MiniPickerContext } from "../context";
-import type { MiniPickerItem, MiniPickerPickableItem } from "../types";
+import { useLogRecentItem } from "../../../../EntityPicker/hooks/use-log-recent-item";
+import type { DataPickerValue } from "../../../DataPicker";
+import { MiniPickerContext } from "../../context";
+import type { MiniPickerItem, MiniPickerPickableItem } from "../../types";
import {
focusFirstMiniPickerItem,
getFolderAndHiddenFunctions,
useGetPathFromValue,
-} from "../utils";
-
-import { MiniPickerListLoader } from "./MiniPickerItemList";
-import { MiniPickerPane } from "./MiniPickerPane";
+} from "../../utils";
+import { MiniPickerListLoader } from "../MiniPickerItemList";
+import { MiniPickerPane } from "../MiniPickerPane";
export type MiniPickerProps = {
searchQuery?: string;
@@ -26,6 +25,7 @@ export type MiniPickerProps = {
models: MiniPickerPickableItem["model"][];
onBrowseAll?: () => void;
shouldHide?: (item: MiniPickerItem | unknown) => boolean;
+ shouldShowLibrary?: boolean;
};
export function MiniPicker({
@@ -38,6 +38,7 @@ export function MiniPicker({
onBrowseAll,
trapFocus = false,
shouldHide,
+ shouldShowLibrary = true,
}: MiniPickerProps) {
const { data: libraryCollection } =
PLUGIN_DATA_STUDIO.useGetLibraryCollection();
@@ -82,6 +83,7 @@ export function MiniPicker({
models,
canBrowse: !!onBrowseAll,
libraryCollection,
+ shouldShowLibrary,
}}
>
<Menu
diff --git a/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/index.ts b/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/index.ts
new file mode 100644
index 00000000000..bc035fbe2a3
--- /dev/null
+++ b/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/index.ts
@@ -0,0 +1 @@
+export * from "./MiniPicker";
diff --git a/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/test/ee.unit.spec.tsx b/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/test/ee.unit.spec.tsx
new file mode 100644
index 00000000000..c69777ccdc5
--- /dev/null
+++ b/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/test/ee.unit.spec.tsx
@@ -0,0 +1,86 @@
+import fetchMock from "fetch-mock";
+
+import { setupCollectionItemsEndpoint } from "__support__/server-mocks";
+import { screen } from "__support__/ui";
+import {
+ createMockCollection,
+ createMockCollectionItem,
+ createMockTokenFeatures,
+} from "metabase-types/api/mocks";
+
+import { setup } from "./setup";
+
+const LIBRARY_COLLECTION = createMockCollection({
+ name: "library",
+ type: "library",
+ id: 1337,
+ below: ["dataset", "metric"],
+});
+
+const LIBRARY_MODELS_COLELCTION = createMockCollectionItem({
+ name: "data",
+ type: "library-models",
+ model: "collection",
+ id: 1338,
+ here: ["dataset"],
+});
+
+const LIBRARY_METRICS_COLELCTION = createMockCollectionItem({
+ name: "metrics",
+ type: "library-metrics",
+ model: "collection",
+ id: 1339,
+ here: ["metric"],
+});
+
+describe("library", () => {
+ it("should default to the library if it's available", async () => {
+ fetchMock.get("/api/ee/library", LIBRARY_COLLECTION);
+ setupCollectionItemsEndpoint({
+ collection: LIBRARY_COLLECTION,
+ collectionItems: [LIBRARY_MODELS_COLELCTION, LIBRARY_METRICS_COLELCTION],
+ });
+
+ setup({}, createMockTokenFeatures({ data_studio: true }));
+
+ expect(await screen.findByText("metrics")).toBeInTheDocument();
+ expect(await screen.findByText("data")).toBeInTheDocument();
+ });
+
+ it("should hide an empty library collection", async () => {
+ fetchMock.get("/api/ee/library", {
+ ...LIBRARY_COLLECTION,
+ below: ["dataset"],
+ });
+ setupCollectionItemsEndpoint({
+ collection: LIBRARY_COLLECTION,
+ collectionItems: [
+ LIBRARY_MODELS_COLELCTION,
+ { ...LIBRARY_METRICS_COLELCTION, here: undefined },
+ ],
+ });
+
+ setup({}, createMockTokenFeatures({ data_studio: true }));
+
+ expect(await screen.findByText("data")).toBeInTheDocument();
+ expect(screen.queryByText("metrics")).not.toBeInTheDocument();
+ });
+
+ it("should hide respect the shouldShowLibrary prop", async () => {
+ fetchMock.get("/api/ee/library", LIBRARY_COLLECTION);
+ setupCollectionItemsEndpoint({
+ collection: LIBRARY_COLLECTION,
+ collectionItems: [LIBRARY_MODELS_COLELCTION, LIBRARY_METRICS_COLELCTION],
+ });
+
+ setup(
+ { shouldShowLibrary: false },
+ createMockTokenFeatures({ data_studio: true }),
+ );
+
+ expect(await screen.findByText("Mini Db")).toBeInTheDocument();
+ expect(await screen.findByText("Our analytics")).toBeInTheDocument();
+ expect(screen.queryByText("data")).not.toBeInTheDocument();
+ expect(screen.queryByText("metrics")).not.toBeInTheDocument();
+ });
+});
diff --git a/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker.unit.spec.tsx b/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/test/oss.unit.spec.tsx
similarity index 57%
rename from frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker.unit.spec.tsx
rename to frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/test/oss.unit.spec.tsx
index 301efb5968a..7422aea6e0b 100644
--- a/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker.unit.spec.tsx
+++ b/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/test/oss.unit.spec.tsx
@@ -1,222 +1,10 @@
import userEvent from "@testing-library/user-event";
-import {
- findRequests,
- setupCardEndpoints,
- setupCollectionItemsEndpoint,
- setupDatabasesEndpoints,
- setupRecentViewsAndSelectionsEndpoints,
- setupSearchEndpoints,
-} from "__support__/server-mocks";
-import {
- mockGetBoundingClientRect,
- renderWithProviders,
- screen,
-} from "__support__/ui";
-import type { CollectionContentModel } from "metabase-types/api";
-import {
- createMockCard,
- createMockCollection,
- createMockCollectionItem,
- createMockDatabase,
- createMockSearchResult,
- createMockTable,
-} from "metabase-types/api/mocks";
+import { findRequests, setupCardEndpoints } from "__support__/server-mocks";
+import { screen } from "__support__/ui";
+import { createMockCard, createMockCollection } from "metabase-types/api/mocks";
-import { MiniPicker, type MiniPickerProps } from "./MiniPicker";
-
-const collectionItemModels = [
- "dataset",
- "metric",
- "card",
-] as CollectionContentModel[];
-
-const setup = async (props: Partial<MiniPickerProps> = {}) => {
- const onChangeSpy = jest.fn();
- const onCloseSpy = jest.fn();
-
- mockGetBoundingClientRect(); // for virtualization
- const db = createMockDatabase({
- name: "Mini Db",
- id: 1,
- tables: [
- createMockTable({
- id: 1,
- db_id: 1,
- display_name: "weather",
- schema: "public",
- }),
- createMockTable({
- id: 2,
- db_id: 1,
- display_name: "roads",
- schema: "public",
- }),
- createMockTable({
- id: 3,
- db_id: 1,
- display_name: "pokedex",
- schema: "pokemon",
- }),
- createMockTable({
- id: 4,
- db_id: 1,
- display_name: "cards",
- schema: "pokemon",
- }),
- createMockTable({
- id: 5,
- db_id: 1,
- display_name: "digimon",
- schema: "digimon",
- }),
- ],
- });
-
- const db2 = createMockDatabase({
- name: "Solo Db",
- id: 2,
- tables: [
- createMockTable({
- id: 6,
- db_id: 2,
- display_name: "pride",
- schema: "only",
- }),
- createMockTable({
- id: 7,
- db_id: 2,
- display_name: "prejudice",
- schema: "only",
- }),
- ],
- });
-
- const db3 = createMockDatabase({
- name: "NoSchema Db",
- id: 3,
- tables: [
- createMockTable({ id: 8, db_id: 3, display_name: "jane", schema: "" }),
- createMockTable({
- id: 9,
- db_id: 3,
- display_name: "elizabeth",
- schema: "",
- }),
- createMockTable({ id: 10, db_id: 3, display_name: "mary", schema: "" }),
- createMockTable({ id: 11, db_id: 3, display_name: "kitty", schema: "" }),
- createMockTable({ id: 12, db_id: 3, display_name: "lydia", schema: "" }),
- ],
- });
- setupDatabasesEndpoints([db, db2, db3]);
-
- setupCollectionItemsEndpoint({
- collection: createMockCollection({ id: "root", name: "Our analytics" }),
- collectionItems: [
- createMockCollectionItem({
- id: 101,
- model: "collection",
- name: "more things",
- collection_id: "root",
- here: collectionItemModels,
- }),
- createMockCollectionItem({
- id: 102,
- model: "card",
- name: "Brighton",
- collection_id: "root",
- }),
- createMockCollectionItem({
- id: 103,
- model: "dataset",
- name: "Pemberly",
- collection_id: "root",
- }),
- createMockCollectionItem({
- id: 104,
- model: "document",
- name: "Longbourn",
- collection_id: "root",
- }),
- createMockCollectionItem({
- id: 105,
- model: "dashboard",
- name: "NetherField",
- collection_id: "root",
- }),
- createMockCollectionItem({
- id: 106,
- model: "metric",
- name: "Catherine",
- collection_id: "root",
- }),
- ],
- });
-
- setupCollectionItemsEndpoint({
- collection: createMockCollection({
- id: 101,
- name: "more things",
- here: collectionItemModels,
- }),
- collectionItems: [
- createMockCollectionItem({
- id: 201,
- model: "collection",
- name: "even more things",
- collection_id: 101,
- }),
- createMockCollectionItem({
- id: 202,
- model: "card",
- name: "Rosings",
- collection_id: 101,
- }),
- createMockCollectionItem({
- id: 203,
- model: "dataset",
- name: "Meryton",
- collection_id: 101,
- }),
- createMockCollectionItem({
- id: 204,
- model: "document",
- name: "Lambton",
- collection_id: 101,
- }),
- createMockCollectionItem({
- id: 205,
- model: "dashboard",
- name: "Hunsford",
- collection_id: 101,
- }),
- ],
- models: ["card", "dataset", "document", "dashboard", "collection"],
- });
-
- setupSearchEndpoints([
- createMockSearchResult({ id: 301, model: "card", name: "Lucas" }),
- createMockSearchResult({ id: 302, model: "dataset", name: "Forster" }),
- createMockSearchResult({ id: 303, model: "metric", name: "Bingley" }),
- createMockSearchResult({ id: 304, model: "document", name: "Wickham" }),
- createMockSearchResult({ id: 305, model: "collection", name: "Reynolds" }),
- ]);
-
- setupRecentViewsAndSelectionsEndpoints([], ["selections", "views"], {}, true);
-
- renderWithProviders(
- <MiniPicker
- opened
- models={["table", "dataset", "metric", "card"]}
- onClose={onCloseSpy}
- onChange={onChangeSpy}
- {...props}
- />,
- );
- expect(await screen.findByTestId("mini-picker")).toBeInTheDocument();
-
- return { onChangeSpy, onCloseSpy };
-};
+import { setup } from "./setup";
describe("MiniPicker", () => {
it("renders the MiniPicker", async () => {
@@ -230,8 +18,7 @@ describe("MiniPicker", () => {
});
it("does not show browse when the onBrowseAll prop is not provided", async () => {
- //@ts-expect-error - using a null value to test absence of prop
- await setup({ onBrowseAll: null });
+ await setup({ onBrowseAll: undefined });
expect(screen.queryByText("Browse all")).not.toBeInTheDocument();
});
diff --git a/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/test/setup.tsx b/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/test/setup.tsx
new file mode 100644
index 00000000000..a644e9833f7
--- /dev/null
+++ b/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPicker/test/setup.tsx
@@ -0,0 +1,234 @@
+import { setupEnterprisePlugins } from "__support__/enterprise";
+import {
+ setupCollectionItemsEndpoint,
+ setupDatabasesEndpoints,
+ setupRecentViewsAndSelectionsEndpoints,
+ setupSearchEndpoints,
+} from "__support__/server-mocks";
+import { mockSettings } from "__support__/settings";
+import {
+ mockGetBoundingClientRect,
+ renderWithProviders,
+ screen,
+} from "__support__/ui";
+import type { CollectionContentModel, TokenFeatures } from "metabase-types/api";
+import {
+ createMockCollection,
+ createMockCollectionItem,
+ createMockDatabase,
+ createMockSearchResult,
+ createMockSettings,
+ createMockTable,
+ createMockTokenFeatures,
+} from "metabase-types/api/mocks";
+
+import { MiniPicker, type MiniPickerProps } from "..";
+
+const collectionItemModels = [
+ "dataset",
+ "metric",
+ "card",
+] as CollectionContentModel[];
+
+export const setup = async (
+ props: Partial<MiniPickerProps> = {},
+ tokenFeatures: TokenFeatures | null = null,
+) => {
+ const onChangeSpy = jest.fn();
+ const onCloseSpy = jest.fn();
+
+ mockGetBoundingClientRect(); // for virtualization
+ const db = createMockDatabase({
+ name: "Mini Db",
+ id: 1,
+ tables: [
+ createMockTable({
+ id: 1,
+ db_id: 1,
+ display_name: "weather",
+ schema: "public",
+ }),
+ createMockTable({
+ id: 2,
+ db_id: 1,
+ display_name: "roads",
+ schema: "public",
+ }),
+ createMockTable({
+ id: 3,
+ db_id: 1,
+ display_name: "pokedex",
+ schema: "pokemon",
+ }),
+ createMockTable({
+ id: 4,
+ db_id: 1,
+ display_name: "cards",
+ schema: "pokemon",
+ }),
+ createMockTable({
+ id: 5,
+ db_id: 1,
+ display_name: "digimon",
+ schema: "digimon",
+ }),
+ ],
+ });
+
+ const db2 = createMockDatabase({
+ name: "Solo Db",
+ id: 2,
+ tables: [
+ createMockTable({
+ id: 6,
+ db_id: 2,
+ display_name: "pride",
+ schema: "only",
+ }),
+ createMockTable({
+ id: 7,
+ db_id: 2,
+ display_name: "prejudice",
+ schema: "only",
+ }),
+ ],
+ });
+
+ const db3 = createMockDatabase({
+ name: "NoSchema Db",
+ id: 3,
+ tables: [
+ createMockTable({ id: 8, db_id: 3, display_name: "jane", schema: "" }),
+ createMockTable({
+ id: 9,
+ db_id: 3,
+ display_name: "elizabeth",
+ schema: "",
+ }),
+ createMockTable({ id: 10, db_id: 3, display_name: "mary", schema: "" }),
+ createMockTable({ id: 11, db_id: 3, display_name: "kitty", schema: "" }),
+ createMockTable({ id: 12, db_id: 3, display_name: "lydia", schema: "" }),
+ ],
+ });
+ setupDatabasesEndpoints([db, db2, db3]);
+
+ setupCollectionItemsEndpoint({
+ collection: createMockCollection({ id: "root", name: "Our analytics" }),
+ collectionItems: [
+ createMockCollectionItem({
+ id: 101,
+ model: "collection",
+ name: "more things",
+ collection_id: "root",
+ here: collectionItemModels,
+ }),
+ createMockCollectionItem({
+ id: 102,
+ model: "card",
+ name: "Brighton",
+ collection_id: "root",
+ }),
+ createMockCollectionItem({
+ id: 103,
+ model: "dataset",
+ name: "Pemberly",
+ collection_id: "root",
+ }),
+ createMockCollectionItem({
+ id: 104,
+ model: "document",
+ name: "Longbourn",
+ collection_id: "root",
+ }),
+ createMockCollectionItem({
+ id: 105,
+ model: "dashboard",
+ name: "NetherField",
+ collection_id: "root",
+ }),
+ createMockCollectionItem({
+ id: 106,
+ model: "metric",
+ name: "Catherine",
+ collection_id: "root",
+ }),
+ ],
+ });
+
+ setupCollectionItemsEndpoint({
+ collection: createMockCollection({
+ id: 101,
+ name: "more things",
+ here: collectionItemModels,
+ }),
+ collectionItems: [
+ createMockCollectionItem({
+ id: 201,
+ model: "collection",
+ name: "even more things",
+ collection_id: 101,
+ }),
+ createMockCollectionItem({
+ id: 202,
+ model: "card",
+ name: "Rosings",
+ collection_id: 101,
+ }),
+ createMockCollectionItem({
+ id: 203,
+ model: "dataset",
+ name: "Meryton",
+ collection_id: 101,
+ }),
+ createMockCollectionItem({
+ id: 204,
+ model: "document",
+ name: "Lambton",
+ collection_id: 101,
+ }),
+ createMockCollectionItem({
+ id: 205,
+ model: "dashboard",
+ name: "Hunsford",
+ collection_id: 101,
+ }),
+ ],
+ models: ["card", "dataset", "document", "dashboard", "collection"],
+ });
+
+ setupSearchEndpoints([
+ createMockSearchResult({ id: 301, model: "card", name: "Lucas" }),
+ createMockSearchResult({ id: 302, model: "dataset", name: "Forster" }),
+ createMockSearchResult({ id: 303, model: "metric", name: "Bingley" }),
+ createMockSearchResult({ id: 304, model: "document", name: "Wickham" }),
+ createMockSearchResult({ id: 305, model: "collection", name: "Reynolds" }),
+ ]);
+
+ setupRecentViewsAndSelectionsEndpoints([], ["selections", "views"], {}, true);
+
+ const settings = tokenFeatures
+ ? mockSettings(
+ createMockSettings({
+ "token-features": createMockTokenFeatures(tokenFeatures),
+ }),
+ )
+ : {};
+
+ if (tokenFeatures) {
+ setupEnterprisePlugins();
+ }
+
+ renderWithProviders(
+ <MiniPicker
+ opened
+ models={["table", "dataset", "metric", "card"]}
+ onClose={onCloseSpy}
+ onChange={onChangeSpy}
+ {...props}
+ />,
+ { storeInitialState: settings },
+ );
+ expect(await screen.findByTestId("mini-picker")).toBeInTheDocument();
+
+ return { onChangeSpy, onCloseSpy };
+};
diff --git a/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPickerItemList.tsx b/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPickerItemList.tsx
index 6226fd589cc..e26c5e14171 100644
--- a/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPickerItemList.tsx
+++ b/frontend/src/metabase/common/components/Pickers/MiniPicker/components/MiniPickerItemList.tsx
@@ -1,4 +1,5 @@
import { t } from "ttag";
+import _ from "underscore";
import {
skipToken,
@@ -51,7 +52,8 @@ export function MiniPickerItemList() {
function RootItemList() {
const { data: databases } = useListDatabasesQuery();
- const { setPath, isHidden } = useMiniPickerContext();
+ const { setPath, isHidden, models, shouldShowLibrary } =
+ useMiniPickerContext();
const { data: libraryCollection, isLoading } =
PLUGIN_DATA_STUDIO.useGetLibraryCollection();
const enableNestedQueries = useSetting("enable-nested-queries");
@@ -82,8 +84,11 @@ function RootItemList() {
</ItemList>
);
}
-
- if (libraryCollection) {
+ if (
+ libraryCollection &&
+ _.intersection(models, libraryCollection.below || []).length &&
+ shouldShowLibrary
+ ) {
return (
<CollectionItemList
parent={{
diff --git a/frontend/src/metabase/common/components/Pickers/MiniPicker/context.ts b/frontend/src/metabase/common/components/Pickers/MiniPicker/context.ts
index 6a605ac90cc..82dd88f2c3a 100644
--- a/frontend/src/metabase/common/components/Pickers/MiniPicker/context.ts
+++ b/frontend/src/metabase/common/components/Pickers/MiniPicker/context.ts
@@ -24,6 +24,7 @@ export interface MiniPickerContextValue {
canBrowse: boolean;
onBrowseAll?: () => void;
libraryCollection?: MiniPickerCollectionItem;
+ shouldShowLibrary?: boolean;
}
export const MiniPickerContext = createContext<
diff --git a/frontend/src/metabase/common/components/Pickers/QuestionPicker/components/QuestionPicker.tsx b/frontend/src/metabase/common/components/Pickers/QuestionPicker/components/QuestionPicker.tsx
index f1554dc1a78..a6949118430 100644
--- a/frontend/src/metabase/common/components/Pickers/QuestionPicker/components/QuestionPicker.tsx
+++ b/frontend/src/metabase/common/components/Pickers/QuestionPicker/components/QuestionPicker.tsx
@@ -49,7 +49,10 @@ interface QuestionPickerProps {
onItemSelect: (item: QuestionPickerItem) => void;
onPathChange: (path: QuestionPickerStatePath) => void;
onTablesPathChange?: (path: TablePickerStatePath) => void;
- shouldDisableItem?: (item: QuestionPickerItem) => boolean;
+ shouldDisableItem?: (
+ item: QuestionPickerItem,
+ models?: QuestionPickerModel[],
+ ) => boolean;
}
export const QuestionPicker = ({
@@ -216,7 +219,11 @@ export const QuestionPicker = ({
path={path}
listResolver={CollectionItemPickerResolver}
shouldShowItem={shouldShowItem}
- shouldDisableItem={shouldDisableItem}
+ shouldDisableItem={
+ shouldDisableItem
+ ? (item) => shouldDisableItem(item, models)
+ : undefined
+ }
tablesPath={tablesPath}
onTablesPathChange={onTablesPathChange}
/>
diff --git a/frontend/src/metabase/common/components/Pickers/QuestionPicker/components/QuestionPickerModal.tsx b/frontend/src/metabase/common/components/Pickers/QuestionPicker/components/QuestionPickerModal.tsx
index 3c2d37c480f..97a77b4cc6d 100644
--- a/frontend/src/metabase/common/components/Pickers/QuestionPicker/components/QuestionPickerModal.tsx
+++ b/frontend/src/metabase/common/components/Pickers/QuestionPicker/components/QuestionPickerModal.tsx
@@ -31,7 +31,10 @@ interface QuestionPickerModalProps {
value?: QuestionPickerValue;
models?: QuestionPickerModel[];
recentFilter?: (items: RecentItem[]) => RecentItem[];
- shouldDisableItem?: (item: QuestionPickerItem) => boolean;
+ shouldDisableItem?: (
+ item: QuestionPickerItem,
+ models?: QuestionPickerModel[],
+ ) => boolean;
}
const canSelectItem = (
@@ -110,7 +113,7 @@ export const QuestionPickerModal = ({
<QuestionPicker
initialValue={value}
models={["card", "dashboard"]}
- options={options}
+ options={{ ...options, showLibrary: false }}
path={questionsPath}
onInit={onItemSelect}
onItemSelect={onItemSelect}
diff --git a/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditor.tsx b/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditor.tsx
index 727be62801c..97b0f1e229d 100644
--- a/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditor.tsx
+++ b/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditor.tsx
@@ -97,6 +97,7 @@ export function QueryEditor({
isShowingSnippetSidebar={uiState.sidebarType === "snippet"}
shouldDisableItem={uiOptions?.shouldDisableDataPickerItem}
shouldDisableDatabase={uiOptions?.shouldDisableDatabasePickerItem}
+ shouldShowLibrary={uiOptions?.shouldShowLibrary}
onChange={setQuestion}
onRunQuery={runQuery}
onCancelQuery={cancelQuery}
diff --git a/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditorBody/QueryEditorBody.tsx b/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditorBody/QueryEditorBody.tsx
index 34073614c03..a17b4826802 100644
--- a/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditorBody/QueryEditorBody.tsx
+++ b/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditorBody/QueryEditorBody.tsx
@@ -55,6 +55,7 @@ type QueryEditorBodyProps = {
shouldDisableItem?: (
item: DataPickerItem | CollectionPickerItem | RecentCollectionItem,
) => boolean;
+ shouldShowLibrary?: boolean;
onChange: (newQuestion: Question) => void;
onRunQuery: () => Promise<void>;
onToggleDataReference: () => void;
@@ -82,6 +83,7 @@ export function QueryEditorBody({
isShowingSnippetSidebar,
shouldDisableDatabase,
shouldDisableItem,
+ shouldShowLibrary,
onChange,
onRunQuery,
onToggleDataReference,
@@ -99,8 +101,8 @@ export function QueryEditorBody({
const editorHeight = useInitialEditorHeight(isNative);
const dataPickerOptions = useMemo(
- () => ({ shouldDisableItem, shouldDisableDatabase }),
- [shouldDisableItem, shouldDisableDatabase],
+ () => ({ shouldDisableItem, shouldDisableDatabase, shouldShowLibrary }),
+ [shouldDisableItem, shouldDisableDatabase, shouldShowLibrary],
);
const resizableBoxProps: Partial<ResizableBoxProps> = useMemo(
diff --git a/frontend/src/metabase/querying/editor/types.ts b/frontend/src/metabase/querying/editor/types.ts
index b19c0944b2f..b38db85b728 100644
--- a/frontend/src/metabase/querying/editor/types.ts
+++ b/frontend/src/metabase/querying/editor/types.ts
@@ -53,4 +53,5 @@ export type QueryEditorUiOptions = {
shouldDisableDatabasePickerItem?: (
item: QueryEditorDatabasePickerItem,
) => boolean;
+ shouldShowLibrary?: false;
};
diff --git a/frontend/src/metabase/querying/notebook/components/NotebookDataPicker/NotebookDataPicker.tsx b/frontend/src/metabase/querying/notebook/components/NotebookDataPicker/NotebookDataPicker.tsx
index 58981ea3254..c8e4a8ead92 100644
--- a/frontend/src/metabase/querying/notebook/components/NotebookDataPicker/NotebookDataPicker.tsx
+++ b/frontend/src/metabase/querying/notebook/components/NotebookDataPicker/NotebookDataPicker.tsx
@@ -54,6 +54,7 @@ export interface NotebookDataPickerProps {
) => boolean;
shouldDisableDatabase?: (item: QueryEditorDatabasePickerItem) => boolean;
columnPicker: React.ReactNode;
+ shouldShowLibrary?: boolean;
}
export function NotebookDataPicker({
@@ -70,6 +71,7 @@ export function NotebookDataPicker({
onChange,
shouldDisableItem,
shouldDisableDatabase,
+ shouldShowLibrary,
columnPicker,
}: NotebookDataPickerProps) {
const store = useStore();
@@ -133,6 +135,7 @@ export function NotebookDataPicker({
onChange={handleChange}
shouldDisableItem={shouldDisableItem}
shouldDisableDatabase={shouldDisableDatabase}
+ shouldShowLibrary={shouldShowLibrary}
/>
);
}
@@ -154,6 +157,7 @@ type ModernDataPickerProps = {
item: DataPickerItem | CollectionPickerItem | RecentCollectionItem,
) => boolean;
shouldDisableDatabase?: (database: QueryEditorDatabasePickerItem) => boolean;
+ shouldShowLibrary?: boolean;
};
function ModernDataPicker({
@@ -169,6 +173,7 @@ function ModernDataPicker({
onChange,
shouldDisableItem,
shouldDisableDatabase,
+ shouldShowLibrary,
}: ModernDataPickerProps) {
const context = useNotebookContext();
const modelList = getModelFilterList(context, hasMetrics);
@@ -225,6 +230,7 @@ function ModernDataPicker({
setIsOpened(false);
}}
shouldHide={shouldHide}
+ shouldShowLibrary={shouldShowLibrary}
/>
{isOpened && isBrowsing && (
<DataPickerModal