Skip to content

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

BOT-1927: gate audit-view chart queries behind a Run query button (#81215)

Andrei Simionescue34e6190c6cb
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.tsx
index 7fb33e0ce97..b7748db81cc 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.tsx
@@ -1,4 +1,4 @@
-import { useClipboard } from "@mantine/hooks";
+import { useClipboard, useDisclosure } from "@mantine/hooks";
import { useMemo, useState } from "react";
import { P, match } from "ts-pattern";
import { t } from "ttag";
@@ -44,7 +44,8 @@ import S from "./MetabotInlineChart.module.css";
/**
* Renders a Metabot-generated `card` entity as a live, read-only chart inline in
* the conversation: it runs the card's embedded query ad-hoc and renders the
- * result; the title bar links out to the full question.
+ * result, in readonly mode only once Run query is clicked; the title bar links
+ * out to the full question.
*/
export function MetabotInlineChart({
value,
@@ -109,7 +110,11 @@ export function MetabotInlineChart({
[question, savedCardId, value],
);
- const { data: dataset, error } = useGetAdhocQueryQuery(datasetQuery);
+ const [isRunRequested, { open: requestRun }] = useDisclosure(false);
+ const shouldRunQuery = !readonly || isRunRequested;
+ const { data: dataset, error } = useGetAdhocQueryQuery(
+ shouldRunQuery ? datasetQuery : skipToken,
+ );
const rawSeries = useMemo(
() => (dataset ? [{ card, data: dataset.data }] : null),
@@ -155,7 +160,17 @@ export function MetabotInlineChart({
/>
</Flex>
<Box className={S.viz}>
- {chartError ? (
+ {!shouldRunQuery ? (
+ <Center h="100%" p="lg">
+ <Button
+ variant="filled"
+ leftSection={<Icon name="play_outlined" aria-hidden />}
+ onClick={requestRun}
+ >
+ {t`Run query`}
+ </Button>
+ </Center>
+ ) : chartError ? (
<Center h="100%" p="lg">
<ErrorView error={chartError.message} icon={chartError.icon} />
</Center>
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.unit.spec.tsx
index 06f5924b473..6f1aa9bcff5 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.unit.spec.tsx
@@ -111,6 +111,17 @@ describe("MetabotInlineChart", () => {
expect(fetchMock.callHistory.called("path:/api/dataset")).toBe(true);
});
+ it("waits for Run query to be clicked before running in readonly mode", async () => {
+ setup({}, {}, { readonly: true });
+ expect(fetchMock.callHistory.called("path:/api/dataset")).toBe(false);
+ expect(screen.queryByTestId("visualization")).not.toBeInTheDocument();
+
+ await userEvent.click(screen.getByRole("button", { name: "Run query" }));
+
+ expect(await screen.findByTestId("visualization")).toBeInTheDocument();
+ expect(fetchMock.callHistory.called("path:/api/dataset")).toBe(true);
+ });
+
it("shows the title as a link", () => {
setup();
expect(screen.getByText("Orders by month")).toHaveAttribute(