Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
24.3 KB
Raw Blame
"use client";

/**
 * DynamicBlock — the frame shared by every fence that renders in place
 * (MUL-7649): ```html previews and ```mermaid diagrams.
 *
 * A dynamic block is part of the message body. That is what separates it from
 * an attachment, which is a file and shows as a card that opens the viewer. So
 * the frame reads as content, not as a file: an always-visible title bar (kind
 * icon, title, kind chip; the Preview | Source, fullscreen and copy actions
 * fade in on hover or focus, MUL-7733) over a body that takes its content's
 * height, collapsed past DYNAMIC_BLOCK_COLLAPSE_AT_PX behind a fade and
 * "Show all". Loading and error states live inside the same frame, so a block
 * that fails never breaks the comment around it.
 *
 * The kinds supply only the preview; the frame owns the chrome, the source
 * view and the error panel.
 *
 * The editor draws the same frame (MUL-9760); see DynamicBlockEditing for what
 * differs there.
 */

import {
  useCallback,
  useEffect,
  useLayoutEffect,
  useRef,
  useState,
  type ReactNode,
  type Ref,
} from "react";
import { prefersReducedMotion } from "@multica/core/appearance";
import {
  AppWindow,
  ChevronDown,
  CodeXml,
  Maximize2,
  RotateCw,
  TriangleAlert,
  Workflow,
} from "@multica/ui/icons";
import { Button } from "@multica/ui/components/ui/button";
import { CopyCheckIcon } from "@multica/ui/components/common/copy-check-icon";
import { IconButton } from "@multica/ui/components/common/icon-button";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@multica/ui/components/ui/tabs";
import { TooltipHint } from "@multica/ui/components/common/tooltip-hint";
import { Spinner } from "@multica/ui/components/ui/spinner";
import { copyText } from "@multica/ui/lib/clipboard";
import { isImeComposing } from "@multica/core/utils";
import { cn } from "@multica/ui/lib/utils";
import { useT } from "../i18n";
import { CodeBlockStatic } from "./code-block-static";

export type DynamicBlockKind = "html" | "mermaid";

/** Content taller than this is collapsed until the reader asks for all of it. */
export const DYNAMIC_BLOCK_COLLAPSE_AT_PX = 480;
/** Floor for a preview body, so a loading or near-empty block keeps a shape. */
export const DYNAMIC_BLOCK_MIN_BODY_PX = 120;
/** Title bar (36px) plus the frame's top and bottom borders. */
export const DYNAMIC_BLOCK_CHROME_PX = 38;

// Kind names are identifiers (the fence language), not translatable copy.
const KIND_LABEL: Record<DynamicBlockKind, string> = {
  html: "HTML",
  mermaid: "Mermaid",
};

const KIND_ICON: Record<DynamicBlockKind, typeof AppWindow> = {
  html: AppWindow,
  mermaid: Workflow,
};

// Highlighting language for the source view. Mermaid has no lowlight grammar.
const SOURCE_LANGUAGE: Record<DynamicBlockKind, string | undefined> = {
  html: "xml",
  mermaid: undefined,
};

export interface DynamicBlockError {
  message: string;
  /** 1-based source line, when the error carries one. */
  line?: number;
}

export type DynamicBlockView = "preview" | "source";

/**
 * The block inside the editor (Docs, an issue description being edited). It
 * is the read-only frame with three differences: the source view is
 * ProseMirror's own editable `<pre>`, the title can be renamed in place, and
 * the NodeView owns the view, because it has to keep the caret out of a
 * source the preview is hiding.
 */
export interface DynamicBlockEditing {
  /**
   * The editable source. Mounted for the block's whole life and only hidden
   * behind the preview: ProseMirror owns its DOM, and unmounting it would
   * break editing.
   */
  source: ReactNode;
  view: DynamicBlockView;
  onViewChange: (view: DynamicBlockView) => void;
  /** Writes the title back to the fence; absent while the editor is read-only. */
  onTitleChange?: (title: string) => void;
  /** The block is node-selected: outline it and keep its actions up. */
  selected?: boolean;
}

