Skip to content

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

fix(sdk): Closes EMB-905 no error when embedding MB localhost->distant MB (#64835)

Sébastien1a8a04cf82f3
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/SdkUsageProblemDisplay.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/SdkUsageProblemDisplay.unit.spec.tsx
index ac5895a2105..4548bf9f055 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/SdkUsageProblemDisplay.unit.spec.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/SdkUsageProblemDisplay.unit.spec.tsx
@@ -6,7 +6,7 @@ import {
} from "__support__/server-mocks";
import { mockSettings } from "__support__/settings";
import { screen, within } from "__support__/ui";
-import * as IsLocalhostModule from "embedding-sdk-bundle/lib/is-localhost";
+import * as IsLocalhostModule from "embedding-sdk-bundle/lib/get-is-localhost";
import { setUsageProblem } from "embedding-sdk-bundle/store/reducer";
import { renderWithSDKProviders } from "embedding-sdk-bundle/test/__support__/ui";
import {
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/index.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/index.tsx
index 28565b7f1a8..b597a48a8c8 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/index.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/index.tsx
@@ -11,11 +11,13 @@ import S from "./SdkUsageProblemBanner.module.css";
interface Props {
authConfig: MetabaseAuthConfig;
allowConsoleLog?: boolean;
+ isLocalHost?: boolean;
}
export const SdkUsageProblemDisplay = ({
authConfig,
allowConsoleLog,
+ isLocalHost,
}: Props) => {
const session = useSdkSelector((state) => getSessionTokenState(state).token);
@@ -23,6 +25,7 @@ export const SdkUsageProblemDisplay = ({
authConfig,
allowConsoleLog,
session,
+ isLocalHost,
});
if (!usageProblem) {
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/ComponentProvider/ComponentProvider.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/ComponentProvider/ComponentProvider.tsx
index 8eb11ebd7c3..b05d466cc93 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/ComponentProvider/ComponentProvider.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/ComponentProvider/ComponentProvider.tsx
@@ -28,6 +28,7 @@ import { SdkUsageProblemDisplay } from "../../private/SdkUsageProblem";
type ComponentProviderInternalProps = ComponentProviderProps & {
reduxStore: SdkStore;
+ isLocalHost?: boolean;
};
export const ComponentProviderInternal = ({
@@ -41,6 +42,7 @@ export const ComponentProviderInternal = ({
errorComponent,
loaderComponent,
allowConsoleLog,
+ isLocalHost,
}: ComponentProviderInternalProps): JSX.Element => {
const { fontFamily } = theme ?? {};
@@ -48,7 +50,7 @@ export const ComponentProviderInternal = ({
// This call in the ComponentProvider is still needed for:
// - Storybook stories, where we don't have the MetabaseProvider
// - Unit tests
- useInitDataInternal({ reduxStore, authConfig });
+ useInitDataInternal({ reduxStore, authConfig, isLocalHost });
useEffect(() => {
if (fontFamily) {
@@ -102,6 +104,7 @@ export const ComponentProviderInternal = ({
<SdkUsageProblemDisplay
authConfig={authConfig}
allowConsoleLog={allowConsoleLog}
+ isLocalHost={isLocalHost}
/>
<PortalContainer />
@@ -117,6 +120,7 @@ export const ComponentProviderInternal = ({
export type ComponentProviderProps = MetabaseProviderProps & {
reduxStore?: SdkStore;
+ isLocalHost?: boolean;
};
export const ComponentProvider = memo(function ComponentProvider({
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/hooks/private/use-init-data/use-init-data-internal.ts b/enterprise/frontend/src/embedding-sdk-bundle/hooks/private/use-init-data/use-init-data-internal.ts
index 42c3cf8904e..7e95e870680 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/hooks/private/use-init-data/use-init-data-internal.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/hooks/private/use-init-data/use-init-data-internal.ts
@@ -24,6 +24,7 @@ const registerVisualizationsOnce = _.once(registerVisualizations);
interface InitDataLoaderParameters {
reduxStore: SdkStore;
authConfig: MetabaseAuthConfig;
+ isLocalHost?: boolean;
}
export const useInitData = () => {
@@ -49,6 +50,7 @@ export const useInitData = () => {
export const useInitDataInternal = ({
reduxStore,
authConfig,
+ isLocalHost,
}: InitDataLoaderParameters) => {
const dispatch = reduxStore.dispatch;
@@ -98,7 +100,7 @@ export const useInitDataInternal = ({
useMount(function initializeData() {
if (isAuthUninitialized()) {
- dispatch(initAuth(authConfig));
+ dispatch(initAuth({ ...authConfig, isLocalHost }));
}
});
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/hooks/private/use-sdk-usage-problem.ts b/enterprise/frontend/src/embedding-sdk-bundle/hooks/private/use-sdk-usage-problem.ts
index 0d44c371c79..b45ad915b3e 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/hooks/private/use-sdk-usage-problem.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/hooks/private/use-sdk-usage-problem.ts
@@ -14,10 +14,12 @@ export function useSdkUsageProblem({
authConfig,
allowConsoleLog = true,
session,
+ isLocalHost,
}: {
authConfig: MetabaseAuthConfig;
allowConsoleLog?: boolean;
session: MetabaseEmbeddingSessionToken | null;
+ isLocalHost?: boolean;
}) {
const hasLoggedRef = useRef(false);
@@ -55,8 +57,16 @@ export function useSdkUsageProblem({
isEnabled,
isDevelopmentMode,
session,
+ isLocalHost,
});
- }, [authConfig, hasTokenFeature, isEnabled, isDevelopmentMode, session]);
+ }, [
+ authConfig,
+ hasTokenFeature,
+ isEnabled,
+ isDevelopmentMode,
+ session,
+ isLocalHost,
+ ]);
useEffect(() => {
// SDK components will stop rendering if a license error is detected.
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/lib/is-localhost.ts b/enterprise/frontend/src/embedding-sdk-bundle/lib/get-is-localhost.ts
similarity index 100%
rename from enterprise/frontend/src/embedding-sdk-bundle/lib/is-localhost.ts
rename to enterprise/frontend/src/embedding-sdk-bundle/lib/get-is-localhost.ts
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/lib/usage-problem.ts b/enterprise/frontend/src/embedding-sdk-bundle/lib/usage-problem.ts
index d4011a7ea8b..78defcb0200 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/lib/usage-problem.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/lib/usage-problem.ts
@@ -8,7 +8,7 @@ import type {
import type { MetabaseEmbeddingSessionToken } from "embedding-sdk-package";
import { EMBEDDING_SDK_IFRAME_EMBEDDING_CONFIG } from "metabase/embedding-sdk/config";
-import { getIsLocalhost } from "./is-localhost";
+import { getIsLocalhost } from "./get-is-localhost";
interface SdkProblemOptions {
authConfig: MetabaseAuthConfig;
@@ -16,6 +16,13 @@ interface SdkProblemOptions {
hasTokenFeature: boolean;
isDevelopmentMode?: boolean;
session: MetabaseEmbeddingSessionToken | null;
+ /**
+ * Indicates whether the current environment is localhost.
+ * If not provided, it will be determined automatically based on window.location.
+ *
+ * @see getIsLocalhost()
+ */
+ isLocalHost?: boolean;
}
export const USAGE_PROBLEM_MESSAGES = {
@@ -58,13 +65,23 @@ export const USAGE_PROBLEM_DOC_URLS: Record<SdkUsageProblemKey, string> = {
export function getSdkUsageProblem(
options: SdkProblemOptions,
): SdkUsageProblem | null {
- const { isEnabled, hasTokenFeature, authConfig, isDevelopmentMode, session } =
- options;
+ const {
+ isEnabled,
+ hasTokenFeature,
+ authConfig,
+ isDevelopmentMode,
+ session,
+ isLocalHost,
+ } = options;
const { apiKey } = authConfig;
const isSSO = !apiKey;
const isApiKey = !!apiKey;
- const isLocalhost = getIsLocalhost();
+
+ // getIsLocalhost() checks for localhost based on window.location,
+ // but we want to allow overriding it via the isLocalHost parameter
+ // in case of a local app running a distant MB instance
+ const isLocalhost = isLocalHost ?? getIsLocalhost();
/**
* TODO: these checks for non-localhost environments are pending on
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/store/auth/auth.ts b/enterprise/frontend/src/embedding-sdk-bundle/store/auth/auth.ts
index 68be7f61eb0..f4b2693e9d4 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/store/auth/auth.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/store/auth/auth.ts
@@ -7,7 +7,7 @@ import {
validateSession,
} from "embedding/auth-common";
import * as MetabaseError from "embedding-sdk-bundle/errors";
-import { getIsLocalhost } from "embedding-sdk-bundle/lib/is-localhost";
+import { getIsLocalhost } from "embedding-sdk-bundle/lib/get-is-localhost";
import type { SdkStoreState } from "embedding-sdk-bundle/store/types";
import type { MetabaseAuthConfig } from "embedding-sdk-bundle/types/auth-config";
import type { MetabaseEmbeddingSessionToken } from "embedding-sdk-bundle/types/refresh-token";
@@ -25,7 +25,12 @@ import { getFetchRefreshTokenFn } from "../selectors";
export const initAuth = createAsyncThunk(
"sdk/token/INIT_AUTH",
async (
- { metabaseInstanceUrl, preferredAuthMethod, apiKey }: MetabaseAuthConfig,
+ {
+ metabaseInstanceUrl,
+ preferredAuthMethod,
+ apiKey,
+ isLocalHost,
+ }: MetabaseAuthConfig & { isLocalHost?: boolean },
{ dispatch },
) => {
// remove any stale tokens that might be there from a previous session=
@@ -34,7 +39,7 @@ export const initAuth = createAsyncThunk(
// Setup JWT or API key
const isValidInstanceUrl =
metabaseInstanceUrl && metabaseInstanceUrl?.length > 0;
- const isValidApiKeyConfig = apiKey && getIsLocalhost();
+ const isValidApiKeyConfig = apiKey && (isLocalHost || getIsLocalhost());
if (isValidApiKeyConfig) {
// API key setup
diff --git a/enterprise/frontend/src/embedding-sdk-package/components/public/MetabaseProvider/MetabaseProvider.tsx b/enterprise/frontend/src/embedding-sdk-package/components/public/MetabaseProvider/MetabaseProvider.tsx
index bcd0babf53d..b24913fd3a7 100644
--- a/enterprise/frontend/src/embedding-sdk-package/components/public/MetabaseProvider/MetabaseProvider.tsx
+++ b/enterprise/frontend/src/embedding-sdk-package/components/public/MetabaseProvider/MetabaseProvider.tsx
@@ -20,7 +20,6 @@ const MetabaseProviderInitDataWrapper = memo(function InitDataWrapper() {
const useInitData = getWindow()?.METABASE_EMBEDDING_SDK_BUNDLE?.useInitData;
const useLogVersionInfo =
getWindow()?.METABASE_EMBEDDING_SDK_BUNDLE?.useLogVersionInfo;
-
useInitData?.();
useLogVersionInfo?.();
diff --git a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/components/SdkIframeEmbedRoute.tsx b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/components/SdkIframeEmbedRoute.tsx
index 1d332e10407..83533190ce9 100644
--- a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/components/SdkIframeEmbedRoute.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/components/SdkIframeEmbedRoute.tsx
@@ -84,6 +84,7 @@ export const SdkIframeEmbedRoute = () => {
theme={theme}
locale={locale}
reduxStore={store}
+ isLocalHost={embedSettings._isLocalhost}
>
<Box h="100vh" bg={theme?.colors?.background}>
<SdkIframeEmbedView settings={embedSettings} />