Skip to content

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

3.1 KB
Raw Blame
/**
 * JSONTableViewRow - Row component for JSONTableView.
 *
 * Renders a single row with columns and handles expand/collapse.
 */

import { memo } from "react";
import { ChevronRight, ChevronDown } from "@langfuse/icons";
import { cn } from "@/src/utils/tailwind";
import { getPlainTextFromReactNode } from "@/src/utils/react-node-plain-text";
import { useClickWithoutSelection } from "@/src/hooks/useClickWithoutSelection";
import { type JSONTableViewRowProps } from "./json-table-view-types";

/**
 * Row component that renders column cells and handles expand/collapse.
 */
function JSONTableViewRowInner<T>({
  item,
  itemKey,
  index,
  columns,
  isExpanded,
  expandable,
  onToggle,
  renderExpanded,
  renderRowPrefix,
}: JSONTableViewRowProps<T>) {
  // Use click-without-selection to allow text selection while still supporting expand
  const { props: clickProps } = useClickWithoutSelection({
    onClick: onToggle,
    enabled: expandable,
  });

  const ChevronIcon = isExpanded ? ChevronDown : ChevronRight;
  const expandedContentId = `expanded-content-${itemKey}`;

  return (
    <div className="border-border bg-surface border-b">
      {/* Preview row - always visible */}
      <div
        className={cn(
          "flex min-h-6 items-center gap-2 px-3 py-0.5",
          expandable && "hover:bg-muted/50 cursor-pointer",
          isExpanded && "border-border/50 bg-muted/30 border-b",
        )}
        {...(expandable ? clickProps : {})}
        role={expandable ? "button" : undefined}
        tabIndex={expandable ? 0 : undefined}
        aria-expanded={expandable ? isExpanded : undefined}
        aria-controls={expandable ? expandedContentId : undefined}
        onKeyDown={
          expandable
            ? (e) => {
                if (e.key === "Enter" || e.key === " ") {
                  e.preventDefault();
                  onToggle();
                }
              }
            : undefined
        }
      >
        {/* Prefix content (e.g., tree indentation) */}
        {renderRowPrefix && renderRowPrefix(item, isExpanded)}

        {/* Expand icon */}
        {expandable && <ChevronIcon size="base" color="muted" />}

        {/* Column cells */}
        {columns.map((column) => {
          const renderedCell = column.render(item, index);

          return (
            <div
              key={column.key}
              className={cn(
                "shrink-0 text-sm",
                column.width ?? "min-w-0 flex-1",
                column.align === "right" && "text-right",
                // Truncate flex-1 columns
                column.width === "flex-1" && "truncate",
              )}
              title={
                column.width === "flex-1"
                  ? getPlainTextFromReactNode(renderedCell)
                  : undefined
              }
            >
              {renderedCell}
            </div>
          );
        })}
      </div>

      {/* Expanded content */}
      {isExpanded && renderExpanded && (
        <div id={expandedContentId} className="w-full">
          {renderExpanded(item)}
        </div>
      )}
    </div>
  );
}

export const JSONTableViewRow = memo(
  JSONTableViewRowInner,
) as typeof JSONTableViewRowInner;