Skip to content

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

3.5 KB
Raw Blame
import { useMemo } from "react";

import type { SdkQuestionProps } from "embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion";
import { SdkQuestion } from "embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion";
import { deserializeCard, parseHash } from "metabase/common/utils/card";

import type { QuestionMockLocationParameters } from "../SdkQuestion/context";

import { resolveQuestionId } from "./utils";

interface SdkAdHocQuestionProps {
  questionPath: string; // route path to load a question, e.g. /question/140-best-selling-products - for saved, or /question/xxxxxxx for ad-hoc encoded question config
  onNavigateBack?: () => void;
}

export const SdkAdHocQuestion = ({
  questionPath,
  title,
  plugins,
  height,
  width,
  className,
  style,
  children = null,
  onBeforeSave,
  onSave,
  entityTypes,
  dataPicker,
  isSaveEnabled,
  targetCollection,
  withChartTypeSelector = true,
  withDownloads = false,
  initialSqlParameters,
  onNavigateBack,
  onVisualizationChange,
  navigateToNewCard,
}: SdkAdHocQuestionProps &
  Pick<
    SdkQuestionProps,
    | "title"
    | "plugins"
    | "height"
    | "width"
    | "className"
    | "style"
    | "children"
    | "onBeforeSave"
    | "onSave"
    | "entityTypes"
    | "dataPicker"
    | "isSaveEnabled"
    | "targetCollection"
    | "withChartTypeSelector"
    | "withDownloads"
    | "initialSqlParameters"
    | "onVisualizationChange"
    | "navigateToNewCard"
  >) => {
  const { location, params } = useMemo(
    () => getQuestionParameters(questionPath),
    [questionPath],
  );

  const { options, deserializedCard } = useMemo(() => {
    const { options, serializedCard } = parseHash(location.hash);
    const deserializedCard = serializedCard
      ? deserializeCard(serializedCard)
      : undefined;

    return { options, deserializedCard };
  }, [location.hash]);

  const questionId = resolveQuestionId(
    params.slug,
    // Unjustified type cast. FIXME
    deserializedCard as { dataset_query?: { type?: string } } | undefined,
  );

  return (
    <SdkQuestion
      questionId={questionId}
      options={options}
      deserializedCard={deserializedCard}
      plugins={plugins}
      onBeforeSave={onBeforeSave}
      onSave={onSave}
      entityTypes={entityTypes}
      dataPicker={dataPicker}
      isSaveEnabled={isSaveEnabled}
      targetCollection={targetCollection}
      initialSqlParameters={initialSqlParameters}
      withDownloads={withDownloads}
      onNavigateBack={onNavigateBack}
      navigateToNewCard={navigateToNewCard}
      height={height}
      width={width}
      className={className}
      style={style}
      title={title}
      withChartTypeSelector={withChartTypeSelector}
      onVisualizationChange={onVisualizationChange}
    >
      {children}
    </SdkQuestion>
  );
};

/**
 * This generates route parameters based on the provided URL path
 * to load the interactive questions. See [use-load-question.ts]
 */
export const getQuestionParameters = (
  questionPath: string,
): QuestionMockLocationParameters => {
  const url = new URL(questionPath, "http://metabase.com"); // we use a dummy host name to fill-in full URL
  const pathSections = questionPath.split("/").slice(1); // remove first empty section
  const entityId = pathSections.length > 1 ? pathSections[1] : null; // extract possible question id if it is a saved question URL

  return {
    location: {
      search: url.search,
      hash: url.hash,
      pathname: url.pathname,
    },
    params: entityId ? { slug: entityId } : {},
  };
};