Skip to content

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

fix(modular): links and type/URL columns should be rendered as clickable (#64243)

Nicolò Pretto88229c321eca
diff --git a/e2e/test-component/scenarios/embedding-sdk/dashboard-click-behavior.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/dashboard-click-behavior.cy.spec.tsx
index b395c77726d..ca5a556f679 100644
--- a/e2e/test-component/scenarios/embedding-sdk/dashboard-click-behavior.cy.spec.tsx
+++ b/e2e/test-component/scenarios/embedding-sdk/dashboard-click-behavior.cy.spec.tsx
@@ -1,6 +1,7 @@
import {
EditableDashboard,
InteractiveDashboard,
+ InteractiveQuestion,
} from "@metabase/embedding-sdk-react";
const { H } = cy;
@@ -26,6 +27,30 @@ describe("scenarios > embedding-sdk > dashboard-click-behavior", () => {
semantic_type: "type/URL",
});
+ // Create a question with custom column that returns a url as a string
+ H.createQuestion({
+ name: "Orders with URL Expression",
+ query: {
+ "source-table": ORDERS_ID,
+ expressions: {
+ // NOTE: this is a question, so it won't have semantic_type set as url, we'll need a model for that
+ url_column: [
+ "concat",
+ "https://example.org/",
+ ["field", ORDERS.ID, { "base-type": "type/BigInteger" }],
+ ],
+ },
+ fields: [
+ ["field", ORDERS.ID, { "base-type": "type/BigInteger" }],
+ ["expression", "url_column", { "base-type": "type/Text" }],
+ ["field", ORDERS.PRODUCT_ID, null],
+ ],
+ limit: 5,
+ },
+ }).then(({ body: question }) => {
+ cy.wrap(question.id).as("questionId");
+ });
+
H.createDashboardWithQuestions({
dashboardName: "Orders in a dashboard",
questions: [
@@ -60,7 +85,7 @@ describe("scenarios > embedding-sdk > dashboard-click-behavior", () => {
],
cards: [
{
- size_x: 12,
+ size_x: 24,
col: 0,
visualization_settings: {
column_settings: {
@@ -77,7 +102,7 @@ describe("scenarios > embedding-sdk > dashboard-click-behavior", () => {
},
{
size_x: 12,
- col: 12,
+ col: 0,
visualization_settings: {
click_behavior: {
type: "link",
@@ -193,4 +218,123 @@ describe("scenarios > embedding-sdk > dashboard-click-behavior", () => {
cy.findByTestId("visualization-root").should("be.visible");
});
});
+
+ it("columns that return a string url should be rendered as a link", () => {
+ cy.get<string>("@questionId").then((questionId) => {
+ mountSdkContent(<InteractiveQuestion questionId={questionId} />);
+ });
+
+ cy.intercept("GET", "/api/card/*").as("getCard");
+ cy.wait("@getCard");
+
+ cy.findByRole("link", { name: "https://example.org/448" })
+ .should("have.length", 1)
+ .should("have.attr", "href", "https://example.org/448");
+ });
+
+ it("columns that have 'type/URL' semantic type should be rendered as a link", () => {
+ cy.signIn("admin");
+
+ cy.get<string>("@questionId").then((questionId) => {
+ // Convert question to model
+ cy.request("GET", `/api/card/${questionId}`)
+ .then((response) => {
+ const questionData = response.body;
+ return cy.request("PUT", `/api/card/${questionId}`, {
+ ...questionData,
+ type: "model",
+ });
+ })
+ .then(() => {
+ // re-fetch it as model
+ return cy.request("GET", `/api/card/${questionId}`);
+ })
+ .then((modelResponse) => {
+ // Update model column semantic type for url_column to type/URL
+
+ const modelData = modelResponse.body;
+ const updatedMetadata = modelData.result_metadata.map((col: any) => {
+ if (col.name === "url_column") {
+ return { ...col, semantic_type: "type/URL" };
+ }
+ return col;
+ });
+
+ return cy.request("PUT", `/api/card/${questionId}`, {
+ ...modelData,
+ result_metadata: updatedMetadata,
+ });
+ })
+ .then(() => {
+ // Create dashboard with the model
+ H.createDashboardWithQuestions({
+ dashboardName: "URL Dashboard",
+ questions: [
+ {
+ name: "Orders with URLs",
+ query: {
+ "source-table": `card__${questionId}`,
+ limit: 5,
+ },
+ },
+ ],
+ }).then(({ dashboard }) => {
+ mountSdkContent(
+ <InteractiveDashboard dashboardId={dashboard.id} />,
+ );
+
+ getSdkRoot().within(() => {
+ H.getDashboardCard(0)
+ .findByRole("link", {
+ name: "https://example.org/448",
+ })
+ .should("have.attr", "href", "https://example.org/448");
+ });
+ });
+ });
+ });
+ });
+
+ it("columns that have 'Display as Link' should be rendered as a link and use the custom rendering", () => {
+ cy.signIn("admin");
+ cy.intercept("GET", "/api/card/*").as("getCard");
+
+ cy.get<string>("@questionId").then((questionId) => {
+ H.createDashboardWithQuestions({
+ dashboardName: "URL Dashboard",
+ questions: [
+ {
+ name: "Orders with URLs",
+ query: {
+ "source-table": `card__${questionId}`,
+ limit: 5,
+ },
+ },
+ ],
+ cards: [
+ {
+ visualization_settings: {
+ column_settings: {
+ [JSON.stringify(["name", "ID"])]: {
+ view_as: "link",
+ link_text: "Link to {{ID}}",
+ link_url: "https://example.org/{{ID}}",
+ },
+ },
+ },
+ },
+ ],
+ }).then(({ dashboard }) => {
+ mountSdkContent(<EditableDashboard dashboardId={dashboard.id} />);
+
+ getSdkRoot().within(() => {
+ H.getDashboardCard(0)
+ .findByRole("link", {
+ name: "Link to 448",
+ })
+ .should("have.attr", "href", "https://example.org/448");
+ });
+ });
+ });
+ });
});
diff --git a/frontend/src/metabase/lib/formatting/url.tsx b/frontend/src/metabase/lib/formatting/url.tsx
index 5af104f2ec9..2a811891a90 100644
--- a/frontend/src/metabase/lib/formatting/url.tsx
+++ b/frontend/src/metabase/lib/formatting/url.tsx
@@ -3,7 +3,6 @@ import cx from "classnames";
import ExternalLink from "metabase/common/components/ExternalLink";
import Link from "metabase/common/components/Link";
import CS from "metabase/css/core/index.css";
-import { isEmbeddingSdk } from "metabase/embedding-sdk/config";
import { isSameOrSiteUrlOrigin } from "metabase/lib/dom";
import { getDataFromClicked } from "metabase-lib/v1/parameters/utils/click-behavior";
import { isURL } from "metabase-lib/v1/types/utils/isa";
@@ -43,11 +42,6 @@ export function formatUrl(value: string, options: OptionsType = {}) {
const text = getLinkText(value, options);
const className = cx(CS.link, CS.linkWrappable);
- // (metabase#51099) prevent url from being rendered as a link when in sdk
- if (isEmbeddingSdk()) {
- return url;
- }
-
if (isSameOrSiteUrlOrigin(url)) {
return (
<Link className={className} to={url}>