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
Hide CSV panel with no meaningful uploadable dbs (#62854)
Jakub Chodorowicz11f98faf74e5
diff --git a/e2e/test/scenarios/onboarding/add-initial-data.cy.spec.ts b/e2e/test/scenarios/onboarding/add-initial-data.cy.spec.tsindex a2d0a826b29..85836832f06 100644--- a/e2e/test/scenarios/onboarding/add-initial-data.cy.spec.ts+++ b/e2e/test/scenarios/onboarding/add-initial-data.cy.spec.ts@@ -383,6 +383,14 @@ describe("Add data modal", () => {.should("contain", "value1").and("contain", "value2");});++ it("should be hidden for non-admins without upload permissions", () => {+ cy.signInAsNormalUser();+ cy.visit("/");+ cy.findByRole("tab", { name: /^Data/i }).within(() => {+ cy.findByLabelText("Add data").should("not.exist");+ });+ });});});diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/AddDataModal.tsx b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/AddDataModal.tsxindex fb7bf4f822d..8a139703593 100644--- a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/AddDataModal.tsx+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/AddDataModal.tsx@@ -1,47 +1,51 @@-import { useState } from "react";+import { useEffect, useMemo, useState } from "react";import { t } from "ttag";import { useListDatabasesQuery } from "metabase/api";import { useSetting } from "metabase/common/hooks";import CS from "metabase/css/core/index.css";-import { useSelector } from "metabase/lib/redux";import { PLUGIN_UPLOAD_MANAGEMENT } from "metabase/plugins";-import { canAccessSettings, getUserIsAdmin } from "metabase/selectors/user";-import { Box, Icon, Modal, Tabs } from "metabase/ui";+import { Box, Icon, type IconName, Modal, Tabs } from "metabase/ui";import S from "./AddDataModal.module.css";import { CSVPanel } from "./Panels/CSVPanel";import { DatabasesPanel } from "./Panels/DatabasesPanel";import { PanelsHeader } from "./Panels/PanelsHeader";import { trackAddDataEvent } from "./analytics";-import { isValidTab } from "./utils";+import { useAddDataPermissions } from "./use-add-data-permission";+import { hasMeaningfulUploadableDatabases, isValidTab } from "./utils";interface AddDataModalProps {opened: boolean;onClose: () => void;}+interface Tabs {+ name: string;+ value: "csv" | "db" | "gsheets";+ isVisible: boolean;+ iconName: IconName;+}+export const AddDataModal = ({ opened, onClose }: AddDataModalProps) => {- const { data: databaseResponse } = useListDatabasesQuery();+ const { areUploadsEnabled, canUploadToDatabase, canManageUploads, isAdmin } =+ useAddDataPermissions();- const [activeTab, setActiveTab] = useState<string | null>("csv");+ const { data: databasesResponse } = useListDatabasesQuery();+ const { data: uploadableDatabasesResponse } = useListDatabasesQuery({+ include_only_uploadable: true,+ });- const isHosted = useSetting("is-hosted?");+ const hasUploadableDatabases = hasMeaningfulUploadableDatabases({+ allDatabases: databasesResponse?.data,+ allUploadableDatabases: uploadableDatabasesResponse?.data,+ });- const isAdmin = useSelector(getUserIsAdmin);- const userCanAccessSettings = useSelector(canAccessSettings);+ const shouldShowUploadsPanel = hasUploadableDatabases || canUploadToDatabase;- const databases = databaseResponse?.data;- const uploadDbId = useSetting("uploads-settings")?.db_id;- const uploadDB = databases?.find((db) => db.id === uploadDbId);+ const [activeTab, setActiveTab] = useState<Tabs["value"] | null>(null);- /**- * This covers the case where instance has the attached dwh. In such cases- * uploads are enabled by default.- */- const areUploadsEnabled = !!uploadDbId;- const canUploadToDatabase = !!uploadDB?.can_upload;- const canManageUploads = userCanAccessSettings;+ const isHosted = useSetting("is-hosted?");const handleTabChange = (tabValue: string | null) => {if (tabValue === activeTab || !isValidTab(tabValue)) {@@ -58,6 +62,31 @@ export const AddDataModal = ({ opened, onClose }: AddDataModalProps) => {setActiveTab(tabValue);};+ const tabs = useMemo(() => {+ return [+ {+ name: t`CSV`,+ value: "csv",+ isVisible: shouldShowUploadsPanel,+ iconName: "table2",+ },+ { name: t`Database`, value: "db", isVisible: true, iconName: "database" },+ {+ name: t`Google Sheets`,+ value: "gsheets",+ isVisible: isHosted,+ iconName: "document",+ },+ ] satisfies Tabs[];+ }, [shouldShowUploadsPanel, isHosted]);++ useEffect(() => {+ const firstVisibleTab = tabs.find((tab) => tab.isVisible);+ if (firstVisibleTab) {+ setActiveTab(firstVisibleTab.value);+ }+ }, [tabs]);+return (<Modal.Root opened={opened} onClose={onClose} size="auto"><Modal.Overlay />@@ -79,20 +108,17 @@ export const AddDataModal = ({ opened, onClose }: AddDataModalProps) => {<Modal.Title fz="lg">{t`Add data`}</Modal.Title></Box><Tabs.List px="md" pb="lg">- <Tabs.Tab value="csv" leftSection={<Icon name="table2" />}>- {t`CSV`}- </Tabs.Tab>- <Tabs.Tab value="db" leftSection={<Icon name="database" />}>- {t`Database`}- </Tabs.Tab>- {isHosted && (- <Tabs.Tab- value="gsheets"- leftSection={<Icon name="document" />}- >- {t`Google Sheets`}- </Tabs.Tab>- )}+ {tabs+ .filter((tab) => tab.isVisible)+ .map((tab) => (+ <Tabs.Tab+ key={tab.value}+ value={tab.value}+ leftSection={<Icon name={tab.iconName} />}+ >+ {tab.name}+ </Tabs.Tab>+ ))}</Tabs.List></Box><Box component="main" w="30rem" className={S.panelContainer}>diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/common.unit.spec.tsx b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/common.unit.spec.tsxindex 9a6603af334..1be5b4c181a 100644--- a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/common.unit.spec.tsx+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/common.unit.spec.tsx@@ -38,10 +38,10 @@ describe("AddDataModal", () => {).not.toBeInTheDocument();});- it("should have csv tab selected by default", () => {+ it("should have csv tab selected by default", async () => {setup();- const csvTab = screen.getByRole("tab", { name: /CSV$/ });+ const csvTab = await screen.findByRole("tab", { name: /CSV$/ });expect(csvTab).toHaveAttribute("data-active", "true");});@@ -49,7 +49,7 @@ describe("AddDataModal", () => {setup();const databaseTab = screen.getByRole("tab", { name: /Database$/ });- const csvTab = screen.getByRole("tab", { name: /CSV$/ });+ const csvTab = await screen.findByRole("tab", { name: /CSV$/ });expect(csvTab).toHaveAttribute("data-active", "true");await userEvent.click(screen.getByRole("tab", { name: /Database$/ }));@@ -60,7 +60,7 @@ describe("AddDataModal", () => {it("should maintain the tab selection state", async () => {setup();- const csvTab = screen.getByRole("tab", { name: /CSV$/ });+ const csvTab = await screen.findByRole("tab", { name: /CSV$/ });expect(csvTab).toHaveAttribute("data-active", "true");await userEvent.click(csvTab);@@ -72,6 +72,9 @@ describe("AddDataModal", () => {it("should show database panel for admin users", async () => {setup({ isAdmin: true });+ expect(+ await screen.findByRole("tab", { name: /CSV$/ }),+ ).toBeInTheDocument();await userEvent.click(screen.getByRole("tab", { name: /Database$/ }));expect(screen.getByRole("tab", { name: /Database$/ }),@@ -88,6 +91,9 @@ describe("AddDataModal", () => {it("should show limited view for non-admin users", async () => {setup({ isAdmin: false });+ expect(+ await screen.findByRole("tab", { name: /CSV$/ }),+ ).toBeInTheDocument();await userEvent.click(screen.getByRole("tab", { name: /Database$/ }));expect(screen.getByRole("tab", { name: /Database$/ }),@@ -117,12 +123,16 @@ describe("AddDataModal", () => {it("doesn't show admin email when there isn't one", async () => {setup({ isAdmin: false, adminEmail: null });+ expect(+ await screen.findByRole("tab", { name: /CSV$/ }),+ ).toBeInTheDocument();+await userEvent.click(screen.getByRole("tab", { name: /Database$/ }));const alert = screen.getByRole("alert");expect(alert).toBeInTheDocument();expect(- within(alert).getByText(+ await within(alert).findByText("To add a new database, please contact your administrator.",),).toBeInTheDocument();@@ -136,7 +146,7 @@ describe("AddDataModal", () => {it("should show CSV panel for admin users", async () => {setup({ isAdmin: true, uploadsEnabled: true });- expect(screen.getByText("Manage uploads")).toBeInTheDocument();+ expect(await screen.findByText("Manage uploads")).toBeInTheDocument();expect(await screen.findByText("Drag and drop a file here"),@@ -151,7 +161,7 @@ describe("AddDataModal", () => {it("should prompt the admin to enable uploads", async () => {setup({ isAdmin: true, uploadsEnabled: false });- expect(screen.getByText("Manage uploads")).toBeInTheDocument();+ expect(await screen.findByText("Manage uploads")).toBeInTheDocument();expect(screen.getByRole("heading", { name: "Upload CSV files" }),).toBeInTheDocument();@@ -168,9 +178,11 @@ describe("AddDataModal", () => {it("should prompt the admin to enable uploads with an upsell on a hosted instance", async () => {setup({ isAdmin: true, uploadsEnabled: false, isHosted: true });- expect(screen.getByText("Manage uploads")).toBeInTheDocument();- expect(screen.getByText("Enable uploads")).toBeInTheDocument();- expect(screen.getByText("Add Metabase Storage")).toBeInTheDocument();+ expect(await screen.findByText("Manage uploads")).toBeInTheDocument();+ expect(await screen.findByText("Enable uploads")).toBeInTheDocument();+ expect(+ await screen.findByText("Add Metabase Storage"),+ ).toBeInTheDocument();});it("regular user should be instructed to contact their admin in order to enable uploads", async () => {@@ -178,7 +190,7 @@ describe("AddDataModal", () => {expect(screen.queryByText("Manage uploads")).not.toBeInTheDocument();expect(- screen.getByRole("heading", { name: "Upload CSV files" }),+ await screen.findByRole("heading", { name: "Upload CSV files" }),).toBeInTheDocument();expect(screen.getByText(@@ -202,7 +214,7 @@ describe("AddDataModal", () => {expect(screen.queryByText("Manage uploads")).not.toBeInTheDocument();expect(- screen.getByRole("heading", { name: "Upload CSV files" }),+ await screen.findByRole("heading", { name: "Upload CSV files" }),).toBeInTheDocument();expect(screen.getByText(@@ -389,14 +401,16 @@ describe("AddDataModal", () => {});describe("Google Sheets panel", () => {- it("should not exist in OSS binaries", () => {+ it("should not exist in OSS binaries", async () => {// Hosting is a premium featuresetup({ isHosted: false });expect(screen.getByRole("tab", { name: /Database$/ }),).toBeInTheDocument();- expect(screen.getByRole("tab", { name: /CSV$/ })).toBeInTheDocument();+ expect(+ await screen.findByRole("tab", { name: /CSV$/ }),+ ).toBeInTheDocument();expect(screen.queryByRole("tab", { name: /Google Sheets$/ }),).not.toBeInTheDocument();diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/enterprise.unit.spec.tsx b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/enterprise.unit.spec.tsxindex ccfacbfcba1..acab75b20c7 100644--- a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/enterprise.unit.spec.tsx+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/enterprise.unit.spec.tsx@@ -4,13 +4,15 @@ import { setup } from "./setup";describe("AddDataModal (Unactivated enterprise binary)", () => {describe("Google Sheets panel", () => {- it("should not exist on self-hosted instances", () => {+ it("should not exist on self-hosted instances", async () => {setup({ hasEnterprisePlugins: true, isHosted: false });expect(screen.getByRole("tab", { name: /Database$/ }),).toBeInTheDocument();- expect(screen.getByRole("tab", { name: /CSV$/ })).toBeInTheDocument();+ expect(+ await screen.findByRole("tab", { name: /CSV$/ }),+ ).toBeInTheDocument();expect(screen.queryByRole("tab", { name: /Google Sheets$/ }),).not.toBeInTheDocument();diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/premium-advanced-permissions.unit.spec.tsx b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/premium-advanced-permissions.unit.spec.tsxindex e7ba214cae5..05d1c168f57 100644--- a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/premium-advanced-permissions.unit.spec.tsx+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/premium-advanced-permissions.unit.spec.tsx@@ -26,8 +26,8 @@ describe("Add data modal (EE with token)", () => {canUpload: true,});- expect(screen.getByText("Manage uploads")).toBeInTheDocument();- expect(screen.getByText("Enable uploads")).toBeInTheDocument();+ expect(await screen.findByText("Manage uploads")).toBeInTheDocument();+ expect(await screen.findByText("Enable uploads")).toBeInTheDocument();});it("should offer a setting manager to manage (csv) uploads", async () => {@@ -38,7 +38,7 @@ describe("Add data modal (EE with token)", () => {canUpload: true,});- expect(screen.getByText("Manage uploads")).toBeInTheDocument();+ expect(await screen.findByText("Manage uploads")).toBeInTheDocument();});/**@@ -57,7 +57,7 @@ describe("Add data modal (EE with token)", () => {canUpload: false,});- expect(screen.getByText("Manage uploads")).toBeInTheDocument();+ expect(await screen.findByText("Manage uploads")).toBeInTheDocument();});});});diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/premium-hosting-upload.unit.spec.tsx b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/premium-hosting-upload.unit.spec.tsxindex bfc70bd3e20..58720205610 100644--- a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/premium-hosting-upload.unit.spec.tsx+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/tests/premium-hosting-upload.unit.spec.tsx@@ -108,6 +108,9 @@ describe("Add data modal (Pro: hosted instance with the attached DWH)", () => {enableGoogleSheets: true,status: "error",});+ expect(+ await screen.findByRole("tab", { name: /CSV$/ }),+ ).toBeInTheDocument();await assertSheetsOpened();const connectButton = await screen.findByRole("button", {diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/use-add-data-permission.ts b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/use-add-data-permission.tsnew file mode 100644index 00000000000..f3ac720ca39--- /dev/null+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/use-add-data-permission.ts@@ -0,0 +1,31 @@+import { useListDatabasesQuery } from "metabase/api";+import { useSetting } from "metabase/common/hooks";+import { useSelector } from "metabase/lib/redux";+import { canAccessSettings, getUserIsAdmin } from "metabase/selectors/user";++export function useAddDataPermissions() {+ const { data: databasesResponse } = useListDatabasesQuery();+ const userCanAccessSettings = useSelector(canAccessSettings);+ const isAdmin = useSelector(getUserIsAdmin);+ const databases = databasesResponse?.data;+ const uploadDbId = useSetting("uploads-settings")?.db_id;+ const uploadDB = databases?.find((db) => db.id === uploadDbId);++ /**+ * This covers the case where instance has the attached dwh. In such cases+ * uploads are enabled by default.+ */+ const areUploadsEnabled = !!uploadDbId;+ const canUploadToDatabase = !!uploadDB?.can_upload;+ const canManageUploads = userCanAccessSettings;+ const canPerformMeaningfulActions =+ canUploadToDatabase || canManageUploads || isAdmin;++ return {+ areUploadsEnabled,+ canUploadToDatabase,+ canManageUploads,+ canPerformMeaningfulActions,+ isAdmin,+ };+}diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/use-add-data-permission.unit.spec.ts b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/use-add-data-permission.unit.spec.tsnew file mode 100644index 00000000000..3180869a53c--- /dev/null+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/use-add-data-permission.unit.spec.ts@@ -0,0 +1,96 @@+import { setupEnterprisePlugins } from "__support__/enterprise";+import {+ setupDatabaseListEndpoint,+ setupTokenStatusEndpoint,+} from "__support__/server-mocks";+import { mockSettings } from "__support__/settings";+import { createMockEntitiesState } from "__support__/store";+import { renderHookWithProviders, waitFor } from "__support__/ui";+import type { UserWithApplicationPermissions } from "metabase/plugins";+import {+ createMockDatabase,+ createMockTokenFeatures,+ createMockUser,+} from "metabase-types/api/mocks";+import { createMockState } from "metabase-types/store/mocks";++import { useAddDataPermissions } from "./use-add-data-permission";++function setup({+ isSuperUser = false,+ canUpload = false,+ canAccessSettings = false,+}) {+ const database = createMockDatabase({+ uploads_enabled: canUpload,+ can_upload: canUpload,+ });+ const currentUser = createMockUser({+ is_superuser: isSuperUser,+ });++ if (canAccessSettings) {+ (currentUser as UserWithApplicationPermissions).permissions = {+ can_access_setting: true,+ can_access_monitoring: false,+ can_access_subscription: false,+ };+ }+ setupTokenStatusEndpoint({ valid: true });++ if (canAccessSettings) {+ // enterprise plugin is needed for advanced permissions to be returned+ setupEnterprisePlugins();+ }++ const storeInitialState = createMockState({+ currentUser,+ entities: createMockEntitiesState({+ databases: [database],+ collections: [],+ }),+ settings: mockSettings({+ "token-features": createMockTokenFeatures({+ advanced_permissions: true,+ }),+ "uploads-settings": {+ db_id: database.id,+ schema_name: "uploads",+ table_prefix: "uploaded_",+ },+ }),+ });++ setupDatabaseListEndpoint([database]);++ return renderHookWithProviders(() => useAddDataPermissions(), {+ storeInitialState,+ });+}++describe("correctly sets canPerformMeaningfulActions", () => {+ const testCases = [+ {+ description: "admin user can perform meaningful actions",+ data: { isSuperUser: true },+ },+ {+ description: "user who can upload can perform meaningful actions",+ data: { canUpload: true },+ },+ {+ description: "user with settings access can perform meaningful actions",+ data: { canAccessSettings: true },+ },+ ];++ it.each(testCases)("$description", async (testCase) => {+ const { result } = setup(testCase.data);++ await waitFor(() => {+ expect(result.current).toEqual(+ expect.objectContaining({ canPerformMeaningfulActions: true }),+ );+ });+ });+});diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/utils.ts b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/utils.tsindex 310b0eae6ed..c779d08a186 100644--- a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/utils.ts+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/utils.ts@@ -1,3 +1,5 @@+import type { Database } from "metabase-types/api";+const validTabArray = ["db", "csv", "gsheets"] as const;const validTabs = new Set<string>(validTabArray);@@ -5,3 +7,28 @@ type AddDataTab = (typeof validTabArray)[number];export const isValidTab = (v: string | null): v is AddDataTab =>typeof v === "string" && validTabs.has(v);++export function hasMeaningfulUploadableDatabases({+ allDatabases = [],+ allUploadableDatabases = [],+}: {+ allDatabases: Database[] | undefined;+ allUploadableDatabases: Database[] | undefined;+}) {+ // if there are no uploadable databases or no databases, return false+ if (!allUploadableDatabases?.length || !allDatabases) {+ return false;+ }++ // if we have more than one database and the only uploadable database is h2, return false+ if (+ allUploadableDatabases.length === 1 &&+ allDatabases.length > 1 &&+ allUploadableDatabases[0]?.engine === "h2"+ ) {+ return false;+ }++ // we have some meaningful uploadable databases, return true+ return true;+}diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/utils.unit.spec.ts b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/utils.unit.spec.tsnew file mode 100644index 00000000000..4582e155f6f--- /dev/null+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/AddDataModal/utils.unit.spec.ts@@ -0,0 +1,65 @@+import { createMockDatabase } from "metabase-types/api/mocks";++import { hasMeaningfulUploadableDatabases } from "./utils";++const h2 = createMockDatabase({+ id: 1,+ name: "Db Uno",+ engine: "h2",+});++const postgres = createMockDatabase({+ id: 2,+ name: "Db Dos",+ engine: "postgres",+});++const athena = createMockDatabase({+ id: 3,+ name: "Db Tres",+ engine: "athena",+});++const testCases = [+ {+ description: "there are no uploadable databases",+ result: false,+ data: {+ allDatabases: [],+ allUploadableDatabases: [],+ },+ },+ {+ description: "there is only one uploadable database and it is h2",+ result: true,+ data: {+ allDatabases: [h2],+ allUploadableDatabases: [h2],+ },+ },+ {+ description: "multiple databases but h2 is only uploadable",+ result: false,+ data: {+ allDatabases: [h2, athena],+ allUploadableDatabases: [h2],+ },+ },+ {+ description: "with non-h2 uploadable databases",+ result: true,+ data: {+ allDatabases: [postgres, athena],+ allUploadableDatabases: [postgres],+ },+ },+];++describe("hasMeaningfulUploadableDatabases", () => {+ it.each(testCases)(+ "should return $result when $description",+ ({ result, data }) => {+ expect(hasMeaningfulUploadableDatabases(data)).toBe(result);+ },+ );+});diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/BrowseNavSection.tsx b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/BrowseNavSection.tsxindex 4da87a3ecba..7d3f831c1ae 100644--- a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/BrowseNavSection.tsx+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/BrowseNavSection.tsx@@ -19,6 +19,8 @@ import { PaddedSidebarLink, SidebarHeading } from "../MainNavbar.styled";import { trackAddDataModalOpened } from "../analytics";import type { SelectedItem } from "../types";+import { useAddDataPermissions } from "./AddDataModal/use-add-data-permission";+export const BrowseNavSection = ({nonEntityItem,onItemSelect,@@ -38,8 +40,8 @@ export const BrowseNavSection = ({"expand-browse-in-nav",);+ const { canPerformMeaningfulActions } = useAddDataPermissions();const [opened, { toggle }] = useDisclosure(expandBrowse);-const entityTypes = useSelector(getEntityTypes);const isEmbeddingIframe = useSelector(getIsEmbeddingIframe);@@ -48,6 +50,8 @@ export const BrowseNavSection = ({setExpandBrowse(!opened);};+ const showAddDataButton = canPerformMeaningfulActions && !isEmbeddingIframe;+return (<div aria-selected={opened} role="tab"><Flex align="center" justify="space-between" mb="sm">@@ -64,7 +68,7 @@ export const BrowseNavSection = ({</SidebarHeading><Icon name={opened ? "chevrondown" : "chevronright"} size={8} /></Group>- {!isEmbeddingIframe && (+ {showAddDataButton && (<Buttonaria-label="Add data"variant="subtle"diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/BrowseNavSection.unit.spec.tsx b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/BrowseNavSection.unit.spec.tsxindex 8baa656fb82..e49ba7a4d67 100644--- a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/BrowseNavSection.unit.spec.tsx+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/BrowseNavSection.unit.spec.tsx@@ -1,7 +1,9 @@import userEvent from "@testing-library/user-event";+import { setupDatabaseListEndpoint } from "__support__/server-mocks";import { getIcon, renderWithProviders, screen, within } from "__support__/ui";import * as domUtils from "metabase/lib/dom";+import { createMockDatabase, createMockUser } from "metabase-types/api/mocks";import type { EmbeddingEntityType } from "metabase-types/store/embedding-data-picker";import { createMockState } from "metabase-types/store/mocks";@@ -12,8 +14,8 @@ describe("BrowseNavSection", () => {jest.restoreAllMocks();});- it("should render a section title and an 'Add data' button", () => {- setup();+ it("should render a section title and an 'Add data' button", async () => {+ setup({ isAdmin: true });const tab = screen.getByRole("tab");@@ -22,12 +24,28 @@ describe("BrowseNavSection", () => {within(tab).getByRole("heading", { name: "Data" }),).toBeInTheDocument();expect(- within(tab).getByRole("button", { name: "Add data" }),+ await within(tab).findByRole("button", { name: "Add data" }),).toBeInTheDocument();// The user-visible text of the button says only "Add"expect(within(tab).getByText("Add")).toBeInTheDocument();});+ it("should not render a section title and an 'Add data' button for users without enough permissions", async () => {+ setup({ isAdmin: false });++ const tab = screen.getByRole("tab");++ expect(tab).toBeInTheDocument();+ expect(+ within(tab).getByRole("heading", { name: "Data" }),+ ).toBeInTheDocument();+ expect(+ within(tab).queryByRole("button", { name: "Add data" }),+ ).not.toBeInTheDocument();+ // The user-visible text of the button says only "Add"+ expect(within(tab).queryByText("Add")).not.toBeInTheDocument();+ });+it("should not render the 'Add data' button for full app embedding", () => {setup({ isEmbeddingIframe: true });@@ -153,25 +171,39 @@ describe("BrowseNavSection", () => {interface SetupOpts {isEmbeddingIframe?: boolean;entityTypes?: EmbeddingEntityType[];+ isAdmin?: boolean;}-function setup({ isEmbeddingIframe, entityTypes }: SetupOpts = {}) {+function setup({+ isEmbeddingIframe,+ entityTypes,+ isAdmin = true,+}: SetupOpts = {}) {const onAddDataModalOpen = jest.fn();+ const database = createMockDatabase({+ uploads_enabled: true,+ can_upload: true,+ });+ setupDatabaseListEndpoint([database]);+if (isEmbeddingIframe) {jest.spyOn(domUtils, "isWithinIframe").mockReturnValue(true);}- const maybeInitialState = entityTypes+ const embeddingDataPickerState = entityTypes? {- storeInitialState: createMockState({- embeddingDataPicker: {- entityTypes,- },- }),+ embeddingDataPicker: {+ entityTypes,+ },}: undefined;+ const storeInitialState = createMockState({+ ...embeddingDataPickerState,+ currentUser: createMockUser({ is_superuser: isAdmin }),+ });+renderWithProviders(<BrowseNavSectionhasDataAccess@@ -186,7 +218,7 @@ function setup({ isEmbeddingIframe, entityTypes }: SetupOpts = {}) {onItemSelect={jest.fn()}onAddDataModalOpen={onAddDataModalOpen}/>,- maybeInitialState,+ { storeInitialState },);return { onAddDataModalOpen };diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/GettingStartedSection.tsx b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/GettingStartedSection.tsxindex 5f722e160bc..c55ad408845 100644--- a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/GettingStartedSection.tsx+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/GettingStartedSection.tsx@@ -9,6 +9,8 @@ import { PaddedSidebarLink, SidebarHeading } from "../MainNavbar.styled";import { trackOnboardingChecklistOpened } from "../analytics";import type { SelectedItem } from "../types";+import { useAddDataPermissions } from "./AddDataModal/use-add-data-permission";+export const GettingStartedSection = ({nonEntityItem,onAddDataModalOpen,@@ -17,6 +19,7 @@ export const GettingStartedSection = ({nonEntityItem: SelectedItem;onAddDataModalOpen: () => void;}>) => {+ const { canPerformMeaningfulActions } = useAddDataPermissions();const [opened, { toggle }] = useDisclosure(true);const ONBOARDING_URL = "/getting-started";@@ -43,9 +46,11 @@ export const GettingStartedSection = ({role="tabpanel"aria-expanded={opened}>- <PaddedSidebarLink icon="add_data" onClick={onAddDataModalOpen}>- {t`Add data`}- </PaddedSidebarLink>+ {canPerformMeaningfulActions && (+ <PaddedSidebarLink icon="add_data" onClick={onAddDataModalOpen}>+ {t`Add data`}+ </PaddedSidebarLink>+ )}<PaddedSidebarLinkicon="learn"diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/GettingStartedSection.unit.spec.tsx b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/GettingStartedSection.unit.spec.tsxindex b849a8a8063..71d314c6e23 100644--- a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/GettingStartedSection.unit.spec.tsx+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/GettingStartedSection.unit.spec.tsx@@ -1,14 +1,17 @@import userEvent from "@testing-library/user-event";import { renderWithProviders, screen } from "__support__/ui";+import { createMockUser } from "metabase-types/api/mocks";import { createMockState } from "metabase-types/store/mocks";import { GettingStartedSection } from "./GettingStartedSection";const setup = ({hasChildren = true,+ isAdmin = true,}: {hasChildren?: boolean;+ isAdmin?: boolean;} = {}) => {const onAddDataModalOpen = jest.fn();@@ -19,7 +22,11 @@ const setup = ({>{hasChildren && "Child"}</GettingStartedSection>,- { storeInitialState: createMockState() },+ {+ storeInitialState: createMockState({+ currentUser: createMockUser({ is_superuser: isAdmin }),+ }),+ },);return { onAddDataModalOpen };@@ -53,6 +60,11 @@ describe("GettingStartedSection", () => {expect(screen.getByLabelText("Add data")).toBeInTheDocument();});+ it("should not render the 'Add data' button if the user is not an admin", () => {+ setup({ isAdmin: false });+ expect(screen.queryByLabelText("Add data")).not.toBeInTheDocument();+ });+it("should trigger the modal on 'Add data' click", async () => {const { onAddDataModalOpen } = setup();await userEvent.click(screen.getByText("Add data"));