GeezerVison is showing github.com — refactor(web): running timers share one clock instead of an interval … · pingdotgg/t3code@451bb45
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +10-30Lines changed: 10 additions & 30 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { RuntimeSubagent } from "@t3tools/client-runtime/state/subagentRuntime"; | 2 | 2 | import { isOrchestrationV2WorkActive } from "@t3tools/contracts"; | 3 | 3 | import { deriveSubagentElapsedMs } from "@t3tools/shared/orchestrationTiming"; | 4 | | -import { useEffect, useRef } from "react"; | | 4 | +import { tickTextContent } from "~/lib/tickTextContent"; | 5 | 5 |
| 6 | 6 | function formatElapsedSeconds(totalSeconds: number): string { | 7 | 7 | const seconds = Math.max(0, Math.floor(totalSeconds)); | @@ -33,38 +33,18 @@ export function AgentElapsed({ | 33 | 33 | compact?: boolean; | 34 | 34 | }) { | 35 | 35 | const format = compact ? formatCompactElapsedSeconds : formatElapsedSeconds; | 36 | | - const textRef = useRef<HTMLSpanElement>(null); | 37 | 36 | const live = isOrchestrationV2WorkActive(agent.status); | 38 | | - const startedAt = agent.startedAt; | 39 | | - const completedAt = agent.completedAt; | 40 | | - | 41 | | - useEffect(() => { | 42 | | - if (!startedAt) { | 43 | | - return; | 44 | | - } | 45 | | - const update = () => { | 46 | | - if (textRef.current) { | 47 | | - const elapsedMs = deriveSubagentElapsedMs( | 48 | | - { status: agent.status, startedAt, completedAt }, | 49 | | - Date.now(), | 50 | | - ); | 51 | | - const text = elapsedMs === null ? "" : format(elapsedMs / 1000); | 52 | | - if (textRef.current.textContent !== text) textRef.current.textContent = text; | 53 | | - } | 54 | | - }; | 55 | | - update(); | 56 | | - if (!live) return; | 57 | | - const id = setInterval(update, 1000); | 58 | | - return () => clearInterval(id); | 59 | | - }, [live, startedAt, completedAt, agent.status, format]); | 60 | | - | 61 | | - const elapsedMs = deriveSubagentElapsedMs(agent, 0); | 62 | | - if (elapsedMs === null) { | | 37 | + const { status, startedAt, completedAt } = agent; | | 38 | + if (deriveSubagentElapsedMs(agent, 0) === null) { | 63 | 39 | return null; | 64 | 40 | } | 65 | 41 | return ( | 66 | | - <span ref={textRef} className="tabular-nums"> | 67 | | - {format(elapsedMs / 1000)} | 68 | | - </span> | | 42 | + <span | | 43 | + ref={tickTextContent((nowMs) => { | | 44 | + const elapsedMs = deriveSubagentElapsedMs({ status, startedAt, completedAt }, nowMs); | | 45 | + return elapsedMs === null ? "" : format(elapsedMs / 1000); | | 46 | + }, live)} | | 47 | + className="tabular-nums" | | 48 | + /> | 69 | 49 | ); | 70 | 50 | } |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/chat/ProviderSubagentBar.tsx +2-16Lines changed: 2 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | } from "@t3tools/client-runtime/state/thread-execution"; | 5 | 5 | import { isOrchestrationV2WorkActive } from "@t3tools/contracts"; | 6 | 6 | import { ArrowUpLeftIcon } from "lucide-react"; | 7 | | -import { useLayoutEffect, useRef } from "react"; | 8 | 7 |
| | 8 | +import { tickTextContent } from "~/lib/tickTextContent"; | 9 | 9 | import type { ProviderInstanceEntry } from "../../providerInstances"; | 10 | 10 | import { Button } from "../ui/button"; | 11 | 11 | import { ProviderInstanceIcon } from "./ProviderInstanceIcon"; | @@ -18,7 +18,6 @@ export function ProviderSubagentBar(props: { | 18 | 18 | readonly status: ProviderSubagentStatus | null; | 19 | 19 | readonly onOpenParent: (() => void) | null; | 20 | 20 | }) { | 21 | | - const statusRef = useRef<HTMLSpanElement>(null); | 22 | 21 | const { status } = props; | 23 | 22 | const live = status !== null && isOrchestrationV2WorkActive(status.status); | 24 | 23 | const modelDescription = | @@ -28,19 +27,6 @@ export function ProviderSubagentBar(props: { | 28 | 27 | 0, | 29 | 28 | ); | 30 | 29 |
| 31 | | - // Write timer labels to the DOM to avoid chat renders on each tick. | 32 | | - useLayoutEffect(() => { | 33 | | - const update = () => { | 34 | | - if (statusRef.current) { | 35 | | - statusRef.current.textContent = formatProviderSubagentStatus(status, Date.now()); | 36 | | - } | 37 | | - }; | 38 | | - update(); | 39 | | - if (!live) return; | 40 | | - const id = setInterval(update, 1_000); | 41 | | - return () => clearInterval(id); | 42 | | - }, [live, status]); | 43 | | - | 44 | 30 | // Sized by the composer, not the viewport: a docked side panel narrows the | 45 | 31 | // chat column on a wide window, and one row there squeezes out the model | 46 | 32 | // and status, so the status drops to its own line instead. | @@ -67,7 +53,7 @@ export function ProviderSubagentBar(props: { | 67 | 53 | )} | 68 | 54 | </span> | 69 | 55 | <span | 70 | | - ref={statusRef} | | 56 | + ref={tickTextContent((nowMs) => formatProviderSubagentStatus(status, nowMs), live)} | 71 | 57 | aria-hidden | 72 | 58 | className="min-w-0 max-w-full truncate text-muted-foreground tabular-nums @max-[560px]/composer-surface:text-xs" | 73 | 59 | /> | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/chat/TimelineMinimap.tsx +4-7Lines changed: 4 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { resolveTimelineMinimapPreview, type TimelineMinimapItem } from "./timelineMinimapItems"; | 2 | | -import { useEffect, useRef, useState, type MouseEvent } from "react"; | | 2 | +import { useRef, useState, type MouseEvent } from "react"; | 3 | 3 | import { ChevronDownIcon, ChevronUpIcon } from "lucide-react"; | 4 | 4 | import { Button } from "../ui/button"; | 5 | 5 | import { | @@ -61,11 +61,6 @@ export function TimelineMinimap({ | 61 | 61 | const pressedOnStripRef = useRef(false); | 62 | 62 | const [passThrough, setPassThrough] = useState(false); | 63 | 63 |
| 64 | | - useEffect(() => { | 65 | | - if (!passThrough) return; | 66 | | - return onMouseButtonsReleased(() => setPassThrough(false)); | 67 | | - }, [passThrough]); | 68 | | - | 69 | 64 | const resolvedActiveIndex = | 70 | 65 | activeIndex !== null && activeIndex < items.length ? activeIndex : null; | 71 | 66 | const activeItem = resolveTimelineMinimapPreview( | @@ -143,7 +138,9 @@ export function TimelineMinimap({ | 143 | 138 | }); | 144 | 139 | }} | 145 | 140 | onMouseMove={(event) => { | 146 | | - if (event.buttons !== 0 && !pressedOnStripRef.current) setPassThrough(true); | | 141 | + if (event.buttons === 0 || pressedOnStripRef.current || passThrough) return; | | 142 | + setPassThrough(true); | | 143 | + onMouseButtonsReleased(() => setPassThrough(false)); | 147 | 144 | }} | 148 | 145 | style={{ | 149 | 146 | height: resolveTimelineMinimapHeightStyle(items.length), | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/chat/TimelineWorkGroupRows.tsx +6-18Lines changed: 6 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,8 +9,9 @@ import { type ToolActivityIcon } from "@t3tools/contracts"; | 9 | 9 | import { liveThoughtLine } from "@t3tools/client-runtime/work-log/presentation"; | 10 | 10 | import { resolveWorkGroupScrollAnchor } from "@t3tools/client-runtime/work-log/scroll-anchor"; | 11 | 11 | import { observeResize } from "~/lib/observeResize"; | | 12 | +import { tickTextContent } from "~/lib/tickTextContent"; | 12 | 13 | import { observeVisibleAnimation } from "../../lib/visibleAnimation"; | 13 | | -import { memo, use, useEffect, useLayoutEffect, useRef, useState, type ReactNode } from "react"; | | 14 | +import { memo, use, useLayoutEffect, useRef, useState, type ReactNode } from "react"; | 14 | 15 | import { LegendList, type LegendListRef } from "@legendapp/list/react"; | 15 | 16 | import { workEntryDisplayIndicatesToolFailure } from "../../session-logic"; | 16 | 17 | import ChatMarkdown from "../ChatMarkdown"; | @@ -109,24 +110,11 @@ export const WorkGroupSection = memo(function WorkGroupSection({ | 109 | 110 | }); | 110 | 111 |
| 111 | 112 | function WorkingTimer({ createdAt }: { createdAt: string }) { | 112 | | - const textRef = useRef<HTMLSpanElement>(null); | 113 | | - const initialText = formatWorkingTimerNow(createdAt); | 114 | | - | 115 | | - useEffect(() => { | 116 | | - const updateText = () => { | 117 | | - if (textRef.current) { | 118 | | - textRef.current.textContent = formatWorkingTimerNow(createdAt); | 119 | | - } | 120 | | - }; | 121 | | - updateText(); | 122 | | - const id = setInterval(updateText, 1000); | 123 | | - return () => clearInterval(id); | 124 | | - }, [createdAt]); | 125 | | - | 126 | 113 | return ( | 127 | | - <span ref={textRef} className="tabular-nums"> | 128 | | - {initialText} | 129 | | - </span> | | 114 | + <span | | 115 | + ref={tickTextContent(() => formatWorkingTimerNow(createdAt), true)} | | 116 | + className="tabular-nums" | | 117 | + /> | 130 | 118 | ); | 131 | 119 | } | 132 | 120 |
| |
Collapse file Copy file name to clipboard +78Lines changed: 78 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { ThreadId, type WorktreeSetupSnapshot } from "@t3tools/contracts"; | | 2 | +import { expect, it, vi } from "vite-plus/test"; | | 3 | + | | 4 | +import { act, fakeTimers, render } from "../../../test/render"; | | 5 | +import { WorktreeSetupCard } from "./WorktreeSetupCard"; | | 6 | + | | 7 | +const NOW = new Date("2026-10-26T12:00:10.000Z"); | | 8 | + | | 9 | +function snapshot(overrides: Partial<WorktreeSetupSnapshot>): WorktreeSetupSnapshot { | | 10 | + return { | | 11 | + threadId: ThreadId.make("thread-1"), | | 12 | + phase: "running", | | 13 | + startedAt: "2026-10-26T12:00:00.000Z", | | 14 | + endedAt: null, | | 15 | + branch: "t3/feature", | | 16 | + baseRef: "main", | | 17 | + worktreePath: "/repo/.t3/worktrees/feature", | | 18 | + setupScript: null, | | 19 | + stages: [ | | 20 | + { | | 21 | + id: "fetch", | | 22 | + status: "done", | | 23 | + startedAt: "2026-10-26T12:00:00.000Z", | | 24 | + endedAt: "2026-10-26T12:00:03.000Z", | | 25 | + percent: null, | | 26 | + detail: null, | | 27 | + tail: [], | | 28 | + }, | | 29 | + { | | 30 | + id: "checkout", | | 31 | + status: "running", | | 32 | + startedAt: "2026-10-26T12:00:03.000Z", | | 33 | + endedAt: null, | | 34 | + percent: null, | | 35 | + detail: null, | | 36 | + tail: [], | | 37 | + }, | | 38 | + ], | | 39 | + error: null, | | 40 | + sequence: 1, | | 41 | + ...overrides, | | 42 | + }; | | 43 | +} | | 44 | + | | 45 | +const card = (value: WorktreeSetupSnapshot) => ( | | 46 | + <WorktreeSetupCard snapshot={value} onCancel={null} onWorkLocally={null} onOpenTerminal={null} /> | | 47 | +); | | 48 | + | | 49 | +it("ticks the running stage while finished stages keep their duration", async () => { | | 50 | + using _timers = fakeTimers(); | | 51 | + vi.setSystemTime(NOW); | | 52 | + await using screen = await render(card(snapshot({}))); | | 53 | + // Assert synchronously: expect.element polls on timers these tests fake. | | 54 | + const section = screen.getByRole("region", { name: "Worktree setup" }); | | 55 | + | | 56 | + expect(section.element().textContent).toContain("Fetch base branch3.0s"); | | 57 | + expect(section.element().textContent).toContain("Check out files7.0s"); | | 58 | + | | 59 | + await act(async () => { | | 60 | + await vi.advanceTimersByTimeAsync(5_000); | | 61 | + }); | | 62 | + expect(section.element().textContent).toContain("Fetch base branch3.0s"); | | 63 | + expect(section.element().textContent).toContain("Check out files12s"); | | 64 | +}); | | 65 | + | | 66 | +it("stops a stage interrupted by the setup ending at the setup's end", async () => { | | 67 | + using _timers = fakeTimers(); | | 68 | + vi.setSystemTime(NOW); | | 69 | + const failed = snapshot({ phase: "failed", endedAt: "2026-10-26T12:00:05.000Z" }); | | 70 | + await using screen = await render(card(failed)); | | 71 | + const section = screen.getByRole("region", { name: "Worktree setup" }); | | 72 | + | | 73 | + expect(section.element().textContent).toContain("Check out files2.0s"); | | 74 | + await act(async () => { | | 75 | + await vi.advanceTimersByTimeAsync(5_000); | | 76 | + }); | | 77 | + expect(section.element().textContent).toContain("Check out files2.0s"); | | 78 | +}); |
|