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-bundle): <metabase-question/> in guest embed no longer calls pulseform input (#71885)

Sébastien2a7c8331bef5
diff --git a/enterprise/frontend/src/embedding-sdk-ee/notifications/QuestionAlertsButton.tsx b/enterprise/frontend/src/embedding-sdk-ee/notifications/QuestionAlertsButton.tsx
index 5918be58d60..b41b6565b52 100644
--- a/enterprise/frontend/src/embedding-sdk-ee/notifications/QuestionAlertsButton.tsx
+++ b/enterprise/frontend/src/embedding-sdk-ee/notifications/QuestionAlertsButton.tsx
@@ -6,6 +6,7 @@ import { useSdkQuestionContext } from "embedding-sdk-bundle/components/private/S
import { useQuestionAlertModalContext } from "embedding-sdk-bundle/components/private/notifications/context/QuestionAlertModalProvider";
import type { QuestionAlertsButtonProps } from "embedding-sdk-bundle/components/public/notifications";
import { useSdkSelector } from "embedding-sdk-bundle/store";
+import { getIsGuestEmbed } from "embedding-sdk-bundle/store/selectors";
import { useHasEmailSetup } from "metabase/common/hooks";
import { canManageSubscriptions as canManageSubscriptionsSelector } from "metabase/selectors/user";
import { isInstanceAnalyticsCollection } from "metabase-enterprise/collections/utils";
@@ -16,16 +17,21 @@ import { isInstanceAnalyticsCollection } from "metabase-enterprise/collections/u
export const QuestionAlertsButton = (props: QuestionAlertsButtonProps) => {
const { withAlerts, question } = useSdkQuestionContext();
const canManageSubscriptions = useSdkSelector(canManageSubscriptionsSelector);
+ const isGuestEmbed = useSdkSelector(getIsGuestEmbed);
const { toggle: toggleModal, close: closeModal } =
useQuestionAlertModalContext();
const isSaved = question?.isSaved();
const isModel = question?.type() === "model";
const isAnalytics = isInstanceAnalyticsCollection(question?.collection());
- const hasEmailSetup = useHasEmailSetup();
+
+ // Skip the pulse/form_input request in guest embed mode (EMB-1525),
+ // as the endpoint does not exist for guest embeds.
+ const hasEmailSetup = useHasEmailSetup({ skip: isGuestEmbed });
const shouldRenderAlertsButton =
hasEmailSetup &&
+ !isGuestEmbed &&
withAlerts &&
isSaved &&
canManageSubscriptions &&
diff --git a/enterprise/frontend/src/embedding-sdk-ee/notifications/QuestionAlertsButton.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-ee/notifications/QuestionAlertsButton.unit.spec.tsx
new file mode 100644
index 00000000000..d7f61de1b9b
--- /dev/null
+++ b/enterprise/frontend/src/embedding-sdk-ee/notifications/QuestionAlertsButton.unit.spec.tsx
@@ -0,0 +1,103 @@
+import { screen, waitFor } from "@testing-library/react";
+
+import {
+ findRequests,
+ setupNotificationChannelsEndpoints,
+} from "__support__/server-mocks";
+import { renderWithSDKProviders } from "embedding-sdk-bundle/test/__support__/ui";
+import { createMockSdkConfig } from "embedding-sdk-bundle/test/mocks/config";
+import {
+ createMockLoginStatusState,
+ createMockSdkState,
+} from "embedding-sdk-bundle/test/mocks/state";
+import { createMockSettings, createMockUser } from "metabase-types/api/mocks";
+import { createMockState } from "metabase-types/store/mocks";
+
+import { QuestionAlertsButton } from "./QuestionAlertsButton";
+
+jest.mock(
+ "embedding-sdk-bundle/components/private/SdkQuestion/context",
+ () => ({
+ useSdkQuestionContext: () => ({
+ withAlerts: true,
+ question: {
+ isSaved: () => true,
+ type: () => "question",
+ collection: () => ({}),
+ },
+ }),
+ }),
+);
+
+jest.mock(
+ "embedding-sdk-bundle/components/private/notifications/context/QuestionAlertModalProvider",
+ () => ({
+ useQuestionAlertModalContext: () => ({
+ toggle: jest.fn(),
+ close: jest.fn(),
+ isOpen: false,
+ }),
+ }),
+);
+
+function setup({ isGuestEmbed }: { isGuestEmbed: boolean }) {
+ setupNotificationChannelsEndpoints({
+ email: { configured: true },
+ } as any);
+
+ const state = createMockState({
+ currentUser: createMockUser({
+ is_superuser: true,
+ permissions: { can_access_subscription: true },
+ }),
+ settings: {
+ values: {
+ ...createMockSettings({ "enable-embedding-sdk": true }),
+ "token-features": {
+ embedding_sdk: true,
+ advanced_permissions: false,
+ },
+ } as any,
+ },
+ sdk: createMockSdkState({
+ isGuestEmbed,
+ initStatus: createMockLoginStatusState({ status: "success" }),
+ }),
+ });
+
+ renderWithSDKProviders(<QuestionAlertsButton />, {
+ componentProviderProps: {
+ authConfig: createMockSdkConfig(),
+ },
+ storeInitialState: state,
+ });
+}
+
+async function getFormInputRequests() {
+ const gets = await findRequests("GET");
+ return gets.filter((req) => req.url.match(/\/api\/pulse\/form_input$/));
+}
+
+describe("QuestionAlertsButton", () => {
+ it("should render the alerts button and call pulse/form_input when not in guest embed mode", async () => {
+ setup({ isGuestEmbed: false });
+
+ await waitFor(async () => {
+ expect(await getFormInputRequests()).toHaveLength(1);
+ });
+
+ expect(
+ await screen.findByRole("button", { name: "Alerts" }),
+ ).toBeInTheDocument();
+ });
+
+ it("should not render the alerts button and should not call pulse/form_input in guest embed mode (EMB-1525)", async () => {
+ setup({ isGuestEmbed: true });
+
+ expect(
+ screen.queryByRole("button", { name: "Alerts" }),
+ ).not.toBeInTheDocument();
+
+ expect(await getFormInputRequests()).toHaveLength(0);
+ });
+});
diff --git a/frontend/src/metabase/common/hooks/use-notification-channels/use-notification-channels.ts b/frontend/src/metabase/common/hooks/use-notification-channels/use-notification-channels.ts
index 4f300adc237..a8635401fca 100644
--- a/frontend/src/metabase/common/hooks/use-notification-channels/use-notification-channels.ts
+++ b/frontend/src/metabase/common/hooks/use-notification-channels/use-notification-channels.ts
@@ -8,8 +8,10 @@ export const useHasAnyNotificationChannel = (): boolean => {
);
};
-export const useHasEmailSetup = (): boolean => {
- const { data: channelInfo } = useGetChannelInfoQuery();
+export const useHasEmailSetup = ({
+ skip,
+}: { skip?: boolean } = {}): boolean => {
+ const { data: channelInfo } = useGetChannelInfoQuery(undefined, { skip });
return !!channelInfo?.channels?.email?.configured;
};