The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Show collection breadcrumbs on collection pages (#74161)
Nick Fitzpatrickafc6a441f722
diff --git a/e2e/test/scenarios/organization/timelines-collection.cy.spec.js b/e2e/test/scenarios/organization/timelines-collection.cy.spec.jsindex 88afe999fe5..ffbf0eccb6d 100644--- a/e2e/test/scenarios/organization/timelines-collection.cy.spec.js+++ b/e2e/test/scenarios/organization/timelines-collection.cy.spec.js@@ -395,12 +395,13 @@ describe("scenarios > organization > timelines > collection", () => {cy.wait("@updateTimeline");});- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage- cy.findByText("Our analytics events").should("be.visible");- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage- cy.findByText(`${H.getFullName(admin)}'s Personal Collection`).should(- "be.visible",- );+ H.modal().within(() => {+ cy.findByText("Our analytics events").should("be.visible");+ cy.button(/close/).click();+ });+ H.main()+ .findByText(`${H.getFullName(admin)}'s Personal Collection`)+ .should("be.visible");});it("should archive a timeline and undo", () => {diff --git a/frontend/src/metabase/nav/containers/AppBar/AppBar.unit.spec.tsx b/frontend/src/metabase/nav/containers/AppBar/AppBar.unit.spec.tsxindex f14195de4fa..060a7f7e2da 100644--- a/frontend/src/metabase/nav/containers/AppBar/AppBar.unit.spec.tsx+++ b/frontend/src/metabase/nav/containers/AppBar/AppBar.unit.spec.tsx@@ -260,6 +260,17 @@ describe("AppBar", () => {expect(await screen.findByText("Foo Collection")).toBeInTheDocument();});++ it("should work for collection pages (UXW-249)", async () => {+ setup({+ initialRoute: `/collection/${FOO_COLLECTION.id}-foo-collection`,+ embedOptions: {+ breadcrumbs: true,+ },+ });++ expect(await screen.findByText("Foo Collection")).toBeInTheDocument();+ });});});});diff --git a/frontend/src/metabase/selectors/app.ts b/frontend/src/metabase/selectors/app.tsindex 32ce98134f4..e867922738c 100644--- a/frontend/src/metabase/selectors/app.ts+++ b/frontend/src/metabase/selectors/app.ts@@ -18,6 +18,7 @@ import {getIsEmbeddingIframe,} from "metabase/selectors/embed";import { getUser } from "metabase/selectors/user";+import * as Urls from "metabase/urls";import { getSetting } from "./settings";@@ -45,6 +46,10 @@ const PATHS_WITH_COLLECTION_BREADCRUMBS = [/\/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) => {@@ -86,6 +91,14 @@ export const getIsCollectionPathVisible = createSelector(return true;}+ if (+ STANDALONE_COLLECTION_BREADCRUMB_PATHS.some((pattern) =>+ pattern.test(path),+ )+ ) {+ return true;+ }+return (((question != null && question.isSaved()) ||dashboard != null ||@@ -229,8 +242,9 @@ export const getCollectionId = createSelector(getDashboardId,getCurrentDocument,getDetailViewState,+ getRouterPath,],- (question, dashboard, dashboardId, document, detailView) => {+ (question, dashboard, dashboardId, document, detailView, path) => {if (detailView) {return detailView.collectionId;}@@ -243,7 +257,13 @@ export const getCollectionId = createSelector(return dashboard?.collection_id;}- return question?.collectionId();+ 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/selectors/app.unit.spec.tsnew file mode 100644index 00000000000..88dc005f32d--- /dev/null+++ b/frontend/src/metabase/selectors/app.unit.spec.ts@@ -0,0 +1,43 @@+import type { Location } from "history";++import { createMockState } from "metabase/redux/store/mocks";++import { type RouterProps, getIsCollectionPathVisible } from "./app";++const createLocation = (pathname: string): Location =>+ ({+ pathname,+ search: "",+ hash: "",+ state: undefined,+ action: "PUSH",+ key: "",+ query: {},+ }) as unknown as Location;++const createRouterProps = (pathname: string): RouterProps => ({+ location: createLocation(pathname),+});++describe("getIsCollectionPathVisible", () => {+ it("is true on a collection page even when no question/dashboard/document is loaded", () => {+ const state = createMockState();+ const props = createRouterProps("/collection/5-foo");++ expect(getIsCollectionPathVisible(state, props)).toBe(true);+ });++ it("is true on the root collection page", () => {+ const state = createMockState();+ const props = createRouterProps("/collection/root");++ expect(getIsCollectionPathVisible(state, props)).toBe(true);+ });++ it("is false on unrelated pages like /browse", () => {+ const state = createMockState();+ const props = createRouterProps("/browse/databases");++ expect(getIsCollectionPathVisible(state, props)).toBe(false);+ });+});diff --git a/frontend/src/metabase/urls/collections.ts b/frontend/src/metabase/urls/collections.tsindex 63bbd3098b4..7082a62cc03 100644--- a/frontend/src/metabase/urls/collections.ts+++ b/frontend/src/metabase/urls/collections.ts@@ -80,3 +80,10 @@ export function extractCollectionId(slug = ""): CollectionId | undefined {}return extractEntityId(slug);}++export function extractCollectionIdFromPath(+ path: string,+): CollectionId | undefined {+ const match = path.match(/^\/collection\/([^/]+)/);+ return match ? extractCollectionId(match[1]) : undefined;+}diff --git a/frontend/src/metabase/urls/urls.unit.spec.ts b/frontend/src/metabase/urls/urls.unit.spec.tsindex 89e63b6e11b..7af347669f6 100644--- a/frontend/src/metabase/urls/urls.unit.spec.ts+++ b/frontend/src/metabase/urls/urls.unit.spec.ts@@ -7,6 +7,7 @@ import {collection,dashboard,extractCollectionId,+ extractCollectionIdFromPath,extractEntityId,extractQueryParams,isCollectionPath,@@ -361,6 +362,52 @@ describe("urls", () => {});});+ describe("extractCollectionIdFromPath", () => {+ const testCases: {+ path: string;+ expected: CollectionId | undefined;+ }[] = [+ // Numeric collection IDs (with and without slug suffixes)+ { path: "/collection/1", expected: 1 },+ { path: "/collection/123", expected: 123 },+ { path: "/collection/1-stats", expected: 1 },+ { path: "/collection/123-stats-stats", expected: 123 },+ // Nested paths under a collection still resolve to the parent id+ { path: "/collection/1-stats/new", expected: 1 },+ { path: "/collection/1-stats/nested/url", expected: 1 },+ // Magic slugs preserved as-is+ { path: "/collection/root", expected: "root" },+ { path: "/collection/users", expected: "users" },+ { path: "/collection/tenant-specific", expected: "tenant-specific" },+ { path: "/collection/tenant-users", expected: "tenant-users" },++ // Anchored at start — `/collection/` must be the leading segment+ {+ path: "/admin/permissions/collections/1",+ expected: undefined,+ },+ { path: "foo/collection/1", expected: undefined },++ // Non-collection paths+ { path: "/dashboard/1", expected: undefined },+ { path: "/question/1-orders", expected: undefined },+ { path: "/browse/databases/1", expected: undefined },++ // Slug present but not parseable to a collection id+ { path: "/collection/no-id-here", expected: undefined },++ // Empty / malformed+ { path: "", expected: undefined },+ { path: "/collection/", expected: undefined },+ ];++ testCases.forEach(({ path, expected }) => {+ it(`returns ${JSON.stringify(expected)} for "${path}"`, () => {+ expect(extractCollectionIdFromPath(path)).toBe(expected);+ });+ });+ });+describe("slug edge-cases", () => {const testCases = [{