Skip to content

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

Require free text for ui-bug and other feedback types (#68234)

Thomas Schmidtba7c5d1fc70a
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotFeedbackModal.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotFeedbackModal.tsx
index 613a9aee7e3..d99724cd5a7 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotFeedbackModal.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotFeedbackModal.tsx
@@ -1,9 +1,12 @@
+import { useFormikContext } from "formik";
import { c, t } from "ttag";
+import * as Yup from "yup";
import { useSetting } from "metabase/common/hooks";
import { Form, FormProvider } from "metabase/forms";
import { FormSelect } from "metabase/forms/components/FormSelect";
import { FormTextarea } from "metabase/forms/components/FormTextarea";
+import * as Errors from "metabase/lib/errors";
import { useSelector } from "metabase/lib/redux";
import { getUserIsAdmin } from "metabase/selectors/user";
import { getApplicationName } from "metabase/selectors/whitelabel";
@@ -15,6 +18,25 @@ import {
} from "metabase-enterprise/metabot/state";
import type { MetabotFeedback } from "metabase-types/api";
+// Issue types that require free text feedback
+const ISSUE_TYPES_REQUIRING_FREEFORM = ["ui-bug", "other"] as const;
+type IssueTypesRequiringFreeform =
+ (typeof ISSUE_TYPES_REQUIRING_FREEFORM)[number];
+
+const isFreeformRequired = (
+ value: string,
+): value is IssueTypesRequiringFreeform =>
+ ISSUE_TYPES_REQUIRING_FREEFORM.includes(value as IssueTypesRequiringFreeform);
+
+const FEEDBACK_SCHEMA = Yup.object({
+ issue_type: Yup.string().nullable().default(""),
+ freeform_feedback: Yup.string().when("issue_type", {
+ is: isFreeformRequired,
+ then: (schema) => schema.required(Errors.required),
+ otherwise: (schema) => schema.nullable(),
+ }),
+});
+
interface MetabotFeedbackModalProps {
onClose: () => void;
onSubmit: (feedback: MetabotFeedback) => void;
@@ -22,6 +44,21 @@ interface MetabotFeedbackModalProps {
positive: boolean;
}
+const FeedbackTextLabel = ({ positive }: { positive: boolean }) => {
+ const { values } = useFormikContext<Yup.InferType<typeof FEEDBACK_SCHEMA>>();
+
+ const isRequired =
+ !positive && values.issue_type && isFreeformRequired(values.issue_type);
+
+ return (
+ <Text>
+ {isRequired
+ ? t`Any details that you'd like to share? (required)`
+ : t`Any details that you'd like to share? (optional)`}
+ </Text>
+ );
+};
+
export const MetabotFeedbackModal = ({
onClose,
onSubmit,
@@ -67,6 +104,7 @@ export const MetabotFeedbackModal = ({
issue_type: positive ? undefined : "",
freeform_feedback: "",
}}
+ validationSchema={FEEDBACK_SCHEMA}
onSubmit={handleSubmit}
>
<Form>
@@ -99,7 +137,7 @@ export const MetabotFeedbackModal = ({
</Stack>
)}
<Stack gap="xs">
- <Text>{t`Any details that you'd like to share? (optional)`}</Text>
+ <FeedbackTextLabel positive={positive} />
<FormTextarea
name="freeform_feedback"
placeholder={
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/tests/feedback.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/metabot/tests/feedback.unit.spec.ts
new file mode 100644
index 00000000000..8902fe0a7d5
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/tests/feedback.unit.spec.ts
@@ -0,0 +1,115 @@
+import userEvent from "@testing-library/user-event";
+
+import { screen, within } from "__support__/ui";
+
+import {
+ setup as defaultSetup,
+ enterChatMessage,
+ feedbackModal,
+ lastChatMessage,
+ mockAgentEndpoint,
+ mockFeedbackEndpoint,
+ thumbsDown,
+ thumbsUp,
+ whoIsYourFavoriteResponse,
+} from "./utils";
+
+const setup = async () => {
+ defaultSetup();
+ const feedbackEndpoint = mockFeedbackEndpoint();
+ mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+
+ await enterChatMessage("Who is your favorite?");
+ const lastMessage = (await lastChatMessage())!;
+
+ await userEvent.click(await thumbsDown(lastMessage));
+ const modal = await feedbackModal();
+
+ return { feedbackEndpoint, lastMessage, modal };
+};
+
+const selectIssueType = async (modal: HTMLElement, issueType: string) => {
+ await userEvent.click(
+ await within(modal).findByPlaceholderText("Select issue type"),
+ );
+ await userEvent.click(await screen.findByText(issueType));
+};
+
+const typeFeedback = async (modal: HTMLElement, text: string) => {
+ await userEvent.type(
+ await within(modal).findByPlaceholderText(
+ "What could be improved about this response?",
+ ),
+ text,
+ );
+};
+
+const submitFeedback = async (modal: HTMLElement) => {
+ await userEvent.click(
+ await within(modal).findByRole("button", { name: /Submit/ }),
+ );
+};
+
+describe("metabot > feedback", () => {
+ it("should present the user an option to provide feedback", async () => {
+ defaultSetup();
+ const feedbackEndpoint = mockFeedbackEndpoint();
+ mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+
+ await enterChatMessage("Who is your favorite?");
+ const lastMessage = (await lastChatMessage())!;
+ expect(lastMessage).toHaveTextContent(/You, but don't tell anyone./);
+
+ expect(await thumbsUp(lastMessage)).toBeInTheDocument();
+ expect(await thumbsDown(lastMessage)).toBeInTheDocument();
+ await userEvent.click(await thumbsDown(lastMessage));
+
+ const modal = await feedbackModal();
+ expect(modal).toBeInTheDocument();
+ await submitFeedback(modal);
+
+ expect(feedbackEndpoint.calls()).toHaveLength(1);
+
+ expect(await thumbsUp(lastMessage)).toBeDisabled();
+ expect(await thumbsDown(lastMessage)).toBeDisabled();
+ });
+
+ it("should prevent submission when ui-bug is selected with empty feedback", async () => {
+ const { feedbackEndpoint, modal } = await setup();
+
+ await selectIssueType(modal, "UI bug");
+ await submitFeedback(modal);
+
+ expect(await within(modal).findByText("required")).toBeInTheDocument();
+ expect(feedbackEndpoint.calls()).toHaveLength(0);
+ });
+
+ it("should allow submission when ui-bug is selected with feedback text", async () => {
+ const { feedbackEndpoint, modal } = await setup();
+
+ await selectIssueType(modal, "UI bug");
+ await typeFeedback(modal, "The button is in the wrong place");
+ await submitFeedback(modal);
+
+ expect(feedbackEndpoint.calls()).toHaveLength(1);
+ });
+
+ it("should prevent submission when other is selected with empty feedback", async () => {
+ const { feedbackEndpoint, modal } = await setup();
+
+ await selectIssueType(modal, "Other");
+ await submitFeedback(modal);
+
+ expect(await within(modal).findByText("required")).toBeInTheDocument();
+ expect(feedbackEndpoint.calls()).toHaveLength(0);
+ });
+
+ it("should allow submission when non-required issue types have empty feedback", async () => {
+ const { feedbackEndpoint, modal } = await setup();
+
+ await selectIssueType(modal, "Not factually correct");
+ await submitFeedback(modal);
+
+ expect(feedbackEndpoint.calls()).toHaveLength(1);
+ });
+});
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/tests/ui.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/tests/ui.unit.spec.tsx
index a020ed6dd7b..80e3819e42c 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/tests/ui.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/tests/ui.unit.spec.tsx
@@ -144,40 +144,6 @@ describe("metabot > ui", () => {
});
});
- it("should present the user an option to provide feedback", async () => {
- const feedbackPath = "path:/api/ee/metabot-v3/feedback";
-
- setup();
- fetchMock.post(feedbackPath, 204);
- mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
-
- await enterChatMessage("Who is your favorite?");
- const lastMessage = await lastChatMessage();
- expect(lastMessage).toHaveTextContent(/You, but don't tell anyone./);
-
- const feedbackModal = () => screen.findByTestId("metabot-feedback-modal");
- const thumbsUp = () =>
- within(lastMessage!).findByTestId("metabot-chat-message-thumbs-up");
- const thumbsDown = () =>
- within(lastMessage!).findByTestId("metabot-chat-message-thumbs-down");
-
- expect(await thumbsUp()).toBeInTheDocument();
- expect(await thumbsDown()).toBeInTheDocument();
- await userEvent.click(await thumbsDown());
-
- expect(await feedbackModal()).toBeInTheDocument();
- await userEvent.click(
- await within(await feedbackModal()).findByRole("button", {
- name: /Submit/,
- }),
- );
-
- expect(fetchMock.callHistory.calls(feedbackPath)).toHaveLength(1);
-
- expect(await thumbsUp()).toBeDisabled();
- expect(await thumbsDown()).toBeDisabled();
- });
-
it("should present the user an option to retry a response", async () => {
setup();
mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/tests/utils.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/tests/utils.tsx
index d7f6eef1a4f..0890fa08cfb 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/tests/utils.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/tests/utils.tsx
@@ -12,6 +12,7 @@ import {
renderWithProviders,
screen,
waitFor,
+ within,
} from "__support__/ui";
import {
type MockStreamedEndpointParams,
@@ -72,6 +73,21 @@ export const responseLoader = () =>
screen.findByTestId("metabot-response-loader");
export const resetChatButton = () => screen.findByTestId("metabot-reset-chat");
+// Feedback helpers
+export const feedbackModal = () =>
+ screen.findByTestId("metabot-feedback-modal");
+export const thumbsUp = (message: HTMLElement) =>
+ within(message).findByTestId("metabot-chat-message-thumbs-up");
+export const thumbsDown = (message: HTMLElement) =>
+ within(message).findByTestId("metabot-chat-message-thumbs-down");
+export const mockFeedbackEndpoint = () => {
+ const path = "path:/api/ee/metabot-v3/feedback";
+ fetchMock.post(path, 204);
+ return {
+ calls: () => fetchMock.callHistory.calls(path),
+ };
+};
+
export const assertVisible = async () =>
expect(await screen.findByTestId("metabot-chat")).toBeInTheDocument();
export const assertNotVisible = async () =>