Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ The `editor-wc` tag accepts the following attributes, which must be provided as

The web component communicates with the host page via the following custom events:

- `editor-aiHintLearnMoreLinkClicked`: When the user clicks the "Learn more" link about AI hints - event detail contains the `projectIdentifier` and the `source` of the click (`"errorMessage"` for the link under an AI error hint, `"settings"` for the link in the settings panel)
- `editor-aiHintRequested`: When an AI error hint request finishes after the user clicks the hint button (or retries after a failure) - event detail contains the `projectIdentifier`, the `errorDetails` of the error being shown, and the `outcome` (`"shown"` or `"failed"`)
- `editor-aiHintToggled`: When the user turns AI hints on or off in the settings panel - event detail contains the `projectIdentifier` and whether hints are now `enabled`
- `editor-aiHintTurnOffLinkClicked`: When the user clicks the turn off link under an AI error hint, which opens the AI hint settings - event detail contains the `projectIdentifier` and the `errorDetails` of the error being shown
Expand Down
16 changes: 14 additions & 2 deletions src/components/Editor/AiErrorMessage/AiErrorMessage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import BadgeQuestionMarkIcon from "../../../assets/icons/badge_question_mark.svg
import SettingsIcon from "../../../assets/icons/settings.svg";
import { openAiHintSettings } from "../../../redux/EditorSlice";
import ApiCallHandler from "../../../utils/apiCallHandler";
import { aiHintLinkComponents } from "../../../utils/aiHintLinks";
import { getAiHintLinkComponents } from "../../../utils/aiHintLinks";
import {
aiHintLearnMoreLinkClickedEvent,
aiHintRequestedEvent,
aiHintTurnOffLinkClickedEvent,
} from "../../../events/WebComponentCustomEvents";
Expand Down Expand Up @@ -113,6 +114,15 @@ const AiErrorMessage = () => {
);
};

const handleLearnMoreClick = () => {
document.dispatchEvent(
aiHintLearnMoreLinkClickedEvent({
projectIdentifier,
source: "errorMessage",
}),
);
};

return (
<div className="ai-error-message">
{showResult && (
Expand Down Expand Up @@ -145,7 +155,9 @@ const AiErrorMessage = () => {
<p className="ai-error-message__notice">
<Trans
i18nKey="output.aiErrorMessages.notice"
components={aiHintLinkComponents}
components={getAiHintLinkComponents({
onLearnMoreClick: handleLearnMoreClick,
})}
/>
</p>
<button
Expand Down
57 changes: 57 additions & 0 deletions src/components/Editor/AiErrorMessage/AiErrorMessage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@ import { Provider } from "react-redux";
import { act, fireEvent, render, screen } from "@testing-library/react";
import AiErrorMessage, { MIN_LOADING_MS } from "./AiErrorMessage";
import ApiCallHandler from "../../../utils/apiCallHandler";
import { AI_HINT_LEARN_MORE_URL } from "../../../utils/aiHintLinks";

vi.mock("../../../utils/apiCallHandler");

vi.mock("react-i18next", async () => {
const { cloneElement, createElement } = await vi.importActual("react");
return {
useTranslation: () => ({ t: (str) => str }),
Trans: ({ i18nKey, components = {} }) => [
createElement("span", { key: i18nKey }, i18nKey),
...Object.entries(components).map(([name, element]) =>
cloneElement(element, { key: name }, name),
),
],
};
});

const middlewares = [];
const mockStore = configureStore(middlewares);

Expand Down Expand Up @@ -401,4 +415,47 @@ describe("AiErrorMessage", () => {
});
});
});

