Desktop: Resolves #15029 Fix: Prevent unexpected focus shift when switching notebooks or using Go To#15036
Conversation
Signed-off-by: justin212407 <charlesjustin2124@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughRefactors useRefocusOnDeletion to track folder snapshots via internal refs and require a DOM Changes
Sequence Diagram(s)sequenceDiagram
participant NoteList as NoteList component
participant Hook as useRefocusOnDeletion
participant DOM as Document / ActiveElement
participant Focus as focusNote callback
NoteList->>Hook: call useRefocusOnDeletion(noteCount, selectedNoteIds, focusedField, selectedFolderId, listRef, focusNote)
NoteList->>Hook: noteCount changes (deletion)
Hook->>Hook: update lastNoteCountFolderRef and reset folderChangedSinceLastNoteCountRef
Hook->>DOM: evaluate listRef.current.contains(document.activeElement) as noteListHasFocus
alt note removed && folder unchanged since snapshot && one selected && no field focused && noteListHasFocus
Hook->>Focus: call focusNote(selectedNoteId)
else
Hook-->>NoteList: do not call focusNote
end
Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 6✅ Passed checks (6 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 |
Signed-off-by: justin212407 <charlesjustin2124@gmail.com>
Thanks! 2f61267 seems to have fixed one aspect of the issue. However, I'm still able to reproduce the focus jump issue in a different case:
|
Signed-off-by: justin212407 <charlesjustin2124@gmail.com>
There was a problem hiding this comment.
Actionable comments posted: 1
♻️ Duplicate comments (1)
packages/app-desktop/gui/NoteList/utils/useRefocusOnDeletion.ts (1)
15-20:⚠️ Potential issue | 🟠 MajorSame-count folder switches still leave this guard stale.
If the user switches folders and both renders start with the same
noteCount, Line 18 setsfolderChangedSinceLastNoteCountRef.currenttotrue, but Lines 34-36 never clear it because the count did not change. The next genuine deletion in the new folder is then treated as a folder-switch artefact, sofocusNotedoes not run.Also applies to: 33-37
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/app-desktop/gui/NoteList/utils/useRefocusOnDeletion.ts` around lines 15 - 20, When selectedFolderId changes we currently set folderChangedSinceLastNoteCountRef.current = true but if the new folder has the same noteCount the effect that clears that flag (which compares lastNoteCountRef.current to noteCount) never runs; fix by also updating the snapshot refs on folder switch: in the useEffect watching selectedFolderId (in useRefocusOnDeletion.ts) set lastNoteCountFolderRef.current = selectedFolderId and lastNoteCountRef.current = noteCount (in addition to folderChangedSinceLastNoteCountRef.current = true) so the subsequent deletion logic doesn’t treat the first delete in the new folder as a folder-switch artefact.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/app-desktop/gui/NoteList/utils/useRefocusOnDeletion.test.ts`:
- Around line 87-99: The test never gives the note list focus so
noteListHasFocus stays false and prevents focusNote from being called; before
the rerenders in the test that uses createFocusContext(), renderHook(...
useRefocusOnDeletion ...), and focusNote, programmatically focus the list
element (via the returned listRef or listItem from createFocusContext) so the
component thinks the list has focus (e.g., call focus on listRef.current or fire
a focus event) prior to the two rerenders; this ensures useRefocusOnDeletion
sees the focused state and will exercise the folder-switch guard that should
prevent refocusing.
---
Duplicate comments:
In `@packages/app-desktop/gui/NoteList/utils/useRefocusOnDeletion.ts`:
- Around line 15-20: When selectedFolderId changes we currently set
folderChangedSinceLastNoteCountRef.current = true but if the new folder has the
same noteCount the effect that clears that flag (which compares
lastNoteCountRef.current to noteCount) never runs; fix by also updating the
snapshot refs on folder switch: in the useEffect watching selectedFolderId (in
useRefocusOnDeletion.ts) set lastNoteCountFolderRef.current = selectedFolderId
and lastNoteCountRef.current = noteCount (in addition to
folderChangedSinceLastNoteCountRef.current = true) so the subsequent deletion
logic doesn’t treat the first delete in the new folder as a folder-switch
artefact.
🪄 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: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 8a0a765f-4a86-4655-a3f3-d2cdbb870310
📒 Files selected for processing (3)
packages/app-desktop/gui/NoteList/NoteList2.tsxpackages/app-desktop/gui/NoteList/utils/useRefocusOnDeletion.test.tspackages/app-desktop/gui/NoteList/utils/useRefocusOnDeletion.ts
Signed-off-by: justin212407 <charlesjustin2124@gmail.com>
I think the edge case you talked about is fixed now after the latest commit. I used the same script in a plugin(it adds a note, then deletes it, from a folder). Here is a working demo of the fix Screencast.From.2026-04-10.11-24-46.mp4 |
|
Thank you for fixing that! Now that there's logic to check whether Additionally, would the hook still work if |
|
I checked this:
Removing
For So I’d keep both as-is unless we want to intentionally change behavior and update tests. |
|
In terms of functionality, this looks good to me! In terms of style and code structure:
|
|
Yeah I think these changes do make a lot of sense. On it right now. |
Fixes: #15029
Summary:
When navigating between notebooks using keyboard arrow keys, focus unexpectedly shifts from the notebook list to the notes list after a few seconds. Additionally, the "Go To" feature exhibits inconsistent behavior where focus sometimes lands on the notes list instead of the selected note.
Fix:
The
useRefocusOnDeletionhook was incorrectly refocusing during notebook folder switches due to lagging state updates across render cycles. When users navigated to a different folder using keyboard, the folder ID and note count updated on separate renders. By the time the note count updated, theusePrevioushook had already captured the new folder ID, causing the hook to mistakenly believe a note was deleted in the same folder when in fact the folder had switched.Updated focus refocus logic to:
useRefinstead of the laggingusePrevioushookChanges Made
packages/app-desktop/gui/NoteList/utils/useRefocusOnDeletion.ts:useReffrom React to create a stablelastFolderIdRefusePrevious(selectedFolderId, '')withuseRef(selectedFolderId)to track the folder at the last note-count snapshotfolderDidNotChangeguard to compare againstlastFolderIdRef.currentinstead of the lagging previous folder valuelastFolderIdRef.currentonly when the note count changes, ensuring the ref remains synchronized with the snapshot pointpreviousFolderIdfrom the effect dependency array to stabilize the guardpackages/app-desktop/gui/NoteList/utils/useRefocusOnDeletion.test.ts:Testing:
BEFORE:
Screencast.From.2026-04-06.20-34-08.mp4
AFTER:
Screencast.From.2026-04-07.02-22-04.mp4
All five test cases pass, including:
AI Disclosure:
AI was used to make changes to the test.
AI was used to make the vocabulary for the PR description.