diff --git a/src/renderer/src/components/editor/UntitledFileRenameDialog.tsx b/src/renderer/src/components/editor/UntitledFileRenameDialog.tsx index c931e97b6..eff040e2c 100644 --- a/src/renderer/src/components/editor/UntitledFileRenameDialog.tsx +++ b/src/renderer/src/components/editor/UntitledFileRenameDialog.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useEffect, useRef, useState } from 'react' +import React, { useCallback, useRef, useState } from 'react' import { FolderOpen } from 'lucide-react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' @@ -41,6 +41,25 @@ export function UntitledFileRenameDialog({ const displayError = externalError ?? error + const cancelFocusFrame = useCallback(() => { + if (focusFrameRef.current !== null) { + cancelAnimationFrame(focusFrameRef.current) + focusFrameRef.current = null + } + }, []) + + const setNameInputNode = useCallback( + (node: HTMLInputElement | null): void => { + if (!node) { + // Why: on a quick close, Radix may unmount the input before the + // deferred focus frame runs. Cancel it from the ref cleanup path. + cancelFocusFrame() + } + nameInputRef.current = node + }, + [cancelFocusFrame] + ) + // Why: seed the drafts before the open dialog paints; focus is handled by // Radix's open lifecycle below so this does not need a post-render Effect. if (open) { @@ -55,15 +74,6 @@ 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) { @@ -108,9 +118,7 @@ export function UntitledFileRenameDialog({ className="max-w-[340px]" onOpenAutoFocus={(event) => { event.preventDefault() - if (focusFrameRef.current !== null) { - cancelAnimationFrame(focusFrameRef.current) - } + cancelFocusFrame() focusFrameRef.current = requestAnimationFrame(() => { focusFrameRef.current = null nameInputRef.current?.focus() @@ -129,7 +137,7 @@ export function UntitledFileRenameDialog({
{ setName(e.target.value)