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

CommandPaletteResults.tsx

219 lines7.3 KB

1import { type ResolvedKeybindingsConfig } from "@t3tools/contracts";
2import { ChevronRightIcon } from "lucide-react";
3import { shortcutLabelForCommand } from "../keybindings";
4import {
5 type CommandPaletteActionItem,
6 type CommandPaletteGroup,
7 type CommandPaletteSubmenuItem,
8} from "./CommandPalette.logic";
9import {
10 CommandCollection,
11 CommandGroup,
12 CommandGroupLabel,
13 CommandItem,
14 CommandList,
15 CommandShortcut,
16} from "./ui/command";
17import { cn } from "~/lib/utils";
18​
19function foldAsciiCase(value: string): string {
20 return value.replace(/[A-Z]/g, (character) => character.toLowerCase());
22​
23function HighlightedSearchText(props: { text: string; query: string }) {
24 const query = props.query.trim();
25 if (query.length === 0) return props.text;
26​
27 const normalizedText = foldAsciiCase(props.text);
28 const normalizedQuery = foldAsciiCase(query);
29 const parts: Array<{
30 readonly text: string;
31 readonly highlighted: boolean;
32 readonly start: number;
33 }> = [];
34 let cursor = 0;
35​
36 while (cursor < props.text.length) {
37 const matchIndex = normalizedText.indexOf(normalizedQuery, cursor);
38 if (matchIndex === -1) {
39 parts.push({ text: props.text.slice(cursor), highlighted: false, start: cursor });
40 break;
41 }
42 if (matchIndex > cursor) {
43 parts.push({
44 text: props.text.slice(cursor, matchIndex),
45 highlighted: false,
46 start: cursor,
47 });
48 }
49 parts.push({
50 text: props.text.slice(matchIndex, matchIndex + query.length),
51 highlighted: true,
52 start: matchIndex,
53 });
54 cursor = matchIndex + query.length;
55 }
56​
57 return parts.map((part) =>
58 part.highlighted ? (
59 <mark className="bg-transparent font-semibold text-foreground" key={part.start}>
60 {part.text}
61 </mark>
62 ) : (
63 part.text
64 ),
65 );
67​
68function ThreadContentMatch(props: {
69 match: NonNullable<CommandPaletteActionItem["threadContentMatch"]>;
70}) {
71 const isUser = props.match.source === "user";
72 return (
73 <span className="truncate text-xs text-muted-foreground/85">
74 <span className={isUser ? "text-blue-400" : "text-emerald-400"}>
75 {isUser ? "You:" : "Agent:"}
76 </span>{" "}
77 <HighlightedSearchText text={props.match.snippet} query={props.match.query} />
78 </span>
79 );
81​
82interface CommandPaletteResultsProps {
83 emptyStateMessage?: string;
84 groups: ReadonlyArray<CommandPaletteGroup>;
85 highlightedItemValue?: string | null;
86 isActionsOnly: boolean;
87 keybindings: ResolvedKeybindingsConfig;
88 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
90​
91export function CommandPaletteResults(props: CommandPaletteResultsProps) {
92 if (props.groups.length === 0) {
93 return (
94 <div className="py-10 text-center text-sm text-muted-foreground">
95 {props.emptyStateMessage ??
96 (props.isActionsOnly
97 ? "No matching actions."
98 : "No matching commands, projects, or threads.")}
99 </div>
100 );
101 }
102​
103 return (
104 <CommandList>
105 {props.groups.map((group) => (
106 <CommandGroup items={group.items} key={group.value}>
107 <CommandGroupLabel className="ps-[9px]">{group.label}</CommandGroupLabel>
108 <CommandCollection>
109 {(item) =>
110 item.disabled ? (
111 <DisabledCommandPaletteResultRow item={item} key={item.value} />
112 ) : (
113 <CommandPaletteResultRow
114 item={item}
115 key={item.value}
116 keybindings={props.keybindings}
117 isActive={props.highlightedItemValue === item.value}
118 onExecuteItem={props.onExecuteItem}
119 />
120 )
121 }
122 </CommandCollection>
123 </CommandGroup>
124 ))}
125 </CommandList>
126 );
128​
129function DisabledCommandPaletteResultRow(props: {
130 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
131}) {
132 return (
133 <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">
134 {props.item.icon}
135 {props.item.description || props.item.threadContentMatch ? (
136 <span className="flex min-w-0 flex-1 flex-col">
137 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
138 {props.item.titleLeadingContent}
139 <span className="truncate">{props.item.title}</span>
140 </span>
141 {props.item.threadContentMatch ? (
142 <ThreadContentMatch match={props.item.threadContentMatch} />
143 ) : null}
144 {props.item.description ? (
145 <span className="min-w-0 text-muted-foreground/70 text-xs">
146 {props.item.description}
147 </span>
148 ) : null}
149 </span>
150 ) : (
151 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
152 {props.item.titleLeadingContent}
153 <span className="truncate">{props.item.title}</span>
154 </span>
155 )}
156 {props.item.titleTrailingContent}
157 </div>
158 );
160​
161function CommandPaletteResultRow(props: {
162 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
163 isActive: boolean;
164 keybindings: ResolvedKeybindingsConfig;
165 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
166}) {
167 const shortcutLabel = props.item.shortcutCommand
168 ? shortcutLabelForCommand(props.keybindings, props.item.shortcutCommand)
169 : null;
170​
171 return (
172 <CommandItem
173 value={props.item.value}
174 className={cn(
175 "cursor-pointer gap-2 hover:bg-transparent hover:text-inherit data-highlighted:bg-transparent data-highlighted:text-inherit data-selected:bg-transparent data-selected:text-inherit [&[data-highlighted][data-selected]]:bg-transparent [&[data-highlighted][data-selected]]:text-inherit",
176 props.isActive && "bg-accent! text-accent-foreground!",
177 )}
178 onMouseDown={(event) => {
179 event.preventDefault();
180 }}
181 onClick={() => {
182 props.onExecuteItem(props.item);
183 }}
184 >
185 {props.item.icon}
186 {props.item.description || props.item.threadContentMatch ? (
187 <span className="flex min-w-0 flex-1 flex-col">
188 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
189 {props.item.titleLeadingContent}
190 <span className="truncate">{props.item.title}</span>
191 </span>
192 {props.item.threadContentMatch ? (
193 <ThreadContentMatch match={props.item.threadContentMatch} />
194 ) : null}
195 {props.item.description ? (
196 <span className="min-w-0 text-muted-foreground/70 text-xs">
197 {props.item.description}
198 </span>
199 ) : null}
200 </span>
201 ) : (
202 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
203 {props.item.titleLeadingContent}
204 <span className="truncate">{props.item.title}</span>
205 </span>
206 )}
207 {props.item.titleTrailingContent}
208 {props.item.timestamp ? (
209 <span className="min-w-12 shrink-0 text-right text-xs tabular-nums text-muted-foreground/70">
210 {props.item.timestamp}
211 </span>
212 ) : null}
213 {shortcutLabel ? <CommandShortcut>{shortcutLabel}</CommandShortcut> : null}
214 {props.item.kind === "submenu" ? (
215 <ChevronRightIcon className="-me-0.5 ms-auto size-4 shrink-0 text-muted-foreground/70" />
216 ) : null}
217 </CommandItem>
218 );