sailfork / t3code-github / apps/ web/ src/ components/ chat/ MarkdownMath.tsx

import { lazy, memo, Suspense, useLayoutEffect, useRef, useState } from "react";

import { observeResize } from "../../lib/observeResize";

import { RenderErrorBoundary } from "../RenderErrorBoundary";

const MathTypeset = lazy(() => import("./MathTypeset"));

/**
 * One formula from `remark-math`. Shows its TeX until KaTeX loads, and
 * keeps showing it when KaTeX cannot parse it, such as mid-stream. Copying
 * any part of a formula copies its whole TeX source.
 */
export const MarkdownMath = memo(function MarkdownMath(props: {
  tex: string;
  display: boolean;
  focusable?: boolean;
}) {
  const viewportRef = useRef<HTMLSpanElement>(null);
  const contentRef = useRef<HTMLSpanElement>(null);
  const [overflows, setOverflows] = useState(false);
  // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- observes formula DOM width to make overflowing equations keyboard scrollable
  useLayoutEffect(() => {
    const viewport = viewportRef.current;
    const content = contentRef.current;
    if (!viewport || !content || props.focusable === false) return;
    const measure = () => setOverflows(viewport.scrollWidth > viewport.clientWidth);
    measure();
    // Typesetting and streamed TeX can change content width without resizing the viewport.
    return observeResize([viewport, content], measure);
  }, [props.focusable]);
  const scrollable = props.focusable !== false && overflows;
  const tex = props.tex.trim();
  const source = props.display ? `\\[\n${tex}\n\\]` : `\\(${tex}\\)`;
  const fallback = <span className="whitespace-pre-wrap">{source}</span>;
  return (
    <span
      className={
        props.display
          ? "chat-markdown-math-display my-[0.65rem] block in-[.chat-markdown-table-container]:max-w-none in-[.chat-markdown-table-container]:overflow-visible"
          : "inline-block max-w-full align-middle whitespace-nowrap wrap-normal break-normal in-[.chat-markdown-table-container]:max-w-none in-[.chat-markdown-table-container]:overflow-visible"
      }
      data-markdown-math={props.display ? "display" : "inline"}
      data-markdown-copy={props.display ? `${source}\n\n` : source}
      role={props.focusable === false ? undefined : "group"}
      aria-label={
        props.focusable === false
          ? undefined
          : props.display
            ? "Display equation"
            : "Inline equation"
      }
    >
      <span
        ref={viewportRef}
        className="block max-w-full overflow-x-auto overflow-y-hidden in-[.chat-markdown-table-container]:max-w-none in-[.chat-markdown-table-container]:overflow-visible px-0.5 py-0.75 focus-visible:outline-1 focus-visible:outline-solid focus-visible:-outline-offset-1 focus-visible:outline-border [&::-webkit-scrollbar]:h-2 [&::-webkit-scrollbar-thumb]:rounded [&::-webkit-scrollbar-thumb]:border [&::-webkit-scrollbar-thumb]:border-transparent [&::-webkit-scrollbar-thumb]:bg-clip-content"
        tabIndex={scrollable ? 0 : -1}
        aria-label={
          !scrollable
            ? undefined
            : props.display
              ? "Scroll display equation"
              : "Scroll inline equation"
        }
      >
        <span
          ref={contentRef}
          className={props.display ? "inline-block w-max min-w-full" : "inline-block w-max"}
        >
          <RenderErrorBoundary resetKeys={[tex]} fallback={fallback}>
            <Suspense fallback={fallback}>
              <MathTypeset tex={tex} display={props.display} fallback={fallback} />
            </Suspense>
          </RenderErrorBoundary>
        </span>
      </span>
    </span>
  );
});