@@ -19,37 +19,33 @@ export type SettingsEnvironmentsSectionProps = {
1919 environmentDraftScript : string ;
2020 environmentSavedScript : string | null ;
2121 environmentDirty : boolean ;
22+ worktreesFolderDraft : string ;
23+ worktreesFolderSaved : string | null ;
24+ worktreesFolderDirty : boolean ;
2225 onSetEnvironmentWorkspaceId : Dispatch < SetStateAction < string | null > > ;
2326 onSetEnvironmentDraftScript : Dispatch < SetStateAction < string > > ;
27+ onSetWorktreesFolderDraft : Dispatch < SetStateAction < string > > ;
2428 onSaveEnvironmentSetup : ( ) => Promise < void > ;
2529} ;
2630
2731export const useSettingsEnvironmentsSection = ( {
2832 mainWorkspaces,
2933 onUpdateWorkspaceSettings,
3034} : UseSettingsEnvironmentsSectionArgs ) : SettingsEnvironmentsSectionProps => {
31- const [ environmentWorkspaceId , setEnvironmentWorkspaceId ] = useState < string | null > (
32- null ,
33- ) ;
35+ const [ environmentWorkspaceId , setEnvironmentWorkspaceId ] = useState < string | null > ( null ) ;
3436 const [ environmentDraftScript , setEnvironmentDraftScript ] = useState ( "" ) ;
35- const [ environmentSavedScript , setEnvironmentSavedScript ] = useState < string | null > (
36- null ,
37- ) ;
38- const [ environmentLoadedWorkspaceId , setEnvironmentLoadedWorkspaceId ] = useState <
39- string | null
40- > ( null ) ;
37+ const [ environmentSavedScript , setEnvironmentSavedScript ] = useState < string | null > ( null ) ;
38+ const [ environmentLoadedWorkspaceId , setEnvironmentLoadedWorkspaceId ] = useState < string | null > ( null ) ;
4139 const [ environmentError , setEnvironmentError ] = useState < string | null > ( null ) ;
4240 const [ environmentSaving , setEnvironmentSaving ] = useState ( false ) ;
41+ const [ worktreesFolderDraft , setWorktreesFolderDraft ] = useState ( "" ) ;
42+ const [ worktreesFolderSaved , setWorktreesFolderSaved ] = useState < string | null > ( null ) ;
4343
4444 const environmentWorkspace = useMemo ( ( ) => {
45- if ( mainWorkspaces . length === 0 ) {
46- return null ;
47- }
45+ if ( mainWorkspaces . length === 0 ) return null ;
4846 if ( environmentWorkspaceId ) {
4947 const found = mainWorkspaces . find ( ( workspace ) => workspace . id === environmentWorkspaceId ) ;
50- if ( found ) {
51- return found ;
52- }
48+ if ( found ) return found ;
5349 }
5450 return mainWorkspaces [ 0 ] ?? null ;
5551 } , [ environmentWorkspaceId , mainWorkspaces ] ) ;
@@ -58,11 +54,16 @@ export const useSettingsEnvironmentsSection = ({
5854 return normalizeWorktreeSetupScript ( environmentWorkspace ?. settings . worktreeSetupScript ) ;
5955 } , [ environmentWorkspace ?. settings . worktreeSetupScript ] ) ;
6056
57+ const worktreesFolderFromWorkspace = useMemo ( ( ) => {
58+ return environmentWorkspace ?. settings . worktreesFolder ?? null ;
59+ } , [ environmentWorkspace ?. settings . worktreesFolder ] ) ;
60+
6161 const environmentDraftNormalized = useMemo ( ( ) => {
6262 return normalizeWorktreeSetupScript ( environmentDraftScript ) ;
6363 } , [ environmentDraftScript ] ) ;
6464
6565 const environmentDirty = environmentDraftNormalized !== environmentSavedScript ;
66+ const worktreesFolderDirty = ( worktreesFolderDraft . trim ( ) || null ) !== worktreesFolderSaved ;
6667
6768 useEffect ( ( ) => {
6869 if ( ! environmentWorkspace ) {
@@ -72,53 +73,61 @@ export const useSettingsEnvironmentsSection = ({
7273 setEnvironmentDraftScript ( "" ) ;
7374 setEnvironmentError ( null ) ;
7475 setEnvironmentSaving ( false ) ;
76+ setWorktreesFolderDraft ( "" ) ;
77+ setWorktreesFolderSaved ( null ) ;
7578 return ;
7679 }
77-
7880 if ( environmentWorkspaceId !== environmentWorkspace . id ) {
7981 setEnvironmentWorkspaceId ( environmentWorkspace . id ) ;
8082 }
8183 } , [ environmentWorkspace , environmentWorkspaceId ] ) ;
8284
8385 useEffect ( ( ) => {
84- if ( ! environmentWorkspace ) {
85- return ;
86- }
87-
86+ if ( ! environmentWorkspace ) return ;
8887 if ( environmentLoadedWorkspaceId !== environmentWorkspace . id ) {
8988 setEnvironmentLoadedWorkspaceId ( environmentWorkspace . id ) ;
9089 setEnvironmentSavedScript ( environmentSavedScriptFromWorkspace ) ;
9190 setEnvironmentDraftScript ( environmentSavedScriptFromWorkspace ?? "" ) ;
91+ setWorktreesFolderSaved ( worktreesFolderFromWorkspace ) ;
92+ setWorktreesFolderDraft ( worktreesFolderFromWorkspace ?? "" ) ;
9293 setEnvironmentError ( null ) ;
9394 return ;
9495 }
95-
9696 if ( ! environmentDirty && environmentSavedScript !== environmentSavedScriptFromWorkspace ) {
9797 setEnvironmentSavedScript ( environmentSavedScriptFromWorkspace ) ;
9898 setEnvironmentDraftScript ( environmentSavedScriptFromWorkspace ?? "" ) ;
9999 setEnvironmentError ( null ) ;
100100 }
101+ if ( ! worktreesFolderDirty && worktreesFolderSaved !== worktreesFolderFromWorkspace ) {
102+ setWorktreesFolderSaved ( worktreesFolderFromWorkspace ) ;
103+ setWorktreesFolderDraft ( worktreesFolderFromWorkspace ?? "" ) ;
104+ }
101105 } , [
102106 environmentDirty ,
103107 environmentLoadedWorkspaceId ,
104108 environmentSavedScript ,
105109 environmentSavedScriptFromWorkspace ,
106110 environmentWorkspace ,
111+ worktreesFolderDirty ,
112+ worktreesFolderFromWorkspace ,
113+ worktreesFolderSaved ,
107114 ] ) ;
108115
109116 const handleSaveEnvironmentSetup = async ( ) => {
110- if ( ! environmentWorkspace || environmentSaving ) {
111- return ;
112- }
117+ if ( ! environmentWorkspace || environmentSaving ) return ;
113118 const nextScript = environmentDraftNormalized ;
119+ const nextFolder = worktreesFolderDraft . trim ( ) || null ;
114120 setEnvironmentSaving ( true ) ;
115121 setEnvironmentError ( null ) ;
116122 try {
117123 await onUpdateWorkspaceSettings ( environmentWorkspace . id , {
118124 worktreeSetupScript : nextScript ,
125+ worktreesFolder : nextFolder ,
119126 } ) ;
120127 setEnvironmentSavedScript ( nextScript ) ;
121128 setEnvironmentDraftScript ( nextScript ?? "" ) ;
129+ setWorktreesFolderSaved ( nextFolder ) ;
130+ setWorktreesFolderDraft ( nextFolder ?? "" ) ;
122131 } catch ( error ) {
123132 setEnvironmentError ( error instanceof Error ? error . message : String ( error ) ) ;
124133 } finally {
@@ -134,8 +143,12 @@ export const useSettingsEnvironmentsSection = ({
134143 environmentDraftScript,
135144 environmentSavedScript,
136145 environmentDirty,
146+ worktreesFolderDraft,
147+ worktreesFolderSaved,
148+ worktreesFolderDirty,
137149 onSetEnvironmentWorkspaceId : setEnvironmentWorkspaceId ,
138150 onSetEnvironmentDraftScript : setEnvironmentDraftScript ,
151+ onSetWorktreesFolderDraft : setWorktreesFolderDraft ,
139152 onSaveEnvironmentSetup : handleSaveEnvironmentSetup ,
140153 } ;
141154} ;
0 commit comments