Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

7.1 KB
Raw Blame
"use client";

import { cva } from "class-variance-authority";
import {
  Check,
  ChevronsDownUp,
  ChevronsUpDown,
  Copy as CopyIcon,
  type IconComponent,
} from "@langfuse/icons";
import { type ReactNode, useState } from "react";

import { IconButton } from "../IconButton/IconButton";
import { SyntaxHighlight } from "../SyntaxHighlight/SyntaxHighlight";
import { Tooltip } from "../Tooltip/Tooltip";
import { useCopyToClipboard } from "@/src/hooks/useCopyToClipboard";

const bodyVariants = cva("relative flex min-w-0 max-w-full items-start", {
  variants: {
    variant: {
      filled: "bg-surface-section rounded-md",
      outline: "bg-background rounded-md border",
      plain: null,
    },
  },
  defaultVariants: {
    variant: "filled",
  },
});

const codeVariants = cva(
  "ph-no-capture min-w-0 max-w-full flex-1 px-4 py-3 font-mono text-xs",
  {
    variants: {
      wrap: {
        true: "wrap-break-word whitespace-pre-wrap",
        false: "overflow-x-auto whitespace-pre",
      },
      collapsed: {
        true: "line-clamp-6",
        false: "block",
      },
      // Keeps text clear of the floating body actions; unwrapped lines can scroll out from under them.
      reservedButtons: {
        0: null,
        1: "pr-10",
        2: "pr-17",
        3: "pr-24",
        4: "pr-31",
      },
    },
    defaultVariants: {
      wrap: true,
      collapsed: false,
    },
  },
);

const actionGroupVariants = cva(
  // Revealed on hover/focus only; touch devices have no hover, so the controls stay visible there.
  "flex min-w-0 shrink-0 items-center gap-1 group-hover/codesection:opacity-100 focus-within:opacity-100 pointer-coarse:opacity-100",
  {
    variants: {
      placement: {
        header: null,
        // Floats over the code so long lines keep the full width.
        body: "absolute top-1.5 right-1.5 rounded-md",
      },
      surface: {
        filled: "bg-surface-section",
        outline: "bg-background",
        plain: "bg-background",
      },
      // Copy feedback stays visible even when the pointer has already left.
      isPinned: {
        true: "opacity-100",
        false: "opacity-0",
      },
    },
    compoundVariants: [
      {
        // Invisible controls over the code must not swallow clicks on the text below.
        placement: "body",
        isPinned: false,
        class:
          "pointer-events-none group-hover/codesection:pointer-events-auto focus-within:pointer-events-auto pointer-coarse:pointer-events-auto",
      },
    ],
  },
);

type CodeSectionAction = {
  /** Stable React key; set it when `label` changes so focus survives the change. */
  id?: string;
  icon: IconComponent;
  label: string;
  onClick: () => void;
};

type CodeSectionProps = {
  /** Text copied by the built-in copy button; also the body when there are no children. */
  content: string;
  /** Custom body, e.g. highlighted variables; copy still uses `content`. */
  children?: ReactNode;
  /** Prism language name, e.g. `sh` or `json`, to syntax-highlight `content`. Ignored when `children` is set. */
  language?: string;
  /** Omitted = no header row; actions move into the body's top-right corner. */
  title?: string;
  variant?: "filled" | "outline" | "plain";
  /** Extra icon actions rendered before expand/collapse and copy. */
  actions?: CodeSectionAction[];
  /** Shown next to the check icon after copying. */
  copiedMessage?: string;
  /** Called after the built-in copy button wrote `content` to the clipboard. */
  onAfterCopy?: () => void;
  /** Clamps the body to six lines and adds an expand/collapse control. */
  isCollapsible?: boolean;
  defaultCollapsed?: boolean;
  shouldWrapLines?: boolean;
};

export function CodeSection({
  content,
  children,
  language,
  title,
  variant = "filled",
  actions = [],
  copiedMessage,
  onAfterCopy,
  isCollapsible = false,
  defaultCollapsed = false,
  shouldWrapLines = true,
}: CodeSectionProps) {
  const [isCollapsed, setIsCollapsed] = useState(
    isCollapsible && defaultCollapsed,
  );
  const { copy, isCopied } = useCopyToClipboard({
    successDuration: copiedMessage ? 3_000 : 1_000,
  });

  const handleCopy = async () => {
    try {
      await copy(content);
      onAfterCopy?.();
    } catch {
      // Clipboard writes can be rejected when the browser denies permission.
    }
  };

  const bodyButtonCount = Math.min(
    actions.length + (isCollapsible ? 1 : 0) + 1,
    4,
  ) as 1 | 2 | 3 | 4;

  const actionGroup = (
    <div
      className={actionGroupVariants({
        placement: title ? "header" : "body",
        surface: title ? undefined : variant,
        isPinned: isCopied,
      })}
    >
      {isCopied && copiedMessage ? (
        <span
          className="text-muted-foreground min-w-0 truncate text-xs"
          title={copiedMessage}
        >
          {copiedMessage}
        </span>
      ) : null}
      {actions.map((action) => (
        <Tooltip key={action.id ?? action.label} label={action.label}>
          {({ getTriggerProps }) => (
            <IconButton
              {...getTriggerProps()}
              icon={action.icon}
              label={action.label}
              size="sm"
              variant="ghost"
              onClick={action.onClick}
            />
          )}
        </Tooltip>
      ))}
      {isCollapsible ? (
        <Tooltip label={isCollapsed ? "Expand" : "Collapse"}>
          {({ getTriggerProps }) => (
            <IconButton
              {...getTriggerProps()}
              icon={isCollapsed ? ChevronsUpDown : ChevronsDownUp}
              label={isCollapsed ? "Expand" : "Collapse"}
              size="sm"
              variant="ghost"
              aria-expanded={!isCollapsed}
              onClick={() => setIsCollapsed((value) => !value)}
            />
          )}
        </Tooltip>
      ) : null}
      <Tooltip label={isCopied ? "Copied" : "Copy"}>
        {({ getTriggerProps }) => (
          <IconButton
            {...getTriggerProps()}
            icon={isCopied ? Check : CopyIcon}
            label="Copy to clipboard"
            size="sm"
            variant="ghost"
            onClick={handleCopy}
          />
        )}
      </Tooltip>
    </div>
  );

  return (
    <div className="group/codesection flex max-w-full min-w-0 flex-col gap-1">
      {title ? (
        <div className="flex h-6 shrink-0 items-center justify-between gap-2">
          <div className="min-w-0 truncate text-sm font-bold" title={title}>
            {title}
          </div>
          {actionGroup}
        </div>
      ) : null}
      <div className={bodyVariants({ variant })}>
        <code
          className={codeVariants({
            wrap: shouldWrapLines,
            collapsed: isCollapsed,
            reservedButtons: title ? 0 : bodyButtonCount,
          })}
          dir="auto"
          style={{ unicodeBidi: "plaintext" }}
        >
          {children ??
            (language === undefined ? (
              content
            ) : (
              <SyntaxHighlight code={content} language={language}>
                {({ content: highlighted }) => highlighted}
              </SyntaxHighlight>
            ))}
        </code>
        {title ? null : actionGroup}
      </div>
    </div>
  );
}