Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

5.9 KB
Raw Blame
import { ChevronDown, ChevronRight, Clock3, User } from "lucide-react";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { HumanizeTs } from "@/components/HumanizeTs";
import { TaskRunStatusIcon } from "@/components/TaskRunStatusIcon";
import { TaskRunLogViewer } from "@/components/task-run-log";
import { Button } from "@/components/ui/button";
import { EllipsisText } from "@/components/ui/ellipsis-text";
import {
  Sheet,
  SheetBody,
  SheetContent,
  SheetHeader,
  SheetTitle,
} from "@/components/ui/sheet";
import { useOnKeyChange } from "@/hooks/useOnKeyChange";
import {
  executorEmailOfTaskRun,
  formatTaskRunDuration,
  getTaskRunComment,
} from "@/lib/taskRun";
import { getTimeForPbTimestampProtoEs } from "@/types";
import type { TaskRun } from "@/types/proto-es/v1/rollout_service_pb";
import { TaskRunErrorAlert } from "./TaskRunErrorAlert";

// With this many runs or more, only the newest run starts expanded; older
// runs collapse to header rows. Keeps the sheet scannable and avoids fetching
// every run's log up front.
const COLLAPSE_OLDER_RUNS_THRESHOLD = 4;

export function DeployTaskRunHistorySheet({
  onOpenChange,
  open,
  taskRuns,
}: {
  onOpenChange: (open: boolean) => void;
  open: boolean;
  // Sorted newest-first by the caller (the page's per-task run grouping).
  taskRuns: TaskRun[];
}) {
  const { t } = useTranslation();

  return (
    <Sheet onOpenChange={onOpenChange} open={open}>
      <SheetContent width="standard">
        <SheetHeader>
          <SheetTitle>{t("task-run.history")}</SheetTitle>
        </SheetHeader>
        {/* SheetContent unmounts its children after the close animation, so
            the run list's expansion state resets on every open. */}
        <SheetBody className="gap-y-2 overscroll-contain">
          <TaskRunHistoryList taskRuns={taskRuns} />
        </SheetBody>
      </SheetContent>
    </Sheet>
  );
}

function TaskRunHistoryList({ taskRuns }: { taskRuns: TaskRun[] }) {
  const [expandedRunNames, setExpandedRunNames] = useState<Set<string>>(() =>
    taskRuns.length >= COLLAPSE_OLDER_RUNS_THRESHOLD
      ? new Set([taskRuns[0].name])
      : new Set(taskRuns.map((taskRun) => taskRun.name))
  );
  // A rerun created while the sheet is open prepends a new newest run; expand
  // it so the latest is visible without reopening the sheet.
  useOnKeyChange(taskRuns[0]?.name ?? "", () => {
    const newest = taskRuns[0]?.name;
    if (newest) {
      setExpandedRunNames((prev) =>
        prev.has(newest) ? prev : new Set(prev).add(newest)
      );
    }
  });
  const toggle = (taskRun: TaskRun) => {
    setExpandedRunNames((prev) => {
      const next = new Set(prev);
      if (next.has(taskRun.name)) next.delete(taskRun.name);
      else next.add(taskRun.name);
      return next;
    });
  };

  return (
    <>
      {taskRuns.map((taskRun, index) => (
        <TaskRunHistoryItem
          isExpanded={expandedRunNames.has(taskRun.name)}
          key={taskRun.name}
          onToggle={() => toggle(taskRun)}
          runNumber={taskRuns.length - index}
          taskRun={taskRun}
        />
      ))}
    </>
  );
}

function TaskRunHistoryItem({
  isExpanded,
  onToggle,
  runNumber,
  taskRun,
}: {
  isExpanded: boolean;
  onToggle: () => void;
  runNumber: number;
  taskRun: TaskRun;
}) {
  const { t } = useTranslation();
  const startMs =
    getTimeForPbTimestampProtoEs(taskRun.startTime) ??
    getTimeForPbTimestampProtoEs(taskRun.createTime);
  const duration = formatTaskRunDuration(taskRun);
  const executorEmail = executorEmailOfTaskRun(taskRun);
  const comment = getTaskRunComment(taskRun, t);

  return (
    <div className="rounded-sm border">
      <Button
        appearance="secondary"
        size="md"
        aria-expanded={isExpanded}
        className="h-auto w-full items-center justify-start gap-x-2 rounded-sm px-3 py-2 text-left whitespace-normal hover:bg-control-bg focus-visible:ring-2 focus-visible:ring-accent"
        onClick={onToggle}
        type="button"
      >
        {isExpanded ? (
          <ChevronDown className="size-4 shrink-0 text-control-light" />
        ) : (
          <ChevronRight className="size-4 shrink-0 text-control-light" />
        )}
        <TaskRunStatusIcon status={taskRun.status} />
        <span className="shrink-0 text-sm font-medium text-control">
          {t("task-run.run-number", { number: runNumber })}
        </span>
        {/* Only when collapsed: the expanded body shows the full error, so a
            header teaser there would just duplicate it. */}
        <span className="min-w-0 flex-1 text-xs text-control-light">
          {!isExpanded && (
            <EllipsisText className="line-clamp-1" text={comment} />
          )}
        </span>
        <span className="flex shrink-0 items-center gap-x-3 text-xs text-control-light">
          <HumanizeTs mode="compact" tsMs={startMs} />
          {duration && (
            <span className="flex items-center gap-x-1">
              <Clock3 className="size-3" />
              {duration}
            </span>
          )}
          {executorEmail && (
            <span className="hidden items-center gap-x-1 lg:flex">
              <User className="size-3" />
              {executorEmail}
            </span>
          )}
        </span>
      </Button>
      {isExpanded && (
        <div className="flex flex-col gap-2 border-t p-3">
          <TaskRunErrorAlert taskRun={taskRun} />
          {/* Status is part of the key (as in the latest-run panel): a
              RUNNING -> DONE flip remounts the viewer so useTaskRunLogData's
              unmount cleanup invalidates the in-flight RUNNING log request,
              which would otherwise cache an incomplete RUNNING log. */}
          <TaskRunLogViewer
            key={`logs-${taskRun.name}-${taskRun.status}`}
            taskRunName={taskRun.name}
            taskRunStatus={taskRun.status}
          />
        </div>
      )}
    </div>
  );
}