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 { getQuestionTitle } from "embedding-sdk-bundle/lib/sdk-question/get-question-title";
import { useTranslateContent } from "metabase/content-translation/hooks";
import { Stack, Text } from "metabase/ui";

import { useSdkQuestionContext } from "../SdkQuestion/context";

import type { SdkQuestionDefaultViewProps } from "./SdkQuestionDefaultView";

type DefaultViewTitleTextProps = Pick<SdkQuestionDefaultViewProps, "title">;

const DefaultViewTitleText = ({ title: Title }: DefaultViewTitleTextProps) => {
  return (
    <Stack gap="xxs" align="flex-start">
      {typeof Title === "function" ? <Title></Title> : Title}
    </Stack>
  );
};

export const DefaultViewTitle = ({ title }: SdkQuestionDefaultViewProps) => {
  const { question } = useSdkQuestionContext();
  const tc = useTranslateContent();

  if (title === false) {
    return null;
  }

  if (title === undefined || title === true) {
    const titleText = getQuestionTitle(question, tc);

    if (titleText === null) {
      return null;
    }

    return (
      <DefaultViewTitleText
        title={
          titleText && (
            <Text
              fw={700}
              fz="xl"
              style={{ color: "var(--mb-color-text-primary)" }}
            >
              {titleText}
            </Text>
          )
        }
      />
    );
  }

  if (typeof title === "string") {
    const titleText = tc(title);

    return (
      <DefaultViewTitleText
        title={
          <Text
            fw={700}
            fz="xl"
            style={{ color: "var(--mb-color-text-primary)" }}
          >
            {titleText}
          </Text>
        }
      />
    );
  }

  if (typeof title === "function") {
    const CustomTitle = title;

    // TODO: pass in question={question} once we have the public-facing question type (metabase#50487)
    return <CustomTitle />;
  }

  return title;
};