diff --git a/src/renderer/src/components/editor/UntitledFileRenameDialog.tsx b/src/renderer/src/components/editor/UntitledFileRenameDialog.tsx index 187b1fe7dcb..c931e97b693 100644 --- a/src/renderer/src/components/editor/UntitledFileRenameDialog.tsx +++ b/src/renderer/src/components/editor/UntitledFileRenameDialog.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useRef, useState } from 'react' +import React, { useCallback, useEffect, useRef, useState } from 'react' import { FolderOpen } from 'lucide-react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' @@ -36,6 +36,7 @@ export function UntitledFileRenameDialog({ const [dir, setDir] = useState(worktreePath) const [error, setError] = useState(null) const nameInputRef = useRef(null) + const focusFrameRef = useRef(null) const seededOpenStateRef = useRef({ open: false, baseName, worktreePath }) const displayError = externalError ?? error @@ -54,6 +55,15 @@ export function UntitledFileRenameDialog({ seededOpenStateRef.current = { open: false, baseName, worktreePath } } + useEffect(() => { + return () => { + if (focusFrameRef.current !== null) { + cancelAnimationFrame(focusFrameRef.current) + focusFrameRef.current = null + } + } + }, []) + const handleBrowse = useCallback(async () => { const picked = await window.api.shell.pickDirectory({ defaultPath: dir || worktreePath }) if (!picked) { @@ -98,7 +108,11 @@ export function UntitledFileRenameDialog({ className="max-w-[340px]" onOpenAutoFocus={(event) => { event.preventDefault() - requestAnimationFrame(() => { + if (focusFrameRef.current !== null) { + cancelAnimationFrame(focusFrameRef.current) + } + focusFrameRef.current = requestAnimationFrame(() => { + focusFrameRef.current = null nameInputRef.current?.focus() nameInputRef.current?.select() })