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

PullRequestCandidatePicker.tsx

149 lines4.0 KB

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