From 423560bdaddf5932372429f40888410a68fb7ec2 Mon Sep 17 00:00:00 2001 From: luowei Date: Tue, 24 Oct 2023 22:32:40 +0800 Subject: [PATCH 1/2] mermaid front-end rendering initialization exception handling logic optimization --- web/app/components/app/chat/mermaid/index.tsx | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/web/app/components/app/chat/mermaid/index.tsx b/web/app/components/app/chat/mermaid/index.tsx index af0b68e6580319..52c0a2020974ff 100644 --- a/web/app/components/app/chat/mermaid/index.tsx +++ b/web/app/components/app/chat/mermaid/index.tsx @@ -32,6 +32,14 @@ const Flowchart = React.forwardRef((props: { const chartId = useRef(`flowchart_${CryptoJS.MD5(props.PrimitiveCode).toString()}`) const [isRender, setIsRender] = useState(true) + const clearFlowchartCache = () => { + for (let i = localStorage.length - 1; i >= 0; --i) { + const key = localStorage.key(i) + if (key && key.startsWith('flowchart_')) + localStorage.removeItem(key) + } + } + const renderFlowchart = async (PrimitiveCode: string) => { try { const cachedSvg: any = localStorage.getItem(chartId.current) @@ -44,15 +52,15 @@ const Flowchart = React.forwardRef((props: { const svgGraph = await mermaidAPI.render(chartId.current, PrimitiveCode) // eslint-disable-next-line @typescript-eslint/no-use-before-define const base64Svg: any = await svgToBase64(svgGraph.svg) - localStorage.setItem(chartId.current, base64Svg) setSvgCode(base64Svg) + if (chartId.current && base64Svg) + localStorage.setItem(chartId.current, base64Svg) } } catch (error) { - localStorage.clear() - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error - console.error(error.toString()) + clearFlowchartCache() + // eslint-disable-next-line @typescript-eslint/no-use-before-define + handleReRender() } } @@ -70,7 +78,9 @@ const Flowchart = React.forwardRef((props: { const handleReRender = () => { setIsRender(false) setSvgCode(null) - localStorage.removeItem(chartId.current) + if (chartId.current) + localStorage.removeItem(chartId.current) + setTimeout(() => { setIsRender(true) renderFlowchart(props.PrimitiveCode) @@ -90,7 +100,10 @@ const Flowchart = React.forwardRef((props: { // @ts-expect-error
{ - isRender &&
{svgCode && (Mermaid chart)}
+ isRender + &&
+ {svgCode && Mermaid chart} +
}
From 948157c718e489179e637d8877863b764a99cc33 Mon Sep 17 00:00:00 2001 From: luowei Date: Wed, 25 Oct 2023 21:28:32 +0800 Subject: [PATCH 2/2] del mermaid front-end rendering rerender --- web/app/components/app/chat/mermaid/index.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/web/app/components/app/chat/mermaid/index.tsx b/web/app/components/app/chat/mermaid/index.tsx index 52c0a2020974ff..04d137e5090cdd 100644 --- a/web/app/components/app/chat/mermaid/index.tsx +++ b/web/app/components/app/chat/mermaid/index.tsx @@ -1,6 +1,5 @@ import React, { useEffect, useRef, useState } from 'react' import mermaid from 'mermaid' -import { t } from 'i18next' import CryptoJS from 'crypto-js' let mermaidAPI: any @@ -105,7 +104,6 @@ const Flowchart = React.forwardRef((props: { {svgCode && Mermaid chart} } - ) })