The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Fix embed flow persistence by reducing scope of persisted items (#62177)
Phoomparin Manob9ccb9c8830b
diff --git a/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/helpers/index.ts b/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/helpers/index.tsindex 58462a941e8..a98eb5abd74 100644--- a/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/helpers/index.ts+++ b/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/helpers/index.ts@@ -77,10 +77,11 @@ type NavigateToStepOptions =dismissEmbedTerms?: boolean;resourceName?: never;}- | ({ experience: "dashboard" | "chart"; dismissEmbedTerms?: boolean } & (- | { resourceName: string; skipResourceSelection?: never }- | { skipResourceSelection: true }- ));+ | {+ experience: "dashboard" | "chart";+ dismissEmbedTerms?: boolean;+ resourceName: string;+ };export const navigateToEntitySelectionStep = (options: NavigateToStepOptions,@@ -105,7 +106,7 @@ export const navigateToEntitySelectionStep = (});}- if (experience !== "exploration" && !options.skipResourceSelection) {+ if (experience !== "exploration") {const { resourceName } = options;const resourceType = match(experience)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.tsindex 4883fcd8a4f..6ef3da153de 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@@ -10,7 +10,6 @@ import {const { H } = cy;const DASHBOARD_NAME = "Orders in a dashboard";-const QUESTION_NAME = "Orders, Count";describe("scenarios > embedding > sdk iframe embed setup > user settings persistence", () => {beforeEach(() => {@@ -24,115 +23,6 @@ describe("scenarios > embedding > sdk iframe embed setup > user settings persist);});- it("persists dashboard embed options", () => {- navigateToEmbedOptionsStep({- experience: "dashboard",- resourceName: DASHBOARD_NAME,- });-- cy.log("1. set embed settings to non-default values");- getEmbedSidebar().within(() => {- cy.findByLabelText("Allow downloads")- .should("not.be.checked")- .click()- .should("be.checked");-- cy.findByLabelText("Show dashboard title")- .should("be.checked")- .click()- .should("not.be.checked");- });-- cy.log("2. reload the page");- waitAndReload();- navigateToEmbedOptionsStep({- experience: "dashboard",- skipResourceSelection: true,- dismissEmbedTerms: false,- });-- cy.log("3. persisted settings should be restored");- getEmbedSidebar().within(() => {- cy.findByLabelText("Allow downloads").should("be.checked");- cy.findByLabelText("Show dashboard title").should("not.be.checked");- cy.findByLabelText("Allow users to drill through on data points").should(- "be.checked",- );- });- });-- it("persists chart embed options", { tags: "@flaky" }, () => {- navigateToEmbedOptionsStep({- experience: "chart",- resourceName: QUESTION_NAME,- });-- cy.wait("@persistSettings");-- cy.log("1. set chart embed settings to non-default values");- getEmbedSidebar().within(() => {- cy.findByLabelText("Allow downloads")- .should("not.be.checked")- .click()- .should("be.checked");-- cy.findByLabelText("Show chart title")- .should("be.checked")- .click()- .should("not.be.checked");- });-- cy.log("2. reload the page");-- waitAndReload();- navigateToEmbedOptionsStep({- experience: "chart",- skipResourceSelection: true,- dismissEmbedTerms: false,- });-- cy.log("3. verify persisted settings are restored");- getEmbedSidebar().within(() => {- cy.findByLabelText("Allow downloads").should("be.checked");- cy.findByLabelText("Show chart title").should("not.be.checked");- cy.findByLabelText("Allow users to drill through on data points").should(- "be.checked",- );- });- });-- it("persists exploration embed options", () => {- navigateToEmbedOptionsStep({ experience: "exploration" });- cy.wait("@persistSettings");-- cy.log("1. set exploration settings to non-default values");- getEmbedSidebar().within(() => {- cy.findByLabelText("Allow users to save new questions")- .should("not.be.checked")- .click()- .should("be.checked");- });-- H.getSimpleEmbedIframeContent().within(() => {- cy.findByText("Orders").click();- cy.findByText("Save").should("exist");- });-- cy.log("2. reload the page");- waitAndReload();- navigateToEmbedOptionsStep({- experience: "exploration",- dismissEmbedTerms: false,- });-- getEmbedSidebar().within(() => {- cy.log("3. persisted settings should be restored");- cy.findByLabelText("Allow users to save new questions").should(- "be.checked",- );- });- });-it("persists brand colors", () => {navigateToEmbedOptionsStep({experience: "dashboard",@@ -189,7 +79,7 @@ describe("scenarios > embedding > sdk iframe embed setup > user settings persistcy.log("3. auth method should persist");navigateToGetCodeStep({experience: "dashboard",- skipResourceSelection: true,+ resourceName: DASHBOARD_NAME,dismissEmbedTerms: false,});@@ -199,76 +89,6 @@ describe("scenarios > embedding > sdk iframe embed setup > user settings persistcodeBlock().should("not.contain", "useExistingUserSession");});});-- it.skip("persists default and hidden parameters", () => {- H.createQuestionAndDashboard({- questionDetails: {- name: "Orders table",- query: { "source-table": 1 },- },- dashboardDetails: {- name: "Dashboard with Parameters",- parameters: DASHBOARD_PARAMETERS,- },- }).then(({ body: card }) => {- H.editDashboardCard(card, {- parameter_mappings: DASHBOARD_PARAMETERS.map((parameter) => ({- card_id: card.card_id,- parameter_id: parameter.id,- target: ["dimension", ["field", 1, null]],- })),- });- });-- navigateToEmbedOptionsStep({- experience: "dashboard",- resourceName: "Dashboard with Parameters",- });-- cy.wait("@persistSettings");-- cy.log("1. hide one parameter and set default value for another");- getEmbedSidebar().within(() => {- parameterVisibilityToggle("id")- .click()- .should("have.attr", "data-hidden", "true");-- cy.findByLabelText("Product ID").type("456").blur();- });-- H.getSimpleEmbedIframeContent().within(() => {- cy.findByTestId("dashboard-parameters-widget-container").within(() => {- cy.findByLabelText("ID").should("not.exist");- cy.findByLabelText("Product ID").should("contain", "456");- });- });-- cy.log("2. reload the page");- waitAndReload();- navigateToEmbedOptionsStep({- experience: "dashboard",- skipResourceSelection: true,- dismissEmbedTerms: false,- });-- cy.log("3. parameter settings should be persisted");- getEmbedSidebar().within(() => {- parameterVisibilityToggle("id").should(- "have.attr",- "data-hidden",- "true",- );-- cy.findByLabelText("Product ID").should("have.value", "456");- });-- H.getSimpleEmbedIframeContent().within(() => {- cy.findByTestId("dashboard-parameters-widget-container").within(() => {- cy.findByLabelText("ID").should("not.exist");- cy.findByLabelText("Product ID").should("contain", "456");- });- });- });});const waitAndReload = () => {@@ -278,23 +98,3 @@ const waitAndReload = () => {H.waitForSimpleEmbedIframesToLoad();};--const parameterVisibilityToggle = (slug: string) =>- cy- .findAllByTestId("parameter-visibility-toggle")- .get(`[data-parameter-slug="${slug}"]`);--const DASHBOARD_PARAMETERS = [- {- name: "ID",- slug: "id",- id: "11111111",- type: "id",- },- {- name: "Product ID",- slug: "product_id",- id: "22222222",- type: "id",- },-];diff --git a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/components/SdkIframeEmbedSetupProvider.tsx b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/components/SdkIframeEmbedSetupProvider.tsxindex 46bfdce69e5..f9ecd2454e2 100644--- a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/components/SdkIframeEmbedSetupProvider.tsx+++ b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/components/SdkIframeEmbedSetupProvider.tsx@@ -6,6 +6,7 @@ import {useState,} from "react";import { P, match } from "ts-pattern";+import _ from "underscore";import { useUserSetting } from "metabase/common/hooks";@@ -37,14 +38,12 @@ export const SdkIframeEmbedSetupProvider = ({const [rawSettings, setRawSettings] = useState<SdkIframeEmbedSetupSettings>();- const [persistedSettings, persistSettings] = useUserSetting(- "sdk-iframe-embed-setup-settings",- { debounceTimeout: USER_SETTINGS_DEBOUNCE_MS },- );+ const [persistedSettings, persistSettings] = usePersistedSettings();// We don't want to re-fetch the recent items every time we switch between// steps, therefore we load recent items once in the provider.- const { recentDashboards, recentQuestions, addRecentItem } = useRecentItems();+ const { recentDashboards, recentQuestions, addRecentItem, isRecentsLoading } =+ useRecentItems();const defaultSettings = useMemo(() => {return getDefaultSdkIframeEmbedSettings(@@ -129,21 +128,12 @@ export const SdkIframeEmbedSetupProvider = ({// Once the persisted settings are loaded, check if they are valid.// If they are, set them as the current settings.useEffect(() => {- if (!isEmbedSettingsLoaded) {- // We consider the settings to be valid if it has at least one- // of the following properties set.- const isPersistedSettingValid =- persistedSettings?.dashboardId ||- persistedSettings?.questionId ||- persistedSettings?.template;-- if (isPersistedSettingValid) {- setRawSettings(persistedSettings);- }+ if (!isEmbedSettingsLoaded && !isRecentsLoading) {+ setRawSettings({ ...settings, ...persistedSettings });setEmbedSettingsLoaded(true);}- }, [persistedSettings, isEmbedSettingsLoaded]);+ }, [persistedSettings, isEmbedSettingsLoaded, settings, isRecentsLoading]);return (<SdkIframeEmbedSetupContext.Provider value={value}>@@ -151,3 +141,30 @@ export const SdkIframeEmbedSetupProvider = ({</SdkIframeEmbedSetupContext.Provider>);};++const getSettingsToPersist = (+ settings: Partial<SdkIframeEmbedSetupSettings>,+) => {+ return _.pick(settings, ["theme", "useExistingUserSession"]);+};++const usePersistedSettings = () => {+ const [rawPersisted, rawPersistSettings] = useUserSetting(+ "sdk-iframe-embed-setup-settings",+ { debounceTimeout: USER_SETTINGS_DEBOUNCE_MS },+ );++ const persistedSettings = useMemo(+ () => getSettingsToPersist(rawPersisted || {}),+ [rawPersisted],+ );++ const persistSettings = useCallback(+ (settings: Partial<SdkIframeEmbedSetupSettings>) => {+ rawPersistSettings(getSettingsToPersist(settings));+ },+ [rawPersistSettings],+ );++ return [persistedSettings, persistSettings] as const;+};diff --git a/frontend/src/metabase-types/api/settings.ts b/frontend/src/metabase-types/api/settings.tsindex 8797cbf325b..fd244819e09 100644--- a/frontend/src/metabase-types/api/settings.ts+++ b/frontend/src/metabase-types/api/settings.ts@@ -528,7 +528,10 @@ export type UserSettings = {"show-updated-permission-modal": boolean;"show-updated-permission-banner": boolean;"trial-banner-dismissal-timestamp"?: string | null;- "sdk-iframe-embed-setup-settings"?: SdkIframeEmbedSetupSettings | null;+ "sdk-iframe-embed-setup-settings"?: Pick<+ SdkIframeEmbedSetupSettings,+ "theme" | "useExistingUserSession"+ > | null;};/**