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
feat(sdk): resolve tenant collection in collection browser (#67050)
Phoomparin Manoc5117a19b21a
diff --git a/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.tsx b/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.tsxindex 6b93561401d..5c7ba646155 100644--- a/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.tsx+++ b/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.tsx@@ -65,7 +65,7 @@ const ENTITY_NAME_MAP: Partial<*/export type CollectionBrowserProps = {/**- * The numerical ID of the collection, "personal" for the user's personal collection, or "root" for the root collection. You can find this ID in the URL when accessing a collection in your Metabase instance. For example, the collection ID in `http://localhost:3000/collection/1-my-collection` would be `1`. Defaults to "personal"+ * The numerical ID of the collection, "personal" for the user's personal collection, "tenant" for the user's tenant collection, or "root" for the root collection. You can find this ID in the URL when accessing a collection in your Metabase instance. For example, the collection ID in `http://localhost:3000/collection/1-my-collection` would be `1`. Defaults to "personal"*/collectionId?: SdkCollectionId;diff --git a/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.unit.spec.tsx b/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.unit.spec.tsxindex f5d373742f2..864487529e3 100644--- a/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.unit.spec.tsx+++ b/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.unit.spec.tsx@@ -15,9 +15,11 @@ import { createMockSdkConfig } from "embedding-sdk-bundle/test/mocks/config";import { setupSdkState } from "embedding-sdk-bundle/test/server-mocks/sdk-init";import { useLocale } from "metabase/common/hooks/use-locale";import { ROOT_COLLECTION } from "metabase/entities/collections";+import type { Collection, CollectionItem, User } from "metabase-types/api";import {createMockCollection,createMockCollectionItem,+ createMockUser,} from "metabase-types/api/mocks";jest.mock("metabase/common/hooks/use-locale", () => ({@@ -151,29 +153,72 @@ describe("CollectionBrowser", () => {expect(columnTexts).toContain("Description");});++ it("should resolve collectionId=tenant to user's tenant collection", async () => {+ const tenantCollection = createMockCollection({+ id: 999,+ name: "Tenant Collection: Acme Inc",+ archived: false,+ can_write: true,+ description: null,+ location: "/",+ });++ const dashboardItem = createMockCollectionItem({+ id: 4,+ name: "Acme Dashboard",+ model: "dashboard",+ });++ await setup({+ props: { collectionId: "tenant" },+ collections: [tenantCollection],+ rootCollection: tenantCollection,+ currentUser: createMockUser({+ tenant_collection_id: tenantCollection.id,+ }),+ collectionItems: [dashboardItem],+ });++ expect(await screen.findByText(tenantCollection.name)).toBeInTheDocument();+ expect(await screen.findByText(dashboardItem.name)).toBeInTheDocument();+ });});async function setup({props,+ collections = TEST_COLLECTIONS,+ rootCollection = ROOT_TEST_COLLECTION,+ currentUser,+ collectionItems = [+ createMockCollectionItem({ id: 2, model: "dashboard" }),+ createMockCollectionItem({ id: 3, model: "card" }),+ ],}: {props?: Partial<ComponentProps<typeof CollectionBrowserInner>>;+ collections?: Collection[];+ rootCollection?: Collection;+ currentUser?: User;+ collectionItems?: CollectionItem[];} = {}) {useLocaleMock.mockReturnValue({ isLocaleLoading: false });setupCollectionsEndpoints({- collections: TEST_COLLECTIONS,- rootCollection: ROOT_TEST_COLLECTION,+ collections,+ rootCollection,});+ // Mock individual collection endpoint if it has a numeric ID+ if (typeof rootCollection.id === "number") {+ fetchMock.get(`path:/api/collection/${rootCollection.id}`, rootCollection);+ }+setupCollectionItemsEndpoint({- collection: ROOT_TEST_COLLECTION,- collectionItems: [- createMockCollectionItem({ id: 2, model: "dashboard" }),- createMockCollectionItem({ id: 3, model: "card" }),- ],+ collection: rootCollection,+ collectionItems,});- const state = setupSdkState();+ const state = setupSdkState(currentUser ? { currentUser } : {});renderWithSDKProviders(<CollectionBrowserInner collectionId="root" {...props} />,@@ -190,7 +235,7 @@ async function setup({await waitFor(() => {expect(fetchMock.callHistory.calls(- `path:/api/collection/${ROOT_TEST_COLLECTION.id}/items`,+ `path:/api/collection/${rootCollection.id}/items`,),).toHaveLength(1);});diff --git a/frontend/src/embedding-sdk-bundle/store/collections.ts b/frontend/src/embedding-sdk-bundle/store/collections.tsindex e9bd90b0d07..16f792e6770 100644--- a/frontend/src/embedding-sdk-bundle/store/collections.ts+++ b/frontend/src/embedding-sdk-bundle/store/collections.ts@@ -1,28 +1,45 @@import { createSelector } from "@reduxjs/toolkit";import { P, match } from "ts-pattern";-import { getUserPersonalCollectionId } from "metabase/selectors/user";+import {+ getUserPersonalCollectionId,+ getUserTenantCollectionId,+} from "metabase/selectors/user";import type { CollectionId, RegularCollectionId } from "metabase-types/api";import type { SdkCollectionId } from "../types/collection";/**- * Converts "personal" and "root" to the ids accepted by the api+ * Converts "personal", "tenant", and "root" to the ids accepted by the api* For the root collection id, the API expects null.*/export const getCollectionIdValueFromReference = createSelector([getUserPersonalCollectionId,+ getUserTenantCollectionId,(_, collectionReference: SdkCollectionId) => collectionReference,],- (personalCollectionId, collectionReference): CollectionId | null => {+ (+ personalCollectionId,+ tenantCollectionId,+ collectionReference,+ ): CollectionId | null => {return match(collectionReference).with("personal", () => personalCollectionId as RegularCollectionId)+ .with("tenant", () => {+ if (!tenantCollectionId) {+ throw new Error(+ "You must be a tenant member to access the tenant collection.",+ );+ }++ return tenantCollectionId as RegularCollectionId;+ }).with("root", () => null).with(P.union(P.number, P.string), (id) => id).otherwise(() => {throw new Error(- "Invalid collection id, expected `number | string | 'root' | 'personal'`",+ "Invalid collection id, expected `number | string | 'root' | 'personal' | 'tenant'`",);});},@@ -37,16 +54,30 @@ export const getCollectionIdValueFromReference = createSelector(export const getCollectionIdSlugFromReference = createSelector([getUserPersonalCollectionId,+ getUserTenantCollectionId,(_, collectionReference: SdkCollectionId) => collectionReference,],- (personalCollectionId, collectionReference): CollectionId => {+ (+ personalCollectionId,+ tenantCollectionId,+ collectionReference,+ ): CollectionId => {return match(collectionReference).with("personal", () => personalCollectionId as RegularCollectionId)+ .with("tenant", () => {+ if (!tenantCollectionId) {+ throw new Error(+ "You must be a tenant member to access the tenant collection.",+ );+ }++ return tenantCollectionId as RegularCollectionId;+ }).with("root", () => "root" as const).with(P.union(P.number, P.string), (id) => id).otherwise(() => {throw new Error(- "Invalid collection id, expected `number | string | 'root' | 'personal'`",+ "Invalid collection id, expected `number | string | 'root' | 'personal' | 'tenant'`",);});},diff --git a/frontend/src/embedding-sdk-bundle/types/collection.ts b/frontend/src/embedding-sdk-bundle/types/collection.tsindex 51fd51e53f1..d78c97eb84c 100644--- a/frontend/src/embedding-sdk-bundle/types/collection.ts+++ b/frontend/src/embedding-sdk-bundle/types/collection.ts@@ -5,7 +5,12 @@ import type { SdkEntityId } from "./entity";// "CollectionId" from core app also includes "root" | "users" and "trash", we don't want to include those// in public apis of the sdk, as we don't support them-export type SdkCollectionId = number | "personal" | "root" | SdkEntityId;+export type SdkCollectionId =+ | number+ | "personal"+ | "root"+ | "tenant"+ | SdkEntityId;/*** The Collection entitydiff --git a/frontend/src/metabase/selectors/user.ts b/frontend/src/metabase/selectors/user.tsindex 5e91bfc0065..39d025f06ab 100644--- a/frontend/src/metabase/selectors/user.ts+++ b/frontend/src/metabase/selectors/user.ts@@ -40,6 +40,11 @@ export const getUserPersonalCollectionId = createSelector((user) => user?.personal_collection_id,);+export const getUserTenantCollectionId = createSelector(+ [getUser],+ (user) => user?.tenant_collection_id,+);+export const canUserCreateQueries = createSelector([getUser],(user) => user?.permissions?.can_create_queries ?? false,