Fix file tree staying stale when switching workspaces#1143
Conversation
Use refs updated synchronously during render to ensure dataLoader callbacks always access current worktreePath and showHiddenFiles values. Add effect to invalidate tree cache when workspace changes. Replace rebuildTree with invalidateChildrenIds for proper cache clearing.
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughFilesView now tracks Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Toolbar as FileTreeToolbar
participant View as FilesView
participant Cache as TreeCache/DataLoader
participant FS as FileSystem
User->>Toolbar: click toggle "show hidden"
Toolbar->>View: handleToggleHiddenFiles()
View->>View: update showHiddenFilesRef.current
View->>Cache: invalidateTree(rootId)
Cache->>View: request children (reads worktreePathRef & showHiddenFilesRef)
View->>FS: readDirectory(dirPath from worktreePathRef, includeHidden from showHiddenFilesRef)
FS-->>View: return directory entries
View->>Cache: update children entries
Cache-->>Toolbar: render updated tree
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 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)
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: 2
🤖 Fix all issues with AI agents
In
`@apps/desktop/src/renderer/screens/main/components/WorkspaceView/RightSidebar/FilesView/FilesView.tsx`:
- Line 14: The prevWorktreePathRef is being overwritten with undefined when
worktreePath becomes undefined, preventing proper invalidation on reselection;
in the FilesView component ensure you only update prevWorktreePathRef.current
when worktreePath is defined (e.g. set prevWorktreePathRef.current =
worktreePath only if worktreePath !== undefined, or use
prevWorktreePathRef.current = worktreePath ?? prevWorktreePathRef.current) so
the last defined worktreePath is retained for comparison and reselection
invalidation.
- Around line 259-262: The state toggle handler handleToggleHiddenFiles must
update the synchronous ref used by fetches before calling invalidateChildrenIds;
change the logic to compute the new visibility, set showHiddenFilesRef.current
to that new value, then call setShowHiddenFiles(...) and finally call
tree.getItemInstance("root")?.invalidateChildrenIds(); apply the same change to
the other similar toggle handler (the one around line 292) so the ref reflects
the new hidden-files state synchronously before invalidation triggers a fetch.
- Remove prevWorktreePathRef !== undefined guard that blocked invalidation on undefined -> new path transitions (happens when query fetches new workspace) - Refresh and toggle hidden files now invalidate all expanded folders, not just root, so nested changes are properly reloaded
Description
The file tree in the workspace view was displaying stale files when switching between different workspaces. This was caused by the
@headless-treelibrary caching thedataLoaderconfiguration at hook initialization, so when workspace changed, the tree's callbacks still referenced the oldworktreePathvalue through closure.Solution: Use refs updated synchronously during render (not in effects) to ensure the dataLoader callbacks always read current values. Added an effect to invalidate the tree cache when the workspace actually changes, triggering a fresh data fetch. Replaced
rebuildTree()withinvalidateChildrenIds()for proper cache clearing.Type of Change
Testing
When switching between workspaces in the sidebar, the file tree now properly displays files from the newly selected workspace instead of keeping the previous workspace's files.
Summary by CodeRabbit
Bug Fixes
Improvements