Skip to content

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

Add expandable contents to data apps in the sidebar (#84207)

Phoomparin Mano886f2ce5b6ce
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/api.clj b/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
index d36921458a4..a719d850ca6 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
@@ -124,7 +124,8 @@
(def ^:private PublicDataAppResponse
[:map {:closed true}
[:name ms/NonBlankString]
- [:display_name ms/NonBlankString]])
+ [:display_name ms/NonBlankString]
+ [:resource_collection_id ms/PositiveInt]])
(def ^:private RepoStatusResponse
[:map
@@ -193,7 +194,7 @@
[app]
(if api/*is-superuser?*
(assoc app :outdated (data-app.config/outdated? app))
- (select-keys app [:name :display_name])))
+ (select-keys app [:name :display_name :resource_collection_id])))
(defn- read-check-data-app
"Check whether the current user can access a data app. Viewing requires read access to the app's
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj
index e5d51510261..bc0ba0123aa 100644
--- a/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj
@@ -178,7 +178,8 @@
(with-redefs [data-app.config/supported-app-version 2]
(testing "a regular user is never told about the outdated app in a list"
(doseq [url ["apps" "apps?available=true"]]
- (is (= [{:name "current" :display_name "Current"}]
+ (is (= [{:name "current" :display_name "Current"
+ :resource_collection_id (t2/select-one-fn :resource_collection_id :model/DataApp :name "current")}]
(mt/user-http-request :rasta :get 200 url)))))
(testing "for a regular user, opening an outdated app is a 409 that says what to do"
(doseq [url ["apps/old" "apps/old/bundle"]]
@@ -186,7 +187,8 @@
:message #"This app was built for version 1 of data apps.*"}
(mt/user-http-request :rasta :get 409 url)))))
(testing "the current app still opens"
- (is (= {:name "current" :display_name "Current"}
+ (is (= {:name "current" :display_name "Current"
+ :resource_collection_id (t2/select-one-fn :resource_collection_id :model/DataApp :name "current")}
(mt/user-http-request :rasta :get 200 "apps/current")))
(is (str/includes?
(str (mt/user-real-request :crowberto :get 200 "apps/current/bundle"))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.clj
index 55b9a9f9e09..5c3abaab24c 100644
--- a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.clj
@@ -44,9 +44,12 @@
(let [list-apps (api.macros/find-route-fn 'metabase-enterprise.data-apps.api :get "/")]
(mt/with-current-user (mt/user->id :rasta)
(list-apps {} {})
- (doseq [[query expected] [[{} [{:name "birds" :display_name "Birds"}
- {:name "disabled" :display_name "Disabled"}]]
- [{:available true} [{:name "birds" :display_name "Birds"}]]]]
+ (doseq [[query expected] [[{} [{:name "birds" :display_name "Birds"
+ :resource_collection_id (:resource_collection_id app)}
+ {:name "disabled" :display_name "Disabled"
+ :resource_collection_id (:resource_collection_id disabled)}]]
+ [{:available true} [{:name "birds" :display_name "Birds"
+ :resource_collection_id (:resource_collection_id app)}]]]]
(t2/with-call-count [call-count]
(is (= expected (list-apps {} query)))
(is (= 1 (call-count)))))))))
@@ -284,7 +287,7 @@
(perms/revoke-collection-permissions! group-id collection-id)
(mt/user-http-request :rasta :get 403 "apps/birds")
(resources/ensure-resources! app)
- (is (= {:name "birds" :display_name "Birds"}
+ (is (= {:name "birds" :display_name "Birds" :resource_collection_id collection-id}
(mt/user-http-request :rasta :get 200 "apps/birds"))))))))
(deftest tenant-users-cannot-inherit-app-access-test
diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppNavbarLink.tsx b/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppNavbarLink.tsx
new file mode 100644
index 00000000000..d3ca1b23266
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppNavbarLink.tsx
@@ -0,0 +1,69 @@
+import { useId, useState } from "react";
+import { t } from "ttag";
+
+import { TreeNode } from "metabase/common/components/tree/TreeNode";
+import { PaddedSidebarLink } from "metabase/nav/containers/MainNavbar/MainNavbar.styled";
+import { SidebarLink } from "metabase/nav/containers/MainNavbar/SidebarItems";
+import { ExpandToggleButton } from "metabase/nav/containers/MainNavbar/SidebarItems/SidebarItems.styled";
+import { Box } from "metabase/ui";
+import * as Urls from "metabase/urls";
+import type { DataApp } from "metabase-types/api";
+
+interface Props {
+ dataApp: DataApp;
+ onItemSelect: () => void;
+}
+
+export function DataAppNavbarLink({ dataApp, onItemSelect }: Props) {
+ const contentsId = useId();
+ const [isContentsExpanded, setIsContentsExpanded] = useState(false);
+
+ const { display_name: displayName, resource_collection_id: collectionId } =
+ dataApp;
+
+ const toggleButton = (
+ <ExpandToggleButton
+ aria-label={
+ isContentsExpanded
+ ? t`Collapse ${displayName}`
+ : t`Expand ${displayName}`
+ }
+ aria-expanded={isContentsExpanded}
+ aria-controls={contentsId}
+ onClick={() => setIsContentsExpanded(!isContentsExpanded)}
+ >
+ <TreeNode.ExpandToggleIcon
+ name="chevronright"
+ isExpanded={isContentsExpanded}
+ size={12}
+ />
+ </ExpandToggleButton>
+ );
+
+ return (
+ <>
+ <SidebarLink
+ icon="app"
+ onClick={onItemSelect}
+ rel="noopener noreferrer"
+ target="_blank"
+ url={Urls.dataApp(dataApp.name)}
+ left={toggleButton}
+ >
+ {displayName}
+ </SidebarLink>
+
+ {isContentsExpanded && (
+ <Box id={contentsId} pl="1.5rem">
+ <PaddedSidebarLink
+ icon="folder"
+ url={`/collection/${collectionId}`}
+ onClick={onItemSelect}
+ >
+ {t`Contents`}
+ </PaddedSidebarLink>
+ </Box>
+ )}
+ </>
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppsNavbarSection.tsx b/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppsNavbarSection.tsx
index 11f842e4047..cb34eabab6c 100644
--- a/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppsNavbarSection.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppsNavbarSection.tsx
@@ -2,13 +2,13 @@ import { t } from "ttag";
import { CollapseSection } from "metabase/common/components/CollapseSection";
import {
- PaddedSidebarLink,
SidebarHeading,
SidebarSection,
} from "metabase/nav/containers/MainNavbar/MainNavbar.styled";
-import * as Urls from "metabase/urls";
import { useListDataAppsQuery } from "metabase-enterprise/api";
+import { DataAppNavbarLink } from "./DataAppNavbarLink";
+
export function DataAppsNavbarSection({
onItemSelect,
}: {
@@ -30,17 +30,11 @@ export function DataAppsNavbarSection({
aria-label={t`Data apps`}
>
{dataApps.map((dataApp) => (
- <PaddedSidebarLink
- // Data-app names are globally unique slugs, enforced by the database.
+ <DataAppNavbarLink
key={dataApp.name}
- icon="app"
- onClick={onItemSelect}
- rel="noopener noreferrer"
- target="_blank"
- url={Urls.dataApp(dataApp.name)}
- >
- {dataApp.display_name}
- </PaddedSidebarLink>
+ dataApp={dataApp}
+ onItemSelect={onItemSelect}
+ />
))}
</CollapseSection>
</SidebarSection>
diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppsNavbarSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppsNavbarSection.unit.spec.tsx
index c5ea7b8d8ba..e82b48b4c70 100644
--- a/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppsNavbarSection.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppsNavbarSection.unit.spec.tsx
@@ -1,3 +1,4 @@
+import userEvent from "@testing-library/user-event";
import fetchMock from "fetch-mock";
import { renderWithProviders, screen, waitFor, within } from "__support__/ui";
@@ -20,8 +21,8 @@ describe("DataAppsNavbarSection", () => {
it("shows every data app as a link", async () => {
fetchMock.get("path:/api/apps?available=true", [
- { name: "gizmo", display_name: "Gizmo" },
- { name: "gadget", display_name: "Gadget" },
+ { name: "gizmo", display_name: "Gizmo", resource_collection_id: 42 },
+ { name: "gadget", display_name: "Gadget", resource_collection_id: 43 },
]);
renderWithProviders(
@@ -49,4 +50,97 @@ describe("DataAppsNavbarSection", () => {
"/apps/gadget",
);
});
+
+ it("expands contents when the chevron is clicked", async () => {
+ fetchMock.get("path:/api/apps?available=true", [
+ { name: "gizmo", display_name: "Gizmo", resource_collection_id: 42 },
+ { name: "gadget", display_name: "Gadget", resource_collection_id: 43 },
+ ]);
+
+ const onItemSelect = jest.fn();
+
+ renderWithProviders(
+ <>
+ <Route
+ path="/"
+ element={<DataAppsNavbarSection onItemSelect={onItemSelect} />}
+ />
+
+ <Route
+ path="/collection/42"
+ element={<div>Gizmo collection page</div>}
+ />
+ </>,
+ { withRouter: true },
+ );
+
+ const gizmoToggle = await screen.findByRole("button", {
+ name: "Expand Gizmo",
+ });
+
+ expect(
+ screen.queryByRole("link", { name: /Contents/ }),
+ ).not.toBeInTheDocument();
+
+ await userEvent.click(gizmoToggle);
+
+ // gizmo app should be expanded
+ expect(gizmoToggle).toHaveAttribute("aria-expanded", "true");
+ expect(onItemSelect).not.toHaveBeenCalled();
+
+ // gadget app should not be expanded
+ expect(
+ screen.getByRole("button", { name: "Expand Gadget" }),
+ ).toHaveAttribute("aria-expanded", "false");
+
+ const contentsLink = screen.getByRole("link", { name: /Contents/ });
+ expect(contentsLink).toHaveAttribute("href", "/collection/42");
+ expect(contentsLink).not.toHaveAttribute("target", "_blank");
+
+ await userEvent.click(screen.getByRole("link", { name: /Gizmo/ }));
+ expect(onItemSelect).toHaveBeenCalledTimes(1);
+ expect(contentsLink).toBeInTheDocument();
+
+ await userEvent.click(
+ screen.getByRole("button", { name: "Collapse Gizmo" }),
+ );
+
+ expect(
+ screen.queryByRole("link", { name: /Contents/ }),
+ ).not.toBeInTheDocument();
+
+ await userEvent.click(screen.getByRole("button", { name: "Expand Gizmo" }));
+ await userEvent.click(screen.getByRole("link", { name: /Contents/ }));
+ expect(onItemSelect).toHaveBeenCalledTimes(2);
+
+ expect(
+ await screen.findByText("Gizmo collection page"),
+ ).toBeInTheDocument();
+ });
+
+ it("expands contents when the chevron is activated with the keyboard", async () => {
+ fetchMock.get("path:/api/apps?available=true", [
+ { name: "gizmo", display_name: "Gizmo", resource_collection_id: 42 },
+ ]);
+
+ renderWithProviders(
+ <Route
+ path="/"
+ element={<DataAppsNavbarSection onItemSelect={jest.fn()} />}
+ />,
+ { withRouter: true },
+ );
+
+ const toggle = await screen.findByRole("button", { name: "Expand Gizmo" });
+ toggle.focus();
+
+ await userEvent.keyboard("{Enter}");
+ expect(screen.getByRole("link", { name: /Contents/ })).toBeInTheDocument();
+
+ await userEvent.keyboard(" ");
+
+ expect(
+ screen.queryByRole("link", { name: /Contents/ }),
+ ).not.toBeInTheDocument();
+ });
});