@@ -54,6 +54,10 @@ function AppInner() {
5454 undefined
5555 ) ;
5656 const [ workspaceModalLoadError , setWorkspaceModalLoadError ] = useState < string | null > ( null ) ;
57+ const [ workspaceModalStartMessage , setWorkspaceModalStartMessage ] = useState < string | undefined > (
58+ undefined
59+ ) ;
60+ const [ workspaceModalModel , setWorkspaceModalModel ] = useState < string | undefined > ( undefined ) ;
5761 const workspaceModalProjectRef = useRef < string | null > ( null ) ;
5862
5963 // Auto-collapse sidebar on mobile by default
@@ -175,46 +179,55 @@ function AppInner() {
175179 [ removeProject , selectedWorkspace , setSelectedWorkspace ]
176180 ) ;
177181
178- const handleAddWorkspace = useCallback ( async ( projectPath : string ) => {
179- const projectName = projectPath . split ( "/" ) . pop ( ) ?? projectPath . split ( "\\" ) . pop ( ) ?? "project" ;
182+ const handleAddWorkspace = useCallback (
183+ async (
184+ projectPath : string ,
185+ initialData ?: { startMessage ?: string ; model ?: string ; error ?: string }
186+ ) => {
187+ const projectName =
188+ projectPath . split ( "/" ) . pop ( ) ?? projectPath . split ( "\\" ) . pop ( ) ?? "project" ;
189+
190+ workspaceModalProjectRef . current = projectPath ;
191+ setWorkspaceModalProject ( projectPath ) ;
192+ setWorkspaceModalProjectName ( projectName ) ;
193+ setWorkspaceModalBranches ( [ ] ) ;
194+ setWorkspaceModalDefaultTrunk ( undefined ) ;
195+ setWorkspaceModalLoadError ( initialData ?. error ?? null ) ;
196+ setWorkspaceModalStartMessage ( initialData ?. startMessage ) ;
197+ setWorkspaceModalModel ( initialData ?. model ) ;
198+ setWorkspaceModalOpen ( true ) ;
180199
181- workspaceModalProjectRef . current = projectPath ;
182- setWorkspaceModalProject ( projectPath ) ;
183- setWorkspaceModalProjectName ( projectName ) ;
184- setWorkspaceModalBranches ( [ ] ) ;
185- setWorkspaceModalDefaultTrunk ( undefined ) ;
186- setWorkspaceModalLoadError ( null ) ;
187- setWorkspaceModalOpen ( true ) ;
200+ try {
201+ const branchResult = await window . api . projects . listBranches ( projectPath ) ;
188202
189- try {
190- const branchResult = await window . api . projects . listBranches ( projectPath ) ;
203+ // Guard against race condition: only update state if this is still the active project
204+ if ( workspaceModalProjectRef . current !== projectPath ) {
205+ return ;
206+ }
191207
192- // Guard against race condition: only update state if this is still the active project
193- if ( workspaceModalProjectRef . current !== projectPath ) {
194- return ;
195- }
208+ const sanitizedBranches = Array . isArray ( branchResult ?. branches )
209+ ? branchResult . branches . filter ( ( branch ) : branch is string => typeof branch === "string" )
210+ : [ ] ;
196211
197- const sanitizedBranches = Array . isArray ( branchResult ?. branches )
198- ? branchResult . branches . filter ( ( branch ) : branch is string => typeof branch === "string" )
199- : [ ] ;
212+ const recommended =
213+ typeof branchResult ?. recommendedTrunk === "string" &&
214+ sanitizedBranches . includes ( branchResult . recommendedTrunk )
215+ ? branchResult . recommendedTrunk
216+ : sanitizedBranches [ 0 ] ;
200217
201- const recommended =
202- typeof branchResult ?. recommendedTrunk === "string" &&
203- sanitizedBranches . includes ( branchResult . recommendedTrunk )
204- ? branchResult . recommendedTrunk
205- : sanitizedBranches [ 0 ] ;
206-
207- setWorkspaceModalBranches ( sanitizedBranches ) ;
208- setWorkspaceModalDefaultTrunk ( recommended ) ;
209- setWorkspaceModalLoadError ( null ) ;
210- } catch ( err ) {
211- console . error ( "Failed to load branches for modal:" , err ) ;
212- const message = err instanceof Error ? err . message : "Unknown error" ;
213- setWorkspaceModalLoadError (
214- `Unable to load branches automatically: ${ message } . You can still enter the trunk branch manually.`
215- ) ;
216- }
217- } , [ ] ) ;
218+ setWorkspaceModalBranches ( sanitizedBranches ) ;
219+ setWorkspaceModalDefaultTrunk ( recommended ) ;
220+ setWorkspaceModalLoadError ( null ) ;
221+ } catch ( err ) {
222+ console . error ( "Failed to load branches for modal:" , err ) ;
223+ const message = err instanceof Error ? err . message : "Unknown error" ;
224+ setWorkspaceModalLoadError (
225+ `Unable to load branches automatically: ${ message } . You can still enter the trunk branch manually.`
226+ ) ;
227+ }
228+ } ,
229+ [ ]
230+ ) ;
218231
219232 // Memoize callbacks to prevent LeftSidebar/ProjectSidebar re-renders
220233 const handleAddProjectCallback = useCallback ( ( ) => {
@@ -238,7 +251,9 @@ function AppInner() {
238251 const handleCreateWorkspace = async (
239252 branchName : string ,
240253 trunkBranch : string ,
241- runtime ?: string
254+ runtime ?: string ,
255+ startMessage ?: string ,
256+ model ?: string
242257 ) => {
243258 if ( ! workspaceModalProject ) return ;
244259
@@ -274,6 +289,26 @@ function AppInner() {
274289 const runtimeKey = getRuntimeKey ( workspaceModalProject ) ;
275290 localStorage . setItem ( runtimeKey , runtime ) ;
276291 }
292+
293+ // Send start message if provided
294+ if ( startMessage ) {
295+ // Build send message options - use provided model or default
296+ const { buildSendMessageOptions } = await import ( "@/hooks/useSendMessageOptions" ) ;
297+ const sendOptions = buildSendMessageOptions ( newWorkspace . workspaceId ) ;
298+
299+ if ( model ) {
300+ sendOptions . model = model ;
301+ }
302+
303+ // Defer until React finishes rendering and WorkspaceStore subscribes
304+ requestAnimationFrame ( ( ) => {
305+ void window . api . workspace . sendMessage (
306+ newWorkspace . workspaceId ,
307+ startMessage ,
308+ sendOptions
309+ ) ;
310+ } ) ;
311+ }
277312 }
278313 } ;
279314
@@ -615,6 +650,30 @@ function AppInner() {
615650 ) ;
616651 } , [ projects , setSelectedWorkspace , setWorkspaceMetadata ] ) ;
617652
653+ // Handle open new workspace modal event
654+ useEffect ( ( ) => {
655+ const handleOpenNewWorkspaceModal = ( e : Event ) => {
656+ const customEvent = e as CustomEvent < {
657+ projectPath : string ;
658+ startMessage ?: string ;
659+ model ?: string ;
660+ error ?: string ;
661+ } > ;
662+ const { projectPath, startMessage, model, error } = customEvent . detail ;
663+ void handleAddWorkspace ( projectPath , { startMessage, model, error } ) ;
664+ } ;
665+
666+ window . addEventListener (
667+ CUSTOM_EVENTS . OPEN_NEW_WORKSPACE_MODAL ,
668+ handleOpenNewWorkspaceModal as EventListener
669+ ) ;
670+ return ( ) =>
671+ window . removeEventListener (
672+ CUSTOM_EVENTS . OPEN_NEW_WORKSPACE_MODAL ,
673+ handleOpenNewWorkspaceModal as EventListener
674+ ) ;
675+ } , [ handleAddWorkspace ] ) ;
676+
618677 return (
619678 < >
620679 < div className = "bg-bg-dark flex h-screen overflow-hidden [@media(max-width:768px)]:flex-col" >
@@ -682,6 +741,8 @@ function AppInner() {
682741 branches = { workspaceModalBranches }
683742 defaultTrunkBranch = { workspaceModalDefaultTrunk }
684743 loadErrorMessage = { workspaceModalLoadError }
744+ initialStartMessage = { workspaceModalStartMessage }
745+ initialModel = { workspaceModalModel }
685746 onClose = { ( ) => {
686747 workspaceModalProjectRef . current = null ;
687748 setWorkspaceModalOpen ( false ) ;
@@ -690,6 +751,8 @@ function AppInner() {
690751 setWorkspaceModalBranches ( [ ] ) ;
691752 setWorkspaceModalDefaultTrunk ( undefined ) ;
692753 setWorkspaceModalLoadError ( null ) ;
754+ setWorkspaceModalStartMessage ( undefined ) ;
755+ setWorkspaceModalModel ( undefined ) ;
693756 } }
694757 onAdd = { handleCreateWorkspace }
695758 />
0 commit comments