import {
AlertTriangleIcon,
ArrowRightIcon,
CpuIcon,
LaptopIcon,
MonitorIcon,
MonitorOffIcon,
ServerIcon,
UsersIcon,
} from "lucide-react";
import { cn } from "../lib/cn";
import { formatRelativeTime } from "../lib/format";
import { machineScopeLabel } from "../sandbox-label-model";
import type { MachineView, MetricSample } from "../types/machines";
import { connectionStatusForState } from "../types/machines";
import { MachineHealthPill } from "./machine-health-pill";
import { MachineMetrics } from "./machine-metrics";
import { AgentStatusIcon } from "./machines/agent-status-icon";
import { describeAgentRuntime, runnerConflict, STATUS_TONE_TEXT } from "./machines/machine-status";
import { MetricSparkline } from "./machines/metric-sparkline";
import { useOpenGeniText } from "../messages/runtime";
export type MachineCardProps = {
machine: MachineView;
/** Attach/swap the session's active sandbox to this machine. */
onAttach?: ((machine: MachineView) => void) | undefined;
/** Whether an attach/swap is currently in flight (disables the affordance). */
attaching?: boolean | undefined;
/** Start/retry the exact signed Connected Machine agent update. */
onUpdateAgent?: ((machine: MachineView) => void) | undefined;
updatingAgent?: boolean | undefined;
/** Short recent history (e.g. 15m) — drives the card's CPU trend + preview. */
series?: MetricSample[] | undefined;
/** Open the full per-machine telemetry detail. Makes the whole card actionable. */
onOpenDetail?: ((machine: MachineView) => void) | undefined;
now?: number | undefined;
className?: string | undefined;
};
function KindIcon({ machine }: { machine: MachineView }) {
if (machine.kind === "local" || machine.kind === "selfhosted")
return ;
if (machine.kind === "docker" || machine.isSessionGroup)
return ;
if (machine.kind === "modal") return ;
return ;
}
/**
* One machine in the Machines dashboard: name + kind + OS/arch, the fused HEALTH
* verdict, latest resource meters, a CPU trend that previews the history, live
* freshness, and attach/swap. The whole card opens the telemetry detail when
* `onOpenDetail` is wired. The session's active sandbox carries an accent edge.
*/
export function MachineCard({
machine,
onAttach,
attaching,
onUpdateAgent,
updatingAgent,
series,
onOpenDetail,
now,
className,
}: MachineCardProps) {
const msg = useOpenGeniText();
const offline = machine.state === "offline";
const attachable = !machine.active && !offline && Boolean(onAttach);
const shared = machine.sharedSessionCount > 1;
const clockNow = now ?? Date.now();
const sampledAgo = machine.metrics
? formatRelativeTime(msg, machine.metrics.sampledAt, new Date(clockNow))
: null;
const cpuPts = (series ?? []).map((s) => ({ t: new Date(s.sampledAt).getTime(), v: s.cpuPct }));
const openable = Boolean(onOpenDetail);
const runtime = machine.runtime;
const agent = runtime
? describeAgentRuntime(
msg,
runtime,
clockNow,
connectionStatusForState(machine.state) === "online",
)
: null;
const updateAction =
agent?.action && machine.enrollmentId && onUpdateAgent && !offline ? agent.action : null;
const conflict = runnerConflict(machine.connectionAuthority, clockNow);
return (
onOpenDetail?.(machine) : undefined}
onKeyDown={
openable
? (e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
onOpenDetail?.(machine);
}
}
: undefined
}
className={cn(
"og-root group relative flex flex-col gap-3 overflow-hidden rounded-og-lg border border-og-border",
"bg-og-surface-1 p-4 shadow-og-sm transition-colors",
openable &&
"cursor-pointer hover:border-og-border-strong focus-visible:border-og-accent/60 focus-visible:outline-hidden",
machine.active && "border-og-accent/40",
className,
)}
>
{machine.active ? (
) : null}
{machine.name}
{machine.active ? (
{msg("machineCard.active")}
) : null}
{machineScopeLabel(msg, machine.scope)}
·
{machine.kind}
·
{machine.os}/{machine.arch}
·
{machine.hasDisplay ? (
) : (
)}
{machine.hasDisplay ? msg("machineCard.display") : msg("machineCard.headless")}
{shared ? (
{msg("machineCard.sharedSharedsessioncountSessionsAreOn", {
sharedSessionCount: machine.sharedSessionCount,
})}
) : null}
{conflict?.active ? (
{msg("machineCard.aSecondCopyOfThe")}
) : null}
{agent ? (
{agent.status}
{agent.note ?
{agent.note}
: null}
{updateAction ? (
) : null}
) : null}
{/* CPU trend — previews the history and invites opening the detail. */}
{cpuPts.length > 1 ? (
{msg("machineCard.cpuLast15m")}
{openable ? (
{msg("machineCard.telemetry")}
) : null}
) : null}
{sampledAgo ? (
<>
{sampledAgo === "now"
? msg("machineCard.live")
: msg("machineCard.updatedSampledagoAgo", { sampledAgo })}
>
) : machine.lastSeenAt ? (
<>
{msg("machineCard.lastSeen")}{" "}
{formatRelativeTime(msg, machine.lastSeenAt, new Date(clockNow))}
>
) : (
msg("machineCard.neverConnected")
)}
{machine.active ? (
{msg("machineCard.routingHere")}
) : attachable ? (
) : (
{offline ? msg("machineCard.unavailable") : "—"}
)}
);
}