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

CommandPaletteResults.tsx

282 lines10.0 KB

1import { type ResolvedKeybindingsConfig } from "@t3tools/contracts";
2import { LegendList, type LegendListRef } from "@legendapp/list/react";
3import { ChevronRightIcon } from "lucide-react";
4import { type ReactNode, type RefObject, useState } from "react";
5import { cn } from "~/lib/utils";
6import { textDirection } from "~/textDirection";
7import { shortcutLabelForCommand } from "../keybindings";
8import {
9 type CommandPaletteActionItem,
10 type CommandPaletteGroup,
11 type CommandPaletteRow,
12 type CommandPaletteSubmenuItem,
13} from "./CommandPalette.logic";
14import {
15 CommandCollection,
16 CommandGroup,
17 CommandGroupLabel,
18 CommandItem,
19 CommandList,
20 CommandListHeading,
21 CommandListVirtualized,
22 CommandShortcut,
23} from "./ui/command";
24import { getVirtualizedScrollFadeClassName } from "./ui/scroll-area";
25import { SearchMatchHintLine, ThreadSearchMatchExcerpt } from "./ThreadSearchMatch";
26​
27interface CommandPaletteResultsProps {
28 emptyStateMessage?: string;
29 groups: ReadonlyArray<CommandPaletteGroup>;
30 highlightedItemValue?: string | null;
31 isActionsOnly: boolean;
32 keybindings: ResolvedKeybindingsConfig;
33 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
35​
36function CommandPaletteEmptyState(props: { emptyStateMessage?: string; isActionsOnly: boolean }) {
37 return (
38 <div className="py-10 text-center text-sm text-muted-foreground">
39 {props.emptyStateMessage ??
40 (props.isActionsOnly
41 ? "No matching actions."
42 : "No matching commands, projects, or threads.")}
43 </div>
44 );
46​
47export function CommandPaletteResults(props: CommandPaletteResultsProps) {
48 if (props.groups.length === 0) {
49 return <CommandPaletteEmptyState {...props} />;
50 }
51​
52 return (
53 <CommandList>
54 {props.groups.map((group) => (
55 <CommandGroup items={group.items} key={group.value}>
56 {group.label ? <CommandGroupLabel>{group.label}</CommandGroupLabel> : null}
57 <CommandCollection>
58 {(item) =>
59 item.disabled ? (
60 <DisabledCommandPaletteResultRow item={item} key={item.value} />
61 ) : (
62 <CommandPaletteResultRow
63 item={item}
64 key={item.value}
65 keybindings={props.keybindings}
66 isActive={props.highlightedItemValue === item.value}
67 onExecuteItem={props.onExecuteItem}
68 />
69 )
70 }
71 </CommandCollection>
72 </CommandGroup>
73 ))}
74 </CommandList>
75 );
77​
78export function scrollCommandPaletteRowIntoView(list: LegendListRef | null, rowIndex: number) {
79 const element = list?.getState?.().elementAtIndex(rowIndex);
80 if (element instanceof HTMLElement) {
81 element.scrollIntoView({ block: "nearest" });
82 return;
83 }
84 void list?.scrollIndexIntoView?.({ index: rowIndex, animated: false });
86​
87interface CommandPaletteVirtualizedResultsProps extends Omit<CommandPaletteResultsProps, "groups"> {
88 rows: ReadonlyArray<CommandPaletteRow>;
89 listRef: RefObject<LegendListRef | null>;
91​
92/** Pass flat items with virtualized to Command, scroll highlights via listRef, and handle Enter for
93 * unmounted rows. */
94export function CommandPaletteVirtualizedResults(props: CommandPaletteVirtualizedResultsProps) {
95 const { listRef } = props;
96 const [scrollFade, setScrollFade] = useState({ top: false, bottom: false });
97 const updateScrollFade = () => {
98 const scrollElement = listRef.current?.getScrollableNode?.();
99 if (!(scrollElement instanceof HTMLElement)) return;
100 const top = scrollElement.scrollTop > 1;
101 const bottom =
102 scrollElement.scrollHeight - scrollElement.clientHeight - scrollElement.scrollTop > 1;
103 setScrollFade((current) =>
104 current.top === top && current.bottom === bottom ? current : { top, bottom },
105 );
106 };
107​
108 if (props.rows.length === 0) {
109 return <CommandPaletteEmptyState {...props} />;
110 }
111​
112 return (
113 <CommandListVirtualized>
114 <LegendList<CommandPaletteRow>
115 ref={listRef}
116 data={props.rows}
117 keyExtractor={(row) => row.key}
118 getItemType={(row) => row.kind}
119 extraData={props.highlightedItemValue}
120 renderItem={({ item: row }) =>
121 row.kind === "label" ? (
122 <div className={row.first ? undefined : "pt-1.5"}>
123 <CommandListHeading>{row.label}</CommandListHeading>
124 </div>
125 ) : row.itemIndex === null ? (
126 <DisabledCommandPaletteResultRow item={row.item} />
127 ) : (
128 <CommandPaletteResultRow
129 index={row.itemIndex}
130 item={row.item}
131 keybindings={props.keybindings}
132 isActive={props.highlightedItemValue === row.item.value}
133 onExecuteItem={props.onExecuteItem}
134 />
135 )
136 }
137 estimatedItemSize={40}
138 drawDistance={400}
139 onLayout={updateScrollFade}
140 onScroll={updateScrollFade}
141 contentContainerClassName="px-2"
142 className={cn(
143 "min-h-0 scroll-py-6 overflow-x-hidden overscroll-y-contain py-2 [&::-webkit-scrollbar-track]:my-2",
144 getVirtualizedScrollFadeClassName(scrollFade),
145 )}
146 />
147 </CommandListVirtualized>
148 );
150​
151export function CommandPaletteTitleIcon(props: { children: ReactNode }) {
152 return <span className="flex h-5 shrink-0 items-center self-start pt-0.5">{props.children}</span>;
154​
155function CommandPaletteTitle({
156 item,
157}: {
158 item: Pick<CommandPaletteActionItem | CommandPaletteSubmenuItem, "title">;
159}) {
160 const { title } = item;
161 return (
162 <span className="truncate" dir={typeof title === "string" ? textDirection(title) : undefined}>
163 {title}
164 </span>
165 );
167​
168/**
169 * Project and environment for a thread row whose second line shows a message
170 * snippet or match hint instead, so similar rows can still be told apart.
171 */
172function ThreadRowContext({
173 item,
174}: {
175 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
176}) {
177 if (!item.threadSearch || !(item.threadContentMatch || item.matchHint)) return null;
178 // A "Project: …" hint already names the project.
179 const context = [item.threadSearch.projectTitle, item.threadSearch.environmentLabel]
180 .filter((part) => part && part !== item.matchHint?.text)
181 .join(" · ");
182 return context ? (
183 <span className="max-w-[45%] shrink-0 truncate text-muted-foreground/70 text-xs">
184 {context}
185 </span>
186 ) : null;
188​
189function DisabledCommandPaletteResultRow(props: {
190 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
191}) {
192 return (
193 <div className="flex min-h-8 select-none items-center gap-2 rounded-sm px-2 py-1.5 text-base opacity-64 sm:min-h-7 sm:text-sm">
194 <CommandPaletteTitleIcon>{props.item.icon}</CommandPaletteTitleIcon>
195 {props.item.description || props.item.threadContentMatch ? (
196 <span className="flex min-w-0 flex-1 flex-col">
197 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
198 {props.item.titleLeadingContent}
199 <CommandPaletteTitle item={props.item} />
200 <ThreadRowContext item={props.item} />
201 </span>
202 {props.item.threadContentMatch ? (
203 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
204 ) : props.item.matchHint ? (
205 <SearchMatchHintLine hint={props.item.matchHint} />
206 ) : props.item.description ? (
207 <span className="min-w-0 text-muted-foreground/70 text-xs">
208 {props.item.description}
209 </span>
210 ) : null}
211 </span>
212 ) : (
213 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
214 {props.item.titleLeadingContent}
215 <CommandPaletteTitle item={props.item} />
216 </span>
217 )}
218 {props.item.titleTrailingContent}
219 </div>
220 );
222​
223function CommandPaletteResultRow(props: {
224 index?: number;
225 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
226 isActive: boolean;
227 keybindings: ResolvedKeybindingsConfig;
228 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
229}) {
230 const shortcutLabel = props.item.shortcutCommand
231 ? shortcutLabelForCommand(props.keybindings, props.item.shortcutCommand)
232 : null;
233​
234 return (
235 <CommandItem
236 {...(props.index === undefined ? {} : { index: props.index })}
237 value={props.item.value}
238 active={props.isActive}
239 onMouseDown={(event) => {
240 event.preventDefault();
241 }}
242 onClick={() => {
243 props.onExecuteItem(props.item);
244 }}
245 >
246 <CommandPaletteTitleIcon>{props.item.icon}</CommandPaletteTitleIcon>
247 {props.item.description || props.item.threadContentMatch ? (
248 <span className="flex min-w-0 flex-1 flex-col">
249 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
250 {props.item.titleLeadingContent}
251 <CommandPaletteTitle item={props.item} />
252 <ThreadRowContext item={props.item} />
253 </span>
254 {props.item.threadContentMatch ? (
255 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
256 ) : props.item.matchHint ? (
257 <SearchMatchHintLine hint={props.item.matchHint} />
258 ) : props.item.description ? (
259 <span className="min-w-0 text-muted-foreground/70 text-xs">
260 {props.item.description}
261 </span>
262 ) : null}
263 </span>
264 ) : (
265 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
266 {props.item.titleLeadingContent}
267 <CommandPaletteTitle item={props.item} />
268 </span>
269 )}
270 {props.item.titleTrailingContent}
271 {props.item.timestamp ? (
272 <span className="min-w-12 shrink-0 text-right text-xs tabular-nums text-muted-foreground/70">
273 {props.item.timestamp}
274 </span>
275 ) : null}
276 {shortcutLabel ? <CommandShortcut>{shortcutLabel}</CommandShortcut> : null}
277 {props.item.kind === "submenu" ? (
278 <ChevronRightIcon className="-me-0.5 ms-auto size-4 shrink-0 text-muted-foreground/70" />
279 ) : null}
280 </CommandItem>
281 );