Skip to content

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

Convert app.js and app-main.js entry points to TypeScript (#79724)

Andrei Chernykhb0826c98f3b4
diff --git a/eslint.config.mjs b/eslint.config.mjs
index af032cc7eec..ee95b327676 100644
--- a/eslint.config.mjs
+++ b/eslint.config.mjs
@@ -609,12 +609,6 @@ const configs = [
],
},
},
- {
- files: ["frontend/src/metabase/app.js"],
- rules: {
- "import/no-duplicates": "off",
- },
- },
{
files: ["frontend/src/metabase/**/*.stories.tsx"],
rules: {
diff --git a/frontend/lint/module-boundaries.mjs b/frontend/lint/module-boundaries.mjs
index 584ddd5b27b..9f30eee1f67 100644
--- a/frontend/lint/module-boundaries.mjs
+++ b/frontend/lint/module-boundaries.mjs
@@ -319,9 +319,9 @@ const elements = [
// app
...[
- "frontend/src/metabase/app.js",
+ "frontend/src/metabase/app.tsx",
"frontend/src/metabase/app-embed-sdk.tsx",
- "frontend/src/metabase/app-main.js",
+ "frontend/src/metabase/app-main.ts",
"frontend/src/metabase/app-embed.ts",
"frontend/src/metabase/app-public.ts",
"frontend/src/metabase/app-static-viz.ts",
@@ -345,7 +345,7 @@ const elements = [
// so the test is app-tier too.
"frontend/src/metabase/common/components/NewItemMenu/NewItemMenu.unit.spec.tsx",
// Entry point for the static-viz bundle (server-side chart rendering in
- // GraalJS) - like app.js, it composes OSS + EE code for a build artifact.
+ // GraalJS) - like app.tsx, it composes OSS + EE code for a build artifact.
// Full-mode entries match before folder patterns, whatever the order.
"frontend/src/metabase/static-viz/index.tsx",
// Storybook config is a composition root: preview wires app-tier decorators.
diff --git a/frontend/src/metabase/api/client/client.ts b/frontend/src/metabase/api/client/client.ts
index 59556324143..f08a653f297 100644
--- a/frontend/src/metabase/api/client/client.ts
+++ b/frontend/src/metabase/api/client/client.ts
@@ -109,7 +109,7 @@ export class ApiClient extends EventEmitter<EventMap> {
);
if (!init.noEvent && (status === 401 || status === 403)) {
- // Strip basename so listeners (app-main.js) see the relative path.
+ // Strip basename so listeners (app-main.ts) see the relative path.
this.emit(status, relativeUrl(getBasename(), init.url));
}
diff --git a/frontend/src/metabase/app-main.js b/frontend/src/metabase/app-main.ts
similarity index 94%
rename from frontend/src/metabase/app-main.js
rename to frontend/src/metabase/app-main.ts
index 78c84078090..0d895ebc015 100644
--- a/frontend/src/metabase/app-main.js
+++ b/frontend/src/metabase/app-main.ts
@@ -2,8 +2,6 @@
// MUST be imported BEFORE `react` and `react-dom`
import "metabase-dev";
-import _ from "underscore";
-
import { Api } from "metabase/api";
import { PLUGIN_API, api } from "metabase/api/client";
import { init } from "metabase/app";
@@ -47,7 +45,7 @@ init(mainReducers, getRoutes, (store) => {
// is `/auth/login/password` instead of `/auth/login`.
// So if call to api when signing in fails, let’s stay in the current page.
// Otherwise it will always redirect us to the Google auth interaction.
- if (_.contains(["/api/session", "/api/session/"], url)) {
+ if (["/api/session", "/api/session/"].includes(url)) {
return;
}
@@ -65,7 +63,7 @@ init(mainReducers, getRoutes, (store) => {
// received a 403 response
api.on(403, (url) => {
if (NOT_AUTHORIZED_TRIGGERS.some((regex) => regex.test(url))) {
- return store.dispatch(setErrorPage({ status: 403 }));
+ store.dispatch(setErrorPage({ status: 403 }));
}
});
});
diff --git a/frontend/src/metabase/app.js b/frontend/src/metabase/app.tsx
similarity index 84%
rename from frontend/src/metabase/app.js
rename to frontend/src/metabase/app.tsx
index 6246eb54ee4..4b6ebd755a1 100644
--- a/frontend/src/metabase/app.js
+++ b/frontend/src/metabase/app.tsx
@@ -23,6 +23,7 @@ import "metabase/plugins/builtin";
// Set CSP nonce for dynamic style injection (e.g. CodeMirror)
import "metabase/utils/csp";
+import { type Middleware, isAction } from "@reduxjs/toolkit";
import { DragDropContextProvider } from "react-dnd";
import { createPortal } from "react-dom";
import { createRoot } from "react-dom/client";
@@ -37,7 +38,13 @@ import { initializeInteractiveEmbedding } from "metabase/embedding/interactive-e
import { MetabotProvider } from "metabase/metabot/context";
import { PLUGIN_APP_INIT_FUNCTIONS } from "metabase/plugins";
import { MetabaseReduxProvider } from "metabase/redux";
-import { LOCATION_CHANGE } from "metabase/router";
+import {
+ LOCATION_CHANGE,
+ type Location,
+ type RouteObject,
+ RouterProvider,
+ createLocationMirror,
+} from "metabase/router";
import { getUserId } from "metabase/selectors/user";
import { refetchSiteSettings } from "metabase/settings";
import { GlobalStyles } from "metabase/styled-components/containers/GlobalStyles";
@@ -50,7 +57,6 @@ import { initTracing, rotateTraceId } from "metabase/utils/otel";
import MetabaseSettings from "metabase/utils/settings";
import { registerVisualizations } from "metabase/visualizations/register";
-import { RouterProvider, createLocationMirror } from "./router";
import { getStore } from "./store";
import { OverlayStackProvider } from "./ui/components/overlays/overlay-stack";
@@ -58,17 +64,29 @@ setBasename(window.MetabaseRoot);
initializePlugins();
-function _init(reducers, getRoutes, callback) {
+type Store = ReturnType<typeof getStore>;
+
+function isLocationChangeAction(
+ action: unknown,
+): action is { type: typeof LOCATION_CHANGE; payload?: Location } {
+ return isAction(action) && action.type === LOCATION_CHANGE;
+}
+
+function _init(
+ reducers: Parameters<typeof getStore>[0],
+ getRoutes: (store: Store) => RouteObject[],
+ callback?: (store: Store) => void,
+) {
// Initialize distributed tracing if enabled via MB_TRACING_ENABLED.
// Uses bootstrap data so it's available before the first API call.
- const extraMiddlewares = [];
+ const extraMiddlewares: Middleware[] = [];
if (window.MetabaseBootstrap?.["tracing-enabled"]) {
initTracing();
// Rotate trace ID on route changes so all API calls within a single page
// view share one trace. The router emits LOCATION_CHANGE on navigation.
- let lastPathname;
+ let lastPathname: string | undefined;
extraMiddlewares.push(() => (next) => (action) => {
- if (action?.type === LOCATION_CHANGE) {
+ if (isLocationChangeAction(action)) {
const pathname = action.payload?.pathname;
if (pathname !== lastPathname) {
lastPathname = pathname;
@@ -90,7 +108,11 @@ function _init(reducers, getRoutes, callback) {
initializeInteractiveEmbedding(store.dispatch);
- const root = createRoot(document.getElementById("root"));
+ const rootElement = document.getElementById("root");
+ if (!rootElement) {
+ throw new Error("no #root element found");
+ }
+ const root = createRoot(rootElement);
root.render(
<MetabaseReduxProvider store={store}>
@@ -134,7 +156,7 @@ function _init(reducers, getRoutes, callback) {
}
}
-export function init(...args) {
+export function init(...args: Parameters<typeof _init>) {
if (document.readyState !== "loading") {
_init(...args);
} else {
diff --git a/frontend/src/metabase/store.ts b/frontend/src/metabase/store.ts
index 044f81bf9d6..0fe2bbfce3b 100644
--- a/frontend/src/metabase/store.ts
+++ b/frontend/src/metabase/store.ts
@@ -1,6 +1,7 @@
import {
type Middleware,
type Reducer,
+ type ReducersMapObject,
combineReducers,
configureStore,
} from "@reduxjs/toolkit";
@@ -17,12 +18,12 @@ export function getStore(
initialState?: Partial<State> | Record<string, unknown>,
extraMiddlewares: Middleware[] = [],
) {
- // The slice map is dynamic (each app passes its own), so the combined state
- // is typed as a plain record rather than one app's State.
- const reducerMap: Record<string, AnySliceReducer> = {
+ // Public/embed/SDK entries pass a subset of the slices, so this overstates
+ // their state; `app.tsx` only reads slices common to every map.
+ const reducerMap = {
...reducers,
[Api.reducerPath]: Api.reducer,
- };
+ } as ReducersMapObject<State>;
const middlewares: Middleware[] = [
Api.middleware,
diff --git a/frontend/src/types/global.d.ts b/frontend/src/types/global.d.ts
index ca62958fe5f..3a351fbc7d1 100644
--- a/frontend/src/types/global.d.ts
+++ b/frontend/src/types/global.d.ts
@@ -23,6 +23,14 @@ interface Window {
overrideIsWithinIframe?: boolean; // Mock that we're embedding, so we could test embed components
METABASE?: boolean; // Add a global so we can check if the parent iframe is Metabase
+ Metabase?: {
+ // Set by the inline asset_loading_error script, before the bundle runs
+ AssetErrorLoad?: (tag: HTMLScriptElement) => void;
+ // Console debugging handles, set on app boot
+ store?: unknown;
+ settings?: unknown;
+ };
+
// Dev-only helpers for inspecting the current query from CLJS REPLs
__lib_metadata?: unknown;
__lib_query?: unknown;
diff --git a/frontend/src/types/metabase-dev.d.ts b/frontend/src/types/metabase-dev.d.ts
new file mode 100644
index 00000000000..29c7c9b6722
--- /dev/null
+++ b/frontend/src/types/metabase-dev.d.ts
@@ -0,0 +1,3 @@
+// Bundler alias: `metabase/dev` in dev mode, `metabase/dev-noop` in prod.
+// Empty body: both targets are side-effect only and export nothing.
+declare module "metabase-dev" {}
diff --git a/rspack.main.config.js b/rspack.main.config.js
index f08787c009b..02d1c094fdd 100644
--- a/rspack.main.config.js
+++ b/rspack.main.config.js
@@ -126,7 +126,7 @@ const config = {
// output a bundle for the app JS and a bundle for styles
// eventually we should have multiple (single file) entry points for various pieces of the app to enable code splitting
entry: {
- "app-main": "./app-main.js",
+ "app-main": "./app-main.ts",
"app-public": "./app-public.ts",
"app-embed": "./app-embed.ts",
"app-embed-sdk": "./app-embed-sdk.tsx",
diff --git a/tsconfig.json b/tsconfig.json
index e0256df710e..9cc826965c3 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -15,6 +15,7 @@
"cljs/*": ["./target/cljs_dev/*"],
"echarts/types/src/*": ["./node_modules/echarts/types/src/*"],
"zrender/lib/core/types": ["./node_modules/zrender/lib/core/types"],
+ "ee-plugins": ["./frontend/src/metabase/plugins/noop"],
"sdk-ee-plugins": ["./frontend/src/metabase/plugins/noop"],
"sdk-specific-imports": ["./frontend/src/metabase/utils/noop"],
"sdk-iframe-embedding-ee-plugins": [
@@ -59,11 +60,6 @@
"enterprise/frontend/src/custom-viz",
// Vendored custom-viz plugin bundles (pre-built UMD) — opaque to
// type-checking; the surrounding story/helper handles the typing.
- "frontend/test/__support__/custom-viz-fixtures/**/*.js",
- // The following files will load `frontend/src/metabase/app` which will conflict with
- // `frontend/src/metabase/AppComponent.tsx`, since now we're loading both JS and TS files.
- "frontend/src/metabase/app-main.js",
- "frontend/src/metabase/app-public.ts",
- "frontend/src/metabase/app-embed.ts"
+ "frontend/test/__support__/custom-viz-fixtures/**/*.js"
]
}