Skip to content

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

fix: documents ignore base path when linking to dashboards (#68633)

Maksym Yakubych0421a12c9ced
diff --git a/frontend/src/metabase/rich_text_editing/tiptap/extensions/SmartLink/SmartLinkNode.tsx b/frontend/src/metabase/rich_text_editing/tiptap/extensions/SmartLink/SmartLinkNode.tsx
index 2599c45bb4a..248e6acbd6a 100644
--- a/frontend/src/metabase/rich_text_editing/tiptap/extensions/SmartLink/SmartLinkNode.tsx
+++ b/frontend/src/metabase/rich_text_editing/tiptap/extensions/SmartLink/SmartLinkNode.tsx
@@ -19,6 +19,7 @@ import {
useGetTableQuery,
useListMentionsQuery,
} from "metabase/api";
+import { Link } from "metabase/common/components/Link";
import { updateMentionsCache } from "metabase/documents/documents.slice";
import {
type IconModel,
@@ -516,8 +517,8 @@ export const SmartLinkComponent = memo(
return (
<NodeViewWrapper as="span" data-type="smart-link">
- <a
- href={entityUrl || "#"}
+ <Link
+ to={entityUrl || "#"}
target="_blank"
rel="noreferrer"
tabIndex={-1}
@@ -531,7 +532,7 @@ export const SmartLinkComponent = memo(
<Icon name={iconData.name} className={styles.icon} />
{getName(entity)}
</span>
- </a>
+ </Link>
</NodeViewWrapper>
);
},
diff --git a/frontend/src/metabase/rich_text_editing/tiptap/extensions/SmartLink/SmartLinkNode.unit.spec.tsx b/frontend/src/metabase/rich_text_editing/tiptap/extensions/SmartLink/SmartLinkNode.unit.spec.tsx
index c91d3ea341b..561b62e3d9e 100644
--- a/frontend/src/metabase/rich_text_editing/tiptap/extensions/SmartLink/SmartLinkNode.unit.spec.tsx
+++ b/frontend/src/metabase/rich_text_editing/tiptap/extensions/SmartLink/SmartLinkNode.unit.spec.tsx
@@ -1,4 +1,6 @@
import type { NodeViewProps } from "@tiptap/react";
+import { createMemoryHistory } from "history";
+import { Route, Router, useRouterHistory } from "react-router";
import {
setupCardEndpoints,
@@ -166,4 +168,67 @@ describe("SmartLink", () => {
});
});
});
+
+ describe("link generation", () => {
+ it("should include subpath in link href when router has basename", async () => {
+ const dashboard = createMockDashboard({
+ id: 456,
+ name: "Subpath Dashboard",
+ });
+
+ setupDashboardEndpoints(dashboard);
+
+ const historyWithBasename = useRouterHistory(createMemoryHistory)({
+ basename: "/subpath",
+ entries: ["/"],
+ });
+
+ const props = createProps("dashboard", dashboard);
+ renderWithProviders(
+ <Router history={historyWithBasename}>
+ <Route path="*" component={() => <SmartLinkComponent {...props} />} />
+ </Router>,
+ );
+
+ await waitFor(() => {
+ expect(screen.getByText("Subpath Dashboard")).toBeInTheDocument();
+ });
+
+ const link = screen.getByText("Subpath Dashboard").closest("a");
+ expect(link).toHaveAttribute(
+ "href",
+ "/subpath/dashboard/456-subpath-dashboard",
+ );
+ });
+
+ it("should work correctly without subpath", async () => {
+ const dashboard = createMockDashboard({
+ id: 789,
+ name: "No Subpath Dashboard",
+ });
+
+ setupDashboardEndpoints(dashboard);
+
+ const historyNoBasename = useRouterHistory(createMemoryHistory)({
+ entries: ["/"],
+ });
+
+ const props = createProps("dashboard", dashboard);
+ renderWithProviders(
+ <Router history={historyNoBasename}>
+ <Route path="*" component={() => <SmartLinkComponent {...props} />} />
+ </Router>,
+ );
+
+ await waitFor(() => {
+ expect(screen.getByText("No Subpath Dashboard")).toBeInTheDocument();
+ });
+
+ const link = screen.getByText("No Subpath Dashboard").closest("a");
+ expect(link).toHaveAttribute(
+ "href",
+ "/dashboard/789-no-subpath-dashboard",
+ );
+ });
+ });
});