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

PullRequestsColumn.tsx

287 lines10.0 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 { 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 );
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 rightPanelControl,
86 titlebarControls,
87 rightPanelOpen,
88 maximizedAway,
89 listBody,
90 scrollRef,
91}: {
92 refreshing: boolean;
93 onRefresh: () => void;
94 searchValue: string;
95 involvement: PullRequestInvolvement;
96 state: PullRequestListState;
97 host: string | undefined;
98 hostMenuOptions: ReadonlyArray<PullRequestFilterOption<string>>;
99 onInvolvement: (involvement: PullRequestInvolvement) => void;
100 onState: (state: PullRequestListState) => void;
101 onHost: (host: string | undefined) => void;
102 searchInput: ReactNode;
103 sortMenu: ReactNode;
104 filtersMenu: ReactNode;
105 rightPanelControl: ReactNode;
106 titlebarControls: ReactNode;
107 rightPanelOpen: boolean;
108 maximizedAway: boolean;
109 listBody: ReactNode;
110 scrollRef: RefObject<HTMLDivElement | null>;
111}) {
112 const contentArea = workspaceAreaProps("pull-requests", 1, "Pull requests");
113 const markerRef = useRef<HTMLDivElement | null>(null);
114 const [condensed, setCondensed] = useState(false);
115 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- IntersectionObserver on the scroll marker condenses the topbar
116 useEffect(() => {
117 const marker = markerRef.current;
118 if (!marker) return;
119 const observer = new IntersectionObserver(
120 ([entry]) => setCondensed(entry ? !entry.isIntersecting : false),
121 { root: scrollRef.current },
122 );
123 observer.observe(marker);
124 return () => observer.disconnect();
125 }, [scrollRef]);
126 // Typing into the topbar search narrows the list, and a short enough list un-scrolls the
127 // page — which dissolves the condensed topbar and unmounts the very input being typed in.
128 const topbarSearchFocusedRef = useRef(false);
129 const inFlowSearchRef = useRef<HTMLDivElement | null>(null);
130 const [searchOpen, setSearchOpen] = useState(false);
131 const topbarSearchRef = useRef<HTMLDivElement | null>(null);
132 const searchExpanded = searchOpen || searchValue.length > 0;
133 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- window keydown listener focuses the visible search input on Cmd/Ctrl+F
134 useEffect(() => {
135 const onKeyDown = (event: KeyboardEvent) => {
136 if (event.defaultPrevented) return;
137 if (event.key.toLowerCase() !== "f" || !(event.metaKey || event.ctrlKey)) return;
138 if (event.altKey || event.shiftKey) return;
139 event.preventDefault();
140 // A closed topbar search has no input yet; opening it mounts one that focuses itself.
141 if (condensed) setSearchOpen(true);
142 const input = (condensed ? topbarSearchRef : inFlowSearchRef).current?.querySelector("input");
143 input?.focus();
144 input?.select();
145 };
146 window.addEventListener("keydown", onKeyDown);
147 return () => window.removeEventListener("keydown", onKeyDown);
148 }, [condensed]);
149 if (!condensed && searchOpen) setSearchOpen(false);
150 // 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
151 useEffect(() => {
152 if (condensed) return;
153 if (!topbarSearchFocusedRef.current) return;
154 topbarSearchFocusedRef.current = false;
155 const input = inFlowSearchRef.current?.querySelector("input");
156 if (!input) return;
157 input.focus();
158 input.setSelectionRange(input.value.length, input.value.length);
159 }, [condensed]);
160​
161 return (
162 <div
163 {...contentArea}
164 inert={maximizedAway}
165 className={cn(
166 "@container/pr-list flex min-h-0 min-w-0 flex-col overflow-x-hidden bg-background",
167 maximizedAway ? "w-0 flex-none" : "flex-1",
168 )}
169 >
170 <WorkspacePageHeader
171 electron={isElectron}
172 reserveNativeControls={!rightPanelOpen}
173 className="relative bg-background"
174 >
175 {titlebarControls}
176 {condensed ? (
177 <WorkspaceBreadcrumb ariaLabel="Pull request scope" className="overflow-hidden">
178 <WorkspaceBreadcrumbItem current className={cn(searchExpanded && "sr-only")}>
179 <h1 className="truncate">Pull requests</h1>
180 </WorkspaceBreadcrumbItem>
181 {searchExpanded ? null : <WorkspaceBreadcrumbSeparator />}
182 <WorkspaceBreadcrumbItem className="shrink gap-1.5">
183 <PullRequestCompactFilterMenu
184 label="Filter by state"
185 value={state}
186 options={STATE_TABS}
187 onChange={onState}
188 className="shrink-0"
189 />
190 <PullRequestCompactFilterMenu
191 label="Filter by involvement"
192 value={involvement}
193 options={INVOLVEMENT_TABS}
194 onChange={onInvolvement}
195 />
196 {hostMenuOptions.length > 2 ? (
197 <PullRequestCompactFilterMenu
198 label="Filter by host"
199 value={host ?? ""}
200 options={hostMenuOptions}
201 onChange={(next) => onHost(next === "" ? undefined : next)}
202 />
203 ) : null}
204 </WorkspaceBreadcrumbItem>
205 </WorkspaceBreadcrumb>
206 ) : (
207 <WorkspaceBreadcrumb ariaLabel="Pull requests breadcrumb">
208 <WorkspaceBreadcrumbItem current>
209 <h1 className="truncate">Pull requests</h1>
210 </WorkspaceBreadcrumbItem>
211 </WorkspaceBreadcrumb>
212 )}
213 <div className="min-w-0 flex-1" />
214 {condensed ? (
215 <div ref={topbarSearchRef} className="flex shrink items-center gap-1.5">
216 <ExpandableSearch
217 searchInput={searchInput}
218 searchValue={searchValue}
219 open={searchOpen}
220 onOpenChange={setSearchOpen}
221 onFocusWithin={(focused) => {
222 topbarSearchFocusedRef.current = focused;
223 }}
224 />
225 <PullRequestRefreshControl compact refreshing={refreshing} onRefresh={onRefresh} />
226 </div>
227 ) : null}
228 {rightPanelControl}
229 </WorkspacePageHeader>
230​
231 <div
232 ref={scrollRef}
233 className="topbar-scroll-fade scrollbar-gutter-both min-h-0 flex-1 overflow-y-auto"
234 >
235 <WorkspacePageContainer width="expanded" className="min-h-full gap-4">
236 <div className="flex flex-col gap-3">
237 <div ref={inFlowSearchRef} className="flex flex-wrap items-center gap-2">
238 <div className="min-w-0 basis-full @lg/pr-list:basis-0 @lg/pr-list:flex-1">
239 {searchInput}
240 </div>
241 {sortMenu}
242 {filtersMenu}
243 <PullRequestCompactFilterMenu
244 label="Filter by provider"
245 outlined
246 iconOnly={host !== undefined}
247 triggerIcon={<Plug2Icon aria-hidden className="size-4" />}
248 triggerLabel="All"
249 value={host ?? ""}
250 options={hostMenuOptions}
251 onChange={(next) => onHost(next === "" ? undefined : next)}
252 />
253 {!condensed ? (
254 <PullRequestRefreshControl refreshing={refreshing} onRefresh={onRefresh} />
255 ) : null}
256 </div>
257 <div ref={markerRef} aria-hidden className="-mt-3 h-px w-full" />
258 </div>
259​
260 {listBody}
261 </WorkspacePageContainer>
262 </div>
263 </div>
264 );
266​
267function PullRequestRefreshControl({
268 compact = false,
269 refreshing,
270 onRefresh,
271}: {
272 compact?: boolean;
273 refreshing: boolean;
274 onRefresh: () => void;
275}) {
276 return (
277 <Button
278 size={compact ? "icon-sm" : "icon"}
279 variant={compact ? "ghost" : "outline"}
280 aria-label="Refresh pull requests"
281 onClick={onRefresh}
282 disabled={refreshing}
283 >
284 <RefreshIcon size="md" refreshing={refreshing} />
285 </Button>
286 );