// Menu typeahead consumes search keystrokes; use a combobox.
import type { ReactNode } from "react";
import { Button } from "../ui/button";
import {
Combobox,
ComboboxSearchInput,
ComboboxItem,
ComboboxList,
ComboboxPopup,
ComboboxTrigger,
} from "../ui/combobox";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import { PullRequestPeopleGhost } from "./PullRequestGhosts";
export function PullRequestCandidatePicker<T>({
icon,
label,
allowed,
disabledReason,
open,
onOpenChange,
query,
onQueryChange,
searchLabel,
isPending,
error,
candidates,
emptyLabel,
noMatchLabel,
errorLabel,
truncated,
truncatedLabel,
candidateKey,
disabled,
onSelect,
children,
}: {
icon: ReactNode;
label: string;
allowed: boolean;
disabledReason: string;
open: boolean;
onOpenChange: (open: boolean) => void;
query: string;
onQueryChange: (query: string) => void;
searchLabel: string;
isPending: boolean;
error: string | null;
candidates: ReadonlyArray<T>;
emptyLabel: string;
noMatchLabel: string;
errorLabel: string;
truncated: boolean;
truncatedLabel: string;
candidateKey: (candidate: T) => string;
disabled: boolean;
onSelect: (candidate: T) => void;
children: (candidate: T) => ReactNode;
}) {
if (!allowed) {
return (
<Tooltip>
<TooltipTrigger
render={
<Button size="icon-xs" variant="ghost" aria-disabled="true" aria-label={label}>
{icon}
</Button>
}
/>
<TooltipPopup side="bottom">{disabledReason}</TooltipPopup>
</Tooltip>
);
}
const keys = candidates.map(candidateKey);
return (
<Combobox
items={keys}
filteredItems={keys}
filter={null}
autoHighlight
value={null}
onValueChange={(key) => {
const candidate = candidates.find((entry) => candidateKey(entry) === key);
if (candidate) onSelect(candidate);
}}
open={open}
onOpenChange={(nextOpen, details) => {
// Item-press closure steals search focus and the next keystroke.
if (!nextOpen && details.reason === "item-press") {
details.cancel();
return;
}
onOpenChange(nextOpen);
}}
>
<ComboboxTrigger
render={
<Button size="icon-xs" variant="ghost" aria-label={label}>
{icon}
</Button>
}
/>
<ComboboxPopup align="start" side="bottom" className="w-72">
<ComboboxSearchInput
aria-label={searchLabel}
placeholder={searchLabel}
value={query}
onChange={(event) => onQueryChange(event.target.value)}
/>
<ComboboxList className="max-h-72">
{isPending ? (
<PullRequestPeopleGhost rows={4} />
) : error !== null ? (
<p className="p-2 text-xs text-muted-foreground">
{errorLabel} {error}
</p>
) : candidates.length === 0 ? (
<p className="p-2 text-xs text-muted-foreground">
{query.length > 0 ? noMatchLabel : emptyLabel}
</p>
) : (
candidates.map((candidate, index) => (
<ComboboxItem
key={keys[index]}
hideIndicator
index={index}
value={keys[index]}
disabled={disabled}
>
{children(candidate)}
</ComboboxItem>
))
)}
{truncated ? (
<p className="px-2 py-1.5 text-xs text-muted-foreground">{truncatedLabel}</p>
) : null}
</ComboboxList>
</ComboboxPopup>
</Combobox>
);
}