Skip to content

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

Fixed issue when hiding SDK banner (#70727)

Sébastien70d4a78914a8
diff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/SdkUsageProblemDisplay-simple-embedding.unit.spec.tsx b/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/SdkUsageProblemDisplay-simple-embedding.unit.spec.tsx
index d47e0f6e517..34066b4b9c3 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/SdkUsageProblemDisplay-simple-embedding.unit.spec.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/SdkUsageProblemDisplay-simple-embedding.unit.spec.tsx
@@ -23,12 +23,6 @@ const TEST_USER = createMockUser();
jest.mock("metabase/visualizations/register", () => jest.fn(() => {}));
-const mockSdkDispatchFn = jest.fn();
-jest.mock("embedding-sdk-bundle/store", () => ({
- ...jest.requireActual("embedding-sdk-bundle/store"),
- useSdkDispatch: () => mockSdkDispatchFn,
-}));
-
jest.mock("metabase/embedding-sdk/config", () => ({
...jest.requireActual("metabase/embedding-sdk/config"),
EMBEDDING_SDK_CONFIG: {
diff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/SdkUsageProblemDisplay.unit.spec.tsx b/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/SdkUsageProblemDisplay.unit.spec.tsx
index 4548bf9f055..d557b17b52c 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/SdkUsageProblemDisplay.unit.spec.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkUsageProblem/SdkUsageProblemDisplay.unit.spec.tsx
@@ -5,9 +5,8 @@ import {
setupSettingsEndpoints,
} from "__support__/server-mocks";
import { mockSettings } from "__support__/settings";
-import { screen, within } from "__support__/ui";
+import { screen, waitFor, within } from "__support__/ui";
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 {
createMockApiKeyConfig,
@@ -29,12 +28,6 @@ const TEST_USER = createMockUser();
jest.mock("metabase/visualizations/register", () => jest.fn(() => {}));
-const mockSdkDispatchFn = jest.fn();
-jest.mock("embedding-sdk-bundle/store", () => ({
- ...jest.requireActual("embedding-sdk-bundle/store"),
- useSdkDispatch: () => mockSdkDispatchFn,
-}));
-
interface Options {
authConfig: MetabaseAuthConfig;
hasEmbeddingFeature?: boolean;
@@ -279,6 +272,10 @@ describe("SdkUsageProblemDisplay", () => {
await userEvent.click(within(card).getByText("Hide warning"));
- expect(mockSdkDispatchFn).toHaveBeenCalledWith(setUsageProblem(null));
+ await waitFor(() => {
+ expect(
+ screen.queryByTestId(PROBLEM_INDICATOR_TEST_ID),
+ ).not.toBeInTheDocument();
+ });
});
});
diff --git a/frontend/src/embedding-sdk-bundle/hooks/private/use-sdk-usage-problem.ts b/frontend/src/embedding-sdk-bundle/hooks/private/use-sdk-usage-problem.ts
index 5329658961d..dd78e0aaf57 100644
--- a/frontend/src/embedding-sdk-bundle/hooks/private/use-sdk-usage-problem.ts
+++ b/frontend/src/embedding-sdk-bundle/hooks/private/use-sdk-usage-problem.ts
@@ -1,4 +1,4 @@
-import { useEffect, useMemo, useRef } from "react";
+import { useEffect, useRef } from "react";
import { printUsageProblemToConsole } from "embedding-sdk-bundle/lib/print-usage-problem";
import { getSdkUsageProblem } from "embedding-sdk-bundle/lib/usage-problem";
@@ -7,6 +7,7 @@ import { setUsageProblem } from "embedding-sdk-bundle/store/reducer";
import {
getHasTokenFeature,
getIsGuestEmbedRaw,
+ getUsageProblem,
} from "embedding-sdk-bundle/store/selectors";
import type { MetabaseAuthConfig } from "embedding-sdk-bundle/types/auth-config";
import { useSetting } from "metabase/common/hooks";
@@ -48,16 +49,23 @@ export function useSdkUsageProblem({
return getTokenFeature(state, "development_mode");
});
- const usageProblem = useMemo(() => {
- return getSdkUsageProblem({
- isGuestEmbed,
- authConfig,
- hasTokenFeature,
- isEnabled,
- isDevelopmentMode,
- session,
- isLocalHost,
- });
+ // Sync the computed usage problem to the store whenever inputs change.
+ // This lets other consumers (e.g. SDK components that stop rendering on
+ // license errors) read the problem from the store.
+ useEffect(() => {
+ dispatch(
+ setUsageProblem(
+ getSdkUsageProblem({
+ isGuestEmbed,
+ authConfig,
+ hasTokenFeature,
+ isEnabled,
+ isDevelopmentMode,
+ session,
+ isLocalHost,
+ }),
+ ),
+ );
}, [
isGuestEmbed,
authConfig,
@@ -66,12 +74,15 @@ export function useSdkUsageProblem({
isDevelopmentMode,
session,
isLocalHost,
+ dispatch,
]);
- useEffect(() => {
- // SDK components will stop rendering if a license error is detected.
- dispatch(setUsageProblem(usageProblem));
+ // Read the problem from the store rather than from a local useMemo so that
+ // external dispatches (e.g. the "Hide" button calling setUsageProblem(null))
+ // are reflected here.
+ const usageProblem = useSdkSelector(getUsageProblem);
+ useEffect(() => {
// Log the problem to the console once.
if (!hasLoggedRef.current && allowConsoleLog) {
printUsageProblemToConsole(usageProblem);