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

settingsSearch.ts

210 lines5.2 KB

1export type SettingsPath =
2 | "/settings/general"
3 | "/settings/appearance"
4 | "/settings/keybindings"
5 | "/settings/providers"
6 | "/settings/mcp"
7 | "/settings/source-control"
8 | "/settings/connections"
9 | "/settings/beta"
10 | "/settings/archived";
11​
12export interface SettingsSearchItem {
13 readonly id: string;
14 readonly title: string;
15 readonly to: SettingsPath;
16 readonly targetId?: string;
18​
19/**
20 * Section labels in sidebar order. The sidebar nav and the search-result
21 * subtitles both render from this record, so each label exists once.
22 */
23export const SETTINGS_SECTION_LABELS: Readonly<Record<SettingsPath, string>> = {
24 "/settings/general": "General",
25 "/settings/appearance": "Appearance",
26 "/settings/keybindings": "Keybindings",
27 "/settings/providers": "Providers",
28 "/settings/mcp": "MCP",
29 "/settings/source-control": "Source Control",
30 "/settings/connections": "Connections",
31 "/settings/beta": "Beta",
32 "/settings/archived": "Archive",
33};
34​
35/**
36 * Every searchable setting, in result order. This catalog is the single
37 * source of truth for anchor ids and visible titles: panels render both via
38 * `searchableSetting`, so a retitle (or, later, a translation pass) happens
39 * here once instead of separately in the panel and the index.
40 */
41export const SETTINGS_SEARCH_ITEMS = [
42 {
43 id: "theme",
44 title: "Theme",
45 to: "/settings/appearance",
46 },
47 {
48 // Prefixed because the slider control already owns the `glass-opacity` id.
49 id: "setting-glass-opacity",
50 title: "Glass opacity",
51 to: "/settings/appearance",
52 },
53 {
54 id: "environment-identification",
55 title: "Environment identification",
56 to: "/settings/appearance",
57 // The setting is stage-dependent, so its parent section is the stable destination.
58 targetId: "appearance",
59 },
60 {
61 id: "word-wrap",
62 title: "Word wrap",
63 to: "/settings/appearance",
64 },
65 {
66 id: "project-grouping",
67 title: "Project grouping",
68 to: "/settings/general",
69 },
70 {
71 id: "time-format",
72 title: "Time format",
73 to: "/settings/general",
74 },
75 {
76 id: "hide-whitespace-changes",
77 title: "Hide whitespace changes",
78 to: "/settings/general",
79 },
80 {
81 id: "assistant-output",
82 title: "Assistant output",
83 to: "/settings/general",
84 },
85 {
86 id: "provider-update-checks",
87 title: "Provider update checks",
88 to: "/settings/general",
89 },
90 {
91 id: "auto-open-task-panel",
92 title: "Auto-open task panel",
93 to: "/settings/general",
94 },
95 {
96 id: "new-threads",
97 title: "New threads",
98 to: "/settings/general",
99 },
100 {
101 id: "start-from-origin",
102 title: "Start from origin",
103 to: "/settings/general",
104 targetId: "new-threads",
105 },
106 {
107 id: "add-project-starts-in",
108 title: "Add project starts in",
109 to: "/settings/general",
110 },
111 {
112 id: "archive-confirmation",
113 title: "Archive confirmation",
114 to: "/settings/general",
115 },
116 {
117 id: "delete-confirmation",
118 title: "Delete confirmation",
119 to: "/settings/general",
120 },
121 {
122 id: "text-generation-model",
123 title: "Text generation model",
124 to: "/settings/general",
125 },
126 {
127 id: "diagnostics",
128 title: "Diagnostics",
129 to: "/settings/general",
130 },
131 {
132 id: "keybindings",
133 title: "Keybindings",
134 to: "/settings/keybindings",
135 },
136 {
137 id: "providers",
138 title: "Providers",
139 to: "/settings/providers",
140 },
141 {
142 id: "mcp",
143 title: "MCP servers",
144 to: "/settings/mcp",
145 },
146 {
147 id: "source-control",
148 title: "Source control",
149 to: "/settings/source-control",
150 },
151 {
152 id: "remote-environments",
153 title: "Remote environments",
154 to: "/settings/connections",
155 },
156 {
157 id: "sidebar-v2",
158 title: "Sidebar v2",
159 to: "/settings/beta",
160 },
161 {
162 id: "auto-settle-inactive-threads",
163 title: "Auto-settle inactive threads",
164 to: "/settings/beta",
165 targetId: "sidebar-v2",
166 },
167 {
168 id: "archive",
169 title: "Archived threads",
170 to: "/settings/archived",
171 },
172] as const satisfies ReadonlyArray<SettingsSearchItem>;
173​
174export type SettingsSearchItemId = (typeof SETTINGS_SEARCH_ITEMS)[number]["id"];
175​
176const SEARCH_ITEMS_BY_ID = Object.fromEntries(
177 SETTINGS_SEARCH_ITEMS.map((item) => [item.id, item]),
178) as Readonly<Record<SettingsSearchItemId, SettingsSearchItem>>;
179​
180/**
181 * `id` and `title` props for the element a search item anchors to. Panels
182 * spread (or pick from) this instead of restating the strings, so the catalog
183 * and the rendered settings cannot drift apart.
184 */
185export function searchableSetting(id: SettingsSearchItemId): {
186 readonly id: string;
187 readonly title: string;
188} {
189 const { id: anchorId, title } = SEARCH_ITEMS_BY_ID[id];
190 return { id: anchorId, title };
192​
193function normalizeSearchText(value: string): string {
194 return value
195 .normalize("NFKD")
196 .replace(/[\u0300-\u036f]/g, "")
197 .toLocaleLowerCase()
198 .replace(/\s+/g, " ")
199 .trim();
201​
202export function searchSettings(
203 query: string,
204 items: ReadonlyArray<SettingsSearchItem> = SETTINGS_SEARCH_ITEMS,
205): ReadonlyArray<SettingsSearchItem> {
206 const normalizedQuery = normalizeSearchText(query);
207 if (normalizedQuery.length === 0) return [];
208​
209 return items.filter((item) => normalizeSearchText(item.title).includes(normalizedQuery));