Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
tsx 104 lines 2.93 KB
Raw
1 'use client';
2
3 import { useEffect, useRef, useState, useSyncExternalStore } from 'react';
4 import mermaid from 'mermaid';
5 import Zoom from 'react-medium-image-zoom';
6 import 'react-medium-image-zoom/dist/styles.css';
7
8 mermaid.registerIconPacks([
9 {
10 name: 'lucide',
11 loader: () => import('@iconify-json/lucide').then((module) => module.icons),
12 },
13 ]);
14
15 const MOBILE_BREAKPOINT = 768;
16
17 function useIsMobile() {
18 return useSyncExternalStore(
19 (cb) => {
20 const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
21 mql.addEventListener('change', cb);
22 return () => mql.removeEventListener('change', cb);
23 },
24 () => window.innerWidth < MOBILE_BREAKPOINT,
25 () => false,
26 );
27 }
28
29 function getCssVar(name: string): string {
30 return getComputedStyle(document.documentElement).getPropertyValue(name).trim();
31 }
32
33 export function Mermaid({ chart }: { chart: string }) {
34 const renderCount = useRef(0);
35 const baseId = useRef(`mermaid-${Math.random().toString(36).slice(2, 9)}`);
36 const containerRef = useRef<HTMLDivElement>(null);
37 const [svg, setSvg] = useState('');
38
39 useEffect(() => {
40 const renderDiagram = () => {
41 const isDark = document.documentElement.classList.contains('dark');
42 const orange = getCssVar('--composio-orange');
43 const bg = isDark ? getCssVar('--color-fd-muted') : getCssVar('--color-fd-card');
44 const fg = getCssVar('--color-fd-foreground');
45 const border = getCssVar('--color-fd-border');
46
47 mermaid.initialize({
48 startOnLoad: false,
49 // 'loose' enables node hyperlinks (the `click ... "url"` directive) and
50 // image nodes in flowcharts. Diagrams here are all authored in-repo.
51 securityLevel: 'loose',
52 theme: 'base',
53 themeVariables: {
54 background: bg,
55 primaryColor: bg,
56 primaryBorderColor: orange,
57 primaryTextColor: fg,
58 lineColor: orange,
59 secondaryColor: bg,
60 tertiaryColor: bg,
61 edgeLabelBackground: bg,
62 clusterBkg: bg,
63 clusterBorder: border,
64 },
65 fontFamily: 'inherit',
66 });
67
68 const uniqueId = `${baseId.current}-${renderCount.current++}`;
69 mermaid.render(uniqueId, chart).then((result) => {
70 setSvg(result.svg);
71 });
72 };
73
74 renderDiagram();
75
76 const observer = new MutationObserver((mutations) => {
77 for (const mutation of mutations) {
78 if (mutation.attributeName === 'class') {
79 renderDiagram();
80 }
81 }
82 });
83
84 observer.observe(document.documentElement, { attributes: true });
85
86 return () => observer.disconnect();
87 }, [chart]);
88
89 const isMobile = useIsMobile();
90
91 if (!svg) return null;
92
93 const diagram = (
94 <div
95 ref={containerRef}
96 className="my-4 overflow-x-auto max-w-full mx-auto [&>svg]:mx-auto"
97 dangerouslySetInnerHTML={{ __html: svg }}
98 />
99 );
100
101 if (isMobile) return diagram;
102
103 return <Zoom wrapElement="div">{diagram}</Zoom>;
104 }