/**
 * `value` while `visible`, else the last value that was shown. A hidden
 * preview is not rebuilt on every keystroke in the source; it catches up the
 * moment it is shown again.
 */
export function useValueWhileVisible<T>(value: T, visible: boolean): T {
  const [shown, setShown] = useState(value);
  if (visible && shown !== value) setShown(value);
  return visible ? value : shown;
}

interface DynamicBlockProps {
  kind: DynamicBlockKind;
  /** From the fence info string (```html title="…"). Falls back to the kind name. */
  title?: string | null;
  /** The fence body: shown in the source view and copied by the copy button. */
  source: string;
  error?: DynamicBlockError | null;
  /** Shows the fullscreen button when set. */
  onFullscreen?: () => void;
  /** Reloads the current preview when the kind supports it. */
  onReload?: () => void;
  /** Reveals the mounted preview while its surface is enlarged. */
  fullscreen?: boolean;
  /** Focus returns here when the kind's fullscreen surface closes. */
  fullscreenButtonRef?: Ref<HTMLButtonElement>;
  /**
   * The rendered content. It stays mounted while the source view is showing,
   * so switching back is instant; `active` says whether it is on screen.
   * `collapsed` says the frame is clipping the body at the collapse point, so
   * the preview must lay out at the clamp, never at its full height — a tall
   * document that paints unclamped for even a frame slams virtualized
   * timelines with a total-height shock (visible flicker on scroll jumps).
   */
  preview: (options: { active: boolean; collapsed: boolean }) => ReactNode;
  /**
   * The preview body's content height in px, when the preview knows it (the
   * html block's measured document height). Passing it moves the collapse
   * decision out of DOM measurement: the frame flips to collapsed in the same
   * render the height arrives, so the clamp and the height cannot land a
   * frame apart. Omit — or pass null while the height is still unknown —
   * for previews the frame must measure (mermaid).
   */
  bodyHeight?: number | null;
  /** Set inside the editor. */
  editing?: DynamicBlockEditing;
  className?: string;
}

