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
Delete the Route translation layer (#79151)
Romeo Van Snick947c11579be4
diff --git a/frontend/src/metabase/router/route.tsx b/frontend/src/metabase/router/route.tsxindex ea8ed25653b..b7c987d2b52 100644--- a/frontend/src/metabase/router/route.tsx+++ b/frontend/src/metabase/router/route.tsx@@ -1,31 +1,11 @@-import type { ComponentClass, ReactNode } from "react";--import type { RouteProps } from "./types";-/**- * Props accepted by the `<Route>` element: react-router v7's `index` and- * `element` on top of the shared route props. `index` registers the route as its- * parent's index route.- */-export type RouteElementProps = Omit<RouteProps, "component"> & {- index?: boolean;- element?: ReactNode;-};--/**- * The route object type, preserved for the call sites that annotate the injected- * `route` prop with `Route`.- */-export type Route = ComponentClass<RouteProps>;--/**- * react-router v7's `<Route>`. Like v3's it is route configuration and never- * renders: `mapToV7` reads its props and rebuilds the tree as real v7 routes.+ * react-router v7's `<Route>`. It is route configuration and never renders:+ * `createRoutesFromElements` reads its props to build the route tree.+ *+ * The app has authored routes in v7 syntax since the start of the migration, so+ * they now go in as they are, with no translation step. A route can therefore+ * carry any prop v7 reads, `lazy` and `loader` included.** @see https://reactrouter.com/7.18.1/api/components/Route*/-export function Route(_props: RouteElementProps): null {- throw new Error(- "<Route> configures the route tree and is never rendered directly",- );-}+export { Route } from "react-router";diff --git a/frontend/src/metabase/router/route.unit.spec.tsx b/frontend/src/metabase/router/route.unit.spec.tsxindex 8718f7c8b1b..636745b9f11 100644--- a/frontend/src/metabase/router/route.unit.spec.tsx+++ b/frontend/src/metabase/router/route.unit.spec.tsx@@ -123,7 +123,7 @@ describe("router/useRoutePathname", () => {});describe("router/Route element={null}", () => {- it("renders nothing for an explicit null, matching v7 (not v3's render-children)", async () => {+ it("falls back to an <Outlet/>, so a child of a null-element route renders", async () => {renderWithProviders(<Route path="/" element={<ParentPage />}><Route path="empty" element={null}>@@ -133,9 +133,23 @@ describe("router/Route element={null}", () => {{ withRouter: true, initialRoute: "/empty" },);- // The `empty` route matches, so the parent chrome renders. But `element={null}`- // renders nothing and provides no <Outlet/>, so its own index child stays- // hidden. With v3's render-children default the child would show instead.+ // v7 reads a nullish `element` as "this route supplies none" and renders an+ // <Outlet/> in its place, so the index child below it shows. A route that+ // means to render nothing has to say so with an empty fragment.+ expect(await screen.findByText("parent chrome")).toBeInTheDocument();+ expect(await screen.findByText("leaf content")).toBeInTheDocument();+ });++ it("renders nothing for an empty fragment, and provides no outlet", async () => {+ renderWithProviders(+ <Route path="/" element={<ParentPage />}>+ <Route path="empty" element={<></>}>+ <Route index element={<LeafPage />} />+ </Route>+ </Route>,+ { withRouter: true, initialRoute: "/empty" },+ );+expect(await screen.findByText("parent chrome")).toBeInTheDocument();expect(screen.queryByText("leaf content")).not.toBeInTheDocument();});diff --git a/frontend/src/metabase/router/types.ts b/frontend/src/metabase/router/types.tsindex 7a50f0a61f0..07198a7cd6d 100644--- a/frontend/src/metabase/router/types.ts+++ b/frontend/src/metabase/router/types.ts@@ -3,7 +3,6 @@ import type {ComponentClass,FunctionComponent,HTMLProps,- ReactNode,} from "react";/**@@ -168,17 +167,6 @@ export type SetURLSearchParams = (*/export type RouteComponent = ComponentClass<any> | FunctionComponent<any>;-/**- * The `<Route>` configuration props, re-homed from v3's `RouteProps`. The- * lifecycle hooks (`onEnter` / `onChange` / `onLeave`) are intentionally absent:- * they have no v7 equivalent and the app does that work in components now.- */-export interface RouteProps {- children?: ReactNode;- path?: string;- component?: RouteComponent;-}-/*** v3's function form of a `<Link to>`, kept because `RouterLink` still handles it.*/diff --git a/frontend/src/metabase/router/v7/create-router.tsx b/frontend/src/metabase/router/v7/create-router.tsxindex 0a8dd8a0acf..8bf08c06981 100644--- a/frontend/src/metabase/router/v7/create-router.tsx+++ b/frontend/src/metabase/router/v7/create-router.tsx@@ -8,7 +8,6 @@ import {} from "react-router";import { AppShell } from "./AppShell";-import { mapToV7 } from "./map-to-v7";export type MemoryTestRouter = DataRouter;@@ -19,16 +18,14 @@ export type MemoryTestRouter = DataRouter;export type MemoryTestRouterHolder = { current: MemoryTestRouter | null };/**- * The whole facade tree as real data routes, under a pathless layout route.+ * The app's route tree as data routes, under a pathless layout route.** The layout route contributes no path, so it changes no matching. It exists so* relative redux navigation resolves from the root (see `AppShell`) and as the* seat for anything the app shell needs above the route tree.*/function toDataRoutes(tree: ReactNode) {- return createRoutesFromElements(- <Route element={<AppShell />}>{mapToV7(tree)}</Route>,- );+ return createRoutesFromElements(<Route element={<AppShell />}>{tree}</Route>);}export function createAppRouter(diff --git a/frontend/src/metabase/router/v7/map-to-v7.tsx b/frontend/src/metabase/router/v7/map-to-v7.tsxdeleted file mode 100644index d413a71602e..00000000000--- a/frontend/src/metabase/router/v7/map-to-v7.tsx+++ /dev/null@@ -1,58 +0,0 @@-import {- Children,- Fragment,- type ReactElement,- type ReactNode,- isValidElement,-} from "react";-import { Route as V7Route } from "react-router";--import type { RouteElementProps } from "../route";-import { Route } from "../route";--/**- * Rebuild the facade route tree (`<Route path element>` + `<Outlet/>`, authored- * in v7 syntax) into a real react-router v7 route tree for `<Routes>`. Paths are- * already v7-native, so they pass straight through, and elements render as-is.- */-export function mapToV7(node: ReactNode): ReactNode {- return Children.map(node, (child) => {- if (!isValidElement(child)) {- return child;- }-- // Fragments group sibling routes (plugin interpolation, conditionals); descend- // so the facade routes inside them get converted too.- if (child.type === Fragment) {- return <Fragment>{mapToV7(child.props.children)}</Fragment>;- }-- if (child.type === Route) {- // `child.type === Route` guarantees these are the facade Route's props.- return toV7Route(child as ReactElement<RouteElementProps>);- }-- // Anything else in a route position is passed through unchanged; v7's- // `createRoutesFromChildren` validates it.- return child;- });-}--function toV7Route(element: ReactElement<RouteElementProps>): ReactElement {- const { path, index, element: routeElement, children } = element.props;-- // v7 reads `element={null}` as "no element" and falls back to `<Outlet/>`,- // which would render the route's children. The facade keeps the two apart: an- // explicit null renders nothing and provides no outlet.- const routeContent = routeElement === null ? <Fragment /> : routeElement;-- if (index) {- return <V7Route index element={routeContent} />;- }-- return (- <V7Route path={path} element={routeContent}>- {mapToV7(children)}- </V7Route>- );-}