Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

fix(agents): redraw mermaid diagrams when the theme changes (#2354)

SunZhiQiang51abfe9481ae
diff --git a/src/features/agents/Markdown.tsx b/src/features/agents/Markdown.tsx
index 81f7a53af..b703031f9 100644
--- a/src/features/agents/Markdown.tsx
+++ b/src/features/agents/Markdown.tsx
@@ -6,7 +6,7 @@
* the drawn diagram hides the code block, and `onDiagramDrawn` lets the conversation
* follow the end when a diagram draws after the last streamed chunk.
*/
-import { useLayoutEffect, useMemo, useRef } from "react";
+import { useLayoutEffect, useMemo, useRef, useSyncExternalStore } from "react";
import { Marked } from "marked";
import DOMPurify from "dompurify";
import { highlightInto } from "@/editor/lsp/markdown";
@@ -17,6 +17,7 @@ import { openExternalUrl } from "@/platform/openUrl";
import { copyText } from "@/ui";
import { editorService } from "@/workbench/contributions";
import { hasMermaid, renderMermaidIn } from "@/features/viewers/markdown/mermaid";
+import { subscribeTheme, themeKey } from "@/platform/themeWatch";
import { fileHref, pathLink, type PathLink } from "./conversation";
import { loadImages } from "./markdownImages";
import styles from "./Session.module.css";
@@ -198,6 +199,8 @@ export function Markdown({
const images = useRef(new Map<string, Promise<string>>());
/** The last drawing of each mermaid block, stood in while a changed one renders. */
const diagrams = useRef<Node[]>([]);
+ // Diagrams are drawn in the active theme's colours: a theme change redraws them.
+ const theme = useSyncExternalStore(subscribeTheme, themeKey, () => "");
useLayoutEffect(() => (ref.current ? enhance(ref.current, cwd, images.current) : undefined), [html, cwd]);
// Mermaid diagrams: before paint, so cached ones don't flash their source. Only a
// draw that lands after the render needs the follow — cached ones are in place by
@@ -212,7 +215,7 @@ export function Markdown({
live = false;
pass.cancel();
};
- }, [html, onDiagramDrawn]);
+ }, [html, theme, onDiagramDrawn]);
return (
<div
ref={ref}
diff --git a/src/features/agents/mermaidFollow.test.tsx b/src/features/agents/mermaidFollow.test.tsx
index f45edaff7..fb57c9cb7 100644
--- a/src/features/agents/mermaidFollow.test.tsx
+++ b/src/features/agents/mermaidFollow.test.tsx
@@ -51,6 +51,28 @@ beforeEach(() => {
useAgents.setState({ tasks: new Map([[task.id, task]]), entries: new Map(), composers: new Map(), focus: null });
});
+it("redraws a diagram when the theme changes", async () => {
+ mermaid.render.mockReset().mockResolvedValue({ svg: `<svg id="x"><style>#x .actor{fill:none}</style></svg>` });
+ const root = document.documentElement;
+ const previous = root.dataset.theme;
+ try {
+ const { container } = render(
+ <SessionEditor input={{ kind: "agent-session", key: task.id, data: { task: task.id } }} />,
+ );
+ await waitFor(() => {
+ const block = container.querySelector("[data-mermaid]");
+ expect(block).toHaveAttribute("data-mermaid-state", "ok");
+ });
+ expect(mermaid.render).toHaveBeenCalledTimes(1);
+
+ root.dataset.theme = previous === "dark" ? "light" : "dark";
+ await waitFor(() => expect(mermaid.render).toHaveBeenCalledTimes(2));
+ } finally {
+ if (previous === undefined) delete root.dataset.theme;
+ else root.dataset.theme = previous;
+ }
+});
+
it("follows the end when a diagram draws after the last update", async () => {
// The diagram is still rendering when the conversation arrives at its end.
let draw!: () => void;