| 12 | } | null; |
| 13 | |
| 14 | export function useBranchSwitcher({ |
| 15 | activeWorkspace, |
| 16 | checkoutBranch, |
| 17 | setActiveWorkspaceId, |
| 18 | }: UseBranchSwitcherOptions) { |
| 19 | const [branchSwitcher, setBranchSwitcher] = useState<BranchSwitcherState>(null); |
| 20 | |
| 21 | const openBranchSwitcher = useCallback(() => { |
| 22 | if ( |
| 23 | !activeWorkspace || |
| 24 | !activeWorkspace.connected || |
| 25 | activeWorkspace.kind === "worktree" |
| 26 | ) { |
| 27 | return; |
| 28 | } |
| 29 | setBranchSwitcher({ isOpen: true }); |
| 30 | }, [activeWorkspace]); |
| 31 | |
| 32 | const closeBranchSwitcher = useCallback(() => { |
| 33 | setBranchSwitcher(null); |
| 34 | }, []); |
| 35 | |
| 36 | const handleBranchSelect = useCallback( |
| 37 | async (branchName: string, worktreeWorkspace: WorkspaceInfo | null) => { |
| 38 | closeBranchSwitcher(); |
| 39 | if (worktreeWorkspace) { |
| 40 | setActiveWorkspaceId(worktreeWorkspace.id); |
| 41 | } else { |
| 42 | await checkoutBranch(branchName); |
| 43 | } |
| 44 | }, |
| 45 | [checkoutBranch, closeBranchSwitcher, setActiveWorkspaceId], |
| 46 | ); |
| 47 | |
| 48 | return { |
| 49 | branchSwitcher, |
| 50 | openBranchSwitcher, |
| 51 | closeBranchSwitcher, |
| 52 | handleBranchSelect, |
| 53 | }; |
| 54 | } |