diff --git a/components/mermaid.tsx b/components/mermaid.tsx index c13d40b..a9c7c56 100644 --- a/components/mermaid.tsx +++ b/components/mermaid.tsx @@ -2,6 +2,7 @@ import { cn } from "@/lib/utils"; import mermaid from "mermaid"; +import { usePathname } from "next/navigation"; import { useEffect, useState } from "react"; mermaid.initialize({ @@ -36,7 +37,13 @@ const htmlReplacer = { ] ?? "", }; -export default function Mermaid({ chart }: { chart: string }) { +type MermaidProps = { + chart: string; + setSpanId: (spanId: string | undefined) => void; +}; + +export default function Mermaid({ chart, setSpanId }: MermaidProps) { + const pathname = usePathname(); const [renderStage, setRenderStage] = useState< "server" | "browser" | "mermaid" >("server"); @@ -67,6 +74,25 @@ export default function Mermaid({ chart }: { chart: string }) { htmlReplacer.regex, htmlReplacer.fn, ); + + const span = msg.firstElementChild; + if (!span) { + continue; + } + + const linkToSpan = span.getAttribute("href"); + if (!linkToSpan) { + continue; + } + + span.addEventListener("click", (e) => { + e.preventDefault(); + + if (!pathname.endsWith(linkToSpan)) { + window.history.replaceState(null, "", linkToSpan); + setSpanId(linkToSpan.split("/").slice(-1)[0]); + } + }); } } @@ -74,7 +100,7 @@ export default function Mermaid({ chart }: { chart: string }) { }); return () => clearInterval(interval); - }, [renderStage]); + }, [pathname, renderStage, setSpanId]); return renderStage !== "server" ? (
diff --git a/components/tx-data.tsx b/components/tx-data/index.tsx similarity index 68% rename from components/tx-data.tsx rename to components/tx-data/index.tsx index e4ce46d..6aa33b8 100644 --- a/components/tx-data.tsx +++ b/components/tx-data/index.tsx @@ -2,27 +2,30 @@ import { useTx } from "@/hooks/api"; import { sequenceDiagramFromSpans } from "@/lib/mermaid"; -import Mermaid from "./mermaid"; +import { useState } from "react"; +import Mermaid from "../mermaid"; type TxDataProps = { txId: string; - spanId: string; + spanId: string | undefined; }; export default function TxData({ txId, spanId }: TxDataProps) { const { isPending, isFetching, error, data: tx } = useTx(txId); + const [spanIdToFind, setSpanIdToFind] = useState(spanId); if (isPending) return "Loading..."; if (error) return "An error has occurred: " + error.message; if (!tx) return "Couldn't find a Tx with id: " + txId; const mermaidChart = sequenceDiagramFromSpans(tx.spans); - - const span = tx.spans.find((span) => span.spanId === spanId); + const span = tx.spans.find((span) => span.spanId === spanIdToFind); return (
- {!isFetching ? : null} + {!isFetching ? ( + + ) : null} {span ? (
           {JSON.stringify(