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

PullRequestsColumn.tsx

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