Handle blur race when file changes during rename

When switching files mid-rename, React may deliver the old input's
blur event after the new file renders, causing a stale rename commit.

Mark the rename as cancelled when the active file changes, and add
test coverage verifying stale blur events are ignored.
This commit is contained in:
Jinjing
2026-09-20 21:14:08 -07:00
parent a594cfb91e
commit c3f0f844e3
2 changed files with 6 additions and 1 deletions
@@ -196,7 +196,8 @@ describe('EditorPanelHeaderPath inline rename', () => {
const rerenderPath = renderPath(baseFile())
openRenameInput()
fireEvent.change(getRenameInput('Rename file notes.md'), { target: { value: 'renamed.md' } })
const input = getRenameInput('Rename file notes.md')
fireEvent.change(input, { target: { value: 'renamed.md' } })
rerenderPath(
baseFile({
id: '/repo/other.md',
@@ -207,6 +208,9 @@ describe('EditorPanelHeaderPath inline rename', () => {
expect(screen.queryByLabelText('Rename file notes.md')).toBeNull()
expect(screen.queryByLabelText('Rename file other.md')).toBeNull()
// React may deliver the removed input's blur after the active file render.
fireEvent.blur(input)
expect(renameFileOnDiskMock).not.toHaveBeenCalled()
})
@@ -30,6 +30,7 @@ export function useEditorHeaderFileRename(activeFile: OpenFile): EditorHeaderFil
// Why: the header renders one unkeyed path strip for every file, so a file
// switch mid-rename would otherwise commit the typed name against the new path.
if (renameFilePath !== activeFile.filePath) {
renameCancelledRef.current = true
setRenameFilePath(activeFile.filePath)
setIsRenaming(false)
}