Skip to content

Commit e9d6125

Browse files
authored
feat(web): command palette search finds options inside submenus (#24709)
1 parent c2845de commit e9d6125

5 files changed

Lines changed: 118 additions & 16 deletions

File tree

‎apps/web/src/components/CommandPalette.logic.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools
33
import type { Thread } from "../types";
44
import { makeThreadFixture } from "../test-fixtures";
55
import {
6+
buildNestedSearchItems,
67
buildThreadActionItems,
78
buildLinkedThreadActionItems,
89
filterPinnedBrowseEntries,
@@ -168,6 +169,56 @@ describe("buildThreadActionItems", () => {
168169
]);
169170
});
170171

172+
it("searches submenu options from the root and from a submenu with no match", () => {
173+
const action = (value: string, title: string) => ({
174+
kind: "action" as const,
175+
value,
176+
searchTerms: [title],
177+
title,
178+
icon: null,
179+
run: async () => undefined,
180+
});
181+
const projects = [action("new-thread-in:t3code", "t3code"), action("new-thread-in:web", "web")];
182+
const newThreadIn = {
183+
kind: "submenu" as const,
184+
value: "action:new-thread-in",
185+
searchTerms: ["new thread"],
186+
title: "New thread in...",
187+
icon: null,
188+
addonIcon: null,
189+
groups: [{ value: "projects", label: "Projects", items: projects }],
190+
};
191+
const addProject = action("action:add-project", "Add project");
192+
const rootGroups = [{ value: "actions", label: "Actions", items: [newThreadIn, addProject] }];
193+
const search = (query: string, isInSubmenu: boolean) =>
194+
filterCommandPaletteGroups({
195+
activeGroups: isInSubmenu ? newThreadIn.groups : rootGroups,
196+
query,
197+
isInSubmenu,
198+
allOptions: {
199+
rootGroups,
200+
nestedItems: buildNestedSearchItems([newThreadIn, addProject], query),
201+
},
202+
projectSearchItems: [],
203+
threadSearchItems: [
204+
...buildThreadActionItems({
205+
threads: [makeThread({ title: "Thread about t3code" })],
206+
projectByKey: new Map(),
207+
icon: null,
208+
runThread: async () => undefined,
209+
}),
210+
],
211+
}).map((group) => [group.label, group.items.map((item) => item.title)]);
212+
213+
expect(search("new thread in t3code", false)[0]).toEqual([
214+
"Suggestions",
215+
["New thread in t3code"],
216+
]);
217+
expect(search("new thread", false).map(([label]) => label)).not.toContain("Suggestions");
218+
expect(search("t3", true)).toEqual([["Projects", ["t3code"]]]);
219+
expect(search("add project", true)).toEqual([["Actions", ["Add project"]]]);
220+
});
221+
171222
it("lists message matches last, once, without moving the title matches above them", () => {
172223
const threads = [
173224
makeThread({ id: ThreadId.make("title"), title: "Disable Convex schema validation" }),

‎apps/web/src/components/CommandPalette.logic.ts‎

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ export interface CommandPaletteItem {
168168
readonly timestamp?: string;
169169
/** Thread rows rank by this, with use history, instead of by `searchTerms`. */
170170
readonly threadSearch?: ThreadSearchSubject;
171-
/** Title ranges the current query matched, for highlighting. */
172-
readonly titleRanges?: ReadonlyArray<SearchRange>;
173171
/** Shown in place of the description when the match is not in the title. */
174172
readonly matchHint?: SearchMatchHint;
175173
readonly icon: ReactNode;
@@ -508,13 +506,29 @@ export function filterCommandPaletteGroups(input: {
508506
activeGroups: ReadonlyArray<CommandPaletteGroup>;
509507
query: string;
510508
isInSubmenu: boolean;
509+
/** Submenu options join the root search, and a submenu with no match falls back to it. */
510+
allOptions?: {
511+
readonly rootGroups: ReadonlyArray<CommandPaletteGroup>;
512+
readonly nestedItems: ReadonlyArray<CommandPaletteActionItem>;
513+
};
511514
projectSearchItems: ReadonlyArray<CommandPaletteActionItem>;
512515
settingsSearchItems?: ReadonlyArray<CommandPaletteActionItem>;
513516
threadSearchItems: ReadonlyArray<CommandPaletteActionItem>;
514517
/** Threads whose messages match, best first; threads already listed are skipped. */
515518
threadMessageItems?: ReadonlyArray<CommandPaletteActionItem>;
516519
now?: number;
517520
}): CommandPaletteGroup[] {
521+
if (input.allOptions && input.isInSubmenu) {
522+
const { allOptions, ...submenuInput } = input;
523+
const scoped = filterCommandPaletteGroups(submenuInput);
524+
if (scoped.length > 0 || input.query.trim().length === 0) return scoped;
525+
return filterCommandPaletteGroups({
526+
...input,
527+
activeGroups: allOptions.rootGroups,
528+
isInSubmenu: false,
529+
});
530+
}
531+
518532
const isActionsFilter = input.query.startsWith(">");
519533
const searchQuery = isActionsFilter ? input.query.slice(1) : input.query;
520534
const normalizedQuery = foldSearchQuery(searchQuery);
@@ -538,6 +552,13 @@ export function filterCommandPaletteGroups(input: {
538552
// On equal match classes, groups keep this order: threads, what people search
539553
// for most, right after commands.
540554
if (searchesEverything) {
555+
if (input.allOptions && input.allOptions.nestedItems.length > 0) {
556+
searchableGroups.push({
557+
value: "nested-search",
558+
label: "Suggestions",
559+
items: input.allOptions.nestedItems,
560+
});
561+
}
541562
if (input.threadSearchItems.length > 0) {
542563
searchableGroups.push({
543564
value: "threads-search",
@@ -572,7 +593,7 @@ export function filterCommandPaletteGroups(input: {
572593
const limit =
573594
group.value === "threads-search"
574595
? PALETTE_THREAD_LIMIT
575-
: searchesEverything && threadsMatch
596+
: group.value === "nested-search" || (searchesEverything && threadsMatch)
576597
? PALETTE_SECONDARY_LIMIT
577598
: ranked.length;
578599
// Hints are built only for the rows shown.
@@ -583,7 +604,7 @@ export function filterCommandPaletteGroups(input: {
583604
)
584605
.slice(0, limit)
585606
.map(({ item, match }) => {
586-
if (match.ranges.length > 0) return { ...item, titleRanges: match.ranges };
607+
if (match.ranges.length > 0) return item;
587608
// Built only for the rows shown.
588609
const matchHint = match.explain?.();
589610
return matchHint ? { ...item, matchHint } : item;
@@ -600,6 +621,35 @@ export function filterCommandPaletteGroups(input: {
600621
: [...groups, { value: "threads-messages", label: "In messages", items: messageItems }];
601622
}
602623

624+
/** Every option inside a submenu as one row, e.g. "New thread in t3code" or "Change theme: Dracula". */
625+
export function buildNestedSearchItems(
626+
items: ReadonlyArray<CommandPaletteActionItem | CommandPaletteSubmenuItem>,
627+
query: string,
628+
): CommandPaletteActionItem[] {
629+
const nameOf = (item: CommandPaletteItem) =>
630+
typeof item.title === "string" ? item.title : item.searchTitle;
631+
const topLevelNames = new Set(items.map(nameOf));
632+
const foldedQuery = foldSearchQuery(query);
633+
return items.flatMap((parent) => {
634+
if (parent.kind !== "submenu" || typeof parent.title !== "string") return [];
635+
const stem = parent.title.replace(/(\.\.\.|…)$/u, "");
636+
// A query that only names the submenu ("new thread in") wants the submenu row itself.
637+
if (foldSearchQuery(stem).startsWith(foldedQuery)) return [];
638+
const prefix = stem === parent.title ? `${stem}: ` : `${stem.trimEnd()} `;
639+
return parent.groups.flatMap((group) =>
640+
group.items.flatMap((child) => {
641+
const name = nameOf(child);
642+
if (child.kind !== "action" || name === undefined) return [];
643+
const title = `${prefix}${name}`;
644+
if (topLevelNames.has(title)) return [];
645+
const { searchTitle: _searchTitle, ...rest } = child;
646+
// Only the combined title: child keywords like "theme" would list every theme.
647+
return [{ ...rest, title, searchTerms: [title] }];
648+
}),
649+
);
650+
});
651+
}
652+
603653
export function buildBrowseGroups(input: {
604654
browseEntries: ReadonlyArray<FilesystemBrowseEntry>;
605655
browseQuery: string;

‎apps/web/src/components/CommandPalette.tsx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ import {
167167
type CommandPaletteSubmenuItem,
168168
type CommandPaletteView,
169169
filterCommandPaletteGroups,
170+
buildNestedSearchItems,
170171
filterPinnedBrowseEntries,
171172
getCommandPaletteInputPlaceholder,
172173
getCommandPaletteMode,
@@ -1681,6 +1682,14 @@ function OpenCommandPaletteDialog(props: {
16811682
activeGroups,
16821683
query: deferredQuery,
16831684
isInSubmenu: currentView !== null,
1685+
...(deferredQuery.trim().length > 0
1686+
? {
1687+
allOptions: {
1688+
rootGroups,
1689+
nestedItems: buildNestedSearchItems(actionItems, deferredQuery),
1690+
},
1691+
}
1692+
: {}),
16841693
projectSearchItems: projectSearchItems,
16851694
settingsSearchItems,
16861695
threadSearchItems:

‎apps/web/src/components/CommandPaletteResults.tsx‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ import {
2222
CommandShortcut,
2323
} from "./ui/command";
2424
import { getVirtualizedScrollFadeClassName } from "./ui/scroll-area";
25-
import {
26-
HighlightedSearchText,
27-
SearchMatchHintLine,
28-
ThreadSearchMatchExcerpt,
29-
} from "./ThreadSearchMatch";
25+
import { SearchMatchHintLine, ThreadSearchMatchExcerpt } from "./ThreadSearchMatch";
3026

3127
interface CommandPaletteResultsProps {
3228
emptyStateMessage?: string;
@@ -159,16 +155,12 @@ export function CommandPaletteTitleIcon(props: { children: ReactNode }) {
159155
function CommandPaletteTitle({
160156
item,
161157
}: {
162-
item: Pick<CommandPaletteActionItem | CommandPaletteSubmenuItem, "title" | "titleRanges">;
158+
item: Pick<CommandPaletteActionItem | CommandPaletteSubmenuItem, "title">;
163159
}) {
164160
const { title } = item;
165161
return (
166162
<span className="truncate" dir={typeof title === "string" ? textDirection(title) : undefined}>
167-
{typeof title === "string" && item.titleRanges ? (
168-
<HighlightedSearchText text={title} ranges={item.titleRanges} />
169-
) : (
170-
title
171-
)}
163+
{title}
172164
</span>
173165
);
174166
}

‎apps/web/src/components/ThreadSearchMatch.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from "@t3tools/shared/paletteSearch";
77

88
/** `text` with each range in semibold foreground. */
9-
export function HighlightedSearchText(props: { text: string; ranges: ReadonlyArray<SearchRange> }) {
9+
function HighlightedSearchText(props: { text: string; ranges: ReadonlyArray<SearchRange> }) {
1010
if (props.ranges.length === 0) return props.text;
1111
return splitSearchText(props.text, props.ranges).map((part) =>
1212
part.highlighted ? (

0 commit comments

Comments
 (0)