Repository navigation
feat: auto-focus current file in the left file tree - #849
Conversation
Highlight the open document, expand its ancestor folders, and scroll it into view so the sidebar stays oriented when navigating. Co-authored-by: Cursor <[email protected]>
✅ Deploy Preview for mdts-unhappychoice ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
📝 WalkthroughWalkthroughThe file tree now expands ancestor directories for the selected document and scrolls that document into view. Selection state is passed from ChangesSelected file tree handling
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant FileTree
participant fileTreeSlice
participant FileTreeContent
participant FileTreeView
participant TreeDOM
FileTree->>fileTreeSlice: dispatch ancestor expansion
fileTreeSlice->>fileTreeSlice: persist expanded nodes
FileTree->>FileTreeContent: pass selectedFilePath
FileTreeContent->>FileTreeView: pass selectedFilePath
FileTreeView->>TreeDOM: locate selected item
TreeDOM-->>FileTreeView: return item element
FileTreeView->>TreeDOM: scroll selected item into view
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install timed out. The project may have too many dependencies for the sandbox. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/frontend/src/components/LeftPane/FileTreeContent/FileTreeView.tsx (1)
28-54: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick winSingle fixed-delay retry may miss deeply-nested or slow-mounting items.
The scroll effect tries once immediately, then once more after a hardcoded 150ms. If ancestor expansion cascades across multiple levels (e.g., a deeply nested file needs several ensureExpandedNodes dispatches to fully mount), the item may still not exist in the DOM after 150ms, and scrolling silently fails with no further retries.
♻️ Suggested approach: bounded retry loop
- if (tryScroll()) { - return () => { - cancelled = true; - }; - } - - // Newly expanded parents may not have mounted the item yet. - const timeoutId = window.setTimeout(tryScroll, 150); - return () => { - cancelled = true; - window.clearTimeout(timeoutId); - }; + if (tryScroll()) { + return () => { + cancelled = true; + }; + } + + // Newly expanded parents may not have mounted the item yet; retry a few times. + let attempts = 0; + const maxAttempts = 5; + const intervalId = window.setInterval(() => { + attempts += 1; + if (tryScroll() || attempts >= maxAttempts) { + window.clearInterval(intervalId); + } + }, 100); + return () => { + cancelled = true; + window.clearInterval(intervalId); + };🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/frontend/src/components/LeftPane/FileTreeContent/FileTreeView.tsx` around lines 28 - 54, Replace the single 150ms retry in the selectedFilePath scroll effect with a bounded retry loop that continues checking getItemDOMElement while ancestor expansion and mounting complete, stopping when the element is found or the retry limit is reached. Preserve cancellation and timeout cleanup so stale effects cannot scroll after dependencies change.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@packages/frontend/test/unit/components/LeftPane/FileTreeContent/FileTreeView.test.tsx`:
- Around line 19-31: Stub element.scrollIntoView for the FileTreeView tests
before the “should render correctly” render, reusing the existing stub from the
following test if available; otherwise add the stub to shared test setup. Ensure
the selected item mount path can execute without calling the unimplemented DOM
method.
---
Nitpick comments:
In `@packages/frontend/src/components/LeftPane/FileTreeContent/FileTreeView.tsx`:
- Around line 28-54: Replace the single 150ms retry in the selectedFilePath
scroll effect with a bounded retry loop that continues checking
getItemDOMElement while ancestor expansion and mounting complete, stopping when
the element is found or the retry limit is reached. Preserve cancellation and
timeout cleanup so stale effects cannot scroll after dependencies change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 16a9c518-bd32-439f-b417-12b5e0d84dfa
⛔ Files ignored due to path filters (1)
packages/frontend/test/unit/components/LeftPane/FileTreeContent/__snapshots__/FileTreeView.test.tsx.snapis excluded by!**/*.snap
📒 Files selected for processing (8)
packages/frontend/src/components/LeftPane/FileTree.tsxpackages/frontend/src/components/LeftPane/FileTreeContent/FileTreeContent.tsxpackages/frontend/src/components/LeftPane/FileTreeContent/FileTreeView.tsxpackages/frontend/src/store/slices/fileTreeSlice.tspackages/frontend/test/unit/components/LeftPane/FileTree.test.tsxpackages/frontend/test/unit/components/LeftPane/FileTreeContent/FileTreeContent.test.tsxpackages/frontend/test/unit/components/LeftPane/FileTreeContent/FileTreeView.test.tsxpackages/frontend/test/unit/store/fileTreeSlice.test.ts
| it('should render correctly', () => { | ||
| const { asFragment } = render( | ||
| <FileTreeView | ||
| filteredFileTree={filteredFileTree} | ||
| expandedNodes={expandedNodes} | ||
| selectedFilePath="dir1/file2.md" | ||
| onFileSelect={onFileSelect} | ||
| onExpandedItemsChange={onExpandedItemsChange} | ||
| getStatusColor={getStatusColor} | ||
| /> | ||
| ); | ||
| expect(asFragment()).toMatchSnapshot(); | ||
| }); |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
rg -n "scrollIntoView" --type=ts --type=js packages/frontend
fd -i 'jest.setup' packages/frontend
fd -i 'setupFilesAfterEnv' packages/frontend
cat packages/frontend/jest.config.* 2>/dev/nullRepository: unhappychoice/mdts
Length of output: 2258
🏁 Script executed:
#!/bin/bash
set -e
wc -l packages/frontend/test/setupTests.ts packages/frontend/test/unit/components/LeftPane/FileTreeContent/FileTreeView.test.tsx packages/frontend/src/components/LeftPane/FileTreeContent/FileTreeView.tsx
cat -n packages/frontend/test/setupTests.ts
printf '\n--- FileTreeView.test.tsx ---\n'
sed -n '1,120p' packages/frontend/test/unit/components/LeftPane/FileTreeContent/FileTreeView.test.tsx
printf '\n--- FileTreeView.tsx ---\n'
sed -n '1,120p' packages/frontend/src/components/LeftPane/FileTreeContent/FileTreeView.tsxRepository: unhappychoice/mdts
Length of output: 5641
🏁 Script executed:
#!/bin/bash
set -e
ls -l packages/frontend/test/setupTests.ts
cat -n packages/frontend/test/setupTests.ts | sed -n '1,200p'Repository: unhappychoice/mdts
Length of output: 1434
Stub scrollIntoView here
This render path can call element.scrollIntoView(...) when the selected item mounts, but the test doesn’t mock it locally and there’s no shared stub in packages/frontend/test/setupTests.ts. Reuse the stub from the next test or add one to shared setup.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In
`@packages/frontend/test/unit/components/LeftPane/FileTreeContent/FileTreeView.test.tsx`
around lines 19 - 31, Stub element.scrollIntoView for the FileTreeView tests
before the “should render correctly” render, reusing the existing stub from the
following test if available; otherwise add the stub to shared test setup. Ensure
the selected item mount path can execute without calling the unimplemented DOM
method.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #849 +/- ##
==========================================
- Coverage 99.41% 99.30% -0.12%
==========================================
Files 69 69
Lines 1549 1586 +37
Branches 374 379 +5
==========================================
+ Hits 1540 1575 +35
- Misses 8 10 +2
Partials 1 1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
unhappychoice
left a comment
There was a problem hiding this comment.
Thanks for the clean, well-tested change. ensureExpandedNodes being idempotent and skipping the localStorage write when there is nothing new is a nice touch, and I like that user-collapsed folders are only re-expanded on path change.
One small concern before I merge: in FileTreeView.tsx, the scroll useEffect lists expandedNodes in its dependency array. This means the selected file will scroll back into view any time the user expands or collapses an unrelated folder elsewhere in the tree, which may feel a bit unexpected during manual browsing. Would it make sense to drop expandedNodes from the deps and rely on selectedFilePath alone, or gate the scroll so it only runs on selection changes?
Approving so this is not blocking. Happy to merge as-is if you prefer to keep the current behavior.
Summary
selectedItemsensureExpandedNodes)Test plan
docs/a/b/file.md) and confirm the sidebar expands parents, selects the file, and scrolls it into viewcurrentPathwithout collapsing unrelated expanded folderscd packages/frontend && npm test -- --testPathPatterns='FileTree|fileTreeSlice'Made with Cursor
Summary by CodeRabbit
New Features
Tests