Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PullRequestsColumn.tsx

283 lines9.8 KB

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