export function DynamicBlock({
  kind,
  title,
  source,
  error,
  onFullscreen,
  onReload,
  fullscreen = false,
  fullscreenButtonRef,
  preview,
  bodyHeight,
  editing,
  className,
}: DynamicBlockProps) {
  const { t } = useT("editor");
  const [ownView, setOwnView] = useState<DynamicBlockView>("preview");
  const view = editing?.view ?? ownView;
  const setView = editing?.onViewChange ?? setOwnView;
  const [copied, setCopied] = useState(false);
  const [expanded, setExpanded] = useState(false);
  // Overflow state for previews the frame must measure itself (mermaid). A
  // preview that reports its height makes this read obsolete — the collapse
  // decision then lands in the same render the height arrives, so the clamp
  // and the height can never paint a frame apart.
  const measuredBodyRef = useRef<HTMLDivElement>(null);
  const [measuredOverflowing, setMeasuredOverflowing] = useState(false);
  useLayoutEffect(() => {
    if (bodyHeight != null) return;
    const el = measuredBodyRef.current;
    if (!el) return;
    const measure = () =>
      setMeasuredOverflowing(el.offsetHeight > DYNAMIC_BLOCK_COLLAPSE_AT_PX);
    measure();
    if (typeof ResizeObserver === "undefined") return;
    const observer = new ResizeObserver(measure);
    observer.observe(el);
    return () => observer.disconnect();
  }, [bodyHeight]);
  const overflowing =
    bodyHeight != null
      ? bodyHeight > DYNAMIC_BLOCK_COLLAPSE_AT_PX
      : measuredOverflowing;
  // The clamp is an inline-layout concern and stays on while the surface is
  // enlarged: the dialog is fixed-position, so it escapes the clip and fills
  // its own box, while the in-place footprint keeps holding the clamp —
  // dropping the clip for the enlargement would re-expand the timeline for as
  // long as the dialog is open, the very height jump this frame prevents.
  const collapsed = overflowing && !expanded;
  const Icon = KIND_ICON[kind];
  const kindLabel = KIND_LABEL[kind];
  const trimmedTitle = title?.trim();
  // The actions stay up while they carry state the reader has to see: the
  // source view (Preview is how to get back) and the copy confirmation. In the
  // editor they also stay up on a node-selected block, which has no hover.
  const actionsPinned = view === "source" || copied || !!editing?.selected;
  // Chrome inside a NodeView must not be editable text; the source must be.
  const chromeEditable = editing ? false : undefined;

  const handleCopy = async () => {
    if (await copyText(source)) {
      setCopied(true);
      setTimeout(() => setCopied(false), 2000);
    }
  };

  return (
    <Tabs
      value={view}
      onValueChange={(value) => setView(value as DynamicBlockView)}
      data-dynamic-block={kind}
      // Read by the editor's arrow keys (CodeBlockExtension).
      data-dynamic-block-view={editing ? view : undefined}
      // No outer margin: the lazy shell around the block owns that spacing.
      className={cn(
        "group/dynamic-block gap-0 overflow-hidden rounded-lg border bg-surface shadow-[var(--surface-shadow)]",
        error && view === "preview" && "border-destructive/35",
        editing?.selected && "outline-2 outline-offset-2 outline-brand",
        className,
      )}
    >
      <div
        contentEditable={chromeEditable}
        suppressContentEditableWarning
        className="flex h-9 items-center gap-2 border-b pr-1.5 pl-3"
      >
        <Icon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
        {/* With no title, the kind name stands in and the chip would repeat it. */}
        {editing?.onTitleChange ? (
          <DynamicBlockTitleInput
            title={trimmedTitle ?? null}
            kindLabel={kindLabel}
            onChange={editing.onTitleChange}
          />
        ) : (
          <span
            className={cn(
              "min-w-0 truncate text-label font-medium",
              !trimmedTitle && "text-muted-foreground",
            )}
          >
            {trimmedTitle || kindLabel}
          </span>
        )}
        {trimmedTitle && (
          <span className="inline-flex h-[18px] shrink-0 items-center rounded-sm bg-secondary px-1.5 text-micro text-muted-foreground">
            {kindLabel}
          </span>
        )}
        <span className="flex-1" />
        {/* The title identifies the block and always shows; the actions fade
            in while the block is hovered or holds focus (MUL-7733). Opacity,
            not display, so they keep their place in the bar and in the tab
            order. Touch has no hover to reveal them, so there they always
            show. */}
        <div
          data-dynamic-block-actions=""
          className={cn(
            "flex shrink-0 items-center gap-2 transition-opacity duration-150 ease-out",
            !actionsPinned &&
              "group-focus-within/dynamic-block:opacity-100 group-hover/dynamic-block:opacity-100 [@media(hover:hover)]:opacity-0",
          )}
        >
          <TabsList
            aria-label={t(($) => $.dynamic_block.view)}
            className="mr-1 shrink-0 gap-0.5 rounded-md p-0.5 group-data-horizontal/tabs:h-[26px]"
          >
            <TabsTrigger value="preview" className="rounded-sm px-2 text-caption">
              {t(($) => $.dynamic_block.preview)}
            </TabsTrigger>
            <TabsTrigger value="source" className="rounded-sm px-2 text-caption">
              {t(($) => $.dynamic_block.source)}
            </TabsTrigger>
          </TabsList>
          {onReload && (
            <IconButton
              type="button"
              variant="ghost"
              size="icon-xs"
              className="text-muted-foreground"
              disabled={view !== "preview"}
              onClick={onReload}
              label={t(($) => $.attachment.reload)}
            >
              <RotateCw />
            </IconButton>
          )}
          {onFullscreen && (
            <IconButton
              ref={fullscreenButtonRef}
              type="button"
              variant="ghost"
              size="icon-xs"
              className="text-muted-foreground"
              onClick={onFullscreen}
              label={t(($) => $.code_block.fullscreen)}
            >
              <Maximize2 />
            </IconButton>
          )}
          <IconButton
            type="button"
            variant="ghost"
            size="icon-xs"
            className="text-muted-foreground"
            onClick={handleCopy}
            label={t(($) => $.dynamic_block.copy_source)}
          >
            <CopyCheckIcon copied={copied} />
          </IconButton>
        </div>
      </div>
      <TabsContent
        value="preview"
        keepMounted
        hidden={!fullscreen && view !== "preview"}
        inert={!fullscreen && view !== "preview"}
        contentEditable={chromeEditable}
        suppressContentEditableWarning
      >
        {error && view === "preview" && (
          <DynamicBlockErrorPanel
            kind={kind}
            error={error}
            onViewSource={() => setView("source")}
          />
        )}
        <div hidden={!!error && !fullscreen}>
          <CollapsibleBody
            collapsed={collapsed}
            onExpand={() => setExpanded(true)}
            measureRef={bodyHeight == null ? measuredBodyRef : undefined}
          >
            {preview({ active: view === "preview" && !error, collapsed })}
          </CollapsibleBody>
        </div>
      </TabsContent>
      {editing ? (
        // display:none rather than a 1px visually-hidden box, where the
        // source would wrap every character onto its own line: tens of
        // thousands of pixels of layout for a chart. The NodeView keeps the
        // caret out while it is hidden. Shown, it does not collapse: the
        // caret must never sit in a clipped line.
        <div data-dynamic-block-source="" hidden={view !== "source"}>
          {editing.source}
        </div>
      ) : (
        <TabsContent value="source">
          <SelfCollapsingBody>
            <CodeBlockStatic
              language={SOURCE_LANGUAGE[kind]}
              body={source}
              className="!m-0 !rounded-none !bg-muted"
            />
          </SelfCollapsingBody>
        </TabsContent>
      )}
    </Tabs>
  );
}

