Skip to content

Commit 727334e

Browse files
committed
fix: udpate based on comments
1 parent efb8abd commit 727334e

File tree

5 files changed

+58
-31
lines changed

5 files changed

+58
-31
lines changed

src/components/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ export interface VegaLiteFormat extends DataFormat {
159159
export type VegaLiteData = VegaLiteFormat & Updates<VegaLiteFormat>
160160

161161
export interface MermaidFormat extends DataFormat {
162-
/** Follow Mermaid's [documentation](https://mermaid.js.org/intro/) to provide a code string. */
163-
code: string
164-
/** Follow Mermaid's [documentation](https://mermaid.js.org/config/setup/README.html) to provide a config object. It's used for additional customizations. */
162+
/** Diagram definition following [Mermaid's syntax](https://mermaid.js.org/intro/syntax-reference.html#syntax-structure). The use of [these](https://mermaid.js.org/intro/syntax-reference.html#diagram-breaking) words or symbols can break diagrams. */
163+
diagram: string
164+
/** Configuration for altering and customizing Mermaid Diagrams. Refer [Mermaid docs](https://mermaid.js.org/config/schema-docs/config.html) for details. */
165165
config?: MermaidConfig
166166
}
167167

src/components/visualization/mermaidViz/mermaidViz.css

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
width: 100%;
44
}
55

6-
.rustic-mermaid-container div:last-of-type {
6+
.rustic-mermaid-container .rustic-mermaid {
77
flex: 1;
88
min-height: 0;
99
display: flex;
1010
justify-content: center;
1111
}
1212

13-
.rustic-mermaid-container div:last-of-type svg {
13+
.rustic-mermaid-container .rustic-mermaid svg {
1414
height: 100%;
1515
width: 100%;
1616
}
17+
18+
.rustic-invisible {
19+
visibility: hidden;
20+
}

src/components/visualization/mermaidViz/mermaidViz.cy.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('MermaidViz', () => {
1616
cy.viewport(viewport)
1717
cy.mount(
1818
<MermaidViz
19-
code={codeExample}
19+
diagram={codeExample}
2020
title="Christmas Shopping"
2121
description="A flow chart"
2222
/>

src/components/visualization/mermaidViz/mermaidViz.stories.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ meta.argTypes = {
3232

3333
export const ClassDiagram = {
3434
args: {
35-
code: `classDiagram
35+
diagram: `classDiagram
3636
Animal <|-- Duck
3737
Animal <|-- Fish
3838
Animal <|-- Zebra
@@ -58,7 +58,7 @@ export const ClassDiagram = {
5858

5959
export const Flow = {
6060
args: {
61-
code: `
61+
diagram: `
6262
flowchart TD
6363
A[Christmas] -->|Get money| B(Go shopping)
6464
B --> C{Let me think}
@@ -73,7 +73,7 @@ export const ERDiagram = {
7373
title: 'Customer Order Management System ER Diagram',
7474
description:
7575
'This ER diagram illustrates the relationships in an ordering system where a customer has a delivery address, places orders, and is liable for invoices; delivery addresses receive orders, invoices cover orders, orders include order items, product categories contain products, and products are ordered in order items.',
76-
code: `
76+
diagram: `
7777
erDiagram
7878
CUSTOMER }|..|{ DELIVERY-ADDRESS : has
7979
CUSTOMER ||--o{ ORDER : places
@@ -88,6 +88,6 @@ export const ERDiagram = {
8888

8989
export const Error = {
9090
args: {
91-
code: '',
91+
diagram: '',
9292
},
9393
}

src/components/visualization/mermaidViz/mermaidViz.tsx

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,68 @@ import Stack from '@mui/material/Stack'
44
import Typography from '@mui/material/Typography'
55
import useTheme from '@mui/system/useTheme'
66
import mermaid from 'mermaid'
7-
import React, { useEffect, useRef } from 'react'
7+
import React, { useEffect, useRef, useState } from 'react'
88

99
import type { MermaidData } from '../../types'
1010

1111
/** The `MermaidViz` component leverages [Mermaid.js](https://mermaid.js.org/) to create dynamic and interactive diagrams, including flowcharts, sequence diagrams, class diagrams, and more. It is ideal for visualizing complex data and processes in a clear and structured manner. */
1212
function MermaidViz(props: MermaidData) {
13-
const chartRef = useRef(null)
13+
const mermaidRef = useRef<HTMLDivElement>(null)
1414
const rusticTheme = useTheme()
1515
const mermaidTheme = rusticTheme.palette.mode === 'dark' ? 'dark' : 'neutral'
16+
const [errorMessage, setErrorMessage] = useState<string>()
17+
const [isProcessed, setIsProcessed] = useState<boolean>(false)
1618

1719
useEffect(() => {
18-
if (chartRef.current) {
20+
if (mermaidRef.current) {
1921
mermaid.initialize({
2022
theme: mermaidTheme,
2123
themeVariables: { fontFamily: 'Inter' },
2224
...props.config,
2325
})
2426

25-
mermaid.contentLoaded()
27+
mermaid
28+
.run({
29+
querySelector: '.rustic-mermaid',
30+
})
31+
.then(() => {
32+
setIsProcessed(true)
33+
setErrorMessage('')
34+
})
35+
.catch(() => {
36+
setIsProcessed(true)
37+
setErrorMessage(
38+
'An error occurred while rendering the diagram. Please check the syntax.'
39+
)
40+
})
2641
}
2742
}, [mermaidTheme])
2843

29-
return (
30-
<Stack
31-
direction="column"
32-
className="rustic-mermaid-container"
33-
data-cy="mermaid-container"
34-
>
35-
{props.title && (
36-
<Typography variant="subtitle2">{props.title}</Typography>
37-
)}
38-
{props.description && (
39-
<Typography variant="caption">{props.description}</Typography>
40-
)}
41-
<div ref={chartRef} className="mermaid" key={mermaidTheme}>
42-
{props.code}
43-
</div>
44-
</Stack>
45-
)
44+
if (errorMessage) {
45+
return (
46+
<Typography variant="body2" className="rustic-error-message">
47+
{errorMessage}
48+
</Typography>
49+
)
50+
} else {
51+
return (
52+
<Stack
53+
direction="column"
54+
className={`${!isProcessed ? 'rustic-invisible ' : ''}rustic-mermaid-container`}
55+
data-cy="mermaid-container"
56+
>
57+
{props.title && (
58+
<Typography variant="subtitle2">{props.title}</Typography>
59+
)}
60+
{props.description && (
61+
<Typography variant="caption">{props.description}</Typography>
62+
)}
63+
<div className="rustic-mermaid" ref={mermaidRef} key={mermaidTheme}>
64+
{props.diagram}
65+
</div>
66+
</Stack>
67+
)
68+
}
4669
}
4770

4871
export default MermaidViz

0 commit comments

Comments
 (0)