Skip to content

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

Fix embed preview stuck loader when reopening with a saved theme (#77150)

Sébastien951be524ecf1
diff --git a/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/user-settings-persistence.cy.spec.ts b/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/user-settings-persistence.cy.spec.ts
index 851f3781afc..8b1042f256b 100644
--- a/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/user-settings-persistence.cy.spec.ts
+++ b/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/user-settings-persistence.cy.spec.ts
@@ -4,6 +4,43 @@ const { H } = cy;
const DASHBOARD_NAME = "Orders in a dashboard";
+const SAVED_THEME_NAME = "Sunset";
+
+const seedSavedTheme = () =>
+ cy.request("POST", "/api/embed-theme", {
+ name: SAVED_THEME_NAME,
+ settings: {
+ colors: {
+ brand: "#FF0000",
+ // Distinct chart colors are what makes the preview container remount
+ // once the saved theme resolves — see the regression test below.
+ charts: [
+ "#FF0000",
+ "#FF7F00",
+ "#FFD400",
+ "#00A86B",
+ "#0080FF",
+ "#3F00FF",
+ "#8B00FF",
+ "#FF00AA",
+ ],
+ },
+ },
+ });
+
+const assertPreviewFinishesLoading = () => {
+ cy.get("[data-iframe-loaded]", { timeout: 20_000 }).should("have.length", 1);
+ cy.findByTestId("preview-loading-indicator").should("not.exist");
+};
+
+const reopenNewEmbedModal = () => {
+ cy.findAllByTestId(/(sdk-setting-card|guest-embeds-setting-card)/)
+ .first()
+ .within(() => {
+ cy.findByText("New embed").click();
+ });
+};
+
describe("scenarios > embedding > sdk iframe embed setup > user settings persistence", () => {
beforeEach(() => {
H.restore();
@@ -58,4 +95,35 @@ describe("scenarios > embedding > sdk iframe embed setup > user settings persist
.first()
.should("have.css", "color", "rgb(255, 0, 0)");
});
+
+ it("finishes loading the preview when a persisted saved theme is restored on reopen", () => {
+ seedSavedTheme();
+
+ navigateToEmbedOptionsStep({
+ experience: "dashboard",
+ resourceName: DASHBOARD_NAME,
+ });
+
+ cy.log("select a saved theme (anything other than the instance theme)");
+ cy.findByTestId(`theme-card-${SAVED_THEME_NAME}`).click();
+
+ cy.wait("@persistSettings");
+
+ cy.log("the preview loads with the selected theme applied");
+ assertPreviewFinishesLoading();
+
+ cy.log("close the New embed modal");
+ cy.findByTestId("sdk-iframe-embed-setup-modal-content")
+ .findByLabelText("Close")
+ .click();
+ cy.findByTestId("sdk-iframe-embed-setup-modal-content").should("not.exist");
+
+ cy.log("reopen the New embed modal");
+ reopenNewEmbedModal();
+
+ cy.log(
+ "the preview must finish loading — the restored saved theme must not leave it stuck on the loader",
+ );
+ assertPreviewFinishesLoading();
+ });
});
diff --git a/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/SdkIframeEmbedPreview.tsx b/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/SdkIframeEmbedPreview.tsx
index bbf325c6d72..48b7ddc8c6f 100644
--- a/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/SdkIframeEmbedPreview.tsx
+++ b/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/SdkIframeEmbedPreview.tsx
@@ -53,7 +53,30 @@ const SdkIframeEmbedPreviewInner = () => {
skip: !isSimpleEmbedFeatureAvailable || !settings.theme?.id,
});
- const containerRef = useRef<HTMLDivElement>(null);
+ const embedElementRef = useRef<HTMLElement | null>(null);
+
+ const handleEmbedReady = useCallback(() => setIsLoading(false), []);
+
+ // Attach the "ready" listener through a ref callback so it re-arms every time
+ // the embed element is recreated. The container remounts whenever chart colors
+ // change (see the `key` on the Card below), and a listener registered in an
+ // effect keyed on `componentName` would stay bound to the discarded element,
+ // leaving the loading overlay stuck forever.
+ const setEmbedElementRef = useCallback(
+ (node: HTMLElement | null) => {
+ if (embedElementRef.current) {
+ embedElementRef.current.removeEventListener("ready", handleEmbedReady);
+ }
+
+ embedElementRef.current = node;
+
+ if (node) {
+ setIsLoading(true);
+ node.addEventListener("ready", handleEmbedReady);
+ }
+ },
+ [handleEmbedReady],
+ );
const defineMetabaseConfig = useCallback(
(metabaseConfig: SdkIframeEmbedBaseSettings) => {
@@ -129,24 +152,6 @@ const SdkIframeEmbedPreviewInner = () => {
[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(() => {
- if (containerRef.current) {
- const embed = containerRef.current.querySelector(settings.componentName);
- const handleReady = () => setIsLoading(false);
-
- if (embed) {
- setIsLoading(true);
- embed.addEventListener("ready", handleReady);
-
- return () => {
- embed.removeEventListener("ready", handleReady);
- };
- }
- }
- }, [settings.componentName]);
-
const attributes = buildEmbedAttributes({
experience,
settings,
@@ -154,13 +159,17 @@ const SdkIframeEmbedPreviewInner = () => {
wrapWithQuotes: false,
});
+ // The embed's own loading indicator only appears after the iframe loads, so we
+ // show a "fake" loader (cleared by the "ready" event) to avoid a white flash.
+ // The ref callback wires up that listener — see `setEmbedElementRef` above.
+ const embedAttributes = { ...attributes, ref: setEmbedElementRef };
+
return (
<Card
className={S.EmbedPreviewIframe}
id="iframe-embed-container"
style={{ backgroundColor: theme?.colors?.background }}
h="100%"
- ref={containerRef}
pos="relative"
// `key` forces a remount when chart colors change. The embed runtime
// propagates other theme keys via CSS variables, but chart colors are
@@ -171,19 +180,19 @@ const SdkIframeEmbedPreviewInner = () => {
{match(settings)
.with(
{ componentName: "metabase-question", template: "exploration" },
- () => createElement("metabase-question", attributes),
+ () => createElement("metabase-question", embedAttributes),
)
.with({ componentName: "metabase-question" }, () =>
- createElement("metabase-question", attributes),
+ createElement("metabase-question", embedAttributes),
)
.with({ componentName: "metabase-dashboard" }, () =>
- createElement("metabase-dashboard", attributes),
+ createElement("metabase-dashboard", embedAttributes),
)
.with({ componentName: "metabase-browser" }, () =>
- createElement("metabase-browser", attributes),
+ createElement("metabase-browser", embedAttributes),
)
.with({ componentName: "metabase-metabot" }, () =>
- createElement("metabase-metabot", attributes),
+ createElement("metabase-metabot", embedAttributes),
)
.exhaustive()}