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

settingsSearch.ts

322 lines8.0 KB

1import { isElectron } from "~/env";
2import { isWindowsPlatform } from "~/lib/utils";
3​
4export type SettingsPath =
5 | "/settings/general"
6 | "/settings/appearance"
7 | "/settings/keybindings"
8 | "/settings/providers"
9 | "/settings/integrations"
10 | "/settings/source-control"
11 | "/settings/connections"
12 | "/settings/archived";
13​
14export interface SettingsSearchItem {
15 readonly id: string;
16 readonly title: string;
17 readonly to: SettingsPath;
18 readonly targetId?: string;
19 // Its row only renders in the desktop app, so a browser result would land on
20 // an anchor that isn't there.
21 readonly desktopOnly?: boolean;
22 // Its row only renders on Windows desktop, so other desktop platforms must
23 // not expose a result that points to a missing anchor.
24 readonly windowsOnly?: boolean;
26​
27/**
28 * Section labels in sidebar order. The sidebar nav and the search-result
29 * subtitles both render from this record, so each label exists once.
30 */
31export const SETTINGS_SECTION_LABELS: Readonly<Record<SettingsPath, string>> = {
32 "/settings/general": "General",
33 "/settings/appearance": "Appearance",
34 "/settings/keybindings": "Keybindings",
35 "/settings/providers": "Providers",
36 "/settings/integrations": "Integrations",
37 "/settings/source-control": "Source Control",
38 "/settings/connections": "Connections",
39 "/settings/archived": "Archive",
40};
41​
42/**
43 * Every searchable setting, in result order. This catalog is the single
44 * source of truth for anchor ids and visible titles: panels render both via
45 * `searchableSetting`, so a retitle (or, later, a translation pass) happens
46 * here once instead of separately in the panel and the index.
47 */
48export const SETTINGS_SEARCH_ITEMS = [
49 {
50 id: "color-scheme",
51 title: "Color scheme",
52 to: "/settings/appearance",
53 // The scheme tiles sit at the top of the Appearance section.
54 targetId: "appearance",
55 },
56 {
57 id: "theme",
58 title: "Themes",
59 to: "/settings/appearance",
60 // Theme cards live directly under the scheme tiles; the section is the
61 // stable scroll destination for both.
62 targetId: "appearance",
63 },
64 {
65 // Prefixed because the slider control already owns the `appearance-contrast` id.
66 id: "setting-appearance-contrast",
67 title: "Contrast",
68 to: "/settings/appearance",
69 },
70 {
71 // Prefixed because the slider control already owns the `glass-opacity` id.
72 id: "setting-glass-opacity",
73 title: "Glass opacity",
74 to: "/settings/appearance",
75 },
76 {
77 id: "environment-identification",
78 title: "Environment identification",
79 to: "/settings/appearance",
80 // The setting is stage-dependent, so its parent section is the stable destination.
81 targetId: "appearance",
82 },
83 {
84 id: "interface-font",
85 title: "Interface font",
86 to: "/settings/appearance",
87 },
88 {
89 id: "prompt-font",
90 title: "Prompt font",
91 to: "/settings/appearance",
92 },
93 {
94 id: "code-font",
95 title: "Code font",
96 to: "/settings/appearance",
97 },
98 {
99 id: "terminal-font",
100 title: "Terminal font",
101 to: "/settings/appearance",
102 },
103 {
104 id: "font-smoothing",
105 title: "Font smoothing",
106 to: "/settings/appearance",
107 },
108 {
109 id: "word-wrap",
110 title: "Word wrap",
111 to: "/settings/appearance",
112 },
113 {
114 id: "project-grouping",
115 title: "Project grouping",
116 to: "/settings/general",
117 },
118 {
119 id: "auto-settle-inactive-threads",
120 title: "Auto-settle inactive threads",
121 to: "/settings/general",
122 },
123 {
124 id: "auto-settle-merged-threads",
125 title: "Auto-settle merged threads",
126 to: "/settings/general",
127 },
128 {
129 id: "time-format",
130 title: "Time format",
131 to: "/settings/general",
132 },
133 {
134 id: "hide-whitespace-changes",
135 title: "Hide whitespace changes",
136 to: "/settings/general",
137 },
138 {
139 id: "skills-in-slash-menu",
140 title: "Show skills in slash menu",
141 to: "/settings/general",
142 },
143 {
144 id: "provider-update-checks",
145 title: "Provider update checks",
146 to: "/settings/general",
147 },
148 {
149 id: "new-threads",
150 title: "New threads",
151 to: "/settings/general",
152 },
153 {
154 id: "start-from-origin",
155 title: "Start from origin",
156 to: "/settings/general",
157 targetId: "new-threads",
158 },
159 {
160 id: "add-project-starts-in",
161 title: "Add project starts in",
162 to: "/settings/general",
163 },
164 {
165 id: "unpin-confirmation",
166 title: "Unpin confirmation",
167 to: "/settings/general",
168 },
169 {
170 id: "archive-confirmation",
171 title: "Archive confirmation",
172 to: "/settings/general",
173 },
174 {
175 id: "delete-confirmation",
176 title: "Delete confirmation",
177 to: "/settings/general",
178 },
179 {
180 id: "quit-confirmation",
181 title: "Hold to quit",
182 to: "/settings/general",
183 desktopOnly: true,
184 },
185 {
186 id: "text-generation-model",
187 title: "Text generation model",
188 to: "/settings/general",
189 },
190 {
191 id: "diagnostics",
192 title: "Diagnostics",
193 to: "/settings/general",
194 },
195 {
196 id: "legacy-plan-mode",
197 title: "Plan mode (legacy)",
198 to: "/settings/general",
199 },
200 {
201 id: "legacy-token-streaming",
202 title: "Stream token by token (legacy)",
203 to: "/settings/general",
204 },
205 {
206 id: "legacy-sidebar",
207 title: "Sidebar (legacy)",
208 to: "/settings/general",
209 },
210 {
211 id: "keybindings",
212 title: "Keybindings",
213 to: "/settings/keybindings",
214 },
215 {
216 id: "providers",
217 title: "Providers",
218 to: "/settings/providers",
219 },
220 {
221 id: "agent-browser-access",
222 title: "Agent browser access",
223 to: "/settings/integrations",
224 targetId: "browser",
225 },
226 {
227 id: "browser-default-viewport",
228 title: "Default browser viewport",
229 to: "/settings/integrations",
230 targetId: "browser",
231 },
232 {
233 id: "browser-default-zoom",
234 title: "Default browser zoom",
235 to: "/settings/integrations",
236 targetId: "browser",
237 },
238 {
239 id: "browser-default-appearance",
240 title: "Default browser appearance",
241 to: "/settings/integrations",
242 targetId: "browser",
243 },
244 {
245 id: "browser-recording-frame-rate",
246 title: "Browser recording frame rate",
247 to: "/settings/integrations",
248 targetId: "browser",
249 },
250 {
251 id: "browser-auto-show-floating-preview",
252 title: "Auto-show floating preview",
253 to: "/settings/integrations",
254 targetId: "browser",
255 },
256 {
257 id: "source-control",
258 title: "Source control",
259 to: "/settings/source-control",
260 },
261 {
262 id: "remote-environments",
263 title: "Remote environments",
264 to: "/settings/connections",
265 },
266 {
267 id: "wsl-backend",
268 title: "WSL backend",
269 to: "/settings/connections",
270 desktopOnly: true,
271 windowsOnly: true,
272 },
273 {
274 id: "archive",
275 title: "Archived threads",
276 to: "/settings/archived",
277 },
278] as const satisfies ReadonlyArray<SettingsSearchItem>;
279​
280export type SettingsSearchItemId = (typeof SETTINGS_SEARCH_ITEMS)[number]["id"];
281​
282const SEARCH_ITEMS_BY_ID = Object.fromEntries(
283 SETTINGS_SEARCH_ITEMS.map((item) => [item.id, item]),
284) as Readonly<Record<SettingsSearchItemId, SettingsSearchItem>>;
285​
286/**
287 * `id` and `title` props for the element a search item anchors to. Panels
288 * spread (or pick from) this instead of restating the strings, so the catalog
289 * and the rendered settings cannot drift apart.
290 */
291export function searchableSetting(id: SettingsSearchItemId): {
292 readonly id: string;
293 readonly title: string;
294} {
295 const { id: anchorId, title } = SEARCH_ITEMS_BY_ID[id];
296 return { id: anchorId, title };
298​
299function normalizeSearchText(value: string): string {
300 return value
301 .normalize("NFKD")
302 .replace(/[\u0300-\u036f]/g, "")
303 .toLocaleLowerCase()
304 .replace(/\s+/g, " ")
305 .trim();
307​
308export function searchSettings(
309 query: string,
310 items: ReadonlyArray<SettingsSearchItem> = SETTINGS_SEARCH_ITEMS,
311): ReadonlyArray<SettingsSearchItem> {
312 const normalizedQuery = normalizeSearchText(query);
313 if (normalizedQuery.length === 0) return [];
314​
315 return items.filter(
316 (item) =>
317 (isElectron || item.desktopOnly !== true) &&
318 (!item.windowsOnly ||
319 isWindowsPlatform(typeof navigator === "undefined" ? "" : navigator.platform)) &&
320 normalizeSearchText(item.title).includes(normalizedQuery),
321 );