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