The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
import { getIsGuestEmbed } from "embedding-sdk-bundle/store/selectors";
import type {
SdkDispatch,
SdkStoreState,
} from "embedding-sdk-bundle/store/types";
import type {
LoadSdkQuestionParams,
SdkQuestionState,
} from "embedding-sdk-bundle/types/question";
import {
paramFieldsFetched,
selectQuestionFromCard,
selectQuestionFromCardBuilder,
} from "metabase/metadata-store";
import {
getParameterValuesForQuestion,
resolveCards,
} from "metabase/query_builder";
import { loadMetadataForCard } from "metabase/questions/actions";
import type { SeriesCard } from "metabase-types/api";
import type { EntityToken } from "metabase-types/api/entity";
type LoadQuestionSdkParams = LoadSdkQuestionParams & {
token: EntityToken | null | undefined;
};
export const loadQuestionSdk =
({
options = {},
deserializedCard,
questionId: initQuestionId,
token,
initialSqlParameters,
targetDashboardId,
}: LoadQuestionSdkParams) =>
async (
dispatch: SdkDispatch,
getState: () => SdkStoreState,
): Promise<
Required<Pick<SdkQuestionState, "question">> &
Pick<SdkQuestionState, "originalQuestion" | "parameterValues">
> => {
const isGuestEmbed = getIsGuestEmbed(getState());
const isNewQuestion =
initQuestionId === "new" || initQuestionId === "new-native";
const isNativeQuestion = initQuestionId === "new-native";
const questionId = isNewQuestion ? undefined : initQuestionId;
const { card: resolvedCard, originalCard } = await resolveCards({
cardId: questionId ?? undefined,
token,
options,
dispatch,
getState,
deserializedCard,
questionType: isNativeQuestion ? "native" : "gui",
});
const card: SeriesCard = isNewQuestion
? { ...resolvedCard, creationType: "custom_question" }
: resolvedCard;
if (!isGuestEmbed) {
// We don't have a `metadata` endpoint for static/guest embeds
await dispatch(loadMetadataForCard(card, { token }));
}
const originalQuestion = originalCard
? selectQuestionFromCard(getState(), originalCard)
: undefined;
let question = selectQuestionFromCard(getState(), card);
if (targetDashboardId) {
question = question.setDashboardId(targetDashboardId);
}
// In Legacy Static Embedding we didn't have this logic,
// it breaks behavior when a parameter is disabled.
if (!isGuestEmbed) {
question = question.applyTemplateTagParameters();
}
const parameterValues = getParameterValuesForQuestion({
card,
buildQuestion: selectQuestionFromCardBuilder(getState()),
queryParams: initialSqlParameters,
});
if (parameterValues) {
question = question.setParameterValues(parameterValues);
}
if ("param_fields" in card && card.param_fields) {
// This is needed to make some parameter widget populate the dropdown list
// otherwise they will use a normal text input
dispatch(paramFieldsFetched(card.param_fields));
}
return { question, originalQuestion, parameterValues };
};