Skip to content

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

Redirect Metabot admin sub-pages to the index until AI is configured (#73775)

Andrei Chernykh34c40def0754
diff --git a/enterprise/frontend/src/metabase-enterprise/ai-controls/components/RequireMetabotConfigured.tsx b/enterprise/frontend/src/metabase-enterprise/ai-controls/components/RequireMetabotConfigured.tsx
new file mode 100644
index 00000000000..27f9517baec
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/ai-controls/components/RequireMetabotConfigured.tsx
@@ -0,0 +1,29 @@
+import { useLayoutEffect } from "react";
+import { replace } from "react-router-redux";
+
+import { useSetting } from "metabase/common/hooks";
+import { useDispatch } from "metabase/redux";
+
+const FALLBACK_PATH = "/admin/metabot/";
+
+/** Redirects Metabot admin sub-pages to the index until AI is configured. */
+export const RequireMetabotConfigured = ({
+ children,
+}: {
+ children?: React.ReactNode;
+}) => {
+ const isConfigured = useSetting("llm-metabot-configured?");
+ const dispatch = useDispatch();
+
+ useLayoutEffect(() => {
+ if (!isConfigured) {
+ dispatch(replace(FALLBACK_PATH));
+ }
+ }, [isConfigured, dispatch]);
+
+ if (!isConfigured) {
+ return null;
+ }
+
+ return <>{children}</>;
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/ai-controls/components/RequireMetabotConfigured.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/ai-controls/components/RequireMetabotConfigured.unit.spec.tsx
new file mode 100644
index 00000000000..07ebd233891
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/ai-controls/components/RequireMetabotConfigured.unit.spec.tsx
@@ -0,0 +1,48 @@
+import { Route } from "react-router";
+
+import { renderWithProviders, screen } from "__support__/ui";
+import { createMockSettingsState } from "metabase/redux/store/mocks";
+
+import { RequireMetabotConfigured } from "./RequireMetabotConfigured";
+
+const SUB_PAGE_PATH = "/admin/metabot/1/usage-controls/ai-usage-limits";
+const INDEX_PATH = "/admin/metabot/";
+
+function setup({ configured }: { configured: boolean }) {
+ return renderWithProviders(
+ <>
+ <Route component={RequireMetabotConfigured}>
+ <Route
+ path={SUB_PAGE_PATH}
+ component={() => <div>SUB PAGE CONTENT</div>}
+ />
+ </Route>
+ <Route path={INDEX_PATH} component={() => <div>METABOT INDEX</div>} />
+ </>,
+ {
+ withRouter: true,
+ initialRoute: SUB_PAGE_PATH,
+ storeInitialState: {
+ settings: createMockSettingsState({
+ "llm-metabot-configured?": configured,
+ }),
+ },
+ },
+ );
+}
+
+describe("RequireMetabotConfigured", () => {
+ it("redirects to the AI settings index when AI is not configured", async () => {
+ const { history } = setup({ configured: false });
+
+ expect(await screen.findByText("METABOT INDEX")).toBeInTheDocument();
+ expect(history?.getCurrentLocation().pathname).toBe(INDEX_PATH);
+ });
+
+ it("renders the requested sub-page when AI is configured", async () => {
+ const { history } = setup({ configured: true });
+
+ expect(await screen.findByText("SUB PAGE CONTENT")).toBeInTheDocument();
+ expect(history?.getCurrentLocation().pathname).toBe(SUB_PAGE_PATH);
+ });
+});
diff --git a/enterprise/frontend/src/metabase-enterprise/ai-controls/routes.tsx b/enterprise/frontend/src/metabase-enterprise/ai-controls/routes.tsx
index c2709179a7a..e6c4b93256d 100644
--- a/enterprise/frontend/src/metabase-enterprise/ai-controls/routes.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/ai-controls/routes.tsx
@@ -1,5 +1,6 @@
import { Route } from "react-router";
+import { RequireMetabotConfigured } from "./components/RequireMetabotConfigured";
import {
MetabotCustomizationPage,
MetabotCustomizationUpsellPage,
@@ -18,7 +19,7 @@ import { MetabotUsageLimitsPage } from "./pages/MetabotUsageLimitsPage";
export function getAiControlsRoutes() {
return (
- <>
+ <Route component={RequireMetabotConfigured}>
<Route
key="ai-feature-access"
path=":metabotId/usage-controls/ai-feature-access"
@@ -49,7 +50,7 @@ export function getAiControlsRoutes() {
path=":metabotId/system-prompts/sql-generation"
component={SqlGenerationPromptPage}
/>
- </>
+ </Route>
);
}