import type { PluginTheme } from "@getpaseo/plugin"; import { Icon, Modal, ScrollView as SheetScrollView } from "@getpaseo/plugin/client/react-native"; import React, { useMemo, useState } from "react"; import { LayoutChangeEvent, Pressable, ScrollView, StyleSheet, Text, View } from "react-native"; import { parseFlowchart, type Flowchart, type NodeShape } from "../../shared/diagram/flowchart"; import { layoutFlowchart, layoutSequence, type FlowLayout, type LaidOutEdge, type LaidOutNode, } from "../../shared/diagram/layout"; import { parseSequence, type SeqEvent, type SequenceDiagram } from "../../shared/diagram/sequence"; import { fitRowZoom, zoomIn, zoomOut, type Size } from "../../shared/diagram/zoom"; import { detailFor, parseDetails, type DiagramDetail } from "../../shared/logic/explain-logic"; import { isNode, text, type Props } from "../../shared/logic/values"; import { MarkdownText } from "../markdown"; import { RenderNode } from "../node-renderer"; import { useUiRuntime } from "../runtime"; const ARROW = 4; const MIN_MEASURABLE = 120; type ParsedDiagram = | { kind: "flow"; layout: FlowLayout; skipped: Flowchart["skipped"] } | { kind: "sequence"; diagram: SequenceDiagram; size: Size } | { kind: "unsupported" }; type DrawableDiagram = Exclude; function parseDiagram(source: string): ParsedDiagram { const flow = parseFlowchart(source); if (flow && flow.nodes.length > 0) { return { kind: "flow", layout: layoutFlowchart(flow), skipped: flow.skipped }; } const sequence = parseSequence(source); if (sequence && sequence.participants.length > 0) { const box = layoutSequence(sequence.participants, sequence.events.length); return { kind: "sequence", diagram: sequence, size: { width: box.width, height: box.height } }; } return { kind: "unsupported" }; } function naturalSize(parsed: DrawableDiagram): Size { return parsed.kind === "flow" ? { width: parsed.layout.width, height: parsed.layout.height } : parsed.size; } export function Diagram({ props }: { props: Props }) { const { theme, compact } = useUiRuntime(); const source = text(props.mermaid); const details = useMemo(() => parseDetails(props.details) ?? [], [props.details]); const parsed = useMemo(() => parseDiagram(source), [source]); const [selectedDetail, setSelectedDetail] = useState(null); const [showCode, setShowCode] = useState(false); const [popped, setPopped] = useState(false); const [available, setAvailable] = useState(0); const [zoom, setZoom] = useState(1); if (parsed.kind === "unsupported") { return ( {source} ); } const natural = naturalSize(parsed); const baseScale = available > MIN_MEASURABLE ? Math.min(1.15, Math.max(0.55, available / natural.width)) : 1; const fitZoom = fitRowZoom(baseScale, natural.width, available); const scale = baseScale * zoom; const box = { width: Math.max(1, Math.floor(natural.width * scale)), height: Math.max(1, Math.floor(natural.height * scale)), }; const onLayout = (e: LayoutChangeEvent) => { const w = Math.round(e.nativeEvent.layout.width); if (w >= MIN_MEASURABLE && w !== available) setAvailable(w); }; return ( setZoom(zoomOut)} style={{ paddingHorizontal: 8, paddingVertical: 4, borderRadius: 6, backgroundColor: theme.colors.surface2, }} > − setZoom(fitZoom)} style={{ paddingHorizontal: 8, paddingVertical: 4, borderRadius: 6, backgroundColor: theme.colors.surface2, }} > {Math.round(zoom * 100)}% setZoom(zoomIn)} style={{ paddingHorizontal: 8, paddingVertical: 4, borderRadius: 6, backgroundColor: theme.colors.surface2, }} > + setShowCode((s) => !s)} style={{ paddingHorizontal: 8, paddingVertical: 4, borderRadius: 6, backgroundColor: theme.colors.surface2, }} > {showCode ? "Diagram" : "Mermaid"} setPopped(true)} style={{ paddingHorizontal: 8, paddingVertical: 4, borderRadius: 6, backgroundColor: theme.colors.surface2, }} > {showCode ? ( {source} ) : ( setSelectedDetail((prev) => (prev?.key === detail.key ? null : detail)) } /> )} {selectedDetail !== null ? ( {selectedDetail.key} setSelectedDetail(null)}> {selectedDetail.parts.map((part, idx) => typeof part === "string" ? ( ) : isNode(part) ? ( ) : null, )} ) : null} {popped ? ( } open={popped} onOpenChange={setPopped} > setSelectedDetail((prev) => (prev?.key === detail.key ? null : detail)) } /> ) : null} ); } function Drawing({ parsed, theme, details, selectedKey, onSelectDetail, }: { parsed: DrawableDiagram; theme: PluginTheme; details: readonly DiagramDetail[]; selectedKey: string | null; onSelectDetail(detail: DiagramDetail): void; }) { if (parsed.kind === "flow") { return ( {parsed.layout.edges.map((edge, index) => ( ))} {parsed.layout.nodes.map((node) => { const detail = detailFor(details, node.id, node.label); return ( { if (detail) onSelectDetail(detail); }} /> ); })} ); } return ; } function radiusFor(shape: NodeShape, node: LaidOutNode): number { if (shape === "circle") return node.width / 2; if (shape === "stadium") return node.height / 2; if (shape === "round") return 12; return 6; } function NodeView({ node, theme, hasDetail, selected, onPress, }: { node: LaidOutNode; theme: PluginTheme; hasDetail: boolean; selected: boolean; onPress(): void; }) { const borderColor = selected ? theme.colors.accent : hasDetail ? theme.colors.accent : theme.colors.border; const content = ( {node.label} ); return hasDetail ? ( {content} ) : ( content ); } function EdgeView({ edge, theme }: { edge: LaidOutEdge; theme: PluginTheme }) { const color = theme.colors.foregroundMuted; return ( <> {edge.segments.map((seg, i) => ( ))} {edge.arrowAt ? ( ) : null} {edge.label && edge.labelAt ? ( {edge.label} ) : null} ); } function SequenceView({ diagram, size, theme, }: { diagram: SequenceDiagram; size: Size; theme: PluginTheme; }) { const box = layoutSequence(diagram.participants, diagram.events.length); const centerById = new Map(box.columns.map((col) => [col.id, col.centerX])); return ( {box.columns.map((col) => ( {col.label} ))} {diagram.events.map((ev: SeqEvent, idx: number) => { const row = box.rows[idx]; const y = (row?.y ?? box.headerHeight) + 20; if (ev.kind === "note") { const firstId = ev.over[0] ?? ""; const cx = centerById.get(firstId) ?? 60; return ( {ev.text} ); } const x1 = centerById.get(ev.from) ?? 0; const x2 = centerById.get(ev.to) ?? 0; const left = Math.min(x1, x2); const width = Math.max(20, Math.abs(x2 - x1)); return ( {ev.text} ); })} ); }