/**
 * The editor's title: reads like the read-only title, renames in place on
 * click. Enter or leaving the field saves; Escape keeps the old title. A blank
 * title removes the attribute, and the kind name stands in again.
 */
function DynamicBlockTitleInput({
  title,
  kindLabel,
  onChange,
}: {
  title: string | null;
  kindLabel: string;
  onChange: (title: string) => void;
}) {
  const { t } = useT("editor");
  const [draft, setDraft] = useState<string | null>(null);
  // Escape unmounts the field, which blurs it; the blur must not save.
  const discarded = useRef(false);

  const commit = (value: string) => {
    setDraft(null);
    if (discarded.current) {
      discarded.current = false;
      return;
    }
    const next = value.trim();
    if (next !== (title ?? "")) onChange(next);
  };

  if (draft != null) {
    return (
      <input
        autoFocus
        value={draft}
        placeholder={kindLabel}
        aria-label={t(($) => $.dynamic_block.title)}
        onFocus={(event) => event.currentTarget.select()}
        onChange={(event) => setDraft(event.target.value)}
        onBlur={(event) => commit(event.currentTarget.value)}
        onKeyDown={(event) => {
          if (isImeComposing(event)) return;
          if (event.key === "Enter") {
            event.preventDefault();
            event.currentTarget.blur();
          } else if (event.key === "Escape") {
            // Escape cancels the rename only; it must not also close or
            // leave whatever surface holds the editor.
            event.preventDefault();
            event.stopPropagation();
            discarded.current = true;
            event.currentTarget.blur();
          }
        }}
        className="-mx-1 h-6 min-w-0 flex-1 rounded-sm bg-transparent px-1 text-label font-medium outline-none ring-1 ring-ring placeholder:text-muted-foreground"
      />
    );
  }

  return (
    <TooltipHint label={title ? t(($) => $.dynamic_block.rename) : t(($) => $.dynamic_block.add_title)}>
      <button
        type="button"
        onClick={() => setDraft(title ?? "")}
        className={cn(
          "-mx-1 min-w-0 truncate rounded-sm px-1 text-left text-label font-medium transition-colors hover:bg-accent",
          !title && "text-muted-foreground",
        )}
      >
        {title || kindLabel}
      </button>
    </TooltipHint>
  );
}

