From a9a09759b0f2a3876cf959a6f7a85e626f17d0b0 Mon Sep 17 00:00:00 2001 From: Roo Code Date: Tue, 26 Aug 2025 17:32:42 +0000 Subject: [PATCH] fix: optimize ChatView to prevent excessive re-rendering when typing - Properly memoized itemContent callback with stable dependencies - Added memoization for lastModifiedMessage to prevent unnecessary recalculations - Removed debug logging after confirming the fix works This fixes the UI jittering issue that occurred when typing in the chat input by preventing the groupedMessages and itemContent from being recalculated on every keystroke. --- webview-ui/src/components/chat/ChatRow.tsx | 1 + webview-ui/src/components/chat/ChatView.tsx | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/webview-ui/src/components/chat/ChatRow.tsx b/webview-ui/src/components/chat/ChatRow.tsx index 4fa921f443..4e22273268 100644 --- a/webview-ui/src/components/chat/ChatRow.tsx +++ b/webview-ui/src/components/chat/ChatRow.tsx @@ -68,6 +68,7 @@ interface ChatRowContentProps extends Omit {} const ChatRow = memo( (props: ChatRowProps) => { const { isLast, onHeightChange, message } = props + // Store the previous height to compare with the current height // This allows us to detect changes without causing re-renders const prevHeightRef = useRef(0) diff --git a/webview-ui/src/components/chat/ChatView.tsx b/webview-ui/src/components/chat/ChatView.tsx index 44eeb33b66..d4670d7aa2 100644 --- a/webview-ui/src/components/chat/ChatView.tsx +++ b/webview-ui/src/components/chat/ChatView.tsx @@ -160,7 +160,9 @@ const ChatViewComponent: React.ForwardRefRenderFunction combineApiRequests(combineCommandSequences(messages.slice(1))), [messages]) + const modifiedMessages = useMemo(() => { + return combineApiRequests(combineCommandSequences(messages.slice(1))) + }, [messages]) // Has to be after api_req_finished are all reduced into api_req_started messages. const apiMetrics = useMemo(() => getApiMetrics(modifiedMessages), [modifiedMessages]) @@ -1521,6 +1523,10 @@ const ChatViewComponent: React.ForwardRefRenderFunction modifiedMessages.at(-1), [modifiedMessages]) + + // Properly memoized itemContent callback with stable dependencies const itemContent = useCallback( (index: number, messageOrGroup: ClineMessage | ClineMessage[]) => { // browser session group @@ -1529,7 +1535,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction expandedRows[messageTs] ?? false} @@ -1549,12 +1555,12 @@ const ChatViewComponent: React.ForwardRefRenderFunction