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