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

CommandPaletteResults.tsx

246 lines8.8 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 { 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;
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 );
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 );
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 });
85​
86interface CommandPaletteVirtualizedResultsProps extends Omit<CommandPaletteResultsProps, "groups"> {
87 rows: ReadonlyArray<CommandPaletteRow>;
88 listRef: RefObject<LegendListRef | null>;
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 );
152​
153export function CommandPaletteTitleIcon(props: { children: ReactNode }) {
154 return <span className="flex h-5 shrink-0 items-center self-start pt-0.5">{props.children}</span>;
156​
157function DisabledCommandPaletteResultRow(props: {
158 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
159}) {
160 return (
161 <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">
162 <CommandPaletteTitleIcon>{props.item.icon}</CommandPaletteTitleIcon>
163 {props.item.description || props.item.threadContentMatch ? (
164 <span className="flex min-w-0 flex-1 flex-col">
165 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
166 {props.item.titleLeadingContent}
167 <span className="truncate">{props.item.title}</span>
168 </span>
169 {props.item.threadContentMatch ? (
170 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
171 ) : null}
172 {props.item.description ? (
173 <span className="min-w-0 text-muted-foreground/70 text-xs">
174 {props.item.description}
175 </span>
176 ) : null}
177 </span>
178 ) : (
179 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
180 {props.item.titleLeadingContent}
181 <span className="truncate">{props.item.title}</span>
182 </span>
183 )}
184 {props.item.titleTrailingContent}
185 </div>
186 );
188​
189function CommandPaletteResultRow(props: {
190 index?: number;
191 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
192 isActive: boolean;
193 keybindings: ResolvedKeybindingsConfig;
194 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
195}) {
196 const shortcutLabel = props.item.shortcutCommand
197 ? shortcutLabelForCommand(props.keybindings, props.item.shortcutCommand)
198 : null;
199​
200 return (
201 <CommandItem
202 {...(props.index === undefined ? {} : { index: props.index })}
203 value={props.item.value}
204 active={props.isActive}
205 onMouseDown={(event) => {
206 event.preventDefault();
207 }}
208 onClick={() => {
209 props.onExecuteItem(props.item);
210 }}
211 >
212 <CommandPaletteTitleIcon>{props.item.icon}</CommandPaletteTitleIcon>
213 {props.item.description || props.item.threadContentMatch ? (
214 <span className="flex min-w-0 flex-1 flex-col">
215 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
216 {props.item.titleLeadingContent}
217 <span className="truncate">{props.item.title}</span>
218 </span>
219 {props.item.threadContentMatch ? (
220 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
221 ) : null}
222 {props.item.description ? (
223 <span className="min-w-0 text-muted-foreground/70 text-xs">
224 {props.item.description}
225 </span>
226 ) : null}
227 </span>
228 ) : (
229 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
230 {props.item.titleLeadingContent}
231 <span className="truncate">{props.item.title}</span>
232 </span>
233 )}
234 {props.item.titleTrailingContent}
235 {props.item.timestamp ? (
236 <span className="min-w-12 shrink-0 text-right text-xs tabular-nums text-muted-foreground/70">
237 {props.item.timestamp}
238 </span>
239 ) : null}
240 {shortcutLabel ? <CommandShortcut>{shortcutLabel}</CommandShortcut> : null}
241 {props.item.kind === "submenu" ? (
242 <ChevronRightIcon className="-me-0.5 ms-auto size-4 shrink-0 text-muted-foreground/70" />
243 ) : null}
244 </CommandItem>
245 );