| 1 | import { useEffect, useState } from "react"; |
| 2 | import type { ContextPanelInfo } from "./types"; |
| 3 | |
| 4 | // The snapshot is refetched only on usage events, so a turn running tools |
| 5 | // between model requests keeps counting here instead of in the host. |
| 6 | export function useSessionRuntimeMs(info: Pick<ContextPanelInfo, "elapsedMs" | "activeTurnStartedAt"> | null | undefined): number { |
| 7 | const startedAt = info?.activeTurnStartedAt ?? 0; |
| 8 | const [now, setNow] = useState(() => Date.now()); |
| 9 | useEffect(() => { |
| 10 | if (startedAt <= 0) return; |
| 11 | setNow(Date.now()); |
| 12 | const timer = setInterval(() => setNow(Date.now()), 1000); |
| 13 | return () => clearInterval(timer); |
| 14 | }, [startedAt]); |
| 15 | const completed = Math.max(0, info?.elapsedMs ?? 0); |
| 16 | return startedAt > 0 ? completed + Math.max(0, now - startedAt) : completed; |
| 17 | } |
| 18 |