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

PullRequestCandidatePicker.tsx

135 lines3.5 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 { PullRequestPeopleGhost } from "./PullRequestGhosts";
14import { SharedTooltipTrigger } from "../ui/shared-tooltip";
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 onSelect,
36 children,
37}: {
38 icon: ReactNode;
39 label: string;
40 allowed: boolean;
41 disabledReason: string;
42 open: boolean;
43 onOpenChange: (open: boolean) => void;
44 query: string;
45 onQueryChange: (query: string) => void;
46 searchLabel: string;
47 isPending: boolean;
48 error: string | null;
49 candidates: ReadonlyArray<T>;
50 emptyLabel: string;
51 noMatchLabel: string;
52 errorLabel: string;
53 truncated: boolean;
54 truncatedLabel: string;
55 candidateKey: (candidate: T) => string;
56 onSelect: (candidate: T) => void;
57 children: (candidate: T) => ReactNode;
58}) {
59 if (!allowed) {
60 return (
61 <SharedTooltipTrigger
62 render={
63 <Button size="icon-xs" variant="ghost" aria-disabled="true" aria-label={label}>
64 {icon}
65 </Button>
66 }
67 label={disabledReason}
68 side="bottom"
69 />
70 );
71 }
72​
73 const keys = candidates.map(candidateKey);
74​
75 return (
76 <Combobox
77 items={keys}
78 filteredItems={keys}
79 filter={null}
80 autoHighlight
81 value={null}
82 onValueChange={(key) => {
83 const candidate = candidates.find((entry) => candidateKey(entry) === key);
84 if (candidate) onSelect(candidate);
85 }}
86 open={open}
87 onOpenChange={(nextOpen, details) => {
88 // Item-press closure steals search focus and the next keystroke.
89 if (!nextOpen && details.reason === "item-press") {
90 details.cancel();
91 return;
92 }
93 onOpenChange(nextOpen);
94 }}
95 >
96 <ComboboxTrigger
97 render={
98 <Button size="icon-xs" variant="ghost" aria-label={label}>
99 {icon}
100 </Button>
101 }
102 />
103 <ComboboxPopup align="start" side="bottom" className="w-72">
104 <ComboboxSearchInput
105 aria-label={searchLabel}
106 placeholder={searchLabel}
107 value={query}
108 onChange={(event) => onQueryChange(event.target.value)}
109 />
110 <ComboboxList scrollAreaClassName="max-h-72">
111 {isPending ? (
112 <PullRequestPeopleGhost rows={4} />
113 ) : error !== null ? (
114 <p className="p-2 text-xs text-muted-foreground">
115 {errorLabel} {error}
116 </p>
117 ) : candidates.length === 0 ? (
118 <p className="p-2 text-xs text-muted-foreground">
119 {query.length > 0 ? noMatchLabel : emptyLabel}
120 </p>
121 ) : (
122 candidates.map((candidate, index) => (
123 <ComboboxItem key={keys[index]} hideIndicator index={index} value={keys[index]}>
124 {children(candidate)}
125 </ComboboxItem>
126 ))
127 )}
128 {truncated ? (
129 <p className="px-2 py-1.5 text-xs text-muted-foreground">{truncatedLabel}</p>
130 ) : null}
131 </ComboboxList>
132 </ComboboxPopup>
133 </Combobox>
134 );