Skip to content

πŸ™Œ OpenHands: AI-Driven Development

4.8 KB
Raw Blame
import type { ReactNode } from "react";
import { Zap } from "lucide-react";
import { useTranslation } from "react-i18next";
import type { LatestAutomationRunState } from "#/hooks/query/use-latest-automation-runs";
import { I18nKey } from "#/i18n/declaration";
import ClockIcon from "#/icons/clock.svg?react";
import {
  resolveRunPhaseText,
  shouldShowRunPhase,
} from "#/components/features/automations/detail/run-phase";
import type { Automation, AutomationRun } from "#/types/automation";
import {
  getAutomationRunBadgeLabelKey,
  getAutomationRunDisplay,
} from "#/utils/automation-run-display";
import { formatRelativeTime } from "#/utils/format-relative-time";
import { AutomationHealthIndicator } from "./automation-health-indicator";
import {
  deriveRunHealth,
  getLastRunTimestamp,
  getRunHealthLabelKey,
  getTriggerSummary,
} from "./automation-run-health";
import {
  getDisablementReasonDisplay,
  hasDisablementReason,
} from "#/utils/automation-disabled-reason";

export function getRunStatusLabelKey(
  runState: LatestAutomationRunState,
): I18nKey {
  if (runState.isError) return I18nKey.FEATURED_AUTOMATIONS$STATUS_UNAVAILABLE;
  if (runState.latestRun) {
    return getAutomationRunBadgeLabelKey(
      getAutomationRunDisplay(runState.latestRun).badgeStatus,
    );
  }
  return getRunHealthLabelKey(deriveRunHealth(runState));
}

function PreviewRow({
  label,
  children,
}: {
  label: string;
  children: ReactNode;
}) {
  return (
    <div className="flex items-start gap-2 text-xs">
      <span className="w-20 shrink-0 text-muted">{label}</span>
      <span className="min-w-0 flex-1 break-words text-foreground">
        {children}
      </span>
    </div>
  );
}

/**
 * The run's phase. The row that opens this hovercard has to clip a long
 * phase; here there is room to wrap, so this is where the whole thing is
 * readable. What the phase says comes from the same helper the row uses.
 */
function PhaseRow({ run }: { run: AutomationRun | null }) {
  const { t } = useTranslation("openhands");
  const text = resolveRunPhaseText(run?.current_phase);

  if (!run || !shouldShowRunPhase(run.status) || !text) return null;

  return (
    <PreviewRow label={t(I18nKey.AUTOMATIONS$DETAIL$PHASE)}>
      <span data-testid="run-phase-row">{text}</span>
    </PreviewRow>
  );
}

/**
 * Left-nav-style hovercard body for an automation's latest-run health.
 * Matches `ConversationCardPreview` layout: title + label/value rows.
 */
export function HomeAutomationRunTooltip({
  automation,
  runState,
}: {
  automation: Automation;
  runState: LatestAutomationRunState;
}) {
  const { t, i18n } = useTranslation("openhands");
  const { latestRun } = runState;
  const timestamp = latestRun ? getLastRunTimestamp(latestRun) : null;
  const TriggerIcon = automation.trigger.type === "event" ? Zap : ClockIcon;
  const health = deriveRunHealth(runState);
  const display = latestRun ? getAutomationRunDisplay(latestRun) : null;
  const disabledReason = hasDisablementReason(automation)
    ? getDisablementReasonDisplay(automation, t)
    : null;

  return (
    <div className="flex w-70 flex-col gap-3 p-3">
      <span className="break-words text-sm font-medium text-contrast">
        {automation.name}
      </span>

      <dl className="flex flex-col gap-1.5">
        <PreviewRow label={t(I18nKey.AUTOMATIONS$DETAIL$TRIGGER)}>
          <span className="inline-flex min-w-0 items-start gap-1.5">
            <TriggerIcon
              className="mt-0.5 size-3 shrink-0"
              aria-hidden="true"
            />
            <span className="break-all">{getTriggerSummary(automation)}</span>
          </span>
        </PreviewRow>

        <PreviewRow label={t(I18nKey.COMMON$STATUS)}>
          <span className="inline-flex min-w-0 items-center gap-1.5">
            <AutomationHealthIndicator health={health} />
            <span>{t(getRunStatusLabelKey(runState))}</span>
          </span>
        </PreviewRow>

        {disabledReason ? (
          <PreviewRow
            label={t(I18nKey.AUTOMATIONS$DETAIL$DISABLED_REASON_HEADING)}
          >
            <span
              data-testid="automation-tooltip-disabled-reason"
              className="line-clamp-3 text-text-secondary"
            >
              {disabledReason.text}
            </span>
          </PreviewRow>
        ) : null}

        <PhaseRow run={latestRun} />

        {timestamp ? (
          <PreviewRow label={t(I18nKey.AUTOMATIONS$DETAIL$LAST_RUN)}>
            {formatRelativeTime(timestamp, i18n.language, t)}
          </PreviewRow>
        ) : null}

        {display?.summary ? (
          <PreviewRow label={t(I18nKey.AUTOMATIONS$DETAIL$TASK_LABEL)}>
            <span className="line-clamp-3 text-text-secondary">
              {display.summary}
            </span>
          </PreviewRow>
        ) : null}
      </dl>
    </div>
  );
}