|
| 1 | +import type { PullRequestInvolvement, PullRequestListState } from "@t3tools/contracts"; |
| 2 | +import { Plug2Icon, SearchIcon } from "lucide-react"; |
| 3 | +import { useEffect, useRef, useState, type ReactNode, type RefObject } from "react"; |
| 4 | + |
| 5 | +import { isElectron } from "~/env"; |
| 6 | +import { cn } from "~/lib/utils"; |
| 7 | +import { Button } from "../ui/button"; |
| 8 | +import { RefreshIcon } from "../ui/refresh-icon"; |
| 9 | +import { |
| 10 | + WorkspaceBreadcrumb, |
| 11 | + WorkspaceBreadcrumbItem, |
| 12 | + WorkspaceBreadcrumbSeparator, |
| 13 | +} from "../WorkspaceBreadcrumb"; |
| 14 | +import { WorkspacePageContainer } from "../WorkspacePageContainer"; |
| 15 | +import { WorkspacePageHeader } from "../WorkspacePageHeader"; |
| 16 | +import { PullRequestCompactFilterMenu } from "./PullRequestCompactFilterMenu"; |
| 17 | +import type { PullRequestFilterOption } from "./PullRequestListFilters"; |
| 18 | +import { INVOLVEMENT_TABS, STATE_TABS } from "./pullRequestListOptions"; |
| 19 | + |
| 20 | +function ExpandableSearch({ |
| 21 | + searchInput, |
| 22 | + searchValue, |
| 23 | + open, |
| 24 | + onOpenChange, |
| 25 | + focusToken, |
| 26 | + onFocusWithin, |
| 27 | +}: { |
| 28 | + searchInput: ReactNode; |
| 29 | + searchValue: string; |
| 30 | + open: boolean; |
| 31 | + onOpenChange: (open: boolean) => void; |
| 32 | + focusToken: number; |
| 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 containerRef = useRef<HTMLDivElement | null>(null); |
| 40 | + useEffect(() => { |
| 41 | + if (!open) return; |
| 42 | + containerRef.current?.querySelector("input")?.focus(); |
| 43 | + }, [open]); |
| 44 | + const appliedFocusToken = useRef(focusToken); |
| 45 | + useEffect(() => { |
| 46 | + if (appliedFocusToken.current === focusToken) return; |
| 47 | + appliedFocusToken.current = focusToken; |
| 48 | + const input = containerRef.current?.querySelector("input"); |
| 49 | + input?.focus(); |
| 50 | + input?.select(); |
| 51 | + }, [focusToken]); |
| 52 | + if (open || searchValue.length > 0) { |
| 53 | + return ( |
| 54 | + <div |
| 55 | + ref={containerRef} |
| 56 | + className="w-56 min-w-24 shrink" |
| 57 | + onFocus={() => onFocusWithin?.(true)} |
| 58 | + onBlur={() => { |
| 59 | + onFocusWithin?.(false); |
| 60 | + if (searchValue.length === 0) onOpenChange(false); |
| 61 | + }} |
| 62 | + > |
| 63 | + {searchInput} |
| 64 | + </div> |
| 65 | + ); |
| 66 | + } |
| 67 | + return ( |
| 68 | + <Button |
| 69 | + size="icon-sm" |
| 70 | + variant="ghost" |
| 71 | + aria-label="Search pull requests" |
| 72 | + onClick={() => onOpenChange(true)} |
| 73 | + > |
| 74 | + <SearchIcon className="size-4" /> |
| 75 | + </Button> |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +export function PullRequestsColumn({ |
| 80 | + refreshing, |
| 81 | + onRefresh, |
| 82 | + searchValue, |
| 83 | + involvement, |
| 84 | + state, |
| 85 | + host, |
| 86 | + hostMenuOptions, |
| 87 | + onInvolvement, |
| 88 | + onState, |
| 89 | + onHost, |
| 90 | + searchInput, |
| 91 | + sortMenu, |
| 92 | + filtersMenu, |
| 93 | + rightPanelControl, |
| 94 | + titlebarControls, |
| 95 | + rightPanelOpen, |
| 96 | + maximizedAway, |
| 97 | + listBody, |
| 98 | + scrollRef, |
| 99 | +}: { |
| 100 | + refreshing: boolean; |
| 101 | + onRefresh: () => void; |
| 102 | + searchValue: string; |
| 103 | + involvement: PullRequestInvolvement; |
| 104 | + state: PullRequestListState; |
| 105 | + host: string | undefined; |
| 106 | + hostMenuOptions: ReadonlyArray<PullRequestFilterOption<string>>; |
| 107 | + onInvolvement: (involvement: PullRequestInvolvement) => void; |
| 108 | + onState: (state: PullRequestListState) => void; |
| 109 | + onHost: (host: string | undefined) => void; |
| 110 | + searchInput: ReactNode; |
| 111 | + sortMenu: ReactNode; |
| 112 | + filtersMenu: ReactNode; |
| 113 | + rightPanelControl: ReactNode; |
| 114 | + titlebarControls: ReactNode; |
| 115 | + rightPanelOpen: boolean; |
| 116 | + maximizedAway: boolean; |
| 117 | + listBody: ReactNode; |
| 118 | + scrollRef: RefObject<HTMLDivElement | null>; |
| 119 | +}) { |
| 120 | + const markerRef = useRef<HTMLDivElement | null>(null); |
| 121 | + const [condensed, setCondensed] = useState(false); |
| 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 [searchFocusToken, setSearchFocusToken] = useState(0); |
| 138 | + const searchExpanded = searchOpen || searchValue.length > 0; |
| 139 | + useEffect(() => { |
| 140 | + const onKeyDown = (event: KeyboardEvent) => { |
| 141 | + if (event.defaultPrevented) return; |
| 142 | + if (event.key.toLowerCase() !== "f" || !(event.metaKey || event.ctrlKey)) return; |
| 143 | + if (event.altKey || event.shiftKey) return; |
| 144 | + event.preventDefault(); |
| 145 | + if (condensed) { |
| 146 | + setSearchOpen(true); |
| 147 | + setSearchFocusToken((token) => token + 1); |
| 148 | + return; |
| 149 | + } |
| 150 | + const input = inFlowSearchRef.current?.querySelector("input"); |
| 151 | + input?.focus(); |
| 152 | + input?.select(); |
| 153 | + }; |
| 154 | + window.addEventListener("keydown", onKeyDown); |
| 155 | + return () => window.removeEventListener("keydown", onKeyDown); |
| 156 | + }, [condensed]); |
| 157 | + if (!condensed && searchOpen) setSearchOpen(false); |
| 158 | + useEffect(() => { |
| 159 | + if (condensed) return; |
| 160 | + if (!topbarSearchFocusedRef.current) return; |
| 161 | + topbarSearchFocusedRef.current = false; |
| 162 | + const input = inFlowSearchRef.current?.querySelector("input"); |
| 163 | + if (!input) return; |
| 164 | + input.focus(); |
| 165 | + input.setSelectionRange(input.value.length, input.value.length); |
| 166 | + }, [condensed]); |
| 167 | + |
| 168 | + return ( |
| 169 | + <div |
| 170 | + className={cn( |
| 171 | + "@container/pr-list flex min-h-0 min-w-0 flex-col overflow-x-hidden bg-background", |
| 172 | + maximizedAway ? "w-0 flex-none" : "flex-1", |
| 173 | + )} |
| 174 | + > |
| 175 | + <WorkspacePageHeader |
| 176 | + electron={isElectron} |
| 177 | + reserveNativeControls={!rightPanelOpen} |
| 178 | + className="relative bg-background" |
| 179 | + > |
| 180 | + {titlebarControls} |
| 181 | + {condensed ? ( |
| 182 | + <WorkspaceBreadcrumb ariaLabel="Pull request scope" className="overflow-hidden"> |
| 183 | + <WorkspaceBreadcrumbItem current className={cn(searchExpanded && "sr-only")}> |
| 184 | + <h1 className="truncate">Pull Requests</h1> |
| 185 | + </WorkspaceBreadcrumbItem> |
| 186 | + {searchExpanded ? null : <WorkspaceBreadcrumbSeparator />} |
| 187 | + <WorkspaceBreadcrumbItem className="shrink gap-1.5"> |
| 188 | + <PullRequestCompactFilterMenu |
| 189 | + label="Filter by state" |
| 190 | + value={state} |
| 191 | + options={STATE_TABS} |
| 192 | + onChange={onState} |
| 193 | + className="shrink-0" |
| 194 | + /> |
| 195 | + <PullRequestCompactFilterMenu |
| 196 | + label="Filter by involvement" |
| 197 | + value={involvement} |
| 198 | + options={INVOLVEMENT_TABS} |
| 199 | + onChange={onInvolvement} |
| 200 | + /> |
| 201 | + {hostMenuOptions.length > 2 ? ( |
| 202 | + <PullRequestCompactFilterMenu |
| 203 | + label="Filter by host" |
| 204 | + value={host ?? ""} |
| 205 | + options={hostMenuOptions} |
| 206 | + onChange={(next) => onHost(next === "" ? undefined : next)} |
| 207 | + /> |
| 208 | + ) : null} |
| 209 | + </WorkspaceBreadcrumbItem> |
| 210 | + </WorkspaceBreadcrumb> |
| 211 | + ) : ( |
| 212 | + <WorkspaceBreadcrumb ariaLabel="Pull requests breadcrumb"> |
| 213 | + <WorkspaceBreadcrumbItem current> |
| 214 | + <h1 className="truncate">Pull Requests</h1> |
| 215 | + </WorkspaceBreadcrumbItem> |
| 216 | + </WorkspaceBreadcrumb> |
| 217 | + )} |
| 218 | + <div className="min-w-0 flex-1" /> |
| 219 | + {condensed ? ( |
| 220 | + <div className="flex shrink items-center gap-1.5"> |
| 221 | + <ExpandableSearch |
| 222 | + searchInput={searchInput} |
| 223 | + searchValue={searchValue} |
| 224 | + open={searchOpen} |
| 225 | + onOpenChange={setSearchOpen} |
| 226 | + focusToken={searchFocusToken} |
| 227 | + onFocusWithin={(focused) => { |
| 228 | + topbarSearchFocusedRef.current = focused; |
| 229 | + }} |
| 230 | + /> |
| 231 | + <PullRequestRefreshControl compact refreshing={refreshing} onRefresh={onRefresh} /> |
| 232 | + </div> |
| 233 | + ) : null} |
| 234 | + {rightPanelControl} |
| 235 | + </WorkspacePageHeader> |
| 236 | + |
| 237 | + <div |
| 238 | + ref={scrollRef} |
| 239 | + className="topbar-scroll-fade scrollbar-gutter-both min-h-0 flex-1 overflow-y-auto" |
| 240 | + > |
| 241 | + <WorkspacePageContainer width="expanded" className="min-h-full gap-4"> |
| 242 | + <div className="flex flex-col gap-3"> |
| 243 | + <div ref={inFlowSearchRef} className="flex flex-wrap items-center gap-2"> |
| 244 | + <div className="min-w-0 basis-full @lg/pr-list:basis-0 @lg/pr-list:flex-1"> |
| 245 | + {searchInput} |
| 246 | + </div> |
| 247 | + {sortMenu} |
| 248 | + {filtersMenu} |
| 249 | + <PullRequestCompactFilterMenu |
| 250 | + label="Filter by provider" |
| 251 | + outlined |
| 252 | + iconOnly={host !== undefined} |
| 253 | + triggerIcon={<Plug2Icon aria-hidden className="size-4" />} |
| 254 | + triggerLabel="All" |
| 255 | + value={host ?? ""} |
| 256 | + options={hostMenuOptions} |
| 257 | + onChange={(next) => onHost(next === "" ? undefined : next)} |
| 258 | + /> |
| 259 | + {!condensed ? ( |
| 260 | + <PullRequestRefreshControl refreshing={refreshing} onRefresh={onRefresh} /> |
| 261 | + ) : null} |
| 262 | + </div> |
| 263 | + <div ref={markerRef} aria-hidden className="-mt-3 h-px w-full" /> |
| 264 | + </div> |
| 265 | + |
| 266 | + {listBody} |
| 267 | + </WorkspacePageContainer> |
| 268 | + </div> |
| 269 | + </div> |
| 270 | + ); |
| 271 | +} |
| 272 | + |
| 273 | +function PullRequestRefreshControl({ |
| 274 | + compact = false, |
| 275 | + refreshing, |
| 276 | + onRefresh, |
| 277 | +}: { |
| 278 | + compact?: boolean; |
| 279 | + refreshing: boolean; |
| 280 | + onRefresh: () => void; |
| 281 | +}) { |
| 282 | + return ( |
| 283 | + <Button |
| 284 | + size={compact ? "icon-sm" : "icon"} |
| 285 | + variant={compact ? "ghost" : "outline"} |
| 286 | + aria-label="Refresh pull requests" |
| 287 | + onClick={onRefresh} |
| 288 | + disabled={refreshing} |
| 289 | + > |
| 290 | + <RefreshIcon size="md" refreshing={refreshing} /> |
| 291 | + </Button> |
| 292 | + ); |
| 293 | +} |
0 commit comments