@@ -53,6 +53,8 @@ function App() {
5353
5454 // Worktree that should auto-enter edit mode for its name (used with focusNewBranchNames config)
5555 const [ autoEditWorktreeId , setAutoEditWorktreeId ] = useState < string | null > ( null ) ;
56+ // Element to restore focus to after editing worktree name
57+ const focusToRestoreRef = useRef < HTMLElement | null > ( null ) ;
5658
5759 // Active project (when viewing main repo terminal instead of a worktree)
5860 // If activeWorktreeId is set, activeProjectId indicates which project's worktree is active
@@ -119,6 +121,9 @@ function App() {
119121 // Per-worktree focus state (which pane has focus)
120122 const [ focusStates , setFocusStates ] = useState < Map < string , FocusedPane > > ( new Map ( ) ) ;
121123
124+ // Counter to trigger focus on main pane (incremented when focus is explicitly requested)
125+ const [ mainFocusTrigger , setMainFocusTrigger ] = useState ( 0 ) ;
126+
122127 // Per-project selected task (persisted to localStorage)
123128 const [ selectedTasksByProject , setSelectedTasksByProject ] = useState < Map < string , string > > ( ( ) => {
124129 try {
@@ -972,6 +977,21 @@ function App() {
972977 } ) ;
973978 } , [ ] ) ;
974979
980+ // Focus main pane of the currently active entity
981+ const handleFocusMain = useCallback ( ( ) => {
982+ const entityId = activeWorktreeId ?? activeScratchId ?? activeProjectId ;
983+ if ( entityId ) {
984+ setFocusStates ( ( prev ) => {
985+ if ( prev . get ( entityId ) === 'main' ) return prev ;
986+ const next = new Map ( prev ) ;
987+ next . set ( entityId , 'main' ) ;
988+ return next ;
989+ } ) ;
990+ // Always increment trigger to ensure focus happens even if state was already 'main'
991+ setMainFocusTrigger ( ( prev ) => prev + 1 ) ;
992+ }
993+ } , [ activeWorktreeId , activeScratchId , activeProjectId ] ) ;
994+
975995 // Switch focus between main and drawer panes
976996 const handleSwitchFocus = useCallback ( ( ) => {
977997 if ( ! activeEntityId ) return ;
@@ -1451,36 +1471,29 @@ function App() {
14511471
14521472 const handleAddWorktree = useCallback (
14531473 async ( projectId : string ) => {
1454- console . log ( '[handleAddWorktree] Called with projectId:' , projectId ) ;
14551474 const project = projects . find ( ( p ) => p . id === projectId ) ;
14561475 if ( ! project ) {
1457- console . log ( '[handleAddWorktree] Project not found' ) ;
14581476 return ;
14591477 }
1460- console . log ( '[handleAddWorktree] Found project:' , project . name , 'path:' , project . path ) ;
1461-
14621478 setExpandedProjects ( ( prev ) => new Set ( [ ...prev , projectId ] ) ) ;
14631479
1464- console . log ( '[handleAddWorktree] About to call createWorktree...' ) ;
14651480 try {
14661481 const worktree = await createWorktree ( project . path ) ;
1467- console . log ( '[handleAddWorktree] createWorktree succeeded:' , worktree . name ) ;
14681482 setLoadingWorktrees ( ( prev ) => new Set ( [ ...prev , worktree . id ] ) ) ;
14691483 setOpenWorktreeIds ( ( prev ) => new Set ( [ ...prev , worktree . id ] ) ) ;
14701484 setActiveWorktreeId ( worktree . id ) ;
14711485 setActiveScratchId ( null ) ;
14721486 // Auto-focus branch name for editing if configured
14731487 if ( config . worktree . focusNewBranchNames ) {
1488+ // For new worktrees, don't set a focusToRestoreRef - the worktree didn't exist before,
1489+ // so there's no valid prior focus within it. We'll fall back to onFocusMain().
1490+ focusToRestoreRef . current = null ;
14741491 setAutoEditWorktreeId ( worktree . id ) ;
14751492 }
14761493 } catch ( err ) {
14771494 const errorMessage = String ( err ) ;
1478- console . log ( '[handleAddWorktree] Error caught:' , errorMessage ) ;
1479- console . log ( '[handleAddWorktree] Error type:' , typeof err ) ;
1480- console . log ( '[handleAddWorktree] Error object:' , err ) ;
14811495 // Check if this is an uncommitted changes error
14821496 if ( errorMessage . includes ( 'uncommitted changes' ) ) {
1483- console . log ( '[handleAddWorktree] Showing stash modal for project:' , project . name ) ;
14841497 setStashError ( null ) ; // Clear any previous error
14851498 setPendingStashProject ( project ) ;
14861499 } else {
@@ -1497,26 +1510,19 @@ function App() {
14971510
14981511 const project = pendingStashProject ;
14991512 setIsStashing ( true ) ;
1500- console . log ( '[handleStashAndCreate] Starting for project:' , project . path ) ;
15011513
15021514 let stashId : string | null = null ;
15031515
15041516 try {
15051517 // Stash the changes and get the stash ID
1506- console . log ( '[handleStashAndCreate] Stashing changes...' ) ;
15071518 stashId = await stashChanges ( project . path ) ;
1508- console . log ( '[handleStashAndCreate] Stash successful with id:' , stashId ) ;
15091519
15101520 // Create the worktree
1511- console . log ( '[handleStashAndCreate] Creating worktree...' ) ;
15121521 const worktree = await createWorktree ( project . path ) ;
1513- console . log ( '[handleStashAndCreate] Worktree created:' , worktree . name ) ;
15141522 setActiveScratchId ( null ) ;
15151523
15161524 // Pop the stash to restore changes
1517- console . log ( '[handleStashAndCreate] Popping stash with id:' , stashId ) ;
15181525 await stashPop ( project . path , stashId ) ;
1519- console . log ( '[handleStashAndCreate] Stash popped' ) ;
15201526
15211527 // Update UI state
15221528 setLoadingWorktrees ( ( prev ) => new Set ( [ ...prev , worktree . id ] ) ) ;
@@ -1525,6 +1531,9 @@ function App() {
15251531 setPendingStashProject ( null ) ;
15261532 // Auto-focus branch name for editing if configured
15271533 if ( config . worktree . focusNewBranchNames ) {
1534+ // For new worktrees, don't set a focusToRestoreRef - the worktree didn't exist before,
1535+ // so there's no valid prior focus within it. We'll fall back to onFocusMain().
1536+ focusToRestoreRef . current = null ;
15281537 setAutoEditWorktreeId ( worktree . id ) ;
15291538 }
15301539 } catch ( err ) {
@@ -1894,6 +1903,7 @@ function App() {
18941903 } , [ ] ) ;
18951904
18961905 const handleRenameBranch = useCallback ( ( worktreeId : string ) => {
1906+ focusToRestoreRef . current = document . activeElement as HTMLElement | null ;
18971907 setAutoEditWorktreeId ( worktreeId ) ;
18981908 } , [ ] ) ;
18991909
@@ -2153,6 +2163,7 @@ function App() {
21532163 // Rename branch shortcut (F2 by default)
21542164 if ( matchesShortcut ( e , mappings . renameBranch ) && activeWorktreeId ) {
21552165 e . preventDefault ( ) ;
2166+ focusToRestoreRef . current = document . activeElement as HTMLElement | null ;
21562167 setAutoEditWorktreeId ( activeWorktreeId ) ;
21572168 return ;
21582169 }
@@ -2643,6 +2654,8 @@ function App() {
26432654 activeScratchCwd = { activeScratchId ? scratchCwds . get ( activeScratchId ) ?? null : null }
26442655 homeDir = { homeDir }
26452656 autoEditWorktreeId = { autoEditWorktreeId }
2657+ focusToRestoreRef = { focusToRestoreRef }
2658+ onFocusMain = { handleFocusMain }
26462659 onToggleProject = { toggleProject }
26472660 onSelectProject = { handleSelectProject }
26482661 onSelectWorktree = { handleSelectWorktree }
@@ -2695,6 +2708,7 @@ function App() {
26952708 mappings = { config . mappings }
26962709 activityTimeout = { config . indicators . activityTimeout }
26972710 shouldAutoFocus = { activeFocusState === 'main' }
2711+ focusTrigger = { mainFocusTrigger }
26982712 configErrors = { configErrors }
26992713 onFocus = { handleMainPaneFocused }
27002714 onWorktreeNotification = { handleWorktreeNotification }
0 commit comments