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

CommandPaletteResults.tsx

306 lines10.8 KB

1import { startPaletteThreadDrag } from "./CommandPalette.drag";
2import { useLegendListRef } from "~/hooks/useLegendListRef";
3import { type ResolvedKeybindingsConfig } from "@t3tools/contracts";
4import { LegendList, type LegendListRef } from "@legendapp/list/react";
5import { ChevronRightIcon } from "lucide-react";
6import { type ReactNode, type RefObject, useState } from "react";
7import { cn } from "~/lib/utils";
8import { textDirection } from "~/textDirection";
9import { shortcutLabelForCommand } from "../keybindings";
10import {
11 type CommandPaletteActionItem,
12 type CommandPaletteGroup,
13 type CommandPaletteRow,
14 type CommandPaletteSubmenuItem,
15} from "./CommandPalette.logic";
16import {
17 CommandCollection,
18 CommandGroup,
19 CommandGroupLabel,
20 CommandItem,
21 CommandList,
22 CommandListHeading,
23 CommandListVirtualized,
24 CommandShortcut,
25} from "./ui/command";
26import { getVirtualizedScrollFadeClassName } from "./ui/scroll-area";
27import { ProjectPinButton } from "./ProjectPinButton";
28import { SearchMatchHintLine, ThreadSearchMatchExcerpt } from "./ThreadSearchMatch";
29​
30interface CommandPaletteResultsProps {
31 emptyStateMessage?: string;
32 groups: ReadonlyArray<CommandPaletteGroup>;
33 highlightedItemValue?: string | null;
34 isActionsOnly: boolean;
35 keybindings: ResolvedKeybindingsConfig;
36 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
38​
39function CommandPaletteEmptyState(props: { emptyStateMessage?: string; isActionsOnly: boolean }) {
40 return (
41 <div className="py-10 text-center text-sm text-muted-foreground">
42 {props.emptyStateMessage ??
43 (props.isActionsOnly
44 ? "No matching actions."
45 : "No matching commands, projects, or threads.")}
46 </div>
47 );
49​
50export function CommandPaletteResults(props: CommandPaletteResultsProps) {
51 if (props.groups.length === 0) {
52 return <CommandPaletteEmptyState {...props} />;
53 }
54​
55 return (
56 <CommandList>
57 {props.groups.map((group) => (
58 <CommandGroup items={group.items} key={group.value}>
59 {group.label ? <CommandGroupLabel>{group.label}</CommandGroupLabel> : null}
60 <CommandCollection>
61 {(item) =>
62 item.disabled ? (
63 <DisabledCommandPaletteResultRow item={item} key={item.value} />
64 ) : (
65 <CommandPaletteResultRow
66 item={item}
67 key={item.value}
68 keybindings={props.keybindings}
69 isActive={props.highlightedItemValue === item.value}
70 onExecuteItem={props.onExecuteItem}
71 />
72 )
73 }
74 </CommandCollection>
75 </CommandGroup>
76 ))}
77 </CommandList>
78 );
80​
81export function scrollCommandPaletteRowIntoView(list: LegendListRef | null, rowIndex: number) {
82 void list?.scrollIndexIntoView({
83 index: rowIndex,
84 animated: false,
85 viewportMargin: { start: 24, end: 24 },
86 });
88​
89interface CommandPaletteVirtualizedResultsProps extends Omit<CommandPaletteResultsProps, "groups"> {
90 rows: ReadonlyArray<CommandPaletteRow>;
91 listRef: RefObject<LegendListRef | null>;
93​
94/** Pass flat items with virtualized to Command, scroll highlights via listRef, and handle Enter for
95 * unmounted rows. */
96export function CommandPaletteVirtualizedResults(props: CommandPaletteVirtualizedResultsProps) {
97 const { listRef } = props;
98 const [scrollFade, setScrollFade] = useState({ top: false, bottom: false });
99 const updateScrollFade = () => {
100 const metrics = listRef.current?.getState();
101 if (!metrics) return;
102 const top = !metrics.isAtStart;
103 const bottom = !metrics.isAtEnd;
104 setScrollFade((current) =>
105 current.top === top && current.bottom === bottom ? current : { top, bottom },
106 );
107 };
108 const scrollRef = useLegendListRef(listRef, updateScrollFade);
109​
110 if (props.rows.length === 0) {
111 return <CommandPaletteEmptyState {...props} />;
112 }
113​
114 return (
115 <CommandListVirtualized>
116 <LegendList<CommandPaletteRow>
117 ref={scrollRef}
118 data={props.rows}
119 initialScrollOffset={0}
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 onReady={updateScrollFade}
145 // Legend counts style padding (not class padding) in its size, end state and row positions.
146 style={{ paddingTop: 8, paddingBottom: 8 }}
147 contentContainerClassName="px-2"
148 className={cn(
149 "min-h-0 scroll-py-6 overflow-x-hidden overscroll-y-contain [&::-webkit-scrollbar-track]:my-2",
150 getVirtualizedScrollFadeClassName(scrollFade),
151 )}
152 />
153 </CommandListVirtualized>
154 );
156​
157export function CommandPaletteTitleIcon(props: { children: ReactNode }) {
158 return <span className="flex h-5 shrink-0 items-center self-start pt-0.5">{props.children}</span>;
160​
161function CommandPaletteTitle({
162 item,
163}: {
164 item: Pick<CommandPaletteActionItem | CommandPaletteSubmenuItem, "title">;
165}) {
166 const { title } = item;
167 return (
168 <span className="truncate" dir={typeof title === "string" ? textDirection(title) : undefined}>
169 {title}
170 </span>
171 );
173​
174/**
175 * Project and environment for a thread row whose second line shows a message
176 * snippet or match hint instead, so similar rows can still be told apart.
177 */
178function ThreadRowContext({
179 item,
180}: {
181 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
182}) {
183 if (!item.threadSearch || !(item.threadContentMatch || item.matchHint)) return null;
184 // A "Project: …" hint already names the project.
185 const context = [item.threadSearch.projectTitle, item.threadSearch.environmentLabel]
186 .filter((part) => part && part !== item.matchHint?.text)
187 .join(" · ");
188 return context ? (
189 <span className="max-w-[45%] shrink-0 truncate text-muted-foreground/70 text-xs">
190 {context}
191 </span>
192 ) : null;
194​
195function DisabledCommandPaletteResultRow(props: {
196 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
197}) {
198 return (
199 <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">
200 <CommandPaletteTitleIcon>{props.item.icon}</CommandPaletteTitleIcon>
201 {props.item.description || props.item.threadContentMatch ? (
202 <span className="flex min-w-0 flex-1 flex-col">
203 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
204 {props.item.titleLeadingContent}
205 <CommandPaletteTitle item={props.item} />
206 <ThreadRowContext item={props.item} />
207 </span>
208 {props.item.threadContentMatch ? (
209 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
210 ) : props.item.matchHint ? (
211 <SearchMatchHintLine hint={props.item.matchHint} />
212 ) : props.item.description ? (
213 <span className="min-w-0 text-muted-foreground/70 text-xs">
214 {props.item.description}
215 </span>
216 ) : null}
217 </span>
218 ) : (
219 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
220 {props.item.titleLeadingContent}
221 <CommandPaletteTitle item={props.item} />
222 </span>
223 )}
224 {props.item.titleTrailingContent}
225 </div>
226 );
228​
229function CommandPaletteResultRow(props: {
230 index?: number;
231 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
232 isActive: boolean;
233 keybindings: ResolvedKeybindingsConfig;
234 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
235}) {
236 const shortcutLabel = props.item.shortcutCommand
237 ? shortcutLabelForCommand(props.keybindings, props.item.shortcutCommand)
238 : null;
239​
240 return (
241 <CommandItem
242 {...(props.index === undefined ? {} : { index: props.index })}
243 value={props.item.value}
244 active={props.isActive}
245 onPointerDown={(event) => {
246 if (props.item.threadRef) {
247 startPaletteThreadDrag(
248 event.nativeEvent,
249 props.item.threadRef,
250 props.item.searchTitle ??
251 (typeof props.item.title === "string" ? props.item.title : "Thread"),
252 );
253 }
254 }}
255 onMouseDown={(event) => {
256 event.preventDefault();
257 }}
258 onClick={() => {
259 props.onExecuteItem(props.item);
260 }}
261 >
262 <CommandPaletteTitleIcon>{props.item.icon}</CommandPaletteTitleIcon>
263 {props.item.description || props.item.threadContentMatch ? (
264 <span className="flex min-w-0 flex-1 flex-col">
265 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
266 {props.item.titleLeadingContent}
267 <CommandPaletteTitle item={props.item} />
268 <ThreadRowContext item={props.item} />
269 </span>
270 {props.item.threadContentMatch ? (
271 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
272 ) : props.item.matchHint ? (
273 <SearchMatchHintLine hint={props.item.matchHint} />
274 ) : props.item.description ? (
275 <span className="min-w-0 text-muted-foreground/70 text-xs">
276 {props.item.description}
277 </span>
278 ) : null}
279 </span>
280 ) : (
281 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
282 {props.item.titleLeadingContent}
283 <CommandPaletteTitle item={props.item} />
284 </span>
285 )}
286 {props.item.titleTrailingContent}
287 {props.item.kind === "action" && props.item.pin ? (
288 <ProjectPinButton
289 name={props.item.pin.name}
290 pinned={props.item.pin.pinned}
291 onToggle={props.item.pin.toggle}
292 shortcutLabel={shortcutLabelForCommand(props.keybindings, "thread.pin")}
293 />
294 ) : null}
295 {props.item.timestamp ? (
296 <span className="min-w-12 shrink-0 text-right text-xs tabular-nums text-muted-foreground/70">
297 {props.item.timestamp}
298 </span>
299 ) : null}
300 {shortcutLabel ? <CommandShortcut>{shortcutLabel}</CommandShortcut> : null}
301 {props.item.kind === "submenu" ? (
302 <ChevronRightIcon className="-me-0.5 ms-auto size-4 shrink-0 text-muted-foreground/70" />
303 ) : null}
304 </CommandItem>
305 );