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-embed): adds onVisualizationChange to InteractiveDashboard and InteractiveQuestion (#63969)

Sébastienfd94a0b0cb09
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkAdHocQuestion/SdkAdHocQuestion.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkAdHocQuestion/SdkAdHocQuestion.tsx
index 7676c18e0ea..30d236c9d02 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkAdHocQuestion/SdkAdHocQuestion.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkAdHocQuestion/SdkAdHocQuestion.tsx
@@ -31,6 +31,7 @@ export const SdkAdHocQuestion = ({
withDownloads = false,
initialSqlParameters,
onNavigateBack,
+ onVisualizationChange,
}: SdkAdHocQuestionProps &
Pick<
SdkQuestionProps,
@@ -50,6 +51,7 @@ export const SdkAdHocQuestion = ({
| "withChartTypeSelector"
| "withDownloads"
| "initialSqlParameters"
+ | "onVisualizationChange"
>) => {
const { location, params } = useMemo(
() => getQuestionParameters(questionPath),
@@ -87,6 +89,7 @@ export const SdkAdHocQuestion = ({
title={title}
withResetButton={withResetButton}
withChartTypeSelector={withChartTypeSelector}
+ onVisualizationChange={onVisualizationChange}
>
{children}
</SdkQuestion>
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/components/Visualization.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/components/Visualization.tsx
index 535135bbf1b..df0815f9cba 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/components/Visualization.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/components/Visualization.tsx
@@ -1,4 +1,5 @@
import cx from "classnames";
+import { useEffect, useMemo } from "react";
import { t } from "ttag";
import type { FlexibleSizeProps } from "embedding-sdk-bundle/components/private/FlexibleSizeComponent";
@@ -13,6 +14,7 @@ import { useLocale } from "metabase/common/hooks/use-locale";
import CS from "metabase/css/core/index.css";
import QueryVisualization from "metabase/query_builder/components/QueryVisualization";
import type Question from "metabase-lib/v1/Question";
+import type { CardDisplayType } from "metabase-types/api";
import { useSdkQuestionContext } from "../context";
@@ -47,8 +49,17 @@ export const QuestionVisualization = ({
onNavigateBack,
updateQuestion,
originalId,
+ onVisualizationChange,
} = useSdkQuestionContext();
+ const display = useMemo(() => question?.display(), [question]);
+
+ useEffect(() => {
+ if (display && onVisualizationChange) {
+ onVisualizationChange(display as CardDisplayType);
+ }
+ }, [display, onVisualizationChange]);
+
// When visualizing a question for the first time, there is no query result yet.
const isQueryResultLoading =
question && shouldRunCardQuery(question) && !queryResults;
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/SdkQuestionProvider.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/SdkQuestionProvider.tsx
index 13e865f7e13..c017b6d661c 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/SdkQuestionProvider.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/SdkQuestionProvider.tsx
@@ -50,6 +50,7 @@ export const SdkQuestionProvider = ({
backToDashboard,
getClickActionMode: userGetClickActionMode,
navigateToNewCard: userNavigateToNewCard,
+ onVisualizationChange,
}: SdkQuestionProviderProps) => {
const handleCreateQuestion = useCreateQuestion();
const handleSaveQuestion = useSaveQuestion();
@@ -159,6 +160,7 @@ export const SdkQuestionProvider = ({
withDownloads,
onRun,
backToDashboard,
+ onVisualizationChange,
};
useEffect(() => {
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/types.ts b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/types.ts
index 5ed8b7eb709..37148c27357 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/types.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestion/context/types.ts
@@ -15,7 +15,7 @@ import type {
QueryClickActionsMode,
} from "metabase/visualizations/types";
import type Question from "metabase-lib/v1/Question";
-import type { DashboardId } from "metabase-types/api";
+import type { CardDisplayType, DashboardId } from "metabase-types/api";
import type { EmbeddingEntityType } from "metabase-types/store/embedding-data-picker";
type SdkQuestionConfig = {
@@ -86,6 +86,13 @@ type SdkQuestionConfig = {
model: "dashboard";
name: string;
};
+
+ /**
+ * A callback function that triggers when the visualization type changes.
+ *
+ * @param display the new display type
+ */
+ onVisualizationChange?: (display: CardDisplayType) => void;
};
export type QuestionMockLocationParameters = {
@@ -121,6 +128,7 @@ export type SdkQuestionContextType = Omit<
| "targetCollection"
| "withDownloads"
| "backToDashboard"
+ | "onVisualizationChange"
> & {
plugins: SdkQuestionConfig["componentPlugins"] | null;
mode: QueryClickActionsMode | Mode | null | undefined;
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/InteractiveQuestion/InteractiveQuestion.schema.ts b/enterprise/frontend/src/embedding-sdk-bundle/components/public/InteractiveQuestion/InteractiveQuestion.schema.ts
index 2009ebce698..c85324b39ca 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/InteractiveQuestion/InteractiveQuestion.schema.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/InteractiveQuestion/InteractiveQuestion.schema.ts
@@ -38,6 +38,7 @@ const propsSchema: Yup.SchemaOf<InteractiveQuestionProps> = Yup.object({
withChartTypeSelector: Yup.mixed().optional(),
withDownloads: Yup.mixed().optional(),
withResetButton: Yup.mixed().optional(),
+ onVisualizationChange: Yup.mixed().optional(),
}).noUnknown();
export const interactiveQuestionSchema: FunctionSchema = {
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion.tsx
index 0052c3ee4ee..104db415ed7 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion.tsx
@@ -139,6 +139,7 @@ export const _SdkQuestion = ({
title,
withResetButton = true,
withChartTypeSelector = true,
+ onVisualizationChange,
}: SdkQuestionProps): JSX.Element | null => (
<SdkQuestionProvider
questionId={questionId}
@@ -158,6 +159,7 @@ export const _SdkQuestion = ({
backToDashboard={backToDashboard}
getClickActionMode={getClickActionMode}
navigateToNewCard={navigateToNewCard}
+ onVisualizationChange={onVisualizationChange}
>
{children ?? (
<SdkQuestionDefaultView
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/EditableDashboard/EditableDashboard.schema.ts b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/EditableDashboard/EditableDashboard.schema.ts
index f2e5f3c73fe..efa7d4e00df 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/EditableDashboard/EditableDashboard.schema.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/EditableDashboard/EditableDashboard.schema.ts
@@ -50,6 +50,7 @@ const propsSchema: Yup.SchemaOf<EditableDashboardProps> = Yup.object({
withCardTitle: Yup.mixed().optional(),
withDownloads: Yup.mixed().optional(),
withTitle: Yup.mixed().optional(),
+ onVisualizationChange: Yup.mixed().optional(),
}).noUnknown();
export const editableDashboardSchema: FunctionSchema = {
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/InteractiveDashboard/InteractiveDashboard.schema.ts b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/InteractiveDashboard/InteractiveDashboard.schema.ts
index 1eb265769e1..422e748bb7d 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/InteractiveDashboard/InteractiveDashboard.schema.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/InteractiveDashboard/InteractiveDashboard.schema.ts
@@ -50,6 +50,7 @@ const propsSchema: Yup.SchemaOf<InteractiveDashboardProps> = Yup.object({
withCardTitle: Yup.mixed().optional(),
withDownloads: Yup.mixed().optional(),
withTitle: Yup.mixed().optional(),
+ onVisualizationChange: Yup.mixed().optional(),
}).noUnknown();
export const interactiveDashboardSchema: FunctionSchema = {
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/InteractiveDashboard/InteractiveDashboard.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/InteractiveDashboard/InteractiveDashboard.unit.spec.tsx
index 88e02842dbc..2ddb6d55bce 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/InteractiveDashboard/InteractiveDashboard.unit.spec.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/InteractiveDashboard/InteractiveDashboard.unit.spec.tsx
@@ -21,6 +21,35 @@ const setup = async (
console.warn = () => {};
describe("InteractiveDashboard", () => {
+ it("should call onVisualizationChange when a card is opened", async () => {
+ const onVisualizationChange = jest.fn();
+
+ await setup({
+ dashboardName: "Test dashboard",
+ props: {
+ onVisualizationChange,
+ },
+ });
+
+ await userEvent.click(screen.getByText("Here is a card title"));
+
+ expect(
+ await screen.findByLabelText("Back to Test dashboard"),
+ ).toBeInTheDocument();
+ expect(screen.getByText("Filter")).toBeInTheDocument();
+ expect(screen.getByText("Group")).toBeInTheDocument();
+ expect(screen.getByTestId("query-visualization-root")).toBeInTheDocument();
+
+ expect(onVisualizationChange).toHaveBeenCalledTimes(1);
+ expect(onVisualizationChange).toHaveBeenCalledWith("table");
+
+ await userEvent.click(screen.getByText("Table"));
+ await userEvent.click(screen.getByText("Bar"));
+
+ expect(onVisualizationChange).toHaveBeenCalledTimes(2);
+ expect(onVisualizationChange).toHaveBeenCalledWith("bar");
+ });
+
it("should allow users to click the dashcard title", async () => {
await setup({
dashboardName: "Test dashboard",
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboard.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboard.tsx
index 9590ce9abd2..ced6b165cc0 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboard.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboard.tsx
@@ -49,6 +49,7 @@ import { useDashboardLoadHandlers } from "metabase/public/containers/PublicOrEmb
import { resetErrorPage, setErrorPage } from "metabase/redux/app";
import { dismissAllUndo } from "metabase/redux/undo";
import { getErrorPage } from "metabase/selectors/app";
+import type { CardDisplayType } from "metabase-types/api";
import type {
DrillThroughQuestionProps,
@@ -141,11 +142,12 @@ const SdkDashboardInner = ({
dashboardActions,
dashcardMenu,
getClickActionMode,
- navigateToNewCardFromDashboard = undefined,
+ navigateToNewCardFromDashboard,
className,
style,
children,
dataPickerProps,
+ onVisualizationChange,
}: SdkDashboardInnerProps) => {
const { handleLoad, handleLoadWithoutCards } = useDashboardLoadHandlers({
onLoad,
@@ -319,6 +321,7 @@ const SdkDashboardInner = ({
questionPath={adhocQuestionUrl!}
onNavigateBack={onNavigateBackToDashboard}
{...drillThroughQuestionProps}
+ onVisualizationChange={onVisualizationChange}
>
{AdHocQuestionView && <AdHocQuestionView />}
</SdkAdHocQuestion>
@@ -350,6 +353,7 @@ const SdkDashboardInner = ({
setRenderMode("dashboard");
}}
dataPickerProps={dataPickerProps}
+ onVisualizationChange={onVisualizationChange}
/>
))
.exhaustive()}
@@ -390,6 +394,7 @@ type DashboardQueryBuilderProps = {
onCreate: (question: MetabaseQuestion) => void;
onNavigateBack: () => void;
dataPickerProps: EditableDashboardOwnProps["dataPickerProps"];
+ onVisualizationChange?: (display: CardDisplayType) => void;
};
/**
@@ -399,6 +404,7 @@ function DashboardQueryBuilder({
onCreate,
onNavigateBack,
dataPickerProps,
+ onVisualizationChange,
}: DashboardQueryBuilderProps) {
const { dashboard, selectTab, setEditingDashboard } = useDashboardContext();
@@ -436,6 +442,7 @@ function DashboardQueryBuilder({
withChartTypeSelector
// The default value is 600px and it cuts off the "Visualize" button.
height="700px"
+ onVisualizationChange={onVisualizationChange}
/>
);
}
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboardStyleWrapper.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboardStyleWrapper.tsx
index 3127e936f26..33f1dbdcedf 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboardStyleWrapper.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboardStyleWrapper.tsx
@@ -13,13 +13,15 @@ export const SdkDashboardStyledWrapper = forwardRef(
) {
return (
<Flex
- mih="100vh"
bg="bg-dashboard"
direction="column"
justify="flex-start"
align="stretch"
className={cx(className, CS.overflowAuto)}
- style={style}
+ style={{
+ minHeight: "100vh",
+ ...style,
+ }}
ref={fullscreenRef}
>
{children}
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/StaticDashboard/StaticDashboard.schema.ts b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/StaticDashboard/StaticDashboard.schema.ts
index 16d8f2cb79a..cb004907b71 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/StaticDashboard/StaticDashboard.schema.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/StaticDashboard/StaticDashboard.schema.ts
@@ -27,6 +27,7 @@ const propsSchema: Yup.SchemaOf<StaticDashboardProps> = Yup.object({
withCardTitle: Yup.mixed().optional(),
withDownloads: Yup.mixed().optional(),
withTitle: Yup.mixed().optional(),
+ onVisualizationChange: Yup.mixed().optional(),
}).noUnknown();
export const staticDashboardSchema: FunctionSchema = {
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/types/dashboard.ts b/enterprise/frontend/src/embedding-sdk-bundle/types/dashboard.ts
index 68cad09e7b5..4248cf2d44c 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/types/dashboard.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/types/dashboard.ts
@@ -3,6 +3,7 @@ import type {
SdkCollectionId,
} from "embedding-sdk-bundle/types/collection";
import type { CreateDashboardProperties } from "metabase/dashboard/containers/CreateDashboardForm";
+import type { CardDisplayType } from "metabase-types/api";
import type { SdkEntityId } from "./entity-id";
@@ -41,6 +42,14 @@ export type DashboardEventHandlersProps = {
* Callback that is called when the dashboard is loaded without cards.
*/
onLoadWithoutCards?: (dashboard: MetabaseDashboard | null) => void;
+
+ /**
+ * A callback function that triggers when a question is opened from a dashboard card
+ * or when the user changes the visualization type of a question.
+ *
+ * @param visualization the new visualization type
+ */
+ onVisualizationChange?: (visualization: CardDisplayType) => void;
};
/**