Open History to see the last commit
1import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
2
3export interface CommandPaletteItem {
4 readonly key: string;
5 readonly kind: "action" | "project" | "thread";
6 readonly project?: EnvironmentProject;
7 readonly title: string;
8 readonly detail?: string;
9 readonly searchTerms: ReadonlyArray<string>;
10 readonly run: () => void;
11}
12
13export function filterCommandPaletteItems(
14 items: ReadonlyArray<CommandPaletteItem>,
15 query: string,
16 matchedThreadKeys: ReadonlySet<string>,
17) {
18 const actionsOnly = query.startsWith(">");
19 const normalized = (actionsOnly ? query.slice(1) : query).trim().toLocaleLowerCase();
20 const tokens = normalized.split(/\s+/);
21 return items
22 .flatMap((item, index) => {
23 if (actionsOnly && item.kind !== "action") return [];
24 if (!normalized) return item.kind === "project" ? [] : [{ item, rank: 0, index }];
25 const title = item.title.toLocaleLowerCase();
26 const haystack = [title, ...item.searchTerms].join(" ").toLocaleLowerCase();
27 if (
28 !tokens.every((token) => haystack.includes(token)) &&
29 !(item.kind === "thread" && matchedThreadKeys.has(item.key))
30 )
31 return [];
32 const rank =
33 title === normalized
34 ? 3
35 : title.startsWith(normalized)
36 ? 2
37 : title.includes(normalized)
38 ? 1
39 : 0;
40 return [{ item, rank, index }];
41 })
42 .sort((left, right) => right.rank - left.rank || left.index - right.index)
43 .map(({ item }) => item);
44}
45
46export function nextPaletteIndex(index: number, direction: -1 | 1, count: number) {
47 return count === 0 ? 0 : (index + direction + count) % count;
48}