Skip to content

Commit 93c72f1

Browse files
authored
fix(ui): hide mermaid diagram during streaming (#1669)
Signed-off-by: Petr Bulánek <[email protected]>
1 parent 9f7d0d2 commit 93c72f1

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

apps/agentstack-ui/src/components/MarkdownContent/MarkdownContent.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import type { PluggableList } from 'unified';
1313

1414
import { components, type ExtendedComponents } from './components';
1515
import { Code } from './components/Code';
16+
import { MermaidDiagram } from './components/MermaidDiagram';
1617
import classes from './MarkdownContent.module.scss';
1718
import { rehypePlugins } from './rehype';
1819
import { remarkPlugins } from './remark';
1920
import { urlTransform } from './utils';
2021

2122
export interface MarkdownContentProps {
2223
codeBlocksExpanded?: boolean;
24+
showMermaidDiagrams?: boolean;
2325
children?: string;
2426
className?: string;
2527
remarkPlugins?: PluggableList;
@@ -28,6 +30,7 @@ export interface MarkdownContentProps {
2830

2931
export function MarkdownContent({
3032
codeBlocksExpanded,
33+
showMermaidDiagrams,
3134
className,
3235
remarkPlugins: remarkPluginsProps,
3336
components: componentsProps,
@@ -37,9 +40,10 @@ export function MarkdownContent({
3740
() => ({
3841
...components,
3942
code: ({ ...props }) => <Code {...props} forceExpand={codeBlocksExpanded} />,
43+
mermaidDiagram: ({ ...props }) => <MermaidDiagram {...props} showDiagram={showMermaidDiagrams} />,
4044
...componentsProps,
4145
}),
42-
[codeBlocksExpanded, componentsProps],
46+
[codeBlocksExpanded, showMermaidDiagrams, componentsProps],
4347
);
4448

4549
const extendedRemarkPlugins = useMemo(() => [...remarkPlugins, ...(remarkPluginsProps ?? [])], [remarkPluginsProps]);

apps/agentstack-ui/src/components/MarkdownContent/components/MermaidDiagram.tsx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,54 @@ import { Theme } from '#contexts/Theme/types.ts';
1313
import { Code } from './Code';
1414
import classes from './MermaidDiagram.module.scss';
1515

16-
export type MermaidDiagramProps = HTMLAttributes<HTMLElement> & ExtraProps;
16+
export type MermaidDiagramProps = HTMLAttributes<HTMLElement> &
17+
ExtraProps & {
18+
showDiagram?: boolean;
19+
};
1720

18-
export function MermaidDiagram({ children }: MermaidDiagramProps) {
21+
export function MermaidDiagram({ showDiagram = true, children }: MermaidDiagramProps) {
1922
const id = useId();
2023
const [diagram, setDiagram] = useState<string | null>(null);
2124

2225
const { theme } = useTheme();
2326

2427
useEffect(() => {
25-
let cancelled = false;
28+
mermaid.initialize({ startOnLoad: false, theme: theme === Theme.Dark ? 'dark' : 'default' });
29+
}, [theme]);
2630

27-
(async () => {
28-
if (typeof children !== 'string') {
31+
useEffect(() => {
32+
let isMounted = true;
33+
34+
async function renderDiagram() {
35+
if (!showDiagram || typeof children !== 'string') {
2936
return;
3037
}
3138

3239
try {
33-
mermaid.initialize({ startOnLoad: false, theme: theme === Theme.Dark ? 'dark' : 'default' });
34-
3540
const { svg } = await mermaid.render(id, children);
3641

37-
if (!cancelled) {
42+
if (isMounted) {
3843
setDiagram(svg);
3944
}
4045
} catch (error) {
41-
if (!cancelled) {
46+
if (isMounted) {
4247
console.warn(error);
4348
}
4449
}
45-
})();
50+
}
51+
52+
renderDiagram();
4653

4754
return () => {
48-
cancelled = true;
55+
isMounted = false;
4956
};
50-
}, [children, theme, id]);
57+
}, [showDiagram, children, theme, id]);
5158

5259
return (
5360
<div className={classes.root}>
5461
<Code className="language-mermaid">{children}</Code>
5562

56-
{diagram && <div dangerouslySetInnerHTML={{ __html: diagram }} className={classes.diagram} />}
63+
{showDiagram && diagram && <div dangerouslySetInnerHTML={{ __html: diagram }} className={classes.diagram} />}
5764
</div>
5865
);
5966
}

apps/agentstack-ui/src/modules/messages/components/MessageContent.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ export const MessageContent = memo(function MessageContent({ message }: Props) {
4242
}
4343

4444
return (
45-
<ChatMarkdownContent className={classes.root} sources={sources} codeBlocksExpanded={isPending}>
45+
<ChatMarkdownContent
46+
className={classes.root}
47+
sources={sources}
48+
codeBlocksExpanded={isPending}
49+
showMermaidDiagrams={!isPending}
50+
>
4651
{content}
4752
</ChatMarkdownContent>
4853
);

apps/agentstack-ui/src/modules/runs/components/RunOutputBox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function RunOutputBox({ isPending, text, downloadFileName, sources, child
3131
)}
3232

3333
{text && (
34-
<ChatMarkdownContent sources={sources} codeBlocksExpanded={isPending}>
34+
<ChatMarkdownContent sources={sources} codeBlocksExpanded={isPending} showMermaidDiagrams={!isPending}>
3535
{text}
3636
</ChatMarkdownContent>
3737
)}

0 commit comments

Comments
 (0)