perf(sidebar): share mounted row scans across review visibility checks

This commit is contained in:
Neil
2026-11-11 16:56:54 -08:00
parent 9d28671414
commit eeaac7a2ed
2 changed files with 49 additions and 22 deletions
@@ -18,6 +18,30 @@ export function getMountedWorktreeOptions(
return result
}
export function getMountedWorktreeOptionsById(
worktreeIds: readonly string[],
root: ParentNode
): Map<string, HTMLElement[]> {
const requested = new Set(worktreeIds)
const result = new Map<string, HTMLElement[]>()
if (requested.size === 0) {
return result
}
root.querySelectorAll<HTMLElement>('[data-worktree-id]').forEach((element) => {
const id = element.dataset.worktreeId
if (id === undefined || !requested.has(id)) {
return
}
const options = result.get(id)
if (options) {
options.push(element)
} else {
result.set(id, [element])
}
})
return result
}
export function markSidebarWorktreeActiveImmediately(
worktreeId: string,
primaryRowKey?: string
@@ -6,7 +6,7 @@ import type { Worktree } from '../../../../../../shared/worktree/types'
import type { WorktreeGroupBy } from '../../../../../../shared/sidebar-list/row-types'
import type { RenderRow } from '../listing/render-row'
import type { WorktreeItemRow } from '../listing/renderable-rows'
import { getMountedWorktreeOptions } from '../rows/option-dom'
import { getMountedWorktreeOptionsById } from '../rows/option-dom'
export function installWorktreeVisibleRefreshVisibilityListener(onChange: () => void): () => void {
document.addEventListener('visibilitychange', onChange)
@@ -85,28 +85,31 @@ export function useVisiblePrRefreshReporting(args: {
useEffect(() => {
const update = (): void => {
const scroll = args.scrollRef.current
const viewport = scroll?.getBoundingClientRect()
if (!enabled || !scroll || document.visibilityState !== 'visible') {
publish([])
return
}
const candidates = visibleReviewCardIds({
enabled,
renderRows: args.renderRows,
virtualItems: args.virtualItems,
viewportTop: scroll.scrollTop,
viewportHeight: scroll.clientHeight
})
if (candidates.length === 0) {
publish([])
return
}
const viewport = scroll.getBoundingClientRect()
const mountedOptions = getMountedWorktreeOptionsById(candidates, scroll)
publish(
scroll && document.visibilityState === 'visible'
? visibleReviewCardIds({
enabled,
renderRows: args.renderRows,
virtualItems: args.virtualItems,
viewportTop: scroll.scrollTop,
viewportHeight: scroll.clientHeight,
isOnScreen: (id) =>
getMountedWorktreeOptions(id, scroll).some((option) => {
const surface = option.querySelector('[data-worktree-card-surface]')
const bounds = (surface?.firstElementChild ?? option).getBoundingClientRect()
return (
viewport !== undefined &&
bounds.height > 0 &&
bounds.top < viewport.bottom &&
bounds.bottom > viewport.top
)
})
})
: []
candidates.filter((id) =>
mountedOptions.get(id)?.some((option) => {
const surface = option.querySelector('[data-worktree-card-surface]')
const bounds = (surface?.firstElementChild ?? option).getBoundingClientRect()
return bounds.height > 0 && bounds.top < viewport.bottom && bounds.bottom > viewport.top
})
)
)
}
update()