The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
import { useStore } from '@nanostores/react'
import { useEffect, useMemo, useState } from 'react'
import { Button } from '@/components/ui/button'
import { Codicon } from '@/components/ui/codicon'
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandItemCheck,
CommandList
} from '@/components/ui/command'
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
import type { HermesGitBaseBranch } from '@/global'
import { useI18n } from '@/i18n'
import { $repoStatus } from '@/store/coding-status'
import { listBaseBranches } from '@/store/projects'
// Filterable combobox for picking the base branch of a new worktree. Lists
// local + remote-tracking branches, defaults to the default branch
// (origin/HEAD, or local main/master when no remote). The current session's
// branch is sorted to the top so it's one click away. The parent owns the
// selected value via `value` / `onValueChange`.
export function BaseBranchPicker({
disabled,
repoPath,
onValueChange,
value
}: {
disabled?: boolean
repoPath: string
onValueChange: (value: string) => void
value: string
}) {
const { t } = useI18n()
const p = t.sidebar.projects
const repoStatus = useStore($repoStatus)
const [branches, setBranches] = useState<HermesGitBaseBranch[]>([])
const [loading, setLoading] = useState(false)
const [open, setOpen] = useState(false)
const currentBranch = repoStatus?.detached ? null : (repoStatus?.branch ?? null)
// List the repo once per mount/repo (#119745): an empty list is a real
// answer (a folder git cannot list, an unborn HEAD, a backend without the
// endpoint) and a failed list is a real failure — neither may re-trigger
// the load, or a non-git folder re-runs the bridge in a loop while the
// dialog is open. The cleanup ignores a list that lands after the picker
// moved to another repo or unmounted, so a late list can neither paint the
// previous repo's branches nor set this repo's base.
useEffect(() => {
let active = true
setBranches([])
if (!repoPath) {
return () => {
active = false
}
}
setLoading(true)
listBaseBranches(repoPath)
.then(list => {
if (active) {
setBranches(list)
}
})
.catch(() => {
if (active) {
setBranches([])
}
})
.finally(() => {
if (active) {
setLoading(false)
}
})
return () => {
active = false
}
}, [repoPath])
// Default to the remote default (origin/HEAD). Fall back to the local
// default branch (main/master) when no remote exists. Only an EMPTY value
// is filled (#119745): the base the caller chose ("Branch off from
// <current>" in the coding row's kebab) stands even though it is not the
// default. Runs on the settled list, never inside the load, so a stale
// response cannot set another project's base.
const fallback = (branches.find(b => b.isDefault) ?? branches[0])?.name
useEffect(() => {
if (!value && fallback) {
onValueChange(fallback)
}
}, [fallback, onValueChange, value])
// Pin the current session's branch to the top, keep the rest in git's
// most-recently-committed order.
const sorted = useMemo(() => {
if (!currentBranch) {
return branches
}
const idx = branches.findIndex(b => b.name === currentBranch)
if (idx <= 0) {
return branches
}
return [branches[idx], ...branches.slice(0, idx), ...branches.slice(idx + 1)]
}, [branches, currentBranch])
// The i18n function returns { before, after } so the branch name can be
// wrapped in its own styled (underlined) span — works for any word order.
const parts = p.branchOff()
return (
<div className="space-y-1.5">
<Popover
onOpenChange={next => {
setOpen(next)
}}
open={open}
>
<PopoverTrigger asChild>
<Button
className="group w-full flex justify-start items-center min-w-0 gap-1.5 hover:no-underline hover:text-muted-foreground"
disabled={disabled || loading}
size="inline"
variant="text"
>
<Codicon className="shrink-0 text-(--ui-text-tertiary)" name="git-branch" size="0.8rem" />
<span className="shrink-0">{parts.before}</span>
<span className="shrink-0 text-primary underline-offset-4 decoration-current/20 group-hover:underline">
{loading ? '...' : value}
</span>
<Codicon className="shrink-0 text-(--ui-text-tertiary)" name="chevron-down" size="0.75rem" />
<span className="shrink-0">{parts.after}</span>
</Button>
</PopoverTrigger>
<PopoverContent
align="start"
className="z-(--z-modal-popover) min-w-(--radix-popover-trigger-width)"
variant="menu"
>
<Command
filter={(searchValue, search) => (searchValue.toLowerCase().includes(search.toLowerCase()) ? 1 : 0)}
variant="menu"
>
<CommandInput autoFocus placeholder={p.baseBranchPlaceholder} />
<CommandList>
<CommandEmpty>{p.baseBranchNone}</CommandEmpty>
<CommandGroup>
{sorted.map(branch => (
<CommandItem
key={branch.name}
onSelect={() => {
onValueChange(branch.name)
setOpen(false)
}}
value={branch.name}
>
<Codicon
className="shrink-0 text-(--ui-text-tertiary)"
name={branch.isRemote ? 'repo' : 'git-branch'}
size="0.8rem"
/>
<span className="truncate">{branch.name}</span>
{branch.isDefault && <span className="shrink-0 text-[0.625rem] text-(--ui-text-tertiary)">★</span>}
<CommandItemCheck checked={value === branch.name} />
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</div>
)
}