Skip to content

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

Fix selectors/app module-boundary violations (#75777)

Fraser5fabe396abfd
diff --git a/frontend/lint/module-boundaries.mjs b/frontend/lint/module-boundaries.mjs
index 1d5f33c4cc3..a23d4d8a393 100644
--- a/frontend/lint/module-boundaries.mjs
+++ b/frontend/lint/module-boundaries.mjs
@@ -153,6 +153,7 @@ const elements = [
"frontend/src/metabase/AppComponent.tsx",
"frontend/src/metabase/App.styled.tsx",
"frontend/src/metabase/AppKBarProvider.tsx",
+ "frontend/src/metabase/app/selectors.ts",
"frontend/src/metabase/reducers-main.ts",
"frontend/src/metabase/routes.tsx",
"frontend/src/metabase/routes-embed.tsx",
diff --git a/frontend/src/metabase/AppComponent.tsx b/frontend/src/metabase/AppComponent.tsx
index 8d00b7ab5e8..908bb2593a7 100644
--- a/frontend/src/metabase/AppComponent.tsx
+++ b/frontend/src/metabase/AppComponent.tsx
@@ -4,6 +4,12 @@ import { useEffect, useState } from "react";
import { AppBarContainer } from "metabase/app/nav/AppBar";
import { Navbar } from "metabase/app/nav/Navbar";
+import {
+ getIsAdminApp,
+ getIsAppBarVisible,
+ getIsDataStudioApp,
+ getIsNavBarEnabled,
+} from "metabase/app/selectors";
import { AppBanner } from "metabase/common/components/AppBanner";
import {
Archived,
@@ -20,13 +26,7 @@ import { usePageTitle } from "metabase/hooks/use-page-title";
import { connect, useSelector } from "metabase/redux";
import { setErrorPage } from "metabase/redux/app";
import type { AppErrorDescriptor, State } from "metabase/redux/store";
-import {
- getErrorPage,
- getIsAdminApp,
- getIsAppBarVisible,
- getIsDataStudioApp,
- getIsNavBarEnabled,
-} from "metabase/selectors/app";
+import { getErrorPage } from "metabase/selectors/app";
import { getApplicationName } from "metabase/selectors/whitelabel";
import { StatusListing } from "metabase/status/components/StatusListing";
import { initializeIframeResizer } from "metabase/utils/dom";
diff --git a/frontend/src/metabase/app/nav/AppBar.tsx b/frontend/src/metabase/app/nav/AppBar.tsx
index 220c5b93d16..699ac416118 100644
--- a/frontend/src/metabase/app/nav/AppBar.tsx
+++ b/frontend/src/metabase/app/nav/AppBar.tsx
@@ -2,6 +2,17 @@ import { withRouter } from "react-router";
import { push } from "react-router-redux";
import { skipToken, useGetDashboardQuery } from "metabase/api";
+import {
+ getCollectionId,
+ getIsAppSwitcherVisible,
+ getIsCollectionPathVisible,
+ getIsLogoVisible,
+ getIsMetricsViewer,
+ getIsNavBarEnabled,
+ getIsNewButtonVisible,
+ getIsQuestionLineageVisible,
+ getIsSearchVisible,
+} from "metabase/app/selectors";
import { useInitialCollectionId } from "metabase/collections/hooks";
import {
getCommentSidebarOpen,
@@ -22,18 +33,7 @@ import { connect, useDispatch, useSelector } from "metabase/redux";
import { closeNavbar, toggleNavbar } from "metabase/redux/app";
import type { State } from "metabase/redux/store";
import type { RouterProps } from "metabase/selectors/app";
-import {
- getDetailViewState,
- getIsAppSwitcherVisible,
- getIsCollectionPathVisible,
- getIsLogoVisible,
- getIsMetricsViewer,
- getIsNavBarEnabled,
- getIsNavbarOpen,
- getIsNewButtonVisible,
- getIsQuestionLineageVisible,
- getIsSearchVisible,
-} from "metabase/selectors/app";
+import { getDetailViewState, getIsNavbarOpen } from "metabase/selectors/app";
import { getIsEmbeddingIframe } from "metabase/selectors/embed";
import { getUser } from "metabase/selectors/user";
import { modelToUrl } from "metabase/urls";
@@ -86,6 +86,11 @@ function AppBarContainerInner(props: AppBarProps & RouterProps) {
const dispatch = useDispatch();
const question = useSelector(getQuestion);
const originalQuestion = useSelector(getOriginalQuestion);
+ // The breadcrumbs' current collection is derived from the active
+ // dashboard/question/document state. CollectionBreadcrumbs used to read this
+ // itself, but getCollectionId orchestrates feature state and now lives in the
+ // app tier, so the app-tier AppBar resolves it and passes it down.
+ const breadcrumbCollectionId = useSelector(getCollectionId);
const { pathname } = props.location;
const isOnQuestionPage = pathname && isQuestionPath(pathname);
@@ -112,6 +117,7 @@ function AppBarContainerInner(props: AppBarProps & RouterProps) {
collectionBreadcrumbs={
<CollectionBreadcrumbs
dashboard={dashboardId != null ? dashboard : undefined}
+ collectionId={breadcrumbCollectionId ?? undefined}
/>
}
questionLineage={
diff --git a/frontend/src/metabase/app/selectors.ts b/frontend/src/metabase/app/selectors.ts
new file mode 100644
index 00000000000..478ac41dc86
--- /dev/null
+++ b/frontend/src/metabase/app/selectors.ts
@@ -0,0 +1,248 @@
+import { createSelector } from "@reduxjs/toolkit";
+
+import {
+ getDashboard,
+ getDashboardId,
+ getIsEditing as getIsEditingDashboard,
+} from "metabase/dashboard/selectors";
+import { getCurrentDocument } from "metabase/documents/selectors";
+import {
+ getIsSavedQuestionChanged,
+ getQuestion,
+} from "metabase/query_builder/selectors";
+import type { State } from "metabase/redux/store";
+import { type RouterProps, getDetailViewState } from "metabase/selectors/app";
+import {
+ getEmbedOptions,
+ getIsEmbeddingIframe,
+} from "metabase/selectors/embed";
+import { getUser } from "metabase/selectors/user";
+import * as Urls from "metabase/urls";
+
+export const getRouterPath = (state: State, props: RouterProps) => {
+ return props?.location?.pathname ?? window.location.pathname;
+};
+
+export const getRouterHash = (state: State, props: RouterProps) => {
+ return props?.location?.hash ?? window.location.hash;
+};
+
+export const getIsAdminApp = createSelector([getRouterPath], (path) => {
+ return path.startsWith("/admin/");
+});
+
+export const getIsDataStudioApp = createSelector([getRouterPath], (path) => {
+ return path.startsWith("/data-studio");
+});
+
+export const getIsMetricsViewer = createSelector([getRouterPath], (path) => {
+ return path.startsWith("/explore");
+});
+
+export const getIsLogoVisible = createSelector(
+ [getIsEmbeddingIframe, getEmbedOptions],
+ (isEmbeddingIframe, embedOptions) => {
+ return !isEmbeddingIframe || embedOptions.logo;
+ },
+);
+
+export const getIsSearchVisible = createSelector(
+ [getIsEmbeddingIframe, getEmbedOptions],
+ (isEmbeddingIframe, embedOptions) => {
+ return !isEmbeddingIframe || embedOptions.search;
+ },
+);
+
+export const getIsNewButtonVisible = createSelector(
+ [getIsEmbeddingIframe, getEmbedOptions],
+ (isEmbeddingIframe, embedOptions) => {
+ return !isEmbeddingIframe || embedOptions.new_button;
+ },
+);
+
+export const getIsAppSwitcherVisible = createSelector(
+ [getIsEmbeddingIframe],
+ (isEmbeddingIframe) => !isEmbeddingIframe,
+);
+
+const PATHS_WITHOUT_NAVBAR = [
+ /^\/setup/,
+ /^\/auth/,
+ /^\/data-studio/,
+ /\/model\/.*\/query/,
+ /\/model\/.*\/columns/,
+ /\/model\/.*\/metadata/,
+ /\/model\/query/,
+ /\/model\/columns/,
+ /\/model\/metadata/,
+ /\/transform\/new\/.*\/query/,
+];
+
+const PATHS_WITH_COLLECTION_BREADCRUMBS = [
+ /\/question\//,
+ /\/model\//,
+ /\/metric\//,
+ /\/dashboard\//,
+ /\/document\//,
+];
+
+// Paths where collection identity comes from the URL itself, so breadcrumbs
+// can render without needing a question/dashboard/document in redux state.
+const STANDALONE_COLLECTION_BREADCRUMB_PATHS = [/\/collection\//];
+const PATHS_WITH_QUESTION_LINEAGE = [/\/question/, /\/model/];
+
+export const getIsCollectionPathVisible = createSelector(
+ [
+ getQuestion,
+ getDashboard,
+ getCurrentDocument,
+ getRouterPath,
+ getIsEmbeddingIframe,
+ getEmbedOptions,
+ ],
+ (question, dashboard, document, path, isEmbedded, embedOptions) => {
+ if (isEmbedded && !embedOptions.breadcrumbs) {
+ return false;
+ }
+
+ const isModelDetail = /\/model\/.*\/detail\/.*/.test(path);
+ if (isModelDetail) {
+ return true;
+ }
+
+ if (
+ STANDALONE_COLLECTION_BREADCRUMB_PATHS.some((pattern) =>
+ pattern.test(path),
+ )
+ ) {
+ return true;
+ }
+
+ return (
+ ((question != null && question.isSaved()) ||
+ dashboard != null ||
+ document !== null) &&
+ PATHS_WITH_COLLECTION_BREADCRUMBS.some((pattern) => pattern.test(path))
+ );
+ },
+);
+
+export const getIsQuestionLineageVisible = createSelector(
+ [getIsSavedQuestionChanged, getRouterPath],
+ (isSavedQuestionChanged, path) =>
+ isSavedQuestionChanged &&
+ PATHS_WITH_QUESTION_LINEAGE.some((pattern) => pattern.test(path)),
+);
+
+export const getIsNavBarEnabled = createSelector(
+ [
+ getUser,
+ getRouterPath,
+ getIsEditingDashboard,
+ getIsEmbeddingIframe,
+ getEmbedOptions,
+ ],
+ (currentUser, path, isEditingDashboard, isEmbedded, embedOptions) => {
+ if (!currentUser || isEditingDashboard) {
+ return false;
+ }
+ if (isEmbedded && !embedOptions.side_nav) {
+ return false;
+ }
+
+ return !PATHS_WITHOUT_NAVBAR.some((pattern) => pattern.test(path));
+ },
+);
+
+const getIsEmbeddedAppBarVisible = createSelector(
+ [
+ getEmbedOptions,
+ getIsQuestionLineageVisible,
+ getIsCollectionPathVisible,
+ getIsNavBarEnabled,
+ ],
+ (
+ embedOptions,
+ isQuestionLineageVisible,
+ isCollectionPathVisible,
+ isNavBarEnabled,
+ ) => {
+ const anyEmbeddedAppBarElementVisible =
+ isNavBarEnabled ||
+ embedOptions.search ||
+ embedOptions.new_button ||
+ embedOptions.logo ||
+ isQuestionLineageVisible ||
+ isCollectionPathVisible;
+ return embedOptions.top_nav && anyEmbeddedAppBarElementVisible;
+ },
+);
+
+export const getIsAppBarVisible = createSelector(
+ [
+ getUser,
+ getRouterPath,
+ getRouterHash,
+ getIsAdminApp,
+ getIsDataStudioApp,
+ getIsEditingDashboard,
+ getIsEmbeddingIframe,
+ getIsEmbeddedAppBarVisible,
+ ],
+ (
+ currentUser,
+ path,
+ hash,
+ isAdminApp,
+ isDataStudioApp,
+ isEditingDashboard,
+ isEmbedded,
+ isEmbeddedAppBarVisible,
+ ) => {
+ const isFullscreen = hash.includes("fullscreen");
+
+ if (
+ !currentUser ||
+ (isEmbedded && !isEmbeddedAppBarVisible) ||
+ isAdminApp ||
+ isDataStudioApp ||
+ isEditingDashboard ||
+ isFullscreen
+ ) {
+ return false;
+ }
+ return !PATHS_WITHOUT_NAVBAR.some((pattern) => pattern.test(path));
+ },
+);
+
+export const getCollectionId = createSelector(
+ [
+ getQuestion,
+ getDashboard,
+ getDashboardId,
+ getCurrentDocument,
+ getDetailViewState,
+ getRouterPath,
+ ],
+ (question, dashboard, dashboardId, document, detailView, path) => {
+ if (detailView) {
+ return detailView.collectionId;
+ }
+
+ if (document) {
+ return document.collection_id;
+ }
+
+ if (dashboardId) {
+ return dashboard?.collection_id;
+ }
+
+ const questionCollectionId = question?.collectionId();
+ if (questionCollectionId != null) {
+ return questionCollectionId;
+ }
+
+ // On a collection page the URL itself identifies the current collection.
+ return Urls.extractCollectionIdFromPath(path);
+ },
+);
diff --git a/frontend/src/metabase/selectors/app.unit.spec.ts b/frontend/src/metabase/app/selectors.unit.spec.ts
similarity index 90%
rename from frontend/src/metabase/selectors/app.unit.spec.ts
rename to frontend/src/metabase/app/selectors.unit.spec.ts
index 88dc005f32d..9ae50b0d2f0 100644
--- a/frontend/src/metabase/selectors/app.unit.spec.ts
+++ b/frontend/src/metabase/app/selectors.unit.spec.ts
@@ -1,8 +1,9 @@
import type { Location } from "history";
import { createMockState } from "metabase/redux/store/mocks";
+import type { RouterProps } from "metabase/selectors/app";
-import { type RouterProps, getIsCollectionPathVisible } from "./app";
+import { getIsCollectionPathVisible } from "./selectors";
const createLocation = (pathname: string): Location =>
({
diff --git a/frontend/src/metabase/nav/containers/CollectionBreadcrumbs/CollectionBreadcrumbs.tsx b/frontend/src/metabase/nav/containers/CollectionBreadcrumbs/CollectionBreadcrumbs.tsx
index 4f537fe4a97..9934c5363de 100644
--- a/frontend/src/metabase/nav/containers/CollectionBreadcrumbs/CollectionBreadcrumbs.tsx
+++ b/frontend/src/metabase/nav/containers/CollectionBreadcrumbs/CollectionBreadcrumbs.tsx
@@ -1,6 +1,4 @@
import { useGetCollectionQuery } from "metabase/api";
-import { useSelector } from "metabase/redux";
-import { getCollectionId } from "metabase/selectors/app";
import type { CollectionId } from "metabase-types/api";
import {
@@ -17,8 +15,7 @@ type CollectionBreadcrumbsProps = Omit<
};
export const CollectionBreadcrumbs = (props: CollectionBreadcrumbsProps) => {
- const statefulCollectionId = useSelector(getCollectionId);
- const collectionId = props.collectionId ?? statefulCollectionId ?? "root";
+ const collectionId = props.collectionId ?? "root";
const { data: collection } = useGetCollectionQuery({ id: collectionId });
diff --git a/frontend/src/metabase/selectors/app.ts b/frontend/src/metabase/selectors/app.ts
index e867922738c..3bd72ca8a9a 100644
--- a/frontend/src/metabase/selectors/app.ts
+++ b/frontend/src/metabase/selectors/app.ts
@@ -2,23 +2,12 @@ import type { Selector } from "@reduxjs/toolkit";
import { createSelector } from "@reduxjs/toolkit";
import type { Location } from "history";
-import {
- getDashboard,
- getDashboardId,
- getIsEditing as getIsEditingDashboard,
-} from "metabase/dashboard/selectors";
-import { getCurrentDocument } from "metabase/documents/selectors";
-import {
- getIsSavedQuestionChanged,
- getQuestion,
-} from "metabase/query_builder/selectors";
import type { State } from "metabase/redux/store";
import {
getEmbedOptions,
getIsEmbeddingIframe,
} from "metabase/selectors/embed";
import { getUser } from "metabase/selectors/user";
-import * as Urls from "metabase/urls";
import { getSetting } from "./settings";
@@ -26,202 +15,6 @@ export interface RouterProps {
location: Location;
}
-const PATHS_WITHOUT_NAVBAR = [
- /^\/setup/,
- /^\/auth/,
- /^\/data-studio/,
- /\/model\/.*\/query/,
- /\/model\/.*\/columns/,
- /\/model\/.*\/metadata/,
- /\/model\/query/,
- /\/model\/columns/,
- /\/model\/metadata/,
- /\/transform\/new\/.*\/query/,
-];
-
-const PATHS_WITH_COLLECTION_BREADCRUMBS = [
- /\/question\//,
- /\/model\//,
- /\/metric\//,
- /\/dashboard\//,
- /\/document\//,
-];
-
-// Paths where collection identity comes from the URL itself, so breadcrumbs
-// can render without needing a question/dashboard/document in redux state.
-const STANDALONE_COLLECTION_BREADCRUMB_PATHS = [/\/collection\//];
-const PATHS_WITH_QUESTION_LINEAGE = [/\/question/, /\/model/];
-
-export const getRouterPath = (state: State, props: RouterProps) => {
- return props?.location?.pathname ?? window.location.pathname;
-};
-
-export const getRouterHash = (state: State, props: RouterProps) => {
- return props?.location?.hash ?? window.location.hash;
-};
-
-export const getIsAdminApp = createSelector([getRouterPath], (path) => {
- return path.startsWith("/admin/");
-});
-
-export const getIsDataStudioApp = createSelector([getRouterPath], (path) => {
- return path.startsWith("/data-studio");
-});
-
-export const getIsMetricsViewer = createSelector([getRouterPath], (path) => {
- return path.startsWith("/explore");
-});
-
-export const getIsCollectionPathVisible = createSelector(
- [
- getQuestion,
- getDashboard,
- getCurrentDocument,
- getRouterPath,
- getIsEmbeddingIframe,
- getEmbedOptions,
- ],
- (question, dashboard, document, path, isEmbedded, embedOptions) => {
- if (isEmbedded && !embedOptions.breadcrumbs) {
- return false;
- }
-
- const isModelDetail = /\/model\/.*\/detail\/.*/.test(path);
- if (isModelDetail) {
- return true;
- }
-
- if (
- STANDALONE_COLLECTION_BREADCRUMB_PATHS.some((pattern) =>
- pattern.test(path),
- )
- ) {
- return true;
- }
-
- return (
- ((question != null && question.isSaved()) ||
- dashboard != null ||
- document !== null) &&
- PATHS_WITH_COLLECTION_BREADCRUMBS.some((pattern) => pattern.test(path))
- );
- },
-);
-
-export const getIsQuestionLineageVisible = createSelector(
- [getIsSavedQuestionChanged, getRouterPath],
- (isSavedQuestionChanged, path) =>
- isSavedQuestionChanged &&
- PATHS_WITH_QUESTION_LINEAGE.some((pattern) => pattern.test(path)),
-);
-
-export const getIsNavBarEnabled = createSelector(
- [
- getUser,
- getRouterPath,
- getIsEditingDashboard,
- getIsEmbeddingIframe,
- getEmbedOptions,
- ],
- (currentUser, path, isEditingDashboard, isEmbedded, embedOptions) => {
- if (!currentUser || isEditingDashboard) {
- return false;
- }
- if (isEmbedded && !embedOptions.side_nav) {
- return false;
- }
-
- return !PATHS_WITHOUT_NAVBAR.some((pattern) => pattern.test(path));
- },
-);
-
-const getIsEmbeddedAppBarVisible = createSelector(
- [
- getEmbedOptions,
- getIsQuestionLineageVisible,
- getIsCollectionPathVisible,
- getIsNavBarEnabled,
- ],
- (
- embedOptions,
- isQuestionLineageVisible,
- isCollectionPathVisible,
- isNavBarEnabled,
- ) => {
- const anyEmbeddedAppBarElementVisible =
- isNavBarEnabled ||
- embedOptions.search ||
- embedOptions.new_button ||
- embedOptions.logo ||
- isQuestionLineageVisible ||
- isCollectionPathVisible;
- return embedOptions.top_nav && anyEmbeddedAppBarElementVisible;
- },
-);
-
-export const getIsAppBarVisible = createSelector(
- [
- getUser,
- getRouterPath,
- getRouterHash,
- getIsAdminApp,
- getIsDataStudioApp,
- getIsEditingDashboard,
- getIsEmbeddingIframe,
- getIsEmbeddedAppBarVisible,
- ],
- (
- currentUser,
- path,
- hash,
- isAdminApp,
- isDataStudioApp,
- isEditingDashboard,
- isEmbedded,
- isEmbeddedAppBarVisible,
- ) => {
- const isFullscreen = hash.includes("fullscreen");
-
- if (
- !currentUser ||
- (isEmbedded && !isEmbeddedAppBarVisible) ||
- isAdminApp ||
- isDataStudioApp ||
- isEditingDashboard ||
- isFullscreen
- ) {
- return false;
- }
- return !PATHS_WITHOUT_NAVBAR.some((pattern) => pattern.test(path));
- },
-);
-
-export const getIsLogoVisible = createSelector(
- [getIsEmbeddingIframe, getEmbedOptions],
- (isEmbeddingIframe, embedOptions) => {
- return !isEmbeddingIframe || embedOptions.logo;
- },
-);
-
-export const getIsSearchVisible = createSelector(
- [getIsEmbeddingIframe, getEmbedOptions],
- (isEmbeddingIframe, embedOptions) => {
- return !isEmbeddingIframe || embedOptions.search;
- },
-);
-
-export const getIsNewButtonVisible = createSelector(
- [getIsEmbeddingIframe, getEmbedOptions],
- (isEmbeddingIframe, embedOptions) => {
- return !isEmbeddingIframe || embedOptions.new_button;
- },
-);
-
-export const getIsAppSwitcherVisible = createSelector(
- [getIsEmbeddingIframe],
- (isEmbeddingIframe) => !isEmbeddingIframe,
-);
-
export const getErrorPage = (state: State) => {
return state.app.errorPage;
};
@@ -235,52 +28,25 @@ export const getErrorMessage = (state: State) => {
return errorPage?.data?.message || errorPage?.data;
};
-export const getCollectionId = createSelector(
- [
- getQuestion,
- getDashboard,
- getDashboardId,
- getCurrentDocument,
- getDetailViewState,
- getRouterPath,
- ],
- (question, dashboard, dashboardId, document, detailView, path) => {
- if (detailView) {
- return detailView.collectionId;
- }
-
- if (document) {
- return document.collection_id;
- }
-
- if (dashboardId) {
- return dashboard?.collection_id;
- }
-
- const questionCollectionId = question?.collectionId();
- if (questionCollectionId != null) {
- return questionCollectionId;
- }
-
- // On a collection page the URL itself identifies the current collection.
- return Urls.extractCollectionIdFromPath(path);
- },
-);
-
export const getIsNavbarOpen: Selector<State, boolean> = createSelector(
[
getIsEmbeddingIframe,
getEmbedOptions,
- getIsAppBarVisible,
+ (_state: State) => window.location.hash,
(state: State) => state.app.isNavbarOpen,
],
- (isEmbeddingIframe, embedOptions, isAppBarVisible, isNavbarOpen) => {
- // in an embedded instance, when the app bar is hidden, but the nav bar is not
- // we need to force the sidebar to be open or else it will be totally inaccessible
+ (isEmbeddingIframe, embedOptions, locationHash, isNavbarOpen) => {
+ // In an embedded instance, when the app bar is hidden but the side nav is
+ // enabled, force the sidebar open or it would be totally inaccessible.
+ //
+ // The app bar is hidden exactly when `top_nav` is off or we're in
+ // fullscreen. The other factors in the app-tier getIsAppBarVisible only
+ // matter when the nav bar isn't rendered anyway.
+ const isFullscreen = locationHash.includes("fullscreen");
if (
isEmbeddingIframe &&
embedOptions.side_nav === true &&
- !isAppBarVisible
+ (!embedOptions.top_nav || isFullscreen)
) {
return true;
}