Skip to content

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

UXW-4718 Dashboard tab selection not persisted on page refresh (#79669)

Denis Berezin706c3862ab47
diff --git a/enterprise/frontend/src/embedding-sdk-ee/notifications/DashboardSubscriptionsButton.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-ee/notifications/DashboardSubscriptionsButton.unit.spec.tsx
index 4798b8ec586..f5814533692 100644
--- a/enterprise/frontend/src/embedding-sdk-ee/notifications/DashboardSubscriptionsButton.unit.spec.tsx
+++ b/enterprise/frontend/src/embedding-sdk-ee/notifications/DashboardSubscriptionsButton.unit.spec.tsx
@@ -7,6 +7,7 @@ import {
setupNotificationChannelsEndpoints,
} from "__support__/server-mocks";
import { setupDashcardQueryEndpoints } from "__support__/server-mocks/dashcard";
+import { mockSettings } from "__support__/settings";
import { renderWithProviders } from "__support__/ui";
import { DashboardContextProvider } from "metabase/dashboard/context";
import type { DashboardCard } from "metabase-types/api";
@@ -61,6 +62,11 @@ const setup = ({
>
<DashboardSubscriptionsButton />
</DashboardContextProvider>,
+ {
+ storeInitialState: {
+ settings: mockSettings(),
+ },
+ },
);
};
diff --git a/frontend/src/metabase/dashboard/hooks/use-dashboard-url-query.ts b/frontend/src/metabase/dashboard/hooks/use-dashboard-url-query.ts
index 5e5960767bc..f12fd4e1629 100644
--- a/frontend/src/metabase/dashboard/hooks/use-dashboard-url-query.ts
+++ b/frontend/src/metabase/dashboard/hooks/use-dashboard-url-query.ts
@@ -16,6 +16,7 @@ import {
import { useSetting } from "metabase/settings";
import * as Urls from "metabase/urls";
import { parseSearchQuery } from "metabase/utils/browser";
+import { getPathnameWithoutSubPath } from "metabase/utils/dom";
import { getParameterValuesBySlug } from "metabase-lib/v1/parameters/utils/parameter-values";
import {
@@ -91,7 +92,7 @@ export function useDashboardUrlQuery(location: Location) {
return;
}
- const pathname = location.pathname.replace(siteUrl, "");
+ const pathname = getPathnameWithoutSubPath(location.pathname, siteUrl);
const isDashboardUrl = pathname.startsWith("/dashboard/");
if (isDashboardUrl) {
const dashboardSlug = pathname.replace("/dashboard/", "");
diff --git a/frontend/src/metabase/dashboard/selectors.ts b/frontend/src/metabase/dashboard/selectors.ts
index c538672ce63..06249cf38df 100644
--- a/frontend/src/metabase/dashboard/selectors.ts
+++ b/frontend/src/metabase/dashboard/selectors.ts
@@ -28,6 +28,7 @@ import { getMetadata } from "metabase/selectors/metadata";
import { getSetting } from "metabase/settings";
import * as Urls from "metabase/urls";
import { isQuestionCard, isQuestionDashCard } from "metabase/utils/dashboard";
+import { getPathnameWithoutSubPath } from "metabase/utils/dom";
import { selectIsWithinIframe } from "metabase/utils/iframe";
import { isNotNull } from "metabase/utils/types";
import { extendCardWithDashcardSettings } from "metabase/visualizations/lib/settings/typed-utils";
@@ -594,7 +595,7 @@ function getInitialSelectedTabId(
siteUrl: string,
isWebApp: boolean,
) {
- const pathname = window.location.pathname.replace(siteUrl, "");
+ const pathname = getPathnameWithoutSubPath(window.location.pathname, siteUrl);
const isDashboardUrl = pathname.includes("/dashboard/");
if (isDashboardUrl) {
diff --git a/frontend/src/metabase/dashboard/selectors.unit.spec.ts b/frontend/src/metabase/dashboard/selectors.unit.spec.ts
index ecbe3696fa0..5562add9e3b 100644
--- a/frontend/src/metabase/dashboard/selectors.unit.spec.ts
+++ b/frontend/src/metabase/dashboard/selectors.unit.spec.ts
@@ -9,6 +9,7 @@ import {
getIsEditingParameter,
getIsSharing,
getParameters,
+ getSelectedTabId,
getShowAddQuestionSidebar,
getSidebar,
} from "metabase/dashboard/selectors";
@@ -24,6 +25,7 @@ import {
createMockCard,
createMockDashboard,
createMockDashboardCard,
+ createMockDashboardTab,
createMockField,
createMockHeadingDashboardCard,
createMockNativeDatasetQuery,
@@ -463,3 +465,57 @@ describe("dashboard/selectors", () => {
});
});
});
+
+describe("getSelectedTabId", () => {
+ const DASHBOARD_ID = 1;
+
+ const createTabbedState = (siteUrl: string) =>
+ createMockState({
+ dashboard: createMockDashboardState({
+ dashboardId: DASHBOARD_ID,
+ selectedTabId: null,
+ dashboards: {
+ [DASHBOARD_ID]: createMockStoreDashboard({
+ id: DASHBOARD_ID,
+ tabs: [
+ createMockDashboardTab({ id: 1 }),
+ createMockDashboardTab({ id: 2 }),
+ ],
+ }),
+ },
+ }),
+ settings: createMockSettingsState({ "site-url": siteUrl }),
+ });
+
+ afterEach(() => {
+ window.history.replaceState({}, "", "/");
+ });
+
+ it("initializes from the tab query param", () => {
+ window.history.replaceState({}, "", "/dashboard/1?tab=2-second-tab");
+
+ const state = createTabbedState("http://localhost:3000");
+
+ expect(getSelectedTabId(state)).toBe(2);
+ });
+
+ it("initializes from the tab query param on a subpath deployment (metabase#76946)", () => {
+ window.history.replaceState(
+ {},
+ "",
+ "/metabase/dashboard/1?tab=2-second-tab",
+ );
+
+ const state = createTabbedState("http://localhost:3000/metabase");
+
+ expect(getSelectedTabId(state)).toBe(2);
+ });
+
+ it("falls back to the first tab while navigating to another dashboard", () => {
+ window.history.replaceState({}, "", "/dashboard/999?tab=2-second-tab");
+
+ const state = createTabbedState("http://localhost:3000");
+
+ expect(getSelectedTabId(state)).toBe(1);
+ });
+});
diff --git a/frontend/src/metabase/embedding/selectors.ts b/frontend/src/metabase/embedding/selectors.ts
index bbcb0fc5772..c5d3bfc2628 100644
--- a/frontend/src/metabase/embedding/selectors.ts
+++ b/frontend/src/metabase/embedding/selectors.ts
@@ -3,12 +3,16 @@ import { createSelector } from "reselect";
import { isEmbeddingSdk } from "metabase/embedding-sdk/config";
import type { State } from "metabase/redux/store";
import { getSetting } from "metabase/settings";
+import { getPathnameWithoutSubPath } from "metabase/utils/dom";
import { selectIsWithinIframe } from "metabase/utils/iframe";
export const getIsWebApp = createSelector(
[(state: State) => getSetting(state, "site-url"), selectIsWithinIframe],
(siteUrl, isEmbeddingIframe) => {
- const pathname = window.location.pathname.replace(siteUrl, "");
+ const pathname = getPathnameWithoutSubPath(
+ window.location.pathname,
+ siteUrl,
+ );
return (
!isEmbeddingIframe &&
!isEmbeddingSdk() &&
diff --git a/frontend/src/metabase/utils/dom.ts b/frontend/src/metabase/utils/dom.ts
index fd6ec7d476a..8b6c10eb47d 100644
--- a/frontend/src/metabase/utils/dom.ts
+++ b/frontend/src/metabase/utils/dom.ts
@@ -112,22 +112,30 @@ const getOrigin = (url: string): string | null => {
}
};
-export function getPathnameWithoutSubPath(pathname: string): string {
+export function getPathnameWithoutSubPath(
+ pathname: string,
+ siteUrl?: string,
+): string {
+ const sitePath =
+ siteUrl === undefined ? getSitePath() : getUrlPathname(siteUrl);
const pathnameSections = pathname.split("/");
- const sitePathSections = getSitePath().split("/");
+ const sitePathSections = sitePath.split("/");
return isPathnameContainSitePath(pathnameSections, sitePathSections)
? "/" + pathnameSections.slice(sitePathSections.length).join("/")
: pathname;
}
+const getUrlPathname = (url: string): string =>
+ url ? new URL(url).pathname.toLowerCase() : "";
+
function isPathnameContainSitePath(
pathnameSections: string[],
sitePathSections: string[],
): boolean {
for (let index = 0; index < sitePathSections.length; index++) {
const sitePathSection = sitePathSections[index].toLowerCase();
- const pathnameSection = pathnameSections[index].toLowerCase();
+ const pathnameSection = pathnameSections[index]?.toLowerCase();
if (sitePathSection !== pathnameSection) {
return false;
diff --git a/frontend/src/metabase/utils/dom.unit.spec.ts b/frontend/src/metabase/utils/dom.unit.spec.ts
new file mode 100644
index 00000000000..2365c075667
--- /dev/null
+++ b/frontend/src/metabase/utils/dom.unit.spec.ts
@@ -0,0 +1,58 @@
+import { getPathnameWithoutSubPath } from "./dom";
+
+describe("getPathnameWithoutSubPath", () => {
+ it("should leave the pathname unchanged when the site url has no subpath", () => {
+ expect(
+ getPathnameWithoutSubPath("/dashboard/1", "http://example.com"),
+ ).toBe("/dashboard/1");
+ expect(
+ getPathnameWithoutSubPath("/dashboard/1", "http://example.com/"),
+ ).toBe("/dashboard/1");
+ });
+
+ it("should strip the subpath from the pathname", () => {
+ expect(
+ getPathnameWithoutSubPath(
+ "/metabase/dashboard/1",
+ "http://example.com/metabase",
+ ),
+ ).toBe("/dashboard/1");
+ });
+
+ it("should strip the subpath case-insensitively", () => {
+ expect(
+ getPathnameWithoutSubPath(
+ "/Metabase/dashboard/1",
+ "http://example.com/metabase",
+ ),
+ ).toBe("/dashboard/1");
+ });
+
+ it("should not strip a subpath that appears mid-pathname", () => {
+ expect(
+ getPathnameWithoutSubPath(
+ "/dashboard/metabase/1",
+ "http://example.com/metabase",
+ ),
+ ).toBe("/dashboard/metabase/1");
+ });
+
+ it("should not strip a subpath that only matches a partial path segment", () => {
+ expect(
+ getPathnameWithoutSubPath(
+ "/metabase2/dashboard/1",
+ "http://example.com/metabase",
+ ),
+ ).toBe("/metabase2/dashboard/1");
+ });
+
+ it("should leave a pathname shorter than the subpath unchanged", () => {
+ expect(
+ getPathnameWithoutSubPath("/metabase", "http://example.com/metabase/sub"),
+ ).toBe("/metabase");
+ });
+
+ it("should leave the pathname unchanged when the site url is empty", () => {
+ expect(getPathnameWithoutSubPath("/dashboard/1", "")).toBe("/dashboard/1");
+ });
+});