Skip to content

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

Show collection picker when saving new question in metabase-browser (#74570)

Sébastienb5efc56ecdc9
diff --git a/e2e/test/scenarios/embedding/sdk-iframe-embedding/view-and-curate-content.cy.spec.ts b/e2e/test/scenarios/embedding/sdk-iframe-embedding/view-and-curate-content.cy.spec.ts
index 8ba4c73389e..4a4dc54dec2 100644
--- a/e2e/test/scenarios/embedding/sdk-iframe-embedding/view-and-curate-content.cy.spec.ts
+++ b/e2e/test/scenarios/embedding/sdk-iframe-embedding/view-and-curate-content.cy.spec.ts
@@ -174,7 +174,7 @@ describe("scenarios > embedding > sdk iframe embedding > view and curate content
.should("be.visible");
});
- it("should show Save button and save modal without entity picker when creating a new question", () => {
+ it("should show Save button and save modal with entity picker preselecting the current collection when creating a new question (EMB-1609)", () => {
setupEmbed(
'<metabase-browser initial-collection="root" read-only="false" />',
);
@@ -190,11 +190,14 @@ describe("scenarios > embedding > sdk iframe embedding > view and curate content
.should("be.visible")
.click();
- cy.log("should show save modal without entity picker");
+ cy.log(
+ "save modal should show the collection picker pre-selected to the current collection",
+ );
H.getSimpleEmbedIframeContent().within(() => {
cy.findByRole("dialog").within(() => {
cy.findByText("Save new question").should("be.visible");
- cy.findByText("Where do you want to save this?").should("not.exist");
+ cy.findByText("Where do you want to save this?").should("be.visible");
+ cy.findByText("Our analytics").should("be.visible");
});
});
});
diff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/SdkQuestionProvider.tsx b/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/SdkQuestionProvider.tsx
index de09ec5e3c0..92bd1c2ca45 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/SdkQuestionProvider.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/SdkQuestionProvider.tsx
@@ -74,6 +74,7 @@ export const SdkQuestionProvider = ({
entityTypes,
dataPicker,
targetCollection,
+ initialCollection,
initialSqlParameters,
sqlParameters,
onSqlParametersChange,
@@ -317,6 +318,7 @@ export const SdkQuestionProvider = ({
onCreate: handleCreate,
isSaveEnabled,
targetCollection,
+ initialCollection,
withDownloads,
withAlerts,
onRun,
diff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/types.ts b/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/types.ts
index 7fc7e198e13..081c465ec47 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/types.ts
+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/types.ts
@@ -92,6 +92,11 @@ type SdkQuestionConfig = {
*/
targetCollection?: SdkCollectionId;
+ /**
+ * The collection to preselect in the save modal's collection picker. Unlike `targetCollection`, the picker remains visible and the user can choose a different collection. Ignored when `targetCollection` is set.
+ */
+ initialCollection?: SdkCollectionId;
+
/**
* Additional mapper function to override or add drill-down menu
*/
@@ -197,6 +202,7 @@ export type SdkQuestionContextType = Omit<
| "onNavigateBack"
| "isSaveEnabled"
| "targetCollection"
+ | "initialCollection"
| "withDownloads"
| "withAlerts"
| "backToDashboard"
diff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx b/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
index 68312d0637d..e0000347402 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
@@ -308,6 +308,7 @@ const DefaultViewSaveModal = ({
onSave,
isSaveEnabled,
targetCollection,
+ initialCollection,
} = useSdkQuestionContext();
if (!isSaveEnabled || !isOpen || !question) {
@@ -327,6 +328,7 @@ const DefaultViewSaveModal = ({
close();
}}
targetCollection={targetCollection}
+ initialCollectionId={initialCollection}
/>
);
};
diff --git a/frontend/src/embedding-sdk-bundle/components/public/InteractiveQuestion/InteractiveQuestion.schema.ts b/frontend/src/embedding-sdk-bundle/components/public/InteractiveQuestion/InteractiveQuestion.schema.ts
index 26f81f5c957..70f6251750a 100644
--- a/frontend/src/embedding-sdk-bundle/components/public/InteractiveQuestion/InteractiveQuestion.schema.ts
+++ b/frontend/src/embedding-sdk-bundle/components/public/InteractiveQuestion/InteractiveQuestion.schema.ts
@@ -46,6 +46,7 @@ const propsSchema: Yup.SchemaOf<InteractiveQuestionInternalProps> = Yup.object({
query: Yup.mixed().optional(),
style: Yup.mixed().optional(),
targetCollection: Yup.mixed().optional(),
+ initialCollection: Yup.mixed().optional(),
targetDashboardId: Yup.mixed().optional(),
title: Yup.mixed().optional(),
width: Yup.mixed().optional(),
diff --git a/frontend/src/embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion.tsx b/frontend/src/embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion.tsx
index 36da1d28a0d..3b88cf1606b 100644
--- a/frontend/src/embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion.tsx
@@ -60,6 +60,7 @@ export type BaseSdkQuestionProps = SdkQuestionIdProps & {
| "withDownloads"
| "withAlerts"
| "targetCollection"
+ | "initialCollection"
| "onRun"
>;
@@ -135,6 +136,7 @@ export const _SdkQuestion = ({
entityTypes,
dataPicker,
targetCollection,
+ initialCollection,
initialSqlParameters,
sqlParameters,
onSqlParametersChange,
@@ -166,6 +168,7 @@ export const _SdkQuestion = ({
withEditorButton,
isSaveEnabled,
targetCollection,
+ initialCollection,
entityTypes,
onBeforeSave,
onSave,
@@ -196,6 +199,7 @@ export const _SdkQuestion = ({
entityTypes={entityTypes}
dataPicker={dataPicker}
targetCollection={targetCollection}
+ initialCollection={initialCollection}
initialSqlParameters={initialSqlParameters}
sqlParameters={sqlParameters}
onSqlParametersChange={onSqlParametersChange}
diff --git a/frontend/src/metabase/common/components/SaveQuestionForm/context.tsx b/frontend/src/metabase/common/components/SaveQuestionForm/context.tsx
index ade8d336ccc..746071243ee 100644
--- a/frontend/src/metabase/common/components/SaveQuestionForm/context.tsx
+++ b/frontend/src/metabase/common/components/SaveQuestionForm/context.tsx
@@ -70,6 +70,7 @@ export const SaveQuestionProvider = ({
onSave,
multiStep = false,
targetCollection: userTargetCollection,
+ initialCollectionId: userInitialCollectionId,
children,
}: PropsWithChildren<SaveQuestionProps>) => {
const [originalQuestion] = useState(latestOriginalQuestion); // originalQuestion from props changes during saving
@@ -121,13 +122,20 @@ export const SaveQuestionProvider = ({
const initialDashboardId =
question.type() === "question" &&
!isAnalytics &&
- // `userTargetCollection` comes from the `targetCollection` sdk prop and should take precedence over the recent dashboards
+ // `userTargetCollection` and `userInitialCollectionId` come from the
+ // `targetCollection` / `initialCollection` sdk props and should take
+ // precedence over the recent dashboards.
userTargetCollection === undefined &&
+ userInitialCollectionId === undefined &&
lastSelectedDashboard?.can_write
? lastSelectedDashboard?.id
: undefined;
+ // When a caller passes `initialCollectionId` explicitly, it takes precedence
+ // over the recent-items logic. Used by the SDK so the picker opens on the
+ // collection the user is browsing.
const initialCollectionId =
+ userInitialCollectionId ??
(!isAnalytics
? lastSelectedDashboard?.parent_collection.id
: defaultCollectionId) ??
diff --git a/frontend/src/metabase/common/components/SaveQuestionForm/context.unit.spec.tsx b/frontend/src/metabase/common/components/SaveQuestionForm/context.unit.spec.tsx
index 98d750285cf..53b43b88c20 100644
--- a/frontend/src/metabase/common/components/SaveQuestionForm/context.unit.spec.tsx
+++ b/frontend/src/metabase/common/components/SaveQuestionForm/context.unit.spec.tsx
@@ -46,6 +46,7 @@ const TestComponent = () => {
interface setupProps {
question?: Question;
originalQuestion?: Question | null;
+ initialCollectionId?: number | string | null;
}
const setup = ({
@@ -57,6 +58,7 @@ const setup = ({
}),
),
originalQuestion = null,
+ initialCollectionId,
}: setupProps = {}) => {
const onCreate = jest.fn();
const onSave = jest.fn();
@@ -81,6 +83,7 @@ const setup = ({
originalQuestion={originalQuestion}
onCreate={onCreate}
onSave={onSave}
+ initialCollectionId={initialCollectionId}
>
<TestComponent />
</SaveQuestionProvider>,
@@ -140,6 +143,38 @@ describe("SaveQuestionContext", () => {
expect(screen.queryByTestId("dashboardId")).not.toBeInTheDocument();
});
+ it("should use the initialCollectionId prop over recent selections (EMB-1609)", async () => {
+ setupRecentViewsAndSelectionsEndpoints(
+ [createMockRecentCollectionItem({ model: "collection", id: 10 })],
+ ["selections"],
+ );
+
+ setup({ initialCollectionId: 42 });
+
+ await waitFor(async () =>
+ expect(await screen.findByTestId("collectionId")).toHaveTextContent(
+ "42",
+ ),
+ );
+ expect(screen.queryByTestId("dashboardId")).not.toBeInTheDocument();
+ });
+
+ it("should not suggest a recent dashboard when initialCollectionId is set (EMB-1609)", async () => {
+ setupRecentViewsAndSelectionsEndpoints(
+ [createMockRecentCollectionItem({ model: "dashboard", id: 10 })],
+ ["selections"],
+ );
+
+ setup({ initialCollectionId: 42 });
+
+ await waitFor(async () =>
+ expect(await screen.findByTestId("collectionId")).toHaveTextContent(
+ "42",
+ ),
+ );
+ expect(screen.queryByTestId("dashboardId")).not.toBeInTheDocument();
+ });
+
it("should require saving to a specific dashboard if the question has a dashboard id already", async () => {
setupRecentViewsAndSelectionsEndpoints(
[createMockRecentCollectionItem({ model: "dashboard", id: 10 })],
diff --git a/frontend/src/metabase/embedding/embedding-iframe-sdk/components/MetabaseBrowser.tsx b/frontend/src/metabase/embedding/embedding-iframe-sdk/components/MetabaseBrowser.tsx
index 70cc776d9d4..b4d3d86c8cd 100644
--- a/frontend/src/metabase/embedding/embedding-iframe-sdk/components/MetabaseBrowser.tsx
+++ b/frontend/src/metabase/embedding/embedding-iframe-sdk/components/MetabaseBrowser.tsx
@@ -92,7 +92,7 @@ export function MetabaseBrowser({ settings }: MetabaseBrowserProps) {
withDownloads
isSaveEnabled={!isReadOnly}
entityTypes={settings.dataPickerEntityTypes}
- targetCollection={targetCollection}
+ initialCollection={targetCollection}
/>
</Box>
))
diff --git a/frontend/src/metabase/query_builder/components/QueryModals/QueryModals.tsx b/frontend/src/metabase/query_builder/components/QueryModals/QueryModals.tsx
index 5b429ca6204..1be61f2d15a 100644
--- a/frontend/src/metabase/query_builder/components/QueryModals/QueryModals.tsx
+++ b/frontend/src/metabase/query_builder/components/QueryModals/QueryModals.tsx
@@ -193,7 +193,6 @@ export function QueryModals({
<SaveQuestionModal
question={question}
originalQuestion={originalQuestion}
- initialCollectionId={initialCollectionId}
onSave={handleSaveAndClose}
onCreate={handleSaveModalCreate}
onClose={onCloseModal}
@@ -222,7 +221,6 @@ export function QueryModals({
onClose={onCloseModal}
opened={true}
multiStep
- initialCollectionId={initialCollectionId}
/>
);
case MODAL_TYPES.MOVE: