Skip to content

Commit 0a9ade9

Browse files
refactor(web): the Mermaid code block lives in its own component file
MarkdownMermaidCodeBlock, with its diagram/source toggle and find reveal, moves to chat/MarkdownMermaidCodeBlock.tsx next to MermaidDiagram. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent eadb13d commit 0a9ade9

2 files changed

Lines changed: 85 additions & 78 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
} from "@t3tools/shared/composerContextClipboard";
2828
import {
2929
ChevronRightIcon,
30-
CodeIcon,
3130
FileSpreadsheetIcon,
3231
FileTextIcon,
3332
GlobeIcon,
@@ -41,7 +40,6 @@ import {
4140
PresentationIcon,
4241
SparklesIcon,
4342
TriangleAlertIcon,
44-
WorkflowIcon,
4543
type LucideIcon,
4644
} from "lucide-react";
4745
import { Check, Copy, Maximize2, Minimize2 } from "lucide";
@@ -154,7 +152,6 @@ import { openInEditorMenuLabel } from "../editorLabels";
154152
import { resolveDiffThemeName } from "../lib/diffRendering";
155153
import { GitHubIcon } from "./Icons";
156154
import { RenderErrorBoundary } from "./RenderErrorBoundary";
157-
import { MermaidDiagram } from "./chat/MermaidDiagram";
158155
import { useRenderedAppearance } from "../hooks/useTheme";
159156
import { getClientSettings, useClientSettings } from "../hooks/useSettings";
160157
import {
@@ -229,6 +226,7 @@ import {
229226
import { resolveLinkTarget } from "../browser/browserLinkTarget";
230227
import { PullRequestLinkPreview } from "./pullRequest/PullRequestLinkPreview";
231228
import { MarkdownCodeBlock, SuspenseShikiCodeBlock } from "./chat/MarkdownCodeBlock";
229+
import { MarkdownMermaidCodeBlock } from "./chat/MarkdownMermaidCodeBlock";
232230
import {
233231
extractCodeBlock,
234232
extractFenceLanguage,
@@ -657,81 +655,6 @@ function MarkdownDetails({
657655
);
658656
}
659657

660-
function MarkdownMermaidCodeBlock({
661-
code,
662-
fenceTitle,
663-
theme,
664-
isStreaming,
665-
onExpand,
666-
children,
667-
}: {
668-
code: string;
669-
fenceTitle: string | null;
670-
theme: "light" | "dark";
671-
isStreaming: boolean;
672-
onExpand: (imageUrl: string) => void;
673-
children: ReactNode;
674-
}) {
675-
const [showCode, setShowCode] = useState(false);
676-
const searching = use(MarkdownFindContext);
677-
const revealSource = () => setShowCode(true);
678-
const sourceRevealRef = findRevealRef(revealSource);
679-
const showDiagram = !showCode && !isStreaming && code.trim().length > 0;
680-
const toggleLabel = showCode ? "Show diagram" : "Show code";
681-
return (
682-
<MarkdownCodeBlock
683-
code={code}
684-
language="mermaid"
685-
fenceTitle={fenceTitle}
686-
theme={theme}
687-
isStreaming={isStreaming}
688-
canWrap={!showDiagram}
689-
diagram={showDiagram}
690-
viewToggle={
691-
isStreaming ? null : (
692-
<Tooltip>
693-
<TooltipTrigger
694-
render={
695-
<Button
696-
type="button"
697-
variant="ghost-muted"
698-
size="icon-xs"
699-
onClick={() => setShowCode((value) => !value)}
700-
aria-label={toggleLabel}
701-
/>
702-
}
703-
>
704-
{showCode ? <WorkflowIcon className="size-3" /> : <CodeIcon className="size-3" />}
705-
</TooltipTrigger>
706-
<TooltipPopup side="top">{toggleLabel}</TooltipPopup>
707-
</Tooltip>
708-
)
709-
}
710-
>
711-
{showDiagram ? (
712-
<RenderErrorBoundary resetKeys={[code, theme]} fallback={children}>
713-
<Suspense
714-
fallback={
715-
<pre className="invisible" aria-hidden>
716-
{code}
717-
</pre>
718-
}
719-
>
720-
<MermaidDiagram source={code} onExpand={onExpand} />
721-
</Suspense>
722-
{searching ? (
723-
<pre ref={sourceRevealRef} hidden aria-hidden>
724-
{code}
725-
</pre>
726-
) : null}
727-
</RenderErrorBoundary>
728-
) : (
729-
children
730-
)}
731-
</MarkdownCodeBlock>
732-
);
733-
}
734-
735658
interface MarkdownFileLinkProps {
736659
environmentId: EnvironmentId | null;
737660
href: string;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { CodeIcon, WorkflowIcon } from "lucide-react";
2+
import { Suspense, use, useState, type ReactNode } from "react";
3+
4+
import { RenderErrorBoundary } from "../RenderErrorBoundary";
5+
import { Button } from "../ui/button";
6+
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
7+
import { MarkdownCodeBlock } from "./MarkdownCodeBlock";
8+
import { findRevealRef, MarkdownFindContext } from "./markdownFindContext";
9+
import { MermaidDiagram } from "./MermaidDiagram";
10+
11+
export function MarkdownMermaidCodeBlock({
12+
code,
13+
fenceTitle,
14+
theme,
15+
isStreaming,
16+
onExpand,
17+
children,
18+
}: {
19+
code: string;
20+
fenceTitle: string | null;
21+
theme: "light" | "dark";
22+
isStreaming: boolean;
23+
onExpand: (imageUrl: string) => void;
24+
children: ReactNode;
25+
}) {
26+
const [showCode, setShowCode] = useState(false);
27+
const searching = use(MarkdownFindContext);
28+
const revealSource = () => setShowCode(true);
29+
const sourceRevealRef = findRevealRef(revealSource);
30+
const showDiagram = !showCode && !isStreaming && code.trim().length > 0;
31+
const toggleLabel = showCode ? "Show diagram" : "Show code";
32+
return (
33+
<MarkdownCodeBlock
34+
code={code}
35+
language="mermaid"
36+
fenceTitle={fenceTitle}
37+
theme={theme}
38+
isStreaming={isStreaming}
39+
canWrap={!showDiagram}
40+
diagram={showDiagram}
41+
viewToggle={
42+
isStreaming ? null : (
43+
<Tooltip>
44+
<TooltipTrigger
45+
render={
46+
<Button
47+
type="button"
48+
variant="ghost-muted"
49+
size="icon-xs"
50+
onClick={() => setShowCode((value) => !value)}
51+
aria-label={toggleLabel}
52+
/>
53+
}
54+
>
55+
{showCode ? <WorkflowIcon className="size-3" /> : <CodeIcon className="size-3" />}
56+
</TooltipTrigger>
57+
<TooltipPopup side="top">{toggleLabel}</TooltipPopup>
58+
</Tooltip>
59+
)
60+
}
61+
>
62+
{showDiagram ? (
63+
<RenderErrorBoundary resetKeys={[code, theme]} fallback={children}>
64+
<Suspense
65+
fallback={
66+
<pre className="invisible" aria-hidden>
67+
{code}
68+
</pre>
69+
}
70+
>
71+
<MermaidDiagram source={code} onExpand={onExpand} />
72+
</Suspense>
73+
{searching ? (
74+
<pre ref={sourceRevealRef} hidden aria-hidden>
75+
{code}
76+
</pre>
77+
) : null}
78+
</RenderErrorBoundary>
79+
) : (
80+
children
81+
)}
82+
</MarkdownCodeBlock>
83+
);
84+
}

0 commit comments

Comments
 (0)