describe("learn more link", () => {
const learnMoreLink = () => screen.getByRole("link", { name: /learnMore/ });

test("Links to the AI hints help article in a new tab", () => {
renderAiErrorMessage();
expect(learnMoreLink()).toHaveAttribute("href", AI_HINT_LEARN_MORE_URL);
expect(learnMoreLink()).toHaveAttribute("target", "_blank");
});

test("Dispatches the learn more link clicked event", () => {
const handler = vi.fn();
const onLearnMoreLinkClicked = (e) => handler(e.detail);
document.addEventListener(
"editor-aiHintLearnMoreLinkClicked",
onLearnMoreLinkClicked,
);
const state = buildState();
state.editor.project.identifier = "coach-gong-glide";
renderAiErrorMessage(state);
fireEvent.click(learnMoreLink());
document.removeEventListener(
"editor-aiHintLearnMoreLinkClicked",
onLearnMoreLinkClicked,
);
expect(handler).toHaveBeenCalledWith({
projectIdentifier: "coach-gong-glide",
source: "errorMessage",
});
});

test("Does not dispatch the event when the feedback link is clicked", () => {
const handler = vi.fn();
document.addEventListener("editor-aiHintLearnMoreLinkClicked", handler);
renderAiErrorMessage();
fireEvent.click(screen.getByRole("link", { name: /feedback/ }));
document.removeEventListener(
"editor-aiHintLearnMoreLinkClicked",
handler,
);
expect(handler).not.toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { useDispatch, useSelector } from "react-redux";
import { Trans, useTranslation } from "react-i18next";
import { Switch } from "@raspberrypifoundation/design-system-react";
import { clearAiHintToggleFocusRequest } from "../../../../../redux/EditorSlice";
import { aiHintLinkComponents } from "../../../../../utils/aiHintLinks";
import { getAiHintLinkComponents } from "../../../../../utils/aiHintLinks";
import { useUpdateUserSettings } from "../../../../../hooks/useUserSettings";
import { aiHintToggledEvent } from "../../../../../events/WebComponentCustomEvents";
import {
aiHintLearnMoreLinkClickedEvent,
aiHintToggledEvent,
} from "../../../../../events/WebComponentCustomEvents";

const AiHintToggle = () => {
const { t } = useTranslation();
Expand Down Expand Up @@ -34,6 +37,15 @@ const AiHintToggle = () => {
document.dispatchEvent(aiHintToggledEvent({ projectIdentifier, enabled }));
};

const handleLearnMoreClick = () => {
document.dispatchEvent(
aiHintLearnMoreLinkClickedEvent({
projectIdentifier,
source: "settings",
}),
);
};

return (
<div ref={containerRef}>
<Switch
Expand All @@ -50,7 +62,9 @@ const AiHintToggle = () => {
<p id="ai_hint_toggle_description">
<Trans
i18nKey="sidebar.settingsMenu.aiHintDescription"
components={aiHintLinkComponents}
components={getAiHintLinkComponents({
onLearnMoreClick: handleLearnMoreClick,
})}
/>
</p>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ import ApiCallHandler from "../../../../../utils/apiCallHandler";

vi.mock("../../../../../utils/apiCallHandler");

vi.mock("react-i18next", async () => {
const { cloneElement, createElement } = await vi.importActual("react");
return {
useTranslation: () => ({ t: (str) => str }),
Trans: ({ i18nKey, components = {} }) => [
createElement("span", { key: i18nKey }, i18nKey),
...Object.entries(components).map(([name, element]) =>
cloneElement(element, { key: name }, name),
),
],
};
});

const updateUserSettings = vi.fn().mockResolvedValue({ data: {} });

const renderToggle = ({
Expand Down Expand Up @@ -104,6 +117,25 @@ describe("AiHintToggle", () => {
});
});

test("dispatches the learn more link clicked event", () => {
const handler = vi.fn();
const onLearnMoreLinkClicked = (e) => handler(e.detail);
document.addEventListener(
"editor-aiHintLearnMoreLinkClicked",
onLearnMoreLinkClicked,
);
renderToggle();
fireEvent.click(screen.getByRole("link", { name: /learnMore/ }));
document.removeEventListener(
"editor-aiHintLearnMoreLinkClicked",
onLearnMoreLinkClicked,
);
expect(handler).toHaveBeenCalledWith({
projectIdentifier: "coach-gong-glide",
source: "settings",
});
});

test("is not focused when focus has not been requested", () => {
const { toggle } = renderToggle();
expect(toggle).not.toHaveFocus();
Expand Down
3 changes: 3 additions & 0 deletions src/events/WebComponentCustomEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export const themeUpdatedEvent = (detail) =>
export const aiHintTurnOffLinkClickedEvent = (detail) =>
webComponentCustomEvent("editor-aiHintTurnOffLinkClicked", detail);

export const aiHintLearnMoreLinkClickedEvent = (detail) =>
webComponentCustomEvent("editor-aiHintLearnMoreLinkClicked", detail);

export const aiHintToggledEvent = (detail) =>
webComponentCustomEvent("editor-aiHintToggled", detail);

Expand Down
17 changes: 9 additions & 8 deletions src/utils/aiHintLinks.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { useTranslation } from "react-i18next";

// TODO: replace with real URLs once available
export const AI_HINT_LEARN_MORE_URL =
"https://help.editor.raspberrypi.org/hc/en-us";
"https://help.editor.raspberrypi.org/hc/en-us/articles/55620760636564-How-Artificial-Intelligence-AI-hints-work-in-the-Code-Editor";
export const AI_HINT_FEEDBACK_URL =
"https://form.raspberrypi.org/f/code-editor-feedback";
"https://form.raspberrypi.org/f/ai-powered-error-messages-feedback";

const NewTabLink = ({ href, children }) => {
const NewTabLink = ({ href, onClick, children }) => {
const { t } = useTranslation();

return (
<a href={href} target="_blank" rel="noreferrer">
<a href={href} target="_blank" rel="noreferrer" onClick={onClick}>
{children}
{/* Hidden text that warns screen reader users the link opens a new tab */}
<span className="rpf-visually-hidden">
Expand All @@ -21,7 +20,9 @@ const NewTabLink = ({ href, children }) => {
};

// Keys match the named tags in the translation strings
export const aiHintLinkComponents = {
learnMore: <NewTabLink href={AI_HINT_LEARN_MORE_URL} />,
export const getAiHintLinkComponents = ({ onLearnMoreClick } = {}) => ({
learnMore: (
<NewTabLink href={AI_HINT_LEARN_MORE_URL} onClick={onLearnMoreClick} />
),
feedback: <NewTabLink href={AI_HINT_FEEDBACK_URL} />,
};
});
Loading