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 { 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;
34}
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 );
45}
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 );
76}
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 });
85}
86
87interface CommandPaletteVirtualizedResultsProps extends Omit<CommandPaletteResultsProps, "groups"> {
88 rows: ReadonlyArray<CommandPaletteRow>;
89 listRef: RefObject<LegendListRef | null>;
90}
91
92/**
93 * The parent passes the flat item order to the Command root as `items` with
94 * `virtualized`, scrolls keyboard highlights into view through `listRef`, and
95 * runs Enter itself since the row may be unmounted.
96 */
97export function CommandPaletteVirtualizedResults(props: CommandPaletteVirtualizedResultsProps) {
98 const { listRef } = props;
99 const [scrollFade, setScrollFade] = useState({ top: false, bottom: false });
100 const updateScrollFade = () => {
101 const scrollElement = listRef.current?.getScrollableNode?.();
102 if (!(scrollElement instanceof HTMLElement)) return;
103 const top = scrollElement.scrollTop > 1;
104 const bottom =
105 scrollElement.scrollHeight - scrollElement.clientHeight - scrollElement.scrollTop > 1;
106 setScrollFade((current) =>
107 current.top === top && current.bottom === bottom ? current : { top, bottom },
108 );
109 };
110
111 if (props.rows.length === 0) {
112 return <CommandPaletteEmptyState {...props} />;
113 }
114
115 return (
116 <CommandListVirtualized>
117 <LegendList<CommandPaletteRow>
118 ref={listRef}
119 data={props.rows}
120 keyExtractor={(row) => row.key}
121 getItemType={(row) => row.kind}
122 extraData={props.highlightedItemValue}
123 renderItem={({ item: row }) =>
124 row.kind === "label" ? (
125 <div className={row.first ? undefined : "pt-1.5"}>
126 <CommandListHeading>{row.label}</CommandListHeading>
127 </div>
128 ) : row.itemIndex === null ? (
129 <DisabledCommandPaletteResultRow item={row.item} />
130 ) : (
131 <CommandPaletteResultRow
132 index={row.itemIndex}
133 item={row.item}
134 keybindings={props.keybindings}
135 isActive={props.highlightedItemValue === row.item.value}
136 onExecuteItem={props.onExecuteItem}
137 />
138 )
139 }
140 estimatedItemSize={40}
141 drawDistance={400}
142 onLayout={updateScrollFade}
143 onScroll={updateScrollFade}
144 contentContainerClassName="px-2"
145 className={cn(
146 "min-h-0 scroll-py-6 overflow-x-hidden overscroll-y-contain py-2 [&::-webkit-scrollbar-track]:my-2",
147 getVirtualizedScrollFadeClassName(scrollFade),
148 )}
149 />
150 </CommandListVirtualized>
151 );
152}
153
154export function CommandPaletteTitleIcon(props: { children: ReactNode }) {
155 return <span className="flex h-5 shrink-0 items-center self-start pt-0.5">{props.children}</span>;
156}
157
158function CommandPaletteTitle({ title }: { title: ReactNode }) {
159 return (
160 <span className="truncate" dir={typeof title === "string" ? textDirection(title) : undefined}>
161 {title}
162 </span>
163 );
164}
165
166function DisabledCommandPaletteResultRow(props: {
167 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
168}) {
169 return (
170 <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">
171 <CommandPaletteTitleIcon>{props.item.icon}</CommandPaletteTitleIcon>
172 {props.item.description || props.item.threadContentMatch ? (
173 <span className="flex min-w-0 flex-1 flex-col">
174 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
175 {props.item.titleLeadingContent}
176 <CommandPaletteTitle title={props.item.title} />
177 </span>
178 {props.item.threadContentMatch ? (
179 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
180 ) : null}
181 {props.item.description ? (
182 <span className="min-w-0 text-muted-foreground/70 text-xs">
183 {props.item.description}
184 </span>
185 ) : null}
186 </span>
187 ) : (
188 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
189 {props.item.titleLeadingContent}
190 <CommandPaletteTitle title={props.item.title} />
191 </span>
192 )}
193 {props.item.titleTrailingContent}
194 </div>
195 );
196}
197
198function CommandPaletteResultRow(props: {
199 index?: number;
200 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
201 isActive: boolean;
202 keybindings: ResolvedKeybindingsConfig;
203 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
204}) {
205 const shortcutLabel = props.item.shortcutCommand
206 ? shortcutLabelForCommand(props.keybindings, props.item.shortcutCommand)
207 : null;
208
209 return (
210 <CommandItem
211 {...(props.index === undefined ? {} : { index: props.index })}
212 value={props.item.value}
213 active={props.isActive}
214 onMouseDown={(event) => {
215 event.preventDefault();
216 }}
217 onClick={() => {
218 props.onExecuteItem(props.item);
219 }}
220 >
221 <CommandPaletteTitleIcon>{props.item.icon}</CommandPaletteTitleIcon>
222 {props.item.description || props.item.threadContentMatch ? (
223 <span className="flex min-w-0 flex-1 flex-col">
224 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
225 {props.item.titleLeadingContent}
226 <CommandPaletteTitle title={props.item.title} />
227 </span>
228 {props.item.threadContentMatch ? (
229 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
230 ) : null}
231 {props.item.description ? (
232 <span className="min-w-0 text-muted-foreground/70 text-xs">
233 {props.item.description}
234 </span>
235 ) : null}
236 </span>
237 ) : (
238 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
239 {props.item.titleLeadingContent}
240 <CommandPaletteTitle title={props.item.title} />
241 </span>
242 )}
243 {props.item.titleTrailingContent}
244 {props.item.timestamp ? (
245 <span className="min-w-12 shrink-0 text-right text-xs tabular-nums text-muted-foreground/70">
246 {props.item.timestamp}
247 </span>
248 ) : null}
249 {shortcutLabel ? <CommandShortcut>{shortcutLabel}</CommandShortcut> : null}
250 {props.item.kind === "submenu" ? (
251 <ChevronRightIcon className="-me-0.5 ms-auto size-4 shrink-0 text-muted-foreground/70" />
252 ) : null}
253 </CommandItem>
254 );
255}