From da2c56a3b4c0fa14fcc7c1c676803c462c69d7bd Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 31 May 2026 10:24:09 -0700 Subject: [PATCH] perf: reset comment code context during render (#4271) --- .../src/components/GitHubItemDialog.tsx | 46 +++++++++++-- .../src/components/PullRequestPage.tsx | 46 +++++++++++-- .../comment-code-context-state.test.ts | 64 +++++++++++++++++++ .../components/comment-code-context-state.ts | 51 +++++++++++++++ 4 files changed, 195 insertions(+), 12 deletions(-) create mode 100644 src/renderer/src/components/comment-code-context-state.test.ts create mode 100644 src/renderer/src/components/comment-code-context-state.ts diff --git a/src/renderer/src/components/GitHubItemDialog.tsx b/src/renderer/src/components/GitHubItemDialog.tsx index 55bf9ff52..7f81e1327 100644 --- a/src/renderer/src/components/GitHubItemDialog.tsx +++ b/src/renderer/src/components/GitHubItemDialog.tsx @@ -102,6 +102,12 @@ import { PR_COMMENT_RESOLVED_CONTAINER_CLASS, type PRCommentGroup } from '@/lib/pr-comment-groups' +import { + createCommentCodeContextExpansionState, + resolveCommentCodeContextExpansionState, + updateCommentCodeContextExpansionState, + type CommentCodeContextLineUpdate +} from '@/components/comment-code-context-state' import { useAppStore } from '@/store' import { useAllWorktrees } from '@/store/selectors' import { callRuntimeRpc, getActiveRuntimeTarget } from '@/runtime/runtime-rpc-client' @@ -2050,8 +2056,9 @@ function CommentCodeContext({ }): React.JSX.Element | null { const [contents, setContents] = useState(null) const [error, setError] = useState(false) - const [contextBefore, setContextBefore] = useState(0) - const [contextAfter, setContextAfter] = useState(0) + const [contextExpansionState, setContextExpansionState] = useState(() => + createCommentCodeContextExpansionState(comment.id) + ) const file = useMemo( () => files.find((candidate) => candidate.path === comment.path), [comment.path, files] @@ -2082,10 +2089,37 @@ function CommentCodeContext({ } }, [baseSha, file, headSha, line, prNumber, repoId, repoPath]) - useEffect(() => { - setContextBefore(0) - setContextAfter(0) - }, [comment.id]) + const resolvedContextExpansionState = resolveCommentCodeContextExpansionState( + contextExpansionState, + comment.id + ) + if (resolvedContextExpansionState !== contextExpansionState) { + // Why: comment rows can be reused when a PR refreshes; reset before paint + // so expanded context from the previous comment is never shown on the next. + setContextExpansionState(resolvedContextExpansionState) + } + const contextBefore = resolvedContextExpansionState.contextBefore + const contextAfter = resolvedContextExpansionState.contextAfter + const setContextBefore = useCallback( + (contextBeforeUpdate: CommentCodeContextLineUpdate) => { + setContextExpansionState((current) => + updateCommentCodeContextExpansionState(current, comment.id, { + contextBefore: contextBeforeUpdate + }) + ) + }, + [comment.id] + ) + const setContextAfter = useCallback( + (contextAfterUpdate: CommentCodeContextLineUpdate) => { + setContextExpansionState((current) => + updateCommentCodeContextExpansionState(current, comment.id, { + contextAfter: contextAfterUpdate + }) + ) + }, + [comment.id] + ) if (!comment.path || !line || !file || file.isBinary || error) { return null diff --git a/src/renderer/src/components/PullRequestPage.tsx b/src/renderer/src/components/PullRequestPage.tsx index a41ae4d8a..218f96049 100644 --- a/src/renderer/src/components/PullRequestPage.tsx +++ b/src/renderer/src/components/PullRequestPage.tsx @@ -102,6 +102,12 @@ import { PR_COMMENT_RESOLVED_CONTAINER_CLASS, type PRCommentGroup } from '@/lib/pr-comment-groups' +import { + createCommentCodeContextExpansionState, + resolveCommentCodeContextExpansionState, + updateCommentCodeContextExpansionState, + type CommentCodeContextLineUpdate +} from '@/components/comment-code-context-state' import { useAppStore } from '@/store' import { useAllWorktrees } from '@/store/selectors' import { callRuntimeRpc, getActiveRuntimeTarget } from '@/runtime/runtime-rpc-client' @@ -2258,8 +2264,9 @@ function CommentCodeContext({ }): React.JSX.Element | null { const [contents, setContents] = useState(null) const [error, setError] = useState(false) - const [contextBefore, setContextBefore] = useState(0) - const [contextAfter, setContextAfter] = useState(0) + const [contextExpansionState, setContextExpansionState] = useState(() => + createCommentCodeContextExpansionState(comment.id) + ) const file = useMemo( () => files.find((candidate) => candidate.path === comment.path), [comment.path, files] @@ -2290,10 +2297,37 @@ function CommentCodeContext({ } }, [baseSha, file, headSha, line, prNumber, repoId, repoPath]) - useEffect(() => { - setContextBefore(0) - setContextAfter(0) - }, [comment.id]) + const resolvedContextExpansionState = resolveCommentCodeContextExpansionState( + contextExpansionState, + comment.id + ) + if (resolvedContextExpansionState !== contextExpansionState) { + // Why: comment rows can be reused when a PR refreshes; reset before paint + // so expanded context from the previous comment is never shown on the next. + setContextExpansionState(resolvedContextExpansionState) + } + const contextBefore = resolvedContextExpansionState.contextBefore + const contextAfter = resolvedContextExpansionState.contextAfter + const setContextBefore = useCallback( + (contextBeforeUpdate: CommentCodeContextLineUpdate) => { + setContextExpansionState((current) => + updateCommentCodeContextExpansionState(current, comment.id, { + contextBefore: contextBeforeUpdate + }) + ) + }, + [comment.id] + ) + const setContextAfter = useCallback( + (contextAfterUpdate: CommentCodeContextLineUpdate) => { + setContextExpansionState((current) => + updateCommentCodeContextExpansionState(current, comment.id, { + contextAfter: contextAfterUpdate + }) + ) + }, + [comment.id] + ) if (!comment.path || !line || !file || file.isBinary || error) { return null diff --git a/src/renderer/src/components/comment-code-context-state.test.ts b/src/renderer/src/components/comment-code-context-state.test.ts new file mode 100644 index 000000000..7df60f7e4 --- /dev/null +++ b/src/renderer/src/components/comment-code-context-state.test.ts @@ -0,0 +1,64 @@ +import { describe, expect, it } from 'vitest' +import { + createCommentCodeContextExpansionState, + resolveCommentCodeContextExpansionState, + updateCommentCodeContextExpansionState +} from './comment-code-context-state' + +describe('comment code context expansion state', () => { + it('keeps expanded context for the same comment', () => { + const expanded = updateCommentCodeContextExpansionState( + createCommentCodeContextExpansionState('comment-1'), + 'comment-1', + { + contextBefore: 5, + contextAfter: 3 + } + ) + + expect(resolveCommentCodeContextExpansionState(expanded, 'comment-1')).toEqual({ + commentId: 'comment-1', + contextBefore: 5, + contextAfter: 3 + }) + }) + + it('resets expansion when a rendered row switches comments', () => { + const expanded = updateCommentCodeContextExpansionState( + createCommentCodeContextExpansionState('comment-1'), + 'comment-1', + { + contextBefore: 5, + contextAfter: 3 + } + ) + + expect(resolveCommentCodeContextExpansionState(expanded, 'comment-2')).toEqual({ + commentId: 'comment-2', + contextBefore: 0, + contextAfter: 0 + }) + }) + + it('applies functional updates after resolving stale comment state', () => { + const expanded = updateCommentCodeContextExpansionState( + createCommentCodeContextExpansionState('comment-1'), + 'comment-1', + { + contextBefore: 5, + contextAfter: 3 + } + ) + + expect( + updateCommentCodeContextExpansionState(expanded, 'comment-2', { + contextBefore: (current) => current + 2, + contextAfter: (current) => current + 4 + }) + ).toEqual({ + commentId: 'comment-2', + contextBefore: 2, + contextAfter: 4 + }) + }) +}) diff --git a/src/renderer/src/components/comment-code-context-state.ts b/src/renderer/src/components/comment-code-context-state.ts new file mode 100644 index 000000000..ae6c2a7ee --- /dev/null +++ b/src/renderer/src/components/comment-code-context-state.ts @@ -0,0 +1,51 @@ +export type CommentCodeContextLineUpdate = number | ((current: number) => number) +export type CommentCodeContextCommentId = string | number + +export type CommentCodeContextExpansionState = { + commentId: CommentCodeContextCommentId + contextBefore: number + contextAfter: number +} + +export function createCommentCodeContextExpansionState( + commentId: CommentCodeContextCommentId +): CommentCodeContextExpansionState { + return { + commentId, + contextBefore: 0, + contextAfter: 0 + } +} + +export function resolveCommentCodeContextExpansionState( + state: CommentCodeContextExpansionState, + commentId: CommentCodeContextCommentId +): CommentCodeContextExpansionState { + return state.commentId === commentId ? state : createCommentCodeContextExpansionState(commentId) +} + +function resolveLineUpdate(update: CommentCodeContextLineUpdate, current: number): number { + return typeof update === 'function' ? update(current) : update +} + +export function updateCommentCodeContextExpansionState( + state: CommentCodeContextExpansionState, + commentId: CommentCodeContextCommentId, + updates: { + contextBefore?: CommentCodeContextLineUpdate + contextAfter?: CommentCodeContextLineUpdate + } +): CommentCodeContextExpansionState { + const resolved = resolveCommentCodeContextExpansionState(state, commentId) + return { + ...resolved, + contextBefore: + updates.contextBefore === undefined + ? resolved.contextBefore + : resolveLineUpdate(updates.contextBefore, resolved.contextBefore), + contextAfter: + updates.contextAfter === undefined + ? resolved.contextAfter + : resolveLineUpdate(updates.contextAfter, resolved.contextAfter) + } +}