fix(ui): fix focusing bugs while editing files#4197
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
PR SummaryMedium Risk Overview In In Reviewed by Cursor Bugbot for commit d2bb494. Bugbot is set up for automated code reviews on this repo. Configure here. |
Greptile SummaryThis PR fixes two focus-related bugs in the file editor: stream completion stealing focus from an active file editor, and network-triggered re-renders moving the cursor in the chat input. The fix adds Confidence Score: 5/5Safe to merge — all changes are targeted defensive guards with no new logic paths that could break existing behaviour. All findings are P2 or lower. The focus guards are narrowly scoped, the ref-callback refactor is a well-established React pattern, and the error-condition tightening is logically sound. No regressions identified. No files require special attention. Important Files Changed
Sequence DiagramsequenceDiagram
participant Stream as Streaming engine
participant UI as UserInput (chat)
participant RAF as requestAnimationFrame
participant Active as document.activeElement
participant FV as FileViewer textarea
Note over FV: User is editing a file
Stream->>UI: isSending → false (stream done)
UI->>Active: check document.activeElement
Active-->>UI: HTMLTextAreaElement (FileViewer)
UI->>UI: isEditingElsewhere = true
UI->>UI: skip focus() — no cursor theft
Note over FV: User NOT editing
Stream->>UI: isSending → false (stream done)
UI->>Active: check document.activeElement
Active-->>UI: body / other element
UI->>UI: isEditingElsewhere = false
UI->>RAF: requestAnimationFrame → focus chat textarea
Reviews (1): Last reviewed commit: "fix(ui): fix focusing bugs while editing..." | Re-trigger Greptile |
| const active = document.activeElement | ||
| const isEditingElsewhere = | ||
| active instanceof HTMLTextAreaElement || active instanceof HTMLInputElement | ||
| if (!isEditingElsewhere) { | ||
| textareaRef.current?.focus() | ||
| } | ||
| } |
There was a problem hiding this comment.
contenteditable elements not covered by focus guard
The guard checks HTMLTextAreaElement | HTMLInputElement, but won't catch contenteditable divs (e.g. rich-text editors, ProseMirror, etc.). Currently fine since the file editor uses a <textarea>, but worth expanding if any future editor component uses contenteditable.
| const active = document.activeElement | |
| const isEditingElsewhere = | |
| active instanceof HTMLTextAreaElement || active instanceof HTMLInputElement | |
| if (!isEditingElsewhere) { | |
| textareaRef.current?.focus() | |
| } | |
| } | |
| const isEditingElsewhere = | |
| active instanceof HTMLTextAreaElement || | |
| active instanceof HTMLInputElement || | |
| (active instanceof HTMLElement && active.isContentEditable) |
The same pattern applies to the second effect (initial mount focus) below.
Summary
We have some issues with refocused views while editing.
Added guards to ensure that if file view is focused that we don't mess with cursor location.
Type of Change
Testing
Checklist
Screenshots/Videos