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 RefObject, useState } from "react";
5import { cn } from "~/lib/utils";
6import { shortcutLabelForCommand } from "../keybindings";
7import {
8 type CommandPaletteActionItem,
9 type CommandPaletteGroup,
10 type CommandPaletteRow,
11 type CommandPaletteSubmenuItem,
12} from "./CommandPalette.logic";
13import {
14 CommandCollection,
15 CommandGroup,
16 CommandGroupLabel,
17 CommandItem,
18 CommandList,
19 CommandListHeading,
20 CommandListVirtualized,
21 CommandShortcut,
22} from "./ui/command";
23import { getVirtualizedScrollFadeClassName } from "./ui/scroll-area";
24import { ThreadSearchMatchExcerpt } from "./ThreadSearchMatch";
25
26interface CommandPaletteResultsProps {
27 emptyStateMessage?: string;
28 groups: ReadonlyArray<CommandPaletteGroup>;
29 highlightedItemValue?: string | null;
30 isActionsOnly: boolean;
31 keybindings: ResolvedKeybindingsConfig;
32 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
33}
34
35function CommandPaletteEmptyState(props: { emptyStateMessage?: string; isActionsOnly: boolean }) {
36 return (
37 <div className="py-10 text-center text-sm text-muted-foreground">
38 {props.emptyStateMessage ??
39 (props.isActionsOnly
40 ? "No matching actions."
41 : "No matching commands, projects, or threads.")}
42 </div>
43 );
44}
45
46export function CommandPaletteResults(props: CommandPaletteResultsProps) {
47 if (props.groups.length === 0) {
48 return <CommandPaletteEmptyState {...props} />;
49 }
50
51 return (
52 <CommandList>
53 {props.groups.map((group) => (
54 <CommandGroup items={group.items} key={group.value}>
55 {group.label ? <CommandGroupLabel>{group.label}</CommandGroupLabel> : null}
56 <CommandCollection>
57 {(item) =>
58 item.disabled ? (
59 <DisabledCommandPaletteResultRow item={item} key={item.value} />
60 ) : (
61 <CommandPaletteResultRow
62 item={item}
63 key={item.value}
64 keybindings={props.keybindings}
65 isActive={props.highlightedItemValue === item.value}
66 onExecuteItem={props.onExecuteItem}
67 />
68 )
69 }
70 </CommandCollection>
71 </CommandGroup>
72 ))}
73 </CommandList>
74 );
75}
76
77export function scrollCommandPaletteRowIntoView(list: LegendListRef | null, rowIndex: number) {
78 const element = list?.getState?.().elementAtIndex(rowIndex);
79 if (element instanceof HTMLElement) {
80 element.scrollIntoView({ block: "nearest" });
81 return;
82 }
83 void list?.scrollIndexIntoView?.({ index: rowIndex, animated: false });
84}
85
86interface CommandPaletteVirtualizedResultsProps extends Omit<CommandPaletteResultsProps, "groups"> {
87 rows: ReadonlyArray<CommandPaletteRow>;
88 listRef: RefObject<LegendListRef | null>;
89}
90
91/**
92 * The parent passes the flat item order to the Command root as `items` with
93 * `virtualized`, scrolls keyboard highlights into view through `listRef`, and
94 * runs Enter itself since the row may be unmounted.
95 */
96export function CommandPaletteVirtualizedResults(props: CommandPaletteVirtualizedResultsProps) {
97 const { listRef } = props;
98 const [scrollFade, setScrollFade] = useState({ top: false, bottom: false });
99 const updateScrollFade = () => {
100 const scrollElement = listRef.current?.getScrollableNode?.();
101 if (!(scrollElement instanceof HTMLElement)) return;
102 const top = scrollElement.scrollTop > 1;
103 const bottom =
104 scrollElement.scrollHeight - scrollElement.clientHeight - scrollElement.scrollTop > 1;
105 setScrollFade((current) =>
106 current.top === top && current.bottom === bottom ? current : { top, bottom },
107 );
108 };
109
110 if (props.rows.length === 0) {
111 return <CommandPaletteEmptyState {...props} />;
112 }
113
114 return (
115 <CommandListVirtualized>
116 <LegendList<CommandPaletteRow>
117 ref={listRef}
118 data={props.rows}
119 keyExtractor={(row) => row.key}
120 getItemType={(row) => row.kind}
121 extraData={props.highlightedItemValue}
122 renderItem={({ item: row }) =>
123 row.kind === "label" ? (
124 <div className={row.first ? undefined : "pt-1.5"}>
125 <CommandListHeading>{row.label}</CommandListHeading>
126 </div>
127 ) : row.itemIndex === null ? (
128 <DisabledCommandPaletteResultRow item={row.item} />
129 ) : (
130 <CommandPaletteResultRow
131 index={row.itemIndex}
132 item={row.item}
133 keybindings={props.keybindings}
134 isActive={props.highlightedItemValue === row.item.value}
135 onExecuteItem={props.onExecuteItem}
136 />
137 )
138 }
139 estimatedItemSize={40}
140 drawDistance={400}
141 onLayout={updateScrollFade}
142 onScroll={updateScrollFade}
143 contentContainerClassName="px-2"
144 className={cn(
145 "min-h-0 scroll-py-6 overflow-x-hidden overscroll-y-contain py-2 [&::-webkit-scrollbar-track]:my-2",
146 getVirtualizedScrollFadeClassName(scrollFade),
147 )}
148 />
149 </CommandListVirtualized>
150 );
151}
152
153function DisabledCommandPaletteResultRow(props: {
154 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
155}) {
156 return (
157 <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">
158 {props.item.icon}
159 {props.item.description || props.item.threadContentMatch ? (
160 <span className="flex min-w-0 flex-1 flex-col">
161 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
162 {props.item.titleLeadingContent}
163 <span className="truncate">{props.item.title}</span>
164 </span>
165 {props.item.threadContentMatch ? (
166 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
167 ) : null}
168 {props.item.description ? (
169 <span className="min-w-0 text-muted-foreground/70 text-xs">
170 {props.item.description}
171 </span>
172 ) : null}
173 </span>
174 ) : (
175 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
176 {props.item.titleLeadingContent}
177 <span className="truncate">{props.item.title}</span>
178 </span>
179 )}
180 {props.item.titleTrailingContent}
181 </div>
182 );
183}
184
185function CommandPaletteResultRow(props: {
186 index?: number;
187 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
188 isActive: boolean;
189 keybindings: ResolvedKeybindingsConfig;
190 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
191}) {
192 const shortcutLabel = props.item.shortcutCommand
193 ? shortcutLabelForCommand(props.keybindings, props.item.shortcutCommand)
194 : null;
195
196 return (
197 <CommandItem
198 {...(props.index === undefined ? {} : { index: props.index })}
199 value={props.item.value}
200 active={props.isActive}
201 onMouseDown={(event) => {
202 event.preventDefault();
203 }}
204 onClick={() => {
205 props.onExecuteItem(props.item);
206 }}
207 >
208 {props.item.icon}
209 {props.item.description || props.item.threadContentMatch ? (
210 <span className="flex min-w-0 flex-1 flex-col">
211 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
212 {props.item.titleLeadingContent}
213 <span className="truncate">{props.item.title}</span>
214 </span>
215 {props.item.threadContentMatch ? (
216 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
217 ) : null}
218 {props.item.description ? (
219 <span className="min-w-0 text-muted-foreground/70 text-xs">
220 {props.item.description}
221 </span>
222 ) : null}
223 </span>
224 ) : (
225 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
226 {props.item.titleLeadingContent}
227 <span className="truncate">{props.item.title}</span>
228 </span>
229 )}
230 {props.item.titleTrailingContent}
231 {props.item.timestamp ? (
232 <span className="min-w-12 shrink-0 text-right text-xs tabular-nums text-muted-foreground/70">
233 {props.item.timestamp}
234 </span>
235 ) : null}
236 {shortcutLabel ? <CommandShortcut>{shortcutLabel}</CommandShortcut> : null}
237 {props.item.kind === "submenu" ? (
238 <ChevronRightIcon className="-me-0.5 ms-auto size-4 shrink-0 text-muted-foreground/70" />
239 ) : null}
240 </CommandItem>
241 );
242}