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

settingsSearch.ts

240 lines5.9 KB

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