The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { T, useGT } from "gt-react"; |
| 2 | import { useState } from "react"; |
| 3 | |
| 4 | import { MermaidDiagram } from "../chat/MermaidDiagram"; |
| 5 | import { ExpandedImageDialog } from "../chat/ExpandedImageDialog"; |
| 6 | |
| 7 | export default function MermaidViewer(props: { readonly text: string }) { |
| 8 | const gt = useGT(); |
| 9 | const [expanded, setExpanded] = useState<string | null>(null); |
| 10 | // Mermaid lays out DOM before sanitizing its SVG. Refuse resource and config |
| 11 | // syntax before that pass so standalone files cannot initiate requests. Node |
| 12 | // metadata is YAML, whose escaped/aliased image keys cannot be checked lexically. |
| 13 | if ( |
| 14 | /%%\s*\{|^\s*---|<\/?[A-Za-z]|<[!?]|@\s*\{|\b(?:img|image)\s*:|url\s*\(|@import|(?:https?|data|blob|file):/im.test( |
| 15 | props.text, |
| 16 | ) |
| 17 | ) |
| 18 | return ( |
| 19 | <div role="alert" className="p-6 text-xs text-muted-foreground"> |
| 20 | <T> |
| 21 | This diagram contains resource URLs, markup, node metadata, or custom configuration. Show |
| 22 | Mermaid source to inspect it safely. |
| 23 | </T> |
| 24 | </div> |
| 25 | ); |
| 26 | return ( |
| 27 | <div className="scrollbar-gutter-both min-h-0 flex-1 overflow-auto p-6 [&_*]:animate-none!"> |
| 28 | <MermaidDiagram source={props.text} onExpand={setExpanded} /> |
| 29 | {expanded ? ( |
| 30 | <ExpandedImageDialog |
| 31 | preview={{ images: [{ src: expanded, name: gt("Mermaid diagram") }], index: 0 }} |
| 32 | onClose={() => setExpanded(null)} |
| 33 | /> |
| 34 | ) : null} |
| 35 | </div> |
| 36 | ); |
| 37 | } |