@@ -13,8 +13,10 @@ import {
1313 getModelKey ,
1414 getRuntimeKey ,
1515 getTrunkBranchKey ,
16+ getTrunkSelectionKey ,
1617 getProjectScopeId ,
1718} from "@/common/constants/storage" ;
19+ import { DEFAULT_TRUNK_BRANCH , TRUNK_SELECTION , type TrunkSelection } from "@/common/constants/workspace" ;
1820import type { UIMode } from "@/common/types/mode" ;
1921import type { ThinkingLevel } from "@/common/types/thinking" ;
2022
@@ -33,6 +35,8 @@ export interface DraftWorkspaceSettings {
3335 runtimeMode : RuntimeMode ;
3436 sshHost : string ;
3537 trunkBranch : string ;
38+ trunkSelection : TrunkSelection ;
39+ customTrunkBranch : string ;
3640}
3741
3842/**
@@ -52,6 +56,7 @@ export function useDraftWorkspaceSettings(
5256 settings : DraftWorkspaceSettings ;
5357 setRuntimeOptions : ( mode : RuntimeMode , host : string ) => void ;
5458 setTrunkBranch : ( branch : string ) => void ;
59+ setTrunkSelection : ( selection : TrunkSelection ) => void ;
5560 getRuntimeString : ( ) => string | undefined ;
5661} {
5762 // Global AI settings (read-only from global state)
@@ -75,22 +80,50 @@ export function useDraftWorkspaceSettings(
7580 ) ;
7681
7782 // Project-scoped trunk branch preference (persisted per project)
78- const [ trunkBranch , setTrunkBranch ] = usePersistedState < string > (
83+ const [ customTrunkBranch , setCustomTrunkBranch ] = usePersistedState < string > (
7984 getTrunkBranchKey ( projectPath ) ,
8085 "" ,
8186 { listener : true }
8287 ) ;
8388
89+ const [ trunkSelection , setTrunkSelection ] = usePersistedState < TrunkSelection > (
90+ getTrunkSelectionKey ( projectPath ) ,
91+ TRUNK_SELECTION . DEFAULT ,
92+ { listener : true }
93+ ) ;
94+
8495 // Parse runtime string into mode and host
8596 const { mode : runtimeMode , host : sshHost } = parseRuntimeModeAndHost ( runtimeString ) ;
8697
87- // Initialize trunk branch from backend recommendation or first branch
98+ // Initialize custom trunk branch from backend recommendation or first branch
8899 useEffect ( ( ) => {
89- if ( ! trunkBranch && branches . length > 0 ) {
90- const defaultBranch = recommendedTrunk ?? branches [ 0 ] ;
91- setTrunkBranch ( defaultBranch ) ;
100+ if ( branches . length === 0 ) {
101+ return ;
92102 }
93- } , [ branches , recommendedTrunk , trunkBranch , setTrunkBranch ] ) ;
103+
104+ const recommendedInList = recommendedTrunk && branches . includes ( recommendedTrunk ) ;
105+ const currentIsValid = customTrunkBranch && branches . includes ( customTrunkBranch ) ;
106+
107+ if ( currentIsValid ) {
108+ return ;
109+ }
110+
111+ const fallback = ( recommendedInList ? recommendedTrunk : undefined ) ?? branches [ 0 ] ;
112+ setCustomTrunkBranch ( fallback ) ;
113+ } , [ branches , recommendedTrunk , customTrunkBranch , setCustomTrunkBranch ] ) ;
114+
115+ // Fall back to custom mode when default "main" is unavailable in the repo
116+ useEffect ( ( ) => {
117+ if (
118+ branches . length === 0 ||
119+ trunkSelection === TRUNK_SELECTION . CUSTOM ||
120+ branches . includes ( DEFAULT_TRUNK_BRANCH )
121+ ) {
122+ return ;
123+ }
124+
125+ setTrunkSelection ( TRUNK_SELECTION . CUSTOM ) ;
126+ } , [ branches , trunkSelection , setTrunkSelection ] ) ;
94127
95128 // Setter for runtime options (updates persisted runtime string)
96129 const setRuntimeOptions = ( newMode : RuntimeMode , newHost : string ) => {
@@ -103,6 +136,17 @@ export function useDraftWorkspaceSettings(
103136 return buildRuntimeString ( runtimeMode , sshHost ) ;
104137 } ;
105138
139+ const resolvedCustomBranch =
140+ customTrunkBranch ||
141+ ( recommendedTrunk ?? branches [ 0 ] ) ||
142+ DEFAULT_TRUNK_BRANCH ;
143+
144+ const defaultAvailable = branches . length === 0 || branches . includes ( DEFAULT_TRUNK_BRANCH ) ;
145+ const effectiveTrunkBranch =
146+ trunkSelection === TRUNK_SELECTION . DEFAULT && defaultAvailable
147+ ? DEFAULT_TRUNK_BRANCH
148+ : resolvedCustomBranch ;
149+
106150 return {
107151 settings : {
108152 model,
@@ -111,10 +155,13 @@ export function useDraftWorkspaceSettings(
111155 use1M,
112156 runtimeMode,
113157 sshHost,
114- trunkBranch,
158+ trunkBranch : effectiveTrunkBranch ,
159+ trunkSelection,
160+ customTrunkBranch : resolvedCustomBranch ,
115161 } ,
116162 setRuntimeOptions,
117- setTrunkBranch,
163+ setTrunkBranch : setCustomTrunkBranch ,
164+ setTrunkSelection,
118165 getRuntimeString,
119166 } ;
120167}
0 commit comments