@@ -16,7 +16,7 @@ import { RUNTIME_MODE, SSH_RUNTIME_PREFIX } from "@/types/runtime";
1616export type StartWorkspaceCreationDetail =
1717 CustomEventPayloads [ typeof CUSTOM_EVENTS . START_WORKSPACE_CREATION ] ;
1818
19- function normalizeRuntimePreference ( runtime : string | undefined ) : string | undefined {
19+ export function normalizeRuntimePreference ( runtime : string | undefined ) : string | undefined {
2020 if ( ! runtime ) {
2121 return undefined ;
2222 }
@@ -43,72 +43,90 @@ function normalizeRuntimePreference(runtime: string | undefined): string | undef
4343 return trimmed ;
4444}
4545
46+ export function getFirstProjectPath ( projects : Map < string , ProjectConfig > ) : string | null {
47+ const iterator = projects . keys ( ) . next ( ) ;
48+ return iterator . done ? null : iterator . value ;
49+ }
50+
51+ type PersistFn = typeof updatePersistedState ;
52+
53+ export function persistWorkspaceCreationPrefill (
54+ projectPath : string ,
55+ detail : StartWorkspaceCreationDetail | undefined ,
56+ persist : PersistFn = updatePersistedState
57+ ) : void {
58+ if ( ! detail ) {
59+ return ;
60+ }
61+
62+ if ( detail . startMessage !== undefined ) {
63+ persist ( getInputKey ( getPendingScopeId ( projectPath ) ) , detail . startMessage ) ;
64+ }
65+
66+ if ( detail . model !== undefined ) {
67+ persist ( getModelKey ( getProjectScopeId ( projectPath ) ) , detail . model ) ;
68+ }
69+
70+ if ( detail . trunkBranch !== undefined ) {
71+ const normalizedTrunk = detail . trunkBranch . trim ( ) ;
72+ persist (
73+ getTrunkBranchKey ( projectPath ) ,
74+ normalizedTrunk . length > 0 ? normalizedTrunk : undefined
75+ ) ;
76+ }
77+
78+ if ( detail . runtime !== undefined ) {
79+ const normalizedRuntime = normalizeRuntimePreference ( detail . runtime ) ;
80+ persist ( getRuntimeKey ( projectPath ) , normalizedRuntime ) ;
81+ }
82+ }
83+
4684interface UseStartWorkspaceCreationOptions {
4785 projects : Map < string , ProjectConfig > ;
4886 setPendingNewWorkspaceProject : ( projectPath : string | null ) => void ;
4987 setSelectedWorkspace : ( selection : WorkspaceSelection | null ) => void ;
5088}
5189
90+ function resolveProjectPath ( projects : Map < string , ProjectConfig > , requestedPath : string ) : string | null {
91+ if ( projects . has ( requestedPath ) ) {
92+ return requestedPath ;
93+ }
94+
95+ return getFirstProjectPath ( projects ) ;
96+ }
97+
5298export function useStartWorkspaceCreation ( {
5399 projects,
54100 setPendingNewWorkspaceProject,
55101 setSelectedWorkspace,
56102} : UseStartWorkspaceCreationOptions ) {
57- const applyWorkspaceCreationPrefill = useCallback (
58- ( projectPath : string , detail ?: StartWorkspaceCreationDetail ) => {
59- if ( ! detail ) {
60- return ;
61- }
62-
63- if ( detail . startMessage !== undefined ) {
64- updatePersistedState ( getInputKey ( getPendingScopeId ( projectPath ) ) , detail . startMessage ) ;
65- }
66-
67- if ( detail . model ) {
68- updatePersistedState ( getModelKey ( getProjectScopeId ( projectPath ) ) , detail . model ) ;
69- }
70-
71- if ( detail . trunkBranch ) {
72- const normalizedTrunk = detail . trunkBranch . trim ( ) ;
73- updatePersistedState (
74- getTrunkBranchKey ( projectPath ) ,
75- normalizedTrunk . length > 0 ? normalizedTrunk : undefined
76- ) ;
77- }
78-
79- if ( detail . runtime !== undefined ) {
80- const normalizedRuntime = normalizeRuntimePreference ( detail . runtime ) ;
81- updatePersistedState ( getRuntimeKey ( projectPath ) , normalizedRuntime ) ;
82- }
83- } ,
84- [ ]
85- ) ;
86-
87103 const startWorkspaceCreation = useCallback (
88104 ( projectPath : string , detail ?: StartWorkspaceCreationDetail ) => {
89- const hasProject = projects . has ( projectPath ) ;
90- const resolvedProjectPath = hasProject
91- ? projectPath
92- : projects . size > 0
93- ? Array . from ( projects . keys ( ) ) [ 0 ]
94- : null ;
105+ const resolvedProjectPath = resolveProjectPath ( projects , projectPath ) ;
95106
96107 if ( ! resolvedProjectPath ) {
97108 console . warn ( "No projects available for workspace creation" ) ;
98109 return ;
99110 }
100111
101- applyWorkspaceCreationPrefill ( resolvedProjectPath , detail ) ;
112+ persistWorkspaceCreationPrefill ( resolvedProjectPath , detail ) ;
102113 setPendingNewWorkspaceProject ( resolvedProjectPath ) ;
103114 setSelectedWorkspace ( null ) ;
104115 } ,
105- [ projects , applyWorkspaceCreationPrefill , setPendingNewWorkspaceProject , setSelectedWorkspace ]
116+ [ projects , setPendingNewWorkspaceProject , setSelectedWorkspace ]
106117 ) ;
107118
108119 useEffect ( ( ) => {
109120 const handleStartCreation = ( event : Event ) => {
110- const customEvent = event as CustomEvent < StartWorkspaceCreationDetail > ;
111- startWorkspaceCreation ( customEvent . detail . projectPath , customEvent . detail ) ;
121+ const customEvent = event as CustomEvent < StartWorkspaceCreationDetail | undefined > ;
122+ const detail = customEvent . detail ;
123+
124+ if ( ! detail ?. projectPath ) {
125+ console . warn ( "START_WORKSPACE_CREATION event missing projectPath detail" ) ;
126+ return ;
127+ }
128+
129+ startWorkspaceCreation ( detail . projectPath , detail ) ;
112130 } ;
113131
114132 window . addEventListener (
0 commit comments