Skip to content

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

refactor(sdk): Properly reinitialize metabaseConfig watcher for EmbedJS wizard (#64235)

Timofey Kachalov1704f14bda75
diff --git a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/constants.ts b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/constants.ts
index a0a06bdd891..c1683a45f32 100644
--- a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/constants.ts
+++ b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/constants.ts
@@ -81,3 +81,5 @@ export const DISABLE_UPDATE_FOR_KEYS = [
"useExistingUserSession",
"fetchRequestToken",
] as const satisfies AllowedEmbedSettingKey[];
+
+export const METABASE_CONFIG_IS_PROXY_FIELD_NAME = "__isProxy";
diff --git a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/embed.ts b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/embed.ts
index 6de34378bdd..baf00dbf750 100644
--- a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/embed.ts
+++ b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/embed.ts
@@ -13,6 +13,7 @@ import { debouncedReportAnalytics } from "./analytics";
import {
ALLOWED_EMBED_SETTING_KEYS_MAP,
DISABLE_UPDATE_FOR_KEYS,
+ METABASE_CONFIG_IS_PROXY_FIELD_NAME,
} from "./constants";
import type {
SdkIframeEmbedElementSettings,
@@ -34,9 +35,17 @@ const _activeEmbeds: Set<MetabaseEmbedElement> = new Set();
// window.metabaseConfig to re-create the proxy if the whole object is replaced,
// for example if this script is loaded before the customer calls
// `defineMetabaseConfig` in their code, which replaces the entire object.
-const setupConfigWatcher = () => {
+export const setupConfigWatcher = () => {
const createProxy = (target: Record<string, unknown>) =>
new Proxy(target, {
+ get(target, prop, receiver) {
+ // Needed for EmbedJS Wizard to call setupConfigWatcher on the Wizard reinitialization
+ if (prop === METABASE_CONFIG_IS_PROXY_FIELD_NAME) {
+ return true;
+ }
+
+ return Reflect.get(target, prop, receiver);
+ },
set(metabaseConfig, prop, newValue) {
metabaseConfig[prop as string] = newValue;
updateAllEmbeds({ [prop]: newValue });
diff --git a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/embed.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/embed.unit.spec.ts
index 584a09d95aa..effce5ac346 100644
--- a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/embed.unit.spec.ts
+++ b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/embed.unit.spec.ts
@@ -11,7 +11,6 @@ describe("embed.js script tag for sdk iframe embedding", () => {
beforeEach(() => {
jest.resetModules();
- // @ts-expect-error -- test cleanup
delete window.metabaseConfig;
require("./embed"); // we do things when the script is loaded
diff --git a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/components/SdkIframeEmbedPreview.tsx b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/components/SdkIframeEmbedPreview.tsx
index 9763547cf12..56dca6c87cc 100644
--- a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/components/SdkIframeEmbedPreview.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/components/SdkIframeEmbedPreview.tsx
@@ -13,6 +13,10 @@ import { useSetting } from "metabase/common/hooks";
import type { MetabaseTheme } from "metabase/embedding-sdk/theme";
import { colors as defaultMetabaseColors } from "metabase/lib/colors";
import { Card } from "metabase/ui";
+import { METABASE_CONFIG_IS_PROXY_FIELD_NAME } from "metabase-enterprise/embedding_iframe_sdk/constants";
+// we import the equivalent of embed.js so that we don't add extra loading time
+// by appending the script
+import { setupConfigWatcher } from "metabase-enterprise/embedding_iframe_sdk/embed";
import type { SdkIframeEmbedBaseSettings } from "metabase-enterprise/embedding_iframe_sdk/types/embed";
import { useSdkIframeEmbedSetupContext } from "../context";
@@ -22,13 +26,11 @@ import { getConfigurableThemeColors } from "../utils/theme-colors";
import { EmbedPreviewLoadingOverlay } from "./EmbedPreviewLoadingOverlay";
import S from "./SdkIframeEmbedPreview.module.css";
-// we import the equivalent of embed.js so that we don't add extra loading time
-// by appending the script
-import "metabase-enterprise/embedding_iframe_sdk/embed";
-
declare global {
interface Window {
- metabaseConfig: Partial<SdkIframeEmbedBaseSettings>;
+ metabaseConfig?: Partial<SdkIframeEmbedBaseSettings> & {
+ [METABASE_CONFIG_IS_PROXY_FIELD_NAME]?: boolean;
+ };
}
}
@@ -46,9 +48,18 @@ export const SdkIframeEmbedPreview = () => {
const defineMetabaseConfig = useCallback(
(metabaseConfig: SdkIframeEmbedBaseSettings) => {
window.metabaseConfig = metabaseConfig;
+
+ if (!window.metabaseConfig[METABASE_CONFIG_IS_PROXY_FIELD_NAME]) {
+ setupConfigWatcher();
+ }
},
[],
);
+ const cleanupMetabaseConfig = useCallback(() => {
+ if (window.metabaseConfig) {
+ delete window.metabaseConfig;
+ }
+ }, []);
const derivedTheme = useMemo(() => {
// TODO(EMB-696): There is a bug in the SDK where if we set the theme back to undefined,
@@ -81,7 +92,7 @@ export const SdkIframeEmbedPreview = () => {
);
// initial configuration, needed so that the element finds the config on first render
- if (!window.metabaseConfig.instanceUrl) {
+ if (!window.metabaseConfig?.instanceUrl) {
defineMetabaseConfig(metabaseConfig);
}
@@ -89,6 +100,13 @@ export const SdkIframeEmbedPreview = () => {
defineMetabaseConfig(metabaseConfig);
}, [metabaseConfig, defineMetabaseConfig]);
+ useEffect(
+ () => () => {
+ cleanupMetabaseConfig();
+ },
+ [cleanupMetabaseConfig],
+ );
+
// Show a "fake" loading indicator when componentName changes.
// Embed JS has its own loading indicator, but it shows up after the iframe loads.
useEffect(() => {