Skip to content

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

Move open-url out of visualizations into urls (#81842)

Fraser75bce3e61926
diff --git a/frontend/lint/side-effect-files.json b/frontend/lint/side-effect-files.json
index f430f96dcc6..ae480ec94ca 100644
--- a/frontend/lint/side-effect-files.json
+++ b/frontend/lint/side-effect-files.json
@@ -204,7 +204,6 @@
"frontend/src/metabase/visualizations/components/EChartsRenderer/EChartsRenderer.tsx": "global",
"frontend/src/metabase/visualizations/components/LeafletChoropleth.tsx": "self",
"frontend/src/metabase/visualizations/components/LeafletMap.tsx": "self",
- "frontend/src/metabase/visualizations/lib/open-url.ts": "self",
"frontend/src/metabase/visualizations/visualizations/Map/Map.tsx": "self",
"frontend/src/metabase/visualizations/visualizations/ObjectDetail/ObjectDetail.ts": "self",
"frontend/src/metabase/visualizations/visualizations/PivotTable/PivotTable.tsx": "self",
diff --git a/frontend/src/metabase/app.tsx b/frontend/src/metabase/app.tsx
index 5b4fbb8f9a4..2a4bb513d51 100644
--- a/frontend/src/metabase/app.tsx
+++ b/frontend/src/metabase/app.tsx
@@ -47,6 +47,7 @@ import { refetchSiteSettings } from "metabase/settings";
import { GlobalStyles } from "metabase/styled-components/containers/GlobalStyles";
import { PortalContainer } from "metabase/ui";
import { EmotionCacheProvider } from "metabase/ui/components/theme/EmotionCacheProvider";
+import { captureClickModifierKeys } from "metabase/urls";
import { setBasename } from "metabase/utils/basename";
import { captureConsoleErrors } from "metabase/utils/errors";
import { initMetaplow } from "metabase/utils/metaplow";
@@ -104,6 +105,7 @@ function _init(
});
initializeInteractiveEmbedding(store.dispatch);
+ captureClickModifierKeys();
const rootElement = document.getElementById("root");
if (!rootElement) {
diff --git a/frontend/src/metabase/common/components/ExternalLink/ExternalLink.tsx b/frontend/src/metabase/common/components/ExternalLink/ExternalLink.tsx
index cc7a4d43646..0e00ab00d52 100644
--- a/frontend/src/metabase/common/components/ExternalLink/ExternalLink.tsx
+++ b/frontend/src/metabase/common/components/ExternalLink/ExternalLink.tsx
@@ -2,7 +2,7 @@ import type { AnchorHTMLAttributes, ReactNode, Ref } from "react";
import { forwardRef } from "react";
import CS from "metabase/css/core/index.css";
-import { getUrlTarget } from "metabase/visualizations/lib/open-url";
+import { getUrlTarget } from "metabase/urls";
import S from "./ExternalLink.module.css";
diff --git a/frontend/src/metabase/dashboard/visualizations/LinkViz/LinkViz.tsx b/frontend/src/metabase/dashboard/visualizations/LinkViz/LinkViz.tsx
index 7dc29f755b3..15fda8e1a31 100644
--- a/frontend/src/metabase/dashboard/visualizations/LinkViz/LinkViz.tsx
+++ b/frontend/src/metabase/dashboard/visualizations/LinkViz/LinkViz.tsx
@@ -8,8 +8,7 @@ import { fillParametersInText } from "metabase/dashboard/visualizations/paramete
import { SearchResults } from "metabase/nav/components/search/SearchResults";
import { useSelector } from "metabase/redux";
import { Popover, TextInput } from "metabase/ui";
-import { modelToUrl } from "metabase/urls";
-import { getUrlTarget } from "metabase/visualizations/lib/open-url";
+import { getUrlTarget, modelToUrl } from "metabase/urls";
import type {
Dashboard,
LinkCardSettings,
diff --git a/frontend/src/metabase/query_builder/actions/core/core.ts b/frontend/src/metabase/query_builder/actions/core/core.ts
index bc0642a5a54..9a671661aa7 100644
--- a/frontend/src/metabase/query_builder/actions/core/core.ts
+++ b/frontend/src/metabase/query_builder/actions/core/core.ts
@@ -25,7 +25,6 @@ import type { Dispatch, GetState } from "metabase/redux/store";
import * as Urls from "metabase/urls";
import { clone } from "metabase/utils/clone";
import { isNotNull } from "metabase/utils/types";
-import { shouldOpenInBlankWindow } from "metabase/visualizations/lib/open-url";
import {
getCardAfterVisualizationClick,
getRegisteredDefaultSize,
@@ -180,7 +179,7 @@ export const navigateToNewCardInsideQB = createThunkAction(
previousCard,
);
const url = Urls.serializedQuestion(cardAfterClick);
- if (shouldOpenInBlankWindow(url, { blankOnMetaOrCtrlKey: true })) {
+ if (Urls.shouldOpenInBlankWindow(url, { blankOnMetaOrCtrlKey: true })) {
dispatch(openUrl(url));
} else {
dispatch(onCloseSidebars());
diff --git a/frontend/src/metabase/redux/app.ts b/frontend/src/metabase/redux/app.ts
index c77417a8dd3..de28e98758c 100644
--- a/frontend/src/metabase/redux/app.ts
+++ b/frontend/src/metabase/redux/app.ts
@@ -12,8 +12,8 @@ import type {
TempStorageValue,
} from "metabase/redux/store";
import { LOCATION_CHANGE, navigate } from "metabase/router";
+import { shouldOpenInBlankWindow } from "metabase/urls";
import { isSmallScreen, openInBlankWindow } from "metabase/utils/dom";
-import { shouldOpenInBlankWindow } from "metabase/visualizations/lib/open-url";
interface LocationChangeAction {
type: string; // "@@router/LOCATION_CHANGE"
diff --git a/frontend/src/metabase/urls/index.ts b/frontend/src/metabase/urls/index.ts
index 53231f94dad..989a2d345a5 100644
--- a/frontend/src/metabase/urls/index.ts
+++ b/frontend/src/metabase/urls/index.ts
@@ -19,6 +19,12 @@ export * from "./misc";
export * from "./monitor";
export * from "./models";
export * from "./modelToUrl";
+export {
+ captureClickModifierKeys,
+ getUrlTarget,
+ openUrl,
+ shouldOpenInBlankWindow,
+} from "./open-url";
export * from "./permissions";
export * from "./questions";
export * from "./timelines";
diff --git a/frontend/src/metabase/visualizations/lib/open-url.ts b/frontend/src/metabase/urls/open-url.ts
similarity index 91%
rename from frontend/src/metabase/visualizations/lib/open-url.ts
rename to frontend/src/metabase/urls/open-url.ts
index a7a939a9293..cde0b189989 100644
--- a/frontend/src/metabase/visualizations/lib/open-url.ts
+++ b/frontend/src/metabase/urls/open-url.ts
@@ -11,18 +11,26 @@ import {
isSameOrigin,
} from "metabase/utils/dom";
-// need to keep track of the latest click's state because sometimes
-// `openUrl` is called asynchronously, thus window.event isn't the click event
+// shouldOpenInBlankWindow falls back to these when window.event is not a mouse event,
+// which happens when openUrl is called after an await.
let metaKey: boolean = false;
let ctrlKey: boolean = false;
-window.addEventListener(
- "mouseup",
- (e: MouseEvent) => {
- metaKey = e.metaKey;
- ctrlKey = e.ctrlKey;
- },
- true,
-);
+let isCapturingClickModifierKeys = false;
+
+export function captureClickModifierKeys() {
+ if (isCapturingClickModifierKeys) {
+ return;
+ }
+ isCapturingClickModifierKeys = true;
+ window.addEventListener(
+ "mouseup",
+ (e: MouseEvent) => {
+ metaKey = e.metaKey;
+ ctrlKey = e.ctrlKey;
+ },
+ true,
+ );
+}
type ShouldOpenInBlankWindowOptions = {
event?: MouseEvent | null;
diff --git a/frontend/src/metabase/visualizations/lib/open-url.unit.spec.ts b/frontend/src/metabase/urls/open-url.unit.spec.ts
similarity index 80%
rename from frontend/src/metabase/visualizations/lib/open-url.unit.spec.ts
rename to frontend/src/metabase/urls/open-url.unit.spec.ts
index b9f20ff1488..dde20a2afce 100644
--- a/frontend/src/metabase/visualizations/lib/open-url.unit.spec.ts
+++ b/frontend/src/metabase/urls/open-url.unit.spec.ts
@@ -2,12 +2,14 @@ import { setupSdkPlugins } from "__support__/enterprise";
import { mockSettings } from "__support__/settings";
import { ensureMetabaseProviderPropsStore } from "embedding-sdk-shared/lib/ensure-metabase-provider-props-store";
import { mockIsEmbeddingSdk } from "metabase/embedding-sdk/mocks/config-mock";
+import { createMockTokenFeatures } from "metabase-types/api/mocks";
+
import {
+ captureClickModifierKeys,
getUrlTarget,
openUrl,
shouldOpenInBlankWindow,
-} from "metabase/visualizations/lib/open-url";
-import { createMockTokenFeatures } from "metabase-types/api/mocks";
+} from "./open-url";
describe("shouldOpenInBlankWindow", () => {
afterEach(() => {
@@ -28,6 +30,37 @@ describe("shouldOpenInBlankWindow", () => {
});
});
+describe("captureClickModifierKeys", () => {
+ const url = `${window.location.origin}/dashboard/1`;
+
+ function mouseUp(metaKey: boolean) {
+ window.dispatchEvent(new MouseEvent("mouseup", { metaKey }));
+ }
+
+ afterEach(() => {
+ jest.restoreAllMocks();
+ });
+
+ // Runs before any test in this file calls captureClickModifierKeys,
+ // since the listener stays registered.
+ it("should ignore the last click's modifier keys until it is called", () => {
+ mouseUp(true);
+ expect(shouldOpenInBlankWindow(url, { event: null })).toBe(false);
+ });
+
+ it("should register one mouseup listener and read the last click's modifier keys", () => {
+ const addEventListener = jest.spyOn(window, "addEventListener");
+ captureClickModifierKeys();
+ captureClickModifierKeys();
+ expect(addEventListener).toHaveBeenCalledTimes(1);
+
+ mouseUp(true);
+ expect(shouldOpenInBlankWindow(url, { event: null })).toBe(true);
+ mouseUp(false);
+ expect(shouldOpenInBlankWindow(url, { event: null })).toBe(false);
+ });
+});
+
describe("getUrlTarget", () => {
afterEach(() => {
jest.restoreAllMocks();
diff --git a/frontend/src/metabase/visualizations/lib/action.ts b/frontend/src/metabase/visualizations/lib/action.ts
index ee71dd27fd6..995b72d39ff 100644
--- a/frontend/src/metabase/visualizations/lib/action.ts
+++ b/frontend/src/metabase/visualizations/lib/action.ts
@@ -3,6 +3,7 @@ import _ from "underscore";
import type { Dispatch } from "metabase/redux/store";
import type { Path } from "metabase/router";
import { navigate } from "metabase/router";
+import { openUrl } from "metabase/urls";
import type Question from "metabase-lib/v1/Question";
import type {
@@ -11,8 +12,6 @@ import type {
QuestionChangeClickAction,
} from "../types";
-import { openUrl } from "./open-url";
-
type ActionProps = {
dispatch: Dispatch;
onChangeCardAndRun?: OnChangeCardAndRun;