@@ -149,6 +149,13 @@ function AppInner() {
149149 ) ;
150150 const [ workspaceModalOpen , setWorkspaceModalOpen ] = useState ( false ) ;
151151 const [ workspaceModalProject , setWorkspaceModalProject ] = useState < string | null > ( null ) ;
152+ const [ workspaceModalProjectName , setWorkspaceModalProjectName ] = useState < string > ( "" ) ;
153+ const [ workspaceModalBranches , setWorkspaceModalBranches ] = useState < string [ ] > ( [ ] ) ;
154+ const [ workspaceModalDefaultTrunk , setWorkspaceModalDefaultTrunk ] = useState < string | undefined > (
155+ undefined
156+ ) ;
157+ const [ workspaceModalLoadError , setWorkspaceModalLoadError ] = useState < string | null > ( null ) ;
158+ const workspaceModalProjectRef = useRef < string | null > ( null ) ;
152159 const [ sidebarCollapsed , setSidebarCollapsed ] = usePersistedState ( "sidebarCollapsed" , false ) ;
153160
154161 const handleToggleSidebar = useCallback ( ( ) => {
@@ -311,9 +318,45 @@ function AppInner() {
311318 [ removeProject , selectedWorkspace , setSelectedWorkspace ]
312319 ) ;
313320
314- const handleAddWorkspace = useCallback ( ( projectPath : string ) => {
321+ const handleAddWorkspace = useCallback ( async ( projectPath : string ) => {
322+ const projectName = projectPath . split ( "/" ) . pop ( ) ?? projectPath . split ( "\\" ) . pop ( ) ?? "project" ;
323+
324+ workspaceModalProjectRef . current = projectPath ;
315325 setWorkspaceModalProject ( projectPath ) ;
326+ setWorkspaceModalProjectName ( projectName ) ;
327+ setWorkspaceModalBranches ( [ ] ) ;
328+ setWorkspaceModalDefaultTrunk ( undefined ) ;
329+ setWorkspaceModalLoadError ( null ) ;
316330 setWorkspaceModalOpen ( true ) ;
331+
332+ try {
333+ const branchResult = await window . api . projects . listBranches ( projectPath ) ;
334+
335+ // Guard against race condition: only update state if this is still the active project
336+ if ( workspaceModalProjectRef . current !== projectPath ) {
337+ return ;
338+ }
339+
340+ const sanitizedBranches = Array . isArray ( branchResult ?. branches )
341+ ? branchResult . branches . filter ( ( branch ) : branch is string => typeof branch === "string" )
342+ : [ ] ;
343+
344+ const recommended =
345+ typeof branchResult ?. recommendedTrunk === "string" &&
346+ sanitizedBranches . includes ( branchResult . recommendedTrunk )
347+ ? branchResult . recommendedTrunk
348+ : sanitizedBranches [ 0 ] ;
349+
350+ setWorkspaceModalBranches ( sanitizedBranches ) ;
351+ setWorkspaceModalDefaultTrunk ( recommended ) ;
352+ setWorkspaceModalLoadError ( null ) ;
353+ } catch ( err ) {
354+ console . error ( "Failed to load branches for modal:" , err ) ;
355+ const message = err instanceof Error ? err . message : "Unknown error" ;
356+ setWorkspaceModalLoadError (
357+ `Unable to load branches automatically: ${ message } . You can still enter the trunk branch manually.`
358+ ) ;
359+ }
317360 } , [ ] ) ;
318361
319362 // Memoize callbacks to prevent LeftSidebar/ProjectSidebar re-renders
@@ -712,10 +755,18 @@ function AppInner() {
712755 { workspaceModalOpen && workspaceModalProject && (
713756 < NewWorkspaceModal
714757 isOpen = { workspaceModalOpen }
715- projectPath = { workspaceModalProject }
758+ projectName = { workspaceModalProjectName }
759+ branches = { workspaceModalBranches }
760+ defaultTrunkBranch = { workspaceModalDefaultTrunk }
761+ loadErrorMessage = { workspaceModalLoadError }
716762 onClose = { ( ) => {
763+ workspaceModalProjectRef . current = null ;
717764 setWorkspaceModalOpen ( false ) ;
718765 setWorkspaceModalProject ( null ) ;
766+ setWorkspaceModalProjectName ( "" ) ;
767+ setWorkspaceModalBranches ( [ ] ) ;
768+ setWorkspaceModalDefaultTrunk ( undefined ) ;
769+ setWorkspaceModalLoadError ( null ) ;
719770 } }
720771 onAdd = { handleCreateWorkspace }
721772 />
0 commit comments