Skip to content

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

Migrate query_builder/analytics to TypeScript (#76934)

Romeo Van Snick544a0c2ba818
diff --git a/frontend/src/embedding-sdk-bundle/lib/sdk-question/load-question.ts b/frontend/src/embedding-sdk-bundle/lib/sdk-question/load-question.ts
index daf65dad91b..58df19c3ce8 100644
--- a/frontend/src/embedding-sdk-bundle/lib/sdk-question/load-question.ts
+++ b/frontend/src/embedding-sdk-bundle/lib/sdk-question/load-question.ts
@@ -14,6 +14,7 @@ import { updateMetadata } from "metabase/redux/metadata";
import { FieldSchema } from "metabase/schema";
import { getMetadata } from "metabase/selectors/metadata";
import Question from "metabase-lib/v1/Question";
+import type { Card } from "metabase-types/api/card";
import type { EntityToken } from "metabase-types/api/entity";
type LoadQuestionSdkParams = LoadSdkQuestionParams & {
@@ -53,7 +54,7 @@ export const loadQuestionSdk =
questionType: isNativeQuestion ? "native" : "gui",
});
- const card = isNewQuestion
+ const card: Card = isNewQuestion
? { ...resolvedCard, creationType: "custom_question" }
: resolvedCard;
diff --git a/frontend/src/metabase-lib/v1/Question.ts b/frontend/src/metabase-lib/v1/Question.ts
index 8645864e84f..9f2bd3ee6a1 100644
--- a/frontend/src/metabase-lib/v1/Question.ts
+++ b/frontend/src/metabase-lib/v1/Question.ts
@@ -15,6 +15,7 @@ import NativeQuery, {
import { STRUCTURED_QUERY_TEMPLATE } from "metabase-lib/v1/queries/StructuredQuery";
import type {
Card,
+ CardCreationType,
CardDashboardInfo,
CardDisplayType,
CardType,
@@ -324,7 +325,7 @@ class Question {
return this.setSettings({ ...this.settings(), ...settings });
}
- creationType(): string | undefined {
+ creationType(): CardCreationType | undefined {
return this.card().creationType;
}
diff --git a/frontend/src/metabase-types/analytics/question.ts b/frontend/src/metabase-types/analytics/question.ts
index 29fd01b3e46..b0a061ca631 100644
--- a/frontend/src/metabase-types/analytics/question.ts
+++ b/frontend/src/metabase-types/analytics/question.ts
@@ -1,3 +1,5 @@
+import type { CardCreationType } from "metabase-types/api";
+
type QuestionEventSchema = {
event: string;
question_id: number;
@@ -17,7 +19,7 @@ export type NewQuestionSavedEvent = ValidateEvent<{
event: "new_question_saved";
question_id: number;
database_id: number | null;
- type: "simple_question" | "custom_question" | "native_question";
+ type?: CardCreationType | null;
method: "from_scratch" | "existing_question";
visualization_type: string;
}>;
diff --git a/frontend/src/metabase-types/api/card.ts b/frontend/src/metabase-types/api/card.ts
index c19cc59d9c7..4f10e9dc127 100644
--- a/frontend/src/metabase-types/api/card.ts
+++ b/frontend/src/metabase-types/api/card.ts
@@ -40,6 +40,11 @@ import type {
export const CARD_TYPES = ["model", "question", "metric"] as const;
export type CardType = (typeof CARD_TYPES)[number];
+export type CardCreationType =
+ | "simple_question"
+ | "custom_question"
+ | "native_question";
+
export type CardDashboardInfo = Pick<Dashboard, "id" | "name">;
export type CardDocumentInfo = Pick<Document, "id" | "name">;
@@ -124,7 +129,7 @@ export interface UnsavedCard<Q extends DatasetQuery = DatasetQuery> {
displayIsLocked?: boolean;
// Not part of the card API contract, a transient marker for how the card was created
- creationType?: string;
+ creationType?: CardCreationType;
}
export type LineSize = "S" | "M" | "L";
diff --git a/frontend/src/metabase/query_builder/analytics.js b/frontend/src/metabase/query_builder/analytics.ts
similarity index 64%
rename from frontend/src/metabase/query_builder/analytics.js
rename to frontend/src/metabase/query_builder/analytics.ts
index 6020fc02e38..099511697eb 100644
--- a/frontend/src/metabase/query_builder/analytics.js
+++ b/frontend/src/metabase/query_builder/analytics.ts
@@ -1,9 +1,11 @@
import { trackSchemaEvent, trackSimpleEvent } from "metabase/analytics";
+import type Question from "metabase-lib/v1/Question";
+import type { Card } from "metabase-types/api";
export const trackNewQuestionSaved = (
- draftQuestion,
- createdQuestion,
- isBasedOnExistingQuestion,
+ draftQuestion: Question,
+ createdQuestion: Question,
+ isBasedOnExistingQuestion: boolean,
) => {
trackSchemaEvent("question", {
event: "new_question_saved",
@@ -11,18 +13,21 @@ export const trackNewQuestionSaved = (
database_id: createdQuestion.databaseId(),
visualization_type: createdQuestion.display(),
type: draftQuestion.creationType(),
- source: isBasedOnExistingQuestion ? "existing_question" : "from_scratch",
+ method: isBasedOnExistingQuestion ? "existing_question" : "from_scratch",
});
};
-export const trackTurnIntoModelClicked = (question) => {
+export const trackTurnIntoModelClicked = (question: Question) => {
trackSchemaEvent("question", {
event: "turn_into_model_clicked",
question_id: question.id(),
});
};
-export const trackNotebookNativePreviewShown = (question, isShown) => {
+export const trackNotebookNativePreviewShown = (
+ question: Question,
+ isShown: boolean,
+) => {
trackSchemaEvent("question", {
event: isShown
? "notebook_native_preview_shown"
@@ -32,14 +37,14 @@ export const trackNotebookNativePreviewShown = (question, isShown) => {
});
};
-export const trackFirstNonTableChartGenerated = (card) => {
+export const trackFirstNonTableChartGenerated = (card: Card) => {
trackSimpleEvent({
event: "chart_generated",
event_detail: card.display,
});
};
-export const trackCardBookmarkAdded = (card) => {
+export const trackCardBookmarkAdded = (card: Card) => {
trackSimpleEvent({
event: "bookmark_added",
event_detail: card.type,