The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 248 lines 8.79 KB
Raw
1 import { useLegendListRef } from "~/hooks/useLegendListRef";
2 import type { VcsRef } from "@t3tools/contracts";
3 import { LegendList, type LegendListRef } from "@legendapp/list/react";
4 import { useId, useRef, useState, type ComponentProps, type ReactNode } from "react";
5 import { T, useGT } from "gt-react";
6
7 import { MiddleTruncate } from "./ui/middle-truncate";
8 import { cn } from "../lib/utils";
9 import { RefreshIcon } from "./ui/refresh-icon";
10 import { Switch } from "./ui/switch";
11 import { getVirtualizedScrollFadeClassName } from "./ui/scroll-area";
12 import {
13 Combobox,
14 ComboboxEmpty,
15 ComboboxSearchInput,
16 ComboboxItem,
17 ComboboxListVirtualized,
18 ComboboxPopup,
19 ComboboxStatus,
20 } from "./ui/combobox";
21 import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
22
23 export function BranchPicker({
24 items,
25 filteredItems,
26 value,
27 query,
28 onQueryChange,
29 open,
30 onOpenChange,
31 onSelectItem,
32 hasNextPage,
33 isFetchingNextPage,
34 onLoadNext,
35 statusText,
36 notice,
37 originControl,
38 popupProps,
39 renderItem,
40 getItemType,
41 children,
42 }: {
43 items: string[];
44 filteredItems: string[];
45 value: string | null;
46 query: string;
47 onQueryChange: (value: string) => void;
48 open: boolean;
49 onOpenChange: (open: boolean) => void;
50 onSelectItem: (value: string) => void;
51 hasNextPage: boolean;
52 isFetchingNextPage: boolean;
53 onLoadNext: () => void;
54 statusText: string | null;
55 notice?: string | null | undefined;
56 originControl?: { checked: boolean; onCheckedChange: (checked: boolean) => void } | undefined;
57 popupProps: Omit<ComponentProps<typeof ComboboxPopup>, "children">;
58 renderItem: (value: string, index: number) => ReactNode;
59 getItemType?: ((value: string) => string) | undefined;
60 children: ReactNode;
61 }) {
62 const gt = useGT();
63 const highlightedValueRef = useRef<string | null>(null);
64 const startFromOriginSwitchId = useId();
65 const handleOpenChange = (nextOpen: boolean) => {
66 if (!nextOpen) highlightedValueRef.current = null;
67 onOpenChange(nextOpen);
68 };
69 const [showTopBranchScrollFade, setShowTopBranchScrollFade] = useState(false);
70 const [showBottomBranchScrollFade, setShowBottomBranchScrollFade] = useState(false);
71 const fetchNextBranchPage = () => {
72 if (!hasNextPage || isFetchingNextPage) {
73 return;
74 }
75
76 onLoadNext();
77 };
78 const branchListRef = useRef<LegendListRef | null>(null);
79 const updateBranchListScrollFades = () => {
80 const metrics = branchListRef.current?.getState();
81 if (!metrics) return;
82 setShowTopBranchScrollFade(!metrics.isAtStart);
83 setShowBottomBranchScrollFade(!metrics.isAtEnd);
84 };
85 const branchScrollRef = useLegendListRef(branchListRef, updateBranchListScrollFades);
86
87 return (
88 <Combobox
89 items={items}
90 filteredItems={filteredItems}
91 autoHighlight
92 virtualized
93 onItemHighlighted={(value, eventDetails) => {
94 highlightedValueRef.current = typeof value === "string" ? value : null;
95 if (!open || eventDetails.index < 0 || eventDetails.reason !== "keyboard") {
96 return;
97 }
98 void branchListRef.current?.scrollIndexIntoView?.({
99 index: eventDetails.index,
100 animated: false,
101 });
102 }}
103 onOpenChange={handleOpenChange}
104 open={open}
105 value={value}
106 >
107 {children}
108 <ComboboxPopup {...popupProps}>
109 <ComboboxSearchInput
110 placeholder={gt("Search refs...")}
111 value={query}
112 onChange={(event) => {
113 onQueryChange(event.target.value);
114 void branchListRef.current?.scrollToOffset?.({ offset: 0, animated: false });
115 }}
116 onKeyDown={(event) => {
117 if (event.key !== "Enter" || event.nativeEvent.isComposing || event.keyCode === 229)
118 return;
119 const highlightedValue = highlightedValueRef.current;
120 if (highlightedValue === null || !filteredItems.includes(highlightedValue)) return;
121 (
122 event as typeof event & { preventBaseUIHandler?: () => void }
123 ).preventBaseUIHandler?.();
124 event.preventDefault();
125 event.stopPropagation();
126 highlightedValueRef.current = null;
127 onSelectItem(highlightedValue);
128 }}
129 />
130 <div className="flex min-h-0 flex-1 flex-col overflow-hidden">
131 <ComboboxEmpty>
132 <T>No refs found.</T>
133 </ComboboxEmpty>
134 <div className="relative flex min-h-0 w-full max-h-56 flex-1 flex-col overflow-hidden">
135 <ComboboxListVirtualized className="flex size-full min-h-0 min-w-0 flex-col">
136 <LegendList<string>
137 ref={branchScrollRef}
138 data={filteredItems}
139 initialScrollOffset={0}
140 keyExtractor={(item) => item}
141 {...(getItemType ? { getItemType } : {})}
142 renderItem={({ item, index }) => renderItem(item, index)}
143 extraData={renderItem}
144 estimatedItemSize={28}
145 drawDistance={336}
146 onScroll={updateBranchListScrollFades}
147 onLayout={updateBranchListScrollFades}
148 onReady={updateBranchListScrollFades}
149 onEndReached={fetchNextBranchPage}
150 onEndReachedThreshold={0.5}
151 className={cn(
152 "min-h-0 max-h-56 scrollbar-gutter-stable overflow-x-hidden overscroll-y-contain ps-1 pe-0 py-2",
153 getVirtualizedScrollFadeClassName({
154 top: showTopBranchScrollFade,
155 bottom: showBottomBranchScrollFade,
156 }),
157 )}
158 />
159 </ComboboxListVirtualized>
160 </div>
161 {originControl ? (
162 <Tooltip>
163 <TooltipTrigger
164 render={
165 <label
166 htmlFor={startFromOriginSwitchId}
167 className="flex cursor-pointer items-center justify-between gap-3 border-t border-border/60 px-3 py-2 text-xs"
168 >
169 <span className="flex min-w-0 items-center gap-1.5 font-medium text-muted-foreground">
170 <RefreshIcon aria-hidden="true" className="size-3 shrink-0" />
171 <span className="truncate">
172 <T>Start from remote</T>
173 </span>
174 </span>
175 <Switch
176 id={startFromOriginSwitchId}
177 checked={originControl.checked}
178 size="sm"
179 aria-label={gt("Start worktree from remote")}
180 onCheckedChange={(checked) => originControl.onCheckedChange(Boolean(checked))}
181 />
182 </label>
183 }
184 />
185 <TooltipPopup side="top" className="max-w-72 whitespace-normal">
186 <T>
187 Uses the latest commit of the base branch’s tracked remote branch, even if its
188 name differs. Otherwise uses the same-named branch on origin. Uses your local
189 branch if that remote branch is missing.
190 </T>
191 </TooltipPopup>
192 </Tooltip>
193 ) : null}
194 {notice ? <ComboboxStatus>{notice}</ComboboxStatus> : null}
195 {statusText ? <ComboboxStatus>{statusText}</ComboboxStatus> : null}
196 </div>
197 </ComboboxPopup>
198 </Combobox>
199 );
200 }
201
202 export function BranchPickerRefItem({
203 branch: refName,
204 projectCwd: activeProjectCwd,
205 index,
206 value,
207 disabled,
208 onClick,
209 onContextMenu,
210 }: {
211 branch: VcsRef;
212 projectCwd: string | null;
213 index: number;
214 value?: string;
215 disabled?: boolean;
216 onClick: ComponentProps<typeof ComboboxItem>["onClick"];
217 onContextMenu?: ComponentProps<typeof ComboboxItem>["onContextMenu"];
218 }) {
219 const gt = useGT();
220 const itemValue = refName.name;
221 const hasSecondaryWorktree =
222 refName.worktreePath && activeProjectCwd && refName.worktreePath !== activeProjectCwd;
223 const badge = refName.current
224 ? gt("current", { $context: "git ref badge, the checked-out branch" })
225 : hasSecondaryWorktree
226 ? gt("worktree", { $context: "git ref badge, checked out in another worktree" })
227 : refName.isRemote
228 ? gt("remote", { $context: "git ref badge, a remote branch" })
229 : refName.isDefault
230 ? gt("default", { $context: "git ref badge, the default branch" })
231 : null;
232 return (
233 <ComboboxItem
234 disabled={disabled}
235 hideIndicator
236 key={itemValue}
237 index={index}
238 value={value ?? itemValue}
239 onClick={onClick}
240 onContextMenu={onContextMenu}
241 >
242 <div className="flex w-full min-w-0 items-center justify-between gap-2">
243 <MiddleTruncate value={itemValue} className="flex-1" />
244 {badge && <span className="shrink-0 text-3xs text-muted-foreground/45">{badge}</span>}
245 </div>
246 </ComboboxItem>
247 );
248 }