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
Re-add new dashboard button in move modal and collection picker when dashboards are selectable (#72363)
Nick Fitzpatrick2d4279d59604
diff --git a/frontend/src/metabase/collections/containers/FormCollectionAndDashboardPicker/FormCollectionAndDashboardPicker.tsx b/frontend/src/metabase/collections/containers/FormCollectionAndDashboardPicker/FormCollectionAndDashboardPicker.tsxindex cebc3b6bb9a..769762ccaf4 100644--- a/frontend/src/metabase/collections/containers/FormCollectionAndDashboardPicker/FormCollectionAndDashboardPicker.tsx+++ b/frontend/src/metabase/collections/containers/FormCollectionAndDashboardPicker/FormCollectionAndDashboardPicker.tsx@@ -159,6 +159,9 @@ export function FormCollectionAndDashboardPicker({filterPersonalCollections !== "only" ||isOpenCollectionInPersonalCollection;+ const canCreateDashboards =+ collectionPickerModalProps?.models?.includes("dashboard") ?? false;+const options = useMemo<EntityPickerOptions>(() => ({hasPersonalCollections: filterPersonalCollections !== "exclude",@@ -169,12 +172,18 @@ export function FormCollectionAndDashboardPicker({hasConfirmButtons: true,namespaces: type === "snippet-collections" ? ["snippets"] : undefined,canCreateCollections: showCreateNewCollectionOption,+ canCreateDashboards,confirmButtonText: (item) =>item?.model === "dashboard"? t`Select this dashboard`: t`Select this collection`,}),- [filterPersonalCollections, type, showCreateNewCollectionOption],+ [+ filterPersonalCollections,+ type,+ showCreateNewCollectionOption,+ canCreateDashboards,+ ],);const [fetchDashboard] = useLazyGetDashboardQuery();diff --git a/frontend/src/metabase/collections/containers/FormCollectionAndDashboardPicker/FormCollectionAndDashboardPicker.unit.spec.tsx b/frontend/src/metabase/collections/containers/FormCollectionAndDashboardPicker/FormCollectionAndDashboardPicker.unit.spec.tsxnew file mode 100644index 00000000000..791e9baabe8--- /dev/null+++ b/frontend/src/metabase/collections/containers/FormCollectionAndDashboardPicker/FormCollectionAndDashboardPicker.unit.spec.tsx@@ -0,0 +1,113 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";++import {+ setupCollectionByIdEndpoint,+ setupCollectionItemsEndpoint,+ setupCollectionsEndpoints,+ setupDatabasesEndpoints,+ setupRecentViewsAndSelectionsEndpoints,+ setupRootCollectionItemsEndpoint,+} from "__support__/server-mocks";+import {+ mockGetBoundingClientRect,+ renderWithProviders,+ screen,+ waitForLoaderToBeRemoved,+} from "__support__/ui";+import type { OmniPickerItem } from "metabase/common/components/Pickers";+import { ROOT_COLLECTION } from "metabase/entities/collections";+import { FormProvider } from "metabase/forms";+import {+ createMockCollection,+ createMockCollectionItem,+} from "metabase-types/api/mocks";++import { FormCollectionAndDashboardPicker } from "./FormCollectionAndDashboardPicker";++const rootCollection = createMockCollection(ROOT_COLLECTION);++const collection1 = createMockCollection({+ id: 11,+ name: "First Collection",+ here: ["card"],+ below: ["card"],+ location: "/",+ can_write: true,+});++const rootCollectionItems = [+ createMockCollectionItem({+ id: 11,+ model: "collection",+ name: collection1.name,+ here: ["collection"],+ below: ["collection", "card"],+ collection_id: null,+ can_write: true,+ }),+];++function setup({ models }: { models: OmniPickerItem["model"][] }) {+ process.env.OVERSCAN = "20";+ mockGetBoundingClientRect();++ setupRecentViewsAndSelectionsEndpoints([], ["views", "selections"]);+ setupDatabasesEndpoints([]);+ setupCollectionsEndpoints({+ collections: [collection1],+ rootCollection,+ });+ setupCollectionByIdEndpoint({ collections: [collection1] });+ setupRootCollectionItemsEndpoint({ rootCollectionItems });+ setupCollectionItemsEndpoint({+ collection: collection1,+ collectionItems: [],+ });+ fetchMock.get("path:/api/search", { data: [] });+ fetchMock.get("path:/api/user/recipients", { data: [] });++ renderWithProviders(+ <FormProvider+ initialValues={{ collection_id: "root", dashboard_id: undefined }}+ onSubmit={jest.fn()}+ >+ <FormCollectionAndDashboardPicker+ collectionIdFieldName="collection_id"+ dashboardIdFieldName="dashboard_id"+ title="Where do you want to save this?"+ collectionPickerModalProps={{ models }}+ />+ </FormProvider>,+ );+}++describe("FormCollectionAndDashboardPicker", () => {+ it('should show "New dashboard" button when models include "dashboard"', async () => {+ setup({ models: ["collection", "dashboard"] });++ await userEvent.click(+ screen.getByTestId("dashboard-and-collection-picker-button"),+ );+ await waitForLoaderToBeRemoved();++ expect(+ await screen.findByRole("button", { name: /new dashboard/i }),+ ).toBeInTheDocument();+ });++ it('should not show "New dashboard" button when models do not include "dashboard"', async () => {+ setup({ models: ["collection"] });++ await userEvent.click(+ screen.getByTestId("dashboard-and-collection-picker-button"),+ );+ await waitForLoaderToBeRemoved();++ // Wait for picker content to load+ expect(await screen.findByText("Our analytics")).toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: /new dashboard/i }),+ ).not.toBeInTheDocument();+ });+});diff --git a/frontend/src/metabase/collections/containers/FormCollectionPicker/FormCollectionPicker.tsx b/frontend/src/metabase/collections/containers/FormCollectionPicker/FormCollectionPicker.tsxindex 8c20b4bc2cb..00e6d45866d 100644--- a/frontend/src/metabase/collections/containers/FormCollectionPicker/FormCollectionPicker.tsx+++ b/frontend/src/metabase/collections/containers/FormCollectionPicker/FormCollectionPicker.tsx@@ -1,5 +1,4 @@import { useField } from "formik";-import type { HTMLAttributes } from "react";import { useCallback, useEffect, useMemo, useState } from "react";import { t } from "ttag";@@ -22,11 +21,11 @@ import { TransformCollectionName } from "metabase/common/components/TransformColimport { useUniqueId } from "metabase/common/hooks/use-unique-id";import { Collections } from "metabase/entities/collections";import { PLUGIN_TENANTS } from "metabase/plugins";-import { Button, Icon, Input } from "metabase/ui";+import { Button, Icon, Input, type InputWrapperProps } from "metabase/ui";import { useSelector } from "metabase/utils/redux";import type { CollectionId, CollectionNamespace } from "metabase-types/api";-interface FormCollectionPickerProps extends HTMLAttributes<HTMLDivElement> {+interface FormCollectionPickerProps extends InputWrapperProps {name: string;title?: string;placeholder?: string;@@ -75,6 +74,7 @@ function FormCollectionPicker({entityType,collectionPickerModalProps,onCollectionSelect,+ ...rest}: FormCollectionPickerProps) {const id = useUniqueId();@@ -161,6 +161,7 @@ function FormCollectionPicker({label={title}labelProps={{ htmlFor: id }}error={touched ? error : undefined}+ {...rest}><Buttondata-testid="collection-picker-button"diff --git a/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ButtonBar.tsx b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ButtonBar.tsxindex a2770442d1c..54ddcdc1d55 100644--- a/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ButtonBar.tsx+++ b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ButtonBar.tsx@@ -71,7 +71,7 @@ export const ButtonBar = ({overflowX: "auto",}}>- <Flex gap="md">+ <Flex gap="md" mr="md"><NewCollectionDialog /><NewDashboardDialog /></Flex>diff --git a/frontend/src/metabase/common/components/Pickers/EntityPicker/components/NewCollectionDialog/NewCollectionDialog.tsx b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/NewCollectionDialog/NewCollectionDialog.tsxindex 18e2f2ae16b..57ab4de9fdc 100644--- a/frontend/src/metabase/common/components/Pickers/EntityPicker/components/NewCollectionDialog/NewCollectionDialog.tsx+++ b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/NewCollectionDialog/NewCollectionDialog.tsx@@ -108,7 +108,7 @@ export const NewCollectionDialog = () => {return (<>- <Button onClick={open} disabled={!canCreateHere} mr="md">+ <Button onClick={open} disabled={!canCreateHere}>{lastCollection?.namespace === "transforms" ||lastCollection?.namespace === "snippets"? t`New folder`diff --git a/frontend/src/metabase/common/components/Pickers/MoveModal/MoveModal.render.unit.spec.tsx b/frontend/src/metabase/common/components/Pickers/MoveModal/MoveModal.render.unit.spec.tsxnew file mode 100644index 00000000000..283c3e734e5--- /dev/null+++ b/frontend/src/metabase/common/components/Pickers/MoveModal/MoveModal.render.unit.spec.tsx@@ -0,0 +1,126 @@+import fetchMock from "fetch-mock";++import {+ setupCollectionByIdEndpoint,+ setupCollectionItemsEndpoint,+ setupCollectionsEndpoints,+ setupDatabasesEndpoints,+ setupRecentViewsAndSelectionsEndpoints,+ setupRootCollectionItemsEndpoint,+} from "__support__/server-mocks";+import {+ mockGetBoundingClientRect,+ renderWithProviders,+ screen,+ waitForLoaderToBeRemoved,+} from "__support__/ui";+import { ROOT_COLLECTION } from "metabase/entities/collections";+import {+ createMockCollection,+ createMockCollectionItem,+} from "metabase-types/api/mocks";++import type { OmniPickerCollectionItem } from "../EntityPicker";++import { MoveModal } from "./MoveModal";++const rootCollection = createMockCollection(ROOT_COLLECTION);++const collection1 = createMockCollection({+ id: 11,+ name: "First Collection",+ here: ["card"],+ below: ["card"],+ location: "/",+ can_write: true,+});++const rootCollectionItems = [+ createMockCollectionItem({+ id: 11,+ model: "collection",+ name: collection1.name,+ here: ["collection"],+ below: ["collection", "card"],+ collection_id: null,+ can_write: true,+ }),+];++function setupEndpoints() {+ process.env.OVERSCAN = "20";+ mockGetBoundingClientRect();++ setupRecentViewsAndSelectionsEndpoints([], ["views", "selections"]);+ setupDatabasesEndpoints([]);+ setupCollectionsEndpoints({+ collections: [collection1],+ rootCollection,+ });+ setupCollectionByIdEndpoint({ collections: [collection1] });+ setupRootCollectionItemsEndpoint({ rootCollectionItems });+ setupCollectionItemsEndpoint({+ collection: collection1,+ collectionItems: [],+ });+ fetchMock.get("path:/api/search", { data: [] });+ fetchMock.get("path:/api/user/recipients", { data: [] });+}++const movingCard = {+ id: 1,+ name: "Test Question",+ model: "card",+ collection: { id: 11, name: "First Collection", authority_level: null },+} as OmniPickerCollectionItem;++const movingDashboard = {+ id: 2,+ name: "Test Dashboard",+ model: "dashboard",+ collection: { id: 11, name: "First Collection", authority_level: null },+} as OmniPickerCollectionItem;++describe("MoveModal", () => {+ beforeEach(() => {+ setupEndpoints();+ });++ it('should show "New dashboard" button when moving a question (canMoveToDashboard=true)', async () => {+ renderWithProviders(+ <MoveModal+ title="Move question"+ onClose={jest.fn()}+ onMove={jest.fn()}+ movingItem={movingCard}+ canMoveToDashboard+ />,+ );++ await waitForLoaderToBeRemoved();++ expect(+ await screen.findByRole("button", { name: /new dashboard/i }),+ ).toBeInTheDocument();+ });++ it('should not show "New dashboard" button when canMoveToDashboard is false', async () => {+ renderWithProviders(+ <MoveModal+ title="Move dashboard"+ onClose={jest.fn()}+ onMove={jest.fn()}+ movingItem={movingDashboard}+ canMoveToDashboard={false}+ />,+ );++ await waitForLoaderToBeRemoved();++ // Wait for picker content to load+ expect(await screen.findByText("Our analytics")).toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: /new dashboard/i }),+ ).not.toBeInTheDocument();+ });+});diff --git a/frontend/src/metabase/common/components/Pickers/MoveModal/MoveModal.tsx b/frontend/src/metabase/common/components/Pickers/MoveModal/MoveModal.tsxindex a42a67fe48a..be86c12a07b 100644--- a/frontend/src/metabase/common/components/Pickers/MoveModal/MoveModal.tsx+++ b/frontend/src/metabase/common/components/Pickers/MoveModal/MoveModal.tsx@@ -183,6 +183,7 @@ export const MoveModal = ({hasRootCollection: true,hasConfirmButtons: true,canCreateCollections: true,+ canCreateDashboards: canMoveToDashboard ?? false,hasPersonalCollections: true,confirmButtonText: t`Move`,}}@@ -336,6 +337,7 @@ export const BulkMoveModal = ({hasRootCollection: true,hasConfirmButtons: true,canCreateCollections: true,+ canCreateDashboards: canMoveToDashboard,confirmButtonText: t`Move`,}}isDisabledItem={shouldDisableItem}diff --git a/frontend/src/metabase/dashboard/containers/CopyDashboardForm.tsx b/frontend/src/metabase/dashboard/containers/CopyDashboardForm.tsxindex c7b5158cb45..867eb85e262 100644--- a/frontend/src/metabase/dashboard/containers/CopyDashboardForm.tsx+++ b/frontend/src/metabase/dashboard/containers/CopyDashboardForm.tsx@@ -138,6 +138,7 @@ function CopyDashboardForm({{!hideShallowCopy && (<FormCheckbox+ mt="1rem"name="is_shallow_copy"label={<Group align="center" gap="xs">@@ -153,7 +154,7 @@ function CopyDashboardForm({/>)}- <FormFooter>+ <FormFooter mt="1.5rem"><FormErrorMessage inline />{!!onClose && (<Button type="button" onClick={onClose}>{t`Cancel`}</Button>diff --git a/frontend/src/metabase/ui/components/inputs/Input/index.ts b/frontend/src/metabase/ui/components/inputs/Input/index.tsindex ea6e3603243..1633ccacdd3 100644--- a/frontend/src/metabase/ui/components/inputs/Input/index.ts+++ b/frontend/src/metabase/ui/components/inputs/Input/index.ts@@ -1,2 +1,2 @@-export { Input } from "@mantine/core";+export { Input, type InputWrapperProps } from "@mantine/core";export { inputOverrides } from "./Input.config";