Skip to content

Fix file tree staying stale when switching workspaces#1143

Merged
Kitenite merged 8 commits into
mainfrom
Kitenite/file-tree-stale-on-switch
Feb 2, 2026
Merged

Fix file tree staying stale when switching workspaces#1143
Kitenite merged 8 commits into
mainfrom
Kitenite/file-tree-stale-on-switch

Conversation

@Kitenite
Copy link
Copy Markdown
Collaborator

@Kitenite Kitenite commented Feb 2, 2026

Description

The file tree in the workspace view was displaying stale files when switching between different workspaces. This was caused by the @headless-tree library caching the dataLoader configuration at hook initialization, so when workspace changed, the tree's callbacks still referenced the old worktreePath value 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() with invalidateChildrenIds() for proper cache clearing.

Type of Change

  • Bug fix
  • New feature
  • Documentation
  • Refactor

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

    • File tree now properly updates when switching workspaces; view invalidation is selective and faster.
    • Refresh action invalidates and reloads only necessary parts of the tree, avoiding full rebuilds.
  • Improvements

    • Hidden files visibility toggle correctly refreshes the file browser and is available from the toolbar.
    • State handling for workspace paths and file-view settings has been made more reliable to keep the UI synchronized.

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.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 2, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

FilesView now tracks worktreePath and showHiddenFiles in render-stable refs and uses them for data loading. Tree updates invalidate affected child nodes (including root) instead of rebuilding; refresh and hidden-file toggles invalidate root children to trigger reloads.

Changes

Cohort / File(s) Summary
FilesView Component
apps/desktop/src/renderer/screens/main/components/WorkspaceView/RightSidebar/FilesView/FilesView.tsx
Added worktreePathRef, showHiddenFilesRef, and prevWorktreePathRef; switched directory reads and dataLoader usage to ref-backed current values. Added effect to invalidate root children when worktreePath changes, updated handleRefresh to invalidate root children, and added handleToggleHiddenFiles wired to FileTreeToolbar. No public API 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
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐇
I nibble refs beneath the moonlit code,
Paths held steady on my quiet road.
Hidden things I gently show, then hide,
I wake just children — watch the branches glide.
A tiny hop, the tree refreshes wide.

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely summarizes the main change: fixing stale file tree display when switching workspaces.
Description check ✅ Passed The description covers the problem, solution, type of change, and testing verification. It follows the template structure with all key sections populated.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch Kitenite/file-tree-stale-on-switch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant