Skip to content

fix(ui): preserve root dir state when sibling dir is expanded#2393

Open
trivikr wants to merge 1 commit intonpmx-dev:mainfrom
trivikr:fix/source-tree-expansion
Open

fix(ui): preserve root dir state when sibling dir is expanded#2393
trivikr wants to merge 1 commit intonpmx-dev:mainfrom
trivikr:fix/source-tree-expansion

Conversation

@trivikr
Copy link
Copy Markdown
Contributor

@trivikr trivikr commented Apr 5, 2026

🔗 Linked issue

resolves #2363

🧭 Context

The package code sidebar was re-expanding a collapsed sibling directory when another directory in the same tree was opened. On ky@1.14.3, collapsing core/ and then expanding types/ caused both directories to appear expanded again.

This change fixes the tree expansion logic so only the intended directory opens, and adds coverage for the regression.

📚 Description

The root cause was that the code tree auto-expanded the current file path from recursive CodeFileTree instances, not just from the root tree. That meant a subtree remount could re-apply ancestor expansion and reopen a directory the user had just collapsed.

This PR:

  • limits auto-expansion of the current file path to the root file tree instance
  • keeps file tree expansion state updates explicit by replacing the stored Set after mutations
  • adds a Nuxt regression test covering the sibling-directory case from the issue

- Recompute expanded state after toggling or auto-expanding directories
- Only auto-expand ancestors for the top-level tree
- Add regression coverage for sibling directory expansion
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 5, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
npmx.dev Ready Ready Preview, Comment Apr 5, 2026 3:44pm
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs.npmx.dev Ignored Ignored Preview Apr 5, 2026 3:44pm
npmx-lunaria Ignored Ignored Apr 5, 2026 3:44pm

Request Review

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 5, 2026

📝 Walkthrough

Walkthrough

This pull request addresses a bug where expanding one directory in the source tree-view would incorrectly re-expand a previously collapsed sibling directory. The changes modify the auto-expansion logic in FileTree.vue to restrict ancestor auto-expansion to root-level instances only, preventing deeper recursive tree instances from triggering unwanted expansions. Additionally, useFileTreeState.ts now properly reassigns Set instances after mutations to ensure correct reactivity handling. A new test file validates the interactive expand/collapse behaviour of the CodeFileTree component.

Suggested reviewers

  • danielroe
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description check ✅ Passed The PR description clearly explains the bug, root cause, and the changes introduced to fix it, with context about the specific package and version affected.
Linked Issues check ✅ Passed The PR directly addresses issue #2363 by limiting auto-expansion to root tree instances and adding a regression test for the exact sibling-directory scenario described.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the tree expansion logic: updating FileTree.vue depth check, modifying useFileTreeState.ts state updates, and adding a regression test.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5fb276bf-51e8-4d8d-a8c7-d5b8cd6fdb78

📥 Commits

Reviewing files that changed from the base of the PR and between 5324b96 and c4d8f05.

📒 Files selected for processing (3)
  • app/components/Code/FileTree.vue
  • app/composables/useFileTreeState.ts
  • test/nuxt/components/CodeFileTree.spec.ts

Comment on lines +63 to +88
const wrapper = await mountCodeFileTree()

await vi.waitFor(() => {
expect(wrapper.text()).toContain('constants.d.ts')
expect(wrapper.text()).not.toContain('common.d.ts')
})

const coreButton = findDirButton(wrapper, 'core')
expect(coreButton).toBeDefined()
await coreButton!.trigger('click')

await vi.waitFor(() => {
expect(wrapper.text()).not.toContain('constants.d.ts')
})

const typesButton = findDirButton(wrapper, 'types')
expect(typesButton).toBeDefined()
await typesButton!.trigger('click')

await vi.waitFor(() => {
expect(wrapper.text()).toContain('common.d.ts')
expect(wrapper.text()).not.toContain('constants.d.ts')
})

wrapper.unmount()
})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Ensure cleanup always runs when the test fails.

With attachTo: document.body, cleanup at Line 87 is skipped if an earlier assertion fails. Wrap the test body in try/finally so unmount() is guaranteed.

♻️ Suggested reliability fix
 it('keeps a collapsed sibling directory closed when another sibling expands', async () => {
   const wrapper = await mountCodeFileTree()
+  try {
 
-  await vi.waitFor(() => {
-    expect(wrapper.text()).toContain('constants.d.ts')
-    expect(wrapper.text()).not.toContain('common.d.ts')
-  })
+    await vi.waitFor(() => {
+      expect(wrapper.text()).toContain('constants.d.ts')
+      expect(wrapper.text()).not.toContain('common.d.ts')
+    })
 
-  const coreButton = findDirButton(wrapper, 'core')
-  expect(coreButton).toBeDefined()
-  await coreButton!.trigger('click')
+    const coreButton = findDirButton(wrapper, 'core')
+    expect(coreButton).toBeDefined()
+    await coreButton!.trigger('click')
 
-  await vi.waitFor(() => {
-    expect(wrapper.text()).not.toContain('constants.d.ts')
-  })
+    await vi.waitFor(() => {
+      expect(wrapper.text()).not.toContain('constants.d.ts')
+    })
 
-  const typesButton = findDirButton(wrapper, 'types')
-  expect(typesButton).toBeDefined()
-  await typesButton!.trigger('click')
+    const typesButton = findDirButton(wrapper, 'types')
+    expect(typesButton).toBeDefined()
+    await typesButton!.trigger('click')
 
-  await vi.waitFor(() => {
-    expect(wrapper.text()).toContain('common.d.ts')
-    expect(wrapper.text()).not.toContain('constants.d.ts')
-  })
+    await vi.waitFor(() => {
+      expect(wrapper.text()).toContain('common.d.ts')
+      expect(wrapper.text()).not.toContain('constants.d.ts')
+    })
 
-  wrapper.unmount()
+  } finally {
+    wrapper.unmount()
+  }
 })

}

describe('CodeFileTree', () => {
it('keeps a collapsed sibling directory closed when another sibling expands', async () => {
Copy link
Copy Markdown
Contributor

@jhroemer jhroemer Apr 5, 2026

Choose a reason for hiding this comment

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

I'm personally a little wary of tests like this, because the space of "this thing should not happen" is incredibly large. There's nothing wrong with the expectation, but maybe it can just be part of a normal collapse/expand test, instead of being a test specifically for this bug. Wdyt?

@serhalp serhalp added the needs review This PR is waiting for a review from a maintainer label Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs review This PR is waiting for a review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Source tree-view expands 2 directories instead of 1

3 participants