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

PullRequestCandidatePicker.tsx

144 lines3.7 KB

1// Menu typeahead consumes search keystrokes; use a combobox.
2import type { ReactNode } from "react";
3​
4import { Button } from "../ui/button";
5import {
6 Combobox,
7 ComboboxSearchInput,
8 ComboboxItem,
9 ComboboxList,
10 ComboboxPopup,
11 ComboboxTrigger,
12} from "../ui/combobox";
13import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
14import { PullRequestPeopleGhost } from "./PullRequestGhosts";
15​
16export function PullRequestCandidatePicker<T>({
17 icon,
18 label,
19 allowed,
20 disabledReason,
21 open,
22 onOpenChange,
23 query,
24 onQueryChange,
25 searchLabel,
26 isPending,
27 error,
28 candidates,
29 emptyLabel,
30 noMatchLabel,
31 errorLabel,
32 truncated,
33 truncatedLabel,
34 candidateKey,
35 disabled,
36 onSelect,
37 children,
38}: {
39 icon: ReactNode;
40 label: string;
41 allowed: boolean;
42 disabledReason: string;
43 open: boolean;
44 onOpenChange: (open: boolean) => void;
45 query: string;
46 onQueryChange: (query: string) => void;
47 searchLabel: string;
48 isPending: boolean;
49 error: string | null;
50 candidates: ReadonlyArray<T>;
51 emptyLabel: string;
52 noMatchLabel: string;
53 errorLabel: string;
54 truncated: boolean;
55 truncatedLabel: string;
56 candidateKey: (candidate: T) => string;
57 disabled: boolean;
58 onSelect: (candidate: T) => void;
59 children: (candidate: T) => ReactNode;
60}) {
61 if (!allowed) {
62 return (
63 <Tooltip>
64 <TooltipTrigger
65 render={
66 <Button size="icon-xs" variant="ghost" aria-disabled="true" aria-label={label}>
67 {icon}
68 </Button>
69 }
70 />
71 <TooltipPopup side="bottom">{disabledReason}</TooltipPopup>
72 </Tooltip>
73 );
74 }
75​
76 const keys = candidates.map(candidateKey);
77​
78 return (
79 <Combobox
80 items={keys}
81 filteredItems={keys}
82 filter={null}
83 autoHighlight
84 value={null}
85 onValueChange={(key) => {
86 const candidate = candidates.find((entry) => candidateKey(entry) === key);
87 if (candidate) onSelect(candidate);
88 }}
89 open={open}
90 onOpenChange={(nextOpen, details) => {
91 // Item-press closure steals search focus and the next keystroke.
92 if (!nextOpen && details.reason === "item-press") {
93 details.cancel();
94 return;
95 }
96 onOpenChange(nextOpen);
97 }}
98 >
99 <ComboboxTrigger
100 render={
101 <Button size="icon-xs" variant="ghost" aria-label={label}>
102 {icon}
103 </Button>
104 }
105 />
106 <ComboboxPopup align="start" side="bottom" className="w-72">
107 <ComboboxSearchInput
108 aria-label={searchLabel}
109 placeholder={searchLabel}
110 value={query}
111 onChange={(event) => onQueryChange(event.target.value)}
112 />
113 <ComboboxList className="max-h-72">
114 {isPending ? (
115 <PullRequestPeopleGhost rows={4} />
116 ) : error !== null ? (
117 <p className="p-2 text-xs text-muted-foreground">
118 {errorLabel} {error}
119 </p>
120 ) : candidates.length === 0 ? (
121 <p className="p-2 text-xs text-muted-foreground">
122 {query.length > 0 ? noMatchLabel : emptyLabel}
123 </p>
124 ) : (
125 candidates.map((candidate, index) => (
126 <ComboboxItem
127 key={keys[index]}
128 hideIndicator
129 index={index}
130 value={keys[index]}
131 disabled={disabled}
132 >
133 {children(candidate)}
134 </ComboboxItem>
135 ))
136 )}
137 {truncated ? (
138 <p className="px-2 py-1.5 text-xs text-muted-foreground">{truncatedLabel}</p>
139 ) : null}
140 </ComboboxList>
141 </ComboboxPopup>
142 </Combobox>
143 );