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

main
tsx 299 lines 10.4 KB
Raw
1 import type { PullRequestInvolvement, PullRequestListState } from "@t3tools/contracts";
2 import { T, useGT, useMessages } from "gt-react";
3 import { Plug2Icon, SearchIcon } from "lucide-react";
4 import { useEffect, useRef, useState, type ReactNode, type RefObject } from "react";
5
6 import { isElectron } from "~/env";
7 import { cn } from "~/lib/utils";
8 import { Button } from "../ui/button";
9 import { RefreshIcon } from "../ui/refresh-icon";
10 import {
11 WorkspaceBreadcrumb,
12 WorkspaceBreadcrumbItem,
13 WorkspaceBreadcrumbSeparator,
14 } from "../WorkspaceBreadcrumb";
15 import { WorkspacePageContainer } from "../WorkspacePageContainer";
16 import { WorkspacePageHeader } from "../WorkspacePageHeader";
17 import { workspaceAreaProps } from "../../lib/workspaceFocus";
18 import { PullRequestCompactFilterMenu } from "./PullRequestCompactFilterMenu";
19 import type { PullRequestFilterOption } from "./PullRequestListFilters";
20 import { INVOLVEMENT_TABS, resolveFilterOptions, STATE_TABS } from "./pullRequestListOptions";
21
22 function ExpandableSearch({
23 searchInput,
24 searchValue,
25 open,
26 onOpenChange,
27 onFocusWithin,
28 }: {
29 searchInput: ReactNode;
30 searchValue: string;
31 open: boolean;
32 onOpenChange: (open: boolean) => void;
33 /**
34 * An unmount fires no blur, which is the point: whoever unmounted this can still see the reader
35 * was mid-typing and move the focus somewhere that continues the sentence.
36 */
37 onFocusWithin?: (focused: boolean) => void;
38 }) {
39 const gt = useGT();
40 const containerRef = useRef<HTMLDivElement | null>(null);
41 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- focuses the search input DOM node when the topbar search opens
42 useEffect(() => {
43 if (!open) return;
44 containerRef.current?.querySelector("input")?.focus();
45 }, [open]);
46 if (open || searchValue.length > 0) {
47 return (
48 <div
49 ref={containerRef}
50 className="w-56 min-w-24 shrink"
51 onFocus={() => onFocusWithin?.(true)}
52 onBlur={() => {
53 onFocusWithin?.(false);
54 if (searchValue.length === 0) onOpenChange(false);
55 }}
56 >
57 {searchInput}
58 </div>
59 );
60 }
61 return (
62 <Button
63 size="icon-sm"
64 variant="ghost"
65 aria-label={gt("Search pull requests")}
66 onClick={() => onOpenChange(true)}
67 >
68 <SearchIcon className="size-4" />
69 </Button>
70 );
71 }
72
73 export function PullRequestsColumn({
74 refreshing,
75 onRefresh,
76 searchValue,
77 involvement,
78 state,
79 host,
80 hostMenuOptions,
81 onInvolvement,
82 onState,
83 onHost,
84 searchInput,
85 sortMenu,
86 filtersMenu,
87 savedViewsMenu,
88 rightPanelControl,
89 titlebarControls,
90 rightPanelOpen,
91 maximizedAway,
92 listBody,
93 scrollRef,
94 }: {
95 refreshing: boolean;
96 onRefresh: () => void;
97 searchValue: string;
98 involvement: PullRequestInvolvement;
99 state: PullRequestListState;
100 host: string | undefined;
101 hostMenuOptions: ReadonlyArray<PullRequestFilterOption<string>>;
102 onInvolvement: (involvement: PullRequestInvolvement) => void;
103 onState: (state: PullRequestListState) => void;
104 onHost: (host: string | undefined) => void;
105 searchInput: ReactNode;
106 sortMenu: ReactNode;
107 filtersMenu: ReactNode;
108 savedViewsMenu: ReactNode;
109 rightPanelControl: ReactNode;
110 titlebarControls: ReactNode;
111 rightPanelOpen: boolean;
112 maximizedAway: boolean;
113 listBody: ReactNode;
114 scrollRef: RefObject<HTMLDivElement | null>;
115 }) {
116 const gt = useGT();
117 const resolve = useMessages();
118 const contentArea = workspaceAreaProps("pull-requests", 1, gt("Pull requests"));
119 const markerRef = useRef<HTMLDivElement | null>(null);
120 const [condensed, setCondensed] = useState(false);
121 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- IntersectionObserver on the scroll marker condenses the topbar
122 useEffect(() => {
123 const marker = markerRef.current;
124 if (!marker) return;
125 const observer = new IntersectionObserver(
126 ([entry]) => setCondensed(entry ? !entry.isIntersecting : false),
127 { root: scrollRef.current },
128 );
129 observer.observe(marker);
130 return () => observer.disconnect();
131 }, [scrollRef]);
132 // Typing into the topbar search narrows the list, and a short enough list un-scrolls the
133 // page — which dissolves the condensed topbar and unmounts the very input being typed in.
134 const topbarSearchFocusedRef = useRef(false);
135 const inFlowSearchRef = useRef<HTMLDivElement | null>(null);
136 const [searchOpen, setSearchOpen] = useState(false);
137 const topbarSearchRef = useRef<HTMLDivElement | null>(null);
138 const searchExpanded = searchOpen || searchValue.length > 0;
139 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- window keydown listener focuses the visible search input on Cmd/Ctrl+F
140 useEffect(() => {
141 const onKeyDown = (event: KeyboardEvent) => {
142 if (event.defaultPrevented) return;
143 if (event.key.toLowerCase() !== "f" || !(event.metaKey || event.ctrlKey)) return;
144 if (event.altKey || event.shiftKey) return;
145 event.preventDefault();
146 // A closed topbar search has no input yet; opening it mounts one that focuses itself.
147 if (condensed) setSearchOpen(true);
148 const input = (condensed ? topbarSearchRef : inFlowSearchRef).current?.querySelector("input");
149 input?.focus();
150 input?.select();
151 };
152 window.addEventListener("keydown", onKeyDown);
153 return () => window.removeEventListener("keydown", onKeyDown);
154 }, [condensed]);
155 if (!condensed && searchOpen) setSearchOpen(false);
156 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- moves DOM focus to the in-flow search when the topbar search it was typed in unmounts
157 useEffect(() => {
158 if (condensed) return;
159 if (!topbarSearchFocusedRef.current) return;
160 topbarSearchFocusedRef.current = false;
161 const input = inFlowSearchRef.current?.querySelector("input");
162 if (!input) return;
163 input.focus();
164 input.setSelectionRange(input.value.length, input.value.length);
165 }, [condensed]);
166
167 return (
168 <div
169 {...contentArea}
170 inert={maximizedAway}
171 className={cn(
172 "@container/pr-list flex min-h-0 min-w-0 flex-col overflow-x-hidden bg-background",
173 maximizedAway ? "w-0 flex-none" : "flex-1",
174 )}
175 >
176 <WorkspacePageHeader
177 electron={isElectron}
178 reserveNativeControls={!rightPanelOpen}
179 className="relative bg-background"
180 >
181 {titlebarControls}
182 {condensed ? (
183 <WorkspaceBreadcrumb ariaLabel={gt("Pull request scope")} className="overflow-hidden">
184 <WorkspaceBreadcrumbItem current className={cn(searchExpanded && "sr-only")}>
185 <h1 className="truncate">
186 <T>Pull requests</T>
187 </h1>
188 </WorkspaceBreadcrumbItem>
189 {searchExpanded ? null : <WorkspaceBreadcrumbSeparator />}
190 <WorkspaceBreadcrumbItem className="shrink gap-1.5">
191 <PullRequestCompactFilterMenu
192 label={gt("Filter by state")}
193 value={state}
194 options={resolveFilterOptions(STATE_TABS, resolve)}
195 onChange={onState}
196 className="shrink-0"
197 />
198 <PullRequestCompactFilterMenu
199 label={gt("Filter by involvement")}
200 value={involvement}
201 options={resolveFilterOptions(INVOLVEMENT_TABS, resolve)}
202 onChange={onInvolvement}
203 />
204 {hostMenuOptions.length > 2 ? (
205 <PullRequestCompactFilterMenu
206 label={gt("Filter by host")}
207 value={host ?? ""}
208 options={hostMenuOptions}
209 onChange={(next) => onHost(next === "" ? undefined : next)}
210 />
211 ) : null}
212 </WorkspaceBreadcrumbItem>
213 </WorkspaceBreadcrumb>
214 ) : (
215 <WorkspaceBreadcrumb ariaLabel={gt("Pull requests breadcrumb")}>
216 <WorkspaceBreadcrumbItem current>
217 <h1 className="truncate">
218 <T>Pull requests</T>
219 </h1>
220 </WorkspaceBreadcrumbItem>
221 </WorkspaceBreadcrumb>
222 )}
223 <div className="min-w-0 flex-1" />
224 {condensed ? (
225 <div ref={topbarSearchRef} className="flex shrink items-center gap-1.5">
226 <ExpandableSearch
227 searchInput={searchInput}
228 searchValue={searchValue}
229 open={searchOpen}
230 onOpenChange={setSearchOpen}
231 onFocusWithin={(focused) => {
232 topbarSearchFocusedRef.current = focused;
233 }}
234 />
235 <PullRequestRefreshControl compact refreshing={refreshing} onRefresh={onRefresh} />
236 </div>
237 ) : null}
238 {rightPanelControl}
239 </WorkspacePageHeader>
240
241 <div
242 ref={scrollRef}
243 className="topbar-scroll-fade scrollbar-gutter-both min-h-0 flex-1 overflow-y-auto"
244 >
245 <WorkspacePageContainer width="expanded" className="min-h-full gap-4">
246 <div className="flex flex-col gap-3">
247 <div ref={inFlowSearchRef} className="flex flex-wrap items-center gap-2">
248 <div className="min-w-0 basis-full @lg/pr-list:basis-0 @lg/pr-list:flex-1">
249 {searchInput}
250 </div>
251 {sortMenu}
252 {filtersMenu}
253 {savedViewsMenu}
254 <PullRequestCompactFilterMenu
255 label={gt("Filter by provider")}
256 outlined
257 iconOnly={host !== undefined}
258 triggerIcon={<Plug2Icon aria-hidden className="size-4" />}
259 triggerLabel={gt("All", { $context: "all providers" })}
260 value={host ?? ""}
261 options={hostMenuOptions}
262 onChange={(next) => onHost(next === "" ? undefined : next)}
263 />
264 {!condensed ? (
265 <PullRequestRefreshControl refreshing={refreshing} onRefresh={onRefresh} />
266 ) : null}
267 </div>
268 <div ref={markerRef} aria-hidden className="-mt-3 h-px w-full" />
269 </div>
270
271 {listBody}
272 </WorkspacePageContainer>
273 </div>
274 </div>
275 );
276 }
277
278 function PullRequestRefreshControl({
279 compact = false,
280 refreshing,
281 onRefresh,
282 }: {
283 compact?: boolean;
284 refreshing: boolean;
285 onRefresh: () => void;
286 }) {
287 const gt = useGT();
288 return (
289 <Button
290 size={compact ? "icon-sm" : "icon"}
291 variant={compact ? "ghost" : "outline"}
292 aria-label={gt("Refresh pull requests")}
293 onClick={onRefresh}
294 disabled={refreshing}
295 >
296 <RefreshIcon size="md" refreshing={refreshing} />
297 </Button>
298 );
299 }