Skip to content

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

1.8 KB
Raw Blame
import { t } from "ttag";

import { ToolbarButton } from "metabase/common/components/ToolbarButton";
import { useHasEmailSetup } from "metabase/common/hooks";
import { toggleSharing } from "metabase/dashboard/actions";
import { useDashboardContext } from "metabase/dashboard/context";
import { isDataApp } from "metabase/embedding-sdk/config";
import type { DashboardSubscriptionsButtonProps } from "metabase/plugins";
import { useDispatch } from "metabase/redux";

/**
 * @internal Do not import this component directly, use either SDK or EAJS EE plugins instead.
 */
export const DashboardSubscriptionsButton = (
  props: DashboardSubscriptionsButtonProps,
) => {
  const dispatch = useDispatch();
  const handleClick = () => dispatch(toggleSharing());

  const { dashboard } = useDashboardContext();

  /**
   * Use the same logic as in the core app
   * @see {@link https://github.com/metabase/metabase/blob/71ed90d16cb7c64946135d404c1a79ef65995f03/frontend/src/metabase/notifications/NotificationsActionsMenu/DashboardSubscriptionMenuItem.tsx#L26-L28}
   */
  const hasDataCards = dashboard?.dashcards?.some(
    (dashCard) => !["text", "heading"].includes(dashCard.card.display),
  );
  // A data app renders a UI; a subscription is scheduled email delivered later, out of band.
  // It is deliberately not part of the data-app surface, so the affordance never appears
  // rather than appearing and failing on the (unscoped) /api/pulse routes behind it.
  const isDataAppEmbed = isDataApp();

  // We decided not to show the subscriptions button if email is not set up
  const hasEmailSetup = useHasEmailSetup({ skip: isDataAppEmbed });

  if (!hasEmailSetup || !hasDataCards || isDataAppEmbed) {
    return null;
  }

  return (
    <ToolbarButton
      icon="subscription"
      tooltipLabel={t`Subscriptions`}
      onClick={handleClick}
      {...props}
    />
  );
};