CodeMapDoc

tcmts.share.CodeMapDoc · #d6ldarc4 · component · runs in the browser

uses useState

A definition's doc in the graph's panel: its first paragraph (#v6frr363), four lines at most, code as code; nothing while it's read, or if there's none.

import { codeMapDoc, codeMapDocs } from "#codebase/tcmts/share";
import { useEffect, useState } from "react";

export function CodeMapDoc({ path }: { path: string }) {
  const [doc, setDoc] = useState(() => codeMapDocs.get(path));
  const [was, setWas] = useState(path);
  if (was !== path) {
    setWas(path);
    setDoc(codeMapDocs.get(path));
  }
  useEffect(() => {
    if (codeMapDocs.has(path)) return;
    let live = true;
    codeMapDoc(path).then(
      (d) => {
        codeMapDocs.set(path, d);
        if (live) setDoc(d);
      },
      () => {},
    );
    return () => {
      live = false;
    };
  }, [path]);
  if (!doc) return null;
  return (
    <p className="cmdoc" title={doc}>
      {doc
        .split(/(`[^`]+`)/)
        .map((part, i) =>
          part.startsWith("`") && part.endsWith("`") && part.length > 1 ? (
            <code key={i}>{part.slice(1, -1)}</code>
          ) : (
            part
          ),
        )}
    </p>
  );
}

uses

used by