/**
 * Clips the preview body past DYNAMIC_BLOCK_COLLAPSE_AT_PX with a fade and
 * "Show all". Whether to clip is decided by the frame (`collapsed`): either
 * from a preview-reported height in the same render the height arrives, or —
 * for previews the frame measures — from the ResizeObserver below. The content
 * keeps laying out at full height underneath the clip (the preview clamps
 * its own layout height when `collapsed`), so expanding only lifts the clip
 * — an HTML preview or diagram is not rebuilt.
 */
function useSmoothExpandStyle(collapsed: boolean, innerRef: React.RefObject<HTMLDivElement | null>) {
  const prevCollapsedRef = useRef(collapsed);
  const [animatingHeight, setAnimatingHeight] = useState<number | null>(null);

  useLayoutEffect(() => {
    const wasCollapsed = prevCollapsedRef.current;
    prevCollapsedRef.current = collapsed;
    if (!wasCollapsed || collapsed) return;
    if (prefersReducedMotion()) return;
    const el = innerRef.current;
    const targetHeight = el?.scrollHeight ?? 0;
    if (targetHeight <= DYNAMIC_BLOCK_COLLAPSE_AT_PX) return;
    setAnimatingHeight(DYNAMIC_BLOCK_COLLAPSE_AT_PX);
    const frame = requestAnimationFrame(() => {
      setAnimatingHeight(targetHeight);
    });
    return () => cancelAnimationFrame(frame);
  }, [collapsed, innerRef]);

  useEffect(() => {
    if (animatingHeight === null) return;
    const timer = window.setTimeout(() => setAnimatingHeight(null), 240);
    return () => window.clearTimeout(timer);
  }, [animatingHeight]);

  const maxHeight = collapsed
    ? DYNAMIC_BLOCK_COLLAPSE_AT_PX
    : animatingHeight !== null
      ? animatingHeight
      : undefined;

  return {
    style: maxHeight !== undefined ? { maxHeight } : undefined,
    onTransitionEnd: (e: React.TransitionEvent<HTMLDivElement>) => {
      if (e.target === e.currentTarget && e.propertyName === "max-height" && !collapsed) {
        setAnimatingHeight(null);
      }
    },
  };
}

function CollapsibleBody({
  collapsed,
  onExpand,
  measureRef,
  children,
}: {
  collapsed: boolean;
  onExpand: () => void;
  /** Overflow measurement target for previews the frame must measure itself. */
  measureRef?: Ref<HTMLDivElement>;
  children: ReactNode;
}) {
  const { t } = useT("editor");
  const innerRef = useRef<HTMLDivElement | null>(null);
  const setInnerRef = useCallback(
    (node: HTMLDivElement | null) => {
      innerRef.current = node;
      if (typeof measureRef === "function") measureRef(node);
      else if (measureRef && "current" in measureRef) (measureRef as React.MutableRefObject<HTMLDivElement | null>).current = node;
    },
    [measureRef],
  );
  const expandProps = useSmoothExpandStyle(collapsed, innerRef);

  return (
    <div
      className="relative overflow-hidden transition-[max-height] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none"
      data-collapsed={collapsed ? "" : undefined}
      style={expandProps.style}
      onTransitionEnd={expandProps.onTransitionEnd}
    >
      <div ref={setInnerRef}>{children}</div>
      {collapsed && (
        <>
          <div className="pointer-events-none absolute inset-x-0 bottom-0 h-24 bg-gradient-to-b from-transparent to-surface" />
          <Button
            type="button"
            variant="outline"
            size="sm"
            className="absolute bottom-3 left-1/2 -translate-x-1/2 bg-surface shadow-[var(--surface-shadow)]"
            onClick={onExpand}
          >
            <ChevronDown />
            {t(($) => $.dynamic_block.show_all)}
          </Button>
        </>
      )}
    </div>
  );
}

/**
 * The source view's body keeps its own measuring collapse (the behavior the
 * preview's body had before the reported-height clamp): the frame cannot know
 * a code block's height, and folding long sources stays useful there. Only
 * the preview moved to the frame-decided clamp — its height is known, and the
 * measure-after-paint race on that path is what the html-block flicker fix
 * removes.
 */
