Open History to see the last commit
1import { foldSearchQuery, rankSearchItems, scoreSearchTerms } from "@t3tools/shared/paletteSearch";
2
3import type { SettingsPageTarget } from "./components/settings-page-targets";
4
5/** What each page holds besides its row's label, so search finds a setting by its own name. */
6const SETTINGS_SEARCH_TERMS = {
7 account: ["sign in", "sign out", "login", "clerk", "connect"],
8 SettingsEnvironments: [
9 "servers",
10 "connections",
11 "pair",
12 "add environment",
13 "remote",
14 "tailscale",
15 ],
16 SettingsNotifications: ["push", "alerts", "live activity", "agent activity"],
17 SettingsAppearance: [
18 "theme",
19 "dark mode",
20 "light mode",
21 "colors",
22 "font size",
23 "text size",
24 "code",
25 "diffs",
26 "terminal",
27 "word break",
28 "math",
29 "tool calls in chat",
30 "hide tool calls",
31 "commands edits searches",
32 "work log",
33 "zen",
34 "final answers only",
35 ],
36 SettingsLanguage: ["language", "locale", "translation", "english"],
37 SettingsNavigation: ["tabs", "tab order", "reorder", "hide tabs"],
38 SettingsKeyboard: ["return key", "send message", "new line", "hardware keyboard"],
39 SettingsVoice: [
40 "microphone",
41 "dictation",
42 "transcription",
43 "transcribe on",
44 "transcription model",
45 "this device",
46 "environment",
47 "on-device",
48 "audio input",
49 "preferred microphone",
50 "microphone order",
51 "bluetooth",
52 ],
53 SettingsScheduledTasks: ["automations", "schedule", "cron", "recurring", "webhook"],
54 SettingsProjectOverview: ["project", "overview", "checkouts", "delete project"],
55 SettingsOrganization: [
56 "project grouping",
57 "group projects",
58 "organization",
59 "sidebar",
60 "working section",
61 "new threads",
62 "last project",
63 ],
64 SettingsThreads: [
65 "new threads",
66 "default workspace",
67 "default permissions",
68 "access",
69 "response streaming",
70 "preview browser",
71 "browser access",
72 "auto-settle",
73 "settle",
74 "snooze",
75 "usage limits",
76 "auto-resume",
77 "thread behavior",
78 ],
79 SettingsFollowUp: ["follow-ups", "queue", "steer", "while the agent is running"],
80 SettingsArchive: ["archive", "archived", "restore", "unarchive"],
81 SettingsProviderAccounts: [
82 "provider accounts",
83 "codex",
84 "claude",
85 "cursor",
86 "opencode",
87 "sign in",
88 "login",
89 "auth",
90 ],
91 SettingsEnvironmentSourceControl: [
92 "git",
93 "pull requests",
94 "co-authors",
95 "agent credits",
96 "default branch",
97 "automatically pull",
98 "clone into host folders",
99 "ghq",
100 ],
101 SettingsWorktrees: [
102 "worktree",
103 "branch naming",
104 "branch prefix",
105 "submodules",
106 "start from remote",
107 ],
108 SettingsLabs: [
109 "beta",
110 "linked environments",
111 "link machine",
112 "fast worktree checkout",
113 "clone cache",
114 ],
115 SettingsEnvironmentMaintenance: ["updates", "provider updates", "restart", "manage environments"],
116 SettingsAbout: [
117 "version",
118 "diagnostics",
119 "client storage",
120 "cache",
121 "legal",
122 "privacy",
123 "terms",
124 "open source licenses",
125 ],
126} satisfies Partial<Record<SettingsPageTarget | "account", readonly string[]>>;
127const termsById: ReadonlyMap<string, readonly string[]> = new Map(
128 Object.entries(SETTINGS_SEARCH_TERMS),
129);
130
131export interface SettingsSearchRow {
132 readonly id: string;
133 readonly label: string;
134}
135
136export interface SettingsSearchSection<Row extends SettingsSearchRow> {
137 readonly title?: string;
138 readonly rows: readonly Row[];
139}
140
141/**
142 * Keeps the rows whose label, section, or page contents match `query`, in list order, and drops
143 * sections left empty. Matching is the command palette's, so both searches agree.
144 */
145export function filterSettingsSections<
146 Row extends SettingsSearchRow,
147 Section extends SettingsSearchSection<Row>,
148>(sections: readonly Section[], query: string): Section[] {
149 const normalizedQuery = foldSearchQuery(query);
150 const rows = sections.flatMap((section) =>
151 section.rows.map((row) => ({ row, section: section.title })),
152 );
153 const matched = normalizedQuery
154 ? new Set(
155 rankSearchItems(rows, ({ row, section }, options) =>
156 scoreSearchTerms(
157 normalizedQuery,
158 [row.label, ...(section ? [section] : []), ...(termsById.get(row.id) ?? [])],
159 options,
160 ),
161 ).map(({ item }) => item.row),
162 )
163 : null;
164 return sections.flatMap((section) => {
165 const kept = matched ? section.rows.filter((row) => matched.has(row)) : section.rows;
166 return kept.length ? [{ ...section, rows: kept }] : [];
167 });
168}