import { useStore } from '@nanostores/react' import { memo, useEffect } from 'react' import { PrTag } from '@/app/chat/pr-tag' import { StatusRow } from '@/components/chat/status-row' import { type ActionItemSpec, ActionsContextMenu, ActionsMenu, type MenuKit, renderActionItem } from '@/components/ui/actions-menu' import { Button } from '@/components/ui/button' import { Codicon } from '@/components/ui/codicon' import { CopyButton } from '@/components/ui/copy-button' import { DiffCount } from '@/components/ui/diff-count' import type { HermesGitBranch } from '@/global' import { useI18n } from '@/i18n' import { displayPath } from '@/lib/display-path' import { openWorktreeDialog, registerRepoStatusCwd, repoStatusForCwd, repoWorktreesForCwd } from '@/store/coding-status' import { notifyError } from '@/store/notifications' import { $pullRequestsByBranch, branchPrKey, refreshPullRequests } from '@/store/pull-requests' // Tiny uppercase section header, matching the composer "+" menu's labels. const MENU_SECTION = 'text-[0.625rem] font-semibold uppercase tracking-wider text-(--ui-text-tertiary)' interface CodingStatusRowProps { /** Branch the current draft off into a fresh worktree + session, based on * `base` (a branch name; omitted = current HEAD). The composer owns the * draft, so it supplies the orchestration; the row just collects the new * branch name + base. Omitted (e.g. remote backend) hides the affordance. */ onBranchOff?: (branch: string, base?: string) => Promise /** Check an existing branch out into a fresh worktree + session (no new * branch). Drives the dialog's "convert a branch" picker. */ onConvertBranch?: (branch: string, path?: null | string, isDefault?: boolean) => Promise /** List the repo's local branches for the "convert a branch" picker. */ onListBranches?: () => Promise /** Open the review pane (changed files + diffs). */ onOpen?: () => void /** Jump into an existing worktree (open a fresh session anchored there). */ onOpenWorktree?: (path: string) => void /** Switch the current repo checkout to another branch. */ onSwitchBranch?: (branch: string) => Promise /** Repo root path for the worktree dialog. */ repoPath?: null | string } /** * The always-on coding-context row, the BASE of the composer status stack: * current branch, dirty summary (+/-), and ahead/behind. A touch more prominent * than the per-turn rows above it (larger branch label, accent glyph), and the * entry point to the review pane. Hidden when the active session isn't in a * local git repo (the probe returns null). */ export const CodingStatusRow = memo(function CodingStatusRow({ onBranchOff, onConvertBranch, onListBranches, onOpen, onOpenWorktree, onSwitchBranch, repoPath }: CodingStatusRowProps) { const { t } = useI18n() const s = t.statusStack.coding const p = t.sidebar.projects const fileMenu = t.fileMenu const resolvedRepoPath = repoPath?.trim() || undefined // This surface's OWN worktree, always — never the primary's. The row used to // fall back to the global `$repoStatus` for a blank repoPath, which painted // the main pane's branch/± onto a tile whose cwd hadn't resolved yet. That // fallback bought nothing (the primary's computed is keyed to `$currentCwd`, // which is blank in exactly the same case) and cost a wrong-tree rail. const status = useStore(repoStatusForCwd(resolvedRepoPath)) const worktrees = useStore(repoWorktreesForCwd(resolvedRepoPath)) // While mounted, keep this worktree in the coding-status refresh set so the // turn-settle / tool-complete / focus edges re-probe it too (tiles otherwise // only refreshed when the MAIN cwd probe happened to cover them). useEffect(() => registerRepoStatusCwd(resolvedRepoPath), [resolvedRepoPath]) // The branch's PR, so the rail links to it instead of leaving you to go find // it. One `gh` lookup for this one branch, TTL-cached in the store and shared // with the sidebar's badges. const prBranch = status?.detached ? null : status?.branch || null useEffect(() => { if (resolvedRepoPath && prBranch) { void refreshPullRequests({ [resolvedRepoPath]: [prBranch] }) } }, [resolvedRepoPath, prBranch]) const pr = useStore($pullRequestsByBranch)[resolvedRepoPath && prBranch ? branchPrKey(resolvedRepoPath, prBranch) : ''] const switchToBranch = async (branch: string) => { if (!onSwitchBranch) { return } try { await onSwitchBranch(branch) } catch (err) { notifyError(err, s.switchFailed(branch)) } } // useKeybinds now handles the ⌘⇧B hotkey globally, through // openWorktreeDialog. One dialog is mounted in the sidebar, so N mounted // rails can no longer each open their own copy. The menu items below only // publish the intent. They pin the repo of THIS rail, so the kebab of a tile // targets the worktree of that tile. const startBranch = (base: string | undefined) => { void openWorktreeDialog({ base, repoPath: resolvedRepoPath }) } if (!status) { return null } const branchLabel = status.detached ? s.detached : status.branch || s.noBranch // The kebab offers branching off the trunk and/or the current branch. The // worktree-add bases the new branch on `base` (a branch name; undefined = // current HEAD). We dedupe so "on main" shows a single trunk entry, and fall // back to a plain off-HEAD branch when no trunk is detected. const current = status.detached ? null : status.branch const branchTargets: { base: string | undefined; label: string }[] = [] // Current branch first (the 99% "branch off where I am"), then the trunk just // below it ("New branch from main"), deduped when they're the same. if (current) { branchTargets.push({ base: current, label: s.branchOffFrom(current) }) } if (status.defaultBranch && status.defaultBranch !== current) { branchTargets.push({ base: status.defaultBranch, label: s.branchOffFrom(status.defaultBranch) }) } if (branchTargets.length === 0) { branchTargets.push({ base: undefined, label: s.newBranch }) } const switchTarget = onSwitchBranch && current && status.defaultBranch && status.defaultBranch !== current ? status.defaultBranch : null // Other worktrees to jump into — everything except the one we're already in // (matched by its checked-out branch) and the bare/main placeholder entry. const otherWorktrees = onOpenWorktree ? worktrees.filter(w => w.path && !w.detached && w.branch && w.branch !== current) : [] const hasLineDelta = status.added > 0 || status.removed > 0 // Untracked files carry no line delta vs HEAD, so surface them as a count when // they're the only change (otherwise +/- tells the story). const untrackedOnly = !hasLineDelta && status.untracked > 0 // The branch actions, rendered identically by the kebab dropdown and the // row's right-click menu so the two never drift. `onBranchOff` gates the // whole menu (omitted = remote backend), matching the kebab. const renderBranchItems = (kit: MenuKit) => { const branchItems: ActionItemSpec[] = branchTargets.map(target => ({ key: target.base ?? '__head__', label: {target.label}, onSelect: () => startBranch(target.base) })) const worktreeItems: ActionItemSpec[] = otherWorktrees.map(worktree => ({ key: worktree.path, label: {worktree.branch}, onSelect: () => onOpenWorktree?.(worktree.path) })) return ( <> {s.newBranch} {branchItems.map(item => renderActionItem(kit, item))} {switchTarget && renderActionItem(kit, { key: '__switch__', label: {s.switchTo(switchTarget)}, onSelect: () => void switchToBranch(switchTarget) })} {s.worktrees} {worktreeItems.map(item => renderActionItem(kit, item))} {/* Create a fresh worktree off the current HEAD (the generic "spin up a worktree here", mirroring the sidebar's + button). */} {renderActionItem(kit, { key: '__start__', label: {p.startWork}, onSelect: () => startBranch(undefined) })} {onConvertBranch && renderActionItem(kit, { key: '__convert__', label: {p.convertBranch}, onSelect: () => startBranch(undefined) })} ) } return ( <> } >
{/* PR number first, right against the leading git glyph — the chip borrows that icon instead of carrying a second one of its own (`showIcon={false}`), so the row reads glyph → #number → branch. */} {pr && } {/* Branch name — the other half of the review-pane target. `contents` so the button lays out nothing of its own: the label stays the same flex child it always was, and the hit area is the text. */} {/* Worktree path + copy — plain muted text, not a chip. Always in the flex so hover doesn't reflow the row; opacity alone reveals the pair. The path sizes to its content (the `flex-1` lives on the wrapper) so the glyph sits against the end of the text instead of drifting to the far edge of the row. `displayPath` collapses home → ~; the copy still takes the real absolute path, and it's the shared `CopyButton` so it confirms with the same inline checkmark as every other copy in the app. */} {resolvedRepoPath && (
{displayPath(resolvedRepoPath)}
)} {/* Branch actions kebab — same pattern as the session/worktree rows. ALWAYS laid out; only its opacity flips on hover/focus/open, so revealing it never reflows the row (no layout shift). pointer-events follow opacity so the invisible trigger isn't clickable at rest. */} {onBranchOff && ( )}
{/* The counts describe what's in the review pane, so clicking them opens it. `contents` again: the two spans stay direct flex children of the row, keeping their gap and `ml-auto` behaviour untouched. */} {(status.ahead > 0 || status.behind > 0 || hasLineDelta || untrackedOnly) && ( )}
) })