function SelfCollapsingBody({ children }: { children: ReactNode }) {
  const { t } = useT("editor");
  const contentRef = useRef<HTMLDivElement>(null);
  const [contentHeight, setContentHeight] = useState(0);
  const [expanded, setExpanded] = useState(false);

  const measure = useCallback(() => {
    const el = contentRef.current;
    if (el) setContentHeight(el.offsetHeight);
  }, []);

  useLayoutEffect(() => {
    const el = contentRef.current;
    if (!el) return;
    measure();
    if (typeof ResizeObserver === "undefined") return;
    const observer = new ResizeObserver(measure);
    observer.observe(el);
    return () => observer.disconnect();
  }, [measure]);

  const collapsed = !expanded && contentHeight > DYNAMIC_BLOCK_COLLAPSE_AT_PX;
  const expandProps = useSmoothExpandStyle(collapsed, contentRef);

  return (
    <div
      className="relative overflow-hidden transition-[max-height] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none"
      data-collapsed={collapsed ? "" : undefined}
      style={expandProps.style}
      onTransitionEnd={expandProps.onTransitionEnd}
    >
      <div ref={contentRef}>{children}</div>
      {collapsed && (
        <>
          <div className="pointer-events-none absolute inset-x-0 bottom-0 h-24 bg-gradient-to-b from-transparent to-surface" />
          <Button
            type="button"
            variant="outline"
            size="sm"
            className="absolute bottom-3 left-1/2 -translate-x-1/2 bg-surface shadow-[var(--surface-shadow)]"
            onClick={() => setExpanded(true)}
          >
            <ChevronDown />
            {t(($) => $.dynamic_block.show_all)}
          </Button>
        </>
      )}
    </div>
  );
}

function DynamicBlockErrorPanel({
  kind,
  error,
  onViewSource,
}: {
  kind: DynamicBlockKind;
  error: DynamicBlockError;
  onViewSource: () => void;
}) {
  const { t } = useT("editor");
  const [copied, setCopied] = useState(false);
  const detail = error.line
    ? t(($) => $.dynamic_block.error_at_line, { message: error.message, line: error.line })
    : error.message;

  const handleCopyError = async () => {
    if (await copyText(detail)) {
      setCopied(true);
      setTimeout(() => setCopied(false), 2000);
    }
  };

  return (
    <div role="alert" className="flex gap-3 p-5 text-label">
      <TriangleAlert aria-hidden className="mt-0.5 size-4 shrink-0 text-destructive" />
      <div className="min-w-0 flex-1">
        <div className="font-semibold">
          {kind === "html"
            ? t(($) => $.dynamic_block.error_html)
            : t(($) => $.dynamic_block.error_mermaid)}
        </div>
        {/* The parser or script message is the only clue about what to fix. */}
        {detail && (
          <div className="mt-0.5 font-mono text-caption break-words whitespace-pre-wrap text-muted-foreground">
            {detail}
          </div>
        )}
        <div className="mt-2.5 flex flex-wrap gap-2">
          <Button type="button" variant="outline" size="sm" onClick={onViewSource}>
            <CodeXml />
            {t(($) => $.dynamic_block.view_source)}
          </Button>
          <Button type="button" variant="outline" size="sm" onClick={handleCopyError}>
            <CopyCheckIcon copied={copied} />
            {t(($) => $.dynamic_block.copy_error)}
          </Button>
        </div>
      </div>
    </div>
  );
}

/**
 * Loading state for a preview body: a quiet page outline at the height the
 * block will most likely take, so the finished render does not move the page.
 */
export function DynamicBlockSkeleton({ className }: { className?: string }) {
  const { t } = useT("editor");
  return (
    <div className={cn("flex h-full flex-col gap-3 px-5 py-[18px]", className)}>
      <div className="h-3 w-[38%] rounded-sm bg-muted" />
      <div className="min-h-3 flex-1 rounded-sm bg-muted" />
      <div className="flex items-center gap-1.5 text-caption text-muted-foreground">
        <Spinner aria-hidden className="size-3.5" />
        {t(($) => $.dynamic_block.rendering)}
      </div>
    </div>
  );
}