Skip to content

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

feat(sdk): use entity ids directly in collection picker and browser (#60562)

Phoomparin Manof4821a0935f3
diff --git a/e2e/test-component/scenarios/embedding-sdk/collection-browser.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/collection-browser.cy.spec.tsx
index b3f980170cb..e13fc6a4cdc 100644
--- a/e2e/test-component/scenarios/embedding-sdk/collection-browser.cy.spec.tsx
+++ b/e2e/test-component/scenarios/embedding-sdk/collection-browser.cy.spec.tsx
@@ -78,6 +78,19 @@ describe("scenarios > embedding-sdk > collection browser", () => {
mockAuthProviderAndJwtSignIn();
});
+ it("does not contain parent collection in breadcrumb (EMB-596)", () => {
+ cy.intercept("GET", "/api/collection/*").as("getCollection");
+
+ mountSdkContent(
+ <CollectionBrowser collectionId={SECOND_COLLECTION_ENTITY_ID} />,
+ );
+
+ getSdkRoot().within(() => {
+ cy.findByText("Second collection").should("exist");
+ cy.findByText("First collection").should("not.exist");
+ });
+ });
+
it("can change collection to a different entity id without crashing (metabase#57438)", () => {
const TestComponent = () => {
const [collectionId, setCollectionId] = useState<string | null>(
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/components/SdkSaveQuestionForm.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/components/SdkSaveQuestionForm.tsx
index b52d04d418d..c1abe7d2067 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/components/SdkSaveQuestionForm.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/components/SdkSaveQuestionForm.tsx
@@ -1,4 +1,3 @@
-import { useTranslatedCollectionId } from "embedding-sdk-bundle/hooks/private/use-translated-collection-id";
import {
SaveQuestionForm,
SaveQuestionTitle,
@@ -35,11 +34,7 @@ export const SdkSaveQuestionForm = ({ onCancel }: SaveQuestionFormProps) => {
const { question, originalQuestion, onSave, onCreate, targetCollection } =
useSdkQuestionContext();
- const { id, isLoading } = useTranslatedCollectionId({
- id: targetCollection,
- });
-
- if (!question || isLoading) {
+ if (!question) {
return null;
}
@@ -50,7 +45,7 @@ export const SdkSaveQuestionForm = ({ onCancel }: SaveQuestionFormProps) => {
onCreate={onCreate}
onSave={onSave}
multiStep={false}
- targetCollection={id}
+ targetCollection={targetCollection ?? undefined}
>
<Stack p="md">
<Title order={2}>
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
index e54b765cfb7..987f7bc6355 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
@@ -11,7 +11,6 @@ import {
} from "embedding-sdk-bundle/components/private/PublicComponentWrapper";
import { QuestionVisualization } from "embedding-sdk-bundle/components/private/SdkQuestion/components/Visualization";
import { useSdkBreadcrumbs } from "embedding-sdk-bundle/hooks/private/use-sdk-breadcrumb";
-import { useTranslatedCollectionId } from "embedding-sdk-bundle/hooks/private/use-translated-collection-id";
import { shouldRunCardQuery } from "embedding-sdk-bundle/lib/sdk-question";
import type { SdkQuestionTitleProps } from "embedding-sdk-bundle/types/question";
import { SaveQuestionModal } from "metabase/common/components/SaveQuestionModal";
@@ -259,11 +258,7 @@ const DefaultViewSaveModal = ({
targetCollection,
} = useSdkQuestionContext();
- const { id, isLoading } = useTranslatedCollectionId({
- id: targetCollection,
- });
-
- if (!isSaveEnabled || !isOpen || !question || isLoading) {
+ if (!isSaveEnabled || !isOpen || !question) {
return null;
}
@@ -279,7 +274,7 @@ const DefaultViewSaveModal = ({
await onSave(question);
close();
}}
- targetCollection={id}
+ targetCollection={targetCollection}
/>
);
};
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.tsx
index c680797129e..f8319c57823 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.tsx
@@ -6,9 +6,8 @@ import {
withPublicComponentWrapper,
} from "embedding-sdk-bundle/components/private/PublicComponentWrapper";
import { useSdkBreadcrumbs } from "embedding-sdk-bundle/hooks/private/use-sdk-breadcrumb";
-import { useTranslatedCollectionId } from "embedding-sdk-bundle/hooks/private/use-translated-collection-id";
+import { useSdkSelector } from "embedding-sdk-bundle/store";
import { getCollectionIdSlugFromReference } from "embedding-sdk-bundle/store/collections";
-import { useSdkSelector } from "embedding-sdk-bundle/store/use-sdk-selector";
import type {
MetabaseCollectionItem,
SdkCollectionId,
@@ -100,9 +99,7 @@ export const CollectionBrowserInner = ({
visibleColumns = COLLECTION_BROWSER_LIST_COLUMNS,
className,
style,
-}: Omit<CollectionBrowserProps, "collectionId"> & {
- collectionId: CollectionId;
-}) => {
+}: CollectionBrowserProps) => {
const baseCollectionId = useSdkSelector((state) =>
getCollectionIdSlugFromReference(state, collectionId),
);
@@ -191,19 +188,16 @@ const CollectionBrowserWrapper = ({
...restProps
}: CollectionBrowserProps) => {
const { isLocaleLoading } = useLocale();
- const { id, isLoading } = useTranslatedCollectionId({
- id: collectionId,
- });
- if (isLocaleLoading || isLoading) {
+ if (isLocaleLoading) {
return <SdkLoader />;
}
- if (!id) {
+ if (!collectionId) {
return <CollectionNotFoundError id={collectionId} />;
}
- return <CollectionBrowserInner collectionId={id} {...restProps} />;
+ return <CollectionBrowserInner collectionId={collectionId} {...restProps} />;
};
export const CollectionBrowser = withPublicComponentWrapper(
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/CreateDashboardModal/CreateDashboardModal.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/CreateDashboardModal/CreateDashboardModal.tsx
index 733d65c003b..c92794c7c8e 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/CreateDashboardModal/CreateDashboardModal.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/CreateDashboardModal/CreateDashboardModal.tsx
@@ -1,9 +1,12 @@
import { withPublicComponentWrapper } from "embedding-sdk-bundle/components/private/PublicComponentWrapper";
-import { useTranslatedCollectionId } from "embedding-sdk-bundle/hooks/private/use-translated-collection-id";
-import type { SdkCollectionId } from "embedding-sdk-bundle/types/collection";
-import type { MetabaseDashboard } from "embedding-sdk-bundle/types/dashboard";
+import {
+ getCollectionIdSlugFromReference,
+ getCollectionIdValueFromReference,
+} from "embedding-sdk-bundle/store/collections";
+import type { MetabaseDashboard, SdkCollectionId } from "embedding-sdk-package";
import { useCollectionQuery, useLocale } from "metabase/common/hooks";
import { CreateDashboardModal as CreateDashboardModalCore } from "metabase/dashboard/containers/CreateDashboardModal";
+import { useSelector } from "metabase/lib/redux";
/**
* @expand
@@ -38,27 +41,29 @@ const CreateDashboardModalInner = ({
onClose,
}: CreateDashboardModalProps) => {
const { isLocaleLoading } = useLocale();
- const { id, isLoading: isTranslateCollectionLoading } =
- useTranslatedCollectionId({
- id: initialCollectionId,
- });
+
+ const collectionId = useSelector((state) =>
+ getCollectionIdValueFromReference(state, initialCollectionId),
+ );
+
+ const collectionIdSlug = useSelector((state) =>
+ getCollectionIdSlugFromReference(state, initialCollectionId),
+ );
const { isLoading: isCollectionQueryLoading } = useCollectionQuery({
- id,
+ id: collectionIdSlug,
});
- const isLoading = isTranslateCollectionLoading && isCollectionQueryLoading;
-
- if (isLocaleLoading || isLoading) {
+ if (isLocaleLoading || isCollectionQueryLoading) {
return null;
}
return (
<CreateDashboardModalCore
- opened={!isLoading && isOpen}
+ opened={!isCollectionQueryLoading && isOpen}
onCreate={onCreate}
onClose={() => onClose?.()}
- collectionId={id}
+ collectionId={collectionId}
/>
);
};
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/CreateDashboardModal/CreateDashboardModal.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/CreateDashboardModal/CreateDashboardModal.unit.spec.tsx
index 4e892aa4036..e7687e7f26a 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/CreateDashboardModal/CreateDashboardModal.unit.spec.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/CreateDashboardModal/CreateDashboardModal.unit.spec.tsx
@@ -59,7 +59,9 @@ describe("CreateDashboardModal", () => {
it("should render", async () => {
setup();
- expect(screen.getByText("New dashboard")).toBeInTheDocument();
+ await waitFor(() => {
+ expect(screen.getByText("New dashboard")).toBeInTheDocument();
+ });
expect(screen.getByText("Description")).toBeInTheDocument();
@@ -82,6 +84,10 @@ describe("CreateDashboardModal", () => {
},
});
+ await waitFor(() => {
+ expect(screen.getByText("New dashboard")).toBeInTheDocument();
+ });
+
await userEvent.type(
screen.getByPlaceholderText("What is the name of your dashboard?"),
"My awesome dashboard title",
@@ -119,7 +125,7 @@ describe("CreateDashboardModal", () => {
expect(onCreate).toHaveBeenLastCalledWith(mockResponseDashboard);
});
- it('should support "isOpen" prop', () => {
+ it('should support "isOpen" prop', async () => {
const { rerender } = setup({
props: {
isOpen: false,
@@ -130,7 +136,9 @@ describe("CreateDashboardModal", () => {
rerender(<CreateDashboardModal isOpen onCreate={jest.fn()} />);
- expect(screen.getByText("New dashboard")).toBeInTheDocument();
+ await waitFor(() => {
+ expect(screen.getByText("New dashboard")).toBeInTheDocument();
+ });
});
});
@@ -147,6 +155,9 @@ function setup(
setupCollectionByIdEndpoint({ collections: COLLECTIONS });
+ // Mock the "personal" collection endpoint since the component now passes string IDs directly
+ fetchMock.get("path:/api/collection/personal", PERSONAL_COLLECTION);
+
return renderWithSDKProviders(
<CreateDashboardModal onCreate={jest.fn()} {...props} />,
{
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/lib/create-dashboard.ts b/enterprise/frontend/src/embedding-sdk-bundle/lib/create-dashboard.ts
index 07fda2d6e57..e22cbab136f 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/lib/create-dashboard.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/lib/create-dashboard.ts
@@ -1,4 +1,4 @@
-import { getCollectionNumericIdFromReference } from "embedding-sdk-bundle/store/collections";
+import { getCollectionIdValueFromReference } from "embedding-sdk-bundle/store/collections";
import type { SdkStore } from "embedding-sdk-bundle/store/types";
import type {
CreateDashboardValues,
@@ -12,7 +12,7 @@ export const createDashboard =
collectionId = "personal",
...rest
}: CreateDashboardValues): Promise<MetabaseDashboard> => {
- const realCollectionId = getCollectionNumericIdFromReference(
+ const realCollectionId = getCollectionIdValueFromReference(
reduxStore.getState(),
collectionId,
);
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/store/collections.ts b/enterprise/frontend/src/embedding-sdk-bundle/store/collections.ts
index e572943cf63..e9bd90b0d07 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/store/collections.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/store/collections.ts
@@ -7,10 +7,10 @@ import type { CollectionId, RegularCollectionId } from "metabase-types/api";
import type { SdkCollectionId } from "../types/collection";
/**
- * Converts "personal" and "root" to the _numeric_ ids accepted by the api
- * For the root collection id, the API expects null
+ * Converts "personal" and "root" to the ids accepted by the api
+ * For the root collection id, the API expects null.
*/
-export const getCollectionNumericIdFromReference = createSelector(
+export const getCollectionIdValueFromReference = createSelector(
[
getUserPersonalCollectionId,
(_, collectionReference: SdkCollectionId) => collectionReference,
@@ -19,10 +19,10 @@ export const getCollectionNumericIdFromReference = createSelector(
return match(collectionReference)
.with("personal", () => personalCollectionId as RegularCollectionId)
.with("root", () => null)
- .with(P.number, () => collectionReference as RegularCollectionId)
+ .with(P.union(P.number, P.string), (id) => id)
.otherwise(() => {
throw new Error(
- "Invalid collection id, expected `number | 'root' | 'personal'`",
+ "Invalid collection id, expected `number | string | 'root' | 'personal'`",
);
});
},
@@ -43,10 +43,10 @@ export const getCollectionIdSlugFromReference = createSelector(
return match(collectionReference)
.with("personal", () => personalCollectionId as RegularCollectionId)
.with("root", () => "root" as const)
- .with(P.number, () => collectionReference as RegularCollectionId)
+ .with(P.union(P.number, P.string), (id) => id)
.otherwise(() => {
throw new Error(
- "Invalid collection id, expected `number | 'root' | 'personal'`",
+ "Invalid collection id, expected `number | string | 'root' | 'personal'`",
);
});
},
diff --git a/enterprise/frontend/src/metabase-enterprise/clean_up/analytics.ts b/enterprise/frontend/src/metabase-enterprise/clean_up/analytics.ts
index 9906c1d6757..34542f18d92 100644
--- a/enterprise/frontend/src/metabase-enterprise/clean_up/analytics.ts
+++ b/enterprise/frontend/src/metabase-enterprise/clean_up/analytics.ts
@@ -1,12 +1,11 @@
import { trackSchemaEvent } from "metabase/lib/analytics";
-import type { RegularCollectionId } from "metabase-types/api";
export const trackStaleItemsArchived = ({
collection_id,
total_items_archived,
cutoff_date,
}: {
- collection_id: RegularCollectionId | null;
+ collection_id: number | null;
total_items_archived: number;
cutoff_date: string;
}) => {
diff --git a/frontend/src/metabase-types/api/collection.ts b/frontend/src/metabase-types/api/collection.ts
index 6e73924caef..468ca7a3922 100644
--- a/frontend/src/metabase-types/api/collection.ts
+++ b/frontend/src/metabase-types/api/collection.ts
@@ -16,7 +16,8 @@ import type { SortingOptions } from "./sorting";
import type { TableId } from "./table";
import type { UserId } from "./user";
-export type RegularCollectionId = number;
+// Collection ID can be either a numeric or entity id
+export type RegularCollectionId = number | string;
export type CollectionId =
| RegularCollectionId
diff --git a/frontend/src/metabase/collections/utils.unit.spec.ts b/frontend/src/metabase/collections/utils.unit.spec.ts
index 796b99bdba1..4c6422f8b8d 100644
--- a/frontend/src/metabase/collections/utils.unit.spec.ts
+++ b/frontend/src/metabase/collections/utils.unit.spec.ts
@@ -129,9 +129,7 @@ describe("Collections > utils", () => {
it("returns false if the collection is not the root collection", () => {
expect(isRootCollection(createMockCollection({ id: 1 }))).toBe(false);
- /* @ts-expect-error unclear why ids are sometimes strings, but they are */
expect(isRootCollection(createMockCollection({ id: "1" }))).toBe(false);
- /* @ts-expect-error unclear why ids are sometimes strings, but they are */
expect(isRootCollection(createMockCollection({ id: "foobar" }))).toBe(
false,
);
diff --git a/frontend/src/metabase/common/components/SaveQuestionForm/context.tsx b/frontend/src/metabase/common/components/SaveQuestionForm/context.tsx
index 065394a00f8..33b53b6d205 100644
--- a/frontend/src/metabase/common/components/SaveQuestionForm/context.tsx
+++ b/frontend/src/metabase/common/components/SaveQuestionForm/context.tsx
@@ -13,7 +13,6 @@ import { useListRecentsQuery } from "metabase/api";
import { useGetDefaultCollectionId } from "metabase/collections/hooks";
import { isInstanceAnalyticsCollection } from "metabase/collections/utils";
import { FormProvider } from "metabase/forms";
-import { useValidatedEntityId } from "metabase/lib/entity-id/hooks/use-validated-entity-id";
import { useSelector } from "metabase/lib/redux";
import { isNotNull } from "metabase/lib/types";
import type Question from "metabase-lib/v1/Question";
@@ -72,16 +71,11 @@ export const SaveQuestionProvider = ({
);
const currentUser = useSelector(getCurrentUser);
- const { id: collectionId } = useValidatedEntityId({
- type: "collection",
- id: userTargetCollection,
- });
const targetCollection =
- collectionId ||
- (currentUser && userTargetCollection === "personal"
+ userTargetCollection === "personal" && currentUser
? currentUser.personal_collection_id
- : userTargetCollection);
+ : userTargetCollection;
const [hasLoadedRecentItems, setHasLoadedRecentItems] = useState(false);
const { data: recentItems, isLoading } = useListRecentsQuery(
diff --git a/frontend/src/metabase/nav/components/CollectionBreadcrumbs/utils.ts b/frontend/src/metabase/nav/components/CollectionBreadcrumbs/utils.ts
index e7311961a0c..4ed802418a8 100644
--- a/frontend/src/metabase/nav/components/CollectionBreadcrumbs/utils.ts
+++ b/frontend/src/metabase/nav/components/CollectionBreadcrumbs/utils.ts
@@ -10,7 +10,12 @@ export const getCollectionList = ({
baseCollectionId = null,
collection,
}: GetCollectionListProps) => {
- if (baseCollectionId && collection.id === baseCollectionId) {
+ // baseCollectionId can be either a numeric or entity id
+ if (
+ baseCollectionId &&
+ (collection.id === baseCollectionId ||
+ collection.entity_id === baseCollectionId)
+ ) {
return [];
}