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

PullRequestCandidatePicker.tsx

136 lines4.4 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 */
6import type { ReactNode } from "react";
7​
8import { Button } from "../ui/button";
9import { Input } from "../ui/input";
10import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu";
11import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
12import { PullRequestPeopleGhost } from "./PullRequestGhosts";
13​
14export function PullRequestCandidatePicker<T>({
15 icon,
16 label,
17 allowed,
18 disabledReason,
19 open,
20 onOpenChange,
21 query,
22 onQueryChange,
23 searchLabel,
24 isPending,
25 error,
26 candidates,
27 emptyLabel,
28 noMatchLabel,
29 errorLabel,
30 truncated,
31 truncatedLabel,
32 candidateKey,
33 disabled,
34 onSelect,
35 children,
36}: {
37 icon: ReactNode;
38 /** The trigger's accessible name; the button carries an icon alone. */
39 label: string;
40 /** False where the host would refuse this account's change. Disabled with the reason rather
41 * than hidden: a control that vanishes teaches nobody why. */
42 allowed: boolean;
43 disabledReason: string;
44 open: boolean;
45 onOpenChange: (open: boolean) => void;
46 query: string;
47 onQueryChange: (query: string) => void;
48 searchLabel: string;
49 isPending: boolean;
50 error: string | null;
51 /** Already narrowed by the query; the shell only decides which state to show. */
52 candidates: ReadonlyArray<T>;
53 emptyLabel: string;
54 noMatchLabel: string;
55 /** Leads the host's own message, which follows it in the same sentence. */
56 errorLabel: string;
57 /** The host has more than the read asked for, so a name missing here may still be askable. */
58 truncated: boolean;
59 truncatedLabel: string;
60 candidateKey: (candidate: T) => string;
61 /** Every row locks while one change is in flight, so a second press cannot race the first. */
62 disabled: boolean;
63 onSelect: (candidate: T) => void;
64 children: (candidate: T) => ReactNode;
65}) {
66 if (!allowed) {
67 return (
68 <Tooltip>
69 <TooltipTrigger
70 render={
71 <Button size="icon-xs" variant="ghost" disabled aria-label={label}>
72 {icon}
73 </Button>
74 }
75 />
76 <TooltipPopup side="bottom">{disabledReason}</TooltipPopup>
77 </Tooltip>
78 );
79 }
80​
81 return (
82 <Menu open={open} onOpenChange={onOpenChange}>
83 <MenuTrigger
84 render={
85 <Button size="icon-xs" variant="ghost" aria-label={label}>
86 {icon}
87 </Button>
88 }
89 />
90 <MenuPopup align="start" side="bottom" className="w-72 p-0">
91 <div className="border-b border-border/60 p-2">
92 <Input
93 autoFocus
94 value={query}
95 onChange={(event) => onQueryChange(event.currentTarget.value)}
96 placeholder={searchLabel}
97 aria-label={searchLabel}
98 size="compact"
99 />
100 </div>
101 <div className="max-h-72 overflow-y-auto p-1">
102 {isPending ? (
103 <PullRequestPeopleGhost rows={4} />
104 ) : error !== null ? (
105 <p className="p-2 text-xs text-muted-foreground">
106 {errorLabel} {error}
107 </p>
108 ) : candidates.length === 0 ? (
109 <p className="p-2 text-xs text-muted-foreground">
110 {query.length > 0 ? noMatchLabel : emptyLabel}
111 </p>
112 ) : (
113 candidates.map((candidate) => (
114 // Stays open on press: a change is confirmed by the row's own check turning over,
115 // and a second label or reviewer is usually wanted right after the first.
116 <MenuItem
117 key={candidateKey(candidate)}
118 closeOnClick={false}
119 disabled={disabled}
120 onClick={() => onSelect(candidate)}
121 className="min-h-0 py-1.5 text-xs sm:min-h-0 sm:text-xs"
122 >
123 {children(candidate)}
124 </MenuItem>
125 ))
126 )}
127 {truncated ? (
128 // Typing filters what arrived; it does not ask the host again, so this says what the
129 // list is rather than offering a search that would find nothing further.
130 <p className="px-2 py-1.5 text-xs text-muted-foreground">{truncatedLabel}</p>
131 ) : null}
132 </div>
133 </MenuPopup>
134 </Menu>
135 );