@@ -3,24 +3,28 @@ import { ChevronDownIcon } from "@radix-ui/react-icons";
33import { Button , DropdownMenu , Flex , Text , TextField } from "@radix-ui/themes" ;
44import type { Responsive } from "@radix-ui/themes/dist/esm/props/prop-def.js" ;
55import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
6+ import type { RunMode } from "./RunModeSelect" ;
67
78const MAX_DISPLAYED_BRANCHES = 20 ;
89
910interface BranchSelectProps {
1011 value : string | null ; // null means use default branch
1112 onChange : ( branch : string | null ) => void ;
1213 directoryPath : string ;
14+ runMode : RunMode ;
1315 size ?: Responsive < "1" | "2" > ;
1416}
1517
1618export function BranchSelect ( {
1719 value,
1820 onChange,
1921 directoryPath,
22+ runMode,
2023 size = "1" ,
2124} : BranchSelectProps ) {
2225 const [ branches , setBranches ] = useState < string [ ] > ( [ ] ) ;
2326 const [ defaultBranch , setDefaultBranch ] = useState < string > ( "" ) ;
27+ const [ currentBranch , setCurrentBranch ] = useState < string > ( "" ) ;
2428 const [ isCreatingNew , setIsCreatingNew ] = useState ( false ) ;
2529 const [ newBranchName , setNewBranchName ] = useState ( "" ) ;
2630 const [ searchQuery , setSearchQuery ] = useState ( "" ) ;
@@ -32,6 +36,7 @@ export function BranchSelect({
3236 setIsLoading ( false ) ;
3337 setBranches ( [ ] ) ;
3438 setDefaultBranch ( "" ) ;
39+ setCurrentBranch ( "" ) ;
3540 return ;
3641 }
3742
@@ -41,15 +46,18 @@ export function BranchSelect({
4146 const load = async ( ) => {
4247 setIsLoading ( true ) ;
4348 try {
44- const [ allBranches , detectedDefault ] = await Promise . all ( [
45- window . electronAPI . getAllBranches ( directoryPath ) ,
46- window . electronAPI . getDefaultBranch ( directoryPath ) ,
47- ] ) ;
49+ const [ allBranches , detectedDefault , detectedCurrent ] =
50+ await Promise . all ( [
51+ window . electronAPI . getAllBranches ( directoryPath ) ,
52+ window . electronAPI . getDefaultBranch ( directoryPath ) ,
53+ window . electronAPI . getCurrentBranch ( directoryPath ) ,
54+ ] ) ;
4855
4956 if ( cancelled ) return ;
5057
5158 setBranches ( allBranches ) ;
5259 setDefaultBranch ( detectedDefault ) ;
60+ setCurrentBranch ( detectedCurrent ?? "" ) ;
5361 } catch ( _error ) {
5462 } finally {
5563 if ( ! cancelled ) {
@@ -65,25 +73,39 @@ export function BranchSelect({
6573 } ;
6674 } , [ directoryPath ] ) ;
6775
76+ // Determine which branch to use as the initial value based on run mode
77+ const initialBranch =
78+ runMode === "local" ? currentBranch || defaultBranch : defaultBranch ;
79+
6880 useEffect ( ( ) => {
69- if ( ! hasSetInitialValue . current && value === null && defaultBranch ) {
81+ if ( ! hasSetInitialValue . current && value === null && initialBranch ) {
7082 hasSetInitialValue . current = true ;
71- onChange ( defaultBranch ) ;
83+ onChange ( initialBranch ) ;
84+ }
85+ } , [ initialBranch , value , onChange ] ) ;
86+
87+ // Reset branch selection when runMode changes
88+ useEffect ( ( ) => {
89+ if ( initialBranch ) {
90+ onChange ( initialBranch ) ;
7291 }
73- } , [ defaultBranch , value , onChange ] ) ;
92+ } , [ initialBranch , onChange ] ) ; // eslint-disable-line react-hooks/exhaustive-deps
7493
7594 const handleOpenChange = useCallback (
7695 async ( open : boolean ) => {
7796 if ( open ) {
7897 setSearchQuery ( "" ) ;
7998 if ( directoryPath ) {
8099 try {
81- const [ allBranches , detectedDefault ] = await Promise . all ( [
82- window . electronAPI . getAllBranches ( directoryPath ) ,
83- window . electronAPI . getDefaultBranch ( directoryPath ) ,
84- ] ) ;
100+ const [ allBranches , detectedDefault , detectedCurrent ] =
101+ await Promise . all ( [
102+ window . electronAPI . getAllBranches ( directoryPath ) ,
103+ window . electronAPI . getDefaultBranch ( directoryPath ) ,
104+ window . electronAPI . getCurrentBranch ( directoryPath ) ,
105+ ] ) ;
85106 setBranches ( allBranches ) ;
86107 setDefaultBranch ( detectedDefault ) ;
108+ setCurrentBranch ( detectedCurrent ?? "" ) ;
87109 } catch ( _error ) { }
88110 }
89111 }
0 commit comments