From 2e92844fb714a786ce70f6b6116b3c0f68f3ef77 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 7 May 2026 16:09:14 +0000 Subject: [PATCH] Avoid timeline timer rerender commits Co-authored-by: Julius Marminge --- .../src/components/chat/MessagesTimeline.tsx | 60 +++++++++++++++---- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index dafa8161bd2..d07aa88aec8 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -520,19 +520,27 @@ function WorkingTimelineRow({ row }: { row: Extract Date.now()); + const textRef = useRef(null); + const initialText = formatWorkingTimerNow(createdAt); + useEffect(() => { - const id = setInterval(() => setNowMs(Date.now()), 1000); + const updateText = () => { + if (textRef.current) { + textRef.current.textContent = formatWorkingTimerNow(createdAt); + } + }; + updateText(); + const id = setInterval(updateText, 1000); return () => clearInterval(id); }, [createdAt]); - return <>{formatWorkingTimer(createdAt, new Date(nowMs).toISOString()) ?? "0s"}; + + return {initialText}; } /** Live timestamp + elapsed duration for a streaming assistant message. */ @@ -545,15 +553,28 @@ function LiveMessageMeta({ durationStart: string | null | undefined; timestampFormat: TimestampFormat; }) { - const [nowMs, setNowMs] = useState(() => Date.now()); + const textRef = useRef(null); + const initialText = formatLiveMessageMetaNow(createdAt, durationStart, timestampFormat); + useEffect(() => { - const id = setInterval(() => setNowMs(Date.now()), 1000); + const updateText = () => { + if (textRef.current) { + textRef.current.textContent = formatLiveMessageMetaNow( + createdAt, + durationStart, + timestampFormat, + ); + } + }; + updateText(); + if (!durationStart) { + return; + } + const id = setInterval(updateText, 1000); return () => clearInterval(id); - }, [durationStart]); - const elapsed = durationStart - ? formatElapsed(durationStart, new Date(nowMs).toISOString()) - : null; - return <>{formatMessageMeta(createdAt, elapsed, timestampFormat)}; + }, [createdAt, durationStart, timestampFormat]); + + return {initialText}; } // --------------------------------------------------------------------------- @@ -860,6 +881,19 @@ function formatWorkingTimer(startIso: string, endIso: string): string | null { return seconds > 0 ? `${minutes}m ${seconds}s` : `${minutes}m`; } +function formatWorkingTimerNow(startIso: string): string { + return formatWorkingTimer(startIso, new Date().toISOString()) ?? "0s"; +} + +function formatLiveMessageMetaNow( + createdAt: string, + durationStart: string | null | undefined, + timestampFormat: TimestampFormat, +): string { + const elapsed = durationStart ? formatElapsed(durationStart, new Date().toISOString()) : null; + return formatMessageMeta(createdAt, elapsed, timestampFormat); +} + function formatMessageMeta( createdAt: string, duration: string | null,