Skip to content

The platform for continuously improving AI agents.

3.7 KB
Raw Blame
// Height easing adapted from AI SDK Agents step-inspector; see NOTICE.md.
import { type ReactNode, useEffect, useLayoutEffect, useRef } from "react";

import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { Icon } from "@/components/ui/icons";
import { StatusBadge } from "@/lib/job-status";

function useReportHeight(open: boolean) {
  const ref = useRef<HTMLDivElement>(null);
  const last = useRef<number | null>(null);
  useEffect(() => {
    const element = ref.current;
    if (!element || typeof ResizeObserver === "undefined") return;
    const observer = new ResizeObserver(() => {
      if (element.style.height === "") last.current = element.offsetHeight;
    });
    observer.observe(element);
    return () => observer.disconnect();
  }, []);
  // biome-ignore lint/correctness/useExhaustiveDependencies: Expansion replaces the measured content; telemetry updates keep their natural height.
  useLayoutEffect(() => {
    const element = ref.current;
    if (!element) return;
    const previous = last.current;
    const next = element.offsetHeight;
    last.current = next;
    if (
      previous === null ||
      previous === next ||
      window.matchMedia?.("(prefers-reduced-motion: reduce)").matches
    )
      return;
    element.style.height = `${previous}px`;
    element.style.overflow = "hidden";
    // Commit the starting height before easing to the newly rendered report.
    void element.offsetHeight;
    element.style.transition = "height 200ms cubic-bezier(0.25, 1, 0.5, 1)";
    element.style.height = `${next}px`;
    const clear = () => {
      element.style.height = "";
      element.style.overflow = "";
      element.style.transition = "";
    };
    const timer = setTimeout(clear, 220);
    return () => {
      clearTimeout(timer);
      clear();
    };
  }, [open]);
  return ref;
}

export function ExecutionCard({
  title,
  embedded = false,
  status,
  open,
  onOpenChange,
  onInspect,
  children,
  summary,
}: {
  title: string;
  embedded?: boolean;
  status?: string;
  open: boolean;
  onOpenChange: (open: boolean) => void;
  onInspect?: () => void;
  children: ReactNode;
  summary?: ReactNode;
}) {
  const height = useReportHeight(open);
  return (
    <Card
      className={`w-full overflow-hidden rounded-md ${embedded ? "border-0 bg-transparent" : ""}`}
    >
      <div className="flex items-center gap-2 px-3 py-3">
        <button
          aria-expanded={open}
          className="flex min-w-0 flex-1 items-center gap-2 rounded-sm text-left outline-none focus-visible:ring-2 focus-visible:ring-ring/60"
          onClick={() => onOpenChange(!open)}
          type="button"
        >
          <span className="flex size-6 shrink-0 items-center justify-center rounded-sm bg-cat-3/10 text-cat-3">
            <Icon.chart className="size-3.5" />
          </span>
          <span className="min-w-0 flex-1 truncate text-xs font-medium text-foreground">
            {title}
          </span>
          <Icon.chevronDown
            className={`size-4 shrink-0 transition-transform duration-150 ease-out motion-reduce:transition-none ${open ? "rotate-180" : ""}`}
          />
        </button>
        {status && <StatusBadge fallback="queued" status={status} />}
        {onInspect && (
          <Button
            aria-label={`Inspect ${title.toLowerCase()}`}
            onClick={onInspect}
            size="icon-xs"
            variant="ghost"
          >
            <Icon.expand />
          </Button>
        )}
      </div>
      <div ref={height}>
        {!open && summary && <div className="px-3 pb-3">{summary}</div>}
        {open && <div className="max-h-[60vh] overflow-auto px-3 pb-3">{children}</div>}
      </div>
    </Card>
  );
}