Repository navigation
[FIX] Calculate DOM for virtualization behaviour - #33
Conversation
- Replaces 200px estimates per cell
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughConflictResolver now measures actual row heights at runtime using ResizeObserver, replaces the estimated fallback with DEFAULT_ROW_HEIGHT, computes cumulative heights for O(1) lookups, and updates virtualization, scrolling, and rendering to use measured heights (including deletion-safe scroll adjustments). Changes
Sequence Diagram(s)sequenceDiagram
participant User as User (scroll)
participant Scroll as ScrollContainer
participant Resolver as ConflictResolver
participant RO as ResizeObserver
participant Renderer as DOM Renderer
rect rgba(135,206,235,0.5)
User->>Scroll: scrolls viewport
Scroll->>Resolver: onScroll(position, viewportHeight)
Resolver->>Resolver: getRowIndexAtPosition(position) / getTotalHeight()
Resolver->>Renderer: render visible rows (startIndex..endIndex)
end
rect rgba(144,238,144,0.5)
Renderer->>RO: observe row element refs
RO-->>Resolver: onResize(rowIndex, newHeight)
Resolver->>Resolver: update rowHeights, recompute cumulative heights
Resolver->>Scroll: adjust scroll position if needed (on deletion)
Resolver->>Renderer: re-render visible rows / update container minHeight
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@src/web/client/ConflictResolver.tsx`:
- Around line 127-154: The ResizeObserver callback closes over the stale
rowHeights because the effect has an empty dependency array; replace direct
reads of rowHeights with a functional state update via setRowHeights(prev => {
... }) inside the observer so you always base changes on the latest Map, e.g.
create a new Map from prev, apply entry updates, and return the updated map only
when hasChanges is true (otherwise return prev); adjust the callback in the
useEffect that assigns resizeObserver.current and keep disconnect logic
unchanged.
- Around line 187-196: The current cleanup in setRowHeights preserves heights
for indices < currentLength which breaks when a middle row is deleted because
subsequent rows shift; update the logic in the component that calls
setRowHeights (the height-management for the virtual rows) to either clear the
entire rowHeights Map after a deletion to force re-measurement or implement
remapping by detecting deleted indexes and shifting all keys > deletedIndex down
by one; locate the call-sites and the setRowHeights usage in
ConflictResolver.tsx and replace the existing pruning logic with a full clear or
a proper key-shift routine so stored heights remain aligned with row indices.
🧹 Nitpick comments (2)
src/web/client/ConflictResolver.tsx (2)
471-517: Clarify virtualization approach: all rows render, only content is windowed.The current implementation renders all row wrapper divs in normal flow and relies on
MergeRowto skip expensive content whenisVisible={false}. This is a valid "content windowing" approach that enables accurate height measurement.However, for notebooks with hundreds of cells, the DOM node count may still impact performance. If this becomes an issue, consider true virtualization with absolutely-positioned rows and only rendering visible elements. For now, this approach is reasonable given the measurement requirements.
100-125: Consider caching cumulative heights for large notebooks.
getCumulativeHeightandgetRowIndexAtPositionboth iterate through all rows, resulting in O(n) complexity per call. During scrolling, these are called multiple times. For typical notebook sizes this is fine, but for very large notebooks (hundreds of cells), consider caching a prefix-sum array of cumulative heights, updated whenrowHeightschanges.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@src/web/client/ConflictResolver.tsx`:
- Around line 119-142: The function getRowIndexAtPosition can return -1 when
rows.length === 0; add an early guard at the start of getRowIndexAtPosition to
handle an empty rows array (e.g., if rows.length === 0 return 0) and avoid
indexing cumulativeHeights out of bounds; also update the fallback that checks
cumulativeHeights[rows.length] to only run when rows.length > 0 (or otherwise
treat as out-of-bounds and return 0). Ensure you reference
getRowIndexAtPosition, cumulativeHeights, and rows.length when making these
changes.
🧹 Nitpick comments (2)
src/web/client/ConflictResolver.tsx (2)
173-190: Minor:rowRefs.currentmay accumulate stale entries after row deletions.When rows are deleted and indices shift, the
rowRefs.currentMap retains entries for old indices. While this doesn't cause functional issues (the ResizeObserver unobserves elements when they unmount), it could lead to gradual memory accumulation in long editing sessions.Consider clearing
rowRefs.currentalongsiderowHeightswhen rows are deleted, though this is a minor concern.♻️ Optional cleanup for rowRefs
// Clean up height measurements for deleted rows setRowHeights(new Map()); + // Also clear stale row refs + rowRefs.current.clear(); }
499-557: Consider using stable keys instead of array indices for rows.Using array indices as keys (
key={i}on Line 508) can cause issues when rows are reordered via drag-and-drop:
- React may incorrectly reuse component instances, leading to stale internal state
- Height measurements in
rowHeightsmay temporarily mismatch as indices shiftWhile the current implementation works because
rowHeightsis cleared on deletions and row data is passed as props, a more robust approach would use stable identifiers.♻️ Suggested approach using stable row identifiers
If rows have unique identifiers (e.g., based on cell indices), use those as keys:
- {rows.map((row, i) => { + {rows.map((row, i) => { + // Generate stable key from cell indices + const rowKey = `${row.baseCellIndex ?? 'x'}-${row.currentCellIndex ?? 'x'}-${row.incomingCellIndex ?? 'x'}`; ... - <React.Fragment key={i}> + <React.Fragment key={rowKey}>Note: This requires ensuring
rowKeyremains stable and unique across row operations.
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Summary by CodeRabbit
Bug Fixes
Style