The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { RuntimeSubagent } from "@t3tools/client-runtime/state/subagentRuntime"; |
| 2 | import { isOrchestrationV2WorkActive } from "@t3tools/contracts"; |
| 3 | import { deriveSubagentElapsedMs } from "@t3tools/shared/orchestrationTiming"; |
| 4 | import { msg, useMessages } from "gt-react"; |
| 5 | import type { MessageResolver } from "~/i18n/messages"; |
| 6 | import { tickTextContent } from "~/lib/tickTextContent"; |
| 7 | |
| 8 | function formatElapsedSeconds(totalSeconds: number, resolve: MessageResolver): string { |
| 9 | const seconds = Math.max(0, Math.floor(totalSeconds)); |
| 10 | const minutes = Math.floor(seconds / 60); |
| 11 | if (minutes === 0) { |
| 12 | return resolve(msg("{seconds}s", { seconds, $context: "elapsed time, seconds" })); |
| 13 | } |
| 14 | const hours = Math.floor(minutes / 60); |
| 15 | if (hours === 0) { |
| 16 | return resolve( |
| 17 | msg("{minutes}m {seconds}s", { |
| 18 | minutes, |
| 19 | seconds: String(seconds % 60).padStart(2, "0"), |
| 20 | $context: "elapsed time, minutes and seconds", |
| 21 | }), |
| 22 | ); |
| 23 | } |
| 24 | return resolve( |
| 25 | msg("{hours}h {minutes}m", { |
| 26 | hours, |
| 27 | minutes: String(minutes % 60).padStart(2, "0"), |
| 28 | $context: "elapsed time, hours and minutes", |
| 29 | }), |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | function formatCompactElapsedSeconds(totalSeconds: number, resolve: MessageResolver): string { |
| 34 | const seconds = Math.max(0, Math.floor(totalSeconds)); |
| 35 | if (seconds < 60) |
| 36 | return resolve(msg("{seconds}s", { seconds, $context: "elapsed time, seconds" })); |
| 37 | const minutes = Math.floor(seconds / 60); |
| 38 | if (minutes < 60) |
| 39 | return resolve(msg("{minutes}m", { minutes, $context: "elapsed time, minutes" })); |
| 40 | if (minutes >= 600) { |
| 41 | return resolve( |
| 42 | msg("{hours}h", { hours: Math.floor(minutes / 60), $context: "elapsed time, hours" }), |
| 43 | ); |
| 44 | } |
| 45 | return resolve( |
| 46 | msg("{hours}h", { hours: Math.floor(minutes / 6) / 10, $context: "elapsed time, hours" }), |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | export function AgentElapsed({ |
| 51 | agent, |
| 52 | compact = false, |
| 53 | }: { |
| 54 | agent: Pick<RuntimeSubagent, "status" | "startedAt" | "completedAt">; |
| 55 | compact?: boolean; |
| 56 | }) { |
| 57 | const m = useMessages(); |
| 58 | const format = compact ? formatCompactElapsedSeconds : formatElapsedSeconds; |
| 59 | const live = isOrchestrationV2WorkActive(agent.status); |
| 60 | const { status, startedAt, completedAt } = agent; |
| 61 | if (deriveSubagentElapsedMs(agent, 0) === null) { |
| 62 | return null; |
| 63 | } |
| 64 | return ( |
| 65 | <span |
| 66 | ref={tickTextContent((nowMs) => { |
| 67 | const elapsedMs = deriveSubagentElapsedMs({ status, startedAt, completedAt }, nowMs); |
| 68 | return elapsedMs === null ? "" : format(elapsedMs / 1000, m); |
| 69 | }, live)} |
| 70 | className="tabular-nums" |
| 71 | /> |
| 72 | ); |
| 73 | } |