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
Fetch the AI auditing pages when their route is matched (#79565)
Romeo Van Snick8350e6cc4a04
diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/routes.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/routes.tsxindex 89283dd7ebb..f554c97baab 100644--- a/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/routes.tsx+++ b/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/routes.tsx@@ -1,32 +1,88 @@-import { Route, redirect } from "metabase/router";+import { Route, redirect, registerPagePrefetch } from "metabase/router";+import * as Urls from "metabase/urls";-import { CliAnalyticsPage } from "./cli-analytics/components/CliAnalyticsPage";import {McpAnalyticsAvailabilityLayout,MetabotAnalyticsAvailabilityLayout,} from "./components/AvailabilityLayouts";-import { McpAnalyticsPage } from "./mcp-analytics/components/McpAnalyticsPage";-import { ConversationDetailPage } from "./metabot-analytics/components/ConversationDetailPage";-import { ConversationStatsPage } from "./metabot-analytics/components/ConversationStatsPage";-import { ConversationsPage } from "./metabot-analytics/components/ConversationsPage";-import { MetabotAnalyticsUpsellPage } from "./metabot-analytics/components/MetabotAnalyticsUpsellPage/MetabotAnalyticsUpsellPage";++/**+ * The AI auditing pages, each in its own chunk.+ *+ * The plugin registry assigns these routes on every page load, so whatever they+ * name is in the initial bundle. The pages carry the charting and data grid+ * stack, which no other page needs on first paint.+ *+ * The route shape stays eager, so matching is unchanged. The availability gates+ * above them still decide what renders, but they now decide a tick later: the+ * router resolves a matched route's `lazy` before it commits, which means a page+ * a gate is about to block is fetched anyway. That is a few tens of kilobytes on+ * an admin page whose feature is switched off, in exchange for one splitting+ * mechanism across the app.+ */+const conversationStatsPage = () =>+ import("./metabot-analytics/components/ConversationStatsPage").then(+ ({ ConversationStatsPage }) => ({ Component: ConversationStatsPage }),+ );++const conversationsPage = () =>+ import("./metabot-analytics/components/ConversationsPage").then(+ ({ ConversationsPage }) => ({ Component: ConversationsPage }),+ );++const conversationDetailPage = () =>+ import("./metabot-analytics/components/ConversationDetailPage").then(+ ({ ConversationDetailPage }) => ({ Component: ConversationDetailPage }),+ );++const metabotAnalyticsUpsellPage = () =>+ import("./metabot-analytics/components/MetabotAnalyticsUpsellPage/MetabotAnalyticsUpsellPage").then(+ ({ MetabotAnalyticsUpsellPage }) => ({+ Component: MetabotAnalyticsUpsellPage,+ }),+ );++const mcpAnalyticsPage = () =>+ import("./mcp-analytics/components/McpAnalyticsPage").then(+ ({ McpAnalyticsPage }) => ({ Component: McpAnalyticsPage }),+ );++const cliAnalyticsPage = () =>+ import("./cli-analytics/components/CliAnalyticsPage").then(+ ({ CliAnalyticsPage }) => ({ Component: CliAnalyticsPage }),+ );++/**+ * Hovering a Monitor sidebar link starts the fetch, so the chunk is usually in+ * hand by the time the click lands.+ *+ * `/usage` renders the stats page or the upsell page depending on the license,+ * so hovering it asks for both. The conversation detail page takes the trailing+ * slash, which keeps the link to the list from dragging it in as well.+ */+registerPagePrefetch(Urls.monitorAiAuditingUsage(), conversationStatsPage);+registerPagePrefetch(Urls.monitorAiAuditingUsage(), metabotAnalyticsUpsellPage);+registerPagePrefetch(Urls.monitorAiAuditingConversations(), conversationsPage);+registerPagePrefetch(+ `${Urls.monitorAiAuditingConversations()}/`,+ conversationDetailPage,+);+registerPagePrefetch(Urls.monitorAiAuditingMcp(), mcpAnalyticsPage);+registerPagePrefetch(Urls.monitorAiAuditingCli(), cliAnalyticsPage);export function getAiAuditingRoutes() {return (<><Route index element={redirect("usage")} /><Route element={<MetabotAnalyticsAvailabilityLayout />}>- <Route path="usage" element={<ConversationStatsPage />} />- <Route path="conversations" element={<ConversationsPage />} />- <Route- path="conversations/:convoId"- element={<ConversationDetailPage />}- />+ <Route path="usage" lazy={conversationStatsPage} />+ <Route path="conversations" lazy={conversationsPage} />+ <Route path="conversations/:convoId" lazy={conversationDetailPage} /></Route><Route element={<McpAnalyticsAvailabilityLayout />}>- <Route path="mcp" element={<McpAnalyticsPage />} />+ <Route path="mcp" lazy={mcpAnalyticsPage} /></Route>- <Route path="cli" element={<CliAnalyticsPage />} />+ <Route path="cli" lazy={cliAnalyticsPage} /></>);}@@ -35,11 +91,11 @@ export function getAiAuditingUpsellRoutes() {return (<><Route index element={redirect("usage")} />- <Route path="usage" element={<MetabotAnalyticsUpsellPage />} />+ <Route path="usage" lazy={metabotAnalyticsUpsellPage} /><Route element={<McpAnalyticsAvailabilityLayout />}>- <Route path="mcp" element={<McpAnalyticsPage />} />+ <Route path="mcp" lazy={mcpAnalyticsPage} /></Route>- <Route path="cli" element={<CliAnalyticsPage />} />+ <Route path="cli" lazy={cliAnalyticsPage} /></>);}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/routes.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/routes.unit.spec.tsxindex 9ee3434a32b..05ca8782dc3 100644--- a/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/routes.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/routes.unit.spec.tsx@@ -83,16 +83,18 @@ describe("AI Auditing routes", () => {[Urls.monitorAiAuditingUsage(), "Usage stats page"],["/monitor/ai-auditing/conversations", "Conversations page"],["/monitor/ai-auditing/conversations/42", "Conversation detail page"],- ])("blocks %s when AI features are disabled", (route, pageText) => {+ ])("blocks %s when AI features are disabled", async (route, pageText) => {setup({ route, aiFeaturesEnabled: false });- expect(screen.getByText("AI features are disabled")).toBeInTheDocument();+ expect(+ await screen.findByText("AI features are disabled"),+ ).toBeInTheDocument();expect(screen.queryByText(pageText)).not.toBeInTheDocument();});it.each([false, true])("prioritizes globally disabled AI on the MCP route when upsell is %s",- (upsell) => {+ async (upsell) => {setup({route: "/monitor/ai-auditing/mcp",upsell,@@ -100,7 +102,9 @@ describe("AI Auditing routes", () => {mcpEnabled: false,});- expect(screen.getByText("AI features are disabled")).toBeInTheDocument();+ expect(+ await screen.findByText("AI features are disabled"),+ ).toBeInTheDocument();expect(screen.queryByText("MCP analytics page")).not.toBeInTheDocument();expect(screen.getByRole("link", { name: "Go to AI Settings" }),@@ -108,26 +112,26 @@ describe("AI Auditing routes", () => {},);- it("renders Usage stats at the canonical route", () => {+ it("renders Usage stats at the canonical route", async () => {setup({ route: Urls.monitorAiAuditingUsage() });- expect(screen.getByText("Usage stats page")).toBeInTheDocument();+ expect(await screen.findByText("Usage stats page")).toBeInTheDocument();});- it("renders full Metabot analytics when enabled and configured", () => {+ it("renders full Metabot analytics when enabled and configured", async () => {setup({ route: "/monitor/ai-auditing/conversations" });- expect(screen.getByText("Conversations page")).toBeInTheDocument();+ expect(await screen.findByText("Conversations page")).toBeInTheDocument();});- it("blocks MCP analytics when MCP is disabled", () => {+ it("blocks MCP analytics when MCP is disabled", async () => {setup({ route: "/monitor/ai-auditing/mcp", mcpEnabled: false });- expect(screen.getByText("MCP is disabled")).toBeInTheDocument();+ expect(await screen.findByText("MCP is disabled")).toBeInTheDocument();expect(screen.queryByText("MCP analytics page")).not.toBeInTheDocument();});- it("keeps the license upsell ahead of AI configuration", () => {+ it("keeps the license upsell ahead of AI configuration", async () => {setup({route: Urls.monitorAiAuditingUsage(),upsell: true,@@ -135,7 +139,7 @@ describe("AI Auditing routes", () => {isConfigured: false,});- expect(screen.getByText("Usage stats upsell")).toBeInTheDocument();+ expect(await screen.findByText("Usage stats upsell")).toBeInTheDocument();expect(screen.queryByText("AI features are disabled"),).not.toBeInTheDocument();@@ -143,7 +147,7 @@ describe("AI Auditing routes", () => {it.each([false, true])("renders CLI analytics unconditionally when upsell is %s",- (upsell) => {+ async (upsell) => {setup({route: Urls.monitorAiAuditingCli(),upsell,@@ -152,18 +156,18 @@ describe("AI Auditing routes", () => {mcpEnabled: false,});- expect(screen.getByText("CLI analytics page")).toBeInTheDocument();+ expect(await screen.findByText("CLI analytics page")).toBeInTheDocument();},);- it("applies MCP availability to the upsell route set", () => {+ it("applies MCP availability to the upsell route set", async () => {setup({route: "/monitor/ai-auditing/mcp",upsell: true,mcpEnabled: false,});- expect(screen.getByText("MCP is disabled")).toBeInTheDocument();+ expect(await screen.findByText("MCP is disabled")).toBeInTheDocument();expect(screen.queryByText("MCP analytics page")).not.toBeInTheDocument();});});