The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { useGT, useMessages } from "gt-react"; |
| 3 | import { ChevronDownIcon, GitBranchIcon } from "lucide-react"; |
| 4 | import { useState } from "react"; |
| 5 | |
| 6 | import { usePaginatedBranches } from "../state/queries"; |
| 7 | import { useEnvironmentQuery } from "../state/query"; |
| 8 | import { vcsEnvironment } from "../state/vcs"; |
| 9 | import { BranchPicker, BranchPickerRefItem } from "./BranchPicker"; |
| 10 | import { resolveBranchTriggerLabel, sanitizeNewRefName } from "./BranchToolbar.logic"; |
| 11 | import { MiddleTruncate } from "./ui/middle-truncate"; |
| 12 | import { Button } from "./ui/button"; |
| 13 | import { ComboboxTrigger } from "./ui/combobox"; |
| 14 | |
| 15 | export function WorktreeBaseBranchPicker({ |
| 16 | environmentId, |
| 17 | cwd, |
| 18 | value, |
| 19 | onValueChange, |
| 20 | startFromOrigin, |
| 21 | onStartFromOriginChange, |
| 22 | disabled = false, |
| 23 | id, |
| 24 | }: { |
| 25 | environmentId: EnvironmentId; |
| 26 | cwd: string | null; |
| 27 | value: string; |
| 28 | onValueChange: (branch: string) => void; |
| 29 | startFromOrigin: boolean; |
| 30 | onStartFromOriginChange: (checked: boolean) => void; |
| 31 | disabled?: boolean; |
| 32 | id?: string; |
| 33 | }) { |
| 34 | const gt = useGT(); |
| 35 | const messages = useMessages(); |
| 36 | const [open, setOpen] = useState(false); |
| 37 | const [query, setQuery] = useState(""); |
| 38 | const branches = usePaginatedBranches( |
| 39 | { |
| 40 | environmentId, |
| 41 | cwd, |
| 42 | query: sanitizeNewRefName(query.trim()), |
| 43 | }, |
| 44 | messages, |
| 45 | ); |
| 46 | const selectedRefQuery = useEnvironmentQuery( |
| 47 | cwd && value |
| 48 | ? vcsEnvironment.listRefs({ |
| 49 | environmentId, |
| 50 | input: { cwd, query: value, limit: 10 }, |
| 51 | }) |
| 52 | : null, |
| 53 | ); |
| 54 | const selectedRef = |
| 55 | branches.refs.find((branch) => branch.name === value) ?? |
| 56 | selectedRefQuery.data?.refs.find((branch) => branch.name === value); |
| 57 | const label = resolveBranchTriggerLabel( |
| 58 | { |
| 59 | activeWorktreePath: null, |
| 60 | effectiveEnvMode: "worktree", |
| 61 | resolvedActiveBranch: value || null, |
| 62 | resolvedActiveBranchIsRemote: selectedRef ? selectedRef.isRemote === true : null, |
| 63 | startFromOrigin, |
| 64 | }, |
| 65 | messages, |
| 66 | ); |
| 67 | const branchByName = new Map(branches.refs.map((branch) => [branch.name, branch])); |
| 68 | const items = [...branchByName.keys()]; |
| 69 | const hasNextPage = branches.data?.nextCursor != null; |
| 70 | const statusText = |
| 71 | branches.error ?? |
| 72 | (branches.isPending && branches.data === null |
| 73 | ? gt("Loading refs...") |
| 74 | : branches.isFetchingNextPage |
| 75 | ? gt("Loading more refs...") |
| 76 | : hasNextPage |
| 77 | ? gt( |
| 78 | "{total, plural, one {Showing {shown} of {total} refs} other {Showing {shown} of {total} refs}}", |
| 79 | { |
| 80 | shown: branches.refs.length, |
| 81 | total: String(branches.data?.totalCount), |
| 82 | }, |
| 83 | ) |
| 84 | : null); |
| 85 | const handleOpenChange = (next: boolean) => { |
| 86 | setOpen(next); |
| 87 | if (!next) setQuery(""); |
| 88 | }; |
| 89 | return ( |
| 90 | <BranchPicker |
| 91 | items={items} |
| 92 | filteredItems={items} |
| 93 | value={value || null} |
| 94 | query={query} |
| 95 | onQueryChange={setQuery} |
| 96 | open={open && !disabled} |
| 97 | onOpenChange={handleOpenChange} |
| 98 | onSelectItem={(name) => { |
| 99 | onValueChange(name); |
| 100 | handleOpenChange(false); |
| 101 | }} |
| 102 | hasNextPage={hasNextPage} |
| 103 | isFetchingNextPage={branches.isFetchingNextPage} |
| 104 | onLoadNext={branches.loadNext} |
| 105 | statusText={statusText} |
| 106 | originControl={{ checked: startFromOrigin, onCheckedChange: onStartFromOriginChange }} |
| 107 | popupProps={{ align: "start", side: "bottom", className: "flex w-80 flex-col" }} |
| 108 | renderItem={(name, index) => { |
| 109 | const branch = branchByName.get(name); |
| 110 | return branch ? ( |
| 111 | <BranchPickerRefItem |
| 112 | branch={branch} |
| 113 | projectCwd={cwd} |
| 114 | index={index} |
| 115 | onClick={() => { |
| 116 | onValueChange(branch.name); |
| 117 | handleOpenChange(false); |
| 118 | }} |
| 119 | /> |
| 120 | ) : null; |
| 121 | }} |
| 122 | > |
| 123 | <ComboboxTrigger |
| 124 | id={id} |
| 125 | disabled={disabled || !cwd} |
| 126 | render={<Button variant="outline" size="sm" />} |
| 127 | className="w-full justify-between " |
| 128 | > |
| 129 | <GitBranchIcon className="size-3.5 shrink-0 text-muted-foreground" /> |
| 130 | <MiddleTruncate value={label} className="flex-1 text-left" /> |
| 131 | <ChevronDownIcon className="size-3.5 shrink-0 text-muted-foreground" /> |
| 132 | </ComboboxTrigger> |
| 133 | </BranchPicker> |
| 134 | ); |
| 135 | } |