Skip to content

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

3.5 KB
Raw Blame
/**
 * LogViewExpandedContent - Content shown when a log view row is expanded.
 *
 * Only renders the I/O data (PrettyJsonView), not the header row.
 * The header row is handled by JSONTableView.
 */

import { PrettyJsonView } from "@/src/components/ui/PrettyJsonView";
import { memo, useMemo } from "react";

import { type TreeNode } from "@/src/features/traces/types/treeNode";
import { useLogViewObservationIO } from "./useLogViewObservationIO";
import { Spinner } from "@/src/components/design-system/Spinner/Spinner";
import {
  type JsonViewPreference,
  toPrettyOrJsonView,
} from "@/src/components/ui/jsonViewPreference";

export interface LogViewExpandedContentProps {
  node: TreeNode;
  traceId: string;
  projectId: string;
  currentView?: JsonViewPreference;
  /** Optional external expansion state for JSON tree (non-virtualized mode) */
  externalExpansionState?: Record<string, boolean> | boolean;
  /** Callback when expansion state changes (non-virtualized mode) */
  onExternalExpansionChange?: (
    expansion: Record<string, boolean> | boolean,
  ) => void;
}

/**
 * Expanded content with full observation data as JSON.
 * Fetches observation data lazily when mounted.
 */
export const LogViewExpandedContent = memo(function LogViewExpandedContent({
  node,
  traceId,
  projectId,
  currentView = "pretty",
  externalExpansionState,
  onExternalExpansionChange,
}: LogViewExpandedContentProps) {
  // Fetch I/O data lazily
  const { data, isLoading, isError } = useLogViewObservationIO({
    observationId: node.id,
    traceId,
    projectId,
    startTime: node.startTime,
    enabled: true, // Always enabled when mounted (row is expanded)
  });

  // Build JSON object with all observation properties
  const jsonData = useMemo(() => {
    if (!data) return null;

    // Filter out null/undefined values for cleaner display
    const result: Record<string, unknown> = {};

    if (data.input !== null && data.input !== undefined) {
      result.input = data.input;
    }
    if (data.output !== null && data.output !== undefined) {
      result.output = data.output;
    }
    if (data.metadata !== null && data.metadata !== undefined) {
      result.metadata = data.metadata;
    }

    return Object.keys(result).length > 0 ? result : null;
  }, [data]);

  return (
    <div className="w-full [&_.border]:border-0 [&_.io-message-content]:p-0 [&_.rounded-sm]:rounded-none [&_td:first-child]:pl-6 [&_th]:h-6 [&_th]:text-xs [&_th:first-child]:pl-6">
      {isLoading && (
        <div className="flex items-center justify-center py-4">
          <Spinner size="sm" variant="muted" />
          <span className="text-muted-foreground ml-2 text-xs">Loading...</span>
        </div>
      )}

      {isError && (
        <div className="bg-destructive/10 text-destructive flex h-full w-full items-center px-6 py-2 text-xs">
          Failed to load data
        </div>
      )}

      {jsonData && !isLoading && (
        <PrettyJsonView
          json={jsonData}
          currentView={toPrettyOrJsonView(currentView)}
          isLoading={false}
          showNullValues={false}
          externalExpansionState={externalExpansionState}
          onExternalExpansionChange={onExternalExpansionChange}
          stickyTopLevelKey={false}
          showObservationTypeBadge
          scrollable
        />
      )}

      {!jsonData && !isLoading && !isError && (
        <div className="text-muted-foreground py-2 pl-6 text-xs">
          No input/output/metadata
        </div>
      )}
    </div>
  );
});