Skip to content

Commit

Permalink
fix: ChatGPTNextWeb#1294 fallback while mermaid render fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa authored and jinker committed Jun 19, 2023
1 parent 649d305 commit cd2a674
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions app/components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ import mermaid from "mermaid";
import LoadingIcon from "../icons/three-dots.svg";
import React from "react";

export function Mermaid(props: { code: string }) {
export function Mermaid(props: { code: string; onError: () => void }) {
const ref = useRef<HTMLDivElement>(null);

useEffect(() => {
if (props.code && ref.current) {
mermaid.run({
nodes: [ref.current],
});
mermaid
.run({
nodes: [ref.current],
})
.catch((e) => {
props.onError();
console.error("[Mermaid] ", e.message);
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.code]);

function viewSvgInNewWindow() {
Expand All @@ -38,7 +44,7 @@ export function Mermaid(props: { code: string }) {
return (
<div
className="no-dark"
style={{ cursor: "pointer" }}
style={{ cursor: "pointer", overflow: "auto" }}
ref={ref}
onClick={() => viewSvgInNewWindow()}
>
Expand All @@ -60,7 +66,7 @@ export function PreCode(props: { children: any }) {
}, [props.children]);

if (mermaidCode) {
return <Mermaid code={mermaidCode} />;
return <Mermaid code={mermaidCode} onError={() => setMermaidCode("")} />;
}

return (
Expand Down

0 comments on commit cd2a674

Please # to comment.