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

PullRequestCandidatePicker.tsx

180 lines6.0 KB

1/**
2 * The menu shell the reviewer and label pickers share: an icon trigger, a search box, and a
3 * scrolling body that says when the list is loading, could not be read, is empty, or is not all
4 * of it. The rows and the words are the caller's; the frame is the same either way.
5 *
6 * The same combobox as the project and branch pickers, and dressed the same, rather than a menu:
7 * a menu's typeahead claims every keypress to jump between rows, which a search box cannot share.
8 */
9import { SearchIcon } from "lucide-react";
10import type { ReactNode } from "react";
11​
12import { Button } from "../ui/button";
13import {
14 Combobox,
15 ComboboxInput,
16 ComboboxItem,
17 ComboboxList,
18 ComboboxPopup,
19 ComboboxTrigger,
20} from "../ui/combobox";
21import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
22import { PullRequestPeopleGhost } from "./PullRequestGhosts";
23​
24export function PullRequestCandidatePicker<T>({
25 icon,
26 label,
27 allowed,
28 disabledReason,
29 open,
30 onOpenChange,
31 query,
32 onQueryChange,
33 searchLabel,
34 isPending,
35 error,
36 candidates,
37 emptyLabel,
38 noMatchLabel,
39 errorLabel,
40 truncated,
41 truncatedLabel,
42 candidateKey,
43 disabled,
44 onSelect,
45 children,
46}: {
47 icon: ReactNode;
48 /** The trigger's accessible name; the button carries an icon alone. */
49 label: string;
50 /** False where the host would refuse this account's change. Disabled with the reason rather
51 * than hidden: a control that vanishes teaches nobody why. */
52 allowed: boolean;
53 disabledReason: string;
54 open: boolean;
55 onOpenChange: (open: boolean) => void;
56 query: string;
57 onQueryChange: (query: string) => void;
58 searchLabel: string;
59 isPending: boolean;
60 error: string | null;
61 /** Already narrowed by the query; the shell only decides which state to show. */
62 candidates: ReadonlyArray<T>;
63 emptyLabel: string;
64 noMatchLabel: string;
65 /** Leads the host's own message, which follows it in the same sentence. */
66 errorLabel: string;
67 /** The host has more than the read asked for, so a name missing here may still be askable. */
68 truncated: boolean;
69 truncatedLabel: string;
70 candidateKey: (candidate: T) => string;
71 /** Every row locks while one change is in flight, so a second press cannot race the first. */
72 disabled: boolean;
73 onSelect: (candidate: T) => void;
74 children: (candidate: T) => ReactNode;
75}) {
76 if (!allowed) {
77 return (
78 <Tooltip>
79 <TooltipTrigger
80 render={
81 <Button size="icon-xs" variant="ghost" disabled aria-label={label}>
82 {icon}
83 </Button>
84 }
85 />
86 <TooltipPopup side="bottom">{disabledReason}</TooltipPopup>
87 </Tooltip>
88 );
89 }
90​
91 const keys = candidates.map(candidateKey);
92​
93 return (
94 <Combobox
95 items={keys}
96 // The caller narrows the list, so the combobox does no filtering of its own.
97 filteredItems={keys}
98 filter={null}
99 autoHighlight
100 value={null}
101 onValueChange={(key) => {
102 const candidate = candidates.find((entry) => candidateKey(entry) === key);
103 if (candidate) onSelect(candidate);
104 }}
105 open={open}
106 onOpenChange={(nextOpen, details) => {
107 // Stays open on a pick: a change is confirmed by the row's own check turning over, and a
108 // second label or reviewer is usually wanted right after the first. Cancelled rather than
109 // ignored, so the combobox also skips its own close work: freezing the query and returning
110 // focus to the trigger, either of which would take the next keystroke away from the box.
111 if (!nextOpen && details.reason === "item-press") {
112 details.cancel();
113 return;
114 }
115 onOpenChange(nextOpen);
116 }}
117 >
118 <ComboboxTrigger
119 render={
120 <Button size="icon-xs" variant="ghost" aria-label={label}>
121 {icon}
122 </Button>
123 }
124 />
125 <ComboboxPopup align="start" side="bottom" className="w-72">
126 <div className="shrink-0 px-3 pt-2.5">
127 <div className="relative -translate-y-px border-b border-border/70 pb-1.5 transition-colors focus-within:border-ring">
128 <SearchIcon
129 aria-hidden="true"
130 className="pointer-events-none absolute top-1.5 left-0 size-4 shrink-0 text-muted-foreground/55"
131 />
132 <ComboboxInput
133 aria-label={searchLabel}
134 className="[&_input]:h-6.5 [&_input]:ps-5 [&_input]:font-sans [&_input]:leading-6.5"
135 inputClassName="rounded-none bg-transparent text-sm"
136 placeholder={searchLabel}
137 showTrigger={false}
138 size="sm"
139 unstyled
140 value={query}
141 onChange={(event) => onQueryChange(event.target.value)}
142 />
143 </div>
144 </div>
145 <ComboboxList className="max-h-72">
146 {isPending ? (
147 <PullRequestPeopleGhost rows={4} />
148 ) : error !== null ? (
149 <p className="p-2 text-xs text-muted-foreground">
150 {errorLabel} {error}
151 </p>
152 ) : candidates.length === 0 ? (
153 <p className="p-2 text-xs text-muted-foreground">
154 {query.length > 0 ? noMatchLabel : emptyLabel}
155 </p>
156 ) : (
157 candidates.map((candidate, index) => (
158 <ComboboxItem
159 key={keys[index]}
160 hideIndicator
161 index={index}
162 value={keys[index]}
163 disabled={disabled}
164 className="min-h-0 py-1.5 text-xs sm:min-h-0 sm:text-xs"
165 contentClassName="flex min-w-0 items-center gap-2"
166 >
167 {children(candidate)}
168 </ComboboxItem>
169 ))
170 )}
171 {truncated ? (
172 // Typing filters what arrived; it does not ask the host again, so this says what the
173 // list is rather than offering a search that would find nothing further.
174 <p className="px-2 py-1.5 text-xs text-muted-foreground">{truncatedLabel}</p>
175 ) : null}
176 </ComboboxList>
177 </ComboboxPopup>
178 </Combobox>
179 );