From 009b7c699c670d0c6e70bad0240174c65237aed4 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Fri, 29 May 2026 20:33:21 -0700 Subject: [PATCH] fix: cancel workspace focus frames (#3438) --- .../components/NewWorkspaceComposerCard.tsx | 17 +++++++- .../new-workspace/SmartWorkspaceNameField.tsx | 41 +++++++++++++++++-- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/renderer/src/components/NewWorkspaceComposerCard.tsx b/src/renderer/src/components/NewWorkspaceComposerCard.tsx index 2149c95b2..f610c2785 100644 --- a/src/renderer/src/components/NewWorkspaceComposerCard.tsx +++ b/src/renderer/src/components/NewWorkspaceComposerCard.tsx @@ -252,6 +252,7 @@ export default function NewWorkspaceComposerCard({ const defaultTuiAgent = useAppStore((s) => s.settings?.defaultTuiAgent ?? null) const disabledTuiAgents = useAppStore((s) => s.settings?.disabledTuiAgents ?? []) const updateSettings = useAppStore((s) => s.updateSettings) + const nameInputFocusFrameRef = React.useRef(null) const submitShortcutModifierLabel = getScreenSubmitModifierLabel() const selectedRepoName = React.useMemo(() => { const repo = eligibleRepos.find((candidate) => candidate.id === repoId) @@ -272,14 +273,26 @@ export default function NewWorkspaceComposerCard({ [updateSettings] ) + const cancelNameInputFocusFrame = React.useCallback((): void => { + if (nameInputFocusFrameRef.current === null) { + return + } + cancelAnimationFrame(nameInputFocusFrameRef.current) + nameInputFocusFrameRef.current = null + }, []) + + React.useEffect(() => cancelNameInputFocusFrame, [cancelNameInputFocusFrame]) + const focusNameInput = React.useCallback(() => { // Why: after the repo picker commits a choice, moving focus to the name // field keeps the keyboard flow progressing through the form instead of // trapping the user in the repo popover interaction. - requestAnimationFrame(() => { + cancelNameInputFocusFrame() + nameInputFocusFrameRef.current = requestAnimationFrame(() => { + nameInputFocusFrameRef.current = null nameInputRef?.current?.focus() }) - }, [nameInputRef]) + }, [cancelNameInputFocusFrame, nameInputRef]) const visibleQuickAgents = React.useMemo(() => { const enabledIds = new Set( diff --git a/src/renderer/src/components/new-workspace/SmartWorkspaceNameField.tsx b/src/renderer/src/components/new-workspace/SmartWorkspaceNameField.tsx index e6bbaa4bd..a45fb1014 100644 --- a/src/renderer/src/components/new-workspace/SmartWorkspaceNameField.tsx +++ b/src/renderer/src/components/new-workspace/SmartWorkspaceNameField.tsx @@ -198,6 +198,8 @@ export default function SmartWorkspaceNameField({ const tabsListRef = useRef(null) const repoSlugCacheRef = useRef>(new Map()) const handledCrossRepoUrlRef = useRef(null) + const selectedSourceFocusFrameRef = useRef(null) + const localInputFocusFrameRef = useRef(null) const [crossRepoPrompt, setCrossRepoPrompt] = useState<{ link: NonNullable> matchingRepo: RepoOption | null @@ -239,6 +241,30 @@ export default function SmartWorkspaceNameField({ [inputRef] ) + const cancelSelectedSourceFocusFrame = useCallback((): void => { + if (selectedSourceFocusFrameRef.current === null) { + return + } + cancelAnimationFrame(selectedSourceFocusFrameRef.current) + selectedSourceFocusFrameRef.current = null + }, []) + + const cancelLocalInputFocusFrame = useCallback((): void => { + if (localInputFocusFrameRef.current === null) { + return + } + cancelAnimationFrame(localInputFocusFrameRef.current) + localInputFocusFrameRef.current = null + }, []) + + useEffect( + () => () => { + cancelSelectedSourceFocusFrame() + cancelLocalInputFocusFrame() + }, + [cancelLocalInputFocusFrame, cancelSelectedSourceFocusFrame] + ) + useEffect(() => { if (disabled || textOnly) { return @@ -308,9 +334,14 @@ export default function SmartWorkspaceNameField({ setOpen(false) // Why: after Enter accepts a PR/issue row, the input unmounts. Keep the // keyboard flow on the source field so the next Enter advances to Agent. - requestAnimationFrame(() => selectedSourceRef.current?.focus({ preventScroll: true })) + cancelSelectedSourceFocusFrame() + selectedSourceFocusFrameRef.current = requestAnimationFrame(() => { + selectedSourceFocusFrameRef.current = null + selectedSourceRef.current?.focus({ preventScroll: true }) + }) } - }, [selectedSource]) + return cancelSelectedSourceFocusFrame + }, [cancelSelectedSourceFocusFrame, selectedSource]) const normalizedGhQuery = useMemo( () => normalizeGitHubLinkQuery(debouncedQuery), @@ -886,7 +917,11 @@ export default function SmartWorkspaceNameField({ const nextMode = next as SmartNameMode setMode(nextMode) setOpen(!disabled && nextMode !== 'text') - requestAnimationFrame(() => localInputRef.current?.focus({ preventScroll: true })) + cancelLocalInputFocusFrame() + localInputFocusFrameRef.current = requestAnimationFrame(() => { + localInputFocusFrameRef.current = null + localInputRef.current?.focus({ preventScroll: true }) + }) }} className="gap-0" >