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
BOT-1953: Suppress the Metabot sidebar on the conversation route (#79574)
Andrei Simionescue9d98d62c3fe
diff --git a/frontend/src/metabase/metabot/components/Metabot.tsx b/frontend/src/metabase/metabot/components/Metabot.tsxindex f438178c25a..0b798602d38 100644--- a/frontend/src/metabase/metabot/components/Metabot.tsx+++ b/frontend/src/metabase/metabot/components/Metabot.tsx@@ -7,7 +7,7 @@ import ErrorBoundary from "metabase/ErrorBoundary";import { metabotApi } from "metabase/api";import { idTag } from "metabase/api/tags";import {- useIsAskPage,+ useIsFullPageMetabot,useMetabotAgent,useUserMetabotPermissions,} from "metabase/metabot/hooks";@@ -124,7 +124,7 @@ export const MetabotAuthenticated = ({ hide, config }: MetabotProps) => {const agentId = config?.agentId ?? "omnibot";const { visible, setVisible } = useMetabotAgent(agentId);const [errorBoundaryKey, setErrorBoundaryKey] = useState(0);- const isAskPage = useIsAskPage();+ const isFullPageMetabot = useIsFullPageMetabot();const handleRetry = () => setErrorBoundaryKey((prev) => prev + 1);@@ -132,7 +132,7 @@ export const MetabotAuthenticated = ({ hide, config }: MetabotProps) => {return tinykeys(window, {"$mod+e": (e) => {e.preventDefault(); // prevent FF from opening bookmark menu- if (isAskPage) {+ if (isFullPageMetabot) {return;}if (!visible) {@@ -141,7 +141,7 @@ export const MetabotAuthenticated = ({ hide, config }: MetabotProps) => {setVisible(!visible);},});- }, [visible, setVisible, isAskPage]);+ }, [visible, setVisible, isFullPageMetabot]);useEffect(function closeViaPropChange() {diff --git a/frontend/src/metabase/metabot/components/MetabotAppBarButton.tsx b/frontend/src/metabase/metabot/components/MetabotAppBarButton.tsxindex ea815db5e10..1fba187d6b0 100644--- a/frontend/src/metabase/metabot/components/MetabotAppBarButton.tsx+++ b/frontend/src/metabase/metabot/components/MetabotAppBarButton.tsx@@ -1,7 +1,7 @@import { t } from "ttag";import {- useIsAskPage,+ useIsFullPageMetabot,useMetabotAgent,useUserMetabotPermissions,} from "metabase/metabot/hooks";@@ -24,7 +24,7 @@ export function MetabotAppBarButton({const { hasMetabotAccess } = useUserMetabotPermissions();const metabot = useMetabotAgent("omnibot");const metabotName = useSetting("metabot-name");- const isAskPage = useIsAskPage();+ const isFullPageMetabot = useIsFullPageMetabot();if (!hasMetabotAccess) {return null;@@ -45,8 +45,8 @@ export function MetabotAppBarButton({<ActionIconclassName={className}variant="subtle"- c={isAskPage ? "text-disabled" : "text-primary"}- opacity={isAskPage ? 0.5 : undefined}+ c={isFullPageMetabot ? "text-disabled" : "text-primary"}+ opacity={isFullPageMetabot ? 0.5 : undefined}bd="1px solid var(--mb-color-border-neutral)"p="sm"h="2.25rem"@@ -54,7 +54,7 @@ export function MetabotAppBarButton({aria-label={label}onClick={handleClick}{...rest}- disabled={isAskPage}+ disabled={isFullPageMetabot}><MetabotIcon /></ActionIcon>diff --git a/frontend/src/metabase/metabot/components/MetabotAppBarButton.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotAppBarButton.unit.spec.tsxindex e7010261e75..03cc7d87635 100644--- a/frontend/src/metabase/metabot/components/MetabotAppBarButton.unit.spec.tsx+++ b/frontend/src/metabase/metabot/components/MetabotAppBarButton.unit.spec.tsx@@ -88,12 +88,19 @@ describe("MetabotAppBarButton", () => {).not.toBeInTheDocument();});- it("should disable the button on the full-page AI exploration (/question/ask) surface", async () => {- setup({ isMetabotEnabled: true, pathname: "/question/ask" });- expect(- await screen.findByRole("button", { name: /Chat with Metabot/ }),- ).toBeDisabled();- });+ it.each([+ "/question/ask",+ "/question/ask/",+ "/metabot/conversation/past-conversation-id",+ ])(+ "should disable the button on the full-page metabot surface (%s)",+ async (pathname) => {+ setup({ isMetabotEnabled: true, pathname });+ expect(+ await screen.findByRole("button", { name: /Chat with Metabot/ }),+ ).toBeDisabled();+ },+ );it("should not disable the button on other question pages", async () => {setup({ isMetabotEnabled: true, pathname: "/question/123" });diff --git a/frontend/src/metabase/metabot/hooks/index.ts b/frontend/src/metabase/metabot/hooks/index.tsindex 44b4ca0aec6..01ef67d1087 100644--- a/frontend/src/metabase/metabot/hooks/index.ts+++ b/frontend/src/metabase/metabot/hooks/index.ts@@ -1,4 +1,5 @@export { useIsAskPage } from "./use-is-ask-page";+export { useIsFullPageMetabot } from "./use-is-full-page-metabot";export { useMetabotEnabledEmbeddingAware } from "./use-metabot-embedding-aware-enabled";export { useBranchableMessages } from "./use-branchable-messages";export { useMetabotAgent } from "./use-metabot-agent";diff --git a/frontend/src/metabase/metabot/hooks/use-is-ask-page.ts b/frontend/src/metabase/metabot/hooks/use-is-ask-page.tsindex f24d48891f8..1e479cf640d 100644--- a/frontend/src/metabase/metabot/hooks/use-is-ask-page.ts+++ b/frontend/src/metabase/metabot/hooks/use-is-ask-page.ts@@ -1,9 +1,12 @@-import { useMaybeLocation } from "metabase/router";+import { matchPath, useMaybeLocation } from "metabase/router";import * as Urls from "metabase/urls";export function useIsAskPage() {// Metabot also renders outside the app router (the SDK, and component tests// that mount it bare), where there is no ask page.const location = useMaybeLocation();- return location?.pathname === Urls.newQuestion({ mode: "ask" });+ return (+ matchPath(Urls.newQuestion({ mode: "ask" }), location?.pathname ?? "") !==+ null+ );}diff --git a/frontend/src/metabase/metabot/hooks/use-is-full-page-metabot.ts b/frontend/src/metabase/metabot/hooks/use-is-full-page-metabot.tsnew file mode 100644index 00000000000..6181f7bf843--- /dev/null+++ b/frontend/src/metabase/metabot/hooks/use-is-full-page-metabot.ts@@ -0,0 +1,17 @@+import { matchPath, useMaybeLocation } from "metabase/router";+import * as Urls from "metabase/urls";++import { useIsAskPage } from "./use-is-ask-page";++export function useIsFullPageMetabot() {+ const isAskPage = useIsAskPage();+ const location = useMaybeLocation();++ return (+ isAskPage ||+ matchPath(+ `/${Urls.CONVERSATION_BASE_PATH}/:convoId`,+ location?.pathname ?? "",+ ) !== null+ );+}diff --git a/frontend/src/metabase/metabot/routes.tsx b/frontend/src/metabase/metabot/routes.tsxindex 1bcdef4631e..9136b597d50 100644--- a/frontend/src/metabase/metabot/routes.tsx+++ b/frontend/src/metabase/metabot/routes.tsx@@ -1,4 +1,5 @@import { Route } from "metabase/router";+import * as Urls from "metabase/urls";import { MetabotConversationPage } from "./components/MetabotConversationPage";import { getMetabotQuickLinks } from "./components/MetabotQuickLinks";@@ -9,7 +10,7 @@ export const getMetabotRoutes = () => {<>{getMetabotQuickLinks()}<Route- path="metabot/conversation/:convoId"+ path={`${Urls.CONVERSATION_BASE_PATH}/:convoId`}element={<MetabotConversationPage />}/><Route path="slack-connect-success" element={<SlackConnectSuccess />} />diff --git a/frontend/src/metabase/metabot/tests/ui.unit.spec.tsx b/frontend/src/metabase/metabot/tests/ui.unit.spec.tsxindex e3e38a29868..8558788bf79 100644--- a/frontend/src/metabase/metabot/tests/ui.unit.spec.tsx+++ b/frontend/src/metabase/metabot/tests/ui.unit.spec.tsx@@ -8,7 +8,7 @@ import {setupGetMetabotConversationEndpoint,setupGetMetabotConversationEndpointError,} from "__support__/server-mocks";-import { act, screen, waitFor, within } from "__support__/ui";+import { act, fireEvent, screen, waitFor, within } from "__support__/ui";import { LONG_CONVO_MSG_LENGTH_THRESHOLD } from "metabase/metabot/constants";import { useMetabotAgent } from "metabase/metabot/hooks";import { metabotActions } from "metabase/metabot/state";@@ -127,6 +127,44 @@ describe("metabot > ui", () => {await assertNotVisible();});+ describe("keyboard shortcut", () => {+ // jsdom reports an empty navigator.platform, so tinykeys binds $mod to Control.+ const pressShortcut = () =>+ fireEvent.keyDown(window, { key: "e", ctrlKey: true });++ it("should toggle visibility", async () => {+ const { store } = setup({+ withRouter: true,+ initialRoute: "/question/123",+ });+ expect(await chat()).toBeInTheDocument();++ hideMetabot(store.dispatch);+ await assertNotVisible();++ pressShortcut();+ expect(await chat()).toBeInTheDocument();+ });++ it.each([+ "/question/ask",+ "/question/ask/",+ "/metabot/conversation/past-conversation-id",+ ])(+ "should do nothing on the full-page metabot surface (%s)",+ async (initialRoute) => {+ const { store } = setup({ withRouter: true, initialRoute });+ expect(await chat()).toBeInTheDocument();++ hideMetabot(store.dispatch);+ await assertNotVisible();++ pressShortcut();+ await assertNotVisible();+ },+ );+ });+it("should be able to hide metabot via a prop", async () => {const { rerender } = setup();await assertVisible();diff --git a/frontend/src/metabase/metabot/tests/utils.tsx b/frontend/src/metabase/metabot/tests/utils.tsxindex b91b8630ffa..d34dd62af22 100644--- a/frontend/src/metabase/metabot/tests/utils.tsx+++ b/frontend/src/metabase/metabot/tests/utils.tsx@@ -156,7 +156,6 @@ export const assertNotVisible = async () =>expect(screen.queryByTestId("metabot-chat")).not.toBeInTheDocument();});-// NOTE: for some reason the keyboard shortcuts won't work with tinykeys while testing, using redux for now...export const hideMetabot = (dispatch: any,agentId: MetabotAgentId = "omnibot",diff --git a/frontend/src/metabase/router/index.ts b/frontend/src/metabase/router/index.tsindex 41903b07649..c0982afb8f9 100644--- a/frontend/src/metabase/router/index.ts+++ b/frontend/src/metabase/router/index.ts@@ -3,6 +3,7 @@ export {NavLink,type NavLinkProps,type NavLinkRenderProps,+ matchPath,Navigate,Outlet,Route,diff --git a/frontend/src/metabase/urls/metabot.ts b/frontend/src/metabase/urls/metabot.tsindex eb85f03f46e..d36ae39f232 100644--- a/frontend/src/metabase/urls/metabot.ts+++ b/frontend/src/metabase/urls/metabot.ts@@ -34,8 +34,10 @@ export function generatedEntity(entity: GeneratedEntity) {}}+export const CONVERSATION_BASE_PATH = "metabot/conversation";+export function metabotConversation(conversationId: string) {- return `/metabot/conversation/${conversationId}`;+ return `/${CONVERSATION_BASE_PATH}/${conversationId}`;}export type ConversationChart = {