Open History to see the last commit
1import type { RuntimeSubagent } from "@t3tools/client-runtime/state/subagentRuntime";
2import { useEffect, useRef } from "react";
3
4function formatElapsedSeconds(totalSeconds: number): string {
5 const seconds = Math.max(0, Math.floor(totalSeconds));
6 const minutes = Math.floor(seconds / 60);
7 if (minutes === 0) {
8 return `${seconds}s`;
9 }
10 const hours = Math.floor(minutes / 60);
11 if (hours === 0) {
12 return `${minutes}m ${String(seconds % 60).padStart(2, "0")}s`;
13 }
14 return `${hours}h ${String(minutes % 60).padStart(2, "0")}m`;
15}
16
17function elapsedBetween(startedAt: string, endIso: string | null): string {
18 const start = Date.parse(startedAt);
19 const end = endIso ? Date.parse(endIso) : Date.now();
20 if (Number.isNaN(start) || Number.isNaN(end)) {
21 return "";
22 }
23 return formatElapsedSeconds((end - start) / 1000);
24}
25
26/**
27 * Elapsed time for the current activation. Live agents self-tick via DOM
28 * writes (zero React commits per tick); settled agents freeze at completedAt.
29 */
30export function AgentElapsed({ agent }: { agent: RuntimeSubagent }) {
31 const textRef = useRef<HTMLSpanElement>(null);
32 const live = agent.status === "running" || agent.status === "waiting";
33 const startedAt = agent.startedAt;
34
35 useEffect(() => {
36 if (!live || !startedAt) {
37 return;
38 }
39 const update = () => {
40 if (textRef.current) {
41 textRef.current.textContent = elapsedBetween(startedAt, null);
42 }
43 };
44 update();
45 const id = setInterval(update, 1000);
46 return () => clearInterval(id);
47 }, [live, startedAt]);
48
49 if (!startedAt) {
50 return null;
51 }
52 return (
53 <span ref={textRef} className="tabular-nums">
54 {elapsedBetween(startedAt, live ? null : agent.completedAt)}
55 </span>
56 );
57}