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

settingsSearch.ts

235 lines5.8 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: "time-format",
108 title: "Time format",
109 to: "/settings/general",
110 },
111 {
112 id: "hide-whitespace-changes",
113 title: "Hide whitespace changes",
114 to: "/settings/general",
115 },
116 {
117 id: "provider-update-checks",
118 title: "Provider update checks",
119 to: "/settings/general",
120 },
121 {
122 id: "new-threads",
123 title: "New threads",
124 to: "/settings/general",
125 },
126 {
127 id: "start-from-origin",
128 title: "Start from origin",
129 to: "/settings/general",
130 targetId: "new-threads",
131 },
132 {
133 id: "add-project-starts-in",
134 title: "Add project starts in",
135 to: "/settings/general",
136 },
137 {
138 id: "archive-confirmation",
139 title: "Archive confirmation",
140 to: "/settings/general",
141 },
142 {
143 id: "delete-confirmation",
144 title: "Delete confirmation",
145 to: "/settings/general",
146 },
147 {
148 id: "text-generation-model",
149 title: "Text generation model",
150 to: "/settings/general",
151 },
152 {
153 id: "diagnostics",
154 title: "Diagnostics",
155 to: "/settings/general",
156 },
157 {
158 id: "legacy-plan-mode",
159 title: "Plan mode (legacy)",
160 to: "/settings/general",
161 },
162 {
163 id: "legacy-token-streaming",
164 title: "Stream token by token (legacy)",
165 to: "/settings/general",
166 },
167 {
168 id: "legacy-sidebar",
169 title: "Sidebar (legacy)",
170 to: "/settings/general",
171 },
172 {
173 id: "keybindings",
174 title: "Keybindings",
175 to: "/settings/keybindings",
176 },
177 {
178 id: "providers",
179 title: "Providers",
180 to: "/settings/providers",
181 },
182 {
183 id: "source-control",
184 title: "Source control",
185 to: "/settings/source-control",
186 },
187 {
188 id: "remote-environments",
189 title: "Remote environments",
190 to: "/settings/connections",
191 },
192 {
193 id: "archive",
194 title: "Archived threads",
195 to: "/settings/archived",
196 },
197] as const satisfies ReadonlyArray<SettingsSearchItem>;
198​
199export type SettingsSearchItemId = (typeof SETTINGS_SEARCH_ITEMS)[number]["id"];
200​
201const SEARCH_ITEMS_BY_ID = Object.fromEntries(
202 SETTINGS_SEARCH_ITEMS.map((item) => [item.id, item]),
203) as Readonly<Record<SettingsSearchItemId, SettingsSearchItem>>;
204​
205/**
206 * `id` and `title` props for the element a search item anchors to. Panels
207 * spread (or pick from) this instead of restating the strings, so the catalog
208 * and the rendered settings cannot drift apart.
209 */
210export function searchableSetting(id: SettingsSearchItemId): {
211 readonly id: string;
212 readonly title: string;
213} {
214 const { id: anchorId, title } = SEARCH_ITEMS_BY_ID[id];
215 return { id: anchorId, title };
217​
218function normalizeSearchText(value: string): string {
219 return value
220 .normalize("NFKD")
221 .replace(/[\u0300-\u036f]/g, "")
222 .toLocaleLowerCase()
223 .replace(/\s+/g, " ")
224 .trim();
226​
227export function searchSettings(
228 query: string,
229 items: ReadonlyArray<SettingsSearchItem> = SETTINGS_SEARCH_ITEMS,
230): ReadonlyArray<SettingsSearchItem> {
231 const normalizedQuery = normalizeSearchText(query);
232 if (normalizedQuery.length === 0) return [];
233​
234 return items.filter((item) => normalizeSearchText(item.title).includes(normalizedQuery));