@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 136 lines 3.52 KB
Raw
1 // Menu typeahead consumes search keystrokes; use a combobox.
2 import type { ReactNode } from "react";
3
4 import { Button } from "../ui/button";
5 import {
6 Combobox,
7 ComboboxSearchInput,
8 ComboboxItem,
9 ComboboxList,
10 ComboboxPopup,
11 ComboboxTrigger,
12 } from "../ui/combobox";
13 import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
14 import { PullRequestPeopleGhost } from "./PullRequestGhosts";
15
16 export 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 <Tooltip>
62 <TooltipTrigger
63 render={
64 <Button size="icon-xs" variant="ghost" aria-disabled="true" aria-label={label}>
65 {icon}
66 </Button>
67 }
68 />
69 <TooltipPopup side="bottom">{disabledReason}</TooltipPopup>
70 </Tooltip>
71 );
72 }
73
74 const keys = candidates.map(candidateKey);
75
76 return (
77 <Combobox
78 items={keys}
79 filteredItems={keys}
80 filter={null}
81 autoHighlight
82 value={null}
83 onValueChange={(key) => {
84 const candidate = candidates.find((entry) => candidateKey(entry) === key);
85 if (candidate) onSelect(candidate);
86 }}
87 open={open}
88 onOpenChange={(nextOpen, details) => {
89 // Item-press closure steals search focus and the next keystroke.
90 if (!nextOpen && details.reason === "item-press") {
91 details.cancel();
92 return;
93 }
94 onOpenChange(nextOpen);
95 }}
96 >
97 <ComboboxTrigger
98 render={
99 <Button size="icon-xs" variant="ghost" aria-label={label}>
100 {icon}
101 </Button>
102 }
103 />
104 <ComboboxPopup align="start" side="bottom" className="w-72">
105 <ComboboxSearchInput
106 aria-label={searchLabel}
107 placeholder={searchLabel}
108 value={query}
109 onChange={(event) => onQueryChange(event.target.value)}
110 />
111 <ComboboxList className="max-h-72">
112 {isPending ? (
113 <PullRequestPeopleGhost rows={4} />
114 ) : error !== null ? (
115 <p className="p-2 text-xs text-muted-foreground">
116 {errorLabel} {error}
117 </p>
118 ) : candidates.length === 0 ? (
119 <p className="p-2 text-xs text-muted-foreground">
120 {query.length > 0 ? noMatchLabel : emptyLabel}
121 </p>
122 ) : (
123 candidates.map((candidate, index) => (
124 <ComboboxItem key={keys[index]} hideIndicator index={index} value={keys[index]}>
125 {children(candidate)}
126 </ComboboxItem>
127 ))
128 )}
129 {truncated ? (
130 <p className="px-2 py-1.5 text-xs text-muted-foreground">{truncatedLabel}</p>
131 ) : null}
132 </ComboboxList>
133 </ComboboxPopup>
134 </Combobox>
135 );
136 }