Skip to content
Merged
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
9 changes: 6 additions & 3 deletions public/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,14 @@
"errors": {
"interrupted": "Execution interrupted"
},
"errorMessage": {
"title": "Error running your code"
},
"aiErrorMessages": {
"loadingText": "Generating hint",
"buttonText": "Need help? Generate an AI hint",
"buttonText": "Generate an AI hint",
"retryButtonText": "Try again",
"aiGeneratedHint": "AI generated hint",
"aiGeneratedHint": "AI-generated hint",
"errorText": "We were not able to generate a hint, try again later.",
"notice": "AI hints can be wrong. <learnMore>Learn more</learnMore> and share <feedback>feedback</feedback>",
"turnOff": "Turn off AI hints"
Expand Down Expand Up @@ -360,7 +363,7 @@
"charity": "Raspberry Pi Foundation - UK registered charity 1129409",
"settingsMenu": {
"heading": "Settings",
"aiHint": "AI generated hints",
"aiHint": "AI-generated hints",
"aiHintDescription": "When your code has an error, you can generate a hint using AI. Hints do not give you the answer and can be wrong. Learn more about <learnMore>how AI hints work</learnMore> or <feedback>share feedback</feedback>.",
"textSize": "Text size",
"theme": "Theme",
Expand Down
6 changes: 3 additions & 3 deletions src/assets/icons/cancel_FILL.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions src/assets/icons/chevron_down.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 79 additions & 12 deletions src/assets/stylesheets/ErrorMessage.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,96 @@
.error-message {
color: var(--rpf-red-900);
background-color: var(--rpf-red-100);
border-block: 1px solid var(--editor-color-outline);
padding: var(--space-1);
border-block-start: 1px solid var(--rpf-red-200);
display: flex;
flex-direction: column;
min-block-size: 0;
overflow-y: auto;

&__summary {
position: sticky;
inset-block-start: 0;
background-color: var(--rpf-red-100);
z-index: 1;
display: flex;
align-items: center;
justify-content: space-between;
gap: var(--space-1);
padding: var(--space-1);
list-style: none;
cursor: pointer;

&::-webkit-details-marker {
display: none;
}

&:focus-visible {
outline: 3px solid var(--editor-focus-color);
outline-offset: -3px;
}
}

@supports selector(::details-content) {
overflow-y: visible;
details {
display: flex;
flex-direction: column;
min-block-size: 0;

&::details-content {
min-block-size: 0;
overflow-y: auto;
}
}

.error-message__summary {
position: static;
}
}

details &__summary {
border-block-end: 1px solid var(--rpf-red-200);
}

&__title {
display: flex;
align-items: center;
gap: var(--space-1);
margin: 0;
@include typography.style-1;
font-weight: bold;

svg {
flex-shrink: 0;
}
}

&__chevron {
flex-shrink: 0;
fill: var(--rpf-text-primary);
transition: transform 0.2s ease-in-out;
}

details[open] &__chevron {
transform: rotate(180deg);
}

&__content {
padding-block: var(--space-1);
padding-inline: var(--space-1-5);
}

&__error {
padding: 0;
margin: 0;
font-family: inherit;
white-space: pre-wrap;
overflow-wrap: break-word;
font-weight: bold;
@include typography.style-1;
}

&__python {
display: flex;
gap: var(--space-1);
@include typography.style-1;

&--medium {
@include typography.style-1;
}

&--large {
@include typography.style-2;
}
}
}
4 changes: 4 additions & 0 deletions src/assets/stylesheets/SettingsPanel.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
p {
color: var(--rpf-text-secondary);
margin-block: var(--space-0-5) 0;

a {
font-weight: var(--font-weight-bold);
}
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/components/Editor/AiErrorMessage/AiErrorMessage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ const AiErrorMessage = () => {
};
}, [errorDetails]);

// Showing the hint removes the button that had focus, so move focus to the result.
const resultRef = useRef(null);
const showResult = Boolean(hint || failed);

useEffect(() => {
if (showResult) resultRef.current?.focus();
}, [showResult]);

const handleButtonClick = async () => {
const code =
projectCode?.find((c) => c.name === "main" && c.extension === "py")
Expand Down Expand Up @@ -106,11 +114,11 @@ const AiErrorMessage = () => {
};

return (
<div className="ai-error-message" aria-live="polite">
{(hint || failed) && (
<div className="ai-error-message__hint">
<div className="ai-error-message">
{showResult && (
<div className="ai-error-message__hint" ref={resultRef} tabIndex={-1}>
<p className="ai-error-message__hint-heading">
<BadgeQuestionMarkIcon />
<BadgeQuestionMarkIcon aria-hidden="true" />
{t("output.aiErrorMessages.aiGeneratedHint")}
</p>
<p className="ai-error-message__hint-text">
Expand Down
24 changes: 24 additions & 0 deletions src/components/Editor/AiErrorMessage/AiErrorMessage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,30 @@ describe("AiErrorMessage", () => {
).not.toBeInTheDocument();
});

test("Moves focus to the hint when it replaces the button", async () => {
getAiErrorHint.mockResolvedValue({ data: { fem: "A friendly hint" } });
renderAiErrorMessage();
hintButton().focus();
await clickButtonAndWait();

expect(document.querySelector(".ai-error-message__hint")).toHaveFocus();
});

test("Moves focus to the error text when the request fails", async () => {
getAiErrorHint.mockRejectedValue(new Error("Bad gateway"));
renderAiErrorMessage();
hintButton().focus();
await clickButtonAndWait();

expect(document.querySelector(".ai-error-message__hint")).toHaveFocus();
});

test("Does not move focus before a hint is requested", () => {
renderAiErrorMessage();

expect(document.body).toHaveFocus();
});

test("Keeps loading for the minimum time when the response is fast", async () => {
getAiErrorHint.mockResolvedValue({ data: { fem: "A friendly hint" } });
renderAiErrorMessage();
Expand Down
53 changes: 36 additions & 17 deletions src/components/Editor/ErrorMessage/ErrorMessage.jsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
import React, { useContext } from "react";
import "../../../assets/stylesheets/ErrorMessage.scss?inline";
import { useSelector } from "react-redux";
import DOMPurify from "dompurify";
import { SettingsContext } from "../../../utils/settings";
import { useTranslation } from "react-i18next";
import CancelFillIcon from "../../../assets/icons/cancel_FILL.svg";
import ChevronDownIcon from "../../../assets/icons/chevron_down.svg";
import FriendlyErrorMessage from "../FriendlyErrorMessage/FriendlyErrorMessage";
import AiErrorMessage from "../AiErrorMessage/AiErrorMessage";
import useAiHintOffered from "../../../hooks/useAiHintOffered";

const ErrorMessage = () => {
const error = useSelector((state) => state.editor.error);
const settings = useContext(SettingsContext);
const { t } = useTranslation();
const aiHintOffered = useAiHintOffered();

return error ? (
<div className="error-message">
<div
className={`error-message__python error-message__python--${settings.fontSize}`}
>
<CancelFillIcon />
<pre
className="error-message__error"
dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(error) }}
/>
return (
<>
{/* Screen readers are already watching it when an error appears.
Announces the title, without moving focus away from the Run button. */}
<div role="status" className="rpf-visually-hidden">
{error ? t("output.errorMessage.title") : ""}
</div>
<FriendlyErrorMessage />
{aiHintOffered && <AiErrorMessage />}
</div>
) : null;
{error ? (
<div className="error-message">
<details open="true">
<summary className="error-message__summary">
<span className="error-message__title">
<CancelFillIcon />
{t("output.errorMessage.title")}
</span>
<ChevronDownIcon className="error-message__chevron" />
</summary>
<div className="error-message__content">
<div className="error-message__python">
<pre
className="error-message__error"
dangerouslySetInnerHTML={{
__html: DOMPurify.sanitize(error),
}}
/>
</div>
<FriendlyErrorMessage />
{aiHintOffered && <AiErrorMessage />}
</div>
</details>
</div>
) : null}
</>
);
};

export default ErrorMessage;
40 changes: 36 additions & 4 deletions src/components/Editor/ErrorMessage/ErrorMessage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import configureStore from "redux-mock-store";
import { Provider } from "react-redux";
import { SettingsContext } from "../../../utils/settings";
import ErrorMessage from "./ErrorMessage";
import { render, screen } from "@testing-library/react";
import { fireEvent, render, screen } from "@testing-library/react";
import useIsOnline from "../../../hooks/useIsOnline";

const mockI18n = vi.hoisted(() => ({ language: "en" }));
Expand Down Expand Up @@ -44,9 +44,27 @@ describe("When error is set", () => {
expect(screen.queryByText("Oops")).toBeInTheDocument();
});

test("Font size class is set correctly", () => {
const errorMessage = screen.queryByText("Oops").parentElement;
expect(errorMessage).toHaveClass("error-message__python--myFontSize");
test("Displays the error title in an expanded details section", () => {
const title = document.querySelector(".error-message__title");
expect(title.closest("summary")).toBeInTheDocument();
expect(title.closest("details")).toHaveAttribute("open");
});

test("Clicking the title collapses and expands the error details", () => {
const title = document.querySelector(".error-message__title");
const details = title.closest("details");

fireEvent.click(title);
expect(details).not.toHaveAttribute("open");

fireEvent.click(title);
expect(details).toHaveAttribute("open");
});

test("Announces the error title in a status region", () => {
expect(screen.getByRole("status")).toHaveTextContent(
"output.errorMessage.title",
);
});

test("Does not display friendly error elements", () => {
Expand Down Expand Up @@ -146,6 +164,20 @@ describe("When error is set", () => {
});
});

describe("When error is not set", () => {
test("Keeps an empty status region rendered for announcements", () => {
const store = configureStore([])({ editor: { error: "" } });
render(
<Provider store={store}>
<ErrorMessage />
</Provider>,
);

expect(screen.getByRole("status")).toBeEmptyDOMElement();
expect(document.querySelector(".error-message")).not.toBeInTheDocument();
});
});

describe("AI hint", () => {
const mockStore = configureStore([]);

Expand